xref: /freebsd/sys/dev/acpica/acpi_pci.c (revision e17f5b1d)
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 #include <dev/acpica/acpi_pcivar.h>
46 
47 #include <sys/pciio.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pci_private.h>
51 
52 #include "pcib_if.h"
53 #include "pci_if.h"
54 
55 /* Hooks for the ACPI CA debugging infrastructure. */
56 #define _COMPONENT	ACPI_BUS
57 ACPI_MODULE_NAME("PCI")
58 
59 struct acpi_pci_devinfo {
60 	struct pci_devinfo	ap_dinfo;
61 	ACPI_HANDLE		ap_handle;
62 	int			ap_flags;
63 };
64 
65 ACPI_SERIAL_DECL(pci_powerstate, "ACPI PCI power methods");
66 
67 /* Be sure that ACPI and PCI power states are equivalent. */
68 CTASSERT(ACPI_STATE_D0 == PCI_POWERSTATE_D0);
69 CTASSERT(ACPI_STATE_D1 == PCI_POWERSTATE_D1);
70 CTASSERT(ACPI_STATE_D2 == PCI_POWERSTATE_D2);
71 CTASSERT(ACPI_STATE_D3 == PCI_POWERSTATE_D3);
72 
73 static struct pci_devinfo *acpi_pci_alloc_devinfo(device_t dev);
74 static int	acpi_pci_attach(device_t dev);
75 static void	acpi_pci_child_deleted(device_t dev, device_t child);
76 static int	acpi_pci_child_location_str_method(device_t cbdev,
77 		    device_t child, char *buf, size_t buflen);
78 static int	acpi_pci_detach(device_t dev);
79 static int	acpi_pci_probe(device_t dev);
80 static int	acpi_pci_read_ivar(device_t dev, device_t child, int which,
81 		    uintptr_t *result);
82 static int	acpi_pci_write_ivar(device_t dev, device_t child, int which,
83 		    uintptr_t value);
84 static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
85 		    void *context, void **status);
86 static int	acpi_pci_set_powerstate_method(device_t dev, device_t child,
87 		    int state);
88 static void	acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child);
89 static bus_dma_tag_t acpi_pci_get_dma_tag(device_t bus, device_t child);
90 
91 static device_method_t acpi_pci_methods[] = {
92 	/* Device interface */
93 	DEVMETHOD(device_probe,		acpi_pci_probe),
94 	DEVMETHOD(device_attach,	acpi_pci_attach),
95 	DEVMETHOD(device_detach,	acpi_pci_detach),
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_deleted,	acpi_pci_child_deleted),
101 	DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
102 	DEVMETHOD(bus_get_cpus,		acpi_get_cpus),
103 	DEVMETHOD(bus_get_dma_tag,	acpi_pci_get_dma_tag),
104 	DEVMETHOD(bus_get_domain,	acpi_get_domain),
105 
106 	/* PCI interface */
107 	DEVMETHOD(pci_alloc_devinfo,	acpi_pci_alloc_devinfo),
108 	DEVMETHOD(pci_child_added,	acpi_pci_child_added),
109 	DEVMETHOD(pci_set_powerstate,	acpi_pci_set_powerstate_method),
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 struct pci_devinfo *
124 acpi_pci_alloc_devinfo(device_t dev)
125 {
126 	struct acpi_pci_devinfo *dinfo;
127 
128 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
129 	return (&dinfo->ap_dinfo);
130 }
131 
132 static int
133 acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
134 {
135     struct acpi_pci_devinfo *dinfo;
136 
137     dinfo = device_get_ivars(child);
138     switch (which) {
139     case ACPI_IVAR_HANDLE:
140 	*result = (uintptr_t)dinfo->ap_handle;
141 	return (0);
142     case ACPI_IVAR_FLAGS:
143 	*result = (uintptr_t)dinfo->ap_flags;
144 	return (0);
145     }
146     return (pci_read_ivar(dev, child, which, result));
147 }
148 
149 static int
150 acpi_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
151 {
152     struct acpi_pci_devinfo *dinfo;
153 
154     dinfo = device_get_ivars(child);
155     switch (which) {
156     case ACPI_IVAR_HANDLE:
157 	dinfo->ap_handle = (ACPI_HANDLE)value;
158 	return (0);
159     case ACPI_IVAR_FLAGS:
160 	dinfo->ap_flags = (int)value;
161 	return (0);
162     }
163     return (pci_write_ivar(dev, child, which, value));
164 }
165 
166 static void
167 acpi_pci_child_deleted(device_t dev, device_t child)
168 {
169 	struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
170 
171 	if (acpi_get_device(dinfo->ap_handle) == child)
172 		AcpiDetachData(dinfo->ap_handle, acpi_fake_objhandler);
173 	pci_child_deleted(dev, child);
174 }
175 
176 static int
177 acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
178     size_t buflen)
179 {
180     struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
181     int pxm;
182     char buf2[32];
183 
184     pci_child_location_str_method(cbdev, child, buf, buflen);
185 
186     if (dinfo->ap_handle) {
187         strlcat(buf, " handle=", buflen);
188         strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
189 
190         if (ACPI_SUCCESS(acpi_GetInteger(dinfo->ap_handle, "_PXM", &pxm))) {
191                 snprintf(buf2, 32, " _PXM=%d", pxm);
192                 strlcat(buf, buf2, buflen);
193         }
194     }
195     return (0);
196 }
197 
198 /*
199  * PCI power manangement
200  */
201 static int
202 acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
203 {
204 	ACPI_HANDLE h;
205 	ACPI_STATUS status;
206 	int old_state, error;
207 
208 	error = 0;
209 	if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
210 		return (EINVAL);
211 
212 	/*
213 	 * We set the state using PCI Power Management outside of setting
214 	 * the ACPI state.  This means that when powering down a device, we
215 	 * first shut it down using PCI, and then using ACPI, which lets ACPI
216 	 * try to power down any Power Resources that are now no longer used.
217 	 * When powering up a device, we let ACPI set the state first so that
218 	 * it can enable any needed Power Resources before changing the PCI
219 	 * power state.
220 	 */
221 	ACPI_SERIAL_BEGIN(pci_powerstate);
222 	old_state = pci_get_powerstate(child);
223 	if (old_state < state && pci_do_power_suspend) {
224 		error = pci_set_powerstate_method(dev, child, state);
225 		if (error)
226 			goto out;
227 	}
228 	h = acpi_get_handle(child);
229 	status = acpi_pwr_switch_consumer(h, state);
230 	if (ACPI_SUCCESS(status)) {
231 		if (bootverbose)
232 			device_printf(dev, "set ACPI power state D%d on %s\n",
233 			    state, acpi_name(h));
234 	} else if (status != AE_NOT_FOUND)
235 		device_printf(dev,
236 		    "failed to set ACPI power state D%d on %s: %s\n",
237 		    state, acpi_name(h), AcpiFormatException(status));
238 	if (old_state > state && pci_do_power_resume)
239 		error = pci_set_powerstate_method(dev, child, state);
240 
241 out:
242 	ACPI_SERIAL_END(pci_powerstate);
243 	return (error);
244 }
245 
246 static void
247 acpi_pci_update_device(ACPI_HANDLE handle, device_t pci_child)
248 {
249 	ACPI_STATUS status;
250 	device_t child;
251 
252 	/*
253 	 * Occasionally a PCI device may show up as an ACPI device
254 	 * with a _HID.  (For example, the TabletPC TC1000 has a
255 	 * second PCI-ISA bridge that has a _HID for an
256 	 * acpi_sysresource device.)  In that case, leave ACPI-CA's
257 	 * device data pointing at the ACPI-enumerated device.
258 	 */
259 	child = acpi_get_device(handle);
260 	if (child != NULL) {
261 		KASSERT(device_get_parent(child) ==
262 		    devclass_get_device(devclass_find("acpi"), 0),
263 		    ("%s: child (%s)'s parent is not acpi0", __func__,
264 		    acpi_name(handle)));
265 		return;
266 	}
267 
268 	/*
269 	 * Update ACPI-CA to use the PCI enumerated device_t for this handle.
270 	 */
271 	status = AcpiAttachData(handle, acpi_fake_objhandler, pci_child);
272 	if (ACPI_FAILURE(status))
273 		printf("WARNING: Unable to attach object data to %s - %s\n",
274 		    acpi_name(handle), AcpiFormatException(status));
275 }
276 
277 static ACPI_STATUS
278 acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
279     void **status)
280 {
281 	struct acpi_pci_devinfo *dinfo;
282 	device_t child;
283 	int func, slot;
284 	UINT32 address;
285 
286 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
287 
288 	child = context;
289 	if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
290 		return_ACPI_STATUS (AE_OK);
291 	slot = ACPI_ADR_PCI_SLOT(address);
292 	func = ACPI_ADR_PCI_FUNC(address);
293 	dinfo = device_get_ivars(child);
294 	if (dinfo->ap_dinfo.cfg.func == func &&
295 	    dinfo->ap_dinfo.cfg.slot == slot) {
296 		dinfo->ap_handle = handle;
297 		acpi_pci_update_device(handle, child);
298 		return_ACPI_STATUS (AE_CTRL_TERMINATE);
299 	}
300 	return_ACPI_STATUS (AE_OK);
301 }
302 
303 void
304 acpi_pci_child_added(device_t dev, device_t child)
305 {
306 
307 	/*
308 	 * PCI devices are added via the bus scan in the normal PCI
309 	 * bus driver.  As each device is added, the
310 	 * acpi_pci_child_added() callback walks the ACPI namespace
311 	 * under the bridge driver to save ACPI handles to all the
312 	 * devices that appear in the ACPI namespace as immediate
313 	 * descendants of the bridge.
314 	 *
315 	 * XXX: Sometimes PCI devices show up in the ACPI namespace that
316 	 * pci_add_children() doesn't find.  We currently just ignore
317 	 * these devices.
318 	 */
319 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
320 	    acpi_pci_save_handle, NULL, child, NULL);
321 }
322 
323 static int
324 acpi_pci_probe(device_t dev)
325 {
326 
327 	if (acpi_get_handle(dev) == NULL)
328 		return (ENXIO);
329 	device_set_desc(dev, "ACPI PCI bus");
330 	return (BUS_PROBE_DEFAULT);
331 }
332 
333 static void
334 acpi_pci_bus_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
335 {
336 	device_t dev;
337 
338 	dev = context;
339 
340 	switch (notify) {
341 	case ACPI_NOTIFY_BUS_CHECK:
342 		mtx_lock(&Giant);
343 		BUS_RESCAN(dev);
344 		mtx_unlock(&Giant);
345 		break;
346 	default:
347 		device_printf(dev, "unknown notify %#x\n", notify);
348 		break;
349 	}
350 }
351 
352 static void
353 acpi_pci_device_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
354 {
355 	device_t child, dev;
356 	ACPI_STATUS status;
357 	int error;
358 
359 	dev = context;
360 
361 	switch (notify) {
362 	case ACPI_NOTIFY_DEVICE_CHECK:
363 		mtx_lock(&Giant);
364 		BUS_RESCAN(dev);
365 		mtx_unlock(&Giant);
366 		break;
367 	case ACPI_NOTIFY_EJECT_REQUEST:
368 		child = acpi_get_device(h);
369 		if (child == NULL) {
370 			device_printf(dev, "no device to eject for %s\n",
371 			    acpi_name(h));
372 			return;
373 		}
374 		mtx_lock(&Giant);
375 		error = device_detach(child);
376 		if (error) {
377 			mtx_unlock(&Giant);
378 			device_printf(dev, "failed to detach %s: %d\n",
379 			    device_get_nameunit(child), error);
380 			return;
381 		}
382 		status = acpi_SetInteger(h, "_EJ0", 1);
383 		if (ACPI_FAILURE(status)) {
384 			mtx_unlock(&Giant);
385 			device_printf(dev, "failed to eject %s: %s\n",
386 			    acpi_name(h), AcpiFormatException(status));
387 			return;
388 		}
389 		BUS_RESCAN(dev);
390 		mtx_unlock(&Giant);
391 		break;
392 	default:
393 		device_printf(dev, "unknown notify %#x for %s\n", notify,
394 		    acpi_name(h));
395 		break;
396 	}
397 }
398 
399 static ACPI_STATUS
400 acpi_pci_install_device_notify_handler(ACPI_HANDLE handle, UINT32 level,
401     void *context, void **status)
402 {
403 	ACPI_HANDLE h;
404 
405 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
406 
407 	if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h)))
408 		return_ACPI_STATUS (AE_OK);
409 
410 	AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
411 	    acpi_pci_device_notify_handler, context);
412 	return_ACPI_STATUS (AE_OK);
413 }
414 
415 static int
416 acpi_pci_attach(device_t dev)
417 {
418 	int error;
419 
420 	error = pci_attach(dev);
421 	if (error)
422 		return (error);
423 	AcpiInstallNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY,
424 	    acpi_pci_bus_notify_handler, dev);
425 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
426 	    acpi_pci_install_device_notify_handler, NULL, dev, NULL);
427 
428 	return (0);
429 }
430 
431 static ACPI_STATUS
432 acpi_pci_remove_notify_handler(ACPI_HANDLE handle, UINT32 level, void *context,
433     void **status)
434 {
435 	ACPI_HANDLE h;
436 
437 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
438 
439 	if (ACPI_FAILURE(AcpiGetHandle(handle, "_EJ0", &h)))
440 		return_ACPI_STATUS (AE_OK);
441 
442 	AcpiRemoveNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
443 	    acpi_pci_device_notify_handler);
444 	return_ACPI_STATUS (AE_OK);
445 }
446 
447 static int
448 acpi_pci_detach(device_t dev)
449 {
450 
451 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
452 	    acpi_pci_remove_notify_handler, NULL, dev, NULL);
453 	AcpiRemoveNotifyHandler(acpi_get_handle(dev), ACPI_SYSTEM_NOTIFY,
454 	    acpi_pci_bus_notify_handler);
455 	return (pci_detach(dev));
456 }
457 
458 #ifdef ACPI_DMAR
459 bus_dma_tag_t acpi_iommu_get_dma_tag(device_t dev, device_t child);
460 static bus_dma_tag_t
461 acpi_pci_get_dma_tag(device_t bus, device_t child)
462 {
463 	bus_dma_tag_t tag;
464 
465 	if (device_get_parent(child) == bus) {
466 		/* try iommu and return if it works */
467 		tag = acpi_iommu_get_dma_tag(bus, child);
468 	} else
469 		tag = NULL;
470 	if (tag == NULL)
471 		tag = pci_get_dma_tag(bus, child);
472 	return (tag);
473 }
474 #else
475 static bus_dma_tag_t
476 acpi_pci_get_dma_tag(device_t bus, device_t child)
477 {
478 
479 	return (pci_get_dma_tag(bus, child));
480 }
481 #endif
482 
483