From owner-svn-src-all@freebsd.org Tue Aug 9 19:06:06 2016 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4B4BEBB400B; Tue, 9 Aug 2016 19:06:06 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 20F351608; Tue, 9 Aug 2016 19:06:06 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u79J65Yd058285; Tue, 9 Aug 2016 19:06:05 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u79J65Uq058283; Tue, 9 Aug 2016 19:06:05 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <201608091906.u79J65Uq058283@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Tue, 9 Aug 2016 19:06:05 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r303887 - head/tools/tools/dmardump X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Aug 2016 19:06:06 -0000 Author: jhb Date: Tue Aug 9 19:06:05 2016 New Revision: 303887 URL: https://svnweb.freebsd.org/changeset/base/303887 Log: Add a dmardump utility to dump the VT-d context tables. This tool parses the ACPI DMAR table looking for DMA remapping devices. For each device it walks the root table and any context tables referenced to display mapping info for PCI devices. Note that acpidump -t already parses the info in the ACPI DMAR tables directly. This tool examines some of the data structures the DMAR remapping engines use to translate DMA requests. Reviewed by: kib, grehan MFC after: 1 month Sponsored by: Chelsio Communications Differential Revision: https://reviews.freebsd.org/D7444 Added: head/tools/tools/dmardump/ head/tools/tools/dmardump/Makefile (contents, props changed) head/tools/tools/dmardump/dmardump.c (contents, props changed) Added: head/tools/tools/dmardump/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/tools/dmardump/Makefile Tue Aug 9 19:06:05 2016 (r303887) @@ -0,0 +1,15 @@ +# $FreeBSD$ + +PROG= dmardump +SRCS= dmardump.c +MAN= + +# Pull in bits from acpidump +ACPIDUMP=${.CURDIR}/../../../usr.sbin/acpi/acpidump +.PATH: ${ACPIDUMP} +SRCS+= acpi_user.c acpi.c +CFLAGS+= -I${ACPIDUMP} + +CFLAGS+= -I${.CURDIR}/../../../sys + +.include Added: head/tools/tools/dmardump/dmardump.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/tools/dmardump/dmardump.c Tue Aug 9 19:06:05 2016 (r303887) @@ -0,0 +1,292 @@ +/*- + * Copyright (c) 2016 Chelsio Communications, Inc. + * All rights reserved. + * Written by: John Baldwin + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "acpidump.h" + +int tflag; +int vflag; + +static uint32_t +read_4(char *regs, size_t offset) +{ + return *(uint32_t *)(regs + offset); +} + +static uint64_t +read_8(char *regs, size_t offset) +{ + return *(uint64_t *)(regs + offset); +} + +static struct pci_conf * +pci_find_conf(int segment, int bus, int slot, int func) +{ + static int pcifd = -1; + static struct pci_conf conf; + struct pci_conf_io pc; + struct pci_match_conf patterns[1]; + + if (pcifd == -1) { + pcifd = open("/dev/pci", O_RDONLY); + if (pcifd < 0) + err(1, "Failed to open /dev/pci"); + } + + bzero(&pc, sizeof(pc)); + pc.match_buf_len = sizeof(conf); + pc.matches = &conf; + bzero(&patterns, sizeof(patterns)); + patterns[0].pc_sel.pc_domain = segment; + patterns[0].pc_sel.pc_bus = bus; + patterns[0].pc_sel.pc_dev = slot; + patterns[0].pc_sel.pc_func = func; + patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | + PCI_GETCONF_MATCH_BUS | PCI_GETCONF_MATCH_DEV | + PCI_GETCONF_MATCH_FUNC; + pc.num_patterns = 1; + pc.pat_buf_len = sizeof(patterns); + pc.patterns = patterns; + if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1) + err(1, "ioctl(PCIOCGETCONF)"); + + if (pc.status != PCI_GETCONF_LAST_DEVICE || + pc.num_matches == 0) + return (NULL); + + return (&conf); +} + +static void +dump_context_table(int segment, int bus, uint64_t base_addr) +{ + struct dmar_ctx_entry *ctx; + struct pci_conf *conf; + bool printed; + int idx; + + printed = false; + ctx = acpi_map_physical(base_addr, DMAR_PAGE_SIZE); + for (idx = 0; idx < DMAR_CTX_CNT; idx++) { + if (!(ctx[idx].ctx1 & DMAR_CTX1_P)) + continue; + if (!printed) { + printf("\tPCI bus %d:\n", bus); + printed = true; + } + + /* Check for ARI device first. */ + conf = pci_find_conf(segment, bus, 0, idx); + if (conf == NULL) + conf = pci_find_conf(segment, bus, idx >> 3, idx & 7); + if (conf != NULL) { + printf("\t { %d,%d }", conf->pc_sel.pc_dev, + conf->pc_sel.pc_func); + if (conf->pd_name[0] != '\0') + printf(" (%s%lu)", conf->pd_name, + conf->pd_unit); + } else + printf("\t { %d,%d } (absent)", idx >> 3, + idx & 7); + if (ctx[idx].ctx1 & DMAR_CTX1_FPD) + printf(" FPD"); + switch (ctx[idx].ctx1 & 0xc) { + case DMAR_CTX1_T_UNTR: + printf(" UNTR"); + break; + case DMAR_CTX1_T_TR: + printf(" TR"); + break; + case DMAR_CTX1_T_PASS: + printf(" PASS"); + break; + default: + printf(" TT3?"); + break; + } + printf(" SLPT %#jx", (uintmax_t)(ctx[idx].ctx1 & + DMAR_CTX1_ASR_MASK)); + printf(" domain %d", (int)DMAR_CTX2_GET_DID(ctx[idx].ctx2)); + printf("\n"); + } +} + +static void +handle_drhd(int segment, uint64_t base_addr) +{ + struct dmar_root_entry *root_table; + char *regs; + uint64_t rtaddr; + uint32_t gsts, ver; + bool extended; + int bus; + + regs = acpi_map_physical(base_addr, 4096); + + ver = read_4(regs, DMAR_VER_REG); + gsts = read_4(regs, DMAR_GSTS_REG); + printf("drhd @ %#jx (version %d.%d) PCI segment %d%s:\n", + (uintmax_t)base_addr, DMAR_MAJOR_VER(ver), DMAR_MINOR_VER(ver), + segment, gsts & DMAR_GSTS_TES ? "" : " (disabled)"); + if ((gsts & (DMAR_GSTS_TES | DMAR_GSTS_RTPS)) != + (DMAR_GSTS_TES | DMAR_GSTS_RTPS)) + return; + rtaddr = read_8(regs, DMAR_RTADDR_REG); + extended = (rtaddr & DMAR_RTADDR_RTT) != 0; + printf(" %sroot table @ 0x%#jx\n", extended ? "extended " : "", + rtaddr & DMAR_RTADDR_RTA_MASK); + root_table = acpi_map_physical(rtaddr & DMAR_RTADDR_RTA_MASK, 4096); + for (bus = 0; bus < 255; bus++) { + if (extended) { +#ifdef notyet + if (root_table[bus].r1 & DMAR_ROOT_R1_P) + dump_ext_context_table(segment, bus, + root_table[bus].r1 & DMAR_ROOT_R1_CTP_MASK, + false); + if (root_table[bus].r2 & DMAR_ROOT_R1_P) + dump_ext_context_table(segment, bus, + root_table[bus].r2 & DMAR_ROOT_R1_CTP_MASK, + true); +#endif + } else if (root_table[bus].r1 & DMAR_ROOT_R1_P) + dump_context_table(segment, bus, root_table[bus].r1 & + DMAR_ROOT_R1_CTP_MASK); + } +} + +/* Borrowed from acpi.c in acpidump: */ + +static void +acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd) +{ + + handle_drhd(drhd->Segment, drhd->Address); +} + +static int +acpi_handle_dmar_remapping_structure(void *addr, int remaining) +{ + ACPI_DMAR_HEADER *hdr = addr; + + if (remaining < (int)sizeof(ACPI_DMAR_HEADER)) + return (-1); + + if (remaining < hdr->Length) + return (-1); + + switch (hdr->Type) { + case ACPI_DMAR_TYPE_HARDWARE_UNIT: + acpi_handle_dmar_drhd(addr); + break; + } + return (hdr->Length); +} + +static void +acpi_handle_dmar(ACPI_TABLE_HEADER *sdp) +{ + char *cp; + int remaining, consumed; + ACPI_TABLE_DMAR *dmar; + + dmar = (ACPI_TABLE_DMAR *)sdp; + remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR); + while (remaining > 0) { + cp = (char *)sdp + sdp->Length - remaining; + consumed = acpi_handle_dmar_remapping_structure(cp, remaining); + if (consumed <= 0) + break; + else + remaining -= consumed; + } +} + +static ACPI_TABLE_HEADER * +acpi_map_sdt(vm_offset_t pa) +{ + ACPI_TABLE_HEADER *sp; + + sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER)); + sp = acpi_map_physical(pa, sp->Length); + return (sp); +} + +static void +walk_rsdt(ACPI_TABLE_HEADER *rsdp) +{ + ACPI_TABLE_HEADER *sdp; + ACPI_TABLE_RSDT *rsdt; + ACPI_TABLE_XSDT *xsdt; + vm_offset_t addr; + int addr_size, entries, i; + + if (memcmp(rsdp->Signature, "RSDT", 4) != 0) + addr_size = sizeof(uint32_t); + else + addr_size = sizeof(uint64_t); + rsdt = (ACPI_TABLE_RSDT *)rsdp; + xsdt = (ACPI_TABLE_XSDT *)rsdp; + entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size; + for (i = 0; i < entries; i++) { + if (addr_size == 4) + addr = le32toh(rsdt->TableOffsetEntry[i]); + else + addr = le64toh(xsdt->TableOffsetEntry[i]); + if (addr == 0) + continue; + sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr); + if (acpi_checksum(sdp, sdp->Length)) { + continue; + } + if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4)) + acpi_handle_dmar(sdp); + } +} + +int +main(int argc __unused, char *argv[] __unused) +{ + ACPI_TABLE_HEADER *rsdt; + + rsdt = sdt_load_devmem(); + walk_rsdt(rsdt); + return 0; +}