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