xref: /netbsd-src/sys/arch/arm/acpi/acpi_iort.c (revision 172ad57d3874bb19819821469d3c552770812007)
1*172ad57dSjmcneill /* $NetBSD: acpi_iort.c,v 1.5 2024/12/09 21:56:19 jmcneill Exp $ */
2e23c320eSjmcneill 
3e23c320eSjmcneill /*-
4e23c320eSjmcneill  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5e23c320eSjmcneill  * All rights reserved.
6e23c320eSjmcneill  *
7e23c320eSjmcneill  * This code is derived from software contributed to The NetBSD Foundation
8e23c320eSjmcneill  * by Jared McNeill <jmcneill@invisible.ca>.
9e23c320eSjmcneill  *
10e23c320eSjmcneill  * Redistribution and use in source and binary forms, with or without
11e23c320eSjmcneill  * modification, are permitted provided that the following conditions
12e23c320eSjmcneill  * are met:
13e23c320eSjmcneill  * 1. Redistributions of source code must retain the above copyright
14e23c320eSjmcneill  *    notice, this list of conditions and the following disclaimer.
15e23c320eSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
16e23c320eSjmcneill  *    notice, this list of conditions and the following disclaimer in the
17e23c320eSjmcneill  *    documentation and/or other materials provided with the distribution.
18e23c320eSjmcneill  *
19e23c320eSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20e23c320eSjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21e23c320eSjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22e23c320eSjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23e23c320eSjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24e23c320eSjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25e23c320eSjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26e23c320eSjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27e23c320eSjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28e23c320eSjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29e23c320eSjmcneill  * POSSIBILITY OF SUCH DAMAGE.
30e23c320eSjmcneill  */
31e23c320eSjmcneill 
32e23c320eSjmcneill #include <sys/cdefs.h>
33*172ad57dSjmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_iort.c,v 1.5 2024/12/09 21:56:19 jmcneill Exp $");
34e23c320eSjmcneill 
35e23c320eSjmcneill #include <sys/param.h>
36e23c320eSjmcneill #include <sys/bus.h>
37e23c320eSjmcneill #include <sys/cpu.h>
38e23c320eSjmcneill 
39e23c320eSjmcneill #include <dev/acpi/acpireg.h>
40e23c320eSjmcneill #include <dev/acpi/acpivar.h>
41e23c320eSjmcneill 
42e23c320eSjmcneill #include <arm/acpi/acpi_iort.h>
43e23c320eSjmcneill 
44e23c320eSjmcneill static ACPI_IORT_ID_MAPPING *
45e23c320eSjmcneill acpi_iort_id_map(ACPI_IORT_NODE *node, uint32_t *id)
46e23c320eSjmcneill {
47e23c320eSjmcneill 	ACPI_IORT_ID_MAPPING *map;
48e23c320eSjmcneill 	uint32_t offset, n;
49e23c320eSjmcneill 
50e4736b55Sjmcneill 	offset = le32toh(node->MappingOffset);
51e4736b55Sjmcneill 	for (n = 0; n < le32toh(node->MappingCount); n++) {
52e23c320eSjmcneill 		map = ACPI_ADD_PTR(ACPI_IORT_ID_MAPPING, node, offset);
53e4736b55Sjmcneill 		if (le32toh(map->Flags) & ACPI_IORT_ID_SINGLE_MAPPING) {
54e4736b55Sjmcneill 			*id = le32toh(map->OutputBase);
55e23c320eSjmcneill 			return map;
56e23c320eSjmcneill 		}
57e4736b55Sjmcneill 		if (*id >= le32toh(map->InputBase) && *id <= le32toh(map->InputBase) + le32toh(map->IdCount)) {
58e4736b55Sjmcneill 			*id = *id - le32toh(map->InputBase) + le32toh(map->OutputBase);
59e23c320eSjmcneill 			return map;
60e23c320eSjmcneill 		}
61e23c320eSjmcneill 		offset += sizeof(ACPI_IORT_ID_MAPPING);
62e23c320eSjmcneill 	}
63e23c320eSjmcneill 
64e23c320eSjmcneill 	return NULL;
65e23c320eSjmcneill }
66e23c320eSjmcneill 
67e23c320eSjmcneill static ACPI_IORT_NODE *
68e23c320eSjmcneill acpi_iort_find_ref(ACPI_TABLE_IORT *iort, ACPI_IORT_NODE *node, uint32_t *id)
69e23c320eSjmcneill {
70e23c320eSjmcneill 	ACPI_IORT_ID_MAPPING *map;
71e23c320eSjmcneill 
72e23c320eSjmcneill 	map = acpi_iort_id_map(node, id);
73e23c320eSjmcneill 	if (map == NULL)
74e23c320eSjmcneill 		return NULL;
75e23c320eSjmcneill 
76e4736b55Sjmcneill 	return ACPI_ADD_PTR(ACPI_IORT_NODE, iort, le32toh(map->OutputReference));
77e23c320eSjmcneill }
78e23c320eSjmcneill 
79e23c320eSjmcneill uint32_t
80e23c320eSjmcneill acpi_iort_pci_root_map(u_int seg, uint32_t devid)
81e23c320eSjmcneill {
82e23c320eSjmcneill 	ACPI_TABLE_IORT *iort;
83e23c320eSjmcneill 	ACPI_IORT_NODE *node;
84e23c320eSjmcneill 	ACPI_IORT_ROOT_COMPLEX *root;
85e23c320eSjmcneill 	uint32_t offset, n;
86e23c320eSjmcneill 	ACPI_STATUS rv;
87e23c320eSjmcneill 
88e23c320eSjmcneill 	rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
89e23c320eSjmcneill 	if (ACPI_FAILURE(rv))
90e23c320eSjmcneill 		return devid;
91e23c320eSjmcneill 
92e4736b55Sjmcneill 	offset = le32toh(iort->NodeOffset);
93e4736b55Sjmcneill 	for (n = 0; n < le32toh(iort->NodeCount); n++) {
94e23c320eSjmcneill 		node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
95e23c320eSjmcneill 		if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
96e23c320eSjmcneill 			root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
97e4736b55Sjmcneill 			if (le32toh(root->PciSegmentNumber) == seg) {
98e23c320eSjmcneill 				const uint32_t odevid = devid;
99e23c320eSjmcneill 				do {
100e23c320eSjmcneill 					node = acpi_iort_find_ref(iort, node, &devid);
101e23c320eSjmcneill 				} while (node != NULL);
1028b7fb0a6Sjmcneill 				aprint_debug("ACPI: IORT mapped devid %#x -> devid %#x\n", odevid, devid);
103e23c320eSjmcneill 				return devid;
104e23c320eSjmcneill 			}
105e23c320eSjmcneill 		}
106e4736b55Sjmcneill 		offset += le16toh(node->Length);
107e23c320eSjmcneill 	}
108e23c320eSjmcneill 
109e23c320eSjmcneill 	return devid;
110e23c320eSjmcneill }
1118b7fb0a6Sjmcneill 
1128b7fb0a6Sjmcneill uint32_t
1138b7fb0a6Sjmcneill acpi_iort_its_id_map(u_int seg, uint32_t devid)
1148b7fb0a6Sjmcneill {
1158b7fb0a6Sjmcneill 	ACPI_TABLE_IORT *iort;
1168b7fb0a6Sjmcneill 	ACPI_IORT_NODE *node;
1178b7fb0a6Sjmcneill 	ACPI_IORT_ROOT_COMPLEX *root;
1188b7fb0a6Sjmcneill 	ACPI_IORT_ITS_GROUP *its_group;
1198b7fb0a6Sjmcneill 	uint32_t offset, n;
1208b7fb0a6Sjmcneill 	ACPI_STATUS rv;
1218b7fb0a6Sjmcneill 
1228b7fb0a6Sjmcneill 	rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
1238b7fb0a6Sjmcneill 	if (ACPI_FAILURE(rv))
1248b7fb0a6Sjmcneill 		return 0;
1258b7fb0a6Sjmcneill 
126e4736b55Sjmcneill 	offset = le32toh(iort->NodeOffset);
127e4736b55Sjmcneill 	for (n = 0; n < le32toh(iort->NodeCount); n++) {
1288b7fb0a6Sjmcneill 		node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
1298b7fb0a6Sjmcneill 		if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1308b7fb0a6Sjmcneill 			root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
131e4736b55Sjmcneill 			if (le32toh(root->PciSegmentNumber) == seg) {
1328b7fb0a6Sjmcneill 				const uint32_t odevid = devid;
1338b7fb0a6Sjmcneill 				do {
1348b7fb0a6Sjmcneill 					node = acpi_iort_find_ref(iort, node, &devid);
1358b7fb0a6Sjmcneill 					if (node != NULL && node->Type == ACPI_IORT_NODE_ITS_GROUP) {
1368b7fb0a6Sjmcneill 						its_group = (ACPI_IORT_ITS_GROUP *)node->NodeData;
137e4736b55Sjmcneill 						if (le32toh(its_group->ItsCount) == 0)
1388b7fb0a6Sjmcneill 							return 0;
1398b7fb0a6Sjmcneill 						aprint_debug("ACPI: IORT mapped devid %#x -> ITS %#x\n",
140e4736b55Sjmcneill 						    odevid, le32toh(its_group->Identifiers[0]));
141e4736b55Sjmcneill 						return le32toh(its_group->Identifiers[0]);
1428b7fb0a6Sjmcneill 					}
1438b7fb0a6Sjmcneill 				} while (node != NULL);
1448b7fb0a6Sjmcneill 				return 0;
1458b7fb0a6Sjmcneill 			}
1468b7fb0a6Sjmcneill 		}
147e4736b55Sjmcneill 		offset += le16toh(node->Length);
1488b7fb0a6Sjmcneill 	}
1498b7fb0a6Sjmcneill 
1508b7fb0a6Sjmcneill 	return 0;
1518b7fb0a6Sjmcneill }
152*172ad57dSjmcneill 
153*172ad57dSjmcneill ACPI_STATUS
154*172ad57dSjmcneill acpi_iort_named_component(ACPI_HANDLE handle,
155*172ad57dSjmcneill     ACPI_IORT_NAMED_COMPONENT **out)
156*172ad57dSjmcneill {
157*172ad57dSjmcneill 	ACPI_TABLE_IORT *iort;
158*172ad57dSjmcneill 	ACPI_IORT_NODE *node;
159*172ad57dSjmcneill 	ACPI_IORT_NAMED_COMPONENT *nc;
160*172ad57dSjmcneill 	ACPI_HANDLE nch;
161*172ad57dSjmcneill 	uint32_t offset, n;
162*172ad57dSjmcneill 	ACPI_STATUS rv;
163*172ad57dSjmcneill 
164*172ad57dSjmcneill 	rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
165*172ad57dSjmcneill 	if (ACPI_FAILURE(rv)) {
166*172ad57dSjmcneill 		return rv;
167*172ad57dSjmcneill 	}
168*172ad57dSjmcneill 
169*172ad57dSjmcneill 	offset = iort->NodeOffset;
170*172ad57dSjmcneill 	for (n = 0; n < iort->NodeCount; n++) {
171*172ad57dSjmcneill 		node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
172*172ad57dSjmcneill 		if (node->Type == ACPI_IORT_NODE_NAMED_COMPONENT) {
173*172ad57dSjmcneill 			nc = (ACPI_IORT_NAMED_COMPONENT *)node->NodeData;
174*172ad57dSjmcneill 			rv = AcpiGetHandle(NULL, nc->DeviceName, &nch);
175*172ad57dSjmcneill 			if (rv == AE_OK && nch == handle) {
176*172ad57dSjmcneill 				*out = nc;
177*172ad57dSjmcneill 				return AE_OK;
178*172ad57dSjmcneill 			}
179*172ad57dSjmcneill 		}
180*172ad57dSjmcneill 		offset += le16toh(node->Length);
181*172ad57dSjmcneill 	}
182*172ad57dSjmcneill 
183*172ad57dSjmcneill 	return AE_NOT_FOUND;
184*172ad57dSjmcneill }
185