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