#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi_pci_graviton.c,v 1.5 2022/10/15 11:07:38 jmcneill Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/cpu.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pciconf.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_pci.h>
#include <dev/acpi/acpi_mcfg.h>
#include <arm/acpi/acpi_pci_machdep.h>
static int
acpi_pci_graviton_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
{
struct acpi_pci_context *ap = pc->pc_conf_v;
int b, d, f;
pci_decompose_tag(pc, tag, &b, &d, &f);
if (ap->ap_bus == b) {
if (d > 0 || f > 0) {
*data = -1;
return EINVAL;
}
*data = bus_space_read_4(ap->ap_bst, ap->ap_conf_bsh, reg);
return 0;
}
return acpimcfg_conf_read(pc, tag, reg, data);
}
static int
acpi_pci_graviton_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
{
struct acpi_pci_context *ap = pc->pc_conf_v;
int b, d, f;
pci_decompose_tag(pc, tag, &b, &d, &f);
if (ap->ap_bus == b) {
if (d > 0 || f > 0) {
return EINVAL;
}
bus_space_write_4(ap->ap_bst, ap->ap_conf_bsh, reg, data);
return 0;
}
return acpimcfg_conf_write(pc, tag, reg, data);
}
static ACPI_STATUS
acpi_pci_graviton_map(ACPI_HANDLE handle, UINT32 level, void *ctx, void **retval)
{
struct acpi_pci_context *ap = ctx;
struct acpi_resources res;
struct acpi_mem *mem;
ACPI_HANDLE parent;
ACPI_INTEGER seg;
ACPI_STATUS rv;
int error;
rv = AcpiGetParent(handle, &parent);
if (ACPI_FAILURE(rv))
return rv;
rv = acpi_eval_integer(parent, "_SEG", &seg);
if (ACPI_FAILURE(rv))
seg = 0;
if (ap->ap_seg != seg)
return AE_OK;
rv = acpi_resource_parse(ap->ap_dev, handle, "_CRS", &res, &acpi_resource_parse_ops_quiet);
if (ACPI_FAILURE(rv))
return rv;
mem = acpi_res_mem(&res, 0);
if (mem == NULL) {
acpi_resource_cleanup(&res);
return AE_NOT_FOUND;
}
error = bus_space_map(ap->ap_bst, mem->ar_base, mem->ar_length,
BUS_SPACE_MAP_NONPOSTED, &ap->ap_conf_bsh);
if (error != 0)
return AE_NO_MEMORY;
ap->ap_conf_read = acpi_pci_graviton_conf_read;
ap->ap_conf_write = acpi_pci_graviton_conf_write;
return AE_CTRL_TERMINATE;
}
void
acpi_pci_graviton_init(struct acpi_pci_context *ap)
{
ACPI_STATUS rv;
rv = AcpiGetDevices(__UNCONST("AMZN0001"), acpi_pci_graviton_map, ap, NULL);
if (ACPI_FAILURE(rv))
return;
}