#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi_iort.c,v 1.5 2024/12/09 21:56:19 jmcneill Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <arm/acpi/acpi_iort.h>
static ACPI_IORT_ID_MAPPING *
acpi_iort_id_map(ACPI_IORT_NODE *node, uint32_t *id)
{
ACPI_IORT_ID_MAPPING *map;
uint32_t offset, n;
offset = le32toh(node->MappingOffset);
for (n = 0; n < le32toh(node->MappingCount); n++) {
map = ACPI_ADD_PTR(ACPI_IORT_ID_MAPPING, node, offset);
if (le32toh(map->Flags) & ACPI_IORT_ID_SINGLE_MAPPING) {
*id = le32toh(map->OutputBase);
return map;
}
if (*id >= le32toh(map->InputBase) && *id <= le32toh(map->InputBase) + le32toh(map->IdCount)) {
*id = *id - le32toh(map->InputBase) + le32toh(map->OutputBase);
return map;
}
offset += sizeof(ACPI_IORT_ID_MAPPING);
}
return NULL;
}
static ACPI_IORT_NODE *
acpi_iort_find_ref(ACPI_TABLE_IORT *iort, ACPI_IORT_NODE *node, uint32_t *id)
{
ACPI_IORT_ID_MAPPING *map;
map = acpi_iort_id_map(node, id);
if (map == NULL)
return NULL;
return ACPI_ADD_PTR(ACPI_IORT_NODE, iort, le32toh(map->OutputReference));
}
uint32_t
acpi_iort_pci_root_map(u_int seg, uint32_t devid)
{
ACPI_TABLE_IORT *iort;
ACPI_IORT_NODE *node;
ACPI_IORT_ROOT_COMPLEX *root;
uint32_t offset, n;
ACPI_STATUS rv;
rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
if (ACPI_FAILURE(rv))
return devid;
offset = le32toh(iort->NodeOffset);
for (n = 0; n < le32toh(iort->NodeCount); n++) {
node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
if (le32toh(root->PciSegmentNumber) == seg) {
const uint32_t odevid = devid;
do {
node = acpi_iort_find_ref(iort, node, &devid);
} while (node != NULL);
aprint_debug("ACPI: IORT mapped devid %#x -> devid %#x\n", odevid, devid);
return devid;
}
}
offset += le16toh(node->Length);
}
return devid;
}
uint32_t
acpi_iort_its_id_map(u_int seg, uint32_t devid)
{
ACPI_TABLE_IORT *iort;
ACPI_IORT_NODE *node;
ACPI_IORT_ROOT_COMPLEX *root;
ACPI_IORT_ITS_GROUP *its_group;
uint32_t offset, n;
ACPI_STATUS rv;
rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
if (ACPI_FAILURE(rv))
return 0;
offset = le32toh(iort->NodeOffset);
for (n = 0; n < le32toh(iort->NodeCount); n++) {
node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
if (le32toh(root->PciSegmentNumber) == seg) {
const uint32_t odevid = devid;
do {
node = acpi_iort_find_ref(iort, node, &devid);
if (node != NULL && node->Type == ACPI_IORT_NODE_ITS_GROUP) {
its_group = (ACPI_IORT_ITS_GROUP *)node->NodeData;
if (le32toh(its_group->ItsCount) == 0)
return 0;
aprint_debug("ACPI: IORT mapped devid %#x -> ITS %#x\n",
odevid, le32toh(its_group->Identifiers[0]));
return le32toh(its_group->Identifiers[0]);
}
} while (node != NULL);
return 0;
}
}
offset += le16toh(node->Length);
}
return 0;
}
ACPI_STATUS
acpi_iort_named_component(ACPI_HANDLE handle,
ACPI_IORT_NAMED_COMPONENT **out)
{
ACPI_TABLE_IORT *iort;
ACPI_IORT_NODE *node;
ACPI_IORT_NAMED_COMPONENT *nc;
ACPI_HANDLE nch;
uint32_t offset, n;
ACPI_STATUS rv;
rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
if (ACPI_FAILURE(rv)) {
return rv;
}
offset = iort->NodeOffset;
for (n = 0; n < iort->NodeCount; n++) {
node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
if (node->Type == ACPI_IORT_NODE_NAMED_COMPONENT) {
nc = (ACPI_IORT_NAMED_COMPONENT *)node->NodeData;
rv = AcpiGetHandle(NULL, nc->DeviceName, &nch);
if (rv == AE_OK && nch == handle) {
*out = nc;
return AE_OK;
}
}
offset += le16toh(node->Length);
}
return AE_NOT_FOUND;
}