xref: /freebsd/sys/dev/acpica/acpi_pci.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
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 #ifdef PCI_IOV
88 static device_t	acpi_pci_create_iov_child(device_t bus, device_t pf,
89 		    uint16_t rid, uint16_t vid, uint16_t did);
90 #endif
91 
92 static device_method_t acpi_pci_methods[] = {
93 	/* Device interface */
94 	DEVMETHOD(device_probe,		acpi_pci_probe),
95 	DEVMETHOD(device_attach,	acpi_pci_attach),
96 
97 	/* Bus interface */
98 	DEVMETHOD(bus_read_ivar,	acpi_pci_read_ivar),
99 	DEVMETHOD(bus_write_ivar,	acpi_pci_write_ivar),
100 	DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
101 	DEVMETHOD(bus_get_dma_tag,	acpi_pci_get_dma_tag),
102 	DEVMETHOD(bus_get_domain,	acpi_get_domain),
103 
104 	/* PCI interface */
105 	DEVMETHOD(pci_set_powerstate,	acpi_pci_set_powerstate_method),
106 #ifdef PCI_IOV
107 	DEVMETHOD(pci_create_iov_child,	acpi_pci_create_iov_child),
108 #endif
109 
110 	DEVMETHOD_END
111 };
112 
113 static devclass_t pci_devclass;
114 
115 DEFINE_CLASS_1(pci, acpi_pci_driver, acpi_pci_methods, sizeof(struct pci_softc),
116     pci_driver);
117 DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
118 MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
119 MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
120 MODULE_VERSION(acpi_pci, 1);
121 
122 static int
123 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
124 {
125     struct acpi_pci_devinfo *dinfo;
126 
127     dinfo = device_get_ivars(child);
128     switch (which) {
129     case ACPI_IVAR_HANDLE:
130 	*result = (uintptr_t)dinfo->ap_handle;
131 	return (0);
132     case ACPI_IVAR_FLAGS:
133 	*result = (uintptr_t)dinfo->ap_flags;
134 	return (0);
135     }
136     return (pci_read_ivar(dev, child, which, result));
137 }
138 
139 static int
140 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
141 {
142     struct acpi_pci_devinfo *dinfo;
143 
144     dinfo = device_get_ivars(child);
145     switch (which) {
146     case ACPI_IVAR_HANDLE:
147 	dinfo->ap_handle = (ACPI_HANDLE)value;
148 	return (0);
149     case ACPI_IVAR_FLAGS:
150 	dinfo->ap_flags = (int)value;
151 	return (0);
152     }
153     return (pci_write_ivar(dev, child, which, value));
154 }
155 
156 static int
157 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
158     size_t buflen)
159 {
160     struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
161     int pxm;
162     char buf2[32];
163 
164     pci_child_location_str_method(cbdev, child, buf, buflen);
165 
166     if (dinfo->ap_handle) {
167         strlcat(buf, " handle=", buflen);
168         strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
169 
170         if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) {
171                 snprintf(buf2, 32, " _PXM=%d", pxm);
172                 strlcat(buf, buf2, buflen);
173         }
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 old_state, error;
187 
188 	error = 0;
189 	if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
190 		return (EINVAL);
191 
192 	/*
193 	 * We set the state using PCI Power Management outside of setting
194 	 * the ACPI state.  This means that when powering down a device, we
195 	 * first shut it down using PCI, and then using ACPI, which lets ACPI
196 	 * try to power down any Power Resources that are now no longer used.
197 	 * When powering up a device, we let ACPI set the state first so that
198 	 * it can enable any needed Power Resources before changing the PCI
199 	 * power state.
200 	 */
201 	ACPI_SERIAL_BEGIN(pci_powerstate);
202 	old_state = pci_get_powerstate(child);
203 	if (old_state < state && pci_do_power_suspend) {
204 		error = pci_set_powerstate_method(dev, child, state);
205 		if (error)
206 			goto out;
207 	}
208 	h = acpi_get_handle(child);
209 	status = acpi_pwr_switch_consumer(h, state);
210 	if (ACPI_SUCCESS(status)) {
211 		if (bootverbose)
212 			device_printf(dev, "set ACPI power state D%d on %s\n",
213 			    state, acpi_name(h));
214 	} else if (status != AE_NOT_FOUND)
215 		device_printf(dev,
216 		    "failed to set ACPI power state D%d on %s: %s\n",
217 		    state, acpi_name(h), AcpiFormatException(status));
218 	if (old_state > state && pci_do_power_resume)
219 		error = pci_set_powerstate_method(dev, child, state);
220 
221 out:
222 	ACPI_SERIAL_END(pci_powerstate);
223 	return (error);
224 }
225 
226 static void
227 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
228 {
229 	ACPI_STATUS status;
230 	device_t child;
231 
232 	/*
233 	 * Occasionally a PCI device may show up as an ACPI device
234 	 * with a _HID.  (For example, the TabletPC TC1000 has a
235 	 * second PCI-ISA bridge that has a _HID for an
236 	 * acpi_sysresource device.)  In that case, leave ACPI-CA's
237 	 * device data pointing at the ACPI-enumerated device.
238 	 */
239 	child = acpi_get_device(handle);
240 	if (child != NULL) {
241 		KASSERT(device_get_parent(child) ==
242 		    devclass_get_device(devclass_find("acpi"), 0),
243 		    ("%s: child (%s)'s parent is not acpi0", __func__,
244 		    acpi_name(handle)));
245 		return;
246 	}
247 
248 	/*
249 	 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
250 	 */
251 	status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
252 	if (ACPI_FAILURE(status))
253 		printf("WARNING: Unable to attach object data to %s - %s\n",
254 		    acpi_name(handle), AcpiFormatException(status));
255 }
256 
257 static ACPI_STATUS
258 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
259     void **status)
260 {
261 	struct acpi_pci_devinfo *dinfo;
262 	device_t *devlist;
263 	int devcount, i, func, slot;
264 	UINT32 address;
265 
266 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
267 
268 	if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
269 		return_ACPI_STATUS (AE_OK);
270 	slot = ACPI_ADR_PCI_SLOT(address);
271 	func = ACPI_ADR_PCI_FUNC(address);
272 	if (device_get_children((device_t)context, &devlist, &devcount) != 0)
273 		return_ACPI_STATUS (AE_OK);
274 	for (i = 0; i < devcount; i++) {
275 		dinfo = device_get_ivars(devlist[i]);
276 		if (dinfo->ap_dinfo.cfg.func == func &&
277 		    dinfo->ap_dinfo.cfg.slot == slot) {
278 			dinfo->ap_handle = handle;
279 			acpi_pci_update_device(handle, devlist[i]);
280 			break;
281 		}
282 	}
283 	free(devlist, M_TEMP);
284 	return_ACPI_STATUS (AE_OK);
285 }
286 
287 static int
288 acpi_pci_probe(device_t dev)
289 {
290 
291 	if (acpi_get_handle(dev) == NULL)
292 		return (ENXIO);
293 	device_set_desc(dev, "ACPI PCI bus");
294 	return (BUS_PROBE_DEFAULT);
295 }
296 
297 static int
298 acpi_pci_attach(device_t dev)
299 {
300 	int busno, domain, error;
301 
302 	error = pci_attach_common(dev);
303 	if (error)
304 		return (error);
305 
306 	/*
307 	 * Since there can be multiple independantly numbered PCI
308 	 * busses on systems with multiple PCI domains, we can't use
309 	 * the unit number to decide which bus we are probing. We ask
310 	 * the parent pcib what our domain and bus numbers are.
311 	 */
312 	domain = pcib_get_domain(dev);
313 	busno = pcib_get_bus(dev);
314 
315 	/*
316 	 * First, PCI devices are added as in the normal PCI bus driver.
317 	 * Afterwards, the ACPI namespace under the bridge driver is
318 	 * walked to save ACPI handles to all the devices that appear in
319 	 * the ACPI namespace as immediate descendants of the bridge.
320 	 *
321 	 * XXX: Sometimes PCI devices show up in the ACPI namespace that
322 	 * pci_add_children() doesn't find.  We currently just ignore
323 	 * these devices.
324 	 */
325 	pci_add_children(dev, domain, busno, sizeof(struct acpi_pci_devinfo));
326 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
327 	    acpi_pci_save_handle, NULL, dev, NULL);
328 
329 	return (bus_generic_attach(dev));
330 }
331 
332 #ifdef ACPI_DMAR
333 bus_dma_tag_t dmar_get_dma_tag(device_t dev, device_t child);
334 static bus_dma_tag_t
335 acpi_pci_get_dma_tag(device_t bus, device_t child)
336 {
337 	bus_dma_tag_t tag;
338 
339 	if (device_get_parent(child) == bus) {
340 		/* try dmar and return if it works */
341 		tag = dmar_get_dma_tag(bus, child);
342 	} else
343 		tag = NULL;
344 	if (tag == NULL)
345 		tag = pci_get_dma_tag(bus, child);
346 	return (tag);
347 }
348 #else
349 static bus_dma_tag_t
350 acpi_pci_get_dma_tag(device_t bus, device_t child)
351 {
352 
353 	return (pci_get_dma_tag(bus, child));
354 }
355 #endif
356 
357 #ifdef PCI_IOV
358 static device_t
359 acpi_pci_create_iov_child(device_t bus, device_t pf, uint16_t rid, uint16_t vid,
360     uint16_t did)
361 {
362 	struct acpi_pci_devinfo *dinfo;
363 	device_t vf;
364 
365 	vf = pci_add_iov_child(bus, pf, sizeof(struct acpi_pci_devinfo), rid,
366 	    vid, did);
367 	if (vf == NULL)
368 		return (NULL);
369 
370 	dinfo = device_get_ivars(vf);
371 	dinfo->ap_handle = NULL;
372 	return (vf);
373 }
374 #endif
375 
376