1*47847e85Sjmcneill /* $NetBSD: acpi_iort.c,v 1.4 2020/09/13 21:41:17 jmcneill Exp $ */
2a4392a64Sjmcneill
3a4392a64Sjmcneill /*-
4a4392a64Sjmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
5a4392a64Sjmcneill * All rights reserved.
6a4392a64Sjmcneill *
7a4392a64Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
8a4392a64Sjmcneill * by Jared McNeill <jmcneill@invisible.ca>.
9a4392a64Sjmcneill *
10a4392a64Sjmcneill * Redistribution and use in source and binary forms, with or without
11a4392a64Sjmcneill * modification, are permitted provided that the following conditions
12a4392a64Sjmcneill * are met:
13a4392a64Sjmcneill * 1. Redistributions of source code must retain the above copyright
14a4392a64Sjmcneill * notice, this list of conditions and the following disclaimer.
15a4392a64Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
16a4392a64Sjmcneill * notice, this list of conditions and the following disclaimer in the
17a4392a64Sjmcneill * documentation and/or other materials provided with the distribution.
18a4392a64Sjmcneill *
19a4392a64Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a4392a64Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a4392a64Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a4392a64Sjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a4392a64Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a4392a64Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a4392a64Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a4392a64Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a4392a64Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a4392a64Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a4392a64Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
30a4392a64Sjmcneill */
31a4392a64Sjmcneill
32a4392a64Sjmcneill #include <sys/cdefs.h>
33*47847e85Sjmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_iort.c,v 1.4 2020/09/13 21:41:17 jmcneill Exp $");
34a4392a64Sjmcneill
35a4392a64Sjmcneill #include <sys/param.h>
36a4392a64Sjmcneill #include <sys/bus.h>
37a4392a64Sjmcneill #include <sys/cpu.h>
38a4392a64Sjmcneill
39a4392a64Sjmcneill #include <dev/acpi/acpireg.h>
40a4392a64Sjmcneill #include <dev/acpi/acpivar.h>
41a4392a64Sjmcneill
42a4392a64Sjmcneill #include <arm/acpi/acpi_iort.h>
43a4392a64Sjmcneill
44a4392a64Sjmcneill static ACPI_IORT_ID_MAPPING *
acpi_iort_id_map(ACPI_IORT_NODE * node,uint32_t * id)45a4392a64Sjmcneill acpi_iort_id_map(ACPI_IORT_NODE *node, uint32_t *id)
46a4392a64Sjmcneill {
47a4392a64Sjmcneill ACPI_IORT_ID_MAPPING *map;
48a4392a64Sjmcneill uint32_t offset, n;
49a4392a64Sjmcneill
50*47847e85Sjmcneill offset = le32toh(node->MappingOffset);
51*47847e85Sjmcneill for (n = 0; n < le32toh(node->MappingCount); n++) {
52a4392a64Sjmcneill map = ACPI_ADD_PTR(ACPI_IORT_ID_MAPPING, node, offset);
53*47847e85Sjmcneill if (le32toh(map->Flags) & ACPI_IORT_ID_SINGLE_MAPPING) {
54*47847e85Sjmcneill *id = le32toh(map->OutputBase);
55a4392a64Sjmcneill return map;
56a4392a64Sjmcneill }
57*47847e85Sjmcneill if (*id >= le32toh(map->InputBase) && *id <= le32toh(map->InputBase) + le32toh(map->IdCount)) {
58*47847e85Sjmcneill *id = *id - le32toh(map->InputBase) + le32toh(map->OutputBase);
59a4392a64Sjmcneill return map;
60a4392a64Sjmcneill }
61a4392a64Sjmcneill offset += sizeof(ACPI_IORT_ID_MAPPING);
62a4392a64Sjmcneill }
63a4392a64Sjmcneill
64a4392a64Sjmcneill return NULL;
65a4392a64Sjmcneill }
66a4392a64Sjmcneill
67a4392a64Sjmcneill static ACPI_IORT_NODE *
acpi_iort_find_ref(ACPI_TABLE_IORT * iort,ACPI_IORT_NODE * node,uint32_t * id)68a4392a64Sjmcneill acpi_iort_find_ref(ACPI_TABLE_IORT *iort, ACPI_IORT_NODE *node, uint32_t *id)
69a4392a64Sjmcneill {
70a4392a64Sjmcneill ACPI_IORT_ID_MAPPING *map;
71a4392a64Sjmcneill
72a4392a64Sjmcneill map = acpi_iort_id_map(node, id);
73a4392a64Sjmcneill if (map == NULL)
74a4392a64Sjmcneill return NULL;
75a4392a64Sjmcneill
76*47847e85Sjmcneill return ACPI_ADD_PTR(ACPI_IORT_NODE, iort, le32toh(map->OutputReference));
77a4392a64Sjmcneill }
78a4392a64Sjmcneill
79a4392a64Sjmcneill uint32_t
acpi_iort_pci_root_map(u_int seg,uint32_t devid)80a4392a64Sjmcneill acpi_iort_pci_root_map(u_int seg, uint32_t devid)
81a4392a64Sjmcneill {
82a4392a64Sjmcneill ACPI_TABLE_IORT *iort;
83a4392a64Sjmcneill ACPI_IORT_NODE *node;
84a4392a64Sjmcneill ACPI_IORT_ROOT_COMPLEX *root;
85a4392a64Sjmcneill uint32_t offset, n;
86a4392a64Sjmcneill ACPI_STATUS rv;
87a4392a64Sjmcneill
88a4392a64Sjmcneill rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
89a4392a64Sjmcneill if (ACPI_FAILURE(rv))
90a4392a64Sjmcneill return devid;
91a4392a64Sjmcneill
92*47847e85Sjmcneill offset = le32toh(iort->NodeOffset);
93*47847e85Sjmcneill for (n = 0; n < le32toh(iort->NodeCount); n++) {
94a4392a64Sjmcneill node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
95a4392a64Sjmcneill if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
96a4392a64Sjmcneill root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
97*47847e85Sjmcneill if (le32toh(root->PciSegmentNumber) == seg) {
98a4392a64Sjmcneill const uint32_t odevid = devid;
99a4392a64Sjmcneill do {
100a4392a64Sjmcneill node = acpi_iort_find_ref(iort, node, &devid);
101a4392a64Sjmcneill } while (node != NULL);
1025462edefSjmcneill aprint_debug("ACPI: IORT mapped devid %#x -> devid %#x\n", odevid, devid);
103a4392a64Sjmcneill return devid;
104a4392a64Sjmcneill }
105a4392a64Sjmcneill }
106*47847e85Sjmcneill offset += le16toh(node->Length);
107a4392a64Sjmcneill }
108a4392a64Sjmcneill
109a4392a64Sjmcneill return devid;
110a4392a64Sjmcneill }
1115462edefSjmcneill
1125462edefSjmcneill uint32_t
acpi_iort_its_id_map(u_int seg,uint32_t devid)1135462edefSjmcneill acpi_iort_its_id_map(u_int seg, uint32_t devid)
1145462edefSjmcneill {
1155462edefSjmcneill ACPI_TABLE_IORT *iort;
1165462edefSjmcneill ACPI_IORT_NODE *node;
1175462edefSjmcneill ACPI_IORT_ROOT_COMPLEX *root;
1185462edefSjmcneill ACPI_IORT_ITS_GROUP *its_group;
1195462edefSjmcneill uint32_t offset, n;
1205462edefSjmcneill ACPI_STATUS rv;
1215462edefSjmcneill
1225462edefSjmcneill rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
1235462edefSjmcneill if (ACPI_FAILURE(rv))
1245462edefSjmcneill return 0;
1255462edefSjmcneill
126*47847e85Sjmcneill offset = le32toh(iort->NodeOffset);
127*47847e85Sjmcneill for (n = 0; n < le32toh(iort->NodeCount); n++) {
1285462edefSjmcneill node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
1295462edefSjmcneill if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1305462edefSjmcneill root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
131*47847e85Sjmcneill if (le32toh(root->PciSegmentNumber) == seg) {
1325462edefSjmcneill const uint32_t odevid = devid;
1335462edefSjmcneill do {
1345462edefSjmcneill node = acpi_iort_find_ref(iort, node, &devid);
1355462edefSjmcneill if (node != NULL && node->Type == ACPI_IORT_NODE_ITS_GROUP) {
1365462edefSjmcneill its_group = (ACPI_IORT_ITS_GROUP *)node->NodeData;
137*47847e85Sjmcneill if (le32toh(its_group->ItsCount) == 0)
1385462edefSjmcneill return 0;
1395462edefSjmcneill aprint_debug("ACPI: IORT mapped devid %#x -> ITS %#x\n",
140*47847e85Sjmcneill odevid, le32toh(its_group->Identifiers[0]));
141*47847e85Sjmcneill return le32toh(its_group->Identifiers[0]);
1425462edefSjmcneill }
1435462edefSjmcneill } while (node != NULL);
1445462edefSjmcneill return 0;
1455462edefSjmcneill }
1465462edefSjmcneill }
147*47847e85Sjmcneill offset += le16toh(node->Length);
1485462edefSjmcneill }
1495462edefSjmcneill
1505462edefSjmcneill return 0;
1515462edefSjmcneill }
152