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