1 /*- 2 * Copyright (c) 2016 Chelsio Communications, Inc. 3 * All rights reserved. 4 * Written by: John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/endian.h> 33 #include <sys/pciio.h> 34 #include <sys/queue.h> 35 #include <err.h> 36 #include <fcntl.h> 37 #include <stdbool.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 41 #include <x86/iommu/intel_reg.h> 42 43 #include "acpidump.h" 44 45 int tflag; 46 int vflag; 47 48 static uint32_t 49 read_4(char *regs, size_t offset) 50 { 51 return *(uint32_t *)(regs + offset); 52 } 53 54 static uint64_t 55 read_8(char *regs, size_t offset) 56 { 57 return *(uint64_t *)(regs + offset); 58 } 59 60 static struct pci_conf * 61 pci_find_conf(int segment, int bus, int slot, int func) 62 { 63 static int pcifd = -1; 64 static struct pci_conf conf; 65 struct pci_conf_io pc; 66 struct pci_match_conf patterns[1]; 67 68 if (pcifd == -1) { 69 pcifd = open("/dev/pci", O_RDONLY); 70 if (pcifd < 0) 71 err(1, "Failed to open /dev/pci"); 72 } 73 74 bzero(&pc, sizeof(pc)); 75 pc.match_buf_len = sizeof(conf); 76 pc.matches = &conf; 77 bzero(&patterns, sizeof(patterns)); 78 patterns[0].pc_sel.pc_domain = segment; 79 patterns[0].pc_sel.pc_bus = bus; 80 patterns[0].pc_sel.pc_dev = slot; 81 patterns[0].pc_sel.pc_func = func; 82 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | 83 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV | 84 PCI_GETCONF_MATCH_FUNC; 85 pc.num_patterns = 1; 86 pc.pat_buf_len = sizeof(patterns); 87 pc.patterns = patterns; 88 if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1) 89 err(1, "ioctl(PCIOCGETCONF)"); 90 91 if (pc.status != PCI_GETCONF_LAST_DEVICE || 92 pc.num_matches == 0) 93 return (NULL); 94 95 return (&conf); 96 } 97 98 static void 99 dump_context_table(int segment, int bus, uint64_t base_addr) 100 { 101 struct dmar_ctx_entry *ctx; 102 struct pci_conf *conf; 103 bool printed; 104 int idx; 105 106 printed = false; 107 ctx = acpi_map_physical(base_addr, DMAR_PAGE_SIZE); 108 for (idx = 0; idx < DMAR_CTX_CNT; idx++) { 109 if (!(ctx[idx].ctx1 & DMAR_CTX1_P)) 110 continue; 111 if (!printed) { 112 printf("\tPCI bus %d:\n", bus); 113 printed = true; 114 } 115 116 /* Check for ARI device first. */ 117 conf = pci_find_conf(segment, bus, 0, idx); 118 if (conf == NULL) 119 conf = pci_find_conf(segment, bus, idx >> 3, idx & 7); 120 if (conf != NULL) { 121 printf("\t { %d,%d }", conf->pc_sel.pc_dev, 122 conf->pc_sel.pc_func); 123 if (conf->pd_name[0] != '\0') 124 printf(" (%s%lu)", conf->pd_name, 125 conf->pd_unit); 126 } else 127 printf("\t { %d,%d } (absent)", idx >> 3, 128 idx & 7); 129 if (ctx[idx].ctx1 & DMAR_CTX1_FPD) 130 printf(" FPD"); 131 switch (ctx[idx].ctx1 & 0xc) { 132 case DMAR_CTX1_T_UNTR: 133 printf(" UNTR"); 134 break; 135 case DMAR_CTX1_T_TR: 136 printf(" TR"); 137 break; 138 case DMAR_CTX1_T_PASS: 139 printf(" PASS"); 140 break; 141 default: 142 printf(" TT3?"); 143 break; 144 } 145 printf(" SLPT %#jx", (uintmax_t)(ctx[idx].ctx1 & 146 DMAR_CTX1_ASR_MASK)); 147 printf(" domain %d", (int)DMAR_CTX2_GET_DID(ctx[idx].ctx2)); 148 printf("\n"); 149 } 150 } 151 152 static void 153 handle_drhd(int segment, uint64_t base_addr) 154 { 155 struct dmar_root_entry *root_table; 156 char *regs; 157 uint64_t rtaddr; 158 uint32_t gsts, ver; 159 bool extended; 160 int bus; 161 162 regs = acpi_map_physical(base_addr, 4096); 163 164 ver = read_4(regs, DMAR_VER_REG); 165 gsts = read_4(regs, DMAR_GSTS_REG); 166 printf("drhd @ %#jx (version %d.%d) PCI segment %d%s:\n", 167 (uintmax_t)base_addr, DMAR_MAJOR_VER(ver), DMAR_MINOR_VER(ver), 168 segment, gsts & DMAR_GSTS_TES ? "" : " (disabled)"); 169 if ((gsts & (DMAR_GSTS_TES | DMAR_GSTS_RTPS)) != 170 (DMAR_GSTS_TES | DMAR_GSTS_RTPS)) 171 return; 172 rtaddr = read_8(regs, DMAR_RTADDR_REG); 173 extended = (rtaddr & DMAR_RTADDR_RTT) != 0; 174 printf(" %sroot table @ 0x%#jx\n", extended ? "extended " : "", 175 rtaddr & DMAR_RTADDR_RTA_MASK); 176 root_table = acpi_map_physical(rtaddr & DMAR_RTADDR_RTA_MASK, 4096); 177 for (bus = 0; bus < 255; bus++) { 178 if (extended) { 179 #ifdef notyet 180 if (root_table[bus].r1 & DMAR_ROOT_R1_P) 181 dump_ext_context_table(segment, bus, 182 root_table[bus].r1 & DMAR_ROOT_R1_CTP_MASK, 183 false); 184 if (root_table[bus].r2 & DMAR_ROOT_R1_P) 185 dump_ext_context_table(segment, bus, 186 root_table[bus].r2 & DMAR_ROOT_R1_CTP_MASK, 187 true); 188 #endif 189 } else if (root_table[bus].r1 & DMAR_ROOT_R1_P) 190 dump_context_table(segment, bus, root_table[bus].r1 & 191 DMAR_ROOT_R1_CTP_MASK); 192 } 193 } 194 195 /* Borrowed from acpi.c in acpidump: */ 196 197 static void 198 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd) 199 { 200 201 handle_drhd(drhd->Segment, drhd->Address); 202 } 203 204 static int 205 acpi_handle_dmar_remapping_structure(void *addr, int remaining) 206 { 207 ACPI_DMAR_HEADER *hdr = addr; 208 209 if (remaining < (int)sizeof(ACPI_DMAR_HEADER)) 210 return (-1); 211 212 if (remaining < hdr->Length) 213 return (-1); 214 215 switch (hdr->Type) { 216 case ACPI_DMAR_TYPE_HARDWARE_UNIT: 217 acpi_handle_dmar_drhd(addr); 218 break; 219 } 220 return (hdr->Length); 221 } 222 223 static void 224 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp) 225 { 226 char *cp; 227 int remaining, consumed; 228 ACPI_TABLE_DMAR *dmar; 229 230 dmar = (ACPI_TABLE_DMAR *)sdp; 231 remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR); 232 while (remaining > 0) { 233 cp = (char *)sdp + sdp->Length - remaining; 234 consumed = acpi_handle_dmar_remapping_structure(cp, remaining); 235 if (consumed <= 0) 236 break; 237 else 238 remaining -= consumed; 239 } 240 } 241 242 static ACPI_TABLE_HEADER * 243 acpi_map_sdt(vm_offset_t pa) 244 { 245 ACPI_TABLE_HEADER *sp; 246 247 sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER)); 248 sp = acpi_map_physical(pa, sp->Length); 249 return (sp); 250 } 251 252 static void 253 walk_rsdt(ACPI_TABLE_HEADER *rsdp) 254 { 255 ACPI_TABLE_HEADER *sdp; 256 ACPI_TABLE_RSDT *rsdt; 257 ACPI_TABLE_XSDT *xsdt; 258 vm_offset_t addr; 259 int addr_size, entries, i; 260 261 if (memcmp(rsdp->Signature, "RSDT", 4) != 0) 262 addr_size = sizeof(uint32_t); 263 else 264 addr_size = sizeof(uint64_t); 265 rsdt = (ACPI_TABLE_RSDT *)rsdp; 266 xsdt = (ACPI_TABLE_XSDT *)rsdp; 267 entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size; 268 for (i = 0; i < entries; i++) { 269 if (addr_size == 4) 270 addr = le32toh(rsdt->TableOffsetEntry[i]); 271 else 272 addr = le64toh(xsdt->TableOffsetEntry[i]); 273 if (addr == 0) 274 continue; 275 sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr); 276 if (acpi_checksum(sdp, sdp->Length)) { 277 continue; 278 } 279 if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4)) 280 acpi_handle_dmar(sdp); 281 } 282 } 283 284 int 285 main(int argc __unused, char *argv[] __unused) 286 { 287 ACPI_TABLE_HEADER *rsdt; 288 289 rsdt = sdt_load_devmem(); 290 walk_rsdt(rsdt); 291 return 0; 292 } 293