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