2 * Copyright (c) 2016 Chelsio Communications, Inc.
4 * Written by: John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/pciio.h>
34 #include <sys/queue.h>
41 #include <x86/iommu/intel_reg.h>
49 read_4(char *regs, size_t offset)
51 return *(uint32_t *)(regs + offset);
55 read_8(char *regs, size_t offset)
57 return *(uint64_t *)(regs + offset);
60 static struct pci_conf *
61 pci_find_conf(int segment, int bus, int slot, int func)
63 static int pcifd = -1;
64 static struct pci_conf conf;
65 struct pci_conf_io pc;
66 struct pci_match_conf patterns[1];
69 pcifd = open("/dev/pci", O_RDONLY);
71 err(1, "Failed to open /dev/pci");
74 bzero(&pc, sizeof(pc));
75 pc.match_buf_len = sizeof(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;
86 pc.pat_buf_len = sizeof(patterns);
87 pc.patterns = patterns;
88 if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1)
89 err(1, "ioctl(PCIOCGETCONF)");
91 if (pc.status != PCI_GETCONF_LAST_DEVICE ||
99 dump_context_table(int segment, int bus, uint64_t base_addr)
101 struct dmar_ctx_entry *ctx;
102 struct pci_conf *conf;
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))
112 printf("\tPCI bus %d:\n", bus);
116 /* Check for ARI device first. */
117 conf = pci_find_conf(segment, bus, 0, idx);
119 conf = pci_find_conf(segment, bus, idx >> 3, idx & 7);
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,
127 printf("\t { %d,%d } (absent)", idx >> 3,
129 if (ctx[idx].ctx1 & DMAR_CTX1_FPD)
131 switch (ctx[idx].ctx1 & 0xc) {
132 case DMAR_CTX1_T_UNTR:
138 case DMAR_CTX1_T_PASS:
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));
153 handle_drhd(int segment, uint64_t base_addr)
155 struct dmar_root_entry *root_table;
162 regs = acpi_map_physical(base_addr, 4096);
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))
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++) {
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,
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,
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);
195 /* Borrowed from acpi.c in acpidump: */
198 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
201 handle_drhd(drhd->Segment, drhd->Address);
205 acpi_handle_dmar_remapping_structure(void *addr, int remaining)
207 ACPI_DMAR_HEADER *hdr = addr;
209 if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
212 if (remaining < hdr->Length)
216 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
217 acpi_handle_dmar_drhd(addr);
220 return (hdr->Length);
224 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
227 int remaining, consumed;
228 ACPI_TABLE_DMAR *dmar;
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);
238 remaining -= consumed;
242 static ACPI_TABLE_HEADER *
243 acpi_map_sdt(vm_offset_t pa)
245 ACPI_TABLE_HEADER *sp;
247 sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
248 sp = acpi_map_physical(pa, sp->Length);
253 walk_rsdt(ACPI_TABLE_HEADER *rsdp)
255 ACPI_TABLE_HEADER *sdp;
256 ACPI_TABLE_RSDT *rsdt;
257 ACPI_TABLE_XSDT *xsdt;
259 int addr_size, entries, i;
261 if (memcmp(rsdp->Signature, "RSDT", 4) != 0)
262 addr_size = sizeof(uint32_t);
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++) {
270 addr = le32toh(rsdt->TableOffsetEntry[i]);
272 addr = le64toh(xsdt->TableOffsetEntry[i]);
275 sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
276 if (acpi_checksum(sdp, sdp->Length)) {
279 if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
280 acpi_handle_dmar(sdp);
285 main(int argc __unused, char *argv[] __unused)
287 ACPI_TABLE_HEADER *rsdt;
289 rsdt = sdt_load_devmem();