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/param.h>
29#include <sys/endian.h>
30#include <sys/pciio.h>
31#include <sys/queue.h>
32#include <err.h>
33#include <fcntl.h>
34#include <stdbool.h>
35#include <stdio.h>
36#include <stdlib.h>
37
38#include <x86/iommu/intel_reg.h>
39
40#include "acpidump.h"
41
42int tflag;
43int vflag;
44
45static uint32_t
46read_4(char *regs, size_t offset)
47{
48	return *(uint32_t *)(regs + offset);
49}
50
51static uint64_t
52read_8(char *regs, size_t offset)
53{
54	return *(uint64_t *)(regs + offset);
55}
56
57static struct pci_conf *
58pci_find_conf(int segment, int bus, int slot, int func)
59{
60	static int pcifd = -1;
61	static struct pci_conf conf;
62	struct pci_conf_io pc;
63	struct pci_match_conf patterns[1];
64
65	if (pcifd == -1) {
66		pcifd = open("/dev/pci", O_RDONLY);
67		if (pcifd < 0)
68			err(1, "Failed to open /dev/pci");
69	}
70
71	bzero(&pc, sizeof(pc));
72	pc.match_buf_len = sizeof(conf);
73	pc.matches = &conf;
74	bzero(&patterns, sizeof(patterns));
75	patterns[0].pc_sel.pc_domain = segment;
76	patterns[0].pc_sel.pc_bus = bus;
77	patterns[0].pc_sel.pc_dev = slot;
78	patterns[0].pc_sel.pc_func = func;
79	patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN |
80	    PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV |
81	    PCI_GETCONF_MATCH_FUNC;
82	pc.num_patterns = 1;
83	pc.pat_buf_len = sizeof(patterns);
84	pc.patterns = patterns;
85	if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1)
86		err(1, "ioctl(PCIOCGETCONF)");
87
88	if (pc.status != PCI_GETCONF_LAST_DEVICE ||
89	    pc.num_matches == 0)
90		return (NULL);
91
92	return (&conf);
93}
94
95static void
96dump_context_table(int segment, int bus, uint64_t base_addr)
97{
98	struct dmar_ctx_entry *ctx;
99	struct pci_conf *conf;
100	bool printed;
101	int idx;
102
103	printed = false;
104	ctx = acpi_map_physical(base_addr, DMAR_PAGE_SIZE);
105	for (idx = 0; idx < DMAR_CTX_CNT; idx++) {
106		if (!(ctx[idx].ctx1 & DMAR_CTX1_P))
107			continue;
108		if (!printed) {
109			printf("\tPCI bus %d:\n", bus);
110			printed = true;
111		}
112
113		/* Check for ARI device first. */
114		conf = pci_find_conf(segment, bus, 0, idx);
115		if (conf == NULL)
116			conf = pci_find_conf(segment, bus, idx >> 3, idx & 7);
117		if (conf != NULL) {
118			printf("\t    { %d,%d }", conf->pc_sel.pc_dev,
119			    conf->pc_sel.pc_func);
120			if (conf->pd_name[0] != '\0')
121				printf(" (%s%lu)", conf->pd_name,
122				    conf->pd_unit);
123		} else
124			printf("\t    { %d,%d } (absent)", idx >> 3,
125			    idx & 7);
126		if (ctx[idx].ctx1 & DMAR_CTX1_FPD)
127			printf(" FPD");
128		switch (ctx[idx].ctx1 & 0xc) {
129		case DMAR_CTX1_T_UNTR:
130			printf(" UNTR");
131			break;
132		case DMAR_CTX1_T_TR:
133			printf(" TR");
134			break;
135		case DMAR_CTX1_T_PASS:
136			printf(" PASS");
137			break;
138		default:
139			printf(" TT3?");
140			break;
141		}
142		printf(" SLPT %#jx", (uintmax_t)(ctx[idx].ctx1 &
143		    DMAR_CTX1_ASR_MASK));
144		printf(" domain %d", (int)DMAR_CTX2_GET_DID(ctx[idx].ctx2));
145		printf("\n");
146	}
147}
148
149static void
150handle_drhd(int segment, uint64_t base_addr)
151{
152	struct dmar_root_entry *root_table;
153	char *regs;
154	uint64_t rtaddr;
155	uint32_t gsts, ver;
156	bool extended;
157	int bus;
158
159	regs = acpi_map_physical(base_addr, 4096);
160
161	ver = read_4(regs, DMAR_VER_REG);
162	gsts = read_4(regs, DMAR_GSTS_REG);
163	printf("drhd @ %#jx (version %d.%d) PCI segment %d%s:\n",
164	    (uintmax_t)base_addr, DMAR_MAJOR_VER(ver), DMAR_MINOR_VER(ver),
165	    segment, gsts & DMAR_GSTS_TES ? "" : " (disabled)");
166	if ((gsts & (DMAR_GSTS_TES | DMAR_GSTS_RTPS)) !=
167	    (DMAR_GSTS_TES | DMAR_GSTS_RTPS))
168		return;
169	rtaddr = read_8(regs, DMAR_RTADDR_REG);
170	extended = (rtaddr & DMAR_RTADDR_RTT) != 0;
171	printf("    %sroot table @ 0x%#jx\n", extended ? "extended " : "",
172	    rtaddr & DMAR_RTADDR_RTA_MASK);
173	root_table = acpi_map_physical(rtaddr & DMAR_RTADDR_RTA_MASK, 4096);
174	for (bus = 0; bus < 255; bus++) {
175		if (extended) {
176#ifdef notyet
177			if (root_table[bus].r1 & DMAR_ROOT_R1_P)
178				dump_ext_context_table(segment, bus,
179				    root_table[bus].r1 & DMAR_ROOT_R1_CTP_MASK,
180				    false);
181			if (root_table[bus].r2 & DMAR_ROOT_R1_P)
182				dump_ext_context_table(segment, bus,
183				    root_table[bus].r2 & DMAR_ROOT_R1_CTP_MASK,
184				    true);
185#endif
186		} else if (root_table[bus].r1 & DMAR_ROOT_R1_P)
187			dump_context_table(segment, bus, root_table[bus].r1 &
188			    DMAR_ROOT_R1_CTP_MASK);
189	}
190}
191
192/* Borrowed from acpi.c in acpidump: */
193
194static void
195acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
196{
197
198	handle_drhd(drhd->Segment, drhd->Address);
199}
200
201static int
202acpi_handle_dmar_remapping_structure(void *addr, int remaining)
203{
204	ACPI_DMAR_HEADER *hdr = addr;
205
206	if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
207		return (-1);
208
209	if (remaining < hdr->Length)
210		return (-1);
211
212	switch (hdr->Type) {
213	case ACPI_DMAR_TYPE_HARDWARE_UNIT:
214		acpi_handle_dmar_drhd(addr);
215		break;
216	}
217	return (hdr->Length);
218}
219
220static void
221acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
222{
223	char *cp;
224	int remaining, consumed;
225	ACPI_TABLE_DMAR *dmar;
226
227	dmar = (ACPI_TABLE_DMAR *)sdp;
228	remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR);
229	while (remaining > 0) {
230		cp = (char *)sdp + sdp->Length - remaining;
231		consumed = acpi_handle_dmar_remapping_structure(cp, remaining);
232		if (consumed <= 0)
233			break;
234		else
235			remaining -= consumed;
236	}
237}
238
239static ACPI_TABLE_HEADER *
240acpi_map_sdt(vm_offset_t pa)
241{
242	ACPI_TABLE_HEADER *sp;
243
244	sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
245	sp = acpi_map_physical(pa, sp->Length);
246	return (sp);
247}
248
249static void
250walk_rsdt(ACPI_TABLE_HEADER *rsdp)
251{
252	ACPI_TABLE_HEADER *sdp;
253	ACPI_TABLE_RSDT *rsdt;
254	ACPI_TABLE_XSDT *xsdt;
255	vm_offset_t addr;
256	int addr_size, entries, i;
257
258	if (memcmp(rsdp->Signature, "RSDT", 4) != 0)
259		addr_size = sizeof(uint32_t);
260	else
261		addr_size = sizeof(uint64_t);
262	rsdt = (ACPI_TABLE_RSDT *)rsdp;
263	xsdt = (ACPI_TABLE_XSDT *)rsdp;
264	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
265	for (i = 0; i < entries; i++) {
266		if (addr_size == 4)
267			addr = le32toh(rsdt->TableOffsetEntry[i]);
268		else
269			addr = le64toh(xsdt->TableOffsetEntry[i]);
270		if (addr == 0)
271			continue;
272		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
273		if (acpi_checksum(sdp, sdp->Length)) {
274			continue;
275		}
276		if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
277			acpi_handle_dmar(sdp);
278	}
279}
280
281int
282main(int argc __unused, char *argv[] __unused)
283{
284	ACPI_TABLE_HEADER *rsdt;
285
286	rsdt = sdt_load_devmem();
287	walk_rsdt(rsdt);
288	return 0;
289}
290