xref: /freebsd/sys/x86/iommu/intel_drv.c (revision c9e22c749c0f3950b4526f630f6853ab104ab52f)
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>
4186be9f0dSKonstantin Belousov #include <sys/kernel.h>
4286be9f0dSKonstantin Belousov #include <sys/lock.h>
4386be9f0dSKonstantin Belousov #include <sys/malloc.h>
4486be9f0dSKonstantin Belousov #include <sys/memdesc.h>
4586be9f0dSKonstantin Belousov #include <sys/module.h>
46e2e050c8SConrad Meyer #include <sys/mutex.h>
4786be9f0dSKonstantin Belousov #include <sys/rman.h>
4886be9f0dSKonstantin Belousov #include <sys/rwlock.h>
4986be9f0dSKonstantin Belousov #include <sys/smp.h>
5086be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
5186be9f0dSKonstantin Belousov #include <sys/tree.h>
520a110d5bSKonstantin Belousov #include <sys/vmem.h>
5386be9f0dSKonstantin Belousov #include <vm/vm.h>
5486be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
5586be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
5686be9f0dSKonstantin Belousov #include <vm/vm_object.h>
5786be9f0dSKonstantin Belousov #include <vm/vm_page.h>
5886be9f0dSKonstantin Belousov #include <vm/vm_pager.h>
5986be9f0dSKonstantin Belousov #include <vm/vm_map.h>
60c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/acpi.h>
61c8597a1fSRuslan Bukin #include <contrib/dev/acpica/include/accommon.h>
62c8597a1fSRuslan Bukin #include <dev/acpica/acpivar.h>
630a110d5bSKonstantin Belousov #include <dev/pci/pcireg.h>
6486be9f0dSKonstantin Belousov #include <dev/pci/pcivar.h>
65c8597a1fSRuslan Bukin #include <machine/bus.h>
66c8597a1fSRuslan Bukin #include <machine/pci_cfgreg.h>
6765b133e5SKonstantin Belousov #include <machine/md_var.h>
6865b133e5SKonstantin Belousov #include <machine/cputypes.h>
69c8597a1fSRuslan Bukin #include <x86/include/busdma_impl.h>
70c8597a1fSRuslan Bukin #include <dev/iommu/busdma_iommu.h>
71c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h>
7240d951bcSKonstantin Belousov #include <x86/iommu/x86_iommu.h>
73685666aaSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
7486be9f0dSKonstantin Belousov 
7586be9f0dSKonstantin Belousov #ifdef DEV_APIC
7686be9f0dSKonstantin Belousov #include "pcib_if.h"
77fd15fee1SKonstantin Belousov #include <machine/intr_machdep.h>
78fd15fee1SKonstantin Belousov #include <x86/apicreg.h>
79fd15fee1SKonstantin Belousov #include <x86/apicvar.h>
8086be9f0dSKonstantin Belousov #endif
8186be9f0dSKonstantin Belousov 
8268eeb96aSKonstantin Belousov #define	DMAR_FAULT_IRQ_RID	0
8368eeb96aSKonstantin Belousov #define	DMAR_QI_IRQ_RID		1
8468eeb96aSKonstantin Belousov #define	DMAR_REG_RID		2
8586be9f0dSKonstantin Belousov 
8686be9f0dSKonstantin Belousov static device_t *dmar_devs;
8786be9f0dSKonstantin Belousov static int dmar_devcnt;
8886be9f0dSKonstantin Belousov 
8986be9f0dSKonstantin Belousov typedef int (*dmar_iter_t)(ACPI_DMAR_HEADER *, void *);
9086be9f0dSKonstantin Belousov 
9186be9f0dSKonstantin Belousov static void
9286be9f0dSKonstantin Belousov dmar_iterate_tbl(dmar_iter_t iter, void *arg)
9386be9f0dSKonstantin Belousov {
9486be9f0dSKonstantin Belousov 	ACPI_TABLE_DMAR *dmartbl;
9586be9f0dSKonstantin Belousov 	ACPI_DMAR_HEADER *dmarh;
9686be9f0dSKonstantin Belousov 	char *ptr, *ptrend;
9786be9f0dSKonstantin Belousov 	ACPI_STATUS status;
9886be9f0dSKonstantin Belousov 
9986be9f0dSKonstantin Belousov 	status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl);
10086be9f0dSKonstantin Belousov 	if (ACPI_FAILURE(status))
10186be9f0dSKonstantin Belousov 		return;
10286be9f0dSKonstantin Belousov 	ptr = (char *)dmartbl + sizeof(*dmartbl);
10386be9f0dSKonstantin Belousov 	ptrend = (char *)dmartbl + dmartbl->Header.Length;
10486be9f0dSKonstantin Belousov 	for (;;) {
10586be9f0dSKonstantin Belousov 		if (ptr >= ptrend)
10686be9f0dSKonstantin Belousov 			break;
10786be9f0dSKonstantin Belousov 		dmarh = (ACPI_DMAR_HEADER *)ptr;
10886be9f0dSKonstantin Belousov 		if (dmarh->Length <= 0) {
10986be9f0dSKonstantin Belousov 			printf("dmar_identify: corrupted DMAR table, l %d\n",
11086be9f0dSKonstantin Belousov 			    dmarh->Length);
11186be9f0dSKonstantin Belousov 			break;
11286be9f0dSKonstantin Belousov 		}
11386be9f0dSKonstantin Belousov 		ptr += dmarh->Length;
11486be9f0dSKonstantin Belousov 		if (!iter(dmarh, arg))
11586be9f0dSKonstantin Belousov 			break;
11686be9f0dSKonstantin Belousov 	}
1173dd3c450SKonstantin Belousov 	AcpiPutTable((ACPI_TABLE_HEADER *)dmartbl);
11886be9f0dSKonstantin Belousov }
11986be9f0dSKonstantin Belousov 
12086be9f0dSKonstantin Belousov struct find_iter_args {
12186be9f0dSKonstantin Belousov 	int i;
12286be9f0dSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *res;
12386be9f0dSKonstantin Belousov };
12486be9f0dSKonstantin Belousov 
12586be9f0dSKonstantin Belousov static int
12686be9f0dSKonstantin Belousov dmar_find_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
12786be9f0dSKonstantin Belousov {
12886be9f0dSKonstantin Belousov 	struct find_iter_args *fia;
12986be9f0dSKonstantin Belousov 
13086be9f0dSKonstantin Belousov 	if (dmarh->Type != ACPI_DMAR_TYPE_HARDWARE_UNIT)
13186be9f0dSKonstantin Belousov 		return (1);
13286be9f0dSKonstantin Belousov 
13386be9f0dSKonstantin Belousov 	fia = arg;
13486be9f0dSKonstantin Belousov 	if (fia->i == 0) {
13586be9f0dSKonstantin Belousov 		fia->res = (ACPI_DMAR_HARDWARE_UNIT *)dmarh;
13686be9f0dSKonstantin Belousov 		return (0);
13786be9f0dSKonstantin Belousov 	}
13886be9f0dSKonstantin Belousov 	fia->i--;
13986be9f0dSKonstantin Belousov 	return (1);
14086be9f0dSKonstantin Belousov }
14186be9f0dSKonstantin Belousov 
14286be9f0dSKonstantin Belousov static ACPI_DMAR_HARDWARE_UNIT *
14386be9f0dSKonstantin Belousov dmar_find_by_index(int idx)
14486be9f0dSKonstantin Belousov {
14586be9f0dSKonstantin Belousov 	struct find_iter_args fia;
14686be9f0dSKonstantin Belousov 
14786be9f0dSKonstantin Belousov 	fia.i = idx;
14886be9f0dSKonstantin Belousov 	fia.res = NULL;
14986be9f0dSKonstantin Belousov 	dmar_iterate_tbl(dmar_find_iter, &fia);
15086be9f0dSKonstantin Belousov 	return (fia.res);
15186be9f0dSKonstantin Belousov }
15286be9f0dSKonstantin Belousov 
15386be9f0dSKonstantin Belousov static int
15486be9f0dSKonstantin Belousov dmar_count_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
15586be9f0dSKonstantin Belousov {
15686be9f0dSKonstantin Belousov 
15786be9f0dSKonstantin Belousov 	if (dmarh->Type == ACPI_DMAR_TYPE_HARDWARE_UNIT)
15886be9f0dSKonstantin Belousov 		dmar_devcnt++;
15986be9f0dSKonstantin Belousov 	return (1);
16086be9f0dSKonstantin Belousov }
16186be9f0dSKonstantin Belousov 
16224e38af6SKonstantin Belousov int dmar_rmrr_enable = 1;
16324e38af6SKonstantin Belousov 
1640875f3cdSEd Maste static int dmar_enable = 0;
16586be9f0dSKonstantin Belousov static void
16686be9f0dSKonstantin Belousov dmar_identify(driver_t *driver, device_t parent)
16786be9f0dSKonstantin Belousov {
16886be9f0dSKonstantin Belousov 	ACPI_TABLE_DMAR *dmartbl;
16986be9f0dSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *dmarh;
17086be9f0dSKonstantin Belousov 	ACPI_STATUS status;
17186be9f0dSKonstantin Belousov 	int i, error;
17286be9f0dSKonstantin Belousov 
17386be9f0dSKonstantin Belousov 	if (acpi_disabled("dmar"))
17486be9f0dSKonstantin Belousov 		return;
17586be9f0dSKonstantin Belousov 	TUNABLE_INT_FETCH("hw.dmar.enable", &dmar_enable);
17686be9f0dSKonstantin Belousov 	if (!dmar_enable)
17786be9f0dSKonstantin Belousov 		return;
17824e38af6SKonstantin Belousov 	TUNABLE_INT_FETCH("hw.dmar.rmrr_enable", &dmar_rmrr_enable);
17924e38af6SKonstantin Belousov 
18086be9f0dSKonstantin Belousov 	status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl);
18186be9f0dSKonstantin Belousov 	if (ACPI_FAILURE(status))
18286be9f0dSKonstantin Belousov 		return;
18386be9f0dSKonstantin Belousov 	haw = dmartbl->Width + 1;
18486be9f0dSKonstantin Belousov 	if ((1ULL << (haw + 1)) > BUS_SPACE_MAXADDR)
18540d951bcSKonstantin Belousov 		iommu_high = BUS_SPACE_MAXADDR;
18686be9f0dSKonstantin Belousov 	else
18740d951bcSKonstantin Belousov 		iommu_high = 1ULL << (haw + 1);
18886be9f0dSKonstantin Belousov 	if (bootverbose) {
18986be9f0dSKonstantin Belousov 		printf("DMAR HAW=%d flags=<%b>\n", dmartbl->Width,
19086be9f0dSKonstantin Belousov 		    (unsigned)dmartbl->Flags,
19186be9f0dSKonstantin Belousov 		    "\020\001INTR_REMAP\002X2APIC_OPT_OUT");
19286be9f0dSKonstantin Belousov 	}
1933dd3c450SKonstantin Belousov 	AcpiPutTable((ACPI_TABLE_HEADER *)dmartbl);
19486be9f0dSKonstantin Belousov 
19586be9f0dSKonstantin Belousov 	dmar_iterate_tbl(dmar_count_iter, NULL);
19686be9f0dSKonstantin Belousov 	if (dmar_devcnt == 0)
19786be9f0dSKonstantin Belousov 		return;
19886be9f0dSKonstantin Belousov 	dmar_devs = malloc(sizeof(device_t) * dmar_devcnt, M_DEVBUF,
19986be9f0dSKonstantin Belousov 	    M_WAITOK | M_ZERO);
20086be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
20186be9f0dSKonstantin Belousov 		dmarh = dmar_find_by_index(i);
20286be9f0dSKonstantin Belousov 		if (dmarh == NULL) {
20386be9f0dSKonstantin Belousov 			printf("dmar_identify: cannot find HWUNIT %d\n", i);
20486be9f0dSKonstantin Belousov 			continue;
20586be9f0dSKonstantin Belousov 		}
20686be9f0dSKonstantin Belousov 		dmar_devs[i] = BUS_ADD_CHILD(parent, 1, "dmar", i);
20786be9f0dSKonstantin Belousov 		if (dmar_devs[i] == NULL) {
20886be9f0dSKonstantin Belousov 			printf("dmar_identify: cannot create instance %d\n", i);
20986be9f0dSKonstantin Belousov 			continue;
21086be9f0dSKonstantin Belousov 		}
21186be9f0dSKonstantin Belousov 		error = bus_set_resource(dmar_devs[i], SYS_RES_MEMORY,
21286be9f0dSKonstantin Belousov 		    DMAR_REG_RID, dmarh->Address, PAGE_SIZE);
21386be9f0dSKonstantin Belousov 		if (error != 0) {
21486be9f0dSKonstantin Belousov 			printf(
21586be9f0dSKonstantin Belousov 	"dmar%d: unable to alloc register window at 0x%08jx: error %d\n",
21686be9f0dSKonstantin Belousov 			    i, (uintmax_t)dmarh->Address, error);
21786be9f0dSKonstantin Belousov 			device_delete_child(parent, dmar_devs[i]);
21886be9f0dSKonstantin Belousov 			dmar_devs[i] = NULL;
21986be9f0dSKonstantin Belousov 		}
22086be9f0dSKonstantin Belousov 	}
22186be9f0dSKonstantin Belousov }
22286be9f0dSKonstantin Belousov 
22386be9f0dSKonstantin Belousov static int
22486be9f0dSKonstantin Belousov dmar_probe(device_t dev)
22586be9f0dSKonstantin Belousov {
22686be9f0dSKonstantin Belousov 
22786be9f0dSKonstantin Belousov 	if (acpi_get_handle(dev) != NULL)
22886be9f0dSKonstantin Belousov 		return (ENXIO);
22986be9f0dSKonstantin Belousov 	device_set_desc(dev, "DMA remap");
2303100f7dfSKonstantin Belousov 	return (BUS_PROBE_NOWILDCARD);
23186be9f0dSKonstantin Belousov }
23286be9f0dSKonstantin Belousov 
23386be9f0dSKonstantin Belousov static void
23486be9f0dSKonstantin Belousov dmar_release_resources(device_t dev, struct dmar_unit *unit)
23586be9f0dSKonstantin Belousov {
23668eeb96aSKonstantin Belousov 	int i;
23786be9f0dSKonstantin Belousov 
23859e37c8aSRuslan Bukin 	iommu_fini_busdma(&unit->iommu);
2390a110d5bSKonstantin Belousov 	dmar_fini_irt(unit);
24068eeb96aSKonstantin Belousov 	dmar_fini_qi(unit);
24186be9f0dSKonstantin Belousov 	dmar_fini_fault_log(unit);
24268eeb96aSKonstantin Belousov 	for (i = 0; i < DMAR_INTR_TOTAL; i++)
2435967352aSKonstantin Belousov 		iommu_release_intr(DMAR2IOMMU(unit), i);
24486be9f0dSKonstantin Belousov 	if (unit->regs != NULL) {
24586be9f0dSKonstantin Belousov 		bus_deactivate_resource(dev, SYS_RES_MEMORY, unit->reg_rid,
24686be9f0dSKonstantin Belousov 		    unit->regs);
24786be9f0dSKonstantin Belousov 		bus_release_resource(dev, SYS_RES_MEMORY, unit->reg_rid,
24886be9f0dSKonstantin Belousov 		    unit->regs);
24986be9f0dSKonstantin Belousov 		unit->regs = NULL;
25086be9f0dSKonstantin Belousov 	}
25186be9f0dSKonstantin Belousov 	if (unit->domids != NULL) {
25286be9f0dSKonstantin Belousov 		delete_unrhdr(unit->domids);
25386be9f0dSKonstantin Belousov 		unit->domids = NULL;
25486be9f0dSKonstantin Belousov 	}
25586be9f0dSKonstantin Belousov 	if (unit->ctx_obj != NULL) {
25686be9f0dSKonstantin Belousov 		vm_object_deallocate(unit->ctx_obj);
25786be9f0dSKonstantin Belousov 		unit->ctx_obj = NULL;
25886be9f0dSKonstantin Belousov 	}
259d50403a6SKonstantin Belousov 	sysctl_ctx_free(&unit->iommu.sysctl_ctx);
26086be9f0dSKonstantin Belousov }
26186be9f0dSKonstantin Belousov 
26286be9f0dSKonstantin Belousov #ifdef DEV_APIC
26386be9f0dSKonstantin Belousov static int
26486be9f0dSKonstantin Belousov dmar_remap_intr(device_t dev, device_t child, u_int irq)
26586be9f0dSKonstantin Belousov {
26686be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
2675967352aSKonstantin Belousov 	struct iommu_msi_data *dmd;
26886be9f0dSKonstantin Belousov 	uint64_t msi_addr;
26986be9f0dSKonstantin Belousov 	uint32_t msi_data;
27068eeb96aSKonstantin Belousov 	int i, error;
27186be9f0dSKonstantin Belousov 
27286be9f0dSKonstantin Belousov 	unit = device_get_softc(dev);
27368eeb96aSKonstantin Belousov 	for (i = 0; i < DMAR_INTR_TOTAL; i++) {
2745967352aSKonstantin Belousov 		dmd = &unit->x86c.intrs[i];
27568eeb96aSKonstantin Belousov 		if (irq == dmd->irq) {
27668eeb96aSKonstantin Belousov 			error = PCIB_MAP_MSI(device_get_parent(
27768eeb96aSKonstantin Belousov 			    device_get_parent(dev)),
27868eeb96aSKonstantin Belousov 			    dev, irq, &msi_addr, &msi_data);
27986be9f0dSKonstantin Belousov 			if (error != 0)
28086be9f0dSKonstantin Belousov 				return (error);
28168eeb96aSKonstantin Belousov 			DMAR_LOCK(unit);
2825967352aSKonstantin Belousov 			dmd->msi_data = msi_data;
2835967352aSKonstantin Belousov 			dmd->msi_addr = msi_addr;
2845967352aSKonstantin Belousov 			(dmd->disable_intr)(DMAR2IOMMU(unit));
2855967352aSKonstantin Belousov 			dmar_write4(unit, dmd->msi_data_reg, dmd->msi_data);
2865967352aSKonstantin Belousov 			dmar_write4(unit, dmd->msi_addr_reg, dmd->msi_addr);
2875967352aSKonstantin Belousov 			dmar_write4(unit, dmd->msi_uaddr_reg,
2885967352aSKonstantin Belousov 			    dmd->msi_addr >> 32);
2895967352aSKonstantin Belousov 			(dmd->enable_intr)(DMAR2IOMMU(unit));
29068eeb96aSKonstantin Belousov 			DMAR_UNLOCK(unit);
29186be9f0dSKonstantin Belousov 			return (0);
29286be9f0dSKonstantin Belousov 		}
29368eeb96aSKonstantin Belousov 	}
29468eeb96aSKonstantin Belousov 	return (ENOENT);
29568eeb96aSKonstantin Belousov }
29686be9f0dSKonstantin Belousov #endif
29786be9f0dSKonstantin Belousov 
29886be9f0dSKonstantin Belousov static void
29986be9f0dSKonstantin Belousov dmar_print_caps(device_t dev, struct dmar_unit *unit,
30086be9f0dSKonstantin Belousov     ACPI_DMAR_HARDWARE_UNIT *dmaru)
30186be9f0dSKonstantin Belousov {
30286be9f0dSKonstantin Belousov 	uint32_t caphi, ecaphi;
30386be9f0dSKonstantin Belousov 
30486be9f0dSKonstantin Belousov 	device_printf(dev, "regs@0x%08jx, ver=%d.%d, seg=%d, flags=<%b>\n",
30586be9f0dSKonstantin Belousov 	    (uintmax_t)dmaru->Address, DMAR_MAJOR_VER(unit->hw_ver),
30686be9f0dSKonstantin Belousov 	    DMAR_MINOR_VER(unit->hw_ver), dmaru->Segment,
30786be9f0dSKonstantin Belousov 	    dmaru->Flags, "\020\001INCLUDE_ALL_PCI");
30886be9f0dSKonstantin Belousov 	caphi = unit->hw_cap >> 32;
30986be9f0dSKonstantin Belousov 	device_printf(dev, "cap=%b,", (u_int)unit->hw_cap,
31086be9f0dSKonstantin Belousov 	    "\020\004AFL\005WBF\006PLMR\007PHMR\010CM\027ZLR\030ISOCH");
311e17c0a1eSKonstantin Belousov 	printf("%b, ", caphi, "\020\010PSI\027DWD\030DRD\031FL1GP\034PSI");
31286be9f0dSKonstantin Belousov 	printf("ndoms=%d, sagaw=%d, mgaw=%d, fro=%d, nfr=%d, superp=%d",
31386be9f0dSKonstantin Belousov 	    DMAR_CAP_ND(unit->hw_cap), DMAR_CAP_SAGAW(unit->hw_cap),
31486be9f0dSKonstantin Belousov 	    DMAR_CAP_MGAW(unit->hw_cap), DMAR_CAP_FRO(unit->hw_cap),
31586be9f0dSKonstantin Belousov 	    DMAR_CAP_NFR(unit->hw_cap), DMAR_CAP_SPS(unit->hw_cap));
31686be9f0dSKonstantin Belousov 	if ((unit->hw_cap & DMAR_CAP_PSI) != 0)
31786be9f0dSKonstantin Belousov 		printf(", mamv=%d", DMAR_CAP_MAMV(unit->hw_cap));
31886be9f0dSKonstantin Belousov 	printf("\n");
31986be9f0dSKonstantin Belousov 	ecaphi = unit->hw_ecap >> 32;
32086be9f0dSKonstantin Belousov 	device_printf(dev, "ecap=%b,", (u_int)unit->hw_ecap,
321e17c0a1eSKonstantin Belousov 	    "\020\001C\002QI\003DI\004IR\005EIM\007PT\010SC\031ECS\032MTS"
322e17c0a1eSKonstantin Belousov 	    "\033NEST\034DIS\035PASID\036PRS\037ERS\040SRS");
323e17c0a1eSKonstantin Belousov 	printf("%b, ", ecaphi, "\020\002NWFS\003EAFS");
32486be9f0dSKonstantin Belousov 	printf("mhmw=%d, iro=%d\n", DMAR_ECAP_MHMV(unit->hw_ecap),
32586be9f0dSKonstantin Belousov 	    DMAR_ECAP_IRO(unit->hw_ecap));
32686be9f0dSKonstantin Belousov }
32786be9f0dSKonstantin Belousov 
32886be9f0dSKonstantin Belousov static int
32986be9f0dSKonstantin Belousov dmar_attach(device_t dev)
33086be9f0dSKonstantin Belousov {
33186be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
33286be9f0dSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *dmaru;
3335967352aSKonstantin Belousov 	struct iommu_msi_data *dmd;
334476358b3SKonstantin Belousov 	uint64_t timeout;
33506f659c3SKornel Duleba 	int disable_pmr;
33668eeb96aSKonstantin Belousov 	int i, error;
33786be9f0dSKonstantin Belousov 
33886be9f0dSKonstantin Belousov 	unit = device_get_softc(dev);
33959e37c8aSRuslan Bukin 	unit->iommu.unit = device_get_unit(dev);
340f5931169SRuslan Bukin 	unit->iommu.dev = dev;
341d50403a6SKonstantin Belousov 	sysctl_ctx_init(&unit->iommu.sysctl_ctx);
34259e37c8aSRuslan Bukin 	dmaru = dmar_find_by_index(unit->iommu.unit);
34386be9f0dSKonstantin Belousov 	if (dmaru == NULL)
34486be9f0dSKonstantin Belousov 		return (EINVAL);
34586be9f0dSKonstantin Belousov 	unit->segment = dmaru->Segment;
34686be9f0dSKonstantin Belousov 	unit->base = dmaru->Address;
34786be9f0dSKonstantin Belousov 	unit->reg_rid = DMAR_REG_RID;
34886be9f0dSKonstantin Belousov 	unit->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
34986be9f0dSKonstantin Belousov 	    &unit->reg_rid, RF_ACTIVE);
35086be9f0dSKonstantin Belousov 	if (unit->regs == NULL) {
35186be9f0dSKonstantin Belousov 		device_printf(dev, "cannot allocate register window\n");
35245543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
35386be9f0dSKonstantin Belousov 		return (ENOMEM);
35486be9f0dSKonstantin Belousov 	}
35586be9f0dSKonstantin Belousov 	unit->hw_ver = dmar_read4(unit, DMAR_VER_REG);
35686be9f0dSKonstantin Belousov 	unit->hw_cap = dmar_read8(unit, DMAR_CAP_REG);
35786be9f0dSKonstantin Belousov 	unit->hw_ecap = dmar_read8(unit, DMAR_ECAP_REG);
35886be9f0dSKonstantin Belousov 	if (bootverbose)
35986be9f0dSKonstantin Belousov 		dmar_print_caps(dev, unit, dmaru);
36086be9f0dSKonstantin Belousov 	dmar_quirks_post_ident(unit);
36186be9f0dSKonstantin Belousov 
362476358b3SKonstantin Belousov 	timeout = dmar_get_timeout();
36312cce599SZhenlei Huang 	TUNABLE_UINT64_FETCH("hw.iommu.dmar.timeout", &timeout);
364476358b3SKonstantin Belousov 	dmar_update_timeout(timeout);
365476358b3SKonstantin Belousov 
36668eeb96aSKonstantin Belousov 	for (i = 0; i < DMAR_INTR_TOTAL; i++)
3675967352aSKonstantin Belousov 		unit->x86c.intrs[i].irq = -1;
36868eeb96aSKonstantin Belousov 
3695967352aSKonstantin Belousov 	dmd = &unit->x86c.intrs[DMAR_INTR_FAULT];
3705967352aSKonstantin Belousov 	dmd->name = "fault";
3715967352aSKonstantin Belousov 	dmd->irq_rid = DMAR_FAULT_IRQ_RID;
3725967352aSKonstantin Belousov 	dmd->handler = dmar_fault_intr;
3735967352aSKonstantin Belousov 	dmd->msi_data_reg = DMAR_FEDATA_REG;
3745967352aSKonstantin Belousov 	dmd->msi_addr_reg = DMAR_FEADDR_REG;
3755967352aSKonstantin Belousov 	dmd->msi_uaddr_reg = DMAR_FEUADDR_REG;
3765967352aSKonstantin Belousov 	dmd->enable_intr = dmar_enable_fault_intr;
3775967352aSKonstantin Belousov 	dmd->disable_intr = dmar_disable_fault_intr;
3785967352aSKonstantin Belousov 	error = iommu_alloc_irq(DMAR2IOMMU(unit), DMAR_INTR_FAULT);
37986be9f0dSKonstantin Belousov 	if (error != 0) {
38086be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
38145543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
38286be9f0dSKonstantin Belousov 		return (error);
38386be9f0dSKonstantin Belousov 	}
3845967352aSKonstantin Belousov 	dmar_write4(unit, dmd->msi_data_reg, dmd->msi_data);
3855967352aSKonstantin Belousov 	dmar_write4(unit, dmd->msi_addr_reg, dmd->msi_addr);
3865967352aSKonstantin Belousov 	dmar_write4(unit, dmd->msi_uaddr_reg, dmd->msi_addr >> 32);
3875967352aSKonstantin Belousov 
38868eeb96aSKonstantin Belousov 	if (DMAR_HAS_QI(unit)) {
3895967352aSKonstantin Belousov 		dmd = &unit->x86c.intrs[DMAR_INTR_QI];
3905967352aSKonstantin Belousov 		dmd->name = "qi";
3915967352aSKonstantin Belousov 		dmd->irq_rid = DMAR_QI_IRQ_RID;
3925967352aSKonstantin Belousov 		dmd->handler = dmar_qi_intr;
3935967352aSKonstantin Belousov 		dmd->msi_data_reg = DMAR_IEDATA_REG;
3945967352aSKonstantin Belousov 		dmd->msi_addr_reg = DMAR_IEADDR_REG;
3955967352aSKonstantin Belousov 		dmd->msi_uaddr_reg = DMAR_IEUADDR_REG;
3965967352aSKonstantin Belousov 		dmd->enable_intr = dmar_enable_qi_intr;
3975967352aSKonstantin Belousov 		dmd->disable_intr = dmar_disable_qi_intr;
3985967352aSKonstantin Belousov 		error = iommu_alloc_irq(DMAR2IOMMU(unit), DMAR_INTR_QI);
39968eeb96aSKonstantin Belousov 		if (error != 0) {
40068eeb96aSKonstantin Belousov 			dmar_release_resources(dev, unit);
40145543d34SKonstantin Belousov 			dmar_devs[unit->iommu.unit] = NULL;
40268eeb96aSKonstantin Belousov 			return (error);
40368eeb96aSKonstantin Belousov 		}
4045967352aSKonstantin Belousov 
4055967352aSKonstantin Belousov 		dmar_write4(unit, dmd->msi_data_reg, dmd->msi_data);
4065967352aSKonstantin Belousov 		dmar_write4(unit, dmd->msi_addr_reg, dmd->msi_addr);
4075967352aSKonstantin Belousov 		dmar_write4(unit, dmd->msi_uaddr_reg, dmd->msi_addr >> 32);
40868eeb96aSKonstantin Belousov 	}
40968eeb96aSKonstantin Belousov 
41059e37c8aSRuslan Bukin 	mtx_init(&unit->iommu.lock, "dmarhw", NULL, MTX_DEF);
41186be9f0dSKonstantin Belousov 	unit->domids = new_unrhdr(0, dmar_nd2mask(DMAR_CAP_ND(unit->hw_cap)),
41259e37c8aSRuslan Bukin 	    &unit->iommu.lock);
4131abfd355SKonstantin Belousov 	LIST_INIT(&unit->domains);
41486be9f0dSKonstantin Belousov 
41586be9f0dSKonstantin Belousov 	/*
41686be9f0dSKonstantin Belousov 	 * 9.2 "Context Entry":
41786be9f0dSKonstantin Belousov 	 * When Caching Mode (CM) field is reported as Set, the
41886be9f0dSKonstantin Belousov 	 * domain-id value of zero is architecturally reserved.
41986be9f0dSKonstantin Belousov 	 * Software must not use domain-id value of zero
42086be9f0dSKonstantin Belousov 	 * when CM is Set.
42186be9f0dSKonstantin Belousov 	 */
42286be9f0dSKonstantin Belousov 	if ((unit->hw_cap & DMAR_CAP_CM) != 0)
42386be9f0dSKonstantin Belousov 		alloc_unr_specific(unit->domids, 0);
42486be9f0dSKonstantin Belousov 
42586be9f0dSKonstantin Belousov 	unit->ctx_obj = vm_pager_allocate(OBJT_PHYS, NULL, IDX_TO_OFF(1 +
42686be9f0dSKonstantin Belousov 	    DMAR_CTX_CNT), 0, 0, NULL);
42786be9f0dSKonstantin Belousov 
42886be9f0dSKonstantin Belousov 	/*
42986be9f0dSKonstantin Belousov 	 * Allocate and load the root entry table pointer.  Enable the
43086be9f0dSKonstantin Belousov 	 * address translation after the required invalidations are
43186be9f0dSKonstantin Belousov 	 * done.
43286be9f0dSKonstantin Belousov 	 */
43340d951bcSKonstantin Belousov 	iommu_pgalloc(unit->ctx_obj, 0, IOMMU_PGF_WAITOK | IOMMU_PGF_ZERO);
43486be9f0dSKonstantin Belousov 	DMAR_LOCK(unit);
43586be9f0dSKonstantin Belousov 	error = dmar_load_root_entry_ptr(unit);
43686be9f0dSKonstantin Belousov 	if (error != 0) {
43786be9f0dSKonstantin Belousov 		DMAR_UNLOCK(unit);
43886be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
43945543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
44086be9f0dSKonstantin Belousov 		return (error);
44186be9f0dSKonstantin Belousov 	}
44286be9f0dSKonstantin Belousov 	error = dmar_inv_ctx_glob(unit);
44386be9f0dSKonstantin Belousov 	if (error != 0) {
44486be9f0dSKonstantin Belousov 		DMAR_UNLOCK(unit);
44586be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
44645543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
44786be9f0dSKonstantin Belousov 		return (error);
44886be9f0dSKonstantin Belousov 	}
44986be9f0dSKonstantin Belousov 	if ((unit->hw_ecap & DMAR_ECAP_DI) != 0) {
45086be9f0dSKonstantin Belousov 		error = dmar_inv_iotlb_glob(unit);
45186be9f0dSKonstantin Belousov 		if (error != 0) {
45286be9f0dSKonstantin Belousov 			DMAR_UNLOCK(unit);
45386be9f0dSKonstantin Belousov 			dmar_release_resources(dev, unit);
45445543d34SKonstantin Belousov 			dmar_devs[unit->iommu.unit] = NULL;
45586be9f0dSKonstantin Belousov 			return (error);
45686be9f0dSKonstantin Belousov 		}
45786be9f0dSKonstantin Belousov 	}
45886be9f0dSKonstantin Belousov 
45986be9f0dSKonstantin Belousov 	DMAR_UNLOCK(unit);
46086be9f0dSKonstantin Belousov 	error = dmar_init_fault_log(unit);
46186be9f0dSKonstantin Belousov 	if (error != 0) {
46286be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
46345543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
46486be9f0dSKonstantin Belousov 		return (error);
46586be9f0dSKonstantin Belousov 	}
46668eeb96aSKonstantin Belousov 	error = dmar_init_qi(unit);
46768eeb96aSKonstantin Belousov 	if (error != 0) {
46868eeb96aSKonstantin Belousov 		dmar_release_resources(dev, unit);
46945543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
47068eeb96aSKonstantin Belousov 		return (error);
47168eeb96aSKonstantin Belousov 	}
4720a110d5bSKonstantin Belousov 	error = dmar_init_irt(unit);
4730a110d5bSKonstantin Belousov 	if (error != 0) {
4740a110d5bSKonstantin Belousov 		dmar_release_resources(dev, unit);
47545543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
4760a110d5bSKonstantin Belousov 		return (error);
4770a110d5bSKonstantin Belousov 	}
47806f659c3SKornel Duleba 
47906f659c3SKornel Duleba 	disable_pmr = 0;
48006f659c3SKornel Duleba 	TUNABLE_INT_FETCH("hw.dmar.pmr.disable", &disable_pmr);
48106f659c3SKornel Duleba 	if (disable_pmr) {
48206f659c3SKornel Duleba 		error = dmar_disable_protected_regions(unit);
48306f659c3SKornel Duleba 		if (error != 0)
48406f659c3SKornel Duleba 			device_printf(dev,
48506f659c3SKornel Duleba 			    "Failed to disable protected regions\n");
48606f659c3SKornel Duleba 	}
48706f659c3SKornel Duleba 
48859e37c8aSRuslan Bukin 	error = iommu_init_busdma(&unit->iommu);
48986be9f0dSKonstantin Belousov 	if (error != 0) {
49086be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
49145543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
49286be9f0dSKonstantin Belousov 		return (error);
49386be9f0dSKonstantin Belousov 	}
49486be9f0dSKonstantin Belousov 
49586be9f0dSKonstantin Belousov #ifdef NOTYET
49686be9f0dSKonstantin Belousov 	DMAR_LOCK(unit);
49786be9f0dSKonstantin Belousov 	error = dmar_enable_translation(unit);
49886be9f0dSKonstantin Belousov 	if (error != 0) {
49986be9f0dSKonstantin Belousov 		DMAR_UNLOCK(unit);
50086be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
50145543d34SKonstantin Belousov 		dmar_devs[unit->iommu.unit] = NULL;
50286be9f0dSKonstantin Belousov 		return (error);
50386be9f0dSKonstantin Belousov 	}
50486be9f0dSKonstantin Belousov 	DMAR_UNLOCK(unit);
50586be9f0dSKonstantin Belousov #endif
50686be9f0dSKonstantin Belousov 
50786be9f0dSKonstantin Belousov 	return (0);
50886be9f0dSKonstantin Belousov }
50986be9f0dSKonstantin Belousov 
51086be9f0dSKonstantin Belousov static int
51186be9f0dSKonstantin Belousov dmar_detach(device_t dev)
51286be9f0dSKonstantin Belousov {
51386be9f0dSKonstantin Belousov 
51486be9f0dSKonstantin Belousov 	return (EBUSY);
51586be9f0dSKonstantin Belousov }
51686be9f0dSKonstantin Belousov 
51786be9f0dSKonstantin Belousov static int
51886be9f0dSKonstantin Belousov dmar_suspend(device_t dev)
51986be9f0dSKonstantin Belousov {
52086be9f0dSKonstantin Belousov 
52186be9f0dSKonstantin Belousov 	return (0);
52286be9f0dSKonstantin Belousov }
52386be9f0dSKonstantin Belousov 
52486be9f0dSKonstantin Belousov static int
52586be9f0dSKonstantin Belousov dmar_resume(device_t dev)
52686be9f0dSKonstantin Belousov {
52786be9f0dSKonstantin Belousov 
52886be9f0dSKonstantin Belousov 	/* XXXKIB */
52986be9f0dSKonstantin Belousov 	return (0);
53086be9f0dSKonstantin Belousov }
53186be9f0dSKonstantin Belousov 
53286be9f0dSKonstantin Belousov static device_method_t dmar_methods[] = {
53386be9f0dSKonstantin Belousov 	DEVMETHOD(device_identify, dmar_identify),
53486be9f0dSKonstantin Belousov 	DEVMETHOD(device_probe, dmar_probe),
53586be9f0dSKonstantin Belousov 	DEVMETHOD(device_attach, dmar_attach),
53686be9f0dSKonstantin Belousov 	DEVMETHOD(device_detach, dmar_detach),
53786be9f0dSKonstantin Belousov 	DEVMETHOD(device_suspend, dmar_suspend),
53886be9f0dSKonstantin Belousov 	DEVMETHOD(device_resume, dmar_resume),
53986be9f0dSKonstantin Belousov #ifdef DEV_APIC
54086be9f0dSKonstantin Belousov 	DEVMETHOD(bus_remap_intr, dmar_remap_intr),
54186be9f0dSKonstantin Belousov #endif
54286be9f0dSKonstantin Belousov 	DEVMETHOD_END
54386be9f0dSKonstantin Belousov };
54486be9f0dSKonstantin Belousov 
54586be9f0dSKonstantin Belousov static driver_t	dmar_driver = {
54686be9f0dSKonstantin Belousov 	"dmar",
54786be9f0dSKonstantin Belousov 	dmar_methods,
54886be9f0dSKonstantin Belousov 	sizeof(struct dmar_unit),
54986be9f0dSKonstantin Belousov };
55086be9f0dSKonstantin Belousov 
55180d2b3deSJohn Baldwin DRIVER_MODULE(dmar, acpi, dmar_driver, 0, 0);
55286be9f0dSKonstantin Belousov MODULE_DEPEND(dmar, acpi, 1, 1, 1);
55386be9f0dSKonstantin Belousov 
55486be9f0dSKonstantin Belousov static void
555f9feb091SKonstantin Belousov dmar_print_path(int busno, int depth, const ACPI_DMAR_PCI_PATH *path)
55686be9f0dSKonstantin Belousov {
55786be9f0dSKonstantin Belousov 	int i;
55886be9f0dSKonstantin Belousov 
559f9feb091SKonstantin Belousov 	printf("[%d, ", busno);
56086be9f0dSKonstantin Belousov 	for (i = 0; i < depth; i++) {
56186be9f0dSKonstantin Belousov 		if (i != 0)
56286be9f0dSKonstantin Belousov 			printf(", ");
56386be9f0dSKonstantin Belousov 		printf("(%d, %d)", path[i].Device, path[i].Function);
56486be9f0dSKonstantin Belousov 	}
565f9feb091SKonstantin Belousov 	printf("]");
56686be9f0dSKonstantin Belousov }
56786be9f0dSKonstantin Belousov 
568f9feb091SKonstantin Belousov int
56986be9f0dSKonstantin Belousov dmar_dev_depth(device_t child)
57086be9f0dSKonstantin Belousov {
57186be9f0dSKonstantin Belousov 	devclass_t pci_class;
57286be9f0dSKonstantin Belousov 	device_t bus, pcib;
57386be9f0dSKonstantin Belousov 	int depth;
57486be9f0dSKonstantin Belousov 
57586be9f0dSKonstantin Belousov 	pci_class = devclass_find("pci");
57686be9f0dSKonstantin Belousov 	for (depth = 1; ; depth++) {
57786be9f0dSKonstantin Belousov 		bus = device_get_parent(child);
57886be9f0dSKonstantin Belousov 		pcib = device_get_parent(bus);
57986be9f0dSKonstantin Belousov 		if (device_get_devclass(device_get_parent(pcib)) !=
58086be9f0dSKonstantin Belousov 		    pci_class)
58186be9f0dSKonstantin Belousov 			return (depth);
58286be9f0dSKonstantin Belousov 		child = pcib;
58386be9f0dSKonstantin Belousov 	}
58486be9f0dSKonstantin Belousov }
58586be9f0dSKonstantin Belousov 
586f9feb091SKonstantin Belousov void
587f9feb091SKonstantin Belousov dmar_dev_path(device_t child, int *busno, void *path1, int depth)
58886be9f0dSKonstantin Belousov {
58986be9f0dSKonstantin Belousov 	devclass_t pci_class;
59086be9f0dSKonstantin Belousov 	device_t bus, pcib;
591f9feb091SKonstantin Belousov 	ACPI_DMAR_PCI_PATH *path;
59286be9f0dSKonstantin Belousov 
59386be9f0dSKonstantin Belousov 	pci_class = devclass_find("pci");
594f9feb091SKonstantin Belousov 	path = path1;
59586be9f0dSKonstantin Belousov 	for (depth--; depth != -1; depth--) {
59686be9f0dSKonstantin Belousov 		path[depth].Device = pci_get_slot(child);
59786be9f0dSKonstantin Belousov 		path[depth].Function = pci_get_function(child);
59886be9f0dSKonstantin Belousov 		bus = device_get_parent(child);
59986be9f0dSKonstantin Belousov 		pcib = device_get_parent(bus);
60086be9f0dSKonstantin Belousov 		if (device_get_devclass(device_get_parent(pcib)) !=
60186be9f0dSKonstantin Belousov 		    pci_class) {
60286be9f0dSKonstantin Belousov 			/* reached a host bridge */
60386be9f0dSKonstantin Belousov 			*busno = pcib_get_bus(bus);
60486be9f0dSKonstantin Belousov 			return;
60586be9f0dSKonstantin Belousov 		}
60686be9f0dSKonstantin Belousov 		child = pcib;
60786be9f0dSKonstantin Belousov 	}
60886be9f0dSKonstantin Belousov 	panic("wrong depth");
60986be9f0dSKonstantin Belousov }
61086be9f0dSKonstantin Belousov 
61186be9f0dSKonstantin Belousov static int
61286be9f0dSKonstantin Belousov dmar_match_pathes(int busno1, const ACPI_DMAR_PCI_PATH *path1, int depth1,
61386be9f0dSKonstantin Belousov     int busno2, const ACPI_DMAR_PCI_PATH *path2, int depth2,
61486be9f0dSKonstantin Belousov     enum AcpiDmarScopeType scope_type)
61586be9f0dSKonstantin Belousov {
61686be9f0dSKonstantin Belousov 	int i, depth;
61786be9f0dSKonstantin Belousov 
61886be9f0dSKonstantin Belousov 	if (busno1 != busno2)
61986be9f0dSKonstantin Belousov 		return (0);
62086be9f0dSKonstantin Belousov 	if (scope_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && depth1 != depth2)
62186be9f0dSKonstantin Belousov 		return (0);
62286be9f0dSKonstantin Belousov 	depth = depth1;
62386be9f0dSKonstantin Belousov 	if (depth2 < depth)
62486be9f0dSKonstantin Belousov 		depth = depth2;
62586be9f0dSKonstantin Belousov 	for (i = 0; i < depth; i++) {
62686be9f0dSKonstantin Belousov 		if (path1[i].Device != path2[i].Device ||
62786be9f0dSKonstantin Belousov 		    path1[i].Function != path2[i].Function)
62886be9f0dSKonstantin Belousov 			return (0);
62986be9f0dSKonstantin Belousov 	}
63086be9f0dSKonstantin Belousov 	return (1);
63186be9f0dSKonstantin Belousov }
63286be9f0dSKonstantin Belousov 
63386be9f0dSKonstantin Belousov static int
634f9feb091SKonstantin Belousov dmar_match_devscope(ACPI_DMAR_DEVICE_SCOPE *devscope, int dev_busno,
635f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len)
63686be9f0dSKonstantin Belousov {
63786be9f0dSKonstantin Belousov 	ACPI_DMAR_PCI_PATH *path;
63886be9f0dSKonstantin Belousov 	int path_len;
63986be9f0dSKonstantin Belousov 
64086be9f0dSKonstantin Belousov 	if (devscope->Length < sizeof(*devscope)) {
641f9feb091SKonstantin Belousov 		printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
64286be9f0dSKonstantin Belousov 		    devscope->Length);
64386be9f0dSKonstantin Belousov 		return (-1);
64486be9f0dSKonstantin Belousov 	}
64586be9f0dSKonstantin Belousov 	if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT &&
64686be9f0dSKonstantin Belousov 	    devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_BRIDGE)
64786be9f0dSKonstantin Belousov 		return (0);
64886be9f0dSKonstantin Belousov 	path_len = devscope->Length - sizeof(*devscope);
64986be9f0dSKonstantin Belousov 	if (path_len % 2 != 0) {
650f9feb091SKonstantin Belousov 		printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
65186be9f0dSKonstantin Belousov 		    devscope->Length);
65286be9f0dSKonstantin Belousov 		return (-1);
65386be9f0dSKonstantin Belousov 	}
65486be9f0dSKonstantin Belousov 	path_len /= 2;
65586be9f0dSKonstantin Belousov 	path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
65686be9f0dSKonstantin Belousov 	if (path_len == 0) {
657f9feb091SKonstantin Belousov 		printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
65886be9f0dSKonstantin Belousov 		    devscope->Length);
65986be9f0dSKonstantin Belousov 		return (-1);
66086be9f0dSKonstantin Belousov 	}
66186be9f0dSKonstantin Belousov 
66286be9f0dSKonstantin Belousov 	return (dmar_match_pathes(devscope->Bus, path, path_len, dev_busno,
66386be9f0dSKonstantin Belousov 	    dev_path, dev_path_len, devscope->EntryType));
66486be9f0dSKonstantin Belousov }
66586be9f0dSKonstantin Belousov 
666f9feb091SKonstantin Belousov static bool
667f9feb091SKonstantin Belousov dmar_match_by_path(struct dmar_unit *unit, int dev_domain, int dev_busno,
668f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len, const char **banner)
66986be9f0dSKonstantin Belousov {
67086be9f0dSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *dmarh;
67186be9f0dSKonstantin Belousov 	ACPI_DMAR_DEVICE_SCOPE *devscope;
67286be9f0dSKonstantin Belousov 	char *ptr, *ptrend;
673f9feb091SKonstantin Belousov 	int match;
674f9feb091SKonstantin Belousov 
67559e37c8aSRuslan Bukin 	dmarh = dmar_find_by_index(unit->iommu.unit);
676f9feb091SKonstantin Belousov 	if (dmarh == NULL)
677f9feb091SKonstantin Belousov 		return (false);
678f9feb091SKonstantin Belousov 	if (dmarh->Segment != dev_domain)
679f9feb091SKonstantin Belousov 		return (false);
680f9feb091SKonstantin Belousov 	if ((dmarh->Flags & ACPI_DMAR_INCLUDE_ALL) != 0) {
681f9feb091SKonstantin Belousov 		if (banner != NULL)
682f9feb091SKonstantin Belousov 			*banner = "INCLUDE_ALL";
683f9feb091SKonstantin Belousov 		return (true);
684f9feb091SKonstantin Belousov 	}
685f9feb091SKonstantin Belousov 	ptr = (char *)dmarh + sizeof(*dmarh);
686f9feb091SKonstantin Belousov 	ptrend = (char *)dmarh + dmarh->Header.Length;
687f9feb091SKonstantin Belousov 	while (ptr < ptrend) {
688f9feb091SKonstantin Belousov 		devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
689f9feb091SKonstantin Belousov 		ptr += devscope->Length;
690f9feb091SKonstantin Belousov 		match = dmar_match_devscope(devscope, dev_busno, dev_path,
691f9feb091SKonstantin Belousov 		    dev_path_len);
692f9feb091SKonstantin Belousov 		if (match == -1)
693f9feb091SKonstantin Belousov 			return (false);
694f9feb091SKonstantin Belousov 		if (match == 1) {
695f9feb091SKonstantin Belousov 			if (banner != NULL)
696f9feb091SKonstantin Belousov 				*banner = "specific match";
697f9feb091SKonstantin Belousov 			return (true);
698f9feb091SKonstantin Belousov 		}
699f9feb091SKonstantin Belousov 	}
700f9feb091SKonstantin Belousov 	return (false);
701f9feb091SKonstantin Belousov }
702f9feb091SKonstantin Belousov 
703f9feb091SKonstantin Belousov static struct dmar_unit *
704f9feb091SKonstantin Belousov dmar_find_by_scope(int dev_domain, int dev_busno,
705f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len)
706f9feb091SKonstantin Belousov {
707f9feb091SKonstantin Belousov 	struct dmar_unit *unit;
708f9feb091SKonstantin Belousov 	int i;
709f9feb091SKonstantin Belousov 
710f9feb091SKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
711f9feb091SKonstantin Belousov 		if (dmar_devs[i] == NULL)
712f9feb091SKonstantin Belousov 			continue;
713f9feb091SKonstantin Belousov 		unit = device_get_softc(dmar_devs[i]);
714f9feb091SKonstantin Belousov 		if (dmar_match_by_path(unit, dev_domain, dev_busno, dev_path,
715f9feb091SKonstantin Belousov 		    dev_path_len, NULL))
716f9feb091SKonstantin Belousov 			return (unit);
717f9feb091SKonstantin Belousov 	}
718f9feb091SKonstantin Belousov 	return (NULL);
719f9feb091SKonstantin Belousov }
720f9feb091SKonstantin Belousov 
721f9feb091SKonstantin Belousov struct dmar_unit *
722f9feb091SKonstantin Belousov dmar_find(device_t dev, bool verbose)
723f9feb091SKonstantin Belousov {
724f9feb091SKonstantin Belousov 	struct dmar_unit *unit;
725f9feb091SKonstantin Belousov 	const char *banner;
726f9feb091SKonstantin Belousov 	int i, dev_domain, dev_busno, dev_path_len;
72786be9f0dSKonstantin Belousov 
728b7b6b7a9SKonstantin Belousov 	/*
729b7b6b7a9SKonstantin Belousov 	 * This function can only handle PCI(e) devices.
730b7b6b7a9SKonstantin Belousov 	 */
731b7b6b7a9SKonstantin Belousov 	if (device_get_devclass(device_get_parent(dev)) !=
732b7b6b7a9SKonstantin Belousov 	    devclass_find("pci"))
733b7b6b7a9SKonstantin Belousov 		return (NULL);
734b7b6b7a9SKonstantin Belousov 
73586be9f0dSKonstantin Belousov 	dev_domain = pci_get_domain(dev);
73686be9f0dSKonstantin Belousov 	dev_path_len = dmar_dev_depth(dev);
73786be9f0dSKonstantin Belousov 	ACPI_DMAR_PCI_PATH dev_path[dev_path_len];
73886be9f0dSKonstantin Belousov 	dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len);
739f9feb091SKonstantin Belousov 	banner = "";
74086be9f0dSKonstantin Belousov 
74186be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
74286be9f0dSKonstantin Belousov 		if (dmar_devs[i] == NULL)
74386be9f0dSKonstantin Belousov 			continue;
744f9feb091SKonstantin Belousov 		unit = device_get_softc(dmar_devs[i]);
745f9feb091SKonstantin Belousov 		if (dmar_match_by_path(unit, dev_domain, dev_busno,
746f9feb091SKonstantin Belousov 		    dev_path, dev_path_len, &banner))
74786be9f0dSKonstantin Belousov 			break;
74886be9f0dSKonstantin Belousov 	}
749f9feb091SKonstantin Belousov 	if (i == dmar_devcnt)
75086be9f0dSKonstantin Belousov 		return (NULL);
751f9feb091SKonstantin Belousov 
752f9feb091SKonstantin Belousov 	if (verbose) {
753f9feb091SKonstantin Belousov 		device_printf(dev, "pci%d:%d:%d:%d matched dmar%d by %s",
754f9feb091SKonstantin Belousov 		    dev_domain, pci_get_bus(dev), pci_get_slot(dev),
75559e37c8aSRuslan Bukin 		    pci_get_function(dev), unit->iommu.unit, banner);
756f9feb091SKonstantin Belousov 		printf(" scope path ");
757f9feb091SKonstantin Belousov 		dmar_print_path(dev_busno, dev_path_len, dev_path);
758f9feb091SKonstantin Belousov 		printf("\n");
75986be9f0dSKonstantin Belousov 	}
760f9feb091SKonstantin Belousov 	return (unit);
76186be9f0dSKonstantin Belousov }
76286be9f0dSKonstantin Belousov 
7630a110d5bSKonstantin Belousov static struct dmar_unit *
7640a110d5bSKonstantin Belousov dmar_find_nonpci(u_int id, u_int entry_type, uint16_t *rid)
7650a110d5bSKonstantin Belousov {
7660a110d5bSKonstantin Belousov 	device_t dmar_dev;
7670a110d5bSKonstantin Belousov 	struct dmar_unit *unit;
7680a110d5bSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *dmarh;
7690a110d5bSKonstantin Belousov 	ACPI_DMAR_DEVICE_SCOPE *devscope;
7700a110d5bSKonstantin Belousov 	ACPI_DMAR_PCI_PATH *path;
7710a110d5bSKonstantin Belousov 	char *ptr, *ptrend;
772fd15fee1SKonstantin Belousov #ifdef DEV_APIC
773fd15fee1SKonstantin Belousov 	int error;
774fd15fee1SKonstantin Belousov #endif
7750a110d5bSKonstantin Belousov 	int i;
7760a110d5bSKonstantin Belousov 
7770a110d5bSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
7780a110d5bSKonstantin Belousov 		dmar_dev = dmar_devs[i];
7790a110d5bSKonstantin Belousov 		if (dmar_dev == NULL)
7800a110d5bSKonstantin Belousov 			continue;
7810a110d5bSKonstantin Belousov 		unit = (struct dmar_unit *)device_get_softc(dmar_dev);
7820a110d5bSKonstantin Belousov 		dmarh = dmar_find_by_index(i);
7830a110d5bSKonstantin Belousov 		if (dmarh == NULL)
7840a110d5bSKonstantin Belousov 			continue;
7850a110d5bSKonstantin Belousov 		ptr = (char *)dmarh + sizeof(*dmarh);
7860a110d5bSKonstantin Belousov 		ptrend = (char *)dmarh + dmarh->Header.Length;
7870a110d5bSKonstantin Belousov 		for (;;) {
7880a110d5bSKonstantin Belousov 			if (ptr >= ptrend)
7890a110d5bSKonstantin Belousov 				break;
7900a110d5bSKonstantin Belousov 			devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
7910a110d5bSKonstantin Belousov 			ptr += devscope->Length;
7920a110d5bSKonstantin Belousov 			if (devscope->EntryType != entry_type)
7930a110d5bSKonstantin Belousov 				continue;
7940a110d5bSKonstantin Belousov 			if (devscope->EnumerationId != id)
7950a110d5bSKonstantin Belousov 				continue;
796fd15fee1SKonstantin Belousov #ifdef DEV_APIC
797fd15fee1SKonstantin Belousov 			if (entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
798fd15fee1SKonstantin Belousov 				error = ioapic_get_rid(id, rid);
799fd15fee1SKonstantin Belousov 				/*
800fd15fee1SKonstantin Belousov 				 * If our IOAPIC has PCI bindings then
801fd15fee1SKonstantin Belousov 				 * use the PCI device rid.
802fd15fee1SKonstantin Belousov 				 */
803fd15fee1SKonstantin Belousov 				if (error == 0)
804fd15fee1SKonstantin Belousov 					return (unit);
805fd15fee1SKonstantin Belousov 			}
806fd15fee1SKonstantin Belousov #endif
8070a110d5bSKonstantin Belousov 			if (devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE)
8080a110d5bSKonstantin Belousov 			    == 2) {
8090a110d5bSKonstantin Belousov 				if (rid != NULL) {
8100a110d5bSKonstantin Belousov 					path = (ACPI_DMAR_PCI_PATH *)
8110a110d5bSKonstantin Belousov 					    (devscope + 1);
8120a110d5bSKonstantin Belousov 					*rid = PCI_RID(devscope->Bus,
8130a110d5bSKonstantin Belousov 					    path->Device, path->Function);
8140a110d5bSKonstantin Belousov 				}
8150a110d5bSKonstantin Belousov 				return (unit);
816fd15fee1SKonstantin Belousov 			}
8170a110d5bSKonstantin Belousov 			printf(
8180a110d5bSKonstantin Belousov 		           "dmar_find_nonpci: id %d type %d path length != 2\n",
8190a110d5bSKonstantin Belousov 			    id, entry_type);
820fd15fee1SKonstantin Belousov 			break;
8210a110d5bSKonstantin Belousov 		}
8220a110d5bSKonstantin Belousov 	}
8230a110d5bSKonstantin Belousov 	return (NULL);
8240a110d5bSKonstantin Belousov }
8250a110d5bSKonstantin Belousov 
8260a110d5bSKonstantin Belousov struct dmar_unit *
8270a110d5bSKonstantin Belousov dmar_find_hpet(device_t dev, uint16_t *rid)
8280a110d5bSKonstantin Belousov {
8290a110d5bSKonstantin Belousov 
8302fe1339eSKonstantin Belousov 	return (dmar_find_nonpci(hpet_get_uid(dev), ACPI_DMAR_SCOPE_TYPE_HPET,
8312fe1339eSKonstantin Belousov 	    rid));
8320a110d5bSKonstantin Belousov }
8330a110d5bSKonstantin Belousov 
8340a110d5bSKonstantin Belousov struct dmar_unit *
8350a110d5bSKonstantin Belousov dmar_find_ioapic(u_int apic_id, uint16_t *rid)
8360a110d5bSKonstantin Belousov {
8370a110d5bSKonstantin Belousov 
8380a110d5bSKonstantin Belousov 	return (dmar_find_nonpci(apic_id, ACPI_DMAR_SCOPE_TYPE_IOAPIC, rid));
8390a110d5bSKonstantin Belousov }
8400a110d5bSKonstantin Belousov 
84186be9f0dSKonstantin Belousov struct rmrr_iter_args {
8421abfd355SKonstantin Belousov 	struct dmar_domain *domain;
84386be9f0dSKonstantin Belousov 	int dev_domain;
84486be9f0dSKonstantin Belousov 	int dev_busno;
845f9feb091SKonstantin Belousov 	const ACPI_DMAR_PCI_PATH *dev_path;
84686be9f0dSKonstantin Belousov 	int dev_path_len;
84759e37c8aSRuslan Bukin 	struct iommu_map_entries_tailq *rmrr_entries;
84886be9f0dSKonstantin Belousov };
84986be9f0dSKonstantin Belousov 
85086be9f0dSKonstantin Belousov static int
85186be9f0dSKonstantin Belousov dmar_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
85286be9f0dSKonstantin Belousov {
85386be9f0dSKonstantin Belousov 	struct rmrr_iter_args *ria;
85486be9f0dSKonstantin Belousov 	ACPI_DMAR_RESERVED_MEMORY *resmem;
85586be9f0dSKonstantin Belousov 	ACPI_DMAR_DEVICE_SCOPE *devscope;
85659e37c8aSRuslan Bukin 	struct iommu_map_entry *entry;
85786be9f0dSKonstantin Belousov 	char *ptr, *ptrend;
85886be9f0dSKonstantin Belousov 	int match;
85986be9f0dSKonstantin Belousov 
86024e38af6SKonstantin Belousov 	if (!dmar_rmrr_enable)
86124e38af6SKonstantin Belousov 		return (1);
86224e38af6SKonstantin Belousov 
86386be9f0dSKonstantin Belousov 	if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY)
86486be9f0dSKonstantin Belousov 		return (1);
86586be9f0dSKonstantin Belousov 
86686be9f0dSKonstantin Belousov 	ria = arg;
86786be9f0dSKonstantin Belousov 	resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh;
86886be9f0dSKonstantin Belousov 	if (resmem->Segment != ria->dev_domain)
86986be9f0dSKonstantin Belousov 		return (1);
87086be9f0dSKonstantin Belousov 
87186be9f0dSKonstantin Belousov 	ptr = (char *)resmem + sizeof(*resmem);
87286be9f0dSKonstantin Belousov 	ptrend = (char *)resmem + resmem->Header.Length;
87386be9f0dSKonstantin Belousov 	for (;;) {
87486be9f0dSKonstantin Belousov 		if (ptr >= ptrend)
87586be9f0dSKonstantin Belousov 			break;
87686be9f0dSKonstantin Belousov 		devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
87786be9f0dSKonstantin Belousov 		ptr += devscope->Length;
878f9feb091SKonstantin Belousov 		match = dmar_match_devscope(devscope, ria->dev_busno,
87986be9f0dSKonstantin Belousov 		    ria->dev_path, ria->dev_path_len);
88086be9f0dSKonstantin Belousov 		if (match == 1) {
88178b51754SRuslan Bukin 			entry = iommu_gas_alloc_entry(DOM2IODOM(ria->domain),
88215f6baf4SRuslan Bukin 			    IOMMU_PGF_WAITOK);
88386be9f0dSKonstantin Belousov 			entry->start = resmem->BaseAddress;
88486be9f0dSKonstantin Belousov 			/* The RMRR entry end address is inclusive. */
88586be9f0dSKonstantin Belousov 			entry->end = resmem->EndAddress;
88686be9f0dSKonstantin Belousov 			TAILQ_INSERT_TAIL(ria->rmrr_entries, entry,
887db0110a5SAlan Cox 			    dmamap_link);
88886be9f0dSKonstantin Belousov 		}
88986be9f0dSKonstantin Belousov 	}
89086be9f0dSKonstantin Belousov 
89186be9f0dSKonstantin Belousov 	return (1);
89286be9f0dSKonstantin Belousov }
89386be9f0dSKonstantin Belousov 
89486be9f0dSKonstantin Belousov void
895f9feb091SKonstantin Belousov dmar_dev_parse_rmrr(struct dmar_domain *domain, int dev_domain, int dev_busno,
896f9feb091SKonstantin Belousov     const void *dev_path, int dev_path_len,
89759e37c8aSRuslan Bukin     struct iommu_map_entries_tailq *rmrr_entries)
89886be9f0dSKonstantin Belousov {
89986be9f0dSKonstantin Belousov 	struct rmrr_iter_args ria;
90086be9f0dSKonstantin Belousov 
9011abfd355SKonstantin Belousov 	ria.domain = domain;
902f9feb091SKonstantin Belousov 	ria.dev_domain = dev_domain;
903f9feb091SKonstantin Belousov 	ria.dev_busno = dev_busno;
904f9feb091SKonstantin Belousov 	ria.dev_path = (const ACPI_DMAR_PCI_PATH *)dev_path;
905f9feb091SKonstantin Belousov 	ria.dev_path_len = dev_path_len;
90686be9f0dSKonstantin Belousov 	ria.rmrr_entries = rmrr_entries;
90786be9f0dSKonstantin Belousov 	dmar_iterate_tbl(dmar_rmrr_iter, &ria);
90886be9f0dSKonstantin Belousov }
90986be9f0dSKonstantin Belousov 
91086be9f0dSKonstantin Belousov struct inst_rmrr_iter_args {
91186be9f0dSKonstantin Belousov 	struct dmar_unit *dmar;
91286be9f0dSKonstantin Belousov };
91386be9f0dSKonstantin Belousov 
91486be9f0dSKonstantin Belousov static device_t
91586be9f0dSKonstantin Belousov dmar_path_dev(int segment, int path_len, int busno,
916f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *path, uint16_t *rid)
91786be9f0dSKonstantin Belousov {
918f9feb091SKonstantin Belousov 	device_t dev;
91986be9f0dSKonstantin Belousov 	int i;
92086be9f0dSKonstantin Belousov 
92186be9f0dSKonstantin Belousov 	dev = NULL;
922f9feb091SKonstantin Belousov 	for (i = 0; i < path_len; i++) {
92386be9f0dSKonstantin Belousov 		dev = pci_find_dbsf(segment, busno, path->Device,
92486be9f0dSKonstantin Belousov 		    path->Function);
92586be9f0dSKonstantin Belousov 		if (i != path_len - 1) {
9261587a9dbSJohn Baldwin 			busno = pci_cfgregread(segment, busno, path->Device,
927f9feb091SKonstantin Belousov 			    path->Function, PCIR_SECBUS_1, 1);
928f9feb091SKonstantin Belousov 			path++;
92986be9f0dSKonstantin Belousov 		}
93086be9f0dSKonstantin Belousov 	}
931f9feb091SKonstantin Belousov 	*rid = PCI_RID(busno, path->Device, path->Function);
93286be9f0dSKonstantin Belousov 	return (dev);
93386be9f0dSKonstantin Belousov }
93486be9f0dSKonstantin Belousov 
93586be9f0dSKonstantin Belousov static int
93686be9f0dSKonstantin Belousov dmar_inst_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
93786be9f0dSKonstantin Belousov {
93886be9f0dSKonstantin Belousov 	const ACPI_DMAR_RESERVED_MEMORY *resmem;
93986be9f0dSKonstantin Belousov 	const ACPI_DMAR_DEVICE_SCOPE *devscope;
94086be9f0dSKonstantin Belousov 	struct inst_rmrr_iter_args *iria;
94186be9f0dSKonstantin Belousov 	const char *ptr, *ptrend;
94286be9f0dSKonstantin Belousov 	device_t dev;
943f9feb091SKonstantin Belousov 	struct dmar_unit *unit;
944f9feb091SKonstantin Belousov 	int dev_path_len;
945f9feb091SKonstantin Belousov 	uint16_t rid;
946f9feb091SKonstantin Belousov 
947f9feb091SKonstantin Belousov 	iria = arg;
94886be9f0dSKonstantin Belousov 
94924e38af6SKonstantin Belousov 	if (!dmar_rmrr_enable)
95024e38af6SKonstantin Belousov 		return (1);
95124e38af6SKonstantin Belousov 
95286be9f0dSKonstantin Belousov 	if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY)
95386be9f0dSKonstantin Belousov 		return (1);
95486be9f0dSKonstantin Belousov 
95586be9f0dSKonstantin Belousov 	resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh;
95686be9f0dSKonstantin Belousov 	if (resmem->Segment != iria->dmar->segment)
95786be9f0dSKonstantin Belousov 		return (1);
95886be9f0dSKonstantin Belousov 
95933552193SDimitry Andric 	ptr = (const char *)resmem + sizeof(*resmem);
96033552193SDimitry Andric 	ptrend = (const char *)resmem + resmem->Header.Length;
96186be9f0dSKonstantin Belousov 	for (;;) {
96286be9f0dSKonstantin Belousov 		if (ptr >= ptrend)
96386be9f0dSKonstantin Belousov 			break;
96433552193SDimitry Andric 		devscope = (const ACPI_DMAR_DEVICE_SCOPE *)ptr;
96586be9f0dSKonstantin Belousov 		ptr += devscope->Length;
96686be9f0dSKonstantin Belousov 		/* XXXKIB bridge */
96786be9f0dSKonstantin Belousov 		if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT)
96886be9f0dSKonstantin Belousov 			continue;
969f9feb091SKonstantin Belousov 		rid = 0;
970f9feb091SKonstantin Belousov 		dev_path_len = (devscope->Length -
971f9feb091SKonstantin Belousov 		    sizeof(ACPI_DMAR_DEVICE_SCOPE)) / 2;
972f9feb091SKonstantin Belousov 		dev = dmar_path_dev(resmem->Segment, dev_path_len,
973f9feb091SKonstantin Belousov 		    devscope->Bus,
974f9feb091SKonstantin Belousov 		    (const ACPI_DMAR_PCI_PATH *)(devscope + 1), &rid);
97586be9f0dSKonstantin Belousov 		if (dev == NULL) {
976f9feb091SKonstantin Belousov 			if (bootverbose) {
977f9feb091SKonstantin Belousov 				printf("dmar%d no dev found for RMRR "
978f9feb091SKonstantin Belousov 				    "[%#jx, %#jx] rid %#x scope path ",
97959e37c8aSRuslan Bukin 				    iria->dmar->iommu.unit,
9802d8bfbdcSKonstantin Belousov 				    (uintmax_t)resmem->BaseAddress,
9812d8bfbdcSKonstantin Belousov 				    (uintmax_t)resmem->EndAddress,
982f9feb091SKonstantin Belousov 				    rid);
983f9feb091SKonstantin Belousov 				dmar_print_path(devscope->Bus, dev_path_len,
984f9feb091SKonstantin Belousov 				    (const ACPI_DMAR_PCI_PATH *)(devscope + 1));
985f9feb091SKonstantin Belousov 				printf("\n");
986f9feb091SKonstantin Belousov 			}
987f9feb091SKonstantin Belousov 			unit = dmar_find_by_scope(resmem->Segment,
988f9feb091SKonstantin Belousov 			    devscope->Bus,
989f9feb091SKonstantin Belousov 			    (const ACPI_DMAR_PCI_PATH *)(devscope + 1),
990f9feb091SKonstantin Belousov 			    dev_path_len);
991f9feb091SKonstantin Belousov 			if (iria->dmar != unit)
99286be9f0dSKonstantin Belousov 				continue;
993f9feb091SKonstantin Belousov 			dmar_get_ctx_for_devpath(iria->dmar, rid,
994f9feb091SKonstantin Belousov 			    resmem->Segment, devscope->Bus,
995f9feb091SKonstantin Belousov 			    (const ACPI_DMAR_PCI_PATH *)(devscope + 1),
996f9feb091SKonstantin Belousov 			    dev_path_len, false, true);
997f9feb091SKonstantin Belousov 		} else {
998f9feb091SKonstantin Belousov 			unit = dmar_find(dev, false);
999f9feb091SKonstantin Belousov 			if (iria->dmar != unit)
100086be9f0dSKonstantin Belousov 				continue;
100159e37c8aSRuslan Bukin 			iommu_instantiate_ctx(&(iria)->dmar->iommu,
100259e37c8aSRuslan Bukin 			    dev, true);
100386be9f0dSKonstantin Belousov 		}
1004f9feb091SKonstantin Belousov 	}
100586be9f0dSKonstantin Belousov 
100686be9f0dSKonstantin Belousov 	return (1);
100786be9f0dSKonstantin Belousov 
100886be9f0dSKonstantin Belousov }
100986be9f0dSKonstantin Belousov 
101086be9f0dSKonstantin Belousov /*
101186be9f0dSKonstantin Belousov  * Pre-create all contexts for the DMAR which have RMRR entries.
101286be9f0dSKonstantin Belousov  */
101386be9f0dSKonstantin Belousov int
101459e37c8aSRuslan Bukin dmar_instantiate_rmrr_ctxs(struct iommu_unit *unit)
101586be9f0dSKonstantin Belousov {
101659e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
101786be9f0dSKonstantin Belousov 	struct inst_rmrr_iter_args iria;
101886be9f0dSKonstantin Belousov 	int error;
101986be9f0dSKonstantin Belousov 
102078b51754SRuslan Bukin 	dmar = IOMMU2DMAR(unit);
102159e37c8aSRuslan Bukin 
102286be9f0dSKonstantin Belousov 	if (!dmar_barrier_enter(dmar, DMAR_BARRIER_RMRR))
102386be9f0dSKonstantin Belousov 		return (0);
102486be9f0dSKonstantin Belousov 
102586be9f0dSKonstantin Belousov 	error = 0;
102686be9f0dSKonstantin Belousov 	iria.dmar = dmar;
102786be9f0dSKonstantin Belousov 	dmar_iterate_tbl(dmar_inst_rmrr_iter, &iria);
102886be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
10291abfd355SKonstantin Belousov 	if (!LIST_EMPTY(&dmar->domains)) {
103086be9f0dSKonstantin Belousov 		KASSERT((dmar->hw_gcmd & DMAR_GCMD_TE) == 0,
103186be9f0dSKonstantin Belousov 	    ("dmar%d: RMRR not handled but translation is already enabled",
103259e37c8aSRuslan Bukin 		    dmar->iommu.unit));
103306e6ca6dSKornel Duleba 		error = dmar_disable_protected_regions(dmar);
103406e6ca6dSKornel Duleba 		if (error != 0)
103506e6ca6dSKornel Duleba 			printf("dmar%d: Failed to disable protected regions\n",
103606e6ca6dSKornel Duleba 			    dmar->iommu.unit);
103786be9f0dSKonstantin Belousov 		error = dmar_enable_translation(dmar);
1038f9feb091SKonstantin Belousov 		if (bootverbose) {
1039f9feb091SKonstantin Belousov 			if (error == 0) {
1040f9feb091SKonstantin Belousov 				printf("dmar%d: enabled translation\n",
104159e37c8aSRuslan Bukin 				    dmar->iommu.unit);
1042f9feb091SKonstantin Belousov 			} else {
1043f9feb091SKonstantin Belousov 				printf("dmar%d: enabling translation failed, "
104459e37c8aSRuslan Bukin 				    "error %d\n", dmar->iommu.unit, error);
1045f9feb091SKonstantin Belousov 			}
1046f9feb091SKonstantin Belousov 		}
104786be9f0dSKonstantin Belousov 	}
104886be9f0dSKonstantin Belousov 	dmar_barrier_exit(dmar, DMAR_BARRIER_RMRR);
104986be9f0dSKonstantin Belousov 	return (error);
105086be9f0dSKonstantin Belousov }
105186be9f0dSKonstantin Belousov 
105286be9f0dSKonstantin Belousov #ifdef DDB
105386be9f0dSKonstantin Belousov #include <ddb/ddb.h>
105486be9f0dSKonstantin Belousov #include <ddb/db_lex.h>
105586be9f0dSKonstantin Belousov 
105686be9f0dSKonstantin Belousov static void
10571abfd355SKonstantin Belousov dmar_print_domain(struct dmar_domain *domain, bool show_mappings)
10581abfd355SKonstantin Belousov {
105962ad310cSRuslan Bukin 	struct iommu_domain *iodom;
10601abfd355SKonstantin Belousov 
106178b51754SRuslan Bukin 	iodom = DOM2IODOM(domain);
106262ad310cSRuslan Bukin 
10631abfd355SKonstantin Belousov 	db_printf(
10641abfd355SKonstantin Belousov 	    "  @%p dom %d mgaw %d agaw %d pglvl %d end %jx refs %d\n"
10651abfd355SKonstantin Belousov 	    "   ctx_cnt %d flags %x pgobj %p map_ents %u\n",
10661abfd355SKonstantin Belousov 	    domain, domain->domain, domain->mgaw, domain->agaw, domain->pglvl,
106762ad310cSRuslan Bukin 	    (uintmax_t)domain->iodom.end, domain->refs, domain->ctx_cnt,
106862ad310cSRuslan Bukin 	    domain->iodom.flags, domain->pgtbl_obj, domain->iodom.entries_cnt);
1069*c9e22c74SKonstantin Belousov 
1070*c9e22c74SKonstantin Belousov 	iommu_db_domain_print_contexts(iodom);
1071*c9e22c74SKonstantin Belousov 
1072*c9e22c74SKonstantin Belousov 	if (show_mappings)
1073*c9e22c74SKonstantin Belousov 		iommu_db_domain_print_mappings(iodom);
107486be9f0dSKonstantin Belousov }
107586be9f0dSKonstantin Belousov 
1076258958b3SMitchell Horne DB_SHOW_COMMAND_FLAGS(dmar_domain, db_dmar_print_domain, CS_OWN)
107786be9f0dSKonstantin Belousov {
107886be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
10791abfd355SKonstantin Belousov 	struct dmar_domain *domain;
1080e9d948cfSKonstantin Belousov 	struct iommu_ctx *ctx;
108186be9f0dSKonstantin Belousov 	bool show_mappings, valid;
10821abfd355SKonstantin Belousov 	int pci_domain, bus, device, function, i, t;
108386be9f0dSKonstantin Belousov 	db_expr_t radix;
108486be9f0dSKonstantin Belousov 
108586be9f0dSKonstantin Belousov 	valid = false;
108686be9f0dSKonstantin Belousov 	radix = db_radix;
108786be9f0dSKonstantin Belousov 	db_radix = 10;
108886be9f0dSKonstantin Belousov 	t = db_read_token();
108986be9f0dSKonstantin Belousov 	if (t == tSLASH) {
109086be9f0dSKonstantin Belousov 		t = db_read_token();
109186be9f0dSKonstantin Belousov 		if (t != tIDENT) {
109286be9f0dSKonstantin Belousov 			db_printf("Bad modifier\n");
109386be9f0dSKonstantin Belousov 			db_radix = radix;
109486be9f0dSKonstantin Belousov 			db_skip_to_eol();
109586be9f0dSKonstantin Belousov 			return;
109686be9f0dSKonstantin Belousov 		}
109786be9f0dSKonstantin Belousov 		show_mappings = strchr(db_tok_string, 'm') != NULL;
109886be9f0dSKonstantin Belousov 		t = db_read_token();
1099f7f5706fSDimitry Andric 	} else {
1100f7f5706fSDimitry Andric 		show_mappings = false;
110186be9f0dSKonstantin Belousov 	}
110286be9f0dSKonstantin Belousov 	if (t == tNUMBER) {
11031abfd355SKonstantin Belousov 		pci_domain = db_tok_number;
110486be9f0dSKonstantin Belousov 		t = db_read_token();
110586be9f0dSKonstantin Belousov 		if (t == tNUMBER) {
110686be9f0dSKonstantin Belousov 			bus = db_tok_number;
110786be9f0dSKonstantin Belousov 			t = db_read_token();
110886be9f0dSKonstantin Belousov 			if (t == tNUMBER) {
110986be9f0dSKonstantin Belousov 				device = db_tok_number;
111086be9f0dSKonstantin Belousov 				t = db_read_token();
111186be9f0dSKonstantin Belousov 				if (t == tNUMBER) {
111286be9f0dSKonstantin Belousov 					function = db_tok_number;
111386be9f0dSKonstantin Belousov 					valid = true;
111486be9f0dSKonstantin Belousov 				}
111586be9f0dSKonstantin Belousov 			}
111686be9f0dSKonstantin Belousov 		}
111786be9f0dSKonstantin Belousov 	}
111886be9f0dSKonstantin Belousov 			db_radix = radix;
111986be9f0dSKonstantin Belousov 	db_skip_to_eol();
112086be9f0dSKonstantin Belousov 	if (!valid) {
11211abfd355SKonstantin Belousov 		db_printf("usage: show dmar_domain [/m] "
112286be9f0dSKonstantin Belousov 		    "<domain> <bus> <device> <func>\n");
112386be9f0dSKonstantin Belousov 		return;
112486be9f0dSKonstantin Belousov 	}
112586be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
112686be9f0dSKonstantin Belousov 		unit = device_get_softc(dmar_devs[i]);
11271abfd355SKonstantin Belousov 		LIST_FOREACH(domain, &unit->domains, link) {
1128e9d948cfSKonstantin Belousov 			LIST_FOREACH(ctx, &domain->iodom.contexts, link) {
11291abfd355SKonstantin Belousov 				if (pci_domain == unit->segment &&
1130e9d948cfSKonstantin Belousov 				    bus == pci_get_bus(ctx->tag->owner) &&
1131e9d948cfSKonstantin Belousov 				    device == pci_get_slot(ctx->tag->owner) &&
1132e9d948cfSKonstantin Belousov 				    function == pci_get_function(ctx->tag->
1133e9d948cfSKonstantin Belousov 				    owner)) {
11341abfd355SKonstantin Belousov 					dmar_print_domain(domain,
11351abfd355SKonstantin Belousov 					    show_mappings);
113686be9f0dSKonstantin Belousov 					goto out;
113786be9f0dSKonstantin Belousov 				}
113886be9f0dSKonstantin Belousov 			}
113986be9f0dSKonstantin Belousov 		}
11401abfd355SKonstantin Belousov 	}
114186be9f0dSKonstantin Belousov out:;
114286be9f0dSKonstantin Belousov }
114386be9f0dSKonstantin Belousov 
114486be9f0dSKonstantin Belousov static void
11451abfd355SKonstantin Belousov dmar_print_one(int idx, bool show_domains, bool show_mappings)
114686be9f0dSKonstantin Belousov {
114786be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
11481abfd355SKonstantin Belousov 	struct dmar_domain *domain;
114986be9f0dSKonstantin Belousov 	int i, frir;
115086be9f0dSKonstantin Belousov 
115186be9f0dSKonstantin Belousov 	unit = device_get_softc(dmar_devs[idx]);
115259e37c8aSRuslan Bukin 	db_printf("dmar%d at %p, root at 0x%jx, ver 0x%x\n", unit->iommu.unit,
115359e37c8aSRuslan Bukin 	    unit, dmar_read8(unit, DMAR_RTADDR_REG),
115459e37c8aSRuslan Bukin 	    dmar_read4(unit, DMAR_VER_REG));
115586be9f0dSKonstantin Belousov 	db_printf("cap 0x%jx ecap 0x%jx gsts 0x%x fsts 0x%x fectl 0x%x\n",
115686be9f0dSKonstantin Belousov 	    (uintmax_t)dmar_read8(unit, DMAR_CAP_REG),
115786be9f0dSKonstantin Belousov 	    (uintmax_t)dmar_read8(unit, DMAR_ECAP_REG),
115886be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_GSTS_REG),
115986be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FSTS_REG),
116086be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FECTL_REG));
11611abfd355SKonstantin Belousov 	if (unit->ir_enabled) {
11621abfd355SKonstantin Belousov 		db_printf("ir is enabled; IRT @%p phys 0x%jx maxcnt %d\n",
11631abfd355SKonstantin Belousov 		    unit->irt, (uintmax_t)unit->irt_phys, unit->irte_cnt);
11641abfd355SKonstantin Belousov 	}
116586be9f0dSKonstantin Belousov 	db_printf("fed 0x%x fea 0x%x feua 0x%x\n",
116686be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FEDATA_REG),
116786be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FEADDR_REG),
116886be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FEUADDR_REG));
116986be9f0dSKonstantin Belousov 	db_printf("primary fault log:\n");
117086be9f0dSKonstantin Belousov 	for (i = 0; i < DMAR_CAP_NFR(unit->hw_cap); i++) {
117186be9f0dSKonstantin Belousov 		frir = (DMAR_CAP_FRO(unit->hw_cap) + i) * 16;
117286be9f0dSKonstantin Belousov 		db_printf("  %d at 0x%x: %jx %jx\n", i, frir,
117386be9f0dSKonstantin Belousov 		    (uintmax_t)dmar_read8(unit, frir),
117486be9f0dSKonstantin Belousov 		    (uintmax_t)dmar_read8(unit, frir + 8));
117586be9f0dSKonstantin Belousov 	}
117668eeb96aSKonstantin Belousov 	if (DMAR_HAS_QI(unit)) {
117768eeb96aSKonstantin Belousov 		db_printf("ied 0x%x iea 0x%x ieua 0x%x\n",
117868eeb96aSKonstantin Belousov 		    dmar_read4(unit, DMAR_IEDATA_REG),
117968eeb96aSKonstantin Belousov 		    dmar_read4(unit, DMAR_IEADDR_REG),
118068eeb96aSKonstantin Belousov 		    dmar_read4(unit, DMAR_IEUADDR_REG));
118168eeb96aSKonstantin Belousov 		if (unit->qi_enabled) {
118268eeb96aSKonstantin Belousov 			db_printf("qi is enabled: queue @0x%jx (IQA 0x%jx) "
118368eeb96aSKonstantin Belousov 			    "size 0x%jx\n"
118468eeb96aSKonstantin Belousov 		    "  head 0x%x tail 0x%x avail 0x%x status 0x%x ctrl 0x%x\n"
1185fc8da73bSKonstantin Belousov 		    "  hw compl 0x%jx@%p/phys@%jx next seq 0x%x gen 0x%x\n",
1186ad794e6dSKonstantin Belousov 			    (uintmax_t)unit->x86c.inv_queue,
118768eeb96aSKonstantin Belousov 			    (uintmax_t)dmar_read8(unit, DMAR_IQA_REG),
1188ad794e6dSKonstantin Belousov 			    (uintmax_t)unit->x86c.inv_queue_size,
118968eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_IQH_REG),
119068eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_IQT_REG),
1191ad794e6dSKonstantin Belousov 			    unit->x86c.inv_queue_avail,
119268eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_ICS_REG),
119368eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_IECTL_REG),
1194fc8da73bSKonstantin Belousov 			    (uintmax_t)unit->x86c.inv_waitd_seq_hw,
1195ad794e6dSKonstantin Belousov 			    &unit->x86c.inv_waitd_seq_hw,
1196ad794e6dSKonstantin Belousov 			    (uintmax_t)unit->x86c.inv_waitd_seq_hw_phys,
1197ad794e6dSKonstantin Belousov 			    unit->x86c.inv_waitd_seq,
1198ad794e6dSKonstantin Belousov 			    unit->x86c.inv_waitd_gen);
119968eeb96aSKonstantin Belousov 		} else {
120068eeb96aSKonstantin Belousov 			db_printf("qi is disabled\n");
120168eeb96aSKonstantin Belousov 		}
120268eeb96aSKonstantin Belousov 	}
12031abfd355SKonstantin Belousov 	if (show_domains) {
12041abfd355SKonstantin Belousov 		db_printf("domains:\n");
12051abfd355SKonstantin Belousov 		LIST_FOREACH(domain, &unit->domains, link) {
12061abfd355SKonstantin Belousov 			dmar_print_domain(domain, show_mappings);
120786be9f0dSKonstantin Belousov 			if (db_pager_quit)
120886be9f0dSKonstantin Belousov 				break;
120986be9f0dSKonstantin Belousov 		}
121086be9f0dSKonstantin Belousov 	}
121186be9f0dSKonstantin Belousov }
121286be9f0dSKonstantin Belousov 
121386be9f0dSKonstantin Belousov DB_SHOW_COMMAND(dmar, db_dmar_print)
121486be9f0dSKonstantin Belousov {
12151abfd355SKonstantin Belousov 	bool show_domains, show_mappings;
121686be9f0dSKonstantin Belousov 
12171abfd355SKonstantin Belousov 	show_domains = strchr(modif, 'd') != NULL;
121886be9f0dSKonstantin Belousov 	show_mappings = strchr(modif, 'm') != NULL;
121986be9f0dSKonstantin Belousov 	if (!have_addr) {
12201abfd355SKonstantin Belousov 		db_printf("usage: show dmar [/d] [/m] index\n");
122186be9f0dSKonstantin Belousov 		return;
122286be9f0dSKonstantin Belousov 	}
12231abfd355SKonstantin Belousov 	dmar_print_one((int)addr, show_domains, show_mappings);
122486be9f0dSKonstantin Belousov }
122586be9f0dSKonstantin Belousov 
122686be9f0dSKonstantin Belousov DB_SHOW_ALL_COMMAND(dmars, db_show_all_dmars)
122786be9f0dSKonstantin Belousov {
122886be9f0dSKonstantin Belousov 	int i;
12291abfd355SKonstantin Belousov 	bool show_domains, show_mappings;
123086be9f0dSKonstantin Belousov 
12311abfd355SKonstantin Belousov 	show_domains = strchr(modif, 'd') != NULL;
123286be9f0dSKonstantin Belousov 	show_mappings = strchr(modif, 'm') != NULL;
123386be9f0dSKonstantin Belousov 
123486be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
12351abfd355SKonstantin Belousov 		dmar_print_one(i, show_domains, show_mappings);
123686be9f0dSKonstantin Belousov 		if (db_pager_quit)
123786be9f0dSKonstantin Belousov 			break;
123886be9f0dSKonstantin Belousov 	}
123986be9f0dSKonstantin Belousov }
124086be9f0dSKonstantin Belousov #endif
124159e37c8aSRuslan Bukin 
124265b133e5SKonstantin Belousov static struct iommu_unit *
124365b133e5SKonstantin Belousov dmar_find_method(device_t dev, bool verbose)
124459e37c8aSRuslan Bukin {
124559e37c8aSRuslan Bukin 	struct dmar_unit *dmar;
124659e37c8aSRuslan Bukin 
124759e37c8aSRuslan Bukin 	dmar = dmar_find(dev, verbose);
124859e37c8aSRuslan Bukin 	return (&dmar->iommu);
124959e37c8aSRuslan Bukin }
125065b133e5SKonstantin Belousov 
1251ad794e6dSKonstantin Belousov static struct x86_unit_common *
1252ad794e6dSKonstantin Belousov dmar_get_x86_common(struct iommu_unit *unit)
1253ad794e6dSKonstantin Belousov {
1254ad794e6dSKonstantin Belousov 	struct dmar_unit *dmar;
1255ad794e6dSKonstantin Belousov 
1256ad794e6dSKonstantin Belousov 	dmar = IOMMU2DMAR(unit);
1257ad794e6dSKonstantin Belousov 	return (&dmar->x86c);
1258ad794e6dSKonstantin Belousov }
1259ad794e6dSKonstantin Belousov 
1260ba33e74cSKonstantin Belousov static void
1261ba33e74cSKonstantin Belousov dmar_unit_pre_instantiate_ctx(struct iommu_unit *unit)
1262ba33e74cSKonstantin Belousov {
1263ba33e74cSKonstantin Belousov 	dmar_quirks_pre_use(unit);
1264ba33e74cSKonstantin Belousov 	dmar_instantiate_rmrr_ctxs(unit);
1265ba33e74cSKonstantin Belousov }
1266ba33e74cSKonstantin Belousov 
126765b133e5SKonstantin Belousov static struct x86_iommu dmar_x86_iommu = {
1268ad794e6dSKonstantin Belousov 	.get_x86_common = dmar_get_x86_common,
1269ba33e74cSKonstantin Belousov 	.unit_pre_instantiate_ctx = dmar_unit_pre_instantiate_ctx,
127065b133e5SKonstantin Belousov 	.domain_unload_entry = dmar_domain_unload_entry,
127165b133e5SKonstantin Belousov 	.domain_unload = dmar_domain_unload,
127265b133e5SKonstantin Belousov 	.get_ctx = dmar_get_ctx,
127365b133e5SKonstantin Belousov 	.free_ctx_locked = dmar_free_ctx_locked_method,
127465b133e5SKonstantin Belousov 	.free_ctx = dmar_free_ctx_method,
127565b133e5SKonstantin Belousov 	.find = dmar_find_method,
127665b133e5SKonstantin Belousov 	.alloc_msi_intr = dmar_alloc_msi_intr,
127765b133e5SKonstantin Belousov 	.map_msi_intr = dmar_map_msi_intr,
127865b133e5SKonstantin Belousov 	.unmap_msi_intr = dmar_unmap_msi_intr,
127965b133e5SKonstantin Belousov 	.map_ioapic_intr = dmar_map_ioapic_intr,
128065b133e5SKonstantin Belousov 	.unmap_ioapic_intr = dmar_unmap_ioapic_intr,
128165b133e5SKonstantin Belousov };
128265b133e5SKonstantin Belousov 
128365b133e5SKonstantin Belousov static void
128465b133e5SKonstantin Belousov x86_iommu_set_intel(void *arg __unused)
128565b133e5SKonstantin Belousov {
128665b133e5SKonstantin Belousov 	if (cpu_vendor_id == CPU_VENDOR_INTEL)
128765b133e5SKonstantin Belousov 		set_x86_iommu(&dmar_x86_iommu);
128865b133e5SKonstantin Belousov }
128965b133e5SKonstantin Belousov 
129065b133e5SKonstantin Belousov SYSINIT(x86_iommu, SI_SUB_TUNABLES, SI_ORDER_ANY, x86_iommu_set_intel, NULL);
1291