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