186be9f0dSKonstantin Belousov /*- 2ebf5747bSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3ebf5747bSPedro F. Giffuni * 40a110d5bSKonstantin Belousov * Copyright (c) 2013-2015 The FreeBSD Foundation 586be9f0dSKonstantin Belousov * 686be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 786be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation. 886be9f0dSKonstantin Belousov * 986be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without 1086be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions 1186be9f0dSKonstantin Belousov * are met: 1286be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 1386be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer. 1486be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 1586be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 1686be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution. 1786be9f0dSKonstantin Belousov * 1886be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1986be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2086be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2186be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2286be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2386be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2486be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2586be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2686be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2786be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2886be9f0dSKonstantin Belousov * SUCH DAMAGE. 2986be9f0dSKonstantin Belousov */ 3086be9f0dSKonstantin Belousov 3186be9f0dSKonstantin Belousov #include <sys/cdefs.h> 3286be9f0dSKonstantin Belousov __FBSDID("$FreeBSD$"); 3386be9f0dSKonstantin Belousov 3486be9f0dSKonstantin Belousov #include "opt_acpi.h" 35e7d939bdSMarcel Moolenaar #if defined(__amd64__) 3686be9f0dSKonstantin Belousov #define DEV_APIC 3786be9f0dSKonstantin Belousov #else 3886be9f0dSKonstantin Belousov #include "opt_apic.h" 3986be9f0dSKonstantin Belousov #endif 4086be9f0dSKonstantin Belousov #include "opt_ddb.h" 4186be9f0dSKonstantin Belousov 4286be9f0dSKonstantin Belousov #include <sys/param.h> 4386be9f0dSKonstantin Belousov #include <sys/bus.h> 4486be9f0dSKonstantin Belousov #include <sys/kernel.h> 4586be9f0dSKonstantin Belousov #include <sys/lock.h> 4686be9f0dSKonstantin Belousov #include <sys/malloc.h> 4786be9f0dSKonstantin Belousov #include <sys/memdesc.h> 4886be9f0dSKonstantin Belousov #include <sys/module.h> 49e2e050c8SConrad Meyer #include <sys/mutex.h> 5086be9f0dSKonstantin Belousov #include <sys/rman.h> 5186be9f0dSKonstantin Belousov #include <sys/rwlock.h> 5286be9f0dSKonstantin Belousov #include <sys/smp.h> 5386be9f0dSKonstantin Belousov #include <sys/taskqueue.h> 5486be9f0dSKonstantin Belousov #include <sys/tree.h> 550a110d5bSKonstantin Belousov #include <sys/vmem.h> 5686be9f0dSKonstantin Belousov #include <vm/vm.h> 5786be9f0dSKonstantin Belousov #include <vm/vm_extern.h> 5886be9f0dSKonstantin Belousov #include <vm/vm_kern.h> 5986be9f0dSKonstantin Belousov #include <vm/vm_object.h> 6086be9f0dSKonstantin Belousov #include <vm/vm_page.h> 6186be9f0dSKonstantin Belousov #include <vm/vm_pager.h> 6286be9f0dSKonstantin Belousov #include <vm/vm_map.h> 63c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/acpi.h> 64c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/accommon.h> 65c8597a1fSRuslan Bukin #include <dev/acpica/acpivar.h> 660a110d5bSKonstantin Belousov #include <dev/pci/pcireg.h> 6786be9f0dSKonstantin Belousov #include <dev/pci/pcivar.h> 68c8597a1fSRuslan Bukin #include <machine/bus.h> 69c8597a1fSRuslan Bukin #include <machine/pci_cfgreg.h> 70c8597a1fSRuslan Bukin #include <x86/include/busdma_impl.h> 71c8597a1fSRuslan Bukin #include <dev/iommu/busdma_iommu.h> 72c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h> 73685666aaSKonstantin Belousov #include <x86/iommu/intel_dmar.h> 7486be9f0dSKonstantin Belousov 7586be9f0dSKonstantin Belousov #ifdef DEV_APIC 7686be9f0dSKonstantin Belousov #include "pcib_if.h" 77fd15fee1SKonstantin Belousov #include <machine/intr_machdep.h> 78fd15fee1SKonstantin Belousov #include <x86/apicreg.h> 79fd15fee1SKonstantin Belousov #include <x86/apicvar.h> 8086be9f0dSKonstantin Belousov #endif 8186be9f0dSKonstantin Belousov 8268eeb96aSKonstantin Belousov #define DMAR_FAULT_IRQ_RID 0 8368eeb96aSKonstantin Belousov #define DMAR_QI_IRQ_RID 1 8468eeb96aSKonstantin Belousov #define DMAR_REG_RID 2 8586be9f0dSKonstantin Belousov 8686be9f0dSKonstantin Belousov static device_t *dmar_devs; 8786be9f0dSKonstantin Belousov static int dmar_devcnt; 8886be9f0dSKonstantin Belousov 8986be9f0dSKonstantin Belousov typedef int (*dmar_iter_t)(ACPI_DMAR_HEADER *, void *); 9086be9f0dSKonstantin Belousov 9186be9f0dSKonstantin Belousov static void 9286be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_iter_t iter, void *arg) 9386be9f0dSKonstantin Belousov { 9486be9f0dSKonstantin Belousov ACPI_TABLE_DMAR *dmartbl; 9586be9f0dSKonstantin Belousov ACPI_DMAR_HEADER *dmarh; 9686be9f0dSKonstantin Belousov char *ptr, *ptrend; 9786be9f0dSKonstantin Belousov ACPI_STATUS status; 9886be9f0dSKonstantin Belousov 9986be9f0dSKonstantin Belousov status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl); 10086be9f0dSKonstantin Belousov if (ACPI_FAILURE(status)) 10186be9f0dSKonstantin Belousov return; 10286be9f0dSKonstantin Belousov ptr = (char *)dmartbl + sizeof(*dmartbl); 10386be9f0dSKonstantin Belousov ptrend = (char *)dmartbl + dmartbl->Header.Length; 10486be9f0dSKonstantin Belousov for (;;) { 10586be9f0dSKonstantin Belousov if (ptr >= ptrend) 10686be9f0dSKonstantin Belousov break; 10786be9f0dSKonstantin Belousov dmarh = (ACPI_DMAR_HEADER *)ptr; 10886be9f0dSKonstantin Belousov if (dmarh->Length <= 0) { 10986be9f0dSKonstantin Belousov printf("dmar_identify: corrupted DMAR table, l %d\n", 11086be9f0dSKonstantin Belousov dmarh->Length); 11186be9f0dSKonstantin Belousov break; 11286be9f0dSKonstantin Belousov } 11386be9f0dSKonstantin Belousov ptr += dmarh->Length; 11486be9f0dSKonstantin Belousov if (!iter(dmarh, arg)) 11586be9f0dSKonstantin Belousov break; 11686be9f0dSKonstantin Belousov } 1173dd3c450SKonstantin Belousov AcpiPutTable((ACPI_TABLE_HEADER *)dmartbl); 11886be9f0dSKonstantin Belousov } 11986be9f0dSKonstantin Belousov 12086be9f0dSKonstantin Belousov struct find_iter_args { 12186be9f0dSKonstantin Belousov int i; 12286be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *res; 12386be9f0dSKonstantin Belousov }; 12486be9f0dSKonstantin Belousov 12586be9f0dSKonstantin Belousov static int 12686be9f0dSKonstantin Belousov dmar_find_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 12786be9f0dSKonstantin Belousov { 12886be9f0dSKonstantin Belousov struct find_iter_args *fia; 12986be9f0dSKonstantin Belousov 13086be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_HARDWARE_UNIT) 13186be9f0dSKonstantin Belousov return (1); 13286be9f0dSKonstantin Belousov 13386be9f0dSKonstantin Belousov fia = arg; 13486be9f0dSKonstantin Belousov if (fia->i == 0) { 13586be9f0dSKonstantin Belousov fia->res = (ACPI_DMAR_HARDWARE_UNIT *)dmarh; 13686be9f0dSKonstantin Belousov return (0); 13786be9f0dSKonstantin Belousov } 13886be9f0dSKonstantin Belousov fia->i--; 13986be9f0dSKonstantin Belousov return (1); 14086be9f0dSKonstantin Belousov } 14186be9f0dSKonstantin Belousov 14286be9f0dSKonstantin Belousov static ACPI_DMAR_HARDWARE_UNIT * 14386be9f0dSKonstantin Belousov dmar_find_by_index(int idx) 14486be9f0dSKonstantin Belousov { 14586be9f0dSKonstantin Belousov struct find_iter_args fia; 14686be9f0dSKonstantin Belousov 14786be9f0dSKonstantin Belousov fia.i = idx; 14886be9f0dSKonstantin Belousov fia.res = NULL; 14986be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_find_iter, &fia); 15086be9f0dSKonstantin Belousov return (fia.res); 15186be9f0dSKonstantin Belousov } 15286be9f0dSKonstantin Belousov 15386be9f0dSKonstantin Belousov static int 15486be9f0dSKonstantin Belousov dmar_count_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 15586be9f0dSKonstantin Belousov { 15686be9f0dSKonstantin Belousov 15786be9f0dSKonstantin Belousov if (dmarh->Type == ACPI_DMAR_TYPE_HARDWARE_UNIT) 15886be9f0dSKonstantin Belousov dmar_devcnt++; 15986be9f0dSKonstantin Belousov return (1); 16086be9f0dSKonstantin Belousov } 16186be9f0dSKonstantin Belousov 16286be9f0dSKonstantin Belousov static int dmar_enable = 0; 16386be9f0dSKonstantin Belousov static void 16486be9f0dSKonstantin Belousov dmar_identify(driver_t *driver, device_t parent) 16586be9f0dSKonstantin Belousov { 16686be9f0dSKonstantin Belousov ACPI_TABLE_DMAR *dmartbl; 16786be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh; 16886be9f0dSKonstantin Belousov ACPI_STATUS status; 16986be9f0dSKonstantin Belousov int i, error; 17086be9f0dSKonstantin Belousov 17186be9f0dSKonstantin Belousov if (acpi_disabled("dmar")) 17286be9f0dSKonstantin Belousov return; 17386be9f0dSKonstantin Belousov TUNABLE_INT_FETCH("hw.dmar.enable", &dmar_enable); 17486be9f0dSKonstantin Belousov if (!dmar_enable) 17586be9f0dSKonstantin Belousov return; 17686be9f0dSKonstantin Belousov status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl); 17786be9f0dSKonstantin Belousov if (ACPI_FAILURE(status)) 17886be9f0dSKonstantin Belousov return; 17986be9f0dSKonstantin Belousov haw = dmartbl->Width + 1; 18086be9f0dSKonstantin Belousov if ((1ULL << (haw + 1)) > BUS_SPACE_MAXADDR) 18186be9f0dSKonstantin Belousov dmar_high = BUS_SPACE_MAXADDR; 18286be9f0dSKonstantin Belousov else 18386be9f0dSKonstantin Belousov dmar_high = 1ULL << (haw + 1); 18486be9f0dSKonstantin Belousov if (bootverbose) { 18586be9f0dSKonstantin Belousov printf("DMAR HAW=%d flags=<%b>\n", dmartbl->Width, 18686be9f0dSKonstantin Belousov (unsigned)dmartbl->Flags, 18786be9f0dSKonstantin Belousov "\020\001INTR_REMAP\002X2APIC_OPT_OUT"); 18886be9f0dSKonstantin Belousov } 1893dd3c450SKonstantin Belousov AcpiPutTable((ACPI_TABLE_HEADER *)dmartbl); 19086be9f0dSKonstantin Belousov 19186be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_count_iter, NULL); 19286be9f0dSKonstantin Belousov if (dmar_devcnt == 0) 19386be9f0dSKonstantin Belousov return; 19486be9f0dSKonstantin Belousov dmar_devs = malloc(sizeof(device_t) * dmar_devcnt, M_DEVBUF, 19586be9f0dSKonstantin Belousov M_WAITOK | M_ZERO); 19686be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 19786be9f0dSKonstantin Belousov dmarh = dmar_find_by_index(i); 19886be9f0dSKonstantin Belousov if (dmarh == NULL) { 19986be9f0dSKonstantin Belousov printf("dmar_identify: cannot find HWUNIT %d\n", i); 20086be9f0dSKonstantin Belousov continue; 20186be9f0dSKonstantin Belousov } 20286be9f0dSKonstantin Belousov dmar_devs[i] = BUS_ADD_CHILD(parent, 1, "dmar", i); 20386be9f0dSKonstantin Belousov if (dmar_devs[i] == NULL) { 20486be9f0dSKonstantin Belousov printf("dmar_identify: cannot create instance %d\n", i); 20586be9f0dSKonstantin Belousov continue; 20686be9f0dSKonstantin Belousov } 20786be9f0dSKonstantin Belousov error = bus_set_resource(dmar_devs[i], SYS_RES_MEMORY, 20886be9f0dSKonstantin Belousov DMAR_REG_RID, dmarh->Address, PAGE_SIZE); 20986be9f0dSKonstantin Belousov if (error != 0) { 21086be9f0dSKonstantin Belousov printf( 21186be9f0dSKonstantin Belousov "dmar%d: unable to alloc register window at 0x%08jx: error %d\n", 21286be9f0dSKonstantin Belousov i, (uintmax_t)dmarh->Address, error); 21386be9f0dSKonstantin Belousov device_delete_child(parent, dmar_devs[i]); 21486be9f0dSKonstantin Belousov dmar_devs[i] = NULL; 21586be9f0dSKonstantin Belousov } 21686be9f0dSKonstantin Belousov } 21786be9f0dSKonstantin Belousov } 21886be9f0dSKonstantin Belousov 21986be9f0dSKonstantin Belousov static int 22086be9f0dSKonstantin Belousov dmar_probe(device_t dev) 22186be9f0dSKonstantin Belousov { 22286be9f0dSKonstantin Belousov 22386be9f0dSKonstantin Belousov if (acpi_get_handle(dev) != NULL) 22486be9f0dSKonstantin Belousov return (ENXIO); 22586be9f0dSKonstantin Belousov device_set_desc(dev, "DMA remap"); 2263100f7dfSKonstantin Belousov return (BUS_PROBE_NOWILDCARD); 22786be9f0dSKonstantin Belousov } 22886be9f0dSKonstantin Belousov 22986be9f0dSKonstantin Belousov static void 23068eeb96aSKonstantin Belousov dmar_release_intr(device_t dev, struct dmar_unit *unit, int idx) 23168eeb96aSKonstantin Belousov { 23268eeb96aSKonstantin Belousov struct dmar_msi_data *dmd; 23368eeb96aSKonstantin Belousov 23468eeb96aSKonstantin Belousov dmd = &unit->intrs[idx]; 23568eeb96aSKonstantin Belousov if (dmd->irq == -1) 23668eeb96aSKonstantin Belousov return; 23768eeb96aSKonstantin Belousov bus_teardown_intr(dev, dmd->irq_res, dmd->intr_handle); 23868eeb96aSKonstantin Belousov bus_release_resource(dev, SYS_RES_IRQ, dmd->irq_rid, dmd->irq_res); 23968eeb96aSKonstantin Belousov bus_delete_resource(dev, SYS_RES_IRQ, dmd->irq_rid); 24068eeb96aSKonstantin Belousov PCIB_RELEASE_MSIX(device_get_parent(device_get_parent(dev)), 24168eeb96aSKonstantin Belousov dev, dmd->irq); 24268eeb96aSKonstantin Belousov dmd->irq = -1; 24368eeb96aSKonstantin Belousov } 24468eeb96aSKonstantin Belousov 24568eeb96aSKonstantin Belousov static void 24686be9f0dSKonstantin Belousov dmar_release_resources(device_t dev, struct dmar_unit *unit) 24786be9f0dSKonstantin Belousov { 24868eeb96aSKonstantin Belousov int i; 24986be9f0dSKonstantin Belousov 25059e37c8aSRuslan Bukin iommu_fini_busdma(&unit->iommu); 2510a110d5bSKonstantin Belousov dmar_fini_irt(unit); 25268eeb96aSKonstantin Belousov dmar_fini_qi(unit); 25386be9f0dSKonstantin Belousov dmar_fini_fault_log(unit); 25468eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++) 25568eeb96aSKonstantin Belousov dmar_release_intr(dev, unit, i); 25686be9f0dSKonstantin Belousov if (unit->regs != NULL) { 25786be9f0dSKonstantin Belousov bus_deactivate_resource(dev, SYS_RES_MEMORY, unit->reg_rid, 25886be9f0dSKonstantin Belousov unit->regs); 25986be9f0dSKonstantin Belousov bus_release_resource(dev, SYS_RES_MEMORY, unit->reg_rid, 26086be9f0dSKonstantin Belousov unit->regs); 26186be9f0dSKonstantin Belousov unit->regs = NULL; 26286be9f0dSKonstantin Belousov } 26386be9f0dSKonstantin Belousov if (unit->domids != NULL) { 26486be9f0dSKonstantin Belousov delete_unrhdr(unit->domids); 26586be9f0dSKonstantin Belousov unit->domids = NULL; 26686be9f0dSKonstantin Belousov } 26786be9f0dSKonstantin Belousov if (unit->ctx_obj != NULL) { 26886be9f0dSKonstantin Belousov vm_object_deallocate(unit->ctx_obj); 26986be9f0dSKonstantin Belousov unit->ctx_obj = NULL; 27086be9f0dSKonstantin Belousov } 27186be9f0dSKonstantin Belousov } 27286be9f0dSKonstantin Belousov 27386be9f0dSKonstantin Belousov static int 27468eeb96aSKonstantin Belousov dmar_alloc_irq(device_t dev, struct dmar_unit *unit, int idx) 27586be9f0dSKonstantin Belousov { 27686be9f0dSKonstantin Belousov device_t pcib; 27768eeb96aSKonstantin Belousov struct dmar_msi_data *dmd; 27886be9f0dSKonstantin Belousov uint64_t msi_addr; 27986be9f0dSKonstantin Belousov uint32_t msi_data; 28086be9f0dSKonstantin Belousov int error; 28186be9f0dSKonstantin Belousov 28268eeb96aSKonstantin Belousov dmd = &unit->intrs[idx]; 28386be9f0dSKonstantin Belousov pcib = device_get_parent(device_get_parent(dev)); /* Really not pcib */ 28468eeb96aSKonstantin Belousov error = PCIB_ALLOC_MSIX(pcib, dev, &dmd->irq); 28586be9f0dSKonstantin Belousov if (error != 0) { 28668eeb96aSKonstantin Belousov device_printf(dev, "cannot allocate %s interrupt, %d\n", 28768eeb96aSKonstantin Belousov dmd->name, error); 28886be9f0dSKonstantin Belousov goto err1; 28986be9f0dSKonstantin Belousov } 29068eeb96aSKonstantin Belousov error = bus_set_resource(dev, SYS_RES_IRQ, dmd->irq_rid, 29168eeb96aSKonstantin Belousov dmd->irq, 1); 29286be9f0dSKonstantin Belousov if (error != 0) { 29368eeb96aSKonstantin Belousov device_printf(dev, "cannot set %s interrupt resource, %d\n", 29468eeb96aSKonstantin Belousov dmd->name, error); 29586be9f0dSKonstantin Belousov goto err2; 29686be9f0dSKonstantin Belousov } 29768eeb96aSKonstantin Belousov dmd->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 29868eeb96aSKonstantin Belousov &dmd->irq_rid, RF_ACTIVE); 29968eeb96aSKonstantin Belousov if (dmd->irq_res == NULL) { 30068eeb96aSKonstantin Belousov device_printf(dev, 30168eeb96aSKonstantin Belousov "cannot allocate resource for %s interrupt\n", dmd->name); 30286be9f0dSKonstantin Belousov error = ENXIO; 30386be9f0dSKonstantin Belousov goto err3; 30486be9f0dSKonstantin Belousov } 30568eeb96aSKonstantin Belousov error = bus_setup_intr(dev, dmd->irq_res, INTR_TYPE_MISC, 30668eeb96aSKonstantin Belousov dmd->handler, NULL, unit, &dmd->intr_handle); 30786be9f0dSKonstantin Belousov if (error != 0) { 30868eeb96aSKonstantin Belousov device_printf(dev, "cannot setup %s interrupt, %d\n", 30968eeb96aSKonstantin Belousov dmd->name, error); 31086be9f0dSKonstantin Belousov goto err4; 31186be9f0dSKonstantin Belousov } 312f454e7ebSJohn Baldwin bus_describe_intr(dev, dmd->irq_res, dmd->intr_handle, "%s", dmd->name); 31368eeb96aSKonstantin Belousov error = PCIB_MAP_MSI(pcib, dev, dmd->irq, &msi_addr, &msi_data); 31486be9f0dSKonstantin Belousov if (error != 0) { 31568eeb96aSKonstantin Belousov device_printf(dev, "cannot map %s interrupt, %d\n", 31668eeb96aSKonstantin Belousov dmd->name, error); 31786be9f0dSKonstantin Belousov goto err5; 31886be9f0dSKonstantin Belousov } 31968eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_data_reg, msi_data); 32068eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_addr_reg, msi_addr); 32186be9f0dSKonstantin Belousov /* Only for xAPIC mode */ 32268eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_uaddr_reg, msi_addr >> 32); 32386be9f0dSKonstantin Belousov return (0); 32486be9f0dSKonstantin Belousov 32586be9f0dSKonstantin Belousov err5: 32668eeb96aSKonstantin Belousov bus_teardown_intr(dev, dmd->irq_res, dmd->intr_handle); 32786be9f0dSKonstantin Belousov err4: 32868eeb96aSKonstantin Belousov bus_release_resource(dev, SYS_RES_IRQ, dmd->irq_rid, dmd->irq_res); 32986be9f0dSKonstantin Belousov err3: 33068eeb96aSKonstantin Belousov bus_delete_resource(dev, SYS_RES_IRQ, dmd->irq_rid); 33186be9f0dSKonstantin Belousov err2: 33268eeb96aSKonstantin Belousov PCIB_RELEASE_MSIX(pcib, dev, dmd->irq); 33368eeb96aSKonstantin Belousov dmd->irq = -1; 33486be9f0dSKonstantin Belousov err1: 33586be9f0dSKonstantin Belousov return (error); 33686be9f0dSKonstantin Belousov } 33786be9f0dSKonstantin Belousov 33886be9f0dSKonstantin Belousov #ifdef DEV_APIC 33986be9f0dSKonstantin Belousov static int 34086be9f0dSKonstantin Belousov dmar_remap_intr(device_t dev, device_t child, u_int irq) 34186be9f0dSKonstantin Belousov { 34286be9f0dSKonstantin Belousov struct dmar_unit *unit; 34368eeb96aSKonstantin Belousov struct dmar_msi_data *dmd; 34486be9f0dSKonstantin Belousov uint64_t msi_addr; 34586be9f0dSKonstantin Belousov uint32_t msi_data; 34668eeb96aSKonstantin Belousov int i, error; 34786be9f0dSKonstantin Belousov 34886be9f0dSKonstantin Belousov unit = device_get_softc(dev); 34968eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++) { 35068eeb96aSKonstantin Belousov dmd = &unit->intrs[i]; 35168eeb96aSKonstantin Belousov if (irq == dmd->irq) { 35268eeb96aSKonstantin Belousov error = PCIB_MAP_MSI(device_get_parent( 35368eeb96aSKonstantin Belousov device_get_parent(dev)), 35468eeb96aSKonstantin Belousov dev, irq, &msi_addr, &msi_data); 35586be9f0dSKonstantin Belousov if (error != 0) 35686be9f0dSKonstantin Belousov return (error); 35768eeb96aSKonstantin Belousov DMAR_LOCK(unit); 35868eeb96aSKonstantin Belousov (dmd->disable_intr)(unit); 35968eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_data_reg, msi_data); 36068eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_addr_reg, msi_addr); 36168eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_uaddr_reg, msi_addr >> 32); 36268eeb96aSKonstantin Belousov (dmd->enable_intr)(unit); 36368eeb96aSKonstantin Belousov DMAR_UNLOCK(unit); 36486be9f0dSKonstantin Belousov return (0); 36586be9f0dSKonstantin Belousov } 36668eeb96aSKonstantin Belousov } 36768eeb96aSKonstantin Belousov return (ENOENT); 36868eeb96aSKonstantin Belousov } 36986be9f0dSKonstantin Belousov #endif 37086be9f0dSKonstantin Belousov 37186be9f0dSKonstantin Belousov static void 37286be9f0dSKonstantin Belousov dmar_print_caps(device_t dev, struct dmar_unit *unit, 37386be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmaru) 37486be9f0dSKonstantin Belousov { 37586be9f0dSKonstantin Belousov uint32_t caphi, ecaphi; 37686be9f0dSKonstantin Belousov 37786be9f0dSKonstantin Belousov device_printf(dev, "regs@0x%08jx, ver=%d.%d, seg=%d, flags=<%b>\n", 37886be9f0dSKonstantin Belousov (uintmax_t)dmaru->Address, DMAR_MAJOR_VER(unit->hw_ver), 37986be9f0dSKonstantin Belousov DMAR_MINOR_VER(unit->hw_ver), dmaru->Segment, 38086be9f0dSKonstantin Belousov dmaru->Flags, "\020\001INCLUDE_ALL_PCI"); 38186be9f0dSKonstantin Belousov caphi = unit->hw_cap >> 32; 38286be9f0dSKonstantin Belousov device_printf(dev, "cap=%b,", (u_int)unit->hw_cap, 38386be9f0dSKonstantin Belousov "\020\004AFL\005WBF\006PLMR\007PHMR\010CM\027ZLR\030ISOCH"); 384e17c0a1eSKonstantin Belousov printf("%b, ", caphi, "\020\010PSI\027DWD\030DRD\031FL1GP\034PSI"); 38586be9f0dSKonstantin Belousov printf("ndoms=%d, sagaw=%d, mgaw=%d, fro=%d, nfr=%d, superp=%d", 38686be9f0dSKonstantin Belousov DMAR_CAP_ND(unit->hw_cap), DMAR_CAP_SAGAW(unit->hw_cap), 38786be9f0dSKonstantin Belousov DMAR_CAP_MGAW(unit->hw_cap), DMAR_CAP_FRO(unit->hw_cap), 38886be9f0dSKonstantin Belousov DMAR_CAP_NFR(unit->hw_cap), DMAR_CAP_SPS(unit->hw_cap)); 38986be9f0dSKonstantin Belousov if ((unit->hw_cap & DMAR_CAP_PSI) != 0) 39086be9f0dSKonstantin Belousov printf(", mamv=%d", DMAR_CAP_MAMV(unit->hw_cap)); 39186be9f0dSKonstantin Belousov printf("\n"); 39286be9f0dSKonstantin Belousov ecaphi = unit->hw_ecap >> 32; 39386be9f0dSKonstantin Belousov device_printf(dev, "ecap=%b,", (u_int)unit->hw_ecap, 394e17c0a1eSKonstantin Belousov "\020\001C\002QI\003DI\004IR\005EIM\007PT\010SC\031ECS\032MTS" 395e17c0a1eSKonstantin Belousov "\033NEST\034DIS\035PASID\036PRS\037ERS\040SRS"); 396e17c0a1eSKonstantin Belousov printf("%b, ", ecaphi, "\020\002NWFS\003EAFS"); 39786be9f0dSKonstantin Belousov printf("mhmw=%d, iro=%d\n", DMAR_ECAP_MHMV(unit->hw_ecap), 39886be9f0dSKonstantin Belousov DMAR_ECAP_IRO(unit->hw_ecap)); 39986be9f0dSKonstantin Belousov } 40086be9f0dSKonstantin Belousov 40186be9f0dSKonstantin Belousov static int 40286be9f0dSKonstantin Belousov dmar_attach(device_t dev) 40386be9f0dSKonstantin Belousov { 40486be9f0dSKonstantin Belousov struct dmar_unit *unit; 40586be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmaru; 406476358b3SKonstantin Belousov uint64_t timeout; 40706f659c3SKornel Duleba int disable_pmr; 40868eeb96aSKonstantin Belousov int i, error; 40986be9f0dSKonstantin Belousov 41086be9f0dSKonstantin Belousov unit = device_get_softc(dev); 41186be9f0dSKonstantin Belousov unit->dev = dev; 41259e37c8aSRuslan Bukin unit->iommu.unit = device_get_unit(dev); 413f5931169SRuslan Bukin unit->iommu.dev = dev; 41459e37c8aSRuslan Bukin dmaru = dmar_find_by_index(unit->iommu.unit); 41586be9f0dSKonstantin Belousov if (dmaru == NULL) 41686be9f0dSKonstantin Belousov return (EINVAL); 41786be9f0dSKonstantin Belousov unit->segment = dmaru->Segment; 41886be9f0dSKonstantin Belousov unit->base = dmaru->Address; 41986be9f0dSKonstantin Belousov unit->reg_rid = DMAR_REG_RID; 42086be9f0dSKonstantin Belousov unit->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 42186be9f0dSKonstantin Belousov &unit->reg_rid, RF_ACTIVE); 42286be9f0dSKonstantin Belousov if (unit->regs == NULL) { 42386be9f0dSKonstantin Belousov device_printf(dev, "cannot allocate register window\n"); 42486be9f0dSKonstantin Belousov return (ENOMEM); 42586be9f0dSKonstantin Belousov } 42686be9f0dSKonstantin Belousov unit->hw_ver = dmar_read4(unit, DMAR_VER_REG); 42786be9f0dSKonstantin Belousov unit->hw_cap = dmar_read8(unit, DMAR_CAP_REG); 42886be9f0dSKonstantin Belousov unit->hw_ecap = dmar_read8(unit, DMAR_ECAP_REG); 42986be9f0dSKonstantin Belousov if (bootverbose) 43086be9f0dSKonstantin Belousov dmar_print_caps(dev, unit, dmaru); 43186be9f0dSKonstantin Belousov dmar_quirks_post_ident(unit); 43286be9f0dSKonstantin Belousov 433476358b3SKonstantin Belousov timeout = dmar_get_timeout(); 434476358b3SKonstantin Belousov TUNABLE_UINT64_FETCH("hw.dmar.timeout", &timeout); 435476358b3SKonstantin Belousov dmar_update_timeout(timeout); 436476358b3SKonstantin Belousov 43768eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++) 43868eeb96aSKonstantin Belousov unit->intrs[i].irq = -1; 43968eeb96aSKonstantin Belousov 44068eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].name = "fault"; 44168eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].irq_rid = DMAR_FAULT_IRQ_RID; 44268eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].handler = dmar_fault_intr; 44368eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].msi_data_reg = DMAR_FEDATA_REG; 44468eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].msi_addr_reg = DMAR_FEADDR_REG; 44568eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].msi_uaddr_reg = DMAR_FEUADDR_REG; 44668eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].enable_intr = dmar_enable_fault_intr; 44768eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].disable_intr = dmar_disable_fault_intr; 44868eeb96aSKonstantin Belousov error = dmar_alloc_irq(dev, unit, DMAR_INTR_FAULT); 44986be9f0dSKonstantin Belousov if (error != 0) { 45086be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 45186be9f0dSKonstantin Belousov return (error); 45286be9f0dSKonstantin Belousov } 45368eeb96aSKonstantin Belousov if (DMAR_HAS_QI(unit)) { 45468eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].name = "qi"; 45568eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].irq_rid = DMAR_QI_IRQ_RID; 45668eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].handler = dmar_qi_intr; 45768eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].msi_data_reg = DMAR_IEDATA_REG; 45868eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].msi_addr_reg = DMAR_IEADDR_REG; 45968eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].msi_uaddr_reg = DMAR_IEUADDR_REG; 46068eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].enable_intr = dmar_enable_qi_intr; 46168eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].disable_intr = dmar_disable_qi_intr; 46268eeb96aSKonstantin Belousov error = dmar_alloc_irq(dev, unit, DMAR_INTR_QI); 46368eeb96aSKonstantin Belousov if (error != 0) { 46468eeb96aSKonstantin Belousov dmar_release_resources(dev, unit); 46568eeb96aSKonstantin Belousov return (error); 46668eeb96aSKonstantin Belousov } 46768eeb96aSKonstantin Belousov } 46868eeb96aSKonstantin Belousov 46959e37c8aSRuslan Bukin mtx_init(&unit->iommu.lock, "dmarhw", NULL, MTX_DEF); 47086be9f0dSKonstantin Belousov unit->domids = new_unrhdr(0, dmar_nd2mask(DMAR_CAP_ND(unit->hw_cap)), 47159e37c8aSRuslan Bukin &unit->iommu.lock); 4721abfd355SKonstantin Belousov LIST_INIT(&unit->domains); 47386be9f0dSKonstantin Belousov 47486be9f0dSKonstantin Belousov /* 47586be9f0dSKonstantin Belousov * 9.2 "Context Entry": 47686be9f0dSKonstantin Belousov * When Caching Mode (CM) field is reported as Set, the 47786be9f0dSKonstantin Belousov * domain-id value of zero is architecturally reserved. 47886be9f0dSKonstantin Belousov * Software must not use domain-id value of zero 47986be9f0dSKonstantin Belousov * when CM is Set. 48086be9f0dSKonstantin Belousov */ 48186be9f0dSKonstantin Belousov if ((unit->hw_cap & DMAR_CAP_CM) != 0) 48286be9f0dSKonstantin Belousov alloc_unr_specific(unit->domids, 0); 48386be9f0dSKonstantin Belousov 48486be9f0dSKonstantin Belousov unit->ctx_obj = vm_pager_allocate(OBJT_PHYS, NULL, IDX_TO_OFF(1 + 48586be9f0dSKonstantin Belousov DMAR_CTX_CNT), 0, 0, NULL); 48686be9f0dSKonstantin Belousov 48786be9f0dSKonstantin Belousov /* 48886be9f0dSKonstantin Belousov * Allocate and load the root entry table pointer. Enable the 48986be9f0dSKonstantin Belousov * address translation after the required invalidations are 49086be9f0dSKonstantin Belousov * done. 49186be9f0dSKonstantin Belousov */ 49215f6baf4SRuslan Bukin dmar_pgalloc(unit->ctx_obj, 0, IOMMU_PGF_WAITOK | IOMMU_PGF_ZERO); 49386be9f0dSKonstantin Belousov DMAR_LOCK(unit); 49486be9f0dSKonstantin Belousov error = dmar_load_root_entry_ptr(unit); 49586be9f0dSKonstantin Belousov if (error != 0) { 49686be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 49786be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 49886be9f0dSKonstantin Belousov return (error); 49986be9f0dSKonstantin Belousov } 50086be9f0dSKonstantin Belousov error = dmar_inv_ctx_glob(unit); 50186be9f0dSKonstantin Belousov if (error != 0) { 50286be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 50386be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 50486be9f0dSKonstantin Belousov return (error); 50586be9f0dSKonstantin Belousov } 50686be9f0dSKonstantin Belousov if ((unit->hw_ecap & DMAR_ECAP_DI) != 0) { 50786be9f0dSKonstantin Belousov error = dmar_inv_iotlb_glob(unit); 50886be9f0dSKonstantin Belousov if (error != 0) { 50986be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 51086be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 51186be9f0dSKonstantin Belousov return (error); 51286be9f0dSKonstantin Belousov } 51386be9f0dSKonstantin Belousov } 51486be9f0dSKonstantin Belousov 51586be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 51686be9f0dSKonstantin Belousov error = dmar_init_fault_log(unit); 51786be9f0dSKonstantin Belousov if (error != 0) { 51886be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 51986be9f0dSKonstantin Belousov return (error); 52086be9f0dSKonstantin Belousov } 52168eeb96aSKonstantin Belousov error = dmar_init_qi(unit); 52268eeb96aSKonstantin Belousov if (error != 0) { 52368eeb96aSKonstantin Belousov dmar_release_resources(dev, unit); 52468eeb96aSKonstantin Belousov return (error); 52568eeb96aSKonstantin Belousov } 5260a110d5bSKonstantin Belousov error = dmar_init_irt(unit); 5270a110d5bSKonstantin Belousov if (error != 0) { 5280a110d5bSKonstantin Belousov dmar_release_resources(dev, unit); 5290a110d5bSKonstantin Belousov return (error); 5300a110d5bSKonstantin Belousov } 53106f659c3SKornel Duleba 53206f659c3SKornel Duleba disable_pmr = 0; 53306f659c3SKornel Duleba TUNABLE_INT_FETCH("hw.dmar.pmr.disable", &disable_pmr); 53406f659c3SKornel Duleba if (disable_pmr) { 53506f659c3SKornel Duleba error = dmar_disable_protected_regions(unit); 53606f659c3SKornel Duleba if (error != 0) 53706f659c3SKornel Duleba device_printf(dev, 53806f659c3SKornel Duleba "Failed to disable protected regions\n"); 53906f659c3SKornel Duleba } 54006f659c3SKornel Duleba 54159e37c8aSRuslan Bukin error = iommu_init_busdma(&unit->iommu); 54286be9f0dSKonstantin Belousov if (error != 0) { 54386be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 54486be9f0dSKonstantin Belousov return (error); 54586be9f0dSKonstantin Belousov } 54686be9f0dSKonstantin Belousov 54786be9f0dSKonstantin Belousov #ifdef NOTYET 54886be9f0dSKonstantin Belousov DMAR_LOCK(unit); 54986be9f0dSKonstantin Belousov error = dmar_enable_translation(unit); 55086be9f0dSKonstantin Belousov if (error != 0) { 55186be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 55286be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 55386be9f0dSKonstantin Belousov return (error); 55486be9f0dSKonstantin Belousov } 55586be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 55686be9f0dSKonstantin Belousov #endif 55786be9f0dSKonstantin Belousov 55886be9f0dSKonstantin Belousov return (0); 55986be9f0dSKonstantin Belousov } 56086be9f0dSKonstantin Belousov 56186be9f0dSKonstantin Belousov static int 56286be9f0dSKonstantin Belousov dmar_detach(device_t dev) 56386be9f0dSKonstantin Belousov { 56486be9f0dSKonstantin Belousov 56586be9f0dSKonstantin Belousov return (EBUSY); 56686be9f0dSKonstantin Belousov } 56786be9f0dSKonstantin Belousov 56886be9f0dSKonstantin Belousov static int 56986be9f0dSKonstantin Belousov dmar_suspend(device_t dev) 57086be9f0dSKonstantin Belousov { 57186be9f0dSKonstantin Belousov 57286be9f0dSKonstantin Belousov return (0); 57386be9f0dSKonstantin Belousov } 57486be9f0dSKonstantin Belousov 57586be9f0dSKonstantin Belousov static int 57686be9f0dSKonstantin Belousov dmar_resume(device_t dev) 57786be9f0dSKonstantin Belousov { 57886be9f0dSKonstantin Belousov 57986be9f0dSKonstantin Belousov /* XXXKIB */ 58086be9f0dSKonstantin Belousov return (0); 58186be9f0dSKonstantin Belousov } 58286be9f0dSKonstantin Belousov 58386be9f0dSKonstantin Belousov static device_method_t dmar_methods[] = { 58486be9f0dSKonstantin Belousov DEVMETHOD(device_identify, dmar_identify), 58586be9f0dSKonstantin Belousov DEVMETHOD(device_probe, dmar_probe), 58686be9f0dSKonstantin Belousov DEVMETHOD(device_attach, dmar_attach), 58786be9f0dSKonstantin Belousov DEVMETHOD(device_detach, dmar_detach), 58886be9f0dSKonstantin Belousov DEVMETHOD(device_suspend, dmar_suspend), 58986be9f0dSKonstantin Belousov DEVMETHOD(device_resume, dmar_resume), 59086be9f0dSKonstantin Belousov #ifdef DEV_APIC 59186be9f0dSKonstantin Belousov DEVMETHOD(bus_remap_intr, dmar_remap_intr), 59286be9f0dSKonstantin Belousov #endif 59386be9f0dSKonstantin Belousov DEVMETHOD_END 59486be9f0dSKonstantin Belousov }; 59586be9f0dSKonstantin Belousov 59686be9f0dSKonstantin Belousov static driver_t dmar_driver = { 59786be9f0dSKonstantin Belousov "dmar", 59886be9f0dSKonstantin Belousov dmar_methods, 59986be9f0dSKonstantin Belousov sizeof(struct dmar_unit), 60086be9f0dSKonstantin Belousov }; 60186be9f0dSKonstantin Belousov 60280d2b3deSJohn Baldwin DRIVER_MODULE(dmar, acpi, dmar_driver, 0, 0); 60386be9f0dSKonstantin Belousov MODULE_DEPEND(dmar, acpi, 1, 1, 1); 60486be9f0dSKonstantin Belousov 60586be9f0dSKonstantin Belousov static void 606f9feb091SKonstantin Belousov dmar_print_path(int busno, int depth, const ACPI_DMAR_PCI_PATH *path) 60786be9f0dSKonstantin Belousov { 60886be9f0dSKonstantin Belousov int i; 60986be9f0dSKonstantin Belousov 610f9feb091SKonstantin Belousov printf("[%d, ", busno); 61186be9f0dSKonstantin Belousov for (i = 0; i < depth; i++) { 61286be9f0dSKonstantin Belousov if (i != 0) 61386be9f0dSKonstantin Belousov printf(", "); 61486be9f0dSKonstantin Belousov printf("(%d, %d)", path[i].Device, path[i].Function); 61586be9f0dSKonstantin Belousov } 616f9feb091SKonstantin Belousov printf("]"); 61786be9f0dSKonstantin Belousov } 61886be9f0dSKonstantin Belousov 619f9feb091SKonstantin Belousov int 62086be9f0dSKonstantin Belousov dmar_dev_depth(device_t child) 62186be9f0dSKonstantin Belousov { 62286be9f0dSKonstantin Belousov devclass_t pci_class; 62386be9f0dSKonstantin Belousov device_t bus, pcib; 62486be9f0dSKonstantin Belousov int depth; 62586be9f0dSKonstantin Belousov 62686be9f0dSKonstantin Belousov pci_class = devclass_find("pci"); 62786be9f0dSKonstantin Belousov for (depth = 1; ; depth++) { 62886be9f0dSKonstantin Belousov bus = device_get_parent(child); 62986be9f0dSKonstantin Belousov pcib = device_get_parent(bus); 63086be9f0dSKonstantin Belousov if (device_get_devclass(device_get_parent(pcib)) != 63186be9f0dSKonstantin Belousov pci_class) 63286be9f0dSKonstantin Belousov return (depth); 63386be9f0dSKonstantin Belousov child = pcib; 63486be9f0dSKonstantin Belousov } 63586be9f0dSKonstantin Belousov } 63686be9f0dSKonstantin Belousov 637f9feb091SKonstantin Belousov void 638f9feb091SKonstantin Belousov dmar_dev_path(device_t child, int *busno, void *path1, int depth) 63986be9f0dSKonstantin Belousov { 64086be9f0dSKonstantin Belousov devclass_t pci_class; 64186be9f0dSKonstantin Belousov device_t bus, pcib; 642f9feb091SKonstantin Belousov ACPI_DMAR_PCI_PATH *path; 64386be9f0dSKonstantin Belousov 64486be9f0dSKonstantin Belousov pci_class = devclass_find("pci"); 645f9feb091SKonstantin Belousov path = path1; 64686be9f0dSKonstantin Belousov for (depth--; depth != -1; depth--) { 64786be9f0dSKonstantin Belousov path[depth].Device = pci_get_slot(child); 64886be9f0dSKonstantin Belousov path[depth].Function = pci_get_function(child); 64986be9f0dSKonstantin Belousov bus = device_get_parent(child); 65086be9f0dSKonstantin Belousov pcib = device_get_parent(bus); 65186be9f0dSKonstantin Belousov if (device_get_devclass(device_get_parent(pcib)) != 65286be9f0dSKonstantin Belousov pci_class) { 65386be9f0dSKonstantin Belousov /* reached a host bridge */ 65486be9f0dSKonstantin Belousov *busno = pcib_get_bus(bus); 65586be9f0dSKonstantin Belousov return; 65686be9f0dSKonstantin Belousov } 65786be9f0dSKonstantin Belousov child = pcib; 65886be9f0dSKonstantin Belousov } 65986be9f0dSKonstantin Belousov panic("wrong depth"); 66086be9f0dSKonstantin Belousov } 66186be9f0dSKonstantin Belousov 66286be9f0dSKonstantin Belousov static int 66386be9f0dSKonstantin Belousov dmar_match_pathes(int busno1, const ACPI_DMAR_PCI_PATH *path1, int depth1, 66486be9f0dSKonstantin Belousov int busno2, const ACPI_DMAR_PCI_PATH *path2, int depth2, 66586be9f0dSKonstantin Belousov enum AcpiDmarScopeType scope_type) 66686be9f0dSKonstantin Belousov { 66786be9f0dSKonstantin Belousov int i, depth; 66886be9f0dSKonstantin Belousov 66986be9f0dSKonstantin Belousov if (busno1 != busno2) 67086be9f0dSKonstantin Belousov return (0); 67186be9f0dSKonstantin Belousov if (scope_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && depth1 != depth2) 67286be9f0dSKonstantin Belousov return (0); 67386be9f0dSKonstantin Belousov depth = depth1; 67486be9f0dSKonstantin Belousov if (depth2 < depth) 67586be9f0dSKonstantin Belousov depth = depth2; 67686be9f0dSKonstantin Belousov for (i = 0; i < depth; i++) { 67786be9f0dSKonstantin Belousov if (path1[i].Device != path2[i].Device || 67886be9f0dSKonstantin Belousov path1[i].Function != path2[i].Function) 67986be9f0dSKonstantin Belousov return (0); 68086be9f0dSKonstantin Belousov } 68186be9f0dSKonstantin Belousov return (1); 68286be9f0dSKonstantin Belousov } 68386be9f0dSKonstantin Belousov 68486be9f0dSKonstantin Belousov static int 685f9feb091SKonstantin Belousov dmar_match_devscope(ACPI_DMAR_DEVICE_SCOPE *devscope, int dev_busno, 686f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len) 68786be9f0dSKonstantin Belousov { 68886be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH *path; 68986be9f0dSKonstantin Belousov int path_len; 69086be9f0dSKonstantin Belousov 69186be9f0dSKonstantin Belousov if (devscope->Length < sizeof(*devscope)) { 692f9feb091SKonstantin Belousov printf("dmar_match_devscope: corrupted DMAR table, dl %d\n", 69386be9f0dSKonstantin Belousov devscope->Length); 69486be9f0dSKonstantin Belousov return (-1); 69586be9f0dSKonstantin Belousov } 69686be9f0dSKonstantin Belousov if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT && 69786be9f0dSKonstantin Belousov devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_BRIDGE) 69886be9f0dSKonstantin Belousov return (0); 69986be9f0dSKonstantin Belousov path_len = devscope->Length - sizeof(*devscope); 70086be9f0dSKonstantin Belousov if (path_len % 2 != 0) { 701f9feb091SKonstantin Belousov printf("dmar_match_devscope: corrupted DMAR table, dl %d\n", 70286be9f0dSKonstantin Belousov devscope->Length); 70386be9f0dSKonstantin Belousov return (-1); 70486be9f0dSKonstantin Belousov } 70586be9f0dSKonstantin Belousov path_len /= 2; 70686be9f0dSKonstantin Belousov path = (ACPI_DMAR_PCI_PATH *)(devscope + 1); 70786be9f0dSKonstantin Belousov if (path_len == 0) { 708f9feb091SKonstantin Belousov printf("dmar_match_devscope: corrupted DMAR table, dl %d\n", 70986be9f0dSKonstantin Belousov devscope->Length); 71086be9f0dSKonstantin Belousov return (-1); 71186be9f0dSKonstantin Belousov } 71286be9f0dSKonstantin Belousov 71386be9f0dSKonstantin Belousov return (dmar_match_pathes(devscope->Bus, path, path_len, dev_busno, 71486be9f0dSKonstantin Belousov dev_path, dev_path_len, devscope->EntryType)); 71586be9f0dSKonstantin Belousov } 71686be9f0dSKonstantin Belousov 717f9feb091SKonstantin Belousov static bool 718f9feb091SKonstantin Belousov dmar_match_by_path(struct dmar_unit *unit, int dev_domain, int dev_busno, 719f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len, const char **banner) 72086be9f0dSKonstantin Belousov { 72186be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh; 72286be9f0dSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope; 72386be9f0dSKonstantin Belousov char *ptr, *ptrend; 724f9feb091SKonstantin Belousov int match; 725f9feb091SKonstantin Belousov 72659e37c8aSRuslan Bukin dmarh = dmar_find_by_index(unit->iommu.unit); 727f9feb091SKonstantin Belousov if (dmarh == NULL) 728f9feb091SKonstantin Belousov return (false); 729f9feb091SKonstantin Belousov if (dmarh->Segment != dev_domain) 730f9feb091SKonstantin Belousov return (false); 731f9feb091SKonstantin Belousov if ((dmarh->Flags & ACPI_DMAR_INCLUDE_ALL) != 0) { 732f9feb091SKonstantin Belousov if (banner != NULL) 733f9feb091SKonstantin Belousov *banner = "INCLUDE_ALL"; 734f9feb091SKonstantin Belousov return (true); 735f9feb091SKonstantin Belousov } 736f9feb091SKonstantin Belousov ptr = (char *)dmarh + sizeof(*dmarh); 737f9feb091SKonstantin Belousov ptrend = (char *)dmarh + dmarh->Header.Length; 738f9feb091SKonstantin Belousov while (ptr < ptrend) { 739f9feb091SKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr; 740f9feb091SKonstantin Belousov ptr += devscope->Length; 741f9feb091SKonstantin Belousov match = dmar_match_devscope(devscope, dev_busno, dev_path, 742f9feb091SKonstantin Belousov dev_path_len); 743f9feb091SKonstantin Belousov if (match == -1) 744f9feb091SKonstantin Belousov return (false); 745f9feb091SKonstantin Belousov if (match == 1) { 746f9feb091SKonstantin Belousov if (banner != NULL) 747f9feb091SKonstantin Belousov *banner = "specific match"; 748f9feb091SKonstantin Belousov return (true); 749f9feb091SKonstantin Belousov } 750f9feb091SKonstantin Belousov } 751f9feb091SKonstantin Belousov return (false); 752f9feb091SKonstantin Belousov } 753f9feb091SKonstantin Belousov 754f9feb091SKonstantin Belousov static struct dmar_unit * 755f9feb091SKonstantin Belousov dmar_find_by_scope(int dev_domain, int dev_busno, 756f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len) 757f9feb091SKonstantin Belousov { 758f9feb091SKonstantin Belousov struct dmar_unit *unit; 759f9feb091SKonstantin Belousov int i; 760f9feb091SKonstantin Belousov 761f9feb091SKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 762f9feb091SKonstantin Belousov if (dmar_devs[i] == NULL) 763f9feb091SKonstantin Belousov continue; 764f9feb091SKonstantin Belousov unit = device_get_softc(dmar_devs[i]); 765f9feb091SKonstantin Belousov if (dmar_match_by_path(unit, dev_domain, dev_busno, dev_path, 766f9feb091SKonstantin Belousov dev_path_len, NULL)) 767f9feb091SKonstantin Belousov return (unit); 768f9feb091SKonstantin Belousov } 769f9feb091SKonstantin Belousov return (NULL); 770f9feb091SKonstantin Belousov } 771f9feb091SKonstantin Belousov 772f9feb091SKonstantin Belousov struct dmar_unit * 773f9feb091SKonstantin Belousov dmar_find(device_t dev, bool verbose) 774f9feb091SKonstantin Belousov { 775f9feb091SKonstantin Belousov struct dmar_unit *unit; 776f9feb091SKonstantin Belousov const char *banner; 777f9feb091SKonstantin Belousov int i, dev_domain, dev_busno, dev_path_len; 77886be9f0dSKonstantin Belousov 779b7b6b7a9SKonstantin Belousov /* 780b7b6b7a9SKonstantin Belousov * This function can only handle PCI(e) devices. 781b7b6b7a9SKonstantin Belousov */ 782b7b6b7a9SKonstantin Belousov if (device_get_devclass(device_get_parent(dev)) != 783b7b6b7a9SKonstantin Belousov devclass_find("pci")) 784b7b6b7a9SKonstantin Belousov return (NULL); 785b7b6b7a9SKonstantin Belousov 78686be9f0dSKonstantin Belousov dev_domain = pci_get_domain(dev); 78786be9f0dSKonstantin Belousov dev_path_len = dmar_dev_depth(dev); 78886be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH dev_path[dev_path_len]; 78986be9f0dSKonstantin Belousov dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len); 790f9feb091SKonstantin Belousov banner = ""; 79186be9f0dSKonstantin Belousov 79286be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 79386be9f0dSKonstantin Belousov if (dmar_devs[i] == NULL) 79486be9f0dSKonstantin Belousov continue; 795f9feb091SKonstantin Belousov unit = device_get_softc(dmar_devs[i]); 796f9feb091SKonstantin Belousov if (dmar_match_by_path(unit, dev_domain, dev_busno, 797f9feb091SKonstantin Belousov dev_path, dev_path_len, &banner)) 79886be9f0dSKonstantin Belousov break; 79986be9f0dSKonstantin Belousov } 800f9feb091SKonstantin Belousov if (i == dmar_devcnt) 80186be9f0dSKonstantin Belousov return (NULL); 802f9feb091SKonstantin Belousov 803f9feb091SKonstantin Belousov if (verbose) { 804f9feb091SKonstantin Belousov device_printf(dev, "pci%d:%d:%d:%d matched dmar%d by %s", 805f9feb091SKonstantin Belousov dev_domain, pci_get_bus(dev), pci_get_slot(dev), 80659e37c8aSRuslan Bukin pci_get_function(dev), unit->iommu.unit, banner); 807f9feb091SKonstantin Belousov printf(" scope path "); 808f9feb091SKonstantin Belousov dmar_print_path(dev_busno, dev_path_len, dev_path); 809f9feb091SKonstantin Belousov printf("\n"); 81086be9f0dSKonstantin Belousov } 811f9feb091SKonstantin Belousov return (unit); 81286be9f0dSKonstantin Belousov } 81386be9f0dSKonstantin Belousov 8140a110d5bSKonstantin Belousov static struct dmar_unit * 8150a110d5bSKonstantin Belousov dmar_find_nonpci(u_int id, u_int entry_type, uint16_t *rid) 8160a110d5bSKonstantin Belousov { 8170a110d5bSKonstantin Belousov device_t dmar_dev; 8180a110d5bSKonstantin Belousov struct dmar_unit *unit; 8190a110d5bSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh; 8200a110d5bSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope; 8210a110d5bSKonstantin Belousov ACPI_DMAR_PCI_PATH *path; 8220a110d5bSKonstantin Belousov char *ptr, *ptrend; 823fd15fee1SKonstantin Belousov #ifdef DEV_APIC 824fd15fee1SKonstantin Belousov int error; 825fd15fee1SKonstantin Belousov #endif 8260a110d5bSKonstantin Belousov int i; 8270a110d5bSKonstantin Belousov 8280a110d5bSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 8290a110d5bSKonstantin Belousov dmar_dev = dmar_devs[i]; 8300a110d5bSKonstantin Belousov if (dmar_dev == NULL) 8310a110d5bSKonstantin Belousov continue; 8320a110d5bSKonstantin Belousov unit = (struct dmar_unit *)device_get_softc(dmar_dev); 8330a110d5bSKonstantin Belousov dmarh = dmar_find_by_index(i); 8340a110d5bSKonstantin Belousov if (dmarh == NULL) 8350a110d5bSKonstantin Belousov continue; 8360a110d5bSKonstantin Belousov ptr = (char *)dmarh + sizeof(*dmarh); 8370a110d5bSKonstantin Belousov ptrend = (char *)dmarh + dmarh->Header.Length; 8380a110d5bSKonstantin Belousov for (;;) { 8390a110d5bSKonstantin Belousov if (ptr >= ptrend) 8400a110d5bSKonstantin Belousov break; 8410a110d5bSKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr; 8420a110d5bSKonstantin Belousov ptr += devscope->Length; 8430a110d5bSKonstantin Belousov if (devscope->EntryType != entry_type) 8440a110d5bSKonstantin Belousov continue; 8450a110d5bSKonstantin Belousov if (devscope->EnumerationId != id) 8460a110d5bSKonstantin Belousov continue; 847fd15fee1SKonstantin Belousov #ifdef DEV_APIC 848fd15fee1SKonstantin Belousov if (entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) { 849fd15fee1SKonstantin Belousov error = ioapic_get_rid(id, rid); 850fd15fee1SKonstantin Belousov /* 851fd15fee1SKonstantin Belousov * If our IOAPIC has PCI bindings then 852fd15fee1SKonstantin Belousov * use the PCI device rid. 853fd15fee1SKonstantin Belousov */ 854fd15fee1SKonstantin Belousov if (error == 0) 855fd15fee1SKonstantin Belousov return (unit); 856fd15fee1SKonstantin Belousov } 857fd15fee1SKonstantin Belousov #endif 8580a110d5bSKonstantin Belousov if (devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE) 8590a110d5bSKonstantin Belousov == 2) { 8600a110d5bSKonstantin Belousov if (rid != NULL) { 8610a110d5bSKonstantin Belousov path = (ACPI_DMAR_PCI_PATH *) 8620a110d5bSKonstantin Belousov (devscope + 1); 8630a110d5bSKonstantin Belousov *rid = PCI_RID(devscope->Bus, 8640a110d5bSKonstantin Belousov path->Device, path->Function); 8650a110d5bSKonstantin Belousov } 8660a110d5bSKonstantin Belousov return (unit); 867fd15fee1SKonstantin Belousov } 8680a110d5bSKonstantin Belousov printf( 8690a110d5bSKonstantin Belousov "dmar_find_nonpci: id %d type %d path length != 2\n", 8700a110d5bSKonstantin Belousov id, entry_type); 871fd15fee1SKonstantin Belousov break; 8720a110d5bSKonstantin Belousov } 8730a110d5bSKonstantin Belousov } 8740a110d5bSKonstantin Belousov return (NULL); 8750a110d5bSKonstantin Belousov } 8760a110d5bSKonstantin Belousov 8770a110d5bSKonstantin Belousov struct dmar_unit * 8780a110d5bSKonstantin Belousov dmar_find_hpet(device_t dev, uint16_t *rid) 8790a110d5bSKonstantin Belousov { 8800a110d5bSKonstantin Belousov 8812fe1339eSKonstantin Belousov return (dmar_find_nonpci(hpet_get_uid(dev), ACPI_DMAR_SCOPE_TYPE_HPET, 8822fe1339eSKonstantin Belousov rid)); 8830a110d5bSKonstantin Belousov } 8840a110d5bSKonstantin Belousov 8850a110d5bSKonstantin Belousov struct dmar_unit * 8860a110d5bSKonstantin Belousov dmar_find_ioapic(u_int apic_id, uint16_t *rid) 8870a110d5bSKonstantin Belousov { 8880a110d5bSKonstantin Belousov 8890a110d5bSKonstantin Belousov return (dmar_find_nonpci(apic_id, ACPI_DMAR_SCOPE_TYPE_IOAPIC, rid)); 8900a110d5bSKonstantin Belousov } 8910a110d5bSKonstantin Belousov 89286be9f0dSKonstantin Belousov struct rmrr_iter_args { 8931abfd355SKonstantin Belousov struct dmar_domain *domain; 89486be9f0dSKonstantin Belousov int dev_domain; 89586be9f0dSKonstantin Belousov int dev_busno; 896f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path; 89786be9f0dSKonstantin Belousov int dev_path_len; 89859e37c8aSRuslan Bukin struct iommu_map_entries_tailq *rmrr_entries; 89986be9f0dSKonstantin Belousov }; 90086be9f0dSKonstantin Belousov 90186be9f0dSKonstantin Belousov static int 90286be9f0dSKonstantin Belousov dmar_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 90386be9f0dSKonstantin Belousov { 90486be9f0dSKonstantin Belousov struct rmrr_iter_args *ria; 90586be9f0dSKonstantin Belousov ACPI_DMAR_RESERVED_MEMORY *resmem; 90686be9f0dSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope; 90759e37c8aSRuslan Bukin struct iommu_map_entry *entry; 90886be9f0dSKonstantin Belousov char *ptr, *ptrend; 90986be9f0dSKonstantin Belousov int match; 91086be9f0dSKonstantin Belousov 91186be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY) 91286be9f0dSKonstantin Belousov return (1); 91386be9f0dSKonstantin Belousov 91486be9f0dSKonstantin Belousov ria = arg; 91586be9f0dSKonstantin Belousov resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh; 91686be9f0dSKonstantin Belousov if (resmem->Segment != ria->dev_domain) 91786be9f0dSKonstantin Belousov return (1); 91886be9f0dSKonstantin Belousov 91986be9f0dSKonstantin Belousov ptr = (char *)resmem + sizeof(*resmem); 92086be9f0dSKonstantin Belousov ptrend = (char *)resmem + resmem->Header.Length; 92186be9f0dSKonstantin Belousov for (;;) { 92286be9f0dSKonstantin Belousov if (ptr >= ptrend) 92386be9f0dSKonstantin Belousov break; 92486be9f0dSKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr; 92586be9f0dSKonstantin Belousov ptr += devscope->Length; 926f9feb091SKonstantin Belousov match = dmar_match_devscope(devscope, ria->dev_busno, 92786be9f0dSKonstantin Belousov ria->dev_path, ria->dev_path_len); 92886be9f0dSKonstantin Belousov if (match == 1) { 92978b51754SRuslan Bukin entry = iommu_gas_alloc_entry(DOM2IODOM(ria->domain), 93015f6baf4SRuslan Bukin IOMMU_PGF_WAITOK); 93186be9f0dSKonstantin Belousov entry->start = resmem->BaseAddress; 93286be9f0dSKonstantin Belousov /* The RMRR entry end address is inclusive. */ 93386be9f0dSKonstantin Belousov entry->end = resmem->EndAddress; 93486be9f0dSKonstantin Belousov TAILQ_INSERT_TAIL(ria->rmrr_entries, entry, 935*db0110a5SAlan Cox dmamap_link); 93686be9f0dSKonstantin Belousov } 93786be9f0dSKonstantin Belousov } 93886be9f0dSKonstantin Belousov 93986be9f0dSKonstantin Belousov return (1); 94086be9f0dSKonstantin Belousov } 94186be9f0dSKonstantin Belousov 94286be9f0dSKonstantin Belousov void 943f9feb091SKonstantin Belousov dmar_dev_parse_rmrr(struct dmar_domain *domain, int dev_domain, int dev_busno, 944f9feb091SKonstantin Belousov const void *dev_path, int dev_path_len, 94559e37c8aSRuslan Bukin struct iommu_map_entries_tailq *rmrr_entries) 94686be9f0dSKonstantin Belousov { 94786be9f0dSKonstantin Belousov struct rmrr_iter_args ria; 94886be9f0dSKonstantin Belousov 9491abfd355SKonstantin Belousov ria.domain = domain; 950f9feb091SKonstantin Belousov ria.dev_domain = dev_domain; 951f9feb091SKonstantin Belousov ria.dev_busno = dev_busno; 952f9feb091SKonstantin Belousov ria.dev_path = (const ACPI_DMAR_PCI_PATH *)dev_path; 953f9feb091SKonstantin Belousov ria.dev_path_len = dev_path_len; 95486be9f0dSKonstantin Belousov ria.rmrr_entries = rmrr_entries; 95586be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_rmrr_iter, &ria); 95686be9f0dSKonstantin Belousov } 95786be9f0dSKonstantin Belousov 95886be9f0dSKonstantin Belousov struct inst_rmrr_iter_args { 95986be9f0dSKonstantin Belousov struct dmar_unit *dmar; 96086be9f0dSKonstantin Belousov }; 96186be9f0dSKonstantin Belousov 96286be9f0dSKonstantin Belousov static device_t 96386be9f0dSKonstantin Belousov dmar_path_dev(int segment, int path_len, int busno, 964f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *path, uint16_t *rid) 96586be9f0dSKonstantin Belousov { 966f9feb091SKonstantin Belousov device_t dev; 96786be9f0dSKonstantin Belousov int i; 96886be9f0dSKonstantin Belousov 96986be9f0dSKonstantin Belousov dev = NULL; 970f9feb091SKonstantin Belousov for (i = 0; i < path_len; i++) { 97186be9f0dSKonstantin Belousov dev = pci_find_dbsf(segment, busno, path->Device, 97286be9f0dSKonstantin Belousov path->Function); 97386be9f0dSKonstantin Belousov if (i != path_len - 1) { 974f9feb091SKonstantin Belousov busno = pci_cfgregread(busno, path->Device, 975f9feb091SKonstantin Belousov path->Function, PCIR_SECBUS_1, 1); 976f9feb091SKonstantin Belousov path++; 97786be9f0dSKonstantin Belousov } 97886be9f0dSKonstantin Belousov } 979f9feb091SKonstantin Belousov *rid = PCI_RID(busno, path->Device, path->Function); 98086be9f0dSKonstantin Belousov return (dev); 98186be9f0dSKonstantin Belousov } 98286be9f0dSKonstantin Belousov 98386be9f0dSKonstantin Belousov static int 98486be9f0dSKonstantin Belousov dmar_inst_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 98586be9f0dSKonstantin Belousov { 98686be9f0dSKonstantin Belousov const ACPI_DMAR_RESERVED_MEMORY *resmem; 98786be9f0dSKonstantin Belousov const ACPI_DMAR_DEVICE_SCOPE *devscope; 98886be9f0dSKonstantin Belousov struct inst_rmrr_iter_args *iria; 98986be9f0dSKonstantin Belousov const char *ptr, *ptrend; 99086be9f0dSKonstantin Belousov device_t dev; 991f9feb091SKonstantin Belousov struct dmar_unit *unit; 992f9feb091SKonstantin Belousov int dev_path_len; 993f9feb091SKonstantin Belousov uint16_t rid; 994f9feb091SKonstantin Belousov 995f9feb091SKonstantin Belousov iria = arg; 99686be9f0dSKonstantin Belousov 99786be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY) 99886be9f0dSKonstantin Belousov return (1); 99986be9f0dSKonstantin Belousov 100086be9f0dSKonstantin Belousov resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh; 100186be9f0dSKonstantin Belousov if (resmem->Segment != iria->dmar->segment) 100286be9f0dSKonstantin Belousov return (1); 100386be9f0dSKonstantin Belousov 100433552193SDimitry Andric ptr = (const char *)resmem + sizeof(*resmem); 100533552193SDimitry Andric ptrend = (const char *)resmem + resmem->Header.Length; 100686be9f0dSKonstantin Belousov for (;;) { 100786be9f0dSKonstantin Belousov if (ptr >= ptrend) 100886be9f0dSKonstantin Belousov break; 100933552193SDimitry Andric devscope = (const ACPI_DMAR_DEVICE_SCOPE *)ptr; 101086be9f0dSKonstantin Belousov ptr += devscope->Length; 101186be9f0dSKonstantin Belousov /* XXXKIB bridge */ 101286be9f0dSKonstantin Belousov if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT) 101386be9f0dSKonstantin Belousov continue; 1014f9feb091SKonstantin Belousov rid = 0; 1015f9feb091SKonstantin Belousov dev_path_len = (devscope->Length - 1016f9feb091SKonstantin Belousov sizeof(ACPI_DMAR_DEVICE_SCOPE)) / 2; 1017f9feb091SKonstantin Belousov dev = dmar_path_dev(resmem->Segment, dev_path_len, 1018f9feb091SKonstantin Belousov devscope->Bus, 1019f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1), &rid); 102086be9f0dSKonstantin Belousov if (dev == NULL) { 1021f9feb091SKonstantin Belousov if (bootverbose) { 1022f9feb091SKonstantin Belousov printf("dmar%d no dev found for RMRR " 1023f9feb091SKonstantin Belousov "[%#jx, %#jx] rid %#x scope path ", 102459e37c8aSRuslan Bukin iria->dmar->iommu.unit, 10252d8bfbdcSKonstantin Belousov (uintmax_t)resmem->BaseAddress, 10262d8bfbdcSKonstantin Belousov (uintmax_t)resmem->EndAddress, 1027f9feb091SKonstantin Belousov rid); 1028f9feb091SKonstantin Belousov dmar_print_path(devscope->Bus, dev_path_len, 1029f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1)); 1030f9feb091SKonstantin Belousov printf("\n"); 1031f9feb091SKonstantin Belousov } 1032f9feb091SKonstantin Belousov unit = dmar_find_by_scope(resmem->Segment, 1033f9feb091SKonstantin Belousov devscope->Bus, 1034f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1), 1035f9feb091SKonstantin Belousov dev_path_len); 1036f9feb091SKonstantin Belousov if (iria->dmar != unit) 103786be9f0dSKonstantin Belousov continue; 1038f9feb091SKonstantin Belousov dmar_get_ctx_for_devpath(iria->dmar, rid, 1039f9feb091SKonstantin Belousov resmem->Segment, devscope->Bus, 1040f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1), 1041f9feb091SKonstantin Belousov dev_path_len, false, true); 1042f9feb091SKonstantin Belousov } else { 1043f9feb091SKonstantin Belousov unit = dmar_find(dev, false); 1044f9feb091SKonstantin Belousov if (iria->dmar != unit) 104586be9f0dSKonstantin Belousov continue; 104659e37c8aSRuslan Bukin iommu_instantiate_ctx(&(iria)->dmar->iommu, 104759e37c8aSRuslan Bukin dev, true); 104886be9f0dSKonstantin Belousov } 1049f9feb091SKonstantin Belousov } 105086be9f0dSKonstantin Belousov 105186be9f0dSKonstantin Belousov return (1); 105286be9f0dSKonstantin Belousov 105386be9f0dSKonstantin Belousov } 105486be9f0dSKonstantin Belousov 105586be9f0dSKonstantin Belousov /* 105686be9f0dSKonstantin Belousov * Pre-create all contexts for the DMAR which have RMRR entries. 105786be9f0dSKonstantin Belousov */ 105886be9f0dSKonstantin Belousov int 105959e37c8aSRuslan Bukin dmar_instantiate_rmrr_ctxs(struct iommu_unit *unit) 106086be9f0dSKonstantin Belousov { 106159e37c8aSRuslan Bukin struct dmar_unit *dmar; 106286be9f0dSKonstantin Belousov struct inst_rmrr_iter_args iria; 106386be9f0dSKonstantin Belousov int error; 106486be9f0dSKonstantin Belousov 106578b51754SRuslan Bukin dmar = IOMMU2DMAR(unit); 106659e37c8aSRuslan Bukin 106786be9f0dSKonstantin Belousov if (!dmar_barrier_enter(dmar, DMAR_BARRIER_RMRR)) 106886be9f0dSKonstantin Belousov return (0); 106986be9f0dSKonstantin Belousov 107086be9f0dSKonstantin Belousov error = 0; 107186be9f0dSKonstantin Belousov iria.dmar = dmar; 107286be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_inst_rmrr_iter, &iria); 107386be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 10741abfd355SKonstantin Belousov if (!LIST_EMPTY(&dmar->domains)) { 107586be9f0dSKonstantin Belousov KASSERT((dmar->hw_gcmd & DMAR_GCMD_TE) == 0, 107686be9f0dSKonstantin Belousov ("dmar%d: RMRR not handled but translation is already enabled", 107759e37c8aSRuslan Bukin dmar->iommu.unit)); 107806e6ca6dSKornel Duleba error = dmar_disable_protected_regions(dmar); 107906e6ca6dSKornel Duleba if (error != 0) 108006e6ca6dSKornel Duleba printf("dmar%d: Failed to disable protected regions\n", 108106e6ca6dSKornel Duleba dmar->iommu.unit); 108286be9f0dSKonstantin Belousov error = dmar_enable_translation(dmar); 1083f9feb091SKonstantin Belousov if (bootverbose) { 1084f9feb091SKonstantin Belousov if (error == 0) { 1085f9feb091SKonstantin Belousov printf("dmar%d: enabled translation\n", 108659e37c8aSRuslan Bukin dmar->iommu.unit); 1087f9feb091SKonstantin Belousov } else { 1088f9feb091SKonstantin Belousov printf("dmar%d: enabling translation failed, " 108959e37c8aSRuslan Bukin "error %d\n", dmar->iommu.unit, error); 1090f9feb091SKonstantin Belousov } 1091f9feb091SKonstantin Belousov } 109286be9f0dSKonstantin Belousov } 109386be9f0dSKonstantin Belousov dmar_barrier_exit(dmar, DMAR_BARRIER_RMRR); 109486be9f0dSKonstantin Belousov return (error); 109586be9f0dSKonstantin Belousov } 109686be9f0dSKonstantin Belousov 109786be9f0dSKonstantin Belousov #ifdef DDB 109886be9f0dSKonstantin Belousov #include <ddb/ddb.h> 109986be9f0dSKonstantin Belousov #include <ddb/db_lex.h> 110086be9f0dSKonstantin Belousov 110186be9f0dSKonstantin Belousov static void 110259e37c8aSRuslan Bukin dmar_print_domain_entry(const struct iommu_map_entry *entry) 110386be9f0dSKonstantin Belousov { 110459e37c8aSRuslan Bukin struct iommu_map_entry *l, *r; 110586be9f0dSKonstantin Belousov 110686be9f0dSKonstantin Belousov db_printf( 1107f886c4baSDoug Moore " start %jx end %jx first %jx last %jx free_down %jx flags %x ", 1108f886c4baSDoug Moore entry->start, entry->end, entry->first, entry->last, 1109f886c4baSDoug Moore entry->free_down, entry->flags); 111086be9f0dSKonstantin Belousov db_printf("left "); 111186be9f0dSKonstantin Belousov l = RB_LEFT(entry, rb_entry); 111286be9f0dSKonstantin Belousov if (l == NULL) 111386be9f0dSKonstantin Belousov db_printf("NULL "); 111486be9f0dSKonstantin Belousov else 111586be9f0dSKonstantin Belousov db_printf("%jx ", l->start); 111686be9f0dSKonstantin Belousov db_printf("right "); 111786be9f0dSKonstantin Belousov r = RB_RIGHT(entry, rb_entry); 111886be9f0dSKonstantin Belousov if (r == NULL) 111986be9f0dSKonstantin Belousov db_printf("NULL"); 112086be9f0dSKonstantin Belousov else 112186be9f0dSKonstantin Belousov db_printf("%jx", r->start); 112286be9f0dSKonstantin Belousov db_printf("\n"); 112386be9f0dSKonstantin Belousov } 112486be9f0dSKonstantin Belousov 112586be9f0dSKonstantin Belousov static void 11261abfd355SKonstantin Belousov dmar_print_ctx(struct dmar_ctx *ctx) 112786be9f0dSKonstantin Belousov { 112886be9f0dSKonstantin Belousov 112986be9f0dSKonstantin Belousov db_printf( 11301abfd355SKonstantin Belousov " @%p pci%d:%d:%d refs %d flags %x loads %lu unloads %lu\n", 113159e37c8aSRuslan Bukin ctx, pci_get_bus(ctx->context.tag->owner), 113259e37c8aSRuslan Bukin pci_get_slot(ctx->context.tag->owner), 113359e37c8aSRuslan Bukin pci_get_function(ctx->context.tag->owner), ctx->refs, 113459e37c8aSRuslan Bukin ctx->context.flags, ctx->context.loads, ctx->context.unloads); 11351abfd355SKonstantin Belousov } 11361abfd355SKonstantin Belousov 11371abfd355SKonstantin Belousov static void 11381abfd355SKonstantin Belousov dmar_print_domain(struct dmar_domain *domain, bool show_mappings) 11391abfd355SKonstantin Belousov { 114062ad310cSRuslan Bukin struct iommu_domain *iodom; 114159e37c8aSRuslan Bukin struct iommu_map_entry *entry; 11421abfd355SKonstantin Belousov struct dmar_ctx *ctx; 11431abfd355SKonstantin Belousov 114478b51754SRuslan Bukin iodom = DOM2IODOM(domain); 114562ad310cSRuslan Bukin 11461abfd355SKonstantin Belousov db_printf( 11471abfd355SKonstantin Belousov " @%p dom %d mgaw %d agaw %d pglvl %d end %jx refs %d\n" 11481abfd355SKonstantin Belousov " ctx_cnt %d flags %x pgobj %p map_ents %u\n", 11491abfd355SKonstantin Belousov domain, domain->domain, domain->mgaw, domain->agaw, domain->pglvl, 115062ad310cSRuslan Bukin (uintmax_t)domain->iodom.end, domain->refs, domain->ctx_cnt, 115162ad310cSRuslan Bukin domain->iodom.flags, domain->pgtbl_obj, domain->iodom.entries_cnt); 11521abfd355SKonstantin Belousov if (!LIST_EMPTY(&domain->contexts)) { 11531abfd355SKonstantin Belousov db_printf(" Contexts:\n"); 11541abfd355SKonstantin Belousov LIST_FOREACH(ctx, &domain->contexts, link) 11551abfd355SKonstantin Belousov dmar_print_ctx(ctx); 11561abfd355SKonstantin Belousov } 115786be9f0dSKonstantin Belousov if (!show_mappings) 115886be9f0dSKonstantin Belousov return; 115986be9f0dSKonstantin Belousov db_printf(" mapped:\n"); 116062ad310cSRuslan Bukin RB_FOREACH(entry, iommu_gas_entries_tree, &iodom->rb_root) { 11611abfd355SKonstantin Belousov dmar_print_domain_entry(entry); 116286be9f0dSKonstantin Belousov if (db_pager_quit) 116386be9f0dSKonstantin Belousov break; 116486be9f0dSKonstantin Belousov } 116586be9f0dSKonstantin Belousov if (db_pager_quit) 116686be9f0dSKonstantin Belousov return; 116786be9f0dSKonstantin Belousov db_printf(" unloading:\n"); 116859e37c8aSRuslan Bukin TAILQ_FOREACH(entry, &domain->iodom.unload_entries, dmamap_link) { 11691abfd355SKonstantin Belousov dmar_print_domain_entry(entry); 117086be9f0dSKonstantin Belousov if (db_pager_quit) 117186be9f0dSKonstantin Belousov break; 117286be9f0dSKonstantin Belousov } 117386be9f0dSKonstantin Belousov } 117486be9f0dSKonstantin Belousov 1175258958b3SMitchell Horne DB_SHOW_COMMAND_FLAGS(dmar_domain, db_dmar_print_domain, CS_OWN) 117686be9f0dSKonstantin Belousov { 117786be9f0dSKonstantin Belousov struct dmar_unit *unit; 11781abfd355SKonstantin Belousov struct dmar_domain *domain; 117986be9f0dSKonstantin Belousov struct dmar_ctx *ctx; 118086be9f0dSKonstantin Belousov bool show_mappings, valid; 11811abfd355SKonstantin Belousov int pci_domain, bus, device, function, i, t; 118286be9f0dSKonstantin Belousov db_expr_t radix; 118386be9f0dSKonstantin Belousov 118486be9f0dSKonstantin Belousov valid = false; 118586be9f0dSKonstantin Belousov radix = db_radix; 118686be9f0dSKonstantin Belousov db_radix = 10; 118786be9f0dSKonstantin Belousov t = db_read_token(); 118886be9f0dSKonstantin Belousov if (t == tSLASH) { 118986be9f0dSKonstantin Belousov t = db_read_token(); 119086be9f0dSKonstantin Belousov if (t != tIDENT) { 119186be9f0dSKonstantin Belousov db_printf("Bad modifier\n"); 119286be9f0dSKonstantin Belousov db_radix = radix; 119386be9f0dSKonstantin Belousov db_skip_to_eol(); 119486be9f0dSKonstantin Belousov return; 119586be9f0dSKonstantin Belousov } 119686be9f0dSKonstantin Belousov show_mappings = strchr(db_tok_string, 'm') != NULL; 119786be9f0dSKonstantin Belousov t = db_read_token(); 1198f7f5706fSDimitry Andric } else { 1199f7f5706fSDimitry Andric show_mappings = false; 120086be9f0dSKonstantin Belousov } 120186be9f0dSKonstantin Belousov if (t == tNUMBER) { 12021abfd355SKonstantin Belousov pci_domain = db_tok_number; 120386be9f0dSKonstantin Belousov t = db_read_token(); 120486be9f0dSKonstantin Belousov if (t == tNUMBER) { 120586be9f0dSKonstantin Belousov bus = db_tok_number; 120686be9f0dSKonstantin Belousov t = db_read_token(); 120786be9f0dSKonstantin Belousov if (t == tNUMBER) { 120886be9f0dSKonstantin Belousov device = db_tok_number; 120986be9f0dSKonstantin Belousov t = db_read_token(); 121086be9f0dSKonstantin Belousov if (t == tNUMBER) { 121186be9f0dSKonstantin Belousov function = db_tok_number; 121286be9f0dSKonstantin Belousov valid = true; 121386be9f0dSKonstantin Belousov } 121486be9f0dSKonstantin Belousov } 121586be9f0dSKonstantin Belousov } 121686be9f0dSKonstantin Belousov } 121786be9f0dSKonstantin Belousov db_radix = radix; 121886be9f0dSKonstantin Belousov db_skip_to_eol(); 121986be9f0dSKonstantin Belousov if (!valid) { 12201abfd355SKonstantin Belousov db_printf("usage: show dmar_domain [/m] " 122186be9f0dSKonstantin Belousov "<domain> <bus> <device> <func>\n"); 122286be9f0dSKonstantin Belousov return; 122386be9f0dSKonstantin Belousov } 122486be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 122586be9f0dSKonstantin Belousov unit = device_get_softc(dmar_devs[i]); 12261abfd355SKonstantin Belousov LIST_FOREACH(domain, &unit->domains, link) { 12271abfd355SKonstantin Belousov LIST_FOREACH(ctx, &domain->contexts, link) { 12281abfd355SKonstantin Belousov if (pci_domain == unit->segment && 122959e37c8aSRuslan Bukin bus == pci_get_bus(ctx->context.tag->owner) && 12301abfd355SKonstantin Belousov device == 123159e37c8aSRuslan Bukin pci_get_slot(ctx->context.tag->owner) && 12321abfd355SKonstantin Belousov function == 123359e37c8aSRuslan Bukin pci_get_function(ctx->context.tag->owner)) { 12341abfd355SKonstantin Belousov dmar_print_domain(domain, 12351abfd355SKonstantin Belousov show_mappings); 123686be9f0dSKonstantin Belousov goto out; 123786be9f0dSKonstantin Belousov } 123886be9f0dSKonstantin Belousov } 123986be9f0dSKonstantin Belousov } 12401abfd355SKonstantin Belousov } 124186be9f0dSKonstantin Belousov out:; 124286be9f0dSKonstantin Belousov } 124386be9f0dSKonstantin Belousov 124486be9f0dSKonstantin Belousov static void 12451abfd355SKonstantin Belousov dmar_print_one(int idx, bool show_domains, bool show_mappings) 124686be9f0dSKonstantin Belousov { 124786be9f0dSKonstantin Belousov struct dmar_unit *unit; 12481abfd355SKonstantin Belousov struct dmar_domain *domain; 124986be9f0dSKonstantin Belousov int i, frir; 125086be9f0dSKonstantin Belousov 125186be9f0dSKonstantin Belousov unit = device_get_softc(dmar_devs[idx]); 125259e37c8aSRuslan Bukin db_printf("dmar%d at %p, root at 0x%jx, ver 0x%x\n", unit->iommu.unit, 125359e37c8aSRuslan Bukin unit, dmar_read8(unit, DMAR_RTADDR_REG), 125459e37c8aSRuslan Bukin dmar_read4(unit, DMAR_VER_REG)); 125586be9f0dSKonstantin Belousov db_printf("cap 0x%jx ecap 0x%jx gsts 0x%x fsts 0x%x fectl 0x%x\n", 125686be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_CAP_REG), 125786be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_ECAP_REG), 125886be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_GSTS_REG), 125986be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FSTS_REG), 126086be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FECTL_REG)); 12611abfd355SKonstantin Belousov if (unit->ir_enabled) { 12621abfd355SKonstantin Belousov db_printf("ir is enabled; IRT @%p phys 0x%jx maxcnt %d\n", 12631abfd355SKonstantin Belousov unit->irt, (uintmax_t)unit->irt_phys, unit->irte_cnt); 12641abfd355SKonstantin Belousov } 126586be9f0dSKonstantin Belousov db_printf("fed 0x%x fea 0x%x feua 0x%x\n", 126686be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEDATA_REG), 126786be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEADDR_REG), 126886be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEUADDR_REG)); 126986be9f0dSKonstantin Belousov db_printf("primary fault log:\n"); 127086be9f0dSKonstantin Belousov for (i = 0; i < DMAR_CAP_NFR(unit->hw_cap); i++) { 127186be9f0dSKonstantin Belousov frir = (DMAR_CAP_FRO(unit->hw_cap) + i) * 16; 127286be9f0dSKonstantin Belousov db_printf(" %d at 0x%x: %jx %jx\n", i, frir, 127386be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, frir), 127486be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, frir + 8)); 127586be9f0dSKonstantin Belousov } 127668eeb96aSKonstantin Belousov if (DMAR_HAS_QI(unit)) { 127768eeb96aSKonstantin Belousov db_printf("ied 0x%x iea 0x%x ieua 0x%x\n", 127868eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEDATA_REG), 127968eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEADDR_REG), 128068eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEUADDR_REG)); 128168eeb96aSKonstantin Belousov if (unit->qi_enabled) { 128268eeb96aSKonstantin Belousov db_printf("qi is enabled: queue @0x%jx (IQA 0x%jx) " 128368eeb96aSKonstantin Belousov "size 0x%jx\n" 128468eeb96aSKonstantin Belousov " head 0x%x tail 0x%x avail 0x%x status 0x%x ctrl 0x%x\n" 128568eeb96aSKonstantin Belousov " hw compl 0x%x@%p/phys@%jx next seq 0x%x gen 0x%x\n", 128668eeb96aSKonstantin Belousov (uintmax_t)unit->inv_queue, 128768eeb96aSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_IQA_REG), 128868eeb96aSKonstantin Belousov (uintmax_t)unit->inv_queue_size, 128968eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IQH_REG), 129068eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IQT_REG), 129168eeb96aSKonstantin Belousov unit->inv_queue_avail, 129268eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_ICS_REG), 129368eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IECTL_REG), 129468eeb96aSKonstantin Belousov unit->inv_waitd_seq_hw, 129568eeb96aSKonstantin Belousov &unit->inv_waitd_seq_hw, 129668eeb96aSKonstantin Belousov (uintmax_t)unit->inv_waitd_seq_hw_phys, 129768eeb96aSKonstantin Belousov unit->inv_waitd_seq, 129868eeb96aSKonstantin Belousov unit->inv_waitd_gen); 129968eeb96aSKonstantin Belousov } else { 130068eeb96aSKonstantin Belousov db_printf("qi is disabled\n"); 130168eeb96aSKonstantin Belousov } 130268eeb96aSKonstantin Belousov } 13031abfd355SKonstantin Belousov if (show_domains) { 13041abfd355SKonstantin Belousov db_printf("domains:\n"); 13051abfd355SKonstantin Belousov LIST_FOREACH(domain, &unit->domains, link) { 13061abfd355SKonstantin Belousov dmar_print_domain(domain, show_mappings); 130786be9f0dSKonstantin Belousov if (db_pager_quit) 130886be9f0dSKonstantin Belousov break; 130986be9f0dSKonstantin Belousov } 131086be9f0dSKonstantin Belousov } 131186be9f0dSKonstantin Belousov } 131286be9f0dSKonstantin Belousov 131386be9f0dSKonstantin Belousov DB_SHOW_COMMAND(dmar, db_dmar_print) 131486be9f0dSKonstantin Belousov { 13151abfd355SKonstantin Belousov bool show_domains, show_mappings; 131686be9f0dSKonstantin Belousov 13171abfd355SKonstantin Belousov show_domains = strchr(modif, 'd') != NULL; 131886be9f0dSKonstantin Belousov show_mappings = strchr(modif, 'm') != NULL; 131986be9f0dSKonstantin Belousov if (!have_addr) { 13201abfd355SKonstantin Belousov db_printf("usage: show dmar [/d] [/m] index\n"); 132186be9f0dSKonstantin Belousov return; 132286be9f0dSKonstantin Belousov } 13231abfd355SKonstantin Belousov dmar_print_one((int)addr, show_domains, show_mappings); 132486be9f0dSKonstantin Belousov } 132586be9f0dSKonstantin Belousov 132686be9f0dSKonstantin Belousov DB_SHOW_ALL_COMMAND(dmars, db_show_all_dmars) 132786be9f0dSKonstantin Belousov { 132886be9f0dSKonstantin Belousov int i; 13291abfd355SKonstantin Belousov bool show_domains, show_mappings; 133086be9f0dSKonstantin Belousov 13311abfd355SKonstantin Belousov show_domains = strchr(modif, 'd') != NULL; 133286be9f0dSKonstantin Belousov show_mappings = strchr(modif, 'm') != NULL; 133386be9f0dSKonstantin Belousov 133486be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 13351abfd355SKonstantin Belousov dmar_print_one(i, show_domains, show_mappings); 133686be9f0dSKonstantin Belousov if (db_pager_quit) 133786be9f0dSKonstantin Belousov break; 133886be9f0dSKonstantin Belousov } 133986be9f0dSKonstantin Belousov } 134086be9f0dSKonstantin Belousov #endif 134159e37c8aSRuslan Bukin 134259e37c8aSRuslan Bukin struct iommu_unit * 134359e37c8aSRuslan Bukin iommu_find(device_t dev, bool verbose) 134459e37c8aSRuslan Bukin { 134559e37c8aSRuslan Bukin struct dmar_unit *dmar; 134659e37c8aSRuslan Bukin 134759e37c8aSRuslan Bukin dmar = dmar_find(dev, verbose); 134859e37c8aSRuslan Bukin 134959e37c8aSRuslan Bukin return (&dmar->iommu); 135059e37c8aSRuslan Bukin } 1351