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