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