186be9f0dSKonstantin Belousov /*- 20a110d5bSKonstantin Belousov * Copyright (c) 2013-2015 The FreeBSD Foundation 386be9f0dSKonstantin Belousov * All rights reserved. 486be9f0dSKonstantin Belousov * 586be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 686be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation. 786be9f0dSKonstantin Belousov * 886be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without 986be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions 1086be9f0dSKonstantin Belousov * are met: 1186be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 1286be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer. 1386be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 1486be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 1586be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution. 1686be9f0dSKonstantin Belousov * 1786be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1886be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1986be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2086be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2186be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2286be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2386be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2486be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2586be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2686be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2786be9f0dSKonstantin Belousov * SUCH DAMAGE. 2886be9f0dSKonstantin Belousov */ 2986be9f0dSKonstantin Belousov 3086be9f0dSKonstantin Belousov #include <sys/cdefs.h> 3186be9f0dSKonstantin Belousov __FBSDID("$FreeBSD$"); 3286be9f0dSKonstantin Belousov 3386be9f0dSKonstantin Belousov #include "opt_acpi.h" 34e7d939bdSMarcel Moolenaar #if defined(__amd64__) 3586be9f0dSKonstantin Belousov #define DEV_APIC 3686be9f0dSKonstantin Belousov #else 3786be9f0dSKonstantin Belousov #include "opt_apic.h" 3886be9f0dSKonstantin Belousov #endif 3986be9f0dSKonstantin Belousov #include "opt_ddb.h" 4086be9f0dSKonstantin Belousov 4186be9f0dSKonstantin Belousov #include <sys/param.h> 4286be9f0dSKonstantin Belousov #include <sys/bus.h> 4386be9f0dSKonstantin Belousov #include <sys/kernel.h> 4486be9f0dSKonstantin Belousov #include <sys/lock.h> 4586be9f0dSKonstantin Belousov #include <sys/malloc.h> 4686be9f0dSKonstantin Belousov #include <sys/memdesc.h> 4786be9f0dSKonstantin Belousov #include <sys/module.h> 4886be9f0dSKonstantin Belousov #include <sys/rman.h> 4986be9f0dSKonstantin Belousov #include <sys/rwlock.h> 5086be9f0dSKonstantin Belousov #include <sys/smp.h> 5186be9f0dSKonstantin Belousov #include <sys/taskqueue.h> 5286be9f0dSKonstantin Belousov #include <sys/tree.h> 530a110d5bSKonstantin Belousov #include <sys/vmem.h> 5486be9f0dSKonstantin Belousov #include <machine/bus.h> 5586be9f0dSKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h> 5686be9f0dSKonstantin Belousov #include <contrib/dev/acpica/include/accommon.h> 5786be9f0dSKonstantin Belousov #include <dev/acpica/acpivar.h> 5886be9f0dSKonstantin Belousov #include <vm/vm.h> 5986be9f0dSKonstantin Belousov #include <vm/vm_extern.h> 6086be9f0dSKonstantin Belousov #include <vm/vm_kern.h> 6186be9f0dSKonstantin Belousov #include <vm/vm_object.h> 6286be9f0dSKonstantin Belousov #include <vm/vm_page.h> 6386be9f0dSKonstantin Belousov #include <vm/vm_pager.h> 6486be9f0dSKonstantin Belousov #include <vm/vm_map.h> 6586be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h> 6686be9f0dSKonstantin Belousov #include <x86/iommu/intel_reg.h> 6786be9f0dSKonstantin Belousov #include <x86/iommu/busdma_dmar.h> 6886be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h> 690a110d5bSKonstantin Belousov #include <dev/pci/pcireg.h> 7086be9f0dSKonstantin Belousov #include <dev/pci/pcivar.h> 7186be9f0dSKonstantin Belousov 7286be9f0dSKonstantin Belousov #ifdef DEV_APIC 7386be9f0dSKonstantin Belousov #include "pcib_if.h" 7486be9f0dSKonstantin Belousov #endif 7586be9f0dSKonstantin Belousov 7668eeb96aSKonstantin Belousov #define DMAR_FAULT_IRQ_RID 0 7768eeb96aSKonstantin Belousov #define DMAR_QI_IRQ_RID 1 7868eeb96aSKonstantin Belousov #define DMAR_REG_RID 2 7986be9f0dSKonstantin Belousov 8086be9f0dSKonstantin Belousov static devclass_t dmar_devclass; 8186be9f0dSKonstantin Belousov static device_t *dmar_devs; 8286be9f0dSKonstantin Belousov static int dmar_devcnt; 8386be9f0dSKonstantin Belousov 8486be9f0dSKonstantin Belousov typedef int (*dmar_iter_t)(ACPI_DMAR_HEADER *, void *); 8586be9f0dSKonstantin Belousov 8686be9f0dSKonstantin Belousov static void 8786be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_iter_t iter, void *arg) 8886be9f0dSKonstantin Belousov { 8986be9f0dSKonstantin Belousov ACPI_TABLE_DMAR *dmartbl; 9086be9f0dSKonstantin Belousov ACPI_DMAR_HEADER *dmarh; 9186be9f0dSKonstantin Belousov char *ptr, *ptrend; 9286be9f0dSKonstantin Belousov ACPI_STATUS status; 9386be9f0dSKonstantin Belousov 9486be9f0dSKonstantin Belousov status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl); 9586be9f0dSKonstantin Belousov if (ACPI_FAILURE(status)) 9686be9f0dSKonstantin Belousov return; 9786be9f0dSKonstantin Belousov ptr = (char *)dmartbl + sizeof(*dmartbl); 9886be9f0dSKonstantin Belousov ptrend = (char *)dmartbl + dmartbl->Header.Length; 9986be9f0dSKonstantin Belousov for (;;) { 10086be9f0dSKonstantin Belousov if (ptr >= ptrend) 10186be9f0dSKonstantin Belousov break; 10286be9f0dSKonstantin Belousov dmarh = (ACPI_DMAR_HEADER *)ptr; 10386be9f0dSKonstantin Belousov if (dmarh->Length <= 0) { 10486be9f0dSKonstantin Belousov printf("dmar_identify: corrupted DMAR table, l %d\n", 10586be9f0dSKonstantin Belousov dmarh->Length); 10686be9f0dSKonstantin Belousov break; 10786be9f0dSKonstantin Belousov } 10886be9f0dSKonstantin Belousov ptr += dmarh->Length; 10986be9f0dSKonstantin Belousov if (!iter(dmarh, arg)) 11086be9f0dSKonstantin Belousov break; 11186be9f0dSKonstantin Belousov } 11286be9f0dSKonstantin Belousov } 11386be9f0dSKonstantin Belousov 11486be9f0dSKonstantin Belousov struct find_iter_args { 11586be9f0dSKonstantin Belousov int i; 11686be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *res; 11786be9f0dSKonstantin Belousov }; 11886be9f0dSKonstantin Belousov 11986be9f0dSKonstantin Belousov static int 12086be9f0dSKonstantin Belousov dmar_find_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 12186be9f0dSKonstantin Belousov { 12286be9f0dSKonstantin Belousov struct find_iter_args *fia; 12386be9f0dSKonstantin Belousov 12486be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_HARDWARE_UNIT) 12586be9f0dSKonstantin Belousov return (1); 12686be9f0dSKonstantin Belousov 12786be9f0dSKonstantin Belousov fia = arg; 12886be9f0dSKonstantin Belousov if (fia->i == 0) { 12986be9f0dSKonstantin Belousov fia->res = (ACPI_DMAR_HARDWARE_UNIT *)dmarh; 13086be9f0dSKonstantin Belousov return (0); 13186be9f0dSKonstantin Belousov } 13286be9f0dSKonstantin Belousov fia->i--; 13386be9f0dSKonstantin Belousov return (1); 13486be9f0dSKonstantin Belousov } 13586be9f0dSKonstantin Belousov 13686be9f0dSKonstantin Belousov static ACPI_DMAR_HARDWARE_UNIT * 13786be9f0dSKonstantin Belousov dmar_find_by_index(int idx) 13886be9f0dSKonstantin Belousov { 13986be9f0dSKonstantin Belousov struct find_iter_args fia; 14086be9f0dSKonstantin Belousov 14186be9f0dSKonstantin Belousov fia.i = idx; 14286be9f0dSKonstantin Belousov fia.res = NULL; 14386be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_find_iter, &fia); 14486be9f0dSKonstantin Belousov return (fia.res); 14586be9f0dSKonstantin Belousov } 14686be9f0dSKonstantin Belousov 14786be9f0dSKonstantin Belousov static int 14886be9f0dSKonstantin Belousov dmar_count_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 14986be9f0dSKonstantin Belousov { 15086be9f0dSKonstantin Belousov 15186be9f0dSKonstantin Belousov if (dmarh->Type == ACPI_DMAR_TYPE_HARDWARE_UNIT) 15286be9f0dSKonstantin Belousov dmar_devcnt++; 15386be9f0dSKonstantin Belousov return (1); 15486be9f0dSKonstantin Belousov } 15586be9f0dSKonstantin Belousov 15686be9f0dSKonstantin Belousov static int dmar_enable = 0; 15786be9f0dSKonstantin Belousov static void 15886be9f0dSKonstantin Belousov dmar_identify(driver_t *driver, device_t parent) 15986be9f0dSKonstantin Belousov { 16086be9f0dSKonstantin Belousov ACPI_TABLE_DMAR *dmartbl; 16186be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh; 16286be9f0dSKonstantin Belousov ACPI_STATUS status; 16386be9f0dSKonstantin Belousov int i, error; 16486be9f0dSKonstantin Belousov 16586be9f0dSKonstantin Belousov if (acpi_disabled("dmar")) 16686be9f0dSKonstantin Belousov return; 16786be9f0dSKonstantin Belousov TUNABLE_INT_FETCH("hw.dmar.enable", &dmar_enable); 16886be9f0dSKonstantin Belousov if (!dmar_enable) 16986be9f0dSKonstantin Belousov return; 17086be9f0dSKonstantin Belousov #ifdef INVARIANTS 17186be9f0dSKonstantin Belousov TUNABLE_INT_FETCH("hw.dmar.check_free", &dmar_check_free); 17286be9f0dSKonstantin Belousov #endif 17386be9f0dSKonstantin Belousov TUNABLE_INT_FETCH("hw.dmar.match_verbose", &dmar_match_verbose); 17486be9f0dSKonstantin Belousov status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl); 17586be9f0dSKonstantin Belousov if (ACPI_FAILURE(status)) 17686be9f0dSKonstantin Belousov return; 17786be9f0dSKonstantin Belousov haw = dmartbl->Width + 1; 17886be9f0dSKonstantin Belousov if ((1ULL << (haw + 1)) > BUS_SPACE_MAXADDR) 17986be9f0dSKonstantin Belousov dmar_high = BUS_SPACE_MAXADDR; 18086be9f0dSKonstantin Belousov else 18186be9f0dSKonstantin Belousov dmar_high = 1ULL << (haw + 1); 18286be9f0dSKonstantin Belousov if (bootverbose) { 18386be9f0dSKonstantin Belousov printf("DMAR HAW=%d flags=<%b>\n", dmartbl->Width, 18486be9f0dSKonstantin Belousov (unsigned)dmartbl->Flags, 18586be9f0dSKonstantin Belousov "\020\001INTR_REMAP\002X2APIC_OPT_OUT"); 18686be9f0dSKonstantin Belousov } 18786be9f0dSKonstantin Belousov 18886be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_count_iter, NULL); 18986be9f0dSKonstantin Belousov if (dmar_devcnt == 0) 19086be9f0dSKonstantin Belousov return; 19186be9f0dSKonstantin Belousov dmar_devs = malloc(sizeof(device_t) * dmar_devcnt, M_DEVBUF, 19286be9f0dSKonstantin Belousov M_WAITOK | M_ZERO); 19386be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 19486be9f0dSKonstantin Belousov dmarh = dmar_find_by_index(i); 19586be9f0dSKonstantin Belousov if (dmarh == NULL) { 19686be9f0dSKonstantin Belousov printf("dmar_identify: cannot find HWUNIT %d\n", i); 19786be9f0dSKonstantin Belousov continue; 19886be9f0dSKonstantin Belousov } 19986be9f0dSKonstantin Belousov dmar_devs[i] = BUS_ADD_CHILD(parent, 1, "dmar", i); 20086be9f0dSKonstantin Belousov if (dmar_devs[i] == NULL) { 20186be9f0dSKonstantin Belousov printf("dmar_identify: cannot create instance %d\n", i); 20286be9f0dSKonstantin Belousov continue; 20386be9f0dSKonstantin Belousov } 20486be9f0dSKonstantin Belousov error = bus_set_resource(dmar_devs[i], SYS_RES_MEMORY, 20586be9f0dSKonstantin Belousov DMAR_REG_RID, dmarh->Address, PAGE_SIZE); 20686be9f0dSKonstantin Belousov if (error != 0) { 20786be9f0dSKonstantin Belousov printf( 20886be9f0dSKonstantin Belousov "dmar%d: unable to alloc register window at 0x%08jx: error %d\n", 20986be9f0dSKonstantin Belousov i, (uintmax_t)dmarh->Address, error); 21086be9f0dSKonstantin Belousov device_delete_child(parent, dmar_devs[i]); 21186be9f0dSKonstantin Belousov dmar_devs[i] = NULL; 21286be9f0dSKonstantin Belousov } 21386be9f0dSKonstantin Belousov } 21486be9f0dSKonstantin Belousov } 21586be9f0dSKonstantin Belousov 21686be9f0dSKonstantin Belousov static int 21786be9f0dSKonstantin Belousov dmar_probe(device_t dev) 21886be9f0dSKonstantin Belousov { 21986be9f0dSKonstantin Belousov 22086be9f0dSKonstantin Belousov if (acpi_get_handle(dev) != NULL) 22186be9f0dSKonstantin Belousov return (ENXIO); 22286be9f0dSKonstantin Belousov device_set_desc(dev, "DMA remap"); 2233100f7dfSKonstantin Belousov return (BUS_PROBE_NOWILDCARD); 22486be9f0dSKonstantin Belousov } 22586be9f0dSKonstantin Belousov 22686be9f0dSKonstantin Belousov static void 22768eeb96aSKonstantin Belousov dmar_release_intr(device_t dev, struct dmar_unit *unit, int idx) 22868eeb96aSKonstantin Belousov { 22968eeb96aSKonstantin Belousov struct dmar_msi_data *dmd; 23068eeb96aSKonstantin Belousov 23168eeb96aSKonstantin Belousov dmd = &unit->intrs[idx]; 23268eeb96aSKonstantin Belousov if (dmd->irq == -1) 23368eeb96aSKonstantin Belousov return; 23468eeb96aSKonstantin Belousov bus_teardown_intr(dev, dmd->irq_res, dmd->intr_handle); 23568eeb96aSKonstantin Belousov bus_release_resource(dev, SYS_RES_IRQ, dmd->irq_rid, dmd->irq_res); 23668eeb96aSKonstantin Belousov bus_delete_resource(dev, SYS_RES_IRQ, dmd->irq_rid); 23768eeb96aSKonstantin Belousov PCIB_RELEASE_MSIX(device_get_parent(device_get_parent(dev)), 23868eeb96aSKonstantin Belousov dev, dmd->irq); 23968eeb96aSKonstantin Belousov dmd->irq = -1; 24068eeb96aSKonstantin Belousov } 24168eeb96aSKonstantin Belousov 24268eeb96aSKonstantin Belousov static void 24386be9f0dSKonstantin Belousov dmar_release_resources(device_t dev, struct dmar_unit *unit) 24486be9f0dSKonstantin Belousov { 24568eeb96aSKonstantin Belousov int i; 24686be9f0dSKonstantin Belousov 24786be9f0dSKonstantin Belousov dmar_fini_busdma(unit); 2480a110d5bSKonstantin Belousov dmar_fini_irt(unit); 24968eeb96aSKonstantin Belousov dmar_fini_qi(unit); 25086be9f0dSKonstantin Belousov dmar_fini_fault_log(unit); 25168eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++) 25268eeb96aSKonstantin Belousov dmar_release_intr(dev, unit, i); 25386be9f0dSKonstantin Belousov if (unit->regs != NULL) { 25486be9f0dSKonstantin Belousov bus_deactivate_resource(dev, SYS_RES_MEMORY, unit->reg_rid, 25586be9f0dSKonstantin Belousov unit->regs); 25686be9f0dSKonstantin Belousov bus_release_resource(dev, SYS_RES_MEMORY, unit->reg_rid, 25786be9f0dSKonstantin Belousov unit->regs); 25886be9f0dSKonstantin Belousov unit->regs = NULL; 25986be9f0dSKonstantin Belousov } 26086be9f0dSKonstantin Belousov if (unit->domids != NULL) { 26186be9f0dSKonstantin Belousov delete_unrhdr(unit->domids); 26286be9f0dSKonstantin Belousov unit->domids = NULL; 26386be9f0dSKonstantin Belousov } 26486be9f0dSKonstantin Belousov if (unit->ctx_obj != NULL) { 26586be9f0dSKonstantin Belousov vm_object_deallocate(unit->ctx_obj); 26686be9f0dSKonstantin Belousov unit->ctx_obj = NULL; 26786be9f0dSKonstantin Belousov } 26886be9f0dSKonstantin Belousov } 26986be9f0dSKonstantin Belousov 27086be9f0dSKonstantin Belousov static int 27168eeb96aSKonstantin Belousov dmar_alloc_irq(device_t dev, struct dmar_unit *unit, int idx) 27286be9f0dSKonstantin Belousov { 27386be9f0dSKonstantin Belousov device_t pcib; 27468eeb96aSKonstantin Belousov struct dmar_msi_data *dmd; 27586be9f0dSKonstantin Belousov uint64_t msi_addr; 27686be9f0dSKonstantin Belousov uint32_t msi_data; 27786be9f0dSKonstantin Belousov int error; 27886be9f0dSKonstantin Belousov 27968eeb96aSKonstantin Belousov dmd = &unit->intrs[idx]; 28086be9f0dSKonstantin Belousov pcib = device_get_parent(device_get_parent(dev)); /* Really not pcib */ 28168eeb96aSKonstantin Belousov error = PCIB_ALLOC_MSIX(pcib, dev, &dmd->irq); 28286be9f0dSKonstantin Belousov if (error != 0) { 28368eeb96aSKonstantin Belousov device_printf(dev, "cannot allocate %s interrupt, %d\n", 28468eeb96aSKonstantin Belousov dmd->name, error); 28586be9f0dSKonstantin Belousov goto err1; 28686be9f0dSKonstantin Belousov } 28768eeb96aSKonstantin Belousov error = bus_set_resource(dev, SYS_RES_IRQ, dmd->irq_rid, 28868eeb96aSKonstantin Belousov dmd->irq, 1); 28986be9f0dSKonstantin Belousov if (error != 0) { 29068eeb96aSKonstantin Belousov device_printf(dev, "cannot set %s interrupt resource, %d\n", 29168eeb96aSKonstantin Belousov dmd->name, error); 29286be9f0dSKonstantin Belousov goto err2; 29386be9f0dSKonstantin Belousov } 29468eeb96aSKonstantin Belousov dmd->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 29568eeb96aSKonstantin Belousov &dmd->irq_rid, RF_ACTIVE); 29668eeb96aSKonstantin Belousov if (dmd->irq_res == NULL) { 29768eeb96aSKonstantin Belousov device_printf(dev, 29868eeb96aSKonstantin Belousov "cannot allocate resource for %s interrupt\n", dmd->name); 29986be9f0dSKonstantin Belousov error = ENXIO; 30086be9f0dSKonstantin Belousov goto err3; 30186be9f0dSKonstantin Belousov } 30268eeb96aSKonstantin Belousov error = bus_setup_intr(dev, dmd->irq_res, INTR_TYPE_MISC, 30368eeb96aSKonstantin Belousov dmd->handler, NULL, unit, &dmd->intr_handle); 30486be9f0dSKonstantin Belousov if (error != 0) { 30568eeb96aSKonstantin Belousov device_printf(dev, "cannot setup %s interrupt, %d\n", 30668eeb96aSKonstantin Belousov dmd->name, error); 30786be9f0dSKonstantin Belousov goto err4; 30886be9f0dSKonstantin Belousov } 30968eeb96aSKonstantin Belousov bus_describe_intr(dev, dmd->irq_res, dmd->intr_handle, dmd->name); 31068eeb96aSKonstantin Belousov error = PCIB_MAP_MSI(pcib, dev, dmd->irq, &msi_addr, &msi_data); 31186be9f0dSKonstantin Belousov if (error != 0) { 31268eeb96aSKonstantin Belousov device_printf(dev, "cannot map %s interrupt, %d\n", 31368eeb96aSKonstantin Belousov dmd->name, error); 31486be9f0dSKonstantin Belousov goto err5; 31586be9f0dSKonstantin Belousov } 31668eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_data_reg, msi_data); 31768eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_addr_reg, msi_addr); 31886be9f0dSKonstantin Belousov /* Only for xAPIC mode */ 31968eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_uaddr_reg, msi_addr >> 32); 32086be9f0dSKonstantin Belousov return (0); 32186be9f0dSKonstantin Belousov 32286be9f0dSKonstantin Belousov err5: 32368eeb96aSKonstantin Belousov bus_teardown_intr(dev, dmd->irq_res, dmd->intr_handle); 32486be9f0dSKonstantin Belousov err4: 32568eeb96aSKonstantin Belousov bus_release_resource(dev, SYS_RES_IRQ, dmd->irq_rid, dmd->irq_res); 32686be9f0dSKonstantin Belousov err3: 32768eeb96aSKonstantin Belousov bus_delete_resource(dev, SYS_RES_IRQ, dmd->irq_rid); 32886be9f0dSKonstantin Belousov err2: 32968eeb96aSKonstantin Belousov PCIB_RELEASE_MSIX(pcib, dev, dmd->irq); 33068eeb96aSKonstantin Belousov dmd->irq = -1; 33186be9f0dSKonstantin Belousov err1: 33286be9f0dSKonstantin Belousov return (error); 33386be9f0dSKonstantin Belousov } 33486be9f0dSKonstantin Belousov 33586be9f0dSKonstantin Belousov #ifdef DEV_APIC 33686be9f0dSKonstantin Belousov static int 33786be9f0dSKonstantin Belousov dmar_remap_intr(device_t dev, device_t child, u_int irq) 33886be9f0dSKonstantin Belousov { 33986be9f0dSKonstantin Belousov struct dmar_unit *unit; 34068eeb96aSKonstantin Belousov struct dmar_msi_data *dmd; 34186be9f0dSKonstantin Belousov uint64_t msi_addr; 34286be9f0dSKonstantin Belousov uint32_t msi_data; 34368eeb96aSKonstantin Belousov int i, error; 34486be9f0dSKonstantin Belousov 34586be9f0dSKonstantin Belousov unit = device_get_softc(dev); 34668eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++) { 34768eeb96aSKonstantin Belousov dmd = &unit->intrs[i]; 34868eeb96aSKonstantin Belousov if (irq == dmd->irq) { 34968eeb96aSKonstantin Belousov error = PCIB_MAP_MSI(device_get_parent( 35068eeb96aSKonstantin Belousov device_get_parent(dev)), 35168eeb96aSKonstantin Belousov dev, irq, &msi_addr, &msi_data); 35286be9f0dSKonstantin Belousov if (error != 0) 35386be9f0dSKonstantin Belousov return (error); 35468eeb96aSKonstantin Belousov DMAR_LOCK(unit); 35568eeb96aSKonstantin Belousov (dmd->disable_intr)(unit); 35668eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_data_reg, msi_data); 35768eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_addr_reg, msi_addr); 35868eeb96aSKonstantin Belousov dmar_write4(unit, dmd->msi_uaddr_reg, msi_addr >> 32); 35968eeb96aSKonstantin Belousov (dmd->enable_intr)(unit); 36068eeb96aSKonstantin Belousov DMAR_UNLOCK(unit); 36186be9f0dSKonstantin Belousov return (0); 36286be9f0dSKonstantin Belousov } 36368eeb96aSKonstantin Belousov } 36468eeb96aSKonstantin Belousov return (ENOENT); 36568eeb96aSKonstantin Belousov } 36686be9f0dSKonstantin Belousov #endif 36786be9f0dSKonstantin Belousov 36886be9f0dSKonstantin Belousov static void 36986be9f0dSKonstantin Belousov dmar_print_caps(device_t dev, struct dmar_unit *unit, 37086be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmaru) 37186be9f0dSKonstantin Belousov { 37286be9f0dSKonstantin Belousov uint32_t caphi, ecaphi; 37386be9f0dSKonstantin Belousov 37486be9f0dSKonstantin Belousov device_printf(dev, "regs@0x%08jx, ver=%d.%d, seg=%d, flags=<%b>\n", 37586be9f0dSKonstantin Belousov (uintmax_t)dmaru->Address, DMAR_MAJOR_VER(unit->hw_ver), 37686be9f0dSKonstantin Belousov DMAR_MINOR_VER(unit->hw_ver), dmaru->Segment, 37786be9f0dSKonstantin Belousov dmaru->Flags, "\020\001INCLUDE_ALL_PCI"); 37886be9f0dSKonstantin Belousov caphi = unit->hw_cap >> 32; 37986be9f0dSKonstantin Belousov device_printf(dev, "cap=%b,", (u_int)unit->hw_cap, 38086be9f0dSKonstantin Belousov "\020\004AFL\005WBF\006PLMR\007PHMR\010CM\027ZLR\030ISOCH"); 381e17c0a1eSKonstantin Belousov printf("%b, ", caphi, "\020\010PSI\027DWD\030DRD\031FL1GP\034PSI"); 38286be9f0dSKonstantin Belousov printf("ndoms=%d, sagaw=%d, mgaw=%d, fro=%d, nfr=%d, superp=%d", 38386be9f0dSKonstantin Belousov DMAR_CAP_ND(unit->hw_cap), DMAR_CAP_SAGAW(unit->hw_cap), 38486be9f0dSKonstantin Belousov DMAR_CAP_MGAW(unit->hw_cap), DMAR_CAP_FRO(unit->hw_cap), 38586be9f0dSKonstantin Belousov DMAR_CAP_NFR(unit->hw_cap), DMAR_CAP_SPS(unit->hw_cap)); 38686be9f0dSKonstantin Belousov if ((unit->hw_cap & DMAR_CAP_PSI) != 0) 38786be9f0dSKonstantin Belousov printf(", mamv=%d", DMAR_CAP_MAMV(unit->hw_cap)); 38886be9f0dSKonstantin Belousov printf("\n"); 38986be9f0dSKonstantin Belousov ecaphi = unit->hw_ecap >> 32; 39086be9f0dSKonstantin Belousov device_printf(dev, "ecap=%b,", (u_int)unit->hw_ecap, 391e17c0a1eSKonstantin Belousov "\020\001C\002QI\003DI\004IR\005EIM\007PT\010SC\031ECS\032MTS" 392e17c0a1eSKonstantin Belousov "\033NEST\034DIS\035PASID\036PRS\037ERS\040SRS"); 393e17c0a1eSKonstantin Belousov printf("%b, ", ecaphi, "\020\002NWFS\003EAFS"); 39486be9f0dSKonstantin Belousov printf("mhmw=%d, iro=%d\n", DMAR_ECAP_MHMV(unit->hw_ecap), 39586be9f0dSKonstantin Belousov DMAR_ECAP_IRO(unit->hw_ecap)); 39686be9f0dSKonstantin Belousov } 39786be9f0dSKonstantin Belousov 39886be9f0dSKonstantin Belousov static int 39986be9f0dSKonstantin Belousov dmar_attach(device_t dev) 40086be9f0dSKonstantin Belousov { 40186be9f0dSKonstantin Belousov struct dmar_unit *unit; 40286be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmaru; 40368eeb96aSKonstantin Belousov int i, error; 40486be9f0dSKonstantin Belousov 40586be9f0dSKonstantin Belousov unit = device_get_softc(dev); 40686be9f0dSKonstantin Belousov unit->dev = dev; 40786be9f0dSKonstantin Belousov unit->unit = device_get_unit(dev); 40886be9f0dSKonstantin Belousov dmaru = dmar_find_by_index(unit->unit); 40986be9f0dSKonstantin Belousov if (dmaru == NULL) 41086be9f0dSKonstantin Belousov return (EINVAL); 41186be9f0dSKonstantin Belousov unit->segment = dmaru->Segment; 41286be9f0dSKonstantin Belousov unit->base = dmaru->Address; 41386be9f0dSKonstantin Belousov unit->reg_rid = DMAR_REG_RID; 41486be9f0dSKonstantin Belousov unit->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 41586be9f0dSKonstantin Belousov &unit->reg_rid, RF_ACTIVE); 41686be9f0dSKonstantin Belousov if (unit->regs == NULL) { 41786be9f0dSKonstantin Belousov device_printf(dev, "cannot allocate register window\n"); 41886be9f0dSKonstantin Belousov return (ENOMEM); 41986be9f0dSKonstantin Belousov } 42086be9f0dSKonstantin Belousov unit->hw_ver = dmar_read4(unit, DMAR_VER_REG); 42186be9f0dSKonstantin Belousov unit->hw_cap = dmar_read8(unit, DMAR_CAP_REG); 42286be9f0dSKonstantin Belousov unit->hw_ecap = dmar_read8(unit, DMAR_ECAP_REG); 42386be9f0dSKonstantin Belousov if (bootverbose) 42486be9f0dSKonstantin Belousov dmar_print_caps(dev, unit, dmaru); 42586be9f0dSKonstantin Belousov dmar_quirks_post_ident(unit); 42686be9f0dSKonstantin Belousov 42768eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++) 42868eeb96aSKonstantin Belousov unit->intrs[i].irq = -1; 42968eeb96aSKonstantin Belousov 43068eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].name = "fault"; 43168eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].irq_rid = DMAR_FAULT_IRQ_RID; 43268eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].handler = dmar_fault_intr; 43368eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].msi_data_reg = DMAR_FEDATA_REG; 43468eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].msi_addr_reg = DMAR_FEADDR_REG; 43568eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].msi_uaddr_reg = DMAR_FEUADDR_REG; 43668eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].enable_intr = dmar_enable_fault_intr; 43768eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_FAULT].disable_intr = dmar_disable_fault_intr; 43868eeb96aSKonstantin Belousov error = dmar_alloc_irq(dev, unit, DMAR_INTR_FAULT); 43986be9f0dSKonstantin Belousov if (error != 0) { 44086be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 44186be9f0dSKonstantin Belousov return (error); 44286be9f0dSKonstantin Belousov } 44368eeb96aSKonstantin Belousov if (DMAR_HAS_QI(unit)) { 44468eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].name = "qi"; 44568eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].irq_rid = DMAR_QI_IRQ_RID; 44668eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].handler = dmar_qi_intr; 44768eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].msi_data_reg = DMAR_IEDATA_REG; 44868eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].msi_addr_reg = DMAR_IEADDR_REG; 44968eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].msi_uaddr_reg = DMAR_IEUADDR_REG; 45068eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].enable_intr = dmar_enable_qi_intr; 45168eeb96aSKonstantin Belousov unit->intrs[DMAR_INTR_QI].disable_intr = dmar_disable_qi_intr; 45268eeb96aSKonstantin Belousov error = dmar_alloc_irq(dev, unit, DMAR_INTR_QI); 45368eeb96aSKonstantin Belousov if (error != 0) { 45468eeb96aSKonstantin Belousov dmar_release_resources(dev, unit); 45568eeb96aSKonstantin Belousov return (error); 45668eeb96aSKonstantin Belousov } 45768eeb96aSKonstantin Belousov } 45868eeb96aSKonstantin Belousov 45986be9f0dSKonstantin Belousov mtx_init(&unit->lock, "dmarhw", NULL, MTX_DEF); 46086be9f0dSKonstantin Belousov unit->domids = new_unrhdr(0, dmar_nd2mask(DMAR_CAP_ND(unit->hw_cap)), 46186be9f0dSKonstantin Belousov &unit->lock); 462*1abfd355SKonstantin Belousov LIST_INIT(&unit->domains); 46386be9f0dSKonstantin Belousov 46486be9f0dSKonstantin Belousov /* 46586be9f0dSKonstantin Belousov * 9.2 "Context Entry": 46686be9f0dSKonstantin Belousov * When Caching Mode (CM) field is reported as Set, the 46786be9f0dSKonstantin Belousov * domain-id value of zero is architecturally reserved. 46886be9f0dSKonstantin Belousov * Software must not use domain-id value of zero 46986be9f0dSKonstantin Belousov * when CM is Set. 47086be9f0dSKonstantin Belousov */ 47186be9f0dSKonstantin Belousov if ((unit->hw_cap & DMAR_CAP_CM) != 0) 47286be9f0dSKonstantin Belousov alloc_unr_specific(unit->domids, 0); 47386be9f0dSKonstantin Belousov 47486be9f0dSKonstantin Belousov unit->ctx_obj = vm_pager_allocate(OBJT_PHYS, NULL, IDX_TO_OFF(1 + 47586be9f0dSKonstantin Belousov DMAR_CTX_CNT), 0, 0, NULL); 47686be9f0dSKonstantin Belousov 47786be9f0dSKonstantin Belousov /* 47886be9f0dSKonstantin Belousov * Allocate and load the root entry table pointer. Enable the 47986be9f0dSKonstantin Belousov * address translation after the required invalidations are 48086be9f0dSKonstantin Belousov * done. 48186be9f0dSKonstantin Belousov */ 48286be9f0dSKonstantin Belousov dmar_pgalloc(unit->ctx_obj, 0, DMAR_PGF_WAITOK | DMAR_PGF_ZERO); 48386be9f0dSKonstantin Belousov DMAR_LOCK(unit); 48486be9f0dSKonstantin Belousov error = dmar_load_root_entry_ptr(unit); 48586be9f0dSKonstantin Belousov if (error != 0) { 48686be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 48786be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 48886be9f0dSKonstantin Belousov return (error); 48986be9f0dSKonstantin Belousov } 49086be9f0dSKonstantin Belousov error = dmar_inv_ctx_glob(unit); 49186be9f0dSKonstantin Belousov if (error != 0) { 49286be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 49386be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 49486be9f0dSKonstantin Belousov return (error); 49586be9f0dSKonstantin Belousov } 49686be9f0dSKonstantin Belousov if ((unit->hw_ecap & DMAR_ECAP_DI) != 0) { 49786be9f0dSKonstantin Belousov error = dmar_inv_iotlb_glob(unit); 49886be9f0dSKonstantin Belousov if (error != 0) { 49986be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 50086be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 50186be9f0dSKonstantin Belousov return (error); 50286be9f0dSKonstantin Belousov } 50386be9f0dSKonstantin Belousov } 50486be9f0dSKonstantin Belousov 50586be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 50686be9f0dSKonstantin Belousov error = dmar_init_fault_log(unit); 50786be9f0dSKonstantin Belousov if (error != 0) { 50886be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 50986be9f0dSKonstantin Belousov return (error); 51086be9f0dSKonstantin Belousov } 51168eeb96aSKonstantin Belousov error = dmar_init_qi(unit); 51268eeb96aSKonstantin Belousov if (error != 0) { 51368eeb96aSKonstantin Belousov dmar_release_resources(dev, unit); 51468eeb96aSKonstantin Belousov return (error); 51568eeb96aSKonstantin Belousov } 5160a110d5bSKonstantin Belousov error = dmar_init_irt(unit); 5170a110d5bSKonstantin Belousov if (error != 0) { 5180a110d5bSKonstantin Belousov dmar_release_resources(dev, unit); 5190a110d5bSKonstantin Belousov return (error); 5200a110d5bSKonstantin Belousov } 52186be9f0dSKonstantin Belousov error = dmar_init_busdma(unit); 52286be9f0dSKonstantin Belousov if (error != 0) { 52386be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 52486be9f0dSKonstantin Belousov return (error); 52586be9f0dSKonstantin Belousov } 52686be9f0dSKonstantin Belousov 52786be9f0dSKonstantin Belousov #ifdef NOTYET 52886be9f0dSKonstantin Belousov DMAR_LOCK(unit); 52986be9f0dSKonstantin Belousov error = dmar_enable_translation(unit); 53086be9f0dSKonstantin Belousov if (error != 0) { 53186be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 53286be9f0dSKonstantin Belousov dmar_release_resources(dev, unit); 53386be9f0dSKonstantin Belousov return (error); 53486be9f0dSKonstantin Belousov } 53586be9f0dSKonstantin Belousov DMAR_UNLOCK(unit); 53686be9f0dSKonstantin Belousov #endif 53786be9f0dSKonstantin Belousov 53886be9f0dSKonstantin Belousov return (0); 53986be9f0dSKonstantin Belousov } 54086be9f0dSKonstantin Belousov 54186be9f0dSKonstantin Belousov static int 54286be9f0dSKonstantin Belousov dmar_detach(device_t dev) 54386be9f0dSKonstantin Belousov { 54486be9f0dSKonstantin Belousov 54586be9f0dSKonstantin Belousov return (EBUSY); 54686be9f0dSKonstantin Belousov } 54786be9f0dSKonstantin Belousov 54886be9f0dSKonstantin Belousov static int 54986be9f0dSKonstantin Belousov dmar_suspend(device_t dev) 55086be9f0dSKonstantin Belousov { 55186be9f0dSKonstantin Belousov 55286be9f0dSKonstantin Belousov return (0); 55386be9f0dSKonstantin Belousov } 55486be9f0dSKonstantin Belousov 55586be9f0dSKonstantin Belousov static int 55686be9f0dSKonstantin Belousov dmar_resume(device_t dev) 55786be9f0dSKonstantin Belousov { 55886be9f0dSKonstantin Belousov 55986be9f0dSKonstantin Belousov /* XXXKIB */ 56086be9f0dSKonstantin Belousov return (0); 56186be9f0dSKonstantin Belousov } 56286be9f0dSKonstantin Belousov 56386be9f0dSKonstantin Belousov static device_method_t dmar_methods[] = { 56486be9f0dSKonstantin Belousov DEVMETHOD(device_identify, dmar_identify), 56586be9f0dSKonstantin Belousov DEVMETHOD(device_probe, dmar_probe), 56686be9f0dSKonstantin Belousov DEVMETHOD(device_attach, dmar_attach), 56786be9f0dSKonstantin Belousov DEVMETHOD(device_detach, dmar_detach), 56886be9f0dSKonstantin Belousov DEVMETHOD(device_suspend, dmar_suspend), 56986be9f0dSKonstantin Belousov DEVMETHOD(device_resume, dmar_resume), 57086be9f0dSKonstantin Belousov #ifdef DEV_APIC 57186be9f0dSKonstantin Belousov DEVMETHOD(bus_remap_intr, dmar_remap_intr), 57286be9f0dSKonstantin Belousov #endif 57386be9f0dSKonstantin Belousov DEVMETHOD_END 57486be9f0dSKonstantin Belousov }; 57586be9f0dSKonstantin Belousov 57686be9f0dSKonstantin Belousov static driver_t dmar_driver = { 57786be9f0dSKonstantin Belousov "dmar", 57886be9f0dSKonstantin Belousov dmar_methods, 57986be9f0dSKonstantin Belousov sizeof(struct dmar_unit), 58086be9f0dSKonstantin Belousov }; 58186be9f0dSKonstantin Belousov 58286be9f0dSKonstantin Belousov DRIVER_MODULE(dmar, acpi, dmar_driver, dmar_devclass, 0, 0); 58386be9f0dSKonstantin Belousov MODULE_DEPEND(dmar, acpi, 1, 1, 1); 58486be9f0dSKonstantin Belousov 58586be9f0dSKonstantin Belousov static void 58686be9f0dSKonstantin Belousov dmar_print_path(device_t dev, const char *banner, int busno, int depth, 58786be9f0dSKonstantin Belousov const ACPI_DMAR_PCI_PATH *path) 58886be9f0dSKonstantin Belousov { 58986be9f0dSKonstantin Belousov int i; 59086be9f0dSKonstantin Belousov 59186be9f0dSKonstantin Belousov device_printf(dev, "%s [%d, ", banner, busno); 59286be9f0dSKonstantin Belousov for (i = 0; i < depth; i++) { 59386be9f0dSKonstantin Belousov if (i != 0) 59486be9f0dSKonstantin Belousov printf(", "); 59586be9f0dSKonstantin Belousov printf("(%d, %d)", path[i].Device, path[i].Function); 59686be9f0dSKonstantin Belousov } 59786be9f0dSKonstantin Belousov printf("]\n"); 59886be9f0dSKonstantin Belousov } 59986be9f0dSKonstantin Belousov 60086be9f0dSKonstantin Belousov static int 60186be9f0dSKonstantin Belousov dmar_dev_depth(device_t child) 60286be9f0dSKonstantin Belousov { 60386be9f0dSKonstantin Belousov devclass_t pci_class; 60486be9f0dSKonstantin Belousov device_t bus, pcib; 60586be9f0dSKonstantin Belousov int depth; 60686be9f0dSKonstantin Belousov 60786be9f0dSKonstantin Belousov pci_class = devclass_find("pci"); 60886be9f0dSKonstantin Belousov for (depth = 1; ; depth++) { 60986be9f0dSKonstantin Belousov bus = device_get_parent(child); 61086be9f0dSKonstantin Belousov pcib = device_get_parent(bus); 61186be9f0dSKonstantin Belousov if (device_get_devclass(device_get_parent(pcib)) != 61286be9f0dSKonstantin Belousov pci_class) 61386be9f0dSKonstantin Belousov return (depth); 61486be9f0dSKonstantin Belousov child = pcib; 61586be9f0dSKonstantin Belousov } 61686be9f0dSKonstantin Belousov } 61786be9f0dSKonstantin Belousov 61886be9f0dSKonstantin Belousov static void 61986be9f0dSKonstantin Belousov dmar_dev_path(device_t child, int *busno, ACPI_DMAR_PCI_PATH *path, int depth) 62086be9f0dSKonstantin Belousov { 62186be9f0dSKonstantin Belousov devclass_t pci_class; 62286be9f0dSKonstantin Belousov device_t bus, pcib; 62386be9f0dSKonstantin Belousov 62486be9f0dSKonstantin Belousov pci_class = devclass_find("pci"); 62586be9f0dSKonstantin Belousov for (depth--; depth != -1; depth--) { 62686be9f0dSKonstantin Belousov path[depth].Device = pci_get_slot(child); 62786be9f0dSKonstantin Belousov path[depth].Function = pci_get_function(child); 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 /* reached a host bridge */ 63386be9f0dSKonstantin Belousov *busno = pcib_get_bus(bus); 63486be9f0dSKonstantin Belousov return; 63586be9f0dSKonstantin Belousov } 63686be9f0dSKonstantin Belousov child = pcib; 63786be9f0dSKonstantin Belousov } 63886be9f0dSKonstantin Belousov panic("wrong depth"); 63986be9f0dSKonstantin Belousov } 64086be9f0dSKonstantin Belousov 64186be9f0dSKonstantin Belousov static int 64286be9f0dSKonstantin Belousov dmar_match_pathes(int busno1, const ACPI_DMAR_PCI_PATH *path1, int depth1, 64386be9f0dSKonstantin Belousov int busno2, const ACPI_DMAR_PCI_PATH *path2, int depth2, 64486be9f0dSKonstantin Belousov enum AcpiDmarScopeType scope_type) 64586be9f0dSKonstantin Belousov { 64686be9f0dSKonstantin Belousov int i, depth; 64786be9f0dSKonstantin Belousov 64886be9f0dSKonstantin Belousov if (busno1 != busno2) 64986be9f0dSKonstantin Belousov return (0); 65086be9f0dSKonstantin Belousov if (scope_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && depth1 != depth2) 65186be9f0dSKonstantin Belousov return (0); 65286be9f0dSKonstantin Belousov depth = depth1; 65386be9f0dSKonstantin Belousov if (depth2 < depth) 65486be9f0dSKonstantin Belousov depth = depth2; 65586be9f0dSKonstantin Belousov for (i = 0; i < depth; i++) { 65686be9f0dSKonstantin Belousov if (path1[i].Device != path2[i].Device || 65786be9f0dSKonstantin Belousov path1[i].Function != path2[i].Function) 65886be9f0dSKonstantin Belousov return (0); 65986be9f0dSKonstantin Belousov } 66086be9f0dSKonstantin Belousov return (1); 66186be9f0dSKonstantin Belousov } 66286be9f0dSKonstantin Belousov 66386be9f0dSKonstantin Belousov static int 66486be9f0dSKonstantin Belousov dmar_match_devscope(ACPI_DMAR_DEVICE_SCOPE *devscope, device_t dev, 66586be9f0dSKonstantin Belousov int dev_busno, const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len) 66686be9f0dSKonstantin Belousov { 66786be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH *path; 66886be9f0dSKonstantin Belousov int path_len; 66986be9f0dSKonstantin Belousov 67086be9f0dSKonstantin Belousov if (devscope->Length < sizeof(*devscope)) { 67186be9f0dSKonstantin Belousov printf("dmar_find: corrupted DMAR table, dl %d\n", 67286be9f0dSKonstantin Belousov devscope->Length); 67386be9f0dSKonstantin Belousov return (-1); 67486be9f0dSKonstantin Belousov } 67586be9f0dSKonstantin Belousov if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT && 67686be9f0dSKonstantin Belousov devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_BRIDGE) 67786be9f0dSKonstantin Belousov return (0); 67886be9f0dSKonstantin Belousov path_len = devscope->Length - sizeof(*devscope); 67986be9f0dSKonstantin Belousov if (path_len % 2 != 0) { 68086be9f0dSKonstantin Belousov printf("dmar_find_bsf: corrupted DMAR table, dl %d\n", 68186be9f0dSKonstantin Belousov devscope->Length); 68286be9f0dSKonstantin Belousov return (-1); 68386be9f0dSKonstantin Belousov } 68486be9f0dSKonstantin Belousov path_len /= 2; 68586be9f0dSKonstantin Belousov path = (ACPI_DMAR_PCI_PATH *)(devscope + 1); 68686be9f0dSKonstantin Belousov if (path_len == 0) { 68786be9f0dSKonstantin Belousov printf("dmar_find: corrupted DMAR table, dl %d\n", 68886be9f0dSKonstantin Belousov devscope->Length); 68986be9f0dSKonstantin Belousov return (-1); 69086be9f0dSKonstantin Belousov } 69186be9f0dSKonstantin Belousov if (dmar_match_verbose) 69286be9f0dSKonstantin Belousov dmar_print_path(dev, "DMAR", devscope->Bus, path_len, path); 69386be9f0dSKonstantin Belousov 69486be9f0dSKonstantin Belousov return (dmar_match_pathes(devscope->Bus, path, path_len, dev_busno, 69586be9f0dSKonstantin Belousov dev_path, dev_path_len, devscope->EntryType)); 69686be9f0dSKonstantin Belousov } 69786be9f0dSKonstantin Belousov 69886be9f0dSKonstantin Belousov struct dmar_unit * 69986be9f0dSKonstantin Belousov dmar_find(device_t dev) 70086be9f0dSKonstantin Belousov { 70186be9f0dSKonstantin Belousov device_t dmar_dev; 70286be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh; 70386be9f0dSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope; 70486be9f0dSKonstantin Belousov char *ptr, *ptrend; 70586be9f0dSKonstantin Belousov int i, match, dev_domain, dev_busno, dev_path_len; 70686be9f0dSKonstantin Belousov 70786be9f0dSKonstantin Belousov dmar_dev = NULL; 70886be9f0dSKonstantin Belousov dev_domain = pci_get_domain(dev); 70986be9f0dSKonstantin Belousov dev_path_len = dmar_dev_depth(dev); 71086be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH dev_path[dev_path_len]; 71186be9f0dSKonstantin Belousov dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len); 71286be9f0dSKonstantin Belousov if (dmar_match_verbose) 71386be9f0dSKonstantin Belousov dmar_print_path(dev, "PCI", dev_busno, dev_path_len, dev_path); 71486be9f0dSKonstantin Belousov 71586be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 71686be9f0dSKonstantin Belousov if (dmar_devs[i] == NULL) 71786be9f0dSKonstantin Belousov continue; 71886be9f0dSKonstantin Belousov dmarh = dmar_find_by_index(i); 71986be9f0dSKonstantin Belousov if (dmarh == NULL) 72086be9f0dSKonstantin Belousov continue; 72186be9f0dSKonstantin Belousov if (dmarh->Segment != dev_domain) 72286be9f0dSKonstantin Belousov continue; 72386be9f0dSKonstantin Belousov if ((dmarh->Flags & ACPI_DMAR_INCLUDE_ALL) != 0) { 72486be9f0dSKonstantin Belousov dmar_dev = dmar_devs[i]; 72586be9f0dSKonstantin Belousov if (dmar_match_verbose) { 72686be9f0dSKonstantin Belousov device_printf(dev, 72786be9f0dSKonstantin Belousov "pci%d:%d:%d:%d matched dmar%d INCLUDE_ALL\n", 72886be9f0dSKonstantin Belousov dev_domain, pci_get_bus(dev), 72986be9f0dSKonstantin Belousov pci_get_slot(dev), 73086be9f0dSKonstantin Belousov pci_get_function(dev), 73186be9f0dSKonstantin Belousov ((struct dmar_unit *)device_get_softc( 73286be9f0dSKonstantin Belousov dmar_dev))->unit); 73386be9f0dSKonstantin Belousov } 73486be9f0dSKonstantin Belousov goto found; 73586be9f0dSKonstantin Belousov } 73686be9f0dSKonstantin Belousov ptr = (char *)dmarh + sizeof(*dmarh); 73786be9f0dSKonstantin Belousov ptrend = (char *)dmarh + dmarh->Header.Length; 73886be9f0dSKonstantin Belousov for (;;) { 73986be9f0dSKonstantin Belousov if (ptr >= ptrend) 74086be9f0dSKonstantin Belousov break; 74186be9f0dSKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr; 74286be9f0dSKonstantin Belousov ptr += devscope->Length; 74386be9f0dSKonstantin Belousov if (dmar_match_verbose) { 74486be9f0dSKonstantin Belousov device_printf(dev, 74586be9f0dSKonstantin Belousov "pci%d:%d:%d:%d matching dmar%d\n", 74686be9f0dSKonstantin Belousov dev_domain, pci_get_bus(dev), 74786be9f0dSKonstantin Belousov pci_get_slot(dev), 74886be9f0dSKonstantin Belousov pci_get_function(dev), 74986be9f0dSKonstantin Belousov ((struct dmar_unit *)device_get_softc( 75086be9f0dSKonstantin Belousov dmar_devs[i]))->unit); 75186be9f0dSKonstantin Belousov } 75286be9f0dSKonstantin Belousov match = dmar_match_devscope(devscope, dev, dev_busno, 75386be9f0dSKonstantin Belousov dev_path, dev_path_len); 75486be9f0dSKonstantin Belousov if (dmar_match_verbose) { 75586be9f0dSKonstantin Belousov if (match == -1) 75686be9f0dSKonstantin Belousov printf("table error\n"); 75786be9f0dSKonstantin Belousov else if (match == 0) 75886be9f0dSKonstantin Belousov printf("not matched\n"); 75986be9f0dSKonstantin Belousov else 76086be9f0dSKonstantin Belousov printf("matched\n"); 76186be9f0dSKonstantin Belousov } 76286be9f0dSKonstantin Belousov if (match == -1) 76386be9f0dSKonstantin Belousov return (NULL); 76486be9f0dSKonstantin Belousov else if (match == 1) { 76586be9f0dSKonstantin Belousov dmar_dev = dmar_devs[i]; 76686be9f0dSKonstantin Belousov goto found; 76786be9f0dSKonstantin Belousov } 76886be9f0dSKonstantin Belousov } 76986be9f0dSKonstantin Belousov } 77086be9f0dSKonstantin Belousov return (NULL); 77186be9f0dSKonstantin Belousov found: 77286be9f0dSKonstantin Belousov return (device_get_softc(dmar_dev)); 77386be9f0dSKonstantin Belousov } 77486be9f0dSKonstantin Belousov 7750a110d5bSKonstantin Belousov static struct dmar_unit * 7760a110d5bSKonstantin Belousov dmar_find_nonpci(u_int id, u_int entry_type, uint16_t *rid) 7770a110d5bSKonstantin Belousov { 7780a110d5bSKonstantin Belousov device_t dmar_dev; 7790a110d5bSKonstantin Belousov struct dmar_unit *unit; 7800a110d5bSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh; 7810a110d5bSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope; 7820a110d5bSKonstantin Belousov ACPI_DMAR_PCI_PATH *path; 7830a110d5bSKonstantin Belousov char *ptr, *ptrend; 7840a110d5bSKonstantin Belousov int i; 7850a110d5bSKonstantin Belousov 7860a110d5bSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 7870a110d5bSKonstantin Belousov dmar_dev = dmar_devs[i]; 7880a110d5bSKonstantin Belousov if (dmar_dev == NULL) 7890a110d5bSKonstantin Belousov continue; 7900a110d5bSKonstantin Belousov unit = (struct dmar_unit *)device_get_softc(dmar_dev); 7910a110d5bSKonstantin Belousov dmarh = dmar_find_by_index(i); 7920a110d5bSKonstantin Belousov if (dmarh == NULL) 7930a110d5bSKonstantin Belousov continue; 7940a110d5bSKonstantin Belousov ptr = (char *)dmarh + sizeof(*dmarh); 7950a110d5bSKonstantin Belousov ptrend = (char *)dmarh + dmarh->Header.Length; 7960a110d5bSKonstantin Belousov for (;;) { 7970a110d5bSKonstantin Belousov if (ptr >= ptrend) 7980a110d5bSKonstantin Belousov break; 7990a110d5bSKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr; 8000a110d5bSKonstantin Belousov ptr += devscope->Length; 8010a110d5bSKonstantin Belousov if (devscope->EntryType != entry_type) 8020a110d5bSKonstantin Belousov continue; 8030a110d5bSKonstantin Belousov if (devscope->EnumerationId != id) 8040a110d5bSKonstantin Belousov continue; 8050a110d5bSKonstantin Belousov if (devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE) 8060a110d5bSKonstantin Belousov == 2) { 8070a110d5bSKonstantin Belousov if (rid != NULL) { 8080a110d5bSKonstantin Belousov path = (ACPI_DMAR_PCI_PATH *) 8090a110d5bSKonstantin Belousov (devscope + 1); 8100a110d5bSKonstantin Belousov *rid = PCI_RID(devscope->Bus, 8110a110d5bSKonstantin Belousov path->Device, path->Function); 8120a110d5bSKonstantin Belousov } 8130a110d5bSKonstantin Belousov return (unit); 8140a110d5bSKonstantin Belousov } else { 8150a110d5bSKonstantin Belousov /* XXXKIB */ 8160a110d5bSKonstantin Belousov printf( 8170a110d5bSKonstantin Belousov "dmar_find_nonpci: id %d type %d path length != 2\n", 8180a110d5bSKonstantin Belousov id, entry_type); 8190a110d5bSKonstantin Belousov } 8200a110d5bSKonstantin Belousov } 8210a110d5bSKonstantin Belousov } 8220a110d5bSKonstantin Belousov return (NULL); 8230a110d5bSKonstantin Belousov } 8240a110d5bSKonstantin Belousov 8250a110d5bSKonstantin Belousov 8260a110d5bSKonstantin Belousov struct dmar_unit * 8270a110d5bSKonstantin Belousov dmar_find_hpet(device_t dev, uint16_t *rid) 8280a110d5bSKonstantin Belousov { 8290a110d5bSKonstantin Belousov ACPI_HANDLE handle; 8300a110d5bSKonstantin Belousov uint32_t hpet_id; 8310a110d5bSKonstantin Belousov 8320a110d5bSKonstantin Belousov handle = acpi_get_handle(dev); 8330a110d5bSKonstantin Belousov if (ACPI_FAILURE(acpi_GetInteger(handle, "_UID", &hpet_id))) 8340a110d5bSKonstantin Belousov return (NULL); 8350a110d5bSKonstantin Belousov return (dmar_find_nonpci(hpet_id, ACPI_DMAR_SCOPE_TYPE_HPET, rid)); 8360a110d5bSKonstantin Belousov } 8370a110d5bSKonstantin Belousov 8380a110d5bSKonstantin Belousov struct dmar_unit * 8390a110d5bSKonstantin Belousov dmar_find_ioapic(u_int apic_id, uint16_t *rid) 8400a110d5bSKonstantin Belousov { 8410a110d5bSKonstantin Belousov 8420a110d5bSKonstantin Belousov return (dmar_find_nonpci(apic_id, ACPI_DMAR_SCOPE_TYPE_IOAPIC, rid)); 8430a110d5bSKonstantin Belousov } 8440a110d5bSKonstantin Belousov 84586be9f0dSKonstantin Belousov struct rmrr_iter_args { 846*1abfd355SKonstantin Belousov struct dmar_domain *domain; 84786be9f0dSKonstantin Belousov device_t dev; 84886be9f0dSKonstantin Belousov int dev_domain; 84986be9f0dSKonstantin Belousov int dev_busno; 85086be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH *dev_path; 85186be9f0dSKonstantin Belousov int dev_path_len; 85286be9f0dSKonstantin Belousov struct dmar_map_entries_tailq *rmrr_entries; 85386be9f0dSKonstantin Belousov }; 85486be9f0dSKonstantin Belousov 85586be9f0dSKonstantin Belousov static int 85686be9f0dSKonstantin Belousov dmar_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 85786be9f0dSKonstantin Belousov { 85886be9f0dSKonstantin Belousov struct rmrr_iter_args *ria; 85986be9f0dSKonstantin Belousov ACPI_DMAR_RESERVED_MEMORY *resmem; 86086be9f0dSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope; 86186be9f0dSKonstantin Belousov struct dmar_map_entry *entry; 86286be9f0dSKonstantin Belousov char *ptr, *ptrend; 86386be9f0dSKonstantin Belousov int match; 86486be9f0dSKonstantin Belousov 86586be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY) 86686be9f0dSKonstantin Belousov return (1); 86786be9f0dSKonstantin Belousov 86886be9f0dSKonstantin Belousov ria = arg; 86986be9f0dSKonstantin Belousov resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh; 87086be9f0dSKonstantin Belousov if (dmar_match_verbose) { 87186be9f0dSKonstantin Belousov printf("RMRR [%jx,%jx] segment %d\n", 87286be9f0dSKonstantin Belousov (uintmax_t)resmem->BaseAddress, 87386be9f0dSKonstantin Belousov (uintmax_t)resmem->EndAddress, 87486be9f0dSKonstantin Belousov resmem->Segment); 87586be9f0dSKonstantin Belousov } 87686be9f0dSKonstantin Belousov if (resmem->Segment != ria->dev_domain) 87786be9f0dSKonstantin Belousov return (1); 87886be9f0dSKonstantin Belousov 87986be9f0dSKonstantin Belousov ptr = (char *)resmem + sizeof(*resmem); 88086be9f0dSKonstantin Belousov ptrend = (char *)resmem + resmem->Header.Length; 88186be9f0dSKonstantin Belousov for (;;) { 88286be9f0dSKonstantin Belousov if (ptr >= ptrend) 88386be9f0dSKonstantin Belousov break; 88486be9f0dSKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr; 88586be9f0dSKonstantin Belousov ptr += devscope->Length; 88686be9f0dSKonstantin Belousov match = dmar_match_devscope(devscope, ria->dev, ria->dev_busno, 88786be9f0dSKonstantin Belousov ria->dev_path, ria->dev_path_len); 88886be9f0dSKonstantin Belousov if (match == 1) { 88986be9f0dSKonstantin Belousov if (dmar_match_verbose) 89086be9f0dSKonstantin Belousov printf("matched\n"); 891*1abfd355SKonstantin Belousov entry = dmar_gas_alloc_entry(ria->domain, 892*1abfd355SKonstantin Belousov DMAR_PGF_WAITOK); 89386be9f0dSKonstantin Belousov entry->start = resmem->BaseAddress; 89486be9f0dSKonstantin Belousov /* The RMRR entry end address is inclusive. */ 89586be9f0dSKonstantin Belousov entry->end = resmem->EndAddress; 89686be9f0dSKonstantin Belousov TAILQ_INSERT_TAIL(ria->rmrr_entries, entry, 89786be9f0dSKonstantin Belousov unroll_link); 89886be9f0dSKonstantin Belousov } else if (dmar_match_verbose) { 89986be9f0dSKonstantin Belousov printf("not matched, err %d\n", match); 90086be9f0dSKonstantin Belousov } 90186be9f0dSKonstantin Belousov } 90286be9f0dSKonstantin Belousov 90386be9f0dSKonstantin Belousov return (1); 90486be9f0dSKonstantin Belousov } 90586be9f0dSKonstantin Belousov 90686be9f0dSKonstantin Belousov void 907*1abfd355SKonstantin Belousov dmar_dev_parse_rmrr(struct dmar_domain *domain, device_t dev, 90886be9f0dSKonstantin Belousov struct dmar_map_entries_tailq *rmrr_entries) 90986be9f0dSKonstantin Belousov { 91086be9f0dSKonstantin Belousov struct rmrr_iter_args ria; 91186be9f0dSKonstantin Belousov 91286be9f0dSKonstantin Belousov ria.dev_domain = pci_get_domain(dev); 91386be9f0dSKonstantin Belousov ria.dev_path_len = dmar_dev_depth(dev); 91486be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH dev_path[ria.dev_path_len]; 91586be9f0dSKonstantin Belousov dmar_dev_path(dev, &ria.dev_busno, dev_path, ria.dev_path_len); 91686be9f0dSKonstantin Belousov 91786be9f0dSKonstantin Belousov if (dmar_match_verbose) { 91886be9f0dSKonstantin Belousov device_printf(dev, "parsing RMRR entries for "); 91986be9f0dSKonstantin Belousov dmar_print_path(dev, "PCI", ria.dev_busno, ria.dev_path_len, 92086be9f0dSKonstantin Belousov dev_path); 92186be9f0dSKonstantin Belousov } 92286be9f0dSKonstantin Belousov 923*1abfd355SKonstantin Belousov ria.domain = domain; 92486be9f0dSKonstantin Belousov ria.dev = dev; 92586be9f0dSKonstantin Belousov ria.dev_path = dev_path; 92686be9f0dSKonstantin Belousov ria.rmrr_entries = rmrr_entries; 92786be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_rmrr_iter, &ria); 92886be9f0dSKonstantin Belousov } 92986be9f0dSKonstantin Belousov 93086be9f0dSKonstantin Belousov struct inst_rmrr_iter_args { 93186be9f0dSKonstantin Belousov struct dmar_unit *dmar; 93286be9f0dSKonstantin Belousov }; 93386be9f0dSKonstantin Belousov 93486be9f0dSKonstantin Belousov static device_t 93586be9f0dSKonstantin Belousov dmar_path_dev(int segment, int path_len, int busno, 93686be9f0dSKonstantin Belousov const ACPI_DMAR_PCI_PATH *path) 93786be9f0dSKonstantin Belousov { 93886be9f0dSKonstantin Belousov devclass_t pci_class; 93986be9f0dSKonstantin Belousov device_t bus, pcib, dev; 94086be9f0dSKonstantin Belousov int i; 94186be9f0dSKonstantin Belousov 94286be9f0dSKonstantin Belousov pci_class = devclass_find("pci"); 94386be9f0dSKonstantin Belousov dev = NULL; 94486be9f0dSKonstantin Belousov for (i = 0; i < path_len; i++, path++) { 94586be9f0dSKonstantin Belousov dev = pci_find_dbsf(segment, busno, path->Device, 94686be9f0dSKonstantin Belousov path->Function); 94786be9f0dSKonstantin Belousov if (dev == NULL) 94886be9f0dSKonstantin Belousov break; 94986be9f0dSKonstantin Belousov if (i != path_len - 1) { 95086be9f0dSKonstantin Belousov bus = device_get_parent(dev); 95186be9f0dSKonstantin Belousov pcib = device_get_parent(bus); 95286be9f0dSKonstantin Belousov if (device_get_devclass(device_get_parent(pcib)) != 95386be9f0dSKonstantin Belousov pci_class) 95486be9f0dSKonstantin Belousov return (NULL); 95586be9f0dSKonstantin Belousov } 95686be9f0dSKonstantin Belousov busno = pcib_get_bus(dev); 95786be9f0dSKonstantin Belousov } 95886be9f0dSKonstantin Belousov return (dev); 95986be9f0dSKonstantin Belousov } 96086be9f0dSKonstantin Belousov 96186be9f0dSKonstantin Belousov static int 96286be9f0dSKonstantin Belousov dmar_inst_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg) 96386be9f0dSKonstantin Belousov { 96486be9f0dSKonstantin Belousov const ACPI_DMAR_RESERVED_MEMORY *resmem; 96586be9f0dSKonstantin Belousov const ACPI_DMAR_DEVICE_SCOPE *devscope; 96686be9f0dSKonstantin Belousov struct inst_rmrr_iter_args *iria; 96786be9f0dSKonstantin Belousov const char *ptr, *ptrend; 96886be9f0dSKonstantin Belousov struct dmar_unit *dev_dmar; 96986be9f0dSKonstantin Belousov device_t dev; 97086be9f0dSKonstantin Belousov 97186be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY) 97286be9f0dSKonstantin Belousov return (1); 97386be9f0dSKonstantin Belousov 97486be9f0dSKonstantin Belousov iria = arg; 97586be9f0dSKonstantin Belousov resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh; 97686be9f0dSKonstantin Belousov if (resmem->Segment != iria->dmar->segment) 97786be9f0dSKonstantin Belousov return (1); 97886be9f0dSKonstantin Belousov if (dmar_match_verbose) { 97986be9f0dSKonstantin Belousov printf("dmar%d: RMRR [%jx,%jx]\n", iria->dmar->unit, 98086be9f0dSKonstantin Belousov (uintmax_t)resmem->BaseAddress, 98186be9f0dSKonstantin Belousov (uintmax_t)resmem->EndAddress); 98286be9f0dSKonstantin Belousov } 98386be9f0dSKonstantin Belousov 98433552193SDimitry Andric ptr = (const char *)resmem + sizeof(*resmem); 98533552193SDimitry Andric ptrend = (const char *)resmem + resmem->Header.Length; 98686be9f0dSKonstantin Belousov for (;;) { 98786be9f0dSKonstantin Belousov if (ptr >= ptrend) 98886be9f0dSKonstantin Belousov break; 98933552193SDimitry Andric devscope = (const ACPI_DMAR_DEVICE_SCOPE *)ptr; 99086be9f0dSKonstantin Belousov ptr += devscope->Length; 99186be9f0dSKonstantin Belousov /* XXXKIB bridge */ 99286be9f0dSKonstantin Belousov if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT) 99386be9f0dSKonstantin Belousov continue; 99486be9f0dSKonstantin Belousov if (dmar_match_verbose) { 99586be9f0dSKonstantin Belousov dmar_print_path(iria->dmar->dev, "RMRR scope", 99686be9f0dSKonstantin Belousov devscope->Bus, (devscope->Length - 99786be9f0dSKonstantin Belousov sizeof(ACPI_DMAR_DEVICE_SCOPE)) / 2, 99833552193SDimitry Andric (const ACPI_DMAR_PCI_PATH *)(devscope + 1)); 99986be9f0dSKonstantin Belousov } 100086be9f0dSKonstantin Belousov dev = dmar_path_dev(resmem->Segment, (devscope->Length - 100186be9f0dSKonstantin Belousov sizeof(ACPI_DMAR_DEVICE_SCOPE)) / 2, devscope->Bus, 100233552193SDimitry Andric (const ACPI_DMAR_PCI_PATH *)(devscope + 1)); 100386be9f0dSKonstantin Belousov if (dev == NULL) { 100486be9f0dSKonstantin Belousov if (dmar_match_verbose) 100586be9f0dSKonstantin Belousov printf("null dev\n"); 100686be9f0dSKonstantin Belousov continue; 100786be9f0dSKonstantin Belousov } 100886be9f0dSKonstantin Belousov dev_dmar = dmar_find(dev); 100986be9f0dSKonstantin Belousov if (dev_dmar != iria->dmar) { 101086be9f0dSKonstantin Belousov if (dmar_match_verbose) { 101186be9f0dSKonstantin Belousov printf("dmar%d matched, skipping\n", 101286be9f0dSKonstantin Belousov dev_dmar->unit); 101386be9f0dSKonstantin Belousov } 101486be9f0dSKonstantin Belousov continue; 101586be9f0dSKonstantin Belousov } 101686be9f0dSKonstantin Belousov if (dmar_match_verbose) 101786be9f0dSKonstantin Belousov printf("matched, instantiating RMRR context\n"); 101886be9f0dSKonstantin Belousov dmar_instantiate_ctx(iria->dmar, dev, true); 101986be9f0dSKonstantin Belousov } 102086be9f0dSKonstantin Belousov 102186be9f0dSKonstantin Belousov return (1); 102286be9f0dSKonstantin Belousov 102386be9f0dSKonstantin Belousov } 102486be9f0dSKonstantin Belousov 102586be9f0dSKonstantin Belousov /* 102686be9f0dSKonstantin Belousov * Pre-create all contexts for the DMAR which have RMRR entries. 102786be9f0dSKonstantin Belousov */ 102886be9f0dSKonstantin Belousov int 102986be9f0dSKonstantin Belousov dmar_instantiate_rmrr_ctxs(struct dmar_unit *dmar) 103086be9f0dSKonstantin Belousov { 103186be9f0dSKonstantin Belousov struct inst_rmrr_iter_args iria; 103286be9f0dSKonstantin Belousov int error; 103386be9f0dSKonstantin Belousov 103486be9f0dSKonstantin Belousov if (!dmar_barrier_enter(dmar, DMAR_BARRIER_RMRR)) 103586be9f0dSKonstantin Belousov return (0); 103686be9f0dSKonstantin Belousov 103786be9f0dSKonstantin Belousov error = 0; 103886be9f0dSKonstantin Belousov iria.dmar = dmar; 103986be9f0dSKonstantin Belousov if (dmar_match_verbose) 104086be9f0dSKonstantin Belousov printf("dmar%d: instantiating RMRR contexts\n", dmar->unit); 104186be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_inst_rmrr_iter, &iria); 104286be9f0dSKonstantin Belousov DMAR_LOCK(dmar); 1043*1abfd355SKonstantin Belousov if (!LIST_EMPTY(&dmar->domains)) { 104486be9f0dSKonstantin Belousov KASSERT((dmar->hw_gcmd & DMAR_GCMD_TE) == 0, 104586be9f0dSKonstantin Belousov ("dmar%d: RMRR not handled but translation is already enabled", 104686be9f0dSKonstantin Belousov dmar->unit)); 104786be9f0dSKonstantin Belousov error = dmar_enable_translation(dmar); 104886be9f0dSKonstantin Belousov } 104986be9f0dSKonstantin Belousov dmar_barrier_exit(dmar, DMAR_BARRIER_RMRR); 105086be9f0dSKonstantin Belousov return (error); 105186be9f0dSKonstantin Belousov } 105286be9f0dSKonstantin Belousov 105386be9f0dSKonstantin Belousov #ifdef DDB 105486be9f0dSKonstantin Belousov #include <ddb/ddb.h> 105586be9f0dSKonstantin Belousov #include <ddb/db_lex.h> 105686be9f0dSKonstantin Belousov 105786be9f0dSKonstantin Belousov static void 1058*1abfd355SKonstantin Belousov dmar_print_domain_entry(const struct dmar_map_entry *entry) 105986be9f0dSKonstantin Belousov { 106086be9f0dSKonstantin Belousov struct dmar_map_entry *l, *r; 106186be9f0dSKonstantin Belousov 106286be9f0dSKonstantin Belousov db_printf( 106386be9f0dSKonstantin Belousov " start %jx end %jx free_after %jx free_down %jx flags %x ", 106486be9f0dSKonstantin Belousov entry->start, entry->end, entry->free_after, entry->free_down, 106586be9f0dSKonstantin Belousov entry->flags); 106686be9f0dSKonstantin Belousov db_printf("left "); 106786be9f0dSKonstantin Belousov l = RB_LEFT(entry, rb_entry); 106886be9f0dSKonstantin Belousov if (l == NULL) 106986be9f0dSKonstantin Belousov db_printf("NULL "); 107086be9f0dSKonstantin Belousov else 107186be9f0dSKonstantin Belousov db_printf("%jx ", l->start); 107286be9f0dSKonstantin Belousov db_printf("right "); 107386be9f0dSKonstantin Belousov r = RB_RIGHT(entry, rb_entry); 107486be9f0dSKonstantin Belousov if (r == NULL) 107586be9f0dSKonstantin Belousov db_printf("NULL"); 107686be9f0dSKonstantin Belousov else 107786be9f0dSKonstantin Belousov db_printf("%jx", r->start); 107886be9f0dSKonstantin Belousov db_printf("\n"); 107986be9f0dSKonstantin Belousov } 108086be9f0dSKonstantin Belousov 108186be9f0dSKonstantin Belousov static void 1082*1abfd355SKonstantin Belousov dmar_print_ctx(struct dmar_ctx *ctx) 108386be9f0dSKonstantin Belousov { 108486be9f0dSKonstantin Belousov 108586be9f0dSKonstantin Belousov db_printf( 1086*1abfd355SKonstantin Belousov " @%p pci%d:%d:%d refs %d flags %x loads %lu unloads %lu\n", 108767499354SRyan Stone ctx, pci_get_bus(ctx->ctx_tag.owner), 108867499354SRyan Stone pci_get_slot(ctx->ctx_tag.owner), 1089*1abfd355SKonstantin Belousov pci_get_function(ctx->ctx_tag.owner), ctx->refs, ctx->flags, 1090*1abfd355SKonstantin Belousov ctx->loads, ctx->unloads); 1091*1abfd355SKonstantin Belousov } 1092*1abfd355SKonstantin Belousov 1093*1abfd355SKonstantin Belousov static void 1094*1abfd355SKonstantin Belousov dmar_print_domain(struct dmar_domain *domain, bool show_mappings) 1095*1abfd355SKonstantin Belousov { 1096*1abfd355SKonstantin Belousov struct dmar_map_entry *entry; 1097*1abfd355SKonstantin Belousov struct dmar_ctx *ctx; 1098*1abfd355SKonstantin Belousov 1099*1abfd355SKonstantin Belousov db_printf( 1100*1abfd355SKonstantin Belousov " @%p dom %d mgaw %d agaw %d pglvl %d end %jx refs %d\n" 1101*1abfd355SKonstantin Belousov " ctx_cnt %d flags %x pgobj %p map_ents %u\n", 1102*1abfd355SKonstantin Belousov domain, domain->domain, domain->mgaw, domain->agaw, domain->pglvl, 1103*1abfd355SKonstantin Belousov (uintmax_t)domain->end, domain->refs, domain->ctx_cnt, 1104*1abfd355SKonstantin Belousov domain->flags, domain->pgtbl_obj, domain->entries_cnt); 1105*1abfd355SKonstantin Belousov if (!LIST_EMPTY(&domain->contexts)) { 1106*1abfd355SKonstantin Belousov db_printf(" Contexts:\n"); 1107*1abfd355SKonstantin Belousov LIST_FOREACH(ctx, &domain->contexts, link) 1108*1abfd355SKonstantin Belousov dmar_print_ctx(ctx); 1109*1abfd355SKonstantin Belousov } 111086be9f0dSKonstantin Belousov if (!show_mappings) 111186be9f0dSKonstantin Belousov return; 111286be9f0dSKonstantin Belousov db_printf(" mapped:\n"); 1113*1abfd355SKonstantin Belousov RB_FOREACH(entry, dmar_gas_entries_tree, &domain->rb_root) { 1114*1abfd355SKonstantin Belousov dmar_print_domain_entry(entry); 111586be9f0dSKonstantin Belousov if (db_pager_quit) 111686be9f0dSKonstantin Belousov break; 111786be9f0dSKonstantin Belousov } 111886be9f0dSKonstantin Belousov if (db_pager_quit) 111986be9f0dSKonstantin Belousov return; 112086be9f0dSKonstantin Belousov db_printf(" unloading:\n"); 1121*1abfd355SKonstantin Belousov TAILQ_FOREACH(entry, &domain->unload_entries, dmamap_link) { 1122*1abfd355SKonstantin Belousov dmar_print_domain_entry(entry); 112386be9f0dSKonstantin Belousov if (db_pager_quit) 112486be9f0dSKonstantin Belousov break; 112586be9f0dSKonstantin Belousov } 112686be9f0dSKonstantin Belousov } 112786be9f0dSKonstantin Belousov 1128*1abfd355SKonstantin Belousov DB_FUNC(dmar_domain, db_dmar_print_domain, db_show_table, CS_OWN, NULL) 112986be9f0dSKonstantin Belousov { 113086be9f0dSKonstantin Belousov struct dmar_unit *unit; 1131*1abfd355SKonstantin Belousov struct dmar_domain *domain; 113286be9f0dSKonstantin Belousov struct dmar_ctx *ctx; 113386be9f0dSKonstantin Belousov bool show_mappings, valid; 1134*1abfd355SKonstantin Belousov int pci_domain, bus, device, function, i, t; 113586be9f0dSKonstantin Belousov db_expr_t radix; 113686be9f0dSKonstantin Belousov 113786be9f0dSKonstantin Belousov valid = false; 113886be9f0dSKonstantin Belousov radix = db_radix; 113986be9f0dSKonstantin Belousov db_radix = 10; 114086be9f0dSKonstantin Belousov t = db_read_token(); 114186be9f0dSKonstantin Belousov if (t == tSLASH) { 114286be9f0dSKonstantin Belousov t = db_read_token(); 114386be9f0dSKonstantin Belousov if (t != tIDENT) { 114486be9f0dSKonstantin Belousov db_printf("Bad modifier\n"); 114586be9f0dSKonstantin Belousov db_radix = radix; 114686be9f0dSKonstantin Belousov db_skip_to_eol(); 114786be9f0dSKonstantin Belousov return; 114886be9f0dSKonstantin Belousov } 114986be9f0dSKonstantin Belousov show_mappings = strchr(db_tok_string, 'm') != NULL; 115086be9f0dSKonstantin Belousov t = db_read_token(); 1151f7f5706fSDimitry Andric } else { 1152f7f5706fSDimitry Andric show_mappings = false; 115386be9f0dSKonstantin Belousov } 115486be9f0dSKonstantin Belousov if (t == tNUMBER) { 1155*1abfd355SKonstantin Belousov pci_domain = db_tok_number; 115686be9f0dSKonstantin Belousov t = db_read_token(); 115786be9f0dSKonstantin Belousov if (t == tNUMBER) { 115886be9f0dSKonstantin Belousov bus = db_tok_number; 115986be9f0dSKonstantin Belousov t = db_read_token(); 116086be9f0dSKonstantin Belousov if (t == tNUMBER) { 116186be9f0dSKonstantin Belousov device = db_tok_number; 116286be9f0dSKonstantin Belousov t = db_read_token(); 116386be9f0dSKonstantin Belousov if (t == tNUMBER) { 116486be9f0dSKonstantin Belousov function = db_tok_number; 116586be9f0dSKonstantin Belousov valid = true; 116686be9f0dSKonstantin Belousov } 116786be9f0dSKonstantin Belousov } 116886be9f0dSKonstantin Belousov } 116986be9f0dSKonstantin Belousov } 117086be9f0dSKonstantin Belousov db_radix = radix; 117186be9f0dSKonstantin Belousov db_skip_to_eol(); 117286be9f0dSKonstantin Belousov if (!valid) { 1173*1abfd355SKonstantin Belousov db_printf("usage: show dmar_domain [/m] " 117486be9f0dSKonstantin Belousov "<domain> <bus> <device> <func>\n"); 117586be9f0dSKonstantin Belousov return; 117686be9f0dSKonstantin Belousov } 117786be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 117886be9f0dSKonstantin Belousov unit = device_get_softc(dmar_devs[i]); 1179*1abfd355SKonstantin Belousov LIST_FOREACH(domain, &unit->domains, link) { 1180*1abfd355SKonstantin Belousov LIST_FOREACH(ctx, &domain->contexts, link) { 1181*1abfd355SKonstantin Belousov if (pci_domain == unit->segment && 118267499354SRyan Stone bus == pci_get_bus(ctx->ctx_tag.owner) && 1183*1abfd355SKonstantin Belousov device == 1184*1abfd355SKonstantin Belousov pci_get_slot(ctx->ctx_tag.owner) && 1185*1abfd355SKonstantin Belousov function == 1186*1abfd355SKonstantin Belousov pci_get_function(ctx->ctx_tag.owner)) { 1187*1abfd355SKonstantin Belousov dmar_print_domain(domain, 1188*1abfd355SKonstantin Belousov show_mappings); 118986be9f0dSKonstantin Belousov goto out; 119086be9f0dSKonstantin Belousov } 119186be9f0dSKonstantin Belousov } 119286be9f0dSKonstantin Belousov } 1193*1abfd355SKonstantin Belousov } 119486be9f0dSKonstantin Belousov out:; 119586be9f0dSKonstantin Belousov } 119686be9f0dSKonstantin Belousov 119786be9f0dSKonstantin Belousov static void 1198*1abfd355SKonstantin Belousov dmar_print_one(int idx, bool show_domains, bool show_mappings) 119986be9f0dSKonstantin Belousov { 120086be9f0dSKonstantin Belousov struct dmar_unit *unit; 1201*1abfd355SKonstantin Belousov struct dmar_domain *domain; 120286be9f0dSKonstantin Belousov int i, frir; 120386be9f0dSKonstantin Belousov 120486be9f0dSKonstantin Belousov unit = device_get_softc(dmar_devs[idx]); 120586be9f0dSKonstantin Belousov db_printf("dmar%d at %p, root at 0x%jx, ver 0x%x\n", unit->unit, unit, 120686be9f0dSKonstantin Belousov dmar_read8(unit, DMAR_RTADDR_REG), dmar_read4(unit, DMAR_VER_REG)); 120786be9f0dSKonstantin Belousov db_printf("cap 0x%jx ecap 0x%jx gsts 0x%x fsts 0x%x fectl 0x%x\n", 120886be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_CAP_REG), 120986be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_ECAP_REG), 121086be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_GSTS_REG), 121186be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FSTS_REG), 121286be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FECTL_REG)); 1213*1abfd355SKonstantin Belousov if (unit->ir_enabled) { 1214*1abfd355SKonstantin Belousov db_printf("ir is enabled; IRT @%p phys 0x%jx maxcnt %d\n", 1215*1abfd355SKonstantin Belousov unit->irt, (uintmax_t)unit->irt_phys, unit->irte_cnt); 1216*1abfd355SKonstantin Belousov } 121786be9f0dSKonstantin Belousov db_printf("fed 0x%x fea 0x%x feua 0x%x\n", 121886be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEDATA_REG), 121986be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEADDR_REG), 122086be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEUADDR_REG)); 122186be9f0dSKonstantin Belousov db_printf("primary fault log:\n"); 122286be9f0dSKonstantin Belousov for (i = 0; i < DMAR_CAP_NFR(unit->hw_cap); i++) { 122386be9f0dSKonstantin Belousov frir = (DMAR_CAP_FRO(unit->hw_cap) + i) * 16; 122486be9f0dSKonstantin Belousov db_printf(" %d at 0x%x: %jx %jx\n", i, frir, 122586be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, frir), 122686be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, frir + 8)); 122786be9f0dSKonstantin Belousov } 122868eeb96aSKonstantin Belousov if (DMAR_HAS_QI(unit)) { 122968eeb96aSKonstantin Belousov db_printf("ied 0x%x iea 0x%x ieua 0x%x\n", 123068eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEDATA_REG), 123168eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEADDR_REG), 123268eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEUADDR_REG)); 123368eeb96aSKonstantin Belousov if (unit->qi_enabled) { 123468eeb96aSKonstantin Belousov db_printf("qi is enabled: queue @0x%jx (IQA 0x%jx) " 123568eeb96aSKonstantin Belousov "size 0x%jx\n" 123668eeb96aSKonstantin Belousov " head 0x%x tail 0x%x avail 0x%x status 0x%x ctrl 0x%x\n" 123768eeb96aSKonstantin Belousov " hw compl 0x%x@%p/phys@%jx next seq 0x%x gen 0x%x\n", 123868eeb96aSKonstantin Belousov (uintmax_t)unit->inv_queue, 123968eeb96aSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_IQA_REG), 124068eeb96aSKonstantin Belousov (uintmax_t)unit->inv_queue_size, 124168eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IQH_REG), 124268eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IQT_REG), 124368eeb96aSKonstantin Belousov unit->inv_queue_avail, 124468eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_ICS_REG), 124568eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IECTL_REG), 124668eeb96aSKonstantin Belousov unit->inv_waitd_seq_hw, 124768eeb96aSKonstantin Belousov &unit->inv_waitd_seq_hw, 124868eeb96aSKonstantin Belousov (uintmax_t)unit->inv_waitd_seq_hw_phys, 124968eeb96aSKonstantin Belousov unit->inv_waitd_seq, 125068eeb96aSKonstantin Belousov unit->inv_waitd_gen); 125168eeb96aSKonstantin Belousov } else { 125268eeb96aSKonstantin Belousov db_printf("qi is disabled\n"); 125368eeb96aSKonstantin Belousov } 125468eeb96aSKonstantin Belousov } 1255*1abfd355SKonstantin Belousov if (show_domains) { 1256*1abfd355SKonstantin Belousov db_printf("domains:\n"); 1257*1abfd355SKonstantin Belousov LIST_FOREACH(domain, &unit->domains, link) { 1258*1abfd355SKonstantin Belousov dmar_print_domain(domain, show_mappings); 125986be9f0dSKonstantin Belousov if (db_pager_quit) 126086be9f0dSKonstantin Belousov break; 126186be9f0dSKonstantin Belousov } 126286be9f0dSKonstantin Belousov } 126386be9f0dSKonstantin Belousov } 126486be9f0dSKonstantin Belousov 126586be9f0dSKonstantin Belousov DB_SHOW_COMMAND(dmar, db_dmar_print) 126686be9f0dSKonstantin Belousov { 1267*1abfd355SKonstantin Belousov bool show_domains, show_mappings; 126886be9f0dSKonstantin Belousov 1269*1abfd355SKonstantin Belousov show_domains = strchr(modif, 'd') != NULL; 127086be9f0dSKonstantin Belousov show_mappings = strchr(modif, 'm') != NULL; 127186be9f0dSKonstantin Belousov if (!have_addr) { 1272*1abfd355SKonstantin Belousov db_printf("usage: show dmar [/d] [/m] index\n"); 127386be9f0dSKonstantin Belousov return; 127486be9f0dSKonstantin Belousov } 1275*1abfd355SKonstantin Belousov dmar_print_one((int)addr, show_domains, show_mappings); 127686be9f0dSKonstantin Belousov } 127786be9f0dSKonstantin Belousov 127886be9f0dSKonstantin Belousov DB_SHOW_ALL_COMMAND(dmars, db_show_all_dmars) 127986be9f0dSKonstantin Belousov { 128086be9f0dSKonstantin Belousov int i; 1281*1abfd355SKonstantin Belousov bool show_domains, show_mappings; 128286be9f0dSKonstantin Belousov 1283*1abfd355SKonstantin Belousov show_domains = strchr(modif, 'd') != NULL; 128486be9f0dSKonstantin Belousov show_mappings = strchr(modif, 'm') != NULL; 128586be9f0dSKonstantin Belousov 128686be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) { 1287*1abfd355SKonstantin Belousov dmar_print_one(i, show_domains, show_mappings); 128886be9f0dSKonstantin Belousov if (db_pager_quit) 128986be9f0dSKonstantin Belousov break; 129086be9f0dSKonstantin Belousov } 129186be9f0dSKonstantin Belousov } 129286be9f0dSKonstantin Belousov #endif 1293