#include "opt_acpi.h"
#include "opt_thunderbolt.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/malloc.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/param.h>
#include <sys/endian.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <machine/stdarg.h>
#include <sys/rman.h>
#include <machine/pci_cfgreg.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcib_private.h>
#include <dev/pci/pci_private.h>
#include <contrib/dev/acpica/include/acpi.h>
#include <contrib/dev/acpica/include/accommon.h>
#include <dev/acpica/acpivar.h>
#include <dev/acpica/acpi_pcibvar.h>
#include <machine/md_var.h>
#include <dev/thunderbolt/tb_reg.h>
#include <dev/thunderbolt/tb_pcib.h>
#include <dev/thunderbolt/nhi_var.h>
#include <dev/thunderbolt/nhi_reg.h>
#include <dev/thunderbolt/tbcfg_reg.h>
#include <dev/thunderbolt/tb_debug.h>
static int tb_acpi_pcib_probe(device_t);
static int tb_acpi_pcib_attach(device_t);
static int tb_acpi_pcib_detach(device_t);
static int
tb_acpi_pcib_probe(device_t dev)
{
char desc[TB_DESC_MAX], desc1[TB_DESC_MAX];
int val;
if (pci_get_class(dev) != PCIC_BRIDGE ||
pci_get_subclass(dev) != PCIS_BRIDGE_PCI ||
acpi_disabled("pci"))
return (ENXIO);
if (acpi_get_handle(dev) == NULL)
return (ENXIO);
if (pci_cfgregopen() == 0)
return (ENXIO);
if ((val = tb_pcib_probe_common(dev, desc)) < 0) {
val++;
snprintf(desc1, TB_DESC_MAX, "ACPI %s", desc);
device_set_desc_copy(dev, desc1);
}
return (val);
}
static int
tb_acpi_pcib_attach(device_t dev)
{
struct tb_pcib_softc *sc;
int error;
error = tb_pcib_attach_common(dev);
if (error)
return (error);
sc = device_get_softc(dev);
sc->ap_handle = acpi_get_handle(dev);
KASSERT(sc->ap_handle != NULL, ("ACPI handle cannot be NULL\n"));
if (TB_IS_ROOT(sc)) {
ACPI_OBJECT_LIST list;
ACPI_OBJECT arg;
ACPI_BUFFER buf;
ACPI_STATUS s;
tb_debug(sc, DBG_BRIDGE, "Executing OSUP\n");
list.Pointer = &arg;
list.Count = 1;
arg.Integer.Value = 0;
arg.Type = ACPI_TYPE_INTEGER;
buf.Length = ACPI_ALLOCATE_BUFFER;
buf.Pointer = NULL;
s = AcpiEvaluateObject(sc->ap_handle, "\\_GPE.OSUP", &list,
&buf);
tb_debug(sc, DBG_BRIDGE|DBG_FULL,
"ACPI returned %d, buf= %p\n", s, buf.Pointer);
if (buf.Pointer != NULL)
tb_debug(sc, DBG_BRIDGE|DBG_FULL, "buffer= 0x%x\n",
*(uint32_t *)buf.Pointer);
AcpiOsFree(buf.Pointer);
}
pcib_attach_common(dev);
acpi_pcib_fetch_prt(dev, &sc->ap_prt);
return (pcib_attach_child(dev));
}
static int
tb_acpi_pcib_detach(device_t dev)
{
struct tb_pcib_softc *sc;
int error;
sc = device_get_softc(dev);
tb_debug(sc, DBG_BRIDGE|DBG_ROUTER|DBG_EXTRA, "tb_acpi_pcib_detach\n");
error = pcib_detach(dev);
if (error == 0)
AcpiOsFree(sc->ap_prt.Pointer);
return (error);
}
static device_method_t tb_acpi_pcib_methods[] = {
DEVMETHOD(device_probe, tb_acpi_pcib_probe),
DEVMETHOD(device_attach, tb_acpi_pcib_attach),
DEVMETHOD(device_detach, tb_acpi_pcib_detach),
DEVMETHOD_END
};
DEFINE_CLASS_2(tbolt, tb_acpi_pcib_driver, tb_acpi_pcib_methods,
sizeof(struct tb_pcib_softc), pcib_driver, tb_pcib_driver);
DRIVER_MODULE_ORDERED(tb_acpi_pcib, pci, tb_acpi_pcib_driver,
NULL, NULL, SI_ORDER_MIDDLE);
MODULE_DEPEND(tb_acpi_pcib, acpi, 1, 1, 1);