2002-08-26 17:55:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1997, Stefan Esser <se@freebsd.org>
|
|
|
|
* Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
|
|
|
|
* Copyright (c) 2000, BSDi
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice unmodified, this list of conditions, and the following
|
|
|
|
* disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-08-24 17:55:58 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-08-26 17:55:42 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
|
|
|
|
#include "acpi.h"
|
|
|
|
#include <dev/acpica/acpivar.h>
|
|
|
|
|
|
|
|
#include <sys/pciio.h>
|
|
|
|
#include <dev/pci/pcireg.h>
|
|
|
|
#include <dev/pci/pcivar.h>
|
|
|
|
#include <dev/pci/pci_private.h>
|
|
|
|
|
|
|
|
#include "pcib_if.h"
|
|
|
|
#include "pci_if.h"
|
|
|
|
|
2004-05-29 04:32:50 +00:00
|
|
|
/* Hooks for the ACPI CA debugging infrastructure. */
|
2002-08-26 17:55:42 +00:00
|
|
|
#define _COMPONENT ACPI_BUS
|
|
|
|
ACPI_MODULE_NAME("PCI")
|
|
|
|
|
|
|
|
struct acpi_pci_devinfo {
|
2004-05-29 04:32:50 +00:00
|
|
|
struct pci_devinfo ap_dinfo;
|
|
|
|
ACPI_HANDLE ap_handle;
|
2002-08-26 17:55:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int acpi_pci_probe(device_t dev);
|
|
|
|
static int acpi_pci_attach(device_t dev);
|
|
|
|
static int acpi_pci_read_ivar(device_t dev, device_t child, int which,
|
2004-05-29 04:32:50 +00:00
|
|
|
uintptr_t *result);
|
2004-03-31 17:27:19 +00:00
|
|
|
static int acpi_pci_child_location_str_method(device_t cbdev,
|
2004-05-29 04:32:50 +00:00
|
|
|
device_t child, char *buf, size_t buflen);
|
2004-03-31 17:27:19 +00:00
|
|
|
|
|
|
|
|
2002-08-26 17:55:42 +00:00
|
|
|
static int acpi_pci_set_powerstate_method(device_t dev, device_t child,
|
2004-05-29 04:32:50 +00:00
|
|
|
int state);
|
2002-08-26 17:55:42 +00:00
|
|
|
static ACPI_STATUS acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level,
|
2004-05-29 04:32:50 +00:00
|
|
|
void *context, void **status);
|
2002-08-26 17:55:42 +00:00
|
|
|
|
|
|
|
static device_method_t acpi_pci_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, acpi_pci_probe),
|
|
|
|
DEVMETHOD(device_attach, acpi_pci_attach),
|
|
|
|
DEVMETHOD(device_shutdown, bus_generic_shutdown),
|
2004-04-09 15:44:34 +00:00
|
|
|
DEVMETHOD(device_suspend, pci_suspend),
|
2003-09-17 08:32:44 +00:00
|
|
|
DEVMETHOD(device_resume, pci_resume),
|
2002-08-26 17:55:42 +00:00
|
|
|
|
|
|
|
/* Bus interface */
|
|
|
|
DEVMETHOD(bus_print_child, pci_print_child),
|
|
|
|
DEVMETHOD(bus_probe_nomatch, pci_probe_nomatch),
|
|
|
|
DEVMETHOD(bus_read_ivar, acpi_pci_read_ivar),
|
|
|
|
DEVMETHOD(bus_write_ivar, pci_write_ivar),
|
2004-04-09 15:44:34 +00:00
|
|
|
DEVMETHOD(bus_driver_added, pci_driver_added),
|
2002-08-26 17:55:42 +00:00
|
|
|
DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
|
|
|
|
DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
|
|
|
|
|
|
|
|
DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
|
|
|
|
DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
|
|
|
|
DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
|
|
|
|
DEVMETHOD(bus_delete_resource, pci_delete_resource),
|
|
|
|
DEVMETHOD(bus_alloc_resource, pci_alloc_resource),
|
|
|
|
DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
|
|
|
|
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
|
|
|
|
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
|
2003-02-17 21:20:35 +00:00
|
|
|
DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
|
2004-03-31 17:27:19 +00:00
|
|
|
DEVMETHOD(bus_child_location_str, acpi_pci_child_location_str_method),
|
2002-08-26 17:55:42 +00:00
|
|
|
|
|
|
|
/* PCI interface */
|
|
|
|
DEVMETHOD(pci_read_config, pci_read_config_method),
|
|
|
|
DEVMETHOD(pci_write_config, pci_write_config_method),
|
|
|
|
DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
|
|
|
|
DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
|
|
|
|
DEVMETHOD(pci_enable_io, pci_enable_io_method),
|
|
|
|
DEVMETHOD(pci_disable_io, pci_disable_io_method),
|
|
|
|
DEVMETHOD(pci_get_powerstate, pci_get_powerstate_method),
|
2004-04-07 19:42:21 +00:00
|
|
|
DEVMETHOD(pci_set_powerstate, acpi_pci_set_powerstate_method),
|
2003-07-01 14:08:33 +00:00
|
|
|
DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method),
|
2002-08-26 17:55:42 +00:00
|
|
|
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t acpi_pci_driver = {
|
|
|
|
"pci",
|
|
|
|
acpi_pci_methods,
|
|
|
|
0, /* no softc */
|
|
|
|
};
|
|
|
|
|
|
|
|
DRIVER_MODULE(acpi_pci, pcib, acpi_pci_driver, pci_devclass, 0, 0);
|
2004-04-09 18:14:32 +00:00
|
|
|
MODULE_DEPEND(acpi_pci, acpi, 1, 1, 1);
|
2002-08-26 17:55:42 +00:00
|
|
|
MODULE_DEPEND(acpi_pci, pci, 1, 1, 1);
|
2004-04-09 18:14:32 +00:00
|
|
|
MODULE_VERSION(acpi_pci, 1);
|
2002-08-26 17:55:42 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
|
|
|
|
{
|
|
|
|
struct acpi_pci_devinfo *dinfo;
|
|
|
|
|
|
|
|
switch (which) {
|
2004-03-31 17:35:28 +00:00
|
|
|
case ACPI_IVAR_HANDLE:
|
2002-08-26 17:55:42 +00:00
|
|
|
dinfo = device_get_ivars(child);
|
|
|
|
*result = (uintptr_t)dinfo->ap_handle;
|
2004-03-31 17:35:28 +00:00
|
|
|
return (0);
|
2002-08-26 17:55:42 +00:00
|
|
|
}
|
2004-03-31 17:35:28 +00:00
|
|
|
return (pci_read_ivar(dev, child, which, result));
|
2002-08-26 17:55:42 +00:00
|
|
|
}
|
|
|
|
|
2004-03-31 17:27:19 +00:00
|
|
|
static int
|
|
|
|
acpi_pci_child_location_str_method(device_t cbdev, device_t child, char *buf,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
struct acpi_pci_devinfo *dinfo = device_get_ivars(child);
|
2004-03-31 17:35:28 +00:00
|
|
|
|
2004-03-31 17:27:19 +00:00
|
|
|
pci_child_location_str_method(cbdev, child, buf, buflen);
|
|
|
|
|
2004-03-31 17:35:28 +00:00
|
|
|
if (dinfo->ap_handle) {
|
2004-03-31 17:27:19 +00:00
|
|
|
strlcat(buf, " path=", buflen);
|
|
|
|
strlcat(buf, acpi_name(dinfo->ap_handle), buflen);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-08-26 17:55:42 +00:00
|
|
|
/*
|
|
|
|
* PCI power manangement
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
acpi_pci_set_powerstate_method(device_t dev, device_t child, int state)
|
|
|
|
{
|
2004-04-14 17:46:21 +00:00
|
|
|
ACPI_HANDLE h;
|
2004-04-07 19:42:21 +00:00
|
|
|
ACPI_STATUS status;
|
|
|
|
int acpi_state, old_state, error;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case PCI_POWERSTATE_D0:
|
|
|
|
acpi_state = ACPI_STATE_D0;
|
|
|
|
break;
|
|
|
|
case PCI_POWERSTATE_D1:
|
|
|
|
acpi_state = ACPI_STATE_D1;
|
|
|
|
break;
|
|
|
|
case PCI_POWERSTATE_D2:
|
|
|
|
acpi_state = ACPI_STATE_D2;
|
|
|
|
break;
|
|
|
|
case PCI_POWERSTATE_D3:
|
|
|
|
acpi_state = ACPI_STATE_D3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2002-08-26 17:55:42 +00:00
|
|
|
|
2004-04-07 19:42:21 +00:00
|
|
|
/*
|
|
|
|
* We set the state using PCI Power Management outside of setting
|
|
|
|
* the ACPI state. This means that when powering down a device, we
|
|
|
|
* first shut it down using PCI, and then using ACPI, which lets ACPI
|
|
|
|
* try to power down any Power Resources that are now no longer used.
|
|
|
|
* When powering up a device, we let ACPI set the state first so that
|
|
|
|
* it can enable any needed Power Resources before changing the PCI
|
|
|
|
* power state.
|
|
|
|
*/
|
|
|
|
old_state = pci_get_powerstate(child);
|
|
|
|
if (old_state < state) {
|
|
|
|
error = pci_set_powerstate_method(dev, child, state);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
2004-06-07 21:44:01 +00:00
|
|
|
h = acpi_get_handle(child);
|
2004-06-07 21:39:15 +00:00
|
|
|
status = acpi_pwr_switch_consumer(h, acpi_state);
|
|
|
|
if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
|
|
|
|
device_printf(dev,
|
|
|
|
"Failed to set ACPI power state D%d on %s: %s\n",
|
|
|
|
acpi_state, acpi_name(h), AcpiFormatException(status));
|
2004-04-26 02:11:38 +00:00
|
|
|
if (old_state > state)
|
2004-04-07 19:42:21 +00:00
|
|
|
return (pci_set_powerstate_method(dev, child, state));
|
|
|
|
else
|
|
|
|
return (0);
|
2002-08-26 17:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ACPI_STATUS
|
|
|
|
acpi_pci_save_handle(ACPI_HANDLE handle, UINT32 level, void *context,
|
|
|
|
void **status)
|
|
|
|
{
|
|
|
|
struct acpi_pci_devinfo *dinfo;
|
|
|
|
device_t *devlist;
|
|
|
|
int devcount, i, func, slot;
|
|
|
|
UINT32 address;
|
|
|
|
|
|
|
|
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
|
|
|
|
|
2004-03-03 18:34:42 +00:00
|
|
|
if (ACPI_FAILURE(acpi_GetInteger(handle, "_ADR", &address)))
|
2004-05-29 04:32:50 +00:00
|
|
|
return_ACPI_STATUS (AE_OK);
|
2002-08-26 17:55:42 +00:00
|
|
|
slot = address >> 16;
|
|
|
|
func = address & 0xffff;
|
|
|
|
if (device_get_children((device_t)context, &devlist, &devcount) != 0)
|
2004-05-29 04:32:50 +00:00
|
|
|
return_ACPI_STATUS (AE_OK);
|
2002-08-26 17:55:42 +00:00
|
|
|
for (i = 0; i < devcount; i++) {
|
|
|
|
dinfo = device_get_ivars(devlist[i]);
|
|
|
|
if (dinfo->ap_dinfo.cfg.func == func &&
|
|
|
|
dinfo->ap_dinfo.cfg.slot == slot) {
|
|
|
|
dinfo->ap_handle = handle;
|
|
|
|
free(devlist, M_TEMP);
|
2004-05-29 04:32:50 +00:00
|
|
|
return_ACPI_STATUS (AE_OK);
|
2002-08-26 17:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(devlist, M_TEMP);
|
2004-05-29 04:32:50 +00:00
|
|
|
return_ACPI_STATUS (AE_OK);
|
2002-08-26 17:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_pci_probe(device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (pcib_get_bus(dev) < 0)
|
|
|
|
return (ENXIO);
|
|
|
|
if (acpi_get_handle(dev) == NULL)
|
|
|
|
return (ENXIO);
|
2004-05-29 04:32:50 +00:00
|
|
|
device_set_desc(dev, "ACPI PCI bus");
|
2002-08-26 17:55:42 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
acpi_pci_attach(device_t dev)
|
|
|
|
{
|
|
|
|
int busno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since there can be multiple independantly numbered PCI
|
|
|
|
* busses on some large alpha systems, we can't use the unit
|
|
|
|
* number to decide what bus we are probing. We ask the parent
|
|
|
|
* pcib what our bus number is.
|
|
|
|
*/
|
|
|
|
busno = pcib_get_bus(dev);
|
|
|
|
if (bootverbose)
|
|
|
|
device_printf(dev, "physical bus=%d\n", busno);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, PCI devices are added as in the normal PCI bus driver.
|
|
|
|
* Afterwards, the ACPI namespace under the bridge driver is
|
|
|
|
* walked to save ACPI handles to all the devices that appear in
|
|
|
|
* the ACPI namespace as immediate descendants of the bridge.
|
|
|
|
*
|
|
|
|
* XXX: Sometimes PCI devices show up in the ACPI namespace that
|
|
|
|
* pci_add_children() doesn't find. We currently just ignore
|
|
|
|
* these devices.
|
|
|
|
*/
|
|
|
|
pci_add_children(dev, busno, sizeof(struct acpi_pci_devinfo));
|
2004-05-29 04:32:50 +00:00
|
|
|
AcpiWalkNamespace(ACPI_TYPE_DEVICE, acpi_get_handle(dev), 1,
|
2002-08-26 17:55:42 +00:00
|
|
|
acpi_pci_save_handle, dev, NULL);
|
|
|
|
|
|
|
|
return (bus_generic_attach(dev));
|
|
|
|
}
|