186be9f0dSKonstantin Belousov /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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 "opt_acpi.h"
32e7d939bdSMarcel Moolenaar #if defined(__amd64__)
3386be9f0dSKonstantin Belousov #define DEV_APIC
3486be9f0dSKonstantin Belousov #else
3586be9f0dSKonstantin Belousov #include "opt_apic.h"
3686be9f0dSKonstantin Belousov #endif
3786be9f0dSKonstantin Belousov #include "opt_ddb.h"
3886be9f0dSKonstantin Belousov
3986be9f0dSKonstantin Belousov #include <sys/param.h>
4086be9f0dSKonstantin Belousov #include <sys/bus.h>
41705090cbSKonstantin Belousov #include <sys/domainset.h>
4286be9f0dSKonstantin Belousov #include <sys/kernel.h>
4386be9f0dSKonstantin Belousov #include <sys/lock.h>
4486be9f0dSKonstantin Belousov #include <sys/malloc.h>
4586be9f0dSKonstantin Belousov #include <sys/memdesc.h>
4686be9f0dSKonstantin Belousov #include <sys/module.h>
47e2e050c8SConrad Meyer #include <sys/mutex.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 <vm/vm.h>
5586be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
5686be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
5786be9f0dSKonstantin Belousov #include <vm/vm_object.h>
5886be9f0dSKonstantin Belousov #include <vm/vm_page.h>
5986be9f0dSKonstantin Belousov #include <vm/vm_pager.h>
6086be9f0dSKonstantin Belousov #include <vm/vm_map.h>
61c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/acpi.h>
62c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/accommon.h>
63c8597a1fSRuslan Bukin #include <dev/acpica/acpivar.h>
640a110d5bSKonstantin Belousov #include <dev/pci/pcireg.h>
6586be9f0dSKonstantin Belousov #include <dev/pci/pcivar.h>
66c8597a1fSRuslan Bukin #include <machine/bus.h>
67c8597a1fSRuslan Bukin #include <machine/pci_cfgreg.h>
6865b133e5SKonstantin Belousov #include <machine/md_var.h>
6965b133e5SKonstantin Belousov #include <machine/cputypes.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>
7340d951bcSKonstantin Belousov #include <x86/iommu/x86_iommu.h>
74685666aaSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
7586be9f0dSKonstantin Belousov
7686be9f0dSKonstantin Belousov #ifdef DEV_APIC
7786be9f0dSKonstantin Belousov #include "pcib_if.h"
78fd15fee1SKonstantin Belousov #include <machine/intr_machdep.h>
79fd15fee1SKonstantin Belousov #include <x86/apicreg.h>
80fd15fee1SKonstantin Belousov #include <x86/apicvar.h>
8186be9f0dSKonstantin Belousov #endif
8286be9f0dSKonstantin Belousov
8368eeb96aSKonstantin Belousov #define DMAR_FAULT_IRQ_RID 0
8468eeb96aSKonstantin Belousov #define DMAR_QI_IRQ_RID 1
8568eeb96aSKonstantin Belousov #define DMAR_REG_RID 2
8686be9f0dSKonstantin Belousov
8786be9f0dSKonstantin Belousov static device_t *dmar_devs;
8886be9f0dSKonstantin Belousov static int dmar_devcnt;
89*512bb0daSKonstantin Belousov static bool dmar_running = false;
9086be9f0dSKonstantin Belousov
9186be9f0dSKonstantin Belousov typedef int (*dmar_iter_t)(ACPI_DMAR_HEADER *, void *);
9286be9f0dSKonstantin Belousov
9386be9f0dSKonstantin Belousov static void
dmar_iterate_tbl(dmar_iter_t iter,void * arg)9486be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_iter_t iter, void *arg)
9586be9f0dSKonstantin Belousov {
9686be9f0dSKonstantin Belousov ACPI_TABLE_DMAR *dmartbl;
9786be9f0dSKonstantin Belousov ACPI_DMAR_HEADER *dmarh;
9886be9f0dSKonstantin Belousov char *ptr, *ptrend;
9986be9f0dSKonstantin Belousov ACPI_STATUS status;
10086be9f0dSKonstantin Belousov
10186be9f0dSKonstantin Belousov status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl);
10286be9f0dSKonstantin Belousov if (ACPI_FAILURE(status))
10386be9f0dSKonstantin Belousov return;
10486be9f0dSKonstantin Belousov ptr = (char *)dmartbl + sizeof(*dmartbl);
10586be9f0dSKonstantin Belousov ptrend = (char *)dmartbl + dmartbl->Header.Length;
10686be9f0dSKonstantin Belousov for (;;) {
10786be9f0dSKonstantin Belousov if (ptr >= ptrend)
10886be9f0dSKonstantin Belousov break;
10986be9f0dSKonstantin Belousov dmarh = (ACPI_DMAR_HEADER *)ptr;
11086be9f0dSKonstantin Belousov if (dmarh->Length <= 0) {
11186be9f0dSKonstantin Belousov printf("dmar_identify: corrupted DMAR table, l %d\n",
11286be9f0dSKonstantin Belousov dmarh->Length);
11386be9f0dSKonstantin Belousov break;
11486be9f0dSKonstantin Belousov }
11586be9f0dSKonstantin Belousov ptr += dmarh->Length;
11686be9f0dSKonstantin Belousov if (!iter(dmarh, arg))
11786be9f0dSKonstantin Belousov break;
11886be9f0dSKonstantin Belousov }
1193dd3c450SKonstantin Belousov AcpiPutTable((ACPI_TABLE_HEADER *)dmartbl);
12086be9f0dSKonstantin Belousov }
12186be9f0dSKonstantin Belousov
12286be9f0dSKonstantin Belousov struct find_iter_args {
12386be9f0dSKonstantin Belousov int i;
12486be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *res;
12586be9f0dSKonstantin Belousov };
12686be9f0dSKonstantin Belousov
12786be9f0dSKonstantin Belousov static int
dmar_find_iter(ACPI_DMAR_HEADER * dmarh,void * arg)12886be9f0dSKonstantin Belousov dmar_find_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
12986be9f0dSKonstantin Belousov {
13086be9f0dSKonstantin Belousov struct find_iter_args *fia;
13186be9f0dSKonstantin Belousov
13286be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_HARDWARE_UNIT)
13386be9f0dSKonstantin Belousov return (1);
13486be9f0dSKonstantin Belousov
13586be9f0dSKonstantin Belousov fia = arg;
13686be9f0dSKonstantin Belousov if (fia->i == 0) {
13786be9f0dSKonstantin Belousov fia->res = (ACPI_DMAR_HARDWARE_UNIT *)dmarh;
13886be9f0dSKonstantin Belousov return (0);
13986be9f0dSKonstantin Belousov }
14086be9f0dSKonstantin Belousov fia->i--;
14186be9f0dSKonstantin Belousov return (1);
14286be9f0dSKonstantin Belousov }
14386be9f0dSKonstantin Belousov
14486be9f0dSKonstantin Belousov static ACPI_DMAR_HARDWARE_UNIT *
dmar_find_by_index(int idx)14586be9f0dSKonstantin Belousov dmar_find_by_index(int idx)
14686be9f0dSKonstantin Belousov {
14786be9f0dSKonstantin Belousov struct find_iter_args fia;
14886be9f0dSKonstantin Belousov
14986be9f0dSKonstantin Belousov fia.i = idx;
15086be9f0dSKonstantin Belousov fia.res = NULL;
15186be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_find_iter, &fia);
15286be9f0dSKonstantin Belousov return (fia.res);
15386be9f0dSKonstantin Belousov }
15486be9f0dSKonstantin Belousov
15586be9f0dSKonstantin Belousov static int
dmar_count_iter(ACPI_DMAR_HEADER * dmarh,void * arg)15686be9f0dSKonstantin Belousov dmar_count_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
15786be9f0dSKonstantin Belousov {
15886be9f0dSKonstantin Belousov
15986be9f0dSKonstantin Belousov if (dmarh->Type == ACPI_DMAR_TYPE_HARDWARE_UNIT)
16086be9f0dSKonstantin Belousov dmar_devcnt++;
16186be9f0dSKonstantin Belousov return (1);
16286be9f0dSKonstantin Belousov }
16386be9f0dSKonstantin Belousov
1643f0289eaSKonstantin Belousov /* Remapping Hardware Static Affinity Structure lookup */
1653f0289eaSKonstantin Belousov struct rhsa_iter_arg {
1663f0289eaSKonstantin Belousov uint64_t base;
1673f0289eaSKonstantin Belousov u_int proxim_dom;
1683f0289eaSKonstantin Belousov };
1693f0289eaSKonstantin Belousov
1703f0289eaSKonstantin Belousov static int
dmar_rhsa_iter(ACPI_DMAR_HEADER * dmarh,void * arg)1713f0289eaSKonstantin Belousov dmar_rhsa_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
1723f0289eaSKonstantin Belousov {
1733f0289eaSKonstantin Belousov struct rhsa_iter_arg *ria;
1743f0289eaSKonstantin Belousov ACPI_DMAR_RHSA *adr;
1753f0289eaSKonstantin Belousov
1763f0289eaSKonstantin Belousov if (dmarh->Type == ACPI_DMAR_TYPE_HARDWARE_AFFINITY) {
1773f0289eaSKonstantin Belousov ria = arg;
1783f0289eaSKonstantin Belousov adr = (ACPI_DMAR_RHSA *)dmarh;
1793f0289eaSKonstantin Belousov if (adr->BaseAddress == ria->base)
1803f0289eaSKonstantin Belousov ria->proxim_dom = adr->ProximityDomain;
1813f0289eaSKonstantin Belousov }
1823f0289eaSKonstantin Belousov return (1);
1833f0289eaSKonstantin Belousov }
1843f0289eaSKonstantin Belousov
18524e38af6SKonstantin Belousov int dmar_rmrr_enable = 1;
18624e38af6SKonstantin Belousov
1870875f3cdSEd Maste static int dmar_enable = 0;
18886be9f0dSKonstantin Belousov static void
dmar_identify(driver_t * driver,device_t parent)18986be9f0dSKonstantin Belousov dmar_identify(driver_t *driver, device_t parent)
19086be9f0dSKonstantin Belousov {
19186be9f0dSKonstantin Belousov ACPI_TABLE_DMAR *dmartbl;
19286be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh;
1933f0289eaSKonstantin Belousov struct rhsa_iter_arg ria;
19486be9f0dSKonstantin Belousov ACPI_STATUS status;
19586be9f0dSKonstantin Belousov int i, error;
19686be9f0dSKonstantin Belousov
19786be9f0dSKonstantin Belousov if (acpi_disabled("dmar"))
19886be9f0dSKonstantin Belousov return;
19986be9f0dSKonstantin Belousov TUNABLE_INT_FETCH("hw.dmar.enable", &dmar_enable);
20086be9f0dSKonstantin Belousov if (!dmar_enable)
20186be9f0dSKonstantin Belousov return;
20224e38af6SKonstantin Belousov TUNABLE_INT_FETCH("hw.dmar.rmrr_enable", &dmar_rmrr_enable);
20324e38af6SKonstantin Belousov
20486be9f0dSKonstantin Belousov status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl);
20586be9f0dSKonstantin Belousov if (ACPI_FAILURE(status))
20686be9f0dSKonstantin Belousov return;
20786be9f0dSKonstantin Belousov haw = dmartbl->Width + 1;
20886be9f0dSKonstantin Belousov if ((1ULL << (haw + 1)) > BUS_SPACE_MAXADDR)
20940d951bcSKonstantin Belousov iommu_high = BUS_SPACE_MAXADDR;
21086be9f0dSKonstantin Belousov else
21140d951bcSKonstantin Belousov iommu_high = 1ULL << (haw + 1);
21286be9f0dSKonstantin Belousov if (bootverbose) {
21386be9f0dSKonstantin Belousov printf("DMAR HAW=%d flags=<%b>\n", dmartbl->Width,
21486be9f0dSKonstantin Belousov (unsigned)dmartbl->Flags,
21586be9f0dSKonstantin Belousov "\020\001INTR_REMAP\002X2APIC_OPT_OUT");
21686be9f0dSKonstantin Belousov }
2173dd3c450SKonstantin Belousov AcpiPutTable((ACPI_TABLE_HEADER *)dmartbl);
21886be9f0dSKonstantin Belousov
21986be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_count_iter, NULL);
22086be9f0dSKonstantin Belousov if (dmar_devcnt == 0)
22186be9f0dSKonstantin Belousov return;
22286be9f0dSKonstantin Belousov dmar_devs = malloc(sizeof(device_t) * dmar_devcnt, M_DEVBUF,
22386be9f0dSKonstantin Belousov M_WAITOK | M_ZERO);
22486be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) {
22586be9f0dSKonstantin Belousov dmarh = dmar_find_by_index(i);
22686be9f0dSKonstantin Belousov if (dmarh == NULL) {
22786be9f0dSKonstantin Belousov printf("dmar_identify: cannot find HWUNIT %d\n", i);
22886be9f0dSKonstantin Belousov continue;
22986be9f0dSKonstantin Belousov }
23086be9f0dSKonstantin Belousov dmar_devs[i] = BUS_ADD_CHILD(parent, 1, "dmar", i);
23186be9f0dSKonstantin Belousov if (dmar_devs[i] == NULL) {
23286be9f0dSKonstantin Belousov printf("dmar_identify: cannot create instance %d\n", i);
23386be9f0dSKonstantin Belousov continue;
23486be9f0dSKonstantin Belousov }
23586be9f0dSKonstantin Belousov error = bus_set_resource(dmar_devs[i], SYS_RES_MEMORY,
23686be9f0dSKonstantin Belousov DMAR_REG_RID, dmarh->Address, PAGE_SIZE);
23786be9f0dSKonstantin Belousov if (error != 0) {
23886be9f0dSKonstantin Belousov printf(
23986be9f0dSKonstantin Belousov "dmar%d: unable to alloc register window at 0x%08jx: error %d\n",
24086be9f0dSKonstantin Belousov i, (uintmax_t)dmarh->Address, error);
24186be9f0dSKonstantin Belousov device_delete_child(parent, dmar_devs[i]);
24286be9f0dSKonstantin Belousov dmar_devs[i] = NULL;
2433f0289eaSKonstantin Belousov continue;
24486be9f0dSKonstantin Belousov }
2453f0289eaSKonstantin Belousov
2463f0289eaSKonstantin Belousov ria.base = dmarh->Address;
2473f0289eaSKonstantin Belousov ria.proxim_dom = -1;
2483f0289eaSKonstantin Belousov dmar_iterate_tbl(dmar_rhsa_iter, &ria);
2493f0289eaSKonstantin Belousov acpi_set_domain(dmar_devs[i], ria.proxim_dom == -1 ?
2503f0289eaSKonstantin Belousov ACPI_DEV_DOMAIN_UNKNOWN :
2513f0289eaSKonstantin Belousov acpi_map_pxm_to_vm_domainid(ria.proxim_dom));
25286be9f0dSKonstantin Belousov }
25386be9f0dSKonstantin Belousov }
25486be9f0dSKonstantin Belousov
25586be9f0dSKonstantin Belousov static int
dmar_probe(device_t dev)25686be9f0dSKonstantin Belousov dmar_probe(device_t dev)
25786be9f0dSKonstantin Belousov {
25886be9f0dSKonstantin Belousov
25986be9f0dSKonstantin Belousov if (acpi_get_handle(dev) != NULL)
26086be9f0dSKonstantin Belousov return (ENXIO);
26186be9f0dSKonstantin Belousov device_set_desc(dev, "DMA remap");
2623100f7dfSKonstantin Belousov return (BUS_PROBE_NOWILDCARD);
26386be9f0dSKonstantin Belousov }
26486be9f0dSKonstantin Belousov
26586be9f0dSKonstantin Belousov static void
dmar_release_resources(device_t dev,struct dmar_unit * unit)26686be9f0dSKonstantin Belousov dmar_release_resources(device_t dev, struct dmar_unit *unit)
26786be9f0dSKonstantin Belousov {
26868eeb96aSKonstantin Belousov int i;
26986be9f0dSKonstantin Belousov
27059e37c8aSRuslan Bukin iommu_fini_busdma(&unit->iommu);
2710a110d5bSKonstantin Belousov dmar_fini_irt(unit);
27268eeb96aSKonstantin Belousov dmar_fini_qi(unit);
27386be9f0dSKonstantin Belousov dmar_fini_fault_log(unit);
27468eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++)
2755967352aSKonstantin Belousov iommu_release_intr(DMAR2IOMMU(unit), i);
27686be9f0dSKonstantin Belousov if (unit->regs != NULL) {
27786be9f0dSKonstantin Belousov bus_deactivate_resource(dev, SYS_RES_MEMORY, unit->reg_rid,
27886be9f0dSKonstantin Belousov unit->regs);
27986be9f0dSKonstantin Belousov bus_release_resource(dev, SYS_RES_MEMORY, unit->reg_rid,
28086be9f0dSKonstantin Belousov unit->regs);
28186be9f0dSKonstantin Belousov unit->regs = NULL;
28286be9f0dSKonstantin Belousov }
28386be9f0dSKonstantin Belousov if (unit->domids != NULL) {
28486be9f0dSKonstantin Belousov delete_unrhdr(unit->domids);
28586be9f0dSKonstantin Belousov unit->domids = NULL;
28686be9f0dSKonstantin Belousov }
28786be9f0dSKonstantin Belousov if (unit->ctx_obj != NULL) {
28886be9f0dSKonstantin Belousov vm_object_deallocate(unit->ctx_obj);
28986be9f0dSKonstantin Belousov unit->ctx_obj = NULL;
29086be9f0dSKonstantin Belousov }
291d50403a6SKonstantin Belousov sysctl_ctx_free(&unit->iommu.sysctl_ctx);
29286be9f0dSKonstantin Belousov }
29386be9f0dSKonstantin Belousov
29486be9f0dSKonstantin Belousov #ifdef DEV_APIC
29586be9f0dSKonstantin Belousov static int
dmar_remap_intr(device_t dev,device_t child,u_int irq)29686be9f0dSKonstantin Belousov dmar_remap_intr(device_t dev, device_t child, u_int irq)
29786be9f0dSKonstantin Belousov {
29886be9f0dSKonstantin Belousov struct dmar_unit *unit;
2995967352aSKonstantin Belousov struct iommu_msi_data *dmd;
30086be9f0dSKonstantin Belousov uint64_t msi_addr;
30186be9f0dSKonstantin Belousov uint32_t msi_data;
30268eeb96aSKonstantin Belousov int i, error;
30386be9f0dSKonstantin Belousov
30486be9f0dSKonstantin Belousov unit = device_get_softc(dev);
30568eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++) {
3065967352aSKonstantin Belousov dmd = &unit->x86c.intrs[i];
30768eeb96aSKonstantin Belousov if (irq == dmd->irq) {
30868eeb96aSKonstantin Belousov error = PCIB_MAP_MSI(device_get_parent(
30968eeb96aSKonstantin Belousov device_get_parent(dev)),
31068eeb96aSKonstantin Belousov dev, irq, &msi_addr, &msi_data);
31186be9f0dSKonstantin Belousov if (error != 0)
31286be9f0dSKonstantin Belousov return (error);
31368eeb96aSKonstantin Belousov DMAR_LOCK(unit);
3145967352aSKonstantin Belousov dmd->msi_data = msi_data;
3155967352aSKonstantin Belousov dmd->msi_addr = msi_addr;
3165967352aSKonstantin Belousov (dmd->disable_intr)(DMAR2IOMMU(unit));
3175967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_data_reg, dmd->msi_data);
3185967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_addr_reg, dmd->msi_addr);
3195967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_uaddr_reg,
3205967352aSKonstantin Belousov dmd->msi_addr >> 32);
3215967352aSKonstantin Belousov (dmd->enable_intr)(DMAR2IOMMU(unit));
32268eeb96aSKonstantin Belousov DMAR_UNLOCK(unit);
32386be9f0dSKonstantin Belousov return (0);
32486be9f0dSKonstantin Belousov }
32568eeb96aSKonstantin Belousov }
32668eeb96aSKonstantin Belousov return (ENOENT);
32768eeb96aSKonstantin Belousov }
32886be9f0dSKonstantin Belousov #endif
32986be9f0dSKonstantin Belousov
33086be9f0dSKonstantin Belousov static void
dmar_print_caps(device_t dev,struct dmar_unit * unit,ACPI_DMAR_HARDWARE_UNIT * dmaru)33186be9f0dSKonstantin Belousov dmar_print_caps(device_t dev, struct dmar_unit *unit,
33286be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmaru)
33386be9f0dSKonstantin Belousov {
33486be9f0dSKonstantin Belousov uint32_t caphi, ecaphi;
33586be9f0dSKonstantin Belousov
33686be9f0dSKonstantin Belousov device_printf(dev, "regs@0x%08jx, ver=%d.%d, seg=%d, flags=<%b>\n",
33786be9f0dSKonstantin Belousov (uintmax_t)dmaru->Address, DMAR_MAJOR_VER(unit->hw_ver),
33886be9f0dSKonstantin Belousov DMAR_MINOR_VER(unit->hw_ver), dmaru->Segment,
33986be9f0dSKonstantin Belousov dmaru->Flags, "\020\001INCLUDE_ALL_PCI");
34086be9f0dSKonstantin Belousov caphi = unit->hw_cap >> 32;
34186be9f0dSKonstantin Belousov device_printf(dev, "cap=%b,", (u_int)unit->hw_cap,
34286be9f0dSKonstantin Belousov "\020\004AFL\005WBF\006PLMR\007PHMR\010CM\027ZLR\030ISOCH");
343e17c0a1eSKonstantin Belousov printf("%b, ", caphi, "\020\010PSI\027DWD\030DRD\031FL1GP\034PSI");
34486be9f0dSKonstantin Belousov printf("ndoms=%d, sagaw=%d, mgaw=%d, fro=%d, nfr=%d, superp=%d",
34586be9f0dSKonstantin Belousov DMAR_CAP_ND(unit->hw_cap), DMAR_CAP_SAGAW(unit->hw_cap),
34686be9f0dSKonstantin Belousov DMAR_CAP_MGAW(unit->hw_cap), DMAR_CAP_FRO(unit->hw_cap),
34786be9f0dSKonstantin Belousov DMAR_CAP_NFR(unit->hw_cap), DMAR_CAP_SPS(unit->hw_cap));
34886be9f0dSKonstantin Belousov if ((unit->hw_cap & DMAR_CAP_PSI) != 0)
34986be9f0dSKonstantin Belousov printf(", mamv=%d", DMAR_CAP_MAMV(unit->hw_cap));
35086be9f0dSKonstantin Belousov printf("\n");
35186be9f0dSKonstantin Belousov ecaphi = unit->hw_ecap >> 32;
35286be9f0dSKonstantin Belousov device_printf(dev, "ecap=%b,", (u_int)unit->hw_ecap,
353e17c0a1eSKonstantin Belousov "\020\001C\002QI\003DI\004IR\005EIM\007PT\010SC\031ECS\032MTS"
354e17c0a1eSKonstantin Belousov "\033NEST\034DIS\035PASID\036PRS\037ERS\040SRS");
355e17c0a1eSKonstantin Belousov printf("%b, ", ecaphi, "\020\002NWFS\003EAFS");
35686be9f0dSKonstantin Belousov printf("mhmw=%d, iro=%d\n", DMAR_ECAP_MHMV(unit->hw_ecap),
35786be9f0dSKonstantin Belousov DMAR_ECAP_IRO(unit->hw_ecap));
35886be9f0dSKonstantin Belousov }
35986be9f0dSKonstantin Belousov
36086be9f0dSKonstantin Belousov static int
dmar_attach(device_t dev)36186be9f0dSKonstantin Belousov dmar_attach(device_t dev)
36286be9f0dSKonstantin Belousov {
36386be9f0dSKonstantin Belousov struct dmar_unit *unit;
36486be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmaru;
3655967352aSKonstantin Belousov struct iommu_msi_data *dmd;
366476358b3SKonstantin Belousov uint64_t timeout;
36706f659c3SKornel Duleba int disable_pmr;
36868eeb96aSKonstantin Belousov int i, error;
36986be9f0dSKonstantin Belousov
37086be9f0dSKonstantin Belousov unit = device_get_softc(dev);
37159e37c8aSRuslan Bukin unit->iommu.unit = device_get_unit(dev);
372f5931169SRuslan Bukin unit->iommu.dev = dev;
373d50403a6SKonstantin Belousov sysctl_ctx_init(&unit->iommu.sysctl_ctx);
37459e37c8aSRuslan Bukin dmaru = dmar_find_by_index(unit->iommu.unit);
37586be9f0dSKonstantin Belousov if (dmaru == NULL)
37686be9f0dSKonstantin Belousov return (EINVAL);
37786be9f0dSKonstantin Belousov unit->segment = dmaru->Segment;
37886be9f0dSKonstantin Belousov unit->base = dmaru->Address;
37986be9f0dSKonstantin Belousov unit->reg_rid = DMAR_REG_RID;
38086be9f0dSKonstantin Belousov unit->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
38186be9f0dSKonstantin Belousov &unit->reg_rid, RF_ACTIVE);
38286be9f0dSKonstantin Belousov if (unit->regs == NULL) {
38386be9f0dSKonstantin Belousov device_printf(dev, "cannot allocate register window\n");
38445543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
38586be9f0dSKonstantin Belousov return (ENOMEM);
38686be9f0dSKonstantin Belousov }
38786be9f0dSKonstantin Belousov unit->hw_ver = dmar_read4(unit, DMAR_VER_REG);
38886be9f0dSKonstantin Belousov unit->hw_cap = dmar_read8(unit, DMAR_CAP_REG);
38986be9f0dSKonstantin Belousov unit->hw_ecap = dmar_read8(unit, DMAR_ECAP_REG);
39086be9f0dSKonstantin Belousov if (bootverbose)
39186be9f0dSKonstantin Belousov dmar_print_caps(dev, unit, dmaru);
39286be9f0dSKonstantin Belousov dmar_quirks_post_ident(unit);
3933f0289eaSKonstantin Belousov unit->memdomain = acpi_get_domain(dev);
394476358b3SKonstantin Belousov timeout = dmar_get_timeout();
39512cce599SZhenlei Huang TUNABLE_UINT64_FETCH("hw.iommu.dmar.timeout", &timeout);
396476358b3SKonstantin Belousov dmar_update_timeout(timeout);
397476358b3SKonstantin Belousov
39868eeb96aSKonstantin Belousov for (i = 0; i < DMAR_INTR_TOTAL; i++)
3995967352aSKonstantin Belousov unit->x86c.intrs[i].irq = -1;
40068eeb96aSKonstantin Belousov
4015967352aSKonstantin Belousov dmd = &unit->x86c.intrs[DMAR_INTR_FAULT];
4025967352aSKonstantin Belousov dmd->name = "fault";
4035967352aSKonstantin Belousov dmd->irq_rid = DMAR_FAULT_IRQ_RID;
4045967352aSKonstantin Belousov dmd->handler = dmar_fault_intr;
4055967352aSKonstantin Belousov dmd->msi_data_reg = DMAR_FEDATA_REG;
4065967352aSKonstantin Belousov dmd->msi_addr_reg = DMAR_FEADDR_REG;
4075967352aSKonstantin Belousov dmd->msi_uaddr_reg = DMAR_FEUADDR_REG;
4085967352aSKonstantin Belousov dmd->enable_intr = dmar_enable_fault_intr;
4095967352aSKonstantin Belousov dmd->disable_intr = dmar_disable_fault_intr;
4105967352aSKonstantin Belousov error = iommu_alloc_irq(DMAR2IOMMU(unit), DMAR_INTR_FAULT);
41186be9f0dSKonstantin Belousov if (error != 0) {
41286be9f0dSKonstantin Belousov dmar_release_resources(dev, unit);
41345543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
41486be9f0dSKonstantin Belousov return (error);
41586be9f0dSKonstantin Belousov }
4165967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_data_reg, dmd->msi_data);
4175967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_addr_reg, dmd->msi_addr);
4185967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_uaddr_reg, dmd->msi_addr >> 32);
4195967352aSKonstantin Belousov
42068eeb96aSKonstantin Belousov if (DMAR_HAS_QI(unit)) {
4215967352aSKonstantin Belousov dmd = &unit->x86c.intrs[DMAR_INTR_QI];
4225967352aSKonstantin Belousov dmd->name = "qi";
4235967352aSKonstantin Belousov dmd->irq_rid = DMAR_QI_IRQ_RID;
4245967352aSKonstantin Belousov dmd->handler = dmar_qi_intr;
4255967352aSKonstantin Belousov dmd->msi_data_reg = DMAR_IEDATA_REG;
4265967352aSKonstantin Belousov dmd->msi_addr_reg = DMAR_IEADDR_REG;
4275967352aSKonstantin Belousov dmd->msi_uaddr_reg = DMAR_IEUADDR_REG;
4285967352aSKonstantin Belousov dmd->enable_intr = dmar_enable_qi_intr;
4295967352aSKonstantin Belousov dmd->disable_intr = dmar_disable_qi_intr;
4305967352aSKonstantin Belousov error = iommu_alloc_irq(DMAR2IOMMU(unit), DMAR_INTR_QI);
43168eeb96aSKonstantin Belousov if (error != 0) {
43268eeb96aSKonstantin Belousov dmar_release_resources(dev, unit);
43345543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
43468eeb96aSKonstantin Belousov return (error);
43568eeb96aSKonstantin Belousov }
4365967352aSKonstantin Belousov
4375967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_data_reg, dmd->msi_data);
4385967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_addr_reg, dmd->msi_addr);
4395967352aSKonstantin Belousov dmar_write4(unit, dmd->msi_uaddr_reg, dmd->msi_addr >> 32);
44068eeb96aSKonstantin Belousov }
44168eeb96aSKonstantin Belousov
44259e37c8aSRuslan Bukin mtx_init(&unit->iommu.lock, "dmarhw", NULL, MTX_DEF);
44386be9f0dSKonstantin Belousov unit->domids = new_unrhdr(0, dmar_nd2mask(DMAR_CAP_ND(unit->hw_cap)),
44459e37c8aSRuslan Bukin &unit->iommu.lock);
4451abfd355SKonstantin Belousov LIST_INIT(&unit->domains);
44686be9f0dSKonstantin Belousov
44786be9f0dSKonstantin Belousov /*
44886be9f0dSKonstantin Belousov * 9.2 "Context Entry":
44986be9f0dSKonstantin Belousov * When Caching Mode (CM) field is reported as Set, the
45086be9f0dSKonstantin Belousov * domain-id value of zero is architecturally reserved.
45186be9f0dSKonstantin Belousov * Software must not use domain-id value of zero
45286be9f0dSKonstantin Belousov * when CM is Set.
45386be9f0dSKonstantin Belousov */
45486be9f0dSKonstantin Belousov if ((unit->hw_cap & DMAR_CAP_CM) != 0)
45586be9f0dSKonstantin Belousov alloc_unr_specific(unit->domids, 0);
45686be9f0dSKonstantin Belousov
45786be9f0dSKonstantin Belousov unit->ctx_obj = vm_pager_allocate(OBJT_PHYS, NULL, IDX_TO_OFF(1 +
45886be9f0dSKonstantin Belousov DMAR_CTX_CNT), 0, 0, NULL);
459705090cbSKonstantin Belousov if (unit->memdomain != -1) {
460705090cbSKonstantin Belousov unit->ctx_obj->domain.dr_policy = DOMAINSET_PREF(
461705090cbSKonstantin Belousov unit->memdomain);
462705090cbSKonstantin Belousov }
46386be9f0dSKonstantin Belousov
46486be9f0dSKonstantin Belousov /*
46586be9f0dSKonstantin Belousov * Allocate and load the root entry table pointer. Enable the
46686be9f0dSKonstantin Belousov * address translation after the required invalidations are
46786be9f0dSKonstantin Belousov * done.
46886be9f0dSKonstantin Belousov */
46940d951bcSKonstantin Belousov iommu_pgalloc(unit->ctx_obj, 0, IOMMU_PGF_WAITOK | IOMMU_PGF_ZERO);
47086be9f0dSKonstantin Belousov DMAR_LOCK(unit);
47186be9f0dSKonstantin Belousov error = dmar_load_root_entry_ptr(unit);
47286be9f0dSKonstantin Belousov if (error != 0) {
47386be9f0dSKonstantin Belousov DMAR_UNLOCK(unit);
47486be9f0dSKonstantin Belousov dmar_release_resources(dev, unit);
47545543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
47686be9f0dSKonstantin Belousov return (error);
47786be9f0dSKonstantin Belousov }
47886be9f0dSKonstantin Belousov error = dmar_inv_ctx_glob(unit);
47986be9f0dSKonstantin Belousov if (error != 0) {
48086be9f0dSKonstantin Belousov DMAR_UNLOCK(unit);
48186be9f0dSKonstantin Belousov dmar_release_resources(dev, unit);
48245543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
48386be9f0dSKonstantin Belousov return (error);
48486be9f0dSKonstantin Belousov }
48586be9f0dSKonstantin Belousov if ((unit->hw_ecap & DMAR_ECAP_DI) != 0) {
48686be9f0dSKonstantin Belousov error = dmar_inv_iotlb_glob(unit);
48786be9f0dSKonstantin Belousov if (error != 0) {
48886be9f0dSKonstantin Belousov DMAR_UNLOCK(unit);
48986be9f0dSKonstantin Belousov dmar_release_resources(dev, unit);
49045543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
49186be9f0dSKonstantin Belousov return (error);
49286be9f0dSKonstantin Belousov }
49386be9f0dSKonstantin Belousov }
49486be9f0dSKonstantin Belousov
49586be9f0dSKonstantin Belousov DMAR_UNLOCK(unit);
49686be9f0dSKonstantin Belousov error = dmar_init_fault_log(unit);
49786be9f0dSKonstantin Belousov if (error != 0) {
49886be9f0dSKonstantin Belousov dmar_release_resources(dev, unit);
49945543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
50086be9f0dSKonstantin Belousov return (error);
50186be9f0dSKonstantin Belousov }
50268eeb96aSKonstantin Belousov error = dmar_init_qi(unit);
50368eeb96aSKonstantin Belousov if (error != 0) {
50468eeb96aSKonstantin Belousov dmar_release_resources(dev, unit);
50545543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
50668eeb96aSKonstantin Belousov return (error);
50768eeb96aSKonstantin Belousov }
5080a110d5bSKonstantin Belousov error = dmar_init_irt(unit);
5090a110d5bSKonstantin Belousov if (error != 0) {
5100a110d5bSKonstantin Belousov dmar_release_resources(dev, unit);
51145543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
5120a110d5bSKonstantin Belousov return (error);
5130a110d5bSKonstantin Belousov }
51406f659c3SKornel Duleba
51506f659c3SKornel Duleba disable_pmr = 0;
51606f659c3SKornel Duleba TUNABLE_INT_FETCH("hw.dmar.pmr.disable", &disable_pmr);
51706f659c3SKornel Duleba if (disable_pmr) {
51806f659c3SKornel Duleba error = dmar_disable_protected_regions(unit);
51906f659c3SKornel Duleba if (error != 0)
52006f659c3SKornel Duleba device_printf(dev,
52106f659c3SKornel Duleba "Failed to disable protected regions\n");
52206f659c3SKornel Duleba }
52306f659c3SKornel Duleba
52459e37c8aSRuslan Bukin error = iommu_init_busdma(&unit->iommu);
52586be9f0dSKonstantin Belousov if (error != 0) {
52686be9f0dSKonstantin Belousov dmar_release_resources(dev, unit);
52745543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
52886be9f0dSKonstantin Belousov return (error);
52986be9f0dSKonstantin Belousov }
53086be9f0dSKonstantin Belousov
53186be9f0dSKonstantin Belousov #ifdef NOTYET
53286be9f0dSKonstantin Belousov DMAR_LOCK(unit);
53386be9f0dSKonstantin Belousov error = dmar_enable_translation(unit);
53486be9f0dSKonstantin Belousov if (error != 0) {
53586be9f0dSKonstantin Belousov DMAR_UNLOCK(unit);
53686be9f0dSKonstantin Belousov dmar_release_resources(dev, unit);
53745543d34SKonstantin Belousov dmar_devs[unit->iommu.unit] = NULL;
53886be9f0dSKonstantin Belousov return (error);
53986be9f0dSKonstantin Belousov }
54086be9f0dSKonstantin Belousov DMAR_UNLOCK(unit);
54186be9f0dSKonstantin Belousov #endif
54286be9f0dSKonstantin Belousov
543*512bb0daSKonstantin Belousov dmar_running = true;
54486be9f0dSKonstantin Belousov return (0);
54586be9f0dSKonstantin Belousov }
54686be9f0dSKonstantin Belousov
54786be9f0dSKonstantin Belousov static int
dmar_detach(device_t dev)54886be9f0dSKonstantin Belousov dmar_detach(device_t dev)
54986be9f0dSKonstantin Belousov {
55086be9f0dSKonstantin Belousov
55186be9f0dSKonstantin Belousov return (EBUSY);
55286be9f0dSKonstantin Belousov }
55386be9f0dSKonstantin Belousov
55486be9f0dSKonstantin Belousov static int
dmar_suspend(device_t dev)55586be9f0dSKonstantin Belousov dmar_suspend(device_t dev)
55686be9f0dSKonstantin Belousov {
55786be9f0dSKonstantin Belousov
55886be9f0dSKonstantin Belousov return (0);
55986be9f0dSKonstantin Belousov }
56086be9f0dSKonstantin Belousov
56186be9f0dSKonstantin Belousov static int
dmar_resume(device_t dev)56286be9f0dSKonstantin Belousov dmar_resume(device_t dev)
56386be9f0dSKonstantin Belousov {
56486be9f0dSKonstantin Belousov
56586be9f0dSKonstantin Belousov /* XXXKIB */
56686be9f0dSKonstantin Belousov return (0);
56786be9f0dSKonstantin Belousov }
56886be9f0dSKonstantin Belousov
56986be9f0dSKonstantin Belousov static device_method_t dmar_methods[] = {
57086be9f0dSKonstantin Belousov DEVMETHOD(device_identify, dmar_identify),
57186be9f0dSKonstantin Belousov DEVMETHOD(device_probe, dmar_probe),
57286be9f0dSKonstantin Belousov DEVMETHOD(device_attach, dmar_attach),
57386be9f0dSKonstantin Belousov DEVMETHOD(device_detach, dmar_detach),
57486be9f0dSKonstantin Belousov DEVMETHOD(device_suspend, dmar_suspend),
57586be9f0dSKonstantin Belousov DEVMETHOD(device_resume, dmar_resume),
57686be9f0dSKonstantin Belousov #ifdef DEV_APIC
57786be9f0dSKonstantin Belousov DEVMETHOD(bus_remap_intr, dmar_remap_intr),
57886be9f0dSKonstantin Belousov #endif
57986be9f0dSKonstantin Belousov DEVMETHOD_END
58086be9f0dSKonstantin Belousov };
58186be9f0dSKonstantin Belousov
58286be9f0dSKonstantin Belousov static driver_t dmar_driver = {
58386be9f0dSKonstantin Belousov "dmar",
58486be9f0dSKonstantin Belousov dmar_methods,
58586be9f0dSKonstantin Belousov sizeof(struct dmar_unit),
58686be9f0dSKonstantin Belousov };
58786be9f0dSKonstantin Belousov
58880d2b3deSJohn Baldwin DRIVER_MODULE(dmar, acpi, dmar_driver, 0, 0);
58986be9f0dSKonstantin Belousov MODULE_DEPEND(dmar, acpi, 1, 1, 1);
59086be9f0dSKonstantin Belousov
59186be9f0dSKonstantin Belousov static void
dmar_print_path(int busno,int depth,const ACPI_DMAR_PCI_PATH * path)592f9feb091SKonstantin Belousov dmar_print_path(int busno, int depth, const ACPI_DMAR_PCI_PATH *path)
59386be9f0dSKonstantin Belousov {
59486be9f0dSKonstantin Belousov int i;
59586be9f0dSKonstantin Belousov
596f9feb091SKonstantin Belousov printf("[%d, ", busno);
59786be9f0dSKonstantin Belousov for (i = 0; i < depth; i++) {
59886be9f0dSKonstantin Belousov if (i != 0)
59986be9f0dSKonstantin Belousov printf(", ");
60086be9f0dSKonstantin Belousov printf("(%d, %d)", path[i].Device, path[i].Function);
60186be9f0dSKonstantin Belousov }
602f9feb091SKonstantin Belousov printf("]");
60386be9f0dSKonstantin Belousov }
60486be9f0dSKonstantin Belousov
605f9feb091SKonstantin Belousov int
dmar_dev_depth(device_t child)60686be9f0dSKonstantin Belousov dmar_dev_depth(device_t child)
60786be9f0dSKonstantin Belousov {
60886be9f0dSKonstantin Belousov devclass_t pci_class;
60986be9f0dSKonstantin Belousov device_t bus, pcib;
61086be9f0dSKonstantin Belousov int depth;
61186be9f0dSKonstantin Belousov
61286be9f0dSKonstantin Belousov pci_class = devclass_find("pci");
61386be9f0dSKonstantin Belousov for (depth = 1; ; depth++) {
61486be9f0dSKonstantin Belousov bus = device_get_parent(child);
61586be9f0dSKonstantin Belousov pcib = device_get_parent(bus);
61686be9f0dSKonstantin Belousov if (device_get_devclass(device_get_parent(pcib)) !=
61786be9f0dSKonstantin Belousov pci_class)
61886be9f0dSKonstantin Belousov return (depth);
61986be9f0dSKonstantin Belousov child = pcib;
62086be9f0dSKonstantin Belousov }
62186be9f0dSKonstantin Belousov }
62286be9f0dSKonstantin Belousov
623f9feb091SKonstantin Belousov void
dmar_dev_path(device_t child,int * busno,void * path1,int depth)624f9feb091SKonstantin Belousov dmar_dev_path(device_t child, int *busno, void *path1, int depth)
62586be9f0dSKonstantin Belousov {
62686be9f0dSKonstantin Belousov devclass_t pci_class;
62786be9f0dSKonstantin Belousov device_t bus, pcib;
628f9feb091SKonstantin Belousov ACPI_DMAR_PCI_PATH *path;
62986be9f0dSKonstantin Belousov
63086be9f0dSKonstantin Belousov pci_class = devclass_find("pci");
631f9feb091SKonstantin Belousov path = path1;
63286be9f0dSKonstantin Belousov for (depth--; depth != -1; depth--) {
63386be9f0dSKonstantin Belousov path[depth].Device = pci_get_slot(child);
63486be9f0dSKonstantin Belousov path[depth].Function = pci_get_function(child);
63586be9f0dSKonstantin Belousov bus = device_get_parent(child);
63686be9f0dSKonstantin Belousov pcib = device_get_parent(bus);
63786be9f0dSKonstantin Belousov if (device_get_devclass(device_get_parent(pcib)) !=
63886be9f0dSKonstantin Belousov pci_class) {
63986be9f0dSKonstantin Belousov /* reached a host bridge */
64086be9f0dSKonstantin Belousov *busno = pcib_get_bus(bus);
64186be9f0dSKonstantin Belousov return;
64286be9f0dSKonstantin Belousov }
64386be9f0dSKonstantin Belousov child = pcib;
64486be9f0dSKonstantin Belousov }
64586be9f0dSKonstantin Belousov panic("wrong depth");
64686be9f0dSKonstantin Belousov }
64786be9f0dSKonstantin Belousov
64886be9f0dSKonstantin Belousov static int
dmar_match_pathes(int busno1,const ACPI_DMAR_PCI_PATH * path1,int depth1,int busno2,const ACPI_DMAR_PCI_PATH * path2,int depth2,enum AcpiDmarScopeType scope_type)64986be9f0dSKonstantin Belousov dmar_match_pathes(int busno1, const ACPI_DMAR_PCI_PATH *path1, int depth1,
65086be9f0dSKonstantin Belousov int busno2, const ACPI_DMAR_PCI_PATH *path2, int depth2,
65186be9f0dSKonstantin Belousov enum AcpiDmarScopeType scope_type)
65286be9f0dSKonstantin Belousov {
65386be9f0dSKonstantin Belousov int i, depth;
65486be9f0dSKonstantin Belousov
65586be9f0dSKonstantin Belousov if (busno1 != busno2)
65686be9f0dSKonstantin Belousov return (0);
65786be9f0dSKonstantin Belousov if (scope_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && depth1 != depth2)
65886be9f0dSKonstantin Belousov return (0);
65986be9f0dSKonstantin Belousov depth = depth1;
66086be9f0dSKonstantin Belousov if (depth2 < depth)
66186be9f0dSKonstantin Belousov depth = depth2;
66286be9f0dSKonstantin Belousov for (i = 0; i < depth; i++) {
66386be9f0dSKonstantin Belousov if (path1[i].Device != path2[i].Device ||
66486be9f0dSKonstantin Belousov path1[i].Function != path2[i].Function)
66586be9f0dSKonstantin Belousov return (0);
66686be9f0dSKonstantin Belousov }
66786be9f0dSKonstantin Belousov return (1);
66886be9f0dSKonstantin Belousov }
66986be9f0dSKonstantin Belousov
67086be9f0dSKonstantin Belousov static int
dmar_match_devscope(ACPI_DMAR_DEVICE_SCOPE * devscope,int dev_busno,const ACPI_DMAR_PCI_PATH * dev_path,int dev_path_len)671f9feb091SKonstantin Belousov dmar_match_devscope(ACPI_DMAR_DEVICE_SCOPE *devscope, int dev_busno,
672f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len)
67386be9f0dSKonstantin Belousov {
67486be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH *path;
67586be9f0dSKonstantin Belousov int path_len;
67686be9f0dSKonstantin Belousov
67786be9f0dSKonstantin Belousov if (devscope->Length < sizeof(*devscope)) {
678f9feb091SKonstantin Belousov printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
67986be9f0dSKonstantin Belousov devscope->Length);
68086be9f0dSKonstantin Belousov return (-1);
68186be9f0dSKonstantin Belousov }
68286be9f0dSKonstantin Belousov if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT &&
68386be9f0dSKonstantin Belousov devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_BRIDGE)
68486be9f0dSKonstantin Belousov return (0);
68586be9f0dSKonstantin Belousov path_len = devscope->Length - sizeof(*devscope);
68686be9f0dSKonstantin Belousov if (path_len % 2 != 0) {
687f9feb091SKonstantin Belousov printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
68886be9f0dSKonstantin Belousov devscope->Length);
68986be9f0dSKonstantin Belousov return (-1);
69086be9f0dSKonstantin Belousov }
69186be9f0dSKonstantin Belousov path_len /= 2;
69286be9f0dSKonstantin Belousov path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
69386be9f0dSKonstantin Belousov if (path_len == 0) {
694f9feb091SKonstantin Belousov printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
69586be9f0dSKonstantin Belousov devscope->Length);
69686be9f0dSKonstantin Belousov return (-1);
69786be9f0dSKonstantin Belousov }
69886be9f0dSKonstantin Belousov
69986be9f0dSKonstantin Belousov return (dmar_match_pathes(devscope->Bus, path, path_len, dev_busno,
70086be9f0dSKonstantin Belousov dev_path, dev_path_len, devscope->EntryType));
70186be9f0dSKonstantin Belousov }
70286be9f0dSKonstantin Belousov
703f9feb091SKonstantin Belousov static bool
dmar_match_by_path(struct dmar_unit * unit,int dev_domain,int dev_busno,const ACPI_DMAR_PCI_PATH * dev_path,int dev_path_len,const char ** banner)704f9feb091SKonstantin Belousov dmar_match_by_path(struct dmar_unit *unit, int dev_domain, int dev_busno,
705f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len, const char **banner)
70686be9f0dSKonstantin Belousov {
70786be9f0dSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh;
70886be9f0dSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope;
70986be9f0dSKonstantin Belousov char *ptr, *ptrend;
710f9feb091SKonstantin Belousov int match;
711f9feb091SKonstantin Belousov
71259e37c8aSRuslan Bukin dmarh = dmar_find_by_index(unit->iommu.unit);
713f9feb091SKonstantin Belousov if (dmarh == NULL)
714f9feb091SKonstantin Belousov return (false);
715f9feb091SKonstantin Belousov if (dmarh->Segment != dev_domain)
716f9feb091SKonstantin Belousov return (false);
717f9feb091SKonstantin Belousov if ((dmarh->Flags & ACPI_DMAR_INCLUDE_ALL) != 0) {
718f9feb091SKonstantin Belousov if (banner != NULL)
719f9feb091SKonstantin Belousov *banner = "INCLUDE_ALL";
720f9feb091SKonstantin Belousov return (true);
721f9feb091SKonstantin Belousov }
722f9feb091SKonstantin Belousov ptr = (char *)dmarh + sizeof(*dmarh);
723f9feb091SKonstantin Belousov ptrend = (char *)dmarh + dmarh->Header.Length;
724f9feb091SKonstantin Belousov while (ptr < ptrend) {
725f9feb091SKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
726f9feb091SKonstantin Belousov ptr += devscope->Length;
727f9feb091SKonstantin Belousov match = dmar_match_devscope(devscope, dev_busno, dev_path,
728f9feb091SKonstantin Belousov dev_path_len);
729f9feb091SKonstantin Belousov if (match == -1)
730f9feb091SKonstantin Belousov return (false);
731f9feb091SKonstantin Belousov if (match == 1) {
732f9feb091SKonstantin Belousov if (banner != NULL)
733f9feb091SKonstantin Belousov *banner = "specific match";
734f9feb091SKonstantin Belousov return (true);
735f9feb091SKonstantin Belousov }
736f9feb091SKonstantin Belousov }
737f9feb091SKonstantin Belousov return (false);
738f9feb091SKonstantin Belousov }
739f9feb091SKonstantin Belousov
740f9feb091SKonstantin Belousov static struct dmar_unit *
dmar_find_by_scope(int dev_domain,int dev_busno,const ACPI_DMAR_PCI_PATH * dev_path,int dev_path_len)741f9feb091SKonstantin Belousov dmar_find_by_scope(int dev_domain, int dev_busno,
742f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len)
743f9feb091SKonstantin Belousov {
744f9feb091SKonstantin Belousov struct dmar_unit *unit;
745f9feb091SKonstantin Belousov int i;
746f9feb091SKonstantin Belousov
747f9feb091SKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) {
748f9feb091SKonstantin Belousov if (dmar_devs[i] == NULL)
749f9feb091SKonstantin Belousov continue;
750f9feb091SKonstantin Belousov unit = device_get_softc(dmar_devs[i]);
751f9feb091SKonstantin Belousov if (dmar_match_by_path(unit, dev_domain, dev_busno, dev_path,
752f9feb091SKonstantin Belousov dev_path_len, NULL))
753f9feb091SKonstantin Belousov return (unit);
754f9feb091SKonstantin Belousov }
755f9feb091SKonstantin Belousov return (NULL);
756f9feb091SKonstantin Belousov }
757f9feb091SKonstantin Belousov
758f9feb091SKonstantin Belousov struct dmar_unit *
dmar_find(device_t dev,bool verbose)759f9feb091SKonstantin Belousov dmar_find(device_t dev, bool verbose)
760f9feb091SKonstantin Belousov {
761f9feb091SKonstantin Belousov struct dmar_unit *unit;
762f9feb091SKonstantin Belousov const char *banner;
763f9feb091SKonstantin Belousov int i, dev_domain, dev_busno, dev_path_len;
76486be9f0dSKonstantin Belousov
765b7b6b7a9SKonstantin Belousov /*
766b7b6b7a9SKonstantin Belousov * This function can only handle PCI(e) devices.
767b7b6b7a9SKonstantin Belousov */
768b7b6b7a9SKonstantin Belousov if (device_get_devclass(device_get_parent(dev)) !=
769b7b6b7a9SKonstantin Belousov devclass_find("pci"))
770b7b6b7a9SKonstantin Belousov return (NULL);
771b7b6b7a9SKonstantin Belousov
77286be9f0dSKonstantin Belousov dev_domain = pci_get_domain(dev);
77386be9f0dSKonstantin Belousov dev_path_len = dmar_dev_depth(dev);
77486be9f0dSKonstantin Belousov ACPI_DMAR_PCI_PATH dev_path[dev_path_len];
77586be9f0dSKonstantin Belousov dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len);
776f9feb091SKonstantin Belousov banner = "";
77786be9f0dSKonstantin Belousov
77886be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) {
77986be9f0dSKonstantin Belousov if (dmar_devs[i] == NULL)
78086be9f0dSKonstantin Belousov continue;
781f9feb091SKonstantin Belousov unit = device_get_softc(dmar_devs[i]);
782f9feb091SKonstantin Belousov if (dmar_match_by_path(unit, dev_domain, dev_busno,
783f9feb091SKonstantin Belousov dev_path, dev_path_len, &banner))
78486be9f0dSKonstantin Belousov break;
78586be9f0dSKonstantin Belousov }
786f9feb091SKonstantin Belousov if (i == dmar_devcnt)
78786be9f0dSKonstantin Belousov return (NULL);
788f9feb091SKonstantin Belousov
789f9feb091SKonstantin Belousov if (verbose) {
790f9feb091SKonstantin Belousov device_printf(dev, "pci%d:%d:%d:%d matched dmar%d by %s",
791f9feb091SKonstantin Belousov dev_domain, pci_get_bus(dev), pci_get_slot(dev),
79259e37c8aSRuslan Bukin pci_get_function(dev), unit->iommu.unit, banner);
793f9feb091SKonstantin Belousov printf(" scope path ");
794f9feb091SKonstantin Belousov dmar_print_path(dev_busno, dev_path_len, dev_path);
795f9feb091SKonstantin Belousov printf("\n");
79686be9f0dSKonstantin Belousov }
797b08d332dSKonstantin Belousov iommu_device_set_iommu_prop(dev, unit->iommu.dev);
798f9feb091SKonstantin Belousov return (unit);
79986be9f0dSKonstantin Belousov }
80086be9f0dSKonstantin Belousov
8010a110d5bSKonstantin Belousov static struct dmar_unit *
dmar_find_nonpci(u_int id,u_int entry_type,uint16_t * rid)8020a110d5bSKonstantin Belousov dmar_find_nonpci(u_int id, u_int entry_type, uint16_t *rid)
8030a110d5bSKonstantin Belousov {
8040a110d5bSKonstantin Belousov device_t dmar_dev;
8050a110d5bSKonstantin Belousov struct dmar_unit *unit;
8060a110d5bSKonstantin Belousov ACPI_DMAR_HARDWARE_UNIT *dmarh;
8070a110d5bSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope;
8080a110d5bSKonstantin Belousov ACPI_DMAR_PCI_PATH *path;
8090a110d5bSKonstantin Belousov char *ptr, *ptrend;
810fd15fee1SKonstantin Belousov #ifdef DEV_APIC
811fd15fee1SKonstantin Belousov int error;
812fd15fee1SKonstantin Belousov #endif
8130a110d5bSKonstantin Belousov int i;
8140a110d5bSKonstantin Belousov
8150a110d5bSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) {
8160a110d5bSKonstantin Belousov dmar_dev = dmar_devs[i];
8170a110d5bSKonstantin Belousov if (dmar_dev == NULL)
8180a110d5bSKonstantin Belousov continue;
8190a110d5bSKonstantin Belousov unit = (struct dmar_unit *)device_get_softc(dmar_dev);
8200a110d5bSKonstantin Belousov dmarh = dmar_find_by_index(i);
8210a110d5bSKonstantin Belousov if (dmarh == NULL)
8220a110d5bSKonstantin Belousov continue;
8230a110d5bSKonstantin Belousov ptr = (char *)dmarh + sizeof(*dmarh);
8240a110d5bSKonstantin Belousov ptrend = (char *)dmarh + dmarh->Header.Length;
8250a110d5bSKonstantin Belousov for (;;) {
8260a110d5bSKonstantin Belousov if (ptr >= ptrend)
8270a110d5bSKonstantin Belousov break;
8280a110d5bSKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
8290a110d5bSKonstantin Belousov ptr += devscope->Length;
8300a110d5bSKonstantin Belousov if (devscope->EntryType != entry_type)
8310a110d5bSKonstantin Belousov continue;
8320a110d5bSKonstantin Belousov if (devscope->EnumerationId != id)
8330a110d5bSKonstantin Belousov continue;
834fd15fee1SKonstantin Belousov #ifdef DEV_APIC
835fd15fee1SKonstantin Belousov if (entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
836fd15fee1SKonstantin Belousov error = ioapic_get_rid(id, rid);
837fd15fee1SKonstantin Belousov /*
838fd15fee1SKonstantin Belousov * If our IOAPIC has PCI bindings then
839fd15fee1SKonstantin Belousov * use the PCI device rid.
840fd15fee1SKonstantin Belousov */
841fd15fee1SKonstantin Belousov if (error == 0)
842fd15fee1SKonstantin Belousov return (unit);
843fd15fee1SKonstantin Belousov }
844fd15fee1SKonstantin Belousov #endif
8450a110d5bSKonstantin Belousov if (devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE)
8460a110d5bSKonstantin Belousov == 2) {
8470a110d5bSKonstantin Belousov if (rid != NULL) {
8480a110d5bSKonstantin Belousov path = (ACPI_DMAR_PCI_PATH *)
8490a110d5bSKonstantin Belousov (devscope + 1);
8500a110d5bSKonstantin Belousov *rid = PCI_RID(devscope->Bus,
8510a110d5bSKonstantin Belousov path->Device, path->Function);
8520a110d5bSKonstantin Belousov }
8530a110d5bSKonstantin Belousov return (unit);
854fd15fee1SKonstantin Belousov }
8550a110d5bSKonstantin Belousov printf(
8560a110d5bSKonstantin Belousov "dmar_find_nonpci: id %d type %d path length != 2\n",
8570a110d5bSKonstantin Belousov id, entry_type);
858fd15fee1SKonstantin Belousov break;
8590a110d5bSKonstantin Belousov }
8600a110d5bSKonstantin Belousov }
8610a110d5bSKonstantin Belousov return (NULL);
8620a110d5bSKonstantin Belousov }
8630a110d5bSKonstantin Belousov
8640a110d5bSKonstantin Belousov struct dmar_unit *
dmar_find_hpet(device_t dev,uint16_t * rid)8650a110d5bSKonstantin Belousov dmar_find_hpet(device_t dev, uint16_t *rid)
8660a110d5bSKonstantin Belousov {
867b08d332dSKonstantin Belousov struct dmar_unit *unit;
8680a110d5bSKonstantin Belousov
869b08d332dSKonstantin Belousov unit = dmar_find_nonpci(hpet_get_uid(dev), ACPI_DMAR_SCOPE_TYPE_HPET,
870b08d332dSKonstantin Belousov rid);
871b08d332dSKonstantin Belousov if (unit != NULL)
872b08d332dSKonstantin Belousov iommu_device_set_iommu_prop(dev, unit->iommu.dev);
873b08d332dSKonstantin Belousov return (unit);
8740a110d5bSKonstantin Belousov }
8750a110d5bSKonstantin Belousov
8760a110d5bSKonstantin Belousov struct dmar_unit *
dmar_find_ioapic(u_int apic_id,uint16_t * rid)8770a110d5bSKonstantin Belousov dmar_find_ioapic(u_int apic_id, uint16_t *rid)
8780a110d5bSKonstantin Belousov {
879b08d332dSKonstantin Belousov struct dmar_unit *unit;
880b08d332dSKonstantin Belousov device_t apic_dev;
8810a110d5bSKonstantin Belousov
882b08d332dSKonstantin Belousov unit = dmar_find_nonpci(apic_id, ACPI_DMAR_SCOPE_TYPE_IOAPIC, rid);
883b08d332dSKonstantin Belousov if (unit != NULL) {
884b08d332dSKonstantin Belousov apic_dev = ioapic_get_dev(apic_id);
885b08d332dSKonstantin Belousov if (apic_dev != NULL)
886b08d332dSKonstantin Belousov iommu_device_set_iommu_prop(apic_dev, unit->iommu.dev);
887b08d332dSKonstantin Belousov }
888b08d332dSKonstantin Belousov return (unit);
8890a110d5bSKonstantin Belousov }
8900a110d5bSKonstantin Belousov
89186be9f0dSKonstantin Belousov struct rmrr_iter_args {
8921abfd355SKonstantin Belousov struct dmar_domain *domain;
89386be9f0dSKonstantin Belousov int dev_domain;
89486be9f0dSKonstantin Belousov int dev_busno;
895f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *dev_path;
89686be9f0dSKonstantin Belousov int dev_path_len;
89759e37c8aSRuslan Bukin struct iommu_map_entries_tailq *rmrr_entries;
89886be9f0dSKonstantin Belousov };
89986be9f0dSKonstantin Belousov
90086be9f0dSKonstantin Belousov static int
dmar_rmrr_iter(ACPI_DMAR_HEADER * dmarh,void * arg)90186be9f0dSKonstantin Belousov dmar_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
90286be9f0dSKonstantin Belousov {
90386be9f0dSKonstantin Belousov struct rmrr_iter_args *ria;
90486be9f0dSKonstantin Belousov ACPI_DMAR_RESERVED_MEMORY *resmem;
90586be9f0dSKonstantin Belousov ACPI_DMAR_DEVICE_SCOPE *devscope;
90659e37c8aSRuslan Bukin struct iommu_map_entry *entry;
90786be9f0dSKonstantin Belousov char *ptr, *ptrend;
90886be9f0dSKonstantin Belousov int match;
90986be9f0dSKonstantin Belousov
91024e38af6SKonstantin Belousov if (!dmar_rmrr_enable)
91124e38af6SKonstantin Belousov return (1);
91224e38af6SKonstantin Belousov
91386be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY)
91486be9f0dSKonstantin Belousov return (1);
91586be9f0dSKonstantin Belousov
91686be9f0dSKonstantin Belousov ria = arg;
91786be9f0dSKonstantin Belousov resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh;
91886be9f0dSKonstantin Belousov if (resmem->Segment != ria->dev_domain)
91986be9f0dSKonstantin Belousov return (1);
92086be9f0dSKonstantin Belousov
92186be9f0dSKonstantin Belousov ptr = (char *)resmem + sizeof(*resmem);
92286be9f0dSKonstantin Belousov ptrend = (char *)resmem + resmem->Header.Length;
92386be9f0dSKonstantin Belousov for (;;) {
92486be9f0dSKonstantin Belousov if (ptr >= ptrend)
92586be9f0dSKonstantin Belousov break;
92686be9f0dSKonstantin Belousov devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
92786be9f0dSKonstantin Belousov ptr += devscope->Length;
928f9feb091SKonstantin Belousov match = dmar_match_devscope(devscope, ria->dev_busno,
92986be9f0dSKonstantin Belousov ria->dev_path, ria->dev_path_len);
93086be9f0dSKonstantin Belousov if (match == 1) {
93178b51754SRuslan Bukin entry = iommu_gas_alloc_entry(DOM2IODOM(ria->domain),
93215f6baf4SRuslan Bukin IOMMU_PGF_WAITOK);
93386be9f0dSKonstantin Belousov entry->start = resmem->BaseAddress;
93486be9f0dSKonstantin Belousov /* The RMRR entry end address is inclusive. */
93586be9f0dSKonstantin Belousov entry->end = resmem->EndAddress;
93686be9f0dSKonstantin Belousov TAILQ_INSERT_TAIL(ria->rmrr_entries, entry,
937db0110a5SAlan Cox dmamap_link);
93886be9f0dSKonstantin Belousov }
93986be9f0dSKonstantin Belousov }
94086be9f0dSKonstantin Belousov
94186be9f0dSKonstantin Belousov return (1);
94286be9f0dSKonstantin Belousov }
94386be9f0dSKonstantin Belousov
94486be9f0dSKonstantin Belousov void
dmar_dev_parse_rmrr(struct dmar_domain * domain,int dev_domain,int dev_busno,const void * dev_path,int dev_path_len,struct iommu_map_entries_tailq * rmrr_entries)945f9feb091SKonstantin Belousov dmar_dev_parse_rmrr(struct dmar_domain *domain, int dev_domain, int dev_busno,
946f9feb091SKonstantin Belousov const void *dev_path, int dev_path_len,
94759e37c8aSRuslan Bukin struct iommu_map_entries_tailq *rmrr_entries)
94886be9f0dSKonstantin Belousov {
94986be9f0dSKonstantin Belousov struct rmrr_iter_args ria;
95086be9f0dSKonstantin Belousov
9511abfd355SKonstantin Belousov ria.domain = domain;
952f9feb091SKonstantin Belousov ria.dev_domain = dev_domain;
953f9feb091SKonstantin Belousov ria.dev_busno = dev_busno;
954f9feb091SKonstantin Belousov ria.dev_path = (const ACPI_DMAR_PCI_PATH *)dev_path;
955f9feb091SKonstantin Belousov ria.dev_path_len = dev_path_len;
95686be9f0dSKonstantin Belousov ria.rmrr_entries = rmrr_entries;
95786be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_rmrr_iter, &ria);
95886be9f0dSKonstantin Belousov }
95986be9f0dSKonstantin Belousov
96086be9f0dSKonstantin Belousov struct inst_rmrr_iter_args {
96186be9f0dSKonstantin Belousov struct dmar_unit *dmar;
96286be9f0dSKonstantin Belousov };
96386be9f0dSKonstantin Belousov
96486be9f0dSKonstantin Belousov static device_t
dmar_path_dev(int segment,int path_len,int busno,const ACPI_DMAR_PCI_PATH * path,uint16_t * rid)96586be9f0dSKonstantin Belousov dmar_path_dev(int segment, int path_len, int busno,
966f9feb091SKonstantin Belousov const ACPI_DMAR_PCI_PATH *path, uint16_t *rid)
96786be9f0dSKonstantin Belousov {
968f9feb091SKonstantin Belousov device_t dev;
96986be9f0dSKonstantin Belousov int i;
97086be9f0dSKonstantin Belousov
97186be9f0dSKonstantin Belousov dev = NULL;
972f9feb091SKonstantin Belousov for (i = 0; i < path_len; i++) {
97386be9f0dSKonstantin Belousov dev = pci_find_dbsf(segment, busno, path->Device,
97486be9f0dSKonstantin Belousov path->Function);
97586be9f0dSKonstantin Belousov if (i != path_len - 1) {
9761587a9dbSJohn Baldwin busno = pci_cfgregread(segment, busno, path->Device,
977f9feb091SKonstantin Belousov path->Function, PCIR_SECBUS_1, 1);
978f9feb091SKonstantin Belousov path++;
97986be9f0dSKonstantin Belousov }
98086be9f0dSKonstantin Belousov }
981f9feb091SKonstantin Belousov *rid = PCI_RID(busno, path->Device, path->Function);
98286be9f0dSKonstantin Belousov return (dev);
98386be9f0dSKonstantin Belousov }
98486be9f0dSKonstantin Belousov
98586be9f0dSKonstantin Belousov static int
dmar_inst_rmrr_iter(ACPI_DMAR_HEADER * dmarh,void * arg)98686be9f0dSKonstantin Belousov dmar_inst_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
98786be9f0dSKonstantin Belousov {
98886be9f0dSKonstantin Belousov const ACPI_DMAR_RESERVED_MEMORY *resmem;
98986be9f0dSKonstantin Belousov const ACPI_DMAR_DEVICE_SCOPE *devscope;
99086be9f0dSKonstantin Belousov struct inst_rmrr_iter_args *iria;
99186be9f0dSKonstantin Belousov const char *ptr, *ptrend;
99286be9f0dSKonstantin Belousov device_t dev;
993f9feb091SKonstantin Belousov struct dmar_unit *unit;
994f9feb091SKonstantin Belousov int dev_path_len;
995f9feb091SKonstantin Belousov uint16_t rid;
996f9feb091SKonstantin Belousov
997f9feb091SKonstantin Belousov iria = arg;
99886be9f0dSKonstantin Belousov
99924e38af6SKonstantin Belousov if (!dmar_rmrr_enable)
100024e38af6SKonstantin Belousov return (1);
100124e38af6SKonstantin Belousov
100286be9f0dSKonstantin Belousov if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY)
100386be9f0dSKonstantin Belousov return (1);
100486be9f0dSKonstantin Belousov
100586be9f0dSKonstantin Belousov resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh;
100686be9f0dSKonstantin Belousov if (resmem->Segment != iria->dmar->segment)
100786be9f0dSKonstantin Belousov return (1);
100886be9f0dSKonstantin Belousov
100933552193SDimitry Andric ptr = (const char *)resmem + sizeof(*resmem);
101033552193SDimitry Andric ptrend = (const char *)resmem + resmem->Header.Length;
101186be9f0dSKonstantin Belousov for (;;) {
101286be9f0dSKonstantin Belousov if (ptr >= ptrend)
101386be9f0dSKonstantin Belousov break;
101433552193SDimitry Andric devscope = (const ACPI_DMAR_DEVICE_SCOPE *)ptr;
101586be9f0dSKonstantin Belousov ptr += devscope->Length;
101686be9f0dSKonstantin Belousov /* XXXKIB bridge */
101786be9f0dSKonstantin Belousov if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT)
101886be9f0dSKonstantin Belousov continue;
1019f9feb091SKonstantin Belousov rid = 0;
1020f9feb091SKonstantin Belousov dev_path_len = (devscope->Length -
1021f9feb091SKonstantin Belousov sizeof(ACPI_DMAR_DEVICE_SCOPE)) / 2;
1022f9feb091SKonstantin Belousov dev = dmar_path_dev(resmem->Segment, dev_path_len,
1023f9feb091SKonstantin Belousov devscope->Bus,
1024f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1), &rid);
102586be9f0dSKonstantin Belousov if (dev == NULL) {
1026f9feb091SKonstantin Belousov if (bootverbose) {
1027f9feb091SKonstantin Belousov printf("dmar%d no dev found for RMRR "
1028f9feb091SKonstantin Belousov "[%#jx, %#jx] rid %#x scope path ",
102959e37c8aSRuslan Bukin iria->dmar->iommu.unit,
10302d8bfbdcSKonstantin Belousov (uintmax_t)resmem->BaseAddress,
10312d8bfbdcSKonstantin Belousov (uintmax_t)resmem->EndAddress,
1032f9feb091SKonstantin Belousov rid);
1033f9feb091SKonstantin Belousov dmar_print_path(devscope->Bus, dev_path_len,
1034f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1));
1035f9feb091SKonstantin Belousov printf("\n");
1036f9feb091SKonstantin Belousov }
1037f9feb091SKonstantin Belousov unit = dmar_find_by_scope(resmem->Segment,
1038f9feb091SKonstantin Belousov devscope->Bus,
1039f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1),
1040f9feb091SKonstantin Belousov dev_path_len);
1041f9feb091SKonstantin Belousov if (iria->dmar != unit)
104286be9f0dSKonstantin Belousov continue;
1043f9feb091SKonstantin Belousov dmar_get_ctx_for_devpath(iria->dmar, rid,
1044f9feb091SKonstantin Belousov resmem->Segment, devscope->Bus,
1045f9feb091SKonstantin Belousov (const ACPI_DMAR_PCI_PATH *)(devscope + 1),
1046f9feb091SKonstantin Belousov dev_path_len, false, true);
1047f9feb091SKonstantin Belousov } else {
1048f9feb091SKonstantin Belousov unit = dmar_find(dev, false);
1049f9feb091SKonstantin Belousov if (iria->dmar != unit)
105086be9f0dSKonstantin Belousov continue;
105159e37c8aSRuslan Bukin iommu_instantiate_ctx(&(iria)->dmar->iommu,
105259e37c8aSRuslan Bukin dev, true);
105386be9f0dSKonstantin Belousov }
1054f9feb091SKonstantin Belousov }
105586be9f0dSKonstantin Belousov
105686be9f0dSKonstantin Belousov return (1);
105786be9f0dSKonstantin Belousov
105886be9f0dSKonstantin Belousov }
105986be9f0dSKonstantin Belousov
1060*512bb0daSKonstantin Belousov int
dmar_is_running(void)1061*512bb0daSKonstantin Belousov dmar_is_running(void)
1062*512bb0daSKonstantin Belousov {
1063*512bb0daSKonstantin Belousov
1064*512bb0daSKonstantin Belousov return (dmar_running ? 0 : ENXIO);
1065*512bb0daSKonstantin Belousov }
1066*512bb0daSKonstantin Belousov
106786be9f0dSKonstantin Belousov /*
106886be9f0dSKonstantin Belousov * Pre-create all contexts for the DMAR which have RMRR entries.
106986be9f0dSKonstantin Belousov */
107086be9f0dSKonstantin Belousov int
dmar_instantiate_rmrr_ctxs(struct iommu_unit * unit)107159e37c8aSRuslan Bukin dmar_instantiate_rmrr_ctxs(struct iommu_unit *unit)
107286be9f0dSKonstantin Belousov {
107359e37c8aSRuslan Bukin struct dmar_unit *dmar;
107486be9f0dSKonstantin Belousov struct inst_rmrr_iter_args iria;
107586be9f0dSKonstantin Belousov int error;
107686be9f0dSKonstantin Belousov
107778b51754SRuslan Bukin dmar = IOMMU2DMAR(unit);
107859e37c8aSRuslan Bukin
107986be9f0dSKonstantin Belousov if (!dmar_barrier_enter(dmar, DMAR_BARRIER_RMRR))
108086be9f0dSKonstantin Belousov return (0);
108186be9f0dSKonstantin Belousov
108286be9f0dSKonstantin Belousov error = 0;
108386be9f0dSKonstantin Belousov iria.dmar = dmar;
108486be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_inst_rmrr_iter, &iria);
108586be9f0dSKonstantin Belousov DMAR_LOCK(dmar);
10861abfd355SKonstantin Belousov if (!LIST_EMPTY(&dmar->domains)) {
108786be9f0dSKonstantin Belousov KASSERT((dmar->hw_gcmd & DMAR_GCMD_TE) == 0,
108886be9f0dSKonstantin Belousov ("dmar%d: RMRR not handled but translation is already enabled",
108959e37c8aSRuslan Bukin dmar->iommu.unit));
109006e6ca6dSKornel Duleba error = dmar_disable_protected_regions(dmar);
109106e6ca6dSKornel Duleba if (error != 0)
109206e6ca6dSKornel Duleba printf("dmar%d: Failed to disable protected regions\n",
109306e6ca6dSKornel Duleba dmar->iommu.unit);
109486be9f0dSKonstantin Belousov error = dmar_enable_translation(dmar);
1095f9feb091SKonstantin Belousov if (bootverbose) {
1096f9feb091SKonstantin Belousov if (error == 0) {
1097f9feb091SKonstantin Belousov printf("dmar%d: enabled translation\n",
109859e37c8aSRuslan Bukin dmar->iommu.unit);
1099f9feb091SKonstantin Belousov } else {
1100f9feb091SKonstantin Belousov printf("dmar%d: enabling translation failed, "
110159e37c8aSRuslan Bukin "error %d\n", dmar->iommu.unit, error);
1102f9feb091SKonstantin Belousov }
1103f9feb091SKonstantin Belousov }
110486be9f0dSKonstantin Belousov }
110586be9f0dSKonstantin Belousov dmar_barrier_exit(dmar, DMAR_BARRIER_RMRR);
110686be9f0dSKonstantin Belousov return (error);
110786be9f0dSKonstantin Belousov }
110886be9f0dSKonstantin Belousov
110986be9f0dSKonstantin Belousov #ifdef DDB
111086be9f0dSKonstantin Belousov #include <ddb/ddb.h>
111186be9f0dSKonstantin Belousov #include <ddb/db_lex.h>
111286be9f0dSKonstantin Belousov
111386be9f0dSKonstantin Belousov static void
dmar_print_domain(struct dmar_domain * domain,bool show_mappings)11141abfd355SKonstantin Belousov dmar_print_domain(struct dmar_domain *domain, bool show_mappings)
11151abfd355SKonstantin Belousov {
111662ad310cSRuslan Bukin struct iommu_domain *iodom;
11171abfd355SKonstantin Belousov
111878b51754SRuslan Bukin iodom = DOM2IODOM(domain);
111962ad310cSRuslan Bukin
11201abfd355SKonstantin Belousov db_printf(
11211abfd355SKonstantin Belousov " @%p dom %d mgaw %d agaw %d pglvl %d end %jx refs %d\n"
11221abfd355SKonstantin Belousov " ctx_cnt %d flags %x pgobj %p map_ents %u\n",
11231abfd355SKonstantin Belousov domain, domain->domain, domain->mgaw, domain->agaw, domain->pglvl,
112462ad310cSRuslan Bukin (uintmax_t)domain->iodom.end, domain->refs, domain->ctx_cnt,
112562ad310cSRuslan Bukin domain->iodom.flags, domain->pgtbl_obj, domain->iodom.entries_cnt);
1126c9e22c74SKonstantin Belousov
1127c9e22c74SKonstantin Belousov iommu_db_domain_print_contexts(iodom);
1128c9e22c74SKonstantin Belousov
1129c9e22c74SKonstantin Belousov if (show_mappings)
1130c9e22c74SKonstantin Belousov iommu_db_domain_print_mappings(iodom);
113186be9f0dSKonstantin Belousov }
113286be9f0dSKonstantin Belousov
DB_SHOW_COMMAND_FLAGS(dmar_domain,db_dmar_print_domain,CS_OWN)1133258958b3SMitchell Horne DB_SHOW_COMMAND_FLAGS(dmar_domain, db_dmar_print_domain, CS_OWN)
113486be9f0dSKonstantin Belousov {
113586be9f0dSKonstantin Belousov struct dmar_unit *unit;
11361abfd355SKonstantin Belousov struct dmar_domain *domain;
1137e9d948cfSKonstantin Belousov struct iommu_ctx *ctx;
113886be9f0dSKonstantin Belousov bool show_mappings, valid;
11391abfd355SKonstantin Belousov int pci_domain, bus, device, function, i, t;
114086be9f0dSKonstantin Belousov db_expr_t radix;
114186be9f0dSKonstantin Belousov
114286be9f0dSKonstantin Belousov valid = false;
114386be9f0dSKonstantin Belousov radix = db_radix;
114486be9f0dSKonstantin Belousov db_radix = 10;
114586be9f0dSKonstantin Belousov t = db_read_token();
114686be9f0dSKonstantin Belousov if (t == tSLASH) {
114786be9f0dSKonstantin Belousov t = db_read_token();
114886be9f0dSKonstantin Belousov if (t != tIDENT) {
114986be9f0dSKonstantin Belousov db_printf("Bad modifier\n");
115086be9f0dSKonstantin Belousov db_radix = radix;
115186be9f0dSKonstantin Belousov db_skip_to_eol();
115286be9f0dSKonstantin Belousov return;
115386be9f0dSKonstantin Belousov }
115486be9f0dSKonstantin Belousov show_mappings = strchr(db_tok_string, 'm') != NULL;
115586be9f0dSKonstantin Belousov t = db_read_token();
1156f7f5706fSDimitry Andric } else {
1157f7f5706fSDimitry Andric show_mappings = false;
115886be9f0dSKonstantin Belousov }
115986be9f0dSKonstantin Belousov if (t == tNUMBER) {
11601abfd355SKonstantin Belousov pci_domain = db_tok_number;
116186be9f0dSKonstantin Belousov t = db_read_token();
116286be9f0dSKonstantin Belousov if (t == tNUMBER) {
116386be9f0dSKonstantin Belousov bus = db_tok_number;
116486be9f0dSKonstantin Belousov t = db_read_token();
116586be9f0dSKonstantin Belousov if (t == tNUMBER) {
116686be9f0dSKonstantin Belousov device = db_tok_number;
116786be9f0dSKonstantin Belousov t = db_read_token();
116886be9f0dSKonstantin Belousov if (t == tNUMBER) {
116986be9f0dSKonstantin Belousov function = db_tok_number;
117086be9f0dSKonstantin Belousov valid = true;
117186be9f0dSKonstantin Belousov }
117286be9f0dSKonstantin Belousov }
117386be9f0dSKonstantin Belousov }
117486be9f0dSKonstantin Belousov }
117586be9f0dSKonstantin Belousov db_radix = radix;
117686be9f0dSKonstantin Belousov db_skip_to_eol();
117786be9f0dSKonstantin Belousov if (!valid) {
11781abfd355SKonstantin Belousov db_printf("usage: show dmar_domain [/m] "
117986be9f0dSKonstantin Belousov "<domain> <bus> <device> <func>\n");
118086be9f0dSKonstantin Belousov return;
118186be9f0dSKonstantin Belousov }
118286be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) {
118386be9f0dSKonstantin Belousov unit = device_get_softc(dmar_devs[i]);
11841abfd355SKonstantin Belousov LIST_FOREACH(domain, &unit->domains, link) {
1185e9d948cfSKonstantin Belousov LIST_FOREACH(ctx, &domain->iodom.contexts, link) {
11861abfd355SKonstantin Belousov if (pci_domain == unit->segment &&
1187e9d948cfSKonstantin Belousov bus == pci_get_bus(ctx->tag->owner) &&
1188e9d948cfSKonstantin Belousov device == pci_get_slot(ctx->tag->owner) &&
1189e9d948cfSKonstantin Belousov function == pci_get_function(ctx->tag->
1190e9d948cfSKonstantin Belousov owner)) {
11911abfd355SKonstantin Belousov dmar_print_domain(domain,
11921abfd355SKonstantin Belousov show_mappings);
119386be9f0dSKonstantin Belousov goto out;
119486be9f0dSKonstantin Belousov }
119586be9f0dSKonstantin Belousov }
119686be9f0dSKonstantin Belousov }
11971abfd355SKonstantin Belousov }
119886be9f0dSKonstantin Belousov out:;
119986be9f0dSKonstantin Belousov }
120086be9f0dSKonstantin Belousov
120186be9f0dSKonstantin Belousov static void
dmar_print_one(int idx,bool show_domains,bool show_mappings)12021abfd355SKonstantin Belousov dmar_print_one(int idx, bool show_domains, bool show_mappings)
120386be9f0dSKonstantin Belousov {
120486be9f0dSKonstantin Belousov struct dmar_unit *unit;
12051abfd355SKonstantin Belousov struct dmar_domain *domain;
120686be9f0dSKonstantin Belousov int i, frir;
120786be9f0dSKonstantin Belousov
120886be9f0dSKonstantin Belousov unit = device_get_softc(dmar_devs[idx]);
120959e37c8aSRuslan Bukin db_printf("dmar%d at %p, root at 0x%jx, ver 0x%x\n", unit->iommu.unit,
121059e37c8aSRuslan Bukin unit, dmar_read8(unit, DMAR_RTADDR_REG),
121159e37c8aSRuslan Bukin dmar_read4(unit, DMAR_VER_REG));
121286be9f0dSKonstantin Belousov db_printf("cap 0x%jx ecap 0x%jx gsts 0x%x fsts 0x%x fectl 0x%x\n",
121386be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_CAP_REG),
121486be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_ECAP_REG),
121586be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_GSTS_REG),
121686be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FSTS_REG),
121786be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FECTL_REG));
12181abfd355SKonstantin Belousov if (unit->ir_enabled) {
12191abfd355SKonstantin Belousov db_printf("ir is enabled; IRT @%p phys 0x%jx maxcnt %d\n",
12201abfd355SKonstantin Belousov unit->irt, (uintmax_t)unit->irt_phys, unit->irte_cnt);
12211abfd355SKonstantin Belousov }
122286be9f0dSKonstantin Belousov db_printf("fed 0x%x fea 0x%x feua 0x%x\n",
122386be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEDATA_REG),
122486be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEADDR_REG),
122586be9f0dSKonstantin Belousov dmar_read4(unit, DMAR_FEUADDR_REG));
122686be9f0dSKonstantin Belousov db_printf("primary fault log:\n");
122786be9f0dSKonstantin Belousov for (i = 0; i < DMAR_CAP_NFR(unit->hw_cap); i++) {
122886be9f0dSKonstantin Belousov frir = (DMAR_CAP_FRO(unit->hw_cap) + i) * 16;
122986be9f0dSKonstantin Belousov db_printf(" %d at 0x%x: %jx %jx\n", i, frir,
123086be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, frir),
123186be9f0dSKonstantin Belousov (uintmax_t)dmar_read8(unit, frir + 8));
123286be9f0dSKonstantin Belousov }
123368eeb96aSKonstantin Belousov if (DMAR_HAS_QI(unit)) {
123468eeb96aSKonstantin Belousov db_printf("ied 0x%x iea 0x%x ieua 0x%x\n",
123568eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEDATA_REG),
123668eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEADDR_REG),
123768eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IEUADDR_REG));
123868eeb96aSKonstantin Belousov if (unit->qi_enabled) {
123968eeb96aSKonstantin Belousov db_printf("qi is enabled: queue @0x%jx (IQA 0x%jx) "
124068eeb96aSKonstantin Belousov "size 0x%jx\n"
124168eeb96aSKonstantin Belousov " head 0x%x tail 0x%x avail 0x%x status 0x%x ctrl 0x%x\n"
1242fc8da73bSKonstantin Belousov " hw compl 0x%jx@%p/phys@%jx next seq 0x%x gen 0x%x\n",
1243ad794e6dSKonstantin Belousov (uintmax_t)unit->x86c.inv_queue,
124468eeb96aSKonstantin Belousov (uintmax_t)dmar_read8(unit, DMAR_IQA_REG),
1245ad794e6dSKonstantin Belousov (uintmax_t)unit->x86c.inv_queue_size,
124668eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IQH_REG),
124768eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IQT_REG),
1248ad794e6dSKonstantin Belousov unit->x86c.inv_queue_avail,
124968eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_ICS_REG),
125068eeb96aSKonstantin Belousov dmar_read4(unit, DMAR_IECTL_REG),
1251fc8da73bSKonstantin Belousov (uintmax_t)unit->x86c.inv_waitd_seq_hw,
1252ad794e6dSKonstantin Belousov &unit->x86c.inv_waitd_seq_hw,
1253ad794e6dSKonstantin Belousov (uintmax_t)unit->x86c.inv_waitd_seq_hw_phys,
1254ad794e6dSKonstantin Belousov unit->x86c.inv_waitd_seq,
1255ad794e6dSKonstantin Belousov unit->x86c.inv_waitd_gen);
125668eeb96aSKonstantin Belousov } else {
125768eeb96aSKonstantin Belousov db_printf("qi is disabled\n");
125868eeb96aSKonstantin Belousov }
125968eeb96aSKonstantin Belousov }
12601abfd355SKonstantin Belousov if (show_domains) {
12611abfd355SKonstantin Belousov db_printf("domains:\n");
12621abfd355SKonstantin Belousov LIST_FOREACH(domain, &unit->domains, link) {
12631abfd355SKonstantin Belousov dmar_print_domain(domain, show_mappings);
126486be9f0dSKonstantin Belousov if (db_pager_quit)
126586be9f0dSKonstantin Belousov break;
126686be9f0dSKonstantin Belousov }
126786be9f0dSKonstantin Belousov }
126886be9f0dSKonstantin Belousov }
126986be9f0dSKonstantin Belousov
DB_SHOW_COMMAND(dmar,db_dmar_print)127086be9f0dSKonstantin Belousov DB_SHOW_COMMAND(dmar, db_dmar_print)
127186be9f0dSKonstantin Belousov {
12721abfd355SKonstantin Belousov bool show_domains, show_mappings;
127386be9f0dSKonstantin Belousov
12741abfd355SKonstantin Belousov show_domains = strchr(modif, 'd') != NULL;
127586be9f0dSKonstantin Belousov show_mappings = strchr(modif, 'm') != NULL;
127686be9f0dSKonstantin Belousov if (!have_addr) {
12771abfd355SKonstantin Belousov db_printf("usage: show dmar [/d] [/m] index\n");
127886be9f0dSKonstantin Belousov return;
127986be9f0dSKonstantin Belousov }
12801abfd355SKonstantin Belousov dmar_print_one((int)addr, show_domains, show_mappings);
128186be9f0dSKonstantin Belousov }
128286be9f0dSKonstantin Belousov
DB_SHOW_ALL_COMMAND(dmars,db_show_all_dmars)128386be9f0dSKonstantin Belousov DB_SHOW_ALL_COMMAND(dmars, db_show_all_dmars)
128486be9f0dSKonstantin Belousov {
128586be9f0dSKonstantin Belousov int i;
12861abfd355SKonstantin Belousov bool show_domains, show_mappings;
128786be9f0dSKonstantin Belousov
12881abfd355SKonstantin Belousov show_domains = strchr(modif, 'd') != NULL;
128986be9f0dSKonstantin Belousov show_mappings = strchr(modif, 'm') != NULL;
129086be9f0dSKonstantin Belousov
129186be9f0dSKonstantin Belousov for (i = 0; i < dmar_devcnt; i++) {
12921abfd355SKonstantin Belousov dmar_print_one(i, show_domains, show_mappings);
129386be9f0dSKonstantin Belousov if (db_pager_quit)
129486be9f0dSKonstantin Belousov break;
129586be9f0dSKonstantin Belousov }
129686be9f0dSKonstantin Belousov }
129786be9f0dSKonstantin Belousov #endif
129859e37c8aSRuslan Bukin
129965b133e5SKonstantin Belousov static struct iommu_unit *
dmar_find_method(device_t dev,bool verbose)130065b133e5SKonstantin Belousov dmar_find_method(device_t dev, bool verbose)
130159e37c8aSRuslan Bukin {
130259e37c8aSRuslan Bukin struct dmar_unit *dmar;
130359e37c8aSRuslan Bukin
130459e37c8aSRuslan Bukin dmar = dmar_find(dev, verbose);
130559e37c8aSRuslan Bukin return (&dmar->iommu);
130659e37c8aSRuslan Bukin }
130765b133e5SKonstantin Belousov
1308ad794e6dSKonstantin Belousov static struct x86_unit_common *
dmar_get_x86_common(struct iommu_unit * unit)1309ad794e6dSKonstantin Belousov dmar_get_x86_common(struct iommu_unit *unit)
1310ad794e6dSKonstantin Belousov {
1311ad794e6dSKonstantin Belousov struct dmar_unit *dmar;
1312ad794e6dSKonstantin Belousov
1313ad794e6dSKonstantin Belousov dmar = IOMMU2DMAR(unit);
1314ad794e6dSKonstantin Belousov return (&dmar->x86c);
1315ad794e6dSKonstantin Belousov }
1316ad794e6dSKonstantin Belousov
1317ba33e74cSKonstantin Belousov static void
dmar_unit_pre_instantiate_ctx(struct iommu_unit * unit)1318ba33e74cSKonstantin Belousov dmar_unit_pre_instantiate_ctx(struct iommu_unit *unit)
1319ba33e74cSKonstantin Belousov {
1320ba33e74cSKonstantin Belousov dmar_quirks_pre_use(unit);
1321ba33e74cSKonstantin Belousov dmar_instantiate_rmrr_ctxs(unit);
1322ba33e74cSKonstantin Belousov }
1323ba33e74cSKonstantin Belousov
132465b133e5SKonstantin Belousov static struct x86_iommu dmar_x86_iommu = {
1325ad794e6dSKonstantin Belousov .get_x86_common = dmar_get_x86_common,
1326ba33e74cSKonstantin Belousov .unit_pre_instantiate_ctx = dmar_unit_pre_instantiate_ctx,
132765b133e5SKonstantin Belousov .domain_unload_entry = dmar_domain_unload_entry,
132865b133e5SKonstantin Belousov .domain_unload = dmar_domain_unload,
132965b133e5SKonstantin Belousov .get_ctx = dmar_get_ctx,
133065b133e5SKonstantin Belousov .free_ctx_locked = dmar_free_ctx_locked_method,
133165b133e5SKonstantin Belousov .find = dmar_find_method,
133265b133e5SKonstantin Belousov .alloc_msi_intr = dmar_alloc_msi_intr,
133365b133e5SKonstantin Belousov .map_msi_intr = dmar_map_msi_intr,
133465b133e5SKonstantin Belousov .unmap_msi_intr = dmar_unmap_msi_intr,
133565b133e5SKonstantin Belousov .map_ioapic_intr = dmar_map_ioapic_intr,
133665b133e5SKonstantin Belousov .unmap_ioapic_intr = dmar_unmap_ioapic_intr,
133765b133e5SKonstantin Belousov };
133865b133e5SKonstantin Belousov
133965b133e5SKonstantin Belousov static void
x86_iommu_set_intel(void * arg __unused)134065b133e5SKonstantin Belousov x86_iommu_set_intel(void *arg __unused)
134165b133e5SKonstantin Belousov {
134265b133e5SKonstantin Belousov if (cpu_vendor_id == CPU_VENDOR_INTEL)
134365b133e5SKonstantin Belousov set_x86_iommu(&dmar_x86_iommu);
134465b133e5SKonstantin Belousov }
134565b133e5SKonstantin Belousov
134665b133e5SKonstantin Belousov SYSINIT(x86_iommu, SI_SUB_TUNABLES, SI_ORDER_ANY, x86_iommu_set_intel, NULL);
1347