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 #include <sys/param.h>
30 #include <sys/endian.h>
31 #include <sys/pciio.h>
32 #include <sys/queue.h>
39 #include <x86/iommu/intel_reg.h>
47 read_4(char *regs, size_t offset)
49 return *(uint32_t *)(regs + offset);
53 read_8(char *regs, size_t offset)
55 return *(uint64_t *)(regs + offset);
58 static struct pci_conf *
59 pci_find_conf(int segment, int bus, int slot, int func)
61 static int pcifd = -1;
62 static struct pci_conf conf;
63 struct pci_conf_io pc;
64 struct pci_match_conf patterns[1];
67 pcifd = open("/dev/pci", O_RDONLY);
69 err(1, "Failed to open /dev/pci");
72 bzero(&pc, sizeof(pc));
73 pc.match_buf_len = sizeof(conf);
75 bzero(&patterns, sizeof(patterns));
76 patterns[0].pc_sel.pc_domain = segment;
77 patterns[0].pc_sel.pc_bus = bus;
78 patterns[0].pc_sel.pc_dev = slot;
79 patterns[0].pc_sel.pc_func = func;
80 patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN |
81 PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV |
82 PCI_GETCONF_MATCH_FUNC;
84 pc.pat_buf_len = sizeof(patterns);
85 pc.patterns = patterns;
86 if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1)
87 err(1, "ioctl(PCIOCGETCONF)");
89 if (pc.status != PCI_GETCONF_LAST_DEVICE ||
97 dump_context_table(int segment, int bus, uint64_t base_addr)
99 struct dmar_ctx_entry *ctx;
100 struct pci_conf *conf;
105 ctx = acpi_map_physical(base_addr, DMAR_PAGE_SIZE);
106 for (idx = 0; idx < DMAR_CTX_CNT; idx++) {
107 if (!(ctx[idx].ctx1 & DMAR_CTX1_P))
110 printf("\tPCI bus %d:\n", bus);
114 /* Check for ARI device first. */
115 conf = pci_find_conf(segment, bus, 0, idx);
117 conf = pci_find_conf(segment, bus, idx >> 3, idx & 7);
119 printf("\t { %d,%d }", conf->pc_sel.pc_dev,
120 conf->pc_sel.pc_func);
121 if (conf->pd_name[0] != '\0')
122 printf(" (%s%lu)", conf->pd_name,
125 printf("\t { %d,%d } (absent)", idx >> 3,
127 if (ctx[idx].ctx1 & DMAR_CTX1_FPD)
129 switch (ctx[idx].ctx1 & 0xc) {
130 case DMAR_CTX1_T_UNTR:
136 case DMAR_CTX1_T_PASS:
143 printf(" SLPT %#jx", (uintmax_t)(ctx[idx].ctx1 &
144 DMAR_CTX1_ASR_MASK));
145 printf(" domain %d", (int)DMAR_CTX2_GET_DID(ctx[idx].ctx2));
151 handle_drhd(int segment, uint64_t base_addr)
153 struct dmar_root_entry *root_table;
160 regs = acpi_map_physical(base_addr, 4096);
162 ver = read_4(regs, DMAR_VER_REG);
163 gsts = read_4(regs, DMAR_GSTS_REG);
164 printf("drhd @ %#jx (version %d.%d) PCI segment %d%s:\n",
165 (uintmax_t)base_addr, DMAR_MAJOR_VER(ver), DMAR_MINOR_VER(ver),
166 segment, gsts & DMAR_GSTS_TES ? "" : " (disabled)");
167 if ((gsts & (DMAR_GSTS_TES | DMAR_GSTS_RTPS)) !=
168 (DMAR_GSTS_TES | DMAR_GSTS_RTPS))
170 rtaddr = read_8(regs, DMAR_RTADDR_REG);
171 extended = (rtaddr & DMAR_RTADDR_RTT) != 0;
172 printf(" %sroot table @ 0x%#jx\n", extended ? "extended " : "",
173 rtaddr & DMAR_RTADDR_RTA_MASK);
174 root_table = acpi_map_physical(rtaddr & DMAR_RTADDR_RTA_MASK, 4096);
175 for (bus = 0; bus < 255; bus++) {
178 if (root_table[bus].r1 & DMAR_ROOT_R1_P)
179 dump_ext_context_table(segment, bus,
180 root_table[bus].r1 & DMAR_ROOT_R1_CTP_MASK,
182 if (root_table[bus].r2 & DMAR_ROOT_R1_P)
183 dump_ext_context_table(segment, bus,
184 root_table[bus].r2 & DMAR_ROOT_R1_CTP_MASK,
187 } else if (root_table[bus].r1 & DMAR_ROOT_R1_P)
188 dump_context_table(segment, bus, root_table[bus].r1 &
189 DMAR_ROOT_R1_CTP_MASK);
193 /* Borrowed from acpi.c in acpidump: */
196 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
199 handle_drhd(drhd->Segment, drhd->Address);
203 acpi_handle_dmar_remapping_structure(void *addr, int remaining)
205 ACPI_DMAR_HEADER *hdr = addr;
207 if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
210 if (remaining < hdr->Length)
214 case ACPI_DMAR_TYPE_HARDWARE_UNIT:
215 acpi_handle_dmar_drhd(addr);
218 return (hdr->Length);
222 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
225 int remaining, consumed;
226 ACPI_TABLE_DMAR *dmar;
228 dmar = (ACPI_TABLE_DMAR *)sdp;
229 remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR);
230 while (remaining > 0) {
231 cp = (char *)sdp + sdp->Length - remaining;
232 consumed = acpi_handle_dmar_remapping_structure(cp, remaining);
236 remaining -= consumed;
240 static ACPI_TABLE_HEADER *
241 acpi_map_sdt(vm_offset_t pa)
243 ACPI_TABLE_HEADER *sp;
245 sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
246 sp = acpi_map_physical(pa, sp->Length);
251 walk_rsdt(ACPI_TABLE_HEADER *rsdp)
253 ACPI_TABLE_HEADER *sdp;
254 ACPI_TABLE_RSDT *rsdt;
255 ACPI_TABLE_XSDT *xsdt;
257 int addr_size, entries, i;
259 if (memcmp(rsdp->Signature, "RSDT", 4) != 0)
260 addr_size = sizeof(uint32_t);
262 addr_size = sizeof(uint64_t);
263 rsdt = (ACPI_TABLE_RSDT *)rsdp;
264 xsdt = (ACPI_TABLE_XSDT *)rsdp;
265 entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
266 for (i = 0; i < entries; i++) {
268 addr = le32toh(rsdt->TableOffsetEntry[i]);
270 addr = le64toh(xsdt->TableOffsetEntry[i]);
273 sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
274 if (acpi_checksum(sdp, sdp->Length)) {
277 if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
278 acpi_handle_dmar(sdp);
283 main(int argc __unused, char *argv[] __unused)
285 ACPI_TABLE_HEADER *rsdt;
287 rsdt = sdt_load_devmem();