xref: /freebsd/tools/tools/dmardump/dmardump.c (revision 1d386b48)
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 #include <sys/param.h>
30 #include <sys/endian.h>
31 #include <sys/pciio.h>
32 #include <sys/queue.h>
33 #include <err.h>
34 #include <fcntl.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #include <x86/iommu/intel_reg.h>
40 
41 #include "acpidump.h"
42 
43 int tflag;
44 int vflag;
45 
46 static uint32_t
47 read_4(char *regs, size_t offset)
48 {
49 	return *(uint32_t *)(regs + offset);
50 }
51 
52 static uint64_t
53 read_8(char *regs, size_t offset)
54 {
55 	return *(uint64_t *)(regs + offset);
56 }
57 
58 static struct pci_conf *
59 pci_find_conf(int segment, int bus, int slot, int func)
60 {
61 	static int pcifd = -1;
62 	static struct pci_conf conf;
63 	struct pci_conf_io pc;
64 	struct pci_match_conf patterns[1];
65 
66 	if (pcifd == -1) {
67 		pcifd = open("/dev/pci", O_RDONLY);
68 		if (pcifd < 0)
69 			err(1, "Failed to open /dev/pci");
70 	}
71 
72 	bzero(&pc, sizeof(pc));
73 	pc.match_buf_len = sizeof(conf);
74 	pc.matches = &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;
83 	pc.num_patterns = 1;
84 	pc.pat_buf_len = sizeof(patterns);
85 	pc.patterns = patterns;
86 	if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1)
87 		err(1, "ioctl(PCIOCGETCONF)");
88 
89 	if (pc.status != PCI_GETCONF_LAST_DEVICE ||
90 	    pc.num_matches == 0)
91 		return (NULL);
92 
93 	return (&conf);
94 }
95 
96 static void
97 dump_context_table(int segment, int bus, uint64_t base_addr)
98 {
99 	struct dmar_ctx_entry *ctx;
100 	struct pci_conf *conf;
101 	bool printed;
102 	int idx;
103 
104 	printed = false;
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))
108 			continue;
109 		if (!printed) {
110 			printf("\tPCI bus %d:\n", bus);
111 			printed = true;
112 		}
113 
114 		/* Check for ARI device first. */
115 		conf = pci_find_conf(segment, bus, 0, idx);
116 		if (conf == NULL)
117 			conf = pci_find_conf(segment, bus, idx >> 3, idx & 7);
118 		if (conf != NULL) {
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,
123 				    conf->pd_unit);
124 		} else
125 			printf("\t    { %d,%d } (absent)", idx >> 3,
126 			    idx & 7);
127 		if (ctx[idx].ctx1 & DMAR_CTX1_FPD)
128 			printf(" FPD");
129 		switch (ctx[idx].ctx1 & 0xc) {
130 		case DMAR_CTX1_T_UNTR:
131 			printf(" UNTR");
132 			break;
133 		case DMAR_CTX1_T_TR:
134 			printf(" TR");
135 			break;
136 		case DMAR_CTX1_T_PASS:
137 			printf(" PASS");
138 			break;
139 		default:
140 			printf(" TT3?");
141 			break;
142 		}
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));
146 		printf("\n");
147 	}
148 }
149 
150 static void
151 handle_drhd(int segment, uint64_t base_addr)
152 {
153 	struct dmar_root_entry *root_table;
154 	char *regs;
155 	uint64_t rtaddr;
156 	uint32_t gsts, ver;
157 	bool extended;
158 	int bus;
159 
160 	regs = acpi_map_physical(base_addr, 4096);
161 
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))
169 		return;
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++) {
176 		if (extended) {
177 #ifdef notyet
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,
181 				    false);
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,
185 				    true);
186 #endif
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);
190 	}
191 }
192 
193 /* Borrowed from acpi.c in acpidump: */
194 
195 static void
196 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
197 {
198 
199 	handle_drhd(drhd->Segment, drhd->Address);
200 }
201 
202 static int
203 acpi_handle_dmar_remapping_structure(void *addr, int remaining)
204 {
205 	ACPI_DMAR_HEADER *hdr = addr;
206 
207 	if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
208 		return (-1);
209 
210 	if (remaining < hdr->Length)
211 		return (-1);
212 
213 	switch (hdr->Type) {
214 	case ACPI_DMAR_TYPE_HARDWARE_UNIT:
215 		acpi_handle_dmar_drhd(addr);
216 		break;
217 	}
218 	return (hdr->Length);
219 }
220 
221 static void
222 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
223 {
224 	char *cp;
225 	int remaining, consumed;
226 	ACPI_TABLE_DMAR *dmar;
227 
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);
233 		if (consumed <= 0)
234 			break;
235 		else
236 			remaining -= consumed;
237 	}
238 }
239 
240 static ACPI_TABLE_HEADER *
241 acpi_map_sdt(vm_offset_t pa)
242 {
243 	ACPI_TABLE_HEADER *sp;
244 
245 	sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
246 	sp = acpi_map_physical(pa, sp->Length);
247 	return (sp);
248 }
249 
250 static void
251 walk_rsdt(ACPI_TABLE_HEADER *rsdp)
252 {
253 	ACPI_TABLE_HEADER *sdp;
254 	ACPI_TABLE_RSDT *rsdt;
255 	ACPI_TABLE_XSDT *xsdt;
256 	vm_offset_t addr;
257 	int addr_size, entries, i;
258 
259 	if (memcmp(rsdp->Signature, "RSDT", 4) != 0)
260 		addr_size = sizeof(uint32_t);
261 	else
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++) {
267 		if (addr_size == 4)
268 			addr = le32toh(rsdt->TableOffsetEntry[i]);
269 		else
270 			addr = le64toh(xsdt->TableOffsetEntry[i]);
271 		if (addr == 0)
272 			continue;
273 		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
274 		if (acpi_checksum(sdp, sdp->Length)) {
275 			continue;
276 		}
277 		if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
278 			acpi_handle_dmar(sdp);
279 	}
280 }
281 
282 int
283 main(int argc __unused, char *argv[] __unused)
284 {
285 	ACPI_TABLE_HEADER *rsdt;
286 
287 	rsdt = sdt_load_devmem();
288 	walk_rsdt(rsdt);
289 	return 0;
290 }
291