xref: /freebsd/sys/x86/iommu/intel_drv.c (revision e2e050c8ef733138fc6a9e514e4b856fefbc3ff1)
186be9f0dSKonstantin Belousov /*-
2ebf5747bSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3ebf5747bSPedro F. Giffuni  *
40a110d5bSKonstantin Belousov  * Copyright (c) 2013-2015 The FreeBSD Foundation
586be9f0dSKonstantin Belousov  * All rights reserved.
686be9f0dSKonstantin Belousov  *
786be9f0dSKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
886be9f0dSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
986be9f0dSKonstantin Belousov  *
1086be9f0dSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
1186be9f0dSKonstantin Belousov  * modification, are permitted provided that the following conditions
1286be9f0dSKonstantin Belousov  * are met:
1386be9f0dSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
1486be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
1586be9f0dSKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
1686be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
1786be9f0dSKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
1886be9f0dSKonstantin Belousov  *
1986be9f0dSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2086be9f0dSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2186be9f0dSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2286be9f0dSKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2386be9f0dSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2486be9f0dSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2586be9f0dSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2686be9f0dSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2786be9f0dSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2886be9f0dSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2986be9f0dSKonstantin Belousov  * SUCH DAMAGE.
3086be9f0dSKonstantin Belousov  */
3186be9f0dSKonstantin Belousov 
3286be9f0dSKonstantin Belousov #include <sys/cdefs.h>
3386be9f0dSKonstantin Belousov __FBSDID("$FreeBSD$");
3486be9f0dSKonstantin Belousov 
3586be9f0dSKonstantin Belousov #include "opt_acpi.h"
36e7d939bdSMarcel Moolenaar #if defined(__amd64__)
3786be9f0dSKonstantin Belousov #define	DEV_APIC
3886be9f0dSKonstantin Belousov #else
3986be9f0dSKonstantin Belousov #include "opt_apic.h"
4086be9f0dSKonstantin Belousov #endif
4186be9f0dSKonstantin Belousov #include "opt_ddb.h"
4286be9f0dSKonstantin Belousov 
4386be9f0dSKonstantin Belousov #include <sys/param.h>
4486be9f0dSKonstantin Belousov #include <sys/bus.h>
4586be9f0dSKonstantin Belousov #include <sys/kernel.h>
4686be9f0dSKonstantin Belousov #include <sys/lock.h>
4786be9f0dSKonstantin Belousov #include <sys/malloc.h>
4886be9f0dSKonstantin Belousov #include <sys/memdesc.h>
4986be9f0dSKonstantin Belousov #include <sys/module.h>
50*e2e050c8SConrad Meyer #include <sys/mutex.h>
5186be9f0dSKonstantin Belousov #include <sys/rman.h>
5286be9f0dSKonstantin Belousov #include <sys/rwlock.h>
5386be9f0dSKonstantin Belousov #include <sys/smp.h>
5486be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
5586be9f0dSKonstantin Belousov #include <sys/tree.h>
560a110d5bSKonstantin Belousov #include <sys/vmem.h>
5786be9f0dSKonstantin Belousov #include <machine/bus.h>
58f9feb091SKonstantin Belousov #include <machine/pci_cfgreg.h>
5986be9f0dSKonstantin Belousov #include <contrib/dev/acpica/include/acpi.h>
6086be9f0dSKonstantin Belousov #include <contrib/dev/acpica/include/accommon.h>
6186be9f0dSKonstantin Belousov #include <dev/acpica/acpivar.h>
6286be9f0dSKonstantin Belousov #include <vm/vm.h>
6386be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
6486be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
6586be9f0dSKonstantin Belousov #include <vm/vm_object.h>
6686be9f0dSKonstantin Belousov #include <vm/vm_page.h>
6786be9f0dSKonstantin Belousov #include <vm/vm_pager.h>
6886be9f0dSKonstantin Belousov #include <vm/vm_map.h>
6986be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h>
7086be9f0dSKonstantin Belousov #include <x86/iommu/intel_reg.h>
7186be9f0dSKonstantin Belousov #include <x86/iommu/busdma_dmar.h>
7286be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
730a110d5bSKonstantin Belousov #include <dev/pci/pcireg.h>
7486be9f0dSKonstantin Belousov #include <dev/pci/pcivar.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 devclass_t dmar_devclass;
8886be9f0dSKonstantin Belousov static device_t *dmar_devs;
8986be9f0dSKonstantin Belousov static int dmar_devcnt;
9086be9f0dSKonstantin Belousov 
9186be9f0dSKonstantin Belousov typedef int (*dmar_iter_t)(ACPI_DMAR_HEADER *, void *);
9286be9f0dSKonstantin Belousov 
9386be9f0dSKonstantin Belousov static void
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
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 *
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
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 
16486be9f0dSKonstantin Belousov 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;
17886be9f0dSKonstantin Belousov #ifdef INVARIANTS
17986be9f0dSKonstantin Belousov 	TUNABLE_INT_FETCH("hw.dmar.check_free", &dmar_check_free);
18086be9f0dSKonstantin Belousov #endif
18186be9f0dSKonstantin Belousov 	status = AcpiGetTable(ACPI_SIG_DMAR, 1, (ACPI_TABLE_HEADER **)&dmartbl);
18286be9f0dSKonstantin Belousov 	if (ACPI_FAILURE(status))
18386be9f0dSKonstantin Belousov 		return;
18486be9f0dSKonstantin Belousov 	haw = dmartbl->Width + 1;
18586be9f0dSKonstantin Belousov 	if ((1ULL << (haw + 1)) > BUS_SPACE_MAXADDR)
18686be9f0dSKonstantin Belousov 		dmar_high = BUS_SPACE_MAXADDR;
18786be9f0dSKonstantin Belousov 	else
18886be9f0dSKonstantin Belousov 		dmar_high = 1ULL << (haw + 1);
18986be9f0dSKonstantin Belousov 	if (bootverbose) {
19086be9f0dSKonstantin Belousov 		printf("DMAR HAW=%d flags=<%b>\n", dmartbl->Width,
19186be9f0dSKonstantin Belousov 		    (unsigned)dmartbl->Flags,
19286be9f0dSKonstantin Belousov 		    "\020\001INTR_REMAP\002X2APIC_OPT_OUT");
19386be9f0dSKonstantin Belousov 	}
1943dd3c450SKonstantin Belousov 	AcpiPutTable((ACPI_TABLE_HEADER *)dmartbl);
19586be9f0dSKonstantin Belousov 
19686be9f0dSKonstantin Belousov 	dmar_iterate_tbl(dmar_count_iter, NULL);
19786be9f0dSKonstantin Belousov 	if (dmar_devcnt == 0)
19886be9f0dSKonstantin Belousov 		return;
19986be9f0dSKonstantin Belousov 	dmar_devs = malloc(sizeof(device_t) * dmar_devcnt, M_DEVBUF,
20086be9f0dSKonstantin Belousov 	    M_WAITOK | M_ZERO);
20186be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
20286be9f0dSKonstantin Belousov 		dmarh = dmar_find_by_index(i);
20386be9f0dSKonstantin Belousov 		if (dmarh == NULL) {
20486be9f0dSKonstantin Belousov 			printf("dmar_identify: cannot find HWUNIT %d\n", i);
20586be9f0dSKonstantin Belousov 			continue;
20686be9f0dSKonstantin Belousov 		}
20786be9f0dSKonstantin Belousov 		dmar_devs[i] = BUS_ADD_CHILD(parent, 1, "dmar", i);
20886be9f0dSKonstantin Belousov 		if (dmar_devs[i] == NULL) {
20986be9f0dSKonstantin Belousov 			printf("dmar_identify: cannot create instance %d\n", i);
21086be9f0dSKonstantin Belousov 			continue;
21186be9f0dSKonstantin Belousov 		}
21286be9f0dSKonstantin Belousov 		error = bus_set_resource(dmar_devs[i], SYS_RES_MEMORY,
21386be9f0dSKonstantin Belousov 		    DMAR_REG_RID, dmarh->Address, PAGE_SIZE);
21486be9f0dSKonstantin Belousov 		if (error != 0) {
21586be9f0dSKonstantin Belousov 			printf(
21686be9f0dSKonstantin Belousov 	"dmar%d: unable to alloc register window at 0x%08jx: error %d\n",
21786be9f0dSKonstantin Belousov 			    i, (uintmax_t)dmarh->Address, error);
21886be9f0dSKonstantin Belousov 			device_delete_child(parent, dmar_devs[i]);
21986be9f0dSKonstantin Belousov 			dmar_devs[i] = NULL;
22086be9f0dSKonstantin Belousov 		}
22186be9f0dSKonstantin Belousov 	}
22286be9f0dSKonstantin Belousov }
22386be9f0dSKonstantin Belousov 
22486be9f0dSKonstantin Belousov static int
22586be9f0dSKonstantin Belousov dmar_probe(device_t dev)
22686be9f0dSKonstantin Belousov {
22786be9f0dSKonstantin Belousov 
22886be9f0dSKonstantin Belousov 	if (acpi_get_handle(dev) != NULL)
22986be9f0dSKonstantin Belousov 		return (ENXIO);
23086be9f0dSKonstantin Belousov 	device_set_desc(dev, "DMA remap");
2313100f7dfSKonstantin Belousov 	return (BUS_PROBE_NOWILDCARD);
23286be9f0dSKonstantin Belousov }
23386be9f0dSKonstantin Belousov 
23486be9f0dSKonstantin Belousov static void
23568eeb96aSKonstantin Belousov dmar_release_intr(device_t dev, struct dmar_unit *unit, int idx)
23668eeb96aSKonstantin Belousov {
23768eeb96aSKonstantin Belousov 	struct dmar_msi_data *dmd;
23868eeb96aSKonstantin Belousov 
23968eeb96aSKonstantin Belousov 	dmd = &unit->intrs[idx];
24068eeb96aSKonstantin Belousov 	if (dmd->irq == -1)
24168eeb96aSKonstantin Belousov 		return;
24268eeb96aSKonstantin Belousov 	bus_teardown_intr(dev, dmd->irq_res, dmd->intr_handle);
24368eeb96aSKonstantin Belousov 	bus_release_resource(dev, SYS_RES_IRQ, dmd->irq_rid, dmd->irq_res);
24468eeb96aSKonstantin Belousov 	bus_delete_resource(dev, SYS_RES_IRQ, dmd->irq_rid);
24568eeb96aSKonstantin Belousov 	PCIB_RELEASE_MSIX(device_get_parent(device_get_parent(dev)),
24668eeb96aSKonstantin Belousov 	    dev, dmd->irq);
24768eeb96aSKonstantin Belousov 	dmd->irq = -1;
24868eeb96aSKonstantin Belousov }
24968eeb96aSKonstantin Belousov 
25068eeb96aSKonstantin Belousov static void
25186be9f0dSKonstantin Belousov dmar_release_resources(device_t dev, struct dmar_unit *unit)
25286be9f0dSKonstantin Belousov {
25368eeb96aSKonstantin Belousov 	int i;
25486be9f0dSKonstantin Belousov 
25586be9f0dSKonstantin Belousov 	dmar_fini_busdma(unit);
2560a110d5bSKonstantin Belousov 	dmar_fini_irt(unit);
25768eeb96aSKonstantin Belousov 	dmar_fini_qi(unit);
25886be9f0dSKonstantin Belousov 	dmar_fini_fault_log(unit);
25968eeb96aSKonstantin Belousov 	for (i = 0; i < DMAR_INTR_TOTAL; i++)
26068eeb96aSKonstantin Belousov 		dmar_release_intr(dev, unit, i);
26186be9f0dSKonstantin Belousov 	if (unit->regs != NULL) {
26286be9f0dSKonstantin Belousov 		bus_deactivate_resource(dev, SYS_RES_MEMORY, unit->reg_rid,
26386be9f0dSKonstantin Belousov 		    unit->regs);
26486be9f0dSKonstantin Belousov 		bus_release_resource(dev, SYS_RES_MEMORY, unit->reg_rid,
26586be9f0dSKonstantin Belousov 		    unit->regs);
26686be9f0dSKonstantin Belousov 		unit->regs = NULL;
26786be9f0dSKonstantin Belousov 	}
26886be9f0dSKonstantin Belousov 	if (unit->domids != NULL) {
26986be9f0dSKonstantin Belousov 		delete_unrhdr(unit->domids);
27086be9f0dSKonstantin Belousov 		unit->domids = NULL;
27186be9f0dSKonstantin Belousov 	}
27286be9f0dSKonstantin Belousov 	if (unit->ctx_obj != NULL) {
27386be9f0dSKonstantin Belousov 		vm_object_deallocate(unit->ctx_obj);
27486be9f0dSKonstantin Belousov 		unit->ctx_obj = NULL;
27586be9f0dSKonstantin Belousov 	}
27686be9f0dSKonstantin Belousov }
27786be9f0dSKonstantin Belousov 
27886be9f0dSKonstantin Belousov static int
27968eeb96aSKonstantin Belousov dmar_alloc_irq(device_t dev, struct dmar_unit *unit, int idx)
28086be9f0dSKonstantin Belousov {
28186be9f0dSKonstantin Belousov 	device_t pcib;
28268eeb96aSKonstantin Belousov 	struct dmar_msi_data *dmd;
28386be9f0dSKonstantin Belousov 	uint64_t msi_addr;
28486be9f0dSKonstantin Belousov 	uint32_t msi_data;
28586be9f0dSKonstantin Belousov 	int error;
28686be9f0dSKonstantin Belousov 
28768eeb96aSKonstantin Belousov 	dmd = &unit->intrs[idx];
28886be9f0dSKonstantin Belousov 	pcib = device_get_parent(device_get_parent(dev)); /* Really not pcib */
28968eeb96aSKonstantin Belousov 	error = PCIB_ALLOC_MSIX(pcib, dev, &dmd->irq);
29086be9f0dSKonstantin Belousov 	if (error != 0) {
29168eeb96aSKonstantin Belousov 		device_printf(dev, "cannot allocate %s interrupt, %d\n",
29268eeb96aSKonstantin Belousov 		    dmd->name, error);
29386be9f0dSKonstantin Belousov 		goto err1;
29486be9f0dSKonstantin Belousov 	}
29568eeb96aSKonstantin Belousov 	error = bus_set_resource(dev, SYS_RES_IRQ, dmd->irq_rid,
29668eeb96aSKonstantin Belousov 	    dmd->irq, 1);
29786be9f0dSKonstantin Belousov 	if (error != 0) {
29868eeb96aSKonstantin Belousov 		device_printf(dev, "cannot set %s interrupt resource, %d\n",
29968eeb96aSKonstantin Belousov 		    dmd->name, error);
30086be9f0dSKonstantin Belousov 		goto err2;
30186be9f0dSKonstantin Belousov 	}
30268eeb96aSKonstantin Belousov 	dmd->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
30368eeb96aSKonstantin Belousov 	    &dmd->irq_rid, RF_ACTIVE);
30468eeb96aSKonstantin Belousov 	if (dmd->irq_res == NULL) {
30568eeb96aSKonstantin Belousov 		device_printf(dev,
30668eeb96aSKonstantin Belousov 		    "cannot allocate resource for %s interrupt\n", dmd->name);
30786be9f0dSKonstantin Belousov 		error = ENXIO;
30886be9f0dSKonstantin Belousov 		goto err3;
30986be9f0dSKonstantin Belousov 	}
31068eeb96aSKonstantin Belousov 	error = bus_setup_intr(dev, dmd->irq_res, INTR_TYPE_MISC,
31168eeb96aSKonstantin Belousov 	    dmd->handler, NULL, unit, &dmd->intr_handle);
31286be9f0dSKonstantin Belousov 	if (error != 0) {
31368eeb96aSKonstantin Belousov 		device_printf(dev, "cannot setup %s interrupt, %d\n",
31468eeb96aSKonstantin Belousov 		    dmd->name, error);
31586be9f0dSKonstantin Belousov 		goto err4;
31686be9f0dSKonstantin Belousov 	}
317f454e7ebSJohn Baldwin 	bus_describe_intr(dev, dmd->irq_res, dmd->intr_handle, "%s", dmd->name);
31868eeb96aSKonstantin Belousov 	error = PCIB_MAP_MSI(pcib, dev, dmd->irq, &msi_addr, &msi_data);
31986be9f0dSKonstantin Belousov 	if (error != 0) {
32068eeb96aSKonstantin Belousov 		device_printf(dev, "cannot map %s interrupt, %d\n",
32168eeb96aSKonstantin Belousov 		    dmd->name, error);
32286be9f0dSKonstantin Belousov 		goto err5;
32386be9f0dSKonstantin Belousov 	}
32468eeb96aSKonstantin Belousov 	dmar_write4(unit, dmd->msi_data_reg, msi_data);
32568eeb96aSKonstantin Belousov 	dmar_write4(unit, dmd->msi_addr_reg, msi_addr);
32686be9f0dSKonstantin Belousov 	/* Only for xAPIC mode */
32768eeb96aSKonstantin Belousov 	dmar_write4(unit, dmd->msi_uaddr_reg, msi_addr >> 32);
32886be9f0dSKonstantin Belousov 	return (0);
32986be9f0dSKonstantin Belousov 
33086be9f0dSKonstantin Belousov err5:
33168eeb96aSKonstantin Belousov 	bus_teardown_intr(dev, dmd->irq_res, dmd->intr_handle);
33286be9f0dSKonstantin Belousov err4:
33368eeb96aSKonstantin Belousov 	bus_release_resource(dev, SYS_RES_IRQ, dmd->irq_rid, dmd->irq_res);
33486be9f0dSKonstantin Belousov err3:
33568eeb96aSKonstantin Belousov 	bus_delete_resource(dev, SYS_RES_IRQ, dmd->irq_rid);
33686be9f0dSKonstantin Belousov err2:
33768eeb96aSKonstantin Belousov 	PCIB_RELEASE_MSIX(pcib, dev, dmd->irq);
33868eeb96aSKonstantin Belousov 	dmd->irq = -1;
33986be9f0dSKonstantin Belousov err1:
34086be9f0dSKonstantin Belousov 	return (error);
34186be9f0dSKonstantin Belousov }
34286be9f0dSKonstantin Belousov 
34386be9f0dSKonstantin Belousov #ifdef DEV_APIC
34486be9f0dSKonstantin Belousov static int
34586be9f0dSKonstantin Belousov dmar_remap_intr(device_t dev, device_t child, u_int irq)
34686be9f0dSKonstantin Belousov {
34786be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
34868eeb96aSKonstantin Belousov 	struct dmar_msi_data *dmd;
34986be9f0dSKonstantin Belousov 	uint64_t msi_addr;
35086be9f0dSKonstantin Belousov 	uint32_t msi_data;
35168eeb96aSKonstantin Belousov 	int i, error;
35286be9f0dSKonstantin Belousov 
35386be9f0dSKonstantin Belousov 	unit = device_get_softc(dev);
35468eeb96aSKonstantin Belousov 	for (i = 0; i < DMAR_INTR_TOTAL; i++) {
35568eeb96aSKonstantin Belousov 		dmd = &unit->intrs[i];
35668eeb96aSKonstantin Belousov 		if (irq == dmd->irq) {
35768eeb96aSKonstantin Belousov 			error = PCIB_MAP_MSI(device_get_parent(
35868eeb96aSKonstantin Belousov 			    device_get_parent(dev)),
35968eeb96aSKonstantin Belousov 			    dev, irq, &msi_addr, &msi_data);
36086be9f0dSKonstantin Belousov 			if (error != 0)
36186be9f0dSKonstantin Belousov 				return (error);
36268eeb96aSKonstantin Belousov 			DMAR_LOCK(unit);
36368eeb96aSKonstantin Belousov 			(dmd->disable_intr)(unit);
36468eeb96aSKonstantin Belousov 			dmar_write4(unit, dmd->msi_data_reg, msi_data);
36568eeb96aSKonstantin Belousov 			dmar_write4(unit, dmd->msi_addr_reg, msi_addr);
36668eeb96aSKonstantin Belousov 			dmar_write4(unit, dmd->msi_uaddr_reg, msi_addr >> 32);
36768eeb96aSKonstantin Belousov 			(dmd->enable_intr)(unit);
36868eeb96aSKonstantin Belousov 			DMAR_UNLOCK(unit);
36986be9f0dSKonstantin Belousov 			return (0);
37086be9f0dSKonstantin Belousov 		}
37168eeb96aSKonstantin Belousov 	}
37268eeb96aSKonstantin Belousov 	return (ENOENT);
37368eeb96aSKonstantin Belousov }
37486be9f0dSKonstantin Belousov #endif
37586be9f0dSKonstantin Belousov 
37686be9f0dSKonstantin Belousov static void
37786be9f0dSKonstantin Belousov dmar_print_caps(device_t dev, struct dmar_unit *unit,
37886be9f0dSKonstantin Belousov     ACPI_DMAR_HARDWARE_UNIT *dmaru)
37986be9f0dSKonstantin Belousov {
38086be9f0dSKonstantin Belousov 	uint32_t caphi, ecaphi;
38186be9f0dSKonstantin Belousov 
38286be9f0dSKonstantin Belousov 	device_printf(dev, "regs@0x%08jx, ver=%d.%d, seg=%d, flags=<%b>\n",
38386be9f0dSKonstantin Belousov 	    (uintmax_t)dmaru->Address, DMAR_MAJOR_VER(unit->hw_ver),
38486be9f0dSKonstantin Belousov 	    DMAR_MINOR_VER(unit->hw_ver), dmaru->Segment,
38586be9f0dSKonstantin Belousov 	    dmaru->Flags, "\020\001INCLUDE_ALL_PCI");
38686be9f0dSKonstantin Belousov 	caphi = unit->hw_cap >> 32;
38786be9f0dSKonstantin Belousov 	device_printf(dev, "cap=%b,", (u_int)unit->hw_cap,
38886be9f0dSKonstantin Belousov 	    "\020\004AFL\005WBF\006PLMR\007PHMR\010CM\027ZLR\030ISOCH");
389e17c0a1eSKonstantin Belousov 	printf("%b, ", caphi, "\020\010PSI\027DWD\030DRD\031FL1GP\034PSI");
39086be9f0dSKonstantin Belousov 	printf("ndoms=%d, sagaw=%d, mgaw=%d, fro=%d, nfr=%d, superp=%d",
39186be9f0dSKonstantin Belousov 	    DMAR_CAP_ND(unit->hw_cap), DMAR_CAP_SAGAW(unit->hw_cap),
39286be9f0dSKonstantin Belousov 	    DMAR_CAP_MGAW(unit->hw_cap), DMAR_CAP_FRO(unit->hw_cap),
39386be9f0dSKonstantin Belousov 	    DMAR_CAP_NFR(unit->hw_cap), DMAR_CAP_SPS(unit->hw_cap));
39486be9f0dSKonstantin Belousov 	if ((unit->hw_cap & DMAR_CAP_PSI) != 0)
39586be9f0dSKonstantin Belousov 		printf(", mamv=%d", DMAR_CAP_MAMV(unit->hw_cap));
39686be9f0dSKonstantin Belousov 	printf("\n");
39786be9f0dSKonstantin Belousov 	ecaphi = unit->hw_ecap >> 32;
39886be9f0dSKonstantin Belousov 	device_printf(dev, "ecap=%b,", (u_int)unit->hw_ecap,
399e17c0a1eSKonstantin Belousov 	    "\020\001C\002QI\003DI\004IR\005EIM\007PT\010SC\031ECS\032MTS"
400e17c0a1eSKonstantin Belousov 	    "\033NEST\034DIS\035PASID\036PRS\037ERS\040SRS");
401e17c0a1eSKonstantin Belousov 	printf("%b, ", ecaphi, "\020\002NWFS\003EAFS");
40286be9f0dSKonstantin Belousov 	printf("mhmw=%d, iro=%d\n", DMAR_ECAP_MHMV(unit->hw_ecap),
40386be9f0dSKonstantin Belousov 	    DMAR_ECAP_IRO(unit->hw_ecap));
40486be9f0dSKonstantin Belousov }
40586be9f0dSKonstantin Belousov 
40686be9f0dSKonstantin Belousov static int
40786be9f0dSKonstantin Belousov dmar_attach(device_t dev)
40886be9f0dSKonstantin Belousov {
40986be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
41086be9f0dSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *dmaru;
411476358b3SKonstantin Belousov 	uint64_t timeout;
41268eeb96aSKonstantin Belousov 	int i, error;
41386be9f0dSKonstantin Belousov 
41486be9f0dSKonstantin Belousov 	unit = device_get_softc(dev);
41586be9f0dSKonstantin Belousov 	unit->dev = dev;
41686be9f0dSKonstantin Belousov 	unit->unit = device_get_unit(dev);
41786be9f0dSKonstantin Belousov 	dmaru = dmar_find_by_index(unit->unit);
41886be9f0dSKonstantin Belousov 	if (dmaru == NULL)
41986be9f0dSKonstantin Belousov 		return (EINVAL);
42086be9f0dSKonstantin Belousov 	unit->segment = dmaru->Segment;
42186be9f0dSKonstantin Belousov 	unit->base = dmaru->Address;
42286be9f0dSKonstantin Belousov 	unit->reg_rid = DMAR_REG_RID;
42386be9f0dSKonstantin Belousov 	unit->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
42486be9f0dSKonstantin Belousov 	    &unit->reg_rid, RF_ACTIVE);
42586be9f0dSKonstantin Belousov 	if (unit->regs == NULL) {
42686be9f0dSKonstantin Belousov 		device_printf(dev, "cannot allocate register window\n");
42786be9f0dSKonstantin Belousov 		return (ENOMEM);
42886be9f0dSKonstantin Belousov 	}
42986be9f0dSKonstantin Belousov 	unit->hw_ver = dmar_read4(unit, DMAR_VER_REG);
43086be9f0dSKonstantin Belousov 	unit->hw_cap = dmar_read8(unit, DMAR_CAP_REG);
43186be9f0dSKonstantin Belousov 	unit->hw_ecap = dmar_read8(unit, DMAR_ECAP_REG);
43286be9f0dSKonstantin Belousov 	if (bootverbose)
43386be9f0dSKonstantin Belousov 		dmar_print_caps(dev, unit, dmaru);
43486be9f0dSKonstantin Belousov 	dmar_quirks_post_ident(unit);
43586be9f0dSKonstantin Belousov 
436476358b3SKonstantin Belousov 	timeout = dmar_get_timeout();
437476358b3SKonstantin Belousov 	TUNABLE_UINT64_FETCH("hw.dmar.timeout", &timeout);
438476358b3SKonstantin Belousov 	dmar_update_timeout(timeout);
439476358b3SKonstantin Belousov 
44068eeb96aSKonstantin Belousov 	for (i = 0; i < DMAR_INTR_TOTAL; i++)
44168eeb96aSKonstantin Belousov 		unit->intrs[i].irq = -1;
44268eeb96aSKonstantin Belousov 
44368eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].name = "fault";
44468eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].irq_rid = DMAR_FAULT_IRQ_RID;
44568eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].handler = dmar_fault_intr;
44668eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].msi_data_reg = DMAR_FEDATA_REG;
44768eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].msi_addr_reg = DMAR_FEADDR_REG;
44868eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].msi_uaddr_reg = DMAR_FEUADDR_REG;
44968eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].enable_intr = dmar_enable_fault_intr;
45068eeb96aSKonstantin Belousov 	unit->intrs[DMAR_INTR_FAULT].disable_intr = dmar_disable_fault_intr;
45168eeb96aSKonstantin Belousov 	error = dmar_alloc_irq(dev, unit, DMAR_INTR_FAULT);
45286be9f0dSKonstantin Belousov 	if (error != 0) {
45386be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
45486be9f0dSKonstantin Belousov 		return (error);
45586be9f0dSKonstantin Belousov 	}
45668eeb96aSKonstantin Belousov 	if (DMAR_HAS_QI(unit)) {
45768eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].name = "qi";
45868eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].irq_rid = DMAR_QI_IRQ_RID;
45968eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].handler = dmar_qi_intr;
46068eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].msi_data_reg = DMAR_IEDATA_REG;
46168eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].msi_addr_reg = DMAR_IEADDR_REG;
46268eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].msi_uaddr_reg = DMAR_IEUADDR_REG;
46368eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].enable_intr = dmar_enable_qi_intr;
46468eeb96aSKonstantin Belousov 		unit->intrs[DMAR_INTR_QI].disable_intr = dmar_disable_qi_intr;
46568eeb96aSKonstantin Belousov 		error = dmar_alloc_irq(dev, unit, DMAR_INTR_QI);
46668eeb96aSKonstantin Belousov 		if (error != 0) {
46768eeb96aSKonstantin Belousov 			dmar_release_resources(dev, unit);
46868eeb96aSKonstantin Belousov 			return (error);
46968eeb96aSKonstantin Belousov 		}
47068eeb96aSKonstantin Belousov 	}
47168eeb96aSKonstantin Belousov 
47286be9f0dSKonstantin Belousov 	mtx_init(&unit->lock, "dmarhw", NULL, MTX_DEF);
47386be9f0dSKonstantin Belousov 	unit->domids = new_unrhdr(0, dmar_nd2mask(DMAR_CAP_ND(unit->hw_cap)),
47486be9f0dSKonstantin Belousov 	    &unit->lock);
4751abfd355SKonstantin Belousov 	LIST_INIT(&unit->domains);
47686be9f0dSKonstantin Belousov 
47786be9f0dSKonstantin Belousov 	/*
47886be9f0dSKonstantin Belousov 	 * 9.2 "Context Entry":
47986be9f0dSKonstantin Belousov 	 * When Caching Mode (CM) field is reported as Set, the
48086be9f0dSKonstantin Belousov 	 * domain-id value of zero is architecturally reserved.
48186be9f0dSKonstantin Belousov 	 * Software must not use domain-id value of zero
48286be9f0dSKonstantin Belousov 	 * when CM is Set.
48386be9f0dSKonstantin Belousov 	 */
48486be9f0dSKonstantin Belousov 	if ((unit->hw_cap & DMAR_CAP_CM) != 0)
48586be9f0dSKonstantin Belousov 		alloc_unr_specific(unit->domids, 0);
48686be9f0dSKonstantin Belousov 
48786be9f0dSKonstantin Belousov 	unit->ctx_obj = vm_pager_allocate(OBJT_PHYS, NULL, IDX_TO_OFF(1 +
48886be9f0dSKonstantin Belousov 	    DMAR_CTX_CNT), 0, 0, NULL);
48986be9f0dSKonstantin Belousov 
49086be9f0dSKonstantin Belousov 	/*
49186be9f0dSKonstantin Belousov 	 * Allocate and load the root entry table pointer.  Enable the
49286be9f0dSKonstantin Belousov 	 * address translation after the required invalidations are
49386be9f0dSKonstantin Belousov 	 * done.
49486be9f0dSKonstantin Belousov 	 */
49586be9f0dSKonstantin Belousov 	dmar_pgalloc(unit->ctx_obj, 0, DMAR_PGF_WAITOK | DMAR_PGF_ZERO);
49686be9f0dSKonstantin Belousov 	DMAR_LOCK(unit);
49786be9f0dSKonstantin Belousov 	error = dmar_load_root_entry_ptr(unit);
49886be9f0dSKonstantin Belousov 	if (error != 0) {
49986be9f0dSKonstantin Belousov 		DMAR_UNLOCK(unit);
50086be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
50186be9f0dSKonstantin Belousov 		return (error);
50286be9f0dSKonstantin Belousov 	}
50386be9f0dSKonstantin Belousov 	error = dmar_inv_ctx_glob(unit);
50486be9f0dSKonstantin Belousov 	if (error != 0) {
50586be9f0dSKonstantin Belousov 		DMAR_UNLOCK(unit);
50686be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
50786be9f0dSKonstantin Belousov 		return (error);
50886be9f0dSKonstantin Belousov 	}
50986be9f0dSKonstantin Belousov 	if ((unit->hw_ecap & DMAR_ECAP_DI) != 0) {
51086be9f0dSKonstantin Belousov 		error = dmar_inv_iotlb_glob(unit);
51186be9f0dSKonstantin Belousov 		if (error != 0) {
51286be9f0dSKonstantin Belousov 			DMAR_UNLOCK(unit);
51386be9f0dSKonstantin Belousov 			dmar_release_resources(dev, unit);
51486be9f0dSKonstantin Belousov 			return (error);
51586be9f0dSKonstantin Belousov 		}
51686be9f0dSKonstantin Belousov 	}
51786be9f0dSKonstantin Belousov 
51886be9f0dSKonstantin Belousov 	DMAR_UNLOCK(unit);
51986be9f0dSKonstantin Belousov 	error = dmar_init_fault_log(unit);
52086be9f0dSKonstantin Belousov 	if (error != 0) {
52186be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
52286be9f0dSKonstantin Belousov 		return (error);
52386be9f0dSKonstantin Belousov 	}
52468eeb96aSKonstantin Belousov 	error = dmar_init_qi(unit);
52568eeb96aSKonstantin Belousov 	if (error != 0) {
52668eeb96aSKonstantin Belousov 		dmar_release_resources(dev, unit);
52768eeb96aSKonstantin Belousov 		return (error);
52868eeb96aSKonstantin Belousov 	}
5290a110d5bSKonstantin Belousov 	error = dmar_init_irt(unit);
5300a110d5bSKonstantin Belousov 	if (error != 0) {
5310a110d5bSKonstantin Belousov 		dmar_release_resources(dev, unit);
5320a110d5bSKonstantin Belousov 		return (error);
5330a110d5bSKonstantin Belousov 	}
53486be9f0dSKonstantin Belousov 	error = dmar_init_busdma(unit);
53586be9f0dSKonstantin Belousov 	if (error != 0) {
53686be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
53786be9f0dSKonstantin Belousov 		return (error);
53886be9f0dSKonstantin Belousov 	}
53986be9f0dSKonstantin Belousov 
54086be9f0dSKonstantin Belousov #ifdef NOTYET
54186be9f0dSKonstantin Belousov 	DMAR_LOCK(unit);
54286be9f0dSKonstantin Belousov 	error = dmar_enable_translation(unit);
54386be9f0dSKonstantin Belousov 	if (error != 0) {
54486be9f0dSKonstantin Belousov 		DMAR_UNLOCK(unit);
54586be9f0dSKonstantin Belousov 		dmar_release_resources(dev, unit);
54686be9f0dSKonstantin Belousov 		return (error);
54786be9f0dSKonstantin Belousov 	}
54886be9f0dSKonstantin Belousov 	DMAR_UNLOCK(unit);
54986be9f0dSKonstantin Belousov #endif
55086be9f0dSKonstantin Belousov 
55186be9f0dSKonstantin Belousov 	return (0);
55286be9f0dSKonstantin Belousov }
55386be9f0dSKonstantin Belousov 
55486be9f0dSKonstantin Belousov static int
55586be9f0dSKonstantin Belousov dmar_detach(device_t dev)
55686be9f0dSKonstantin Belousov {
55786be9f0dSKonstantin Belousov 
55886be9f0dSKonstantin Belousov 	return (EBUSY);
55986be9f0dSKonstantin Belousov }
56086be9f0dSKonstantin Belousov 
56186be9f0dSKonstantin Belousov static int
56286be9f0dSKonstantin Belousov dmar_suspend(device_t dev)
56386be9f0dSKonstantin Belousov {
56486be9f0dSKonstantin Belousov 
56586be9f0dSKonstantin Belousov 	return (0);
56686be9f0dSKonstantin Belousov }
56786be9f0dSKonstantin Belousov 
56886be9f0dSKonstantin Belousov static int
56986be9f0dSKonstantin Belousov dmar_resume(device_t dev)
57086be9f0dSKonstantin Belousov {
57186be9f0dSKonstantin Belousov 
57286be9f0dSKonstantin Belousov 	/* XXXKIB */
57386be9f0dSKonstantin Belousov 	return (0);
57486be9f0dSKonstantin Belousov }
57586be9f0dSKonstantin Belousov 
57686be9f0dSKonstantin Belousov static device_method_t dmar_methods[] = {
57786be9f0dSKonstantin Belousov 	DEVMETHOD(device_identify, dmar_identify),
57886be9f0dSKonstantin Belousov 	DEVMETHOD(device_probe, dmar_probe),
57986be9f0dSKonstantin Belousov 	DEVMETHOD(device_attach, dmar_attach),
58086be9f0dSKonstantin Belousov 	DEVMETHOD(device_detach, dmar_detach),
58186be9f0dSKonstantin Belousov 	DEVMETHOD(device_suspend, dmar_suspend),
58286be9f0dSKonstantin Belousov 	DEVMETHOD(device_resume, dmar_resume),
58386be9f0dSKonstantin Belousov #ifdef DEV_APIC
58486be9f0dSKonstantin Belousov 	DEVMETHOD(bus_remap_intr, dmar_remap_intr),
58586be9f0dSKonstantin Belousov #endif
58686be9f0dSKonstantin Belousov 	DEVMETHOD_END
58786be9f0dSKonstantin Belousov };
58886be9f0dSKonstantin Belousov 
58986be9f0dSKonstantin Belousov static driver_t	dmar_driver = {
59086be9f0dSKonstantin Belousov 	"dmar",
59186be9f0dSKonstantin Belousov 	dmar_methods,
59286be9f0dSKonstantin Belousov 	sizeof(struct dmar_unit),
59386be9f0dSKonstantin Belousov };
59486be9f0dSKonstantin Belousov 
59586be9f0dSKonstantin Belousov DRIVER_MODULE(dmar, acpi, dmar_driver, dmar_devclass, 0, 0);
59686be9f0dSKonstantin Belousov MODULE_DEPEND(dmar, acpi, 1, 1, 1);
59786be9f0dSKonstantin Belousov 
59886be9f0dSKonstantin Belousov static void
599f9feb091SKonstantin Belousov dmar_print_path(int busno, int depth, const ACPI_DMAR_PCI_PATH *path)
60086be9f0dSKonstantin Belousov {
60186be9f0dSKonstantin Belousov 	int i;
60286be9f0dSKonstantin Belousov 
603f9feb091SKonstantin Belousov 	printf("[%d, ", busno);
60486be9f0dSKonstantin Belousov 	for (i = 0; i < depth; i++) {
60586be9f0dSKonstantin Belousov 		if (i != 0)
60686be9f0dSKonstantin Belousov 			printf(", ");
60786be9f0dSKonstantin Belousov 		printf("(%d, %d)", path[i].Device, path[i].Function);
60886be9f0dSKonstantin Belousov 	}
609f9feb091SKonstantin Belousov 	printf("]");
61086be9f0dSKonstantin Belousov }
61186be9f0dSKonstantin Belousov 
612f9feb091SKonstantin Belousov int
61386be9f0dSKonstantin Belousov dmar_dev_depth(device_t child)
61486be9f0dSKonstantin Belousov {
61586be9f0dSKonstantin Belousov 	devclass_t pci_class;
61686be9f0dSKonstantin Belousov 	device_t bus, pcib;
61786be9f0dSKonstantin Belousov 	int depth;
61886be9f0dSKonstantin Belousov 
61986be9f0dSKonstantin Belousov 	pci_class = devclass_find("pci");
62086be9f0dSKonstantin Belousov 	for (depth = 1; ; depth++) {
62186be9f0dSKonstantin Belousov 		bus = device_get_parent(child);
62286be9f0dSKonstantin Belousov 		pcib = device_get_parent(bus);
62386be9f0dSKonstantin Belousov 		if (device_get_devclass(device_get_parent(pcib)) !=
62486be9f0dSKonstantin Belousov 		    pci_class)
62586be9f0dSKonstantin Belousov 			return (depth);
62686be9f0dSKonstantin Belousov 		child = pcib;
62786be9f0dSKonstantin Belousov 	}
62886be9f0dSKonstantin Belousov }
62986be9f0dSKonstantin Belousov 
630f9feb091SKonstantin Belousov void
631f9feb091SKonstantin Belousov dmar_dev_path(device_t child, int *busno, void *path1, int depth)
63286be9f0dSKonstantin Belousov {
63386be9f0dSKonstantin Belousov 	devclass_t pci_class;
63486be9f0dSKonstantin Belousov 	device_t bus, pcib;
635f9feb091SKonstantin Belousov 	ACPI_DMAR_PCI_PATH *path;
63686be9f0dSKonstantin Belousov 
63786be9f0dSKonstantin Belousov 	pci_class = devclass_find("pci");
638f9feb091SKonstantin Belousov 	path = path1;
63986be9f0dSKonstantin Belousov 	for (depth--; depth != -1; depth--) {
64086be9f0dSKonstantin Belousov 		path[depth].Device = pci_get_slot(child);
64186be9f0dSKonstantin Belousov 		path[depth].Function = pci_get_function(child);
64286be9f0dSKonstantin Belousov 		bus = device_get_parent(child);
64386be9f0dSKonstantin Belousov 		pcib = device_get_parent(bus);
64486be9f0dSKonstantin Belousov 		if (device_get_devclass(device_get_parent(pcib)) !=
64586be9f0dSKonstantin Belousov 		    pci_class) {
64686be9f0dSKonstantin Belousov 			/* reached a host bridge */
64786be9f0dSKonstantin Belousov 			*busno = pcib_get_bus(bus);
64886be9f0dSKonstantin Belousov 			return;
64986be9f0dSKonstantin Belousov 		}
65086be9f0dSKonstantin Belousov 		child = pcib;
65186be9f0dSKonstantin Belousov 	}
65286be9f0dSKonstantin Belousov 	panic("wrong depth");
65386be9f0dSKonstantin Belousov }
65486be9f0dSKonstantin Belousov 
65586be9f0dSKonstantin Belousov static int
65686be9f0dSKonstantin Belousov dmar_match_pathes(int busno1, const ACPI_DMAR_PCI_PATH *path1, int depth1,
65786be9f0dSKonstantin Belousov     int busno2, const ACPI_DMAR_PCI_PATH *path2, int depth2,
65886be9f0dSKonstantin Belousov     enum AcpiDmarScopeType scope_type)
65986be9f0dSKonstantin Belousov {
66086be9f0dSKonstantin Belousov 	int i, depth;
66186be9f0dSKonstantin Belousov 
66286be9f0dSKonstantin Belousov 	if (busno1 != busno2)
66386be9f0dSKonstantin Belousov 		return (0);
66486be9f0dSKonstantin Belousov 	if (scope_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT && depth1 != depth2)
66586be9f0dSKonstantin Belousov 		return (0);
66686be9f0dSKonstantin Belousov 	depth = depth1;
66786be9f0dSKonstantin Belousov 	if (depth2 < depth)
66886be9f0dSKonstantin Belousov 		depth = depth2;
66986be9f0dSKonstantin Belousov 	for (i = 0; i < depth; i++) {
67086be9f0dSKonstantin Belousov 		if (path1[i].Device != path2[i].Device ||
67186be9f0dSKonstantin Belousov 		    path1[i].Function != path2[i].Function)
67286be9f0dSKonstantin Belousov 			return (0);
67386be9f0dSKonstantin Belousov 	}
67486be9f0dSKonstantin Belousov 	return (1);
67586be9f0dSKonstantin Belousov }
67686be9f0dSKonstantin Belousov 
67786be9f0dSKonstantin Belousov static int
678f9feb091SKonstantin Belousov dmar_match_devscope(ACPI_DMAR_DEVICE_SCOPE *devscope, int dev_busno,
679f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len)
68086be9f0dSKonstantin Belousov {
68186be9f0dSKonstantin Belousov 	ACPI_DMAR_PCI_PATH *path;
68286be9f0dSKonstantin Belousov 	int path_len;
68386be9f0dSKonstantin Belousov 
68486be9f0dSKonstantin Belousov 	if (devscope->Length < sizeof(*devscope)) {
685f9feb091SKonstantin Belousov 		printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
68686be9f0dSKonstantin Belousov 		    devscope->Length);
68786be9f0dSKonstantin Belousov 		return (-1);
68886be9f0dSKonstantin Belousov 	}
68986be9f0dSKonstantin Belousov 	if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT &&
69086be9f0dSKonstantin Belousov 	    devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_BRIDGE)
69186be9f0dSKonstantin Belousov 		return (0);
69286be9f0dSKonstantin Belousov 	path_len = devscope->Length - sizeof(*devscope);
69386be9f0dSKonstantin Belousov 	if (path_len % 2 != 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 	path_len /= 2;
69986be9f0dSKonstantin Belousov 	path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
70086be9f0dSKonstantin Belousov 	if (path_len == 0) {
701f9feb091SKonstantin Belousov 		printf("dmar_match_devscope: corrupted DMAR table, dl %d\n",
70286be9f0dSKonstantin Belousov 		    devscope->Length);
70386be9f0dSKonstantin Belousov 		return (-1);
70486be9f0dSKonstantin Belousov 	}
70586be9f0dSKonstantin Belousov 
70686be9f0dSKonstantin Belousov 	return (dmar_match_pathes(devscope->Bus, path, path_len, dev_busno,
70786be9f0dSKonstantin Belousov 	    dev_path, dev_path_len, devscope->EntryType));
70886be9f0dSKonstantin Belousov }
70986be9f0dSKonstantin Belousov 
710f9feb091SKonstantin Belousov static bool
711f9feb091SKonstantin Belousov dmar_match_by_path(struct dmar_unit *unit, int dev_domain, int dev_busno,
712f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len, const char **banner)
71386be9f0dSKonstantin Belousov {
71486be9f0dSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *dmarh;
71586be9f0dSKonstantin Belousov 	ACPI_DMAR_DEVICE_SCOPE *devscope;
71686be9f0dSKonstantin Belousov 	char *ptr, *ptrend;
717f9feb091SKonstantin Belousov 	int match;
718f9feb091SKonstantin Belousov 
719f9feb091SKonstantin Belousov 	dmarh = dmar_find_by_index(unit->unit);
720f9feb091SKonstantin Belousov 	if (dmarh == NULL)
721f9feb091SKonstantin Belousov 		return (false);
722f9feb091SKonstantin Belousov 	if (dmarh->Segment != dev_domain)
723f9feb091SKonstantin Belousov 		return (false);
724f9feb091SKonstantin Belousov 	if ((dmarh->Flags & ACPI_DMAR_INCLUDE_ALL) != 0) {
725f9feb091SKonstantin Belousov 		if (banner != NULL)
726f9feb091SKonstantin Belousov 			*banner = "INCLUDE_ALL";
727f9feb091SKonstantin Belousov 		return (true);
728f9feb091SKonstantin Belousov 	}
729f9feb091SKonstantin Belousov 	ptr = (char *)dmarh + sizeof(*dmarh);
730f9feb091SKonstantin Belousov 	ptrend = (char *)dmarh + dmarh->Header.Length;
731f9feb091SKonstantin Belousov 	while (ptr < ptrend) {
732f9feb091SKonstantin Belousov 		devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
733f9feb091SKonstantin Belousov 		ptr += devscope->Length;
734f9feb091SKonstantin Belousov 		match = dmar_match_devscope(devscope, dev_busno, dev_path,
735f9feb091SKonstantin Belousov 		    dev_path_len);
736f9feb091SKonstantin Belousov 		if (match == -1)
737f9feb091SKonstantin Belousov 			return (false);
738f9feb091SKonstantin Belousov 		if (match == 1) {
739f9feb091SKonstantin Belousov 			if (banner != NULL)
740f9feb091SKonstantin Belousov 				*banner = "specific match";
741f9feb091SKonstantin Belousov 			return (true);
742f9feb091SKonstantin Belousov 		}
743f9feb091SKonstantin Belousov 	}
744f9feb091SKonstantin Belousov 	return (false);
745f9feb091SKonstantin Belousov }
746f9feb091SKonstantin Belousov 
747f9feb091SKonstantin Belousov static struct dmar_unit *
748f9feb091SKonstantin Belousov dmar_find_by_scope(int dev_domain, int dev_busno,
749f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *dev_path, int dev_path_len)
750f9feb091SKonstantin Belousov {
751f9feb091SKonstantin Belousov 	struct dmar_unit *unit;
752f9feb091SKonstantin Belousov 	int i;
753f9feb091SKonstantin Belousov 
754f9feb091SKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
755f9feb091SKonstantin Belousov 		if (dmar_devs[i] == NULL)
756f9feb091SKonstantin Belousov 			continue;
757f9feb091SKonstantin Belousov 		unit = device_get_softc(dmar_devs[i]);
758f9feb091SKonstantin Belousov 		if (dmar_match_by_path(unit, dev_domain, dev_busno, dev_path,
759f9feb091SKonstantin Belousov 		    dev_path_len, NULL))
760f9feb091SKonstantin Belousov 			return (unit);
761f9feb091SKonstantin Belousov 	}
762f9feb091SKonstantin Belousov 	return (NULL);
763f9feb091SKonstantin Belousov }
764f9feb091SKonstantin Belousov 
765f9feb091SKonstantin Belousov struct dmar_unit *
766f9feb091SKonstantin Belousov dmar_find(device_t dev, bool verbose)
767f9feb091SKonstantin Belousov {
768f9feb091SKonstantin Belousov 	device_t dmar_dev;
769f9feb091SKonstantin Belousov 	struct dmar_unit *unit;
770f9feb091SKonstantin Belousov 	const char *banner;
771f9feb091SKonstantin Belousov 	int i, dev_domain, dev_busno, dev_path_len;
77286be9f0dSKonstantin Belousov 
77386be9f0dSKonstantin Belousov 	dmar_dev = NULL;
77486be9f0dSKonstantin Belousov 	dev_domain = pci_get_domain(dev);
77586be9f0dSKonstantin Belousov 	dev_path_len = dmar_dev_depth(dev);
77686be9f0dSKonstantin Belousov 	ACPI_DMAR_PCI_PATH dev_path[dev_path_len];
77786be9f0dSKonstantin Belousov 	dmar_dev_path(dev, &dev_busno, dev_path, dev_path_len);
778f9feb091SKonstantin Belousov 	banner = "";
77986be9f0dSKonstantin Belousov 
78086be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
78186be9f0dSKonstantin Belousov 		if (dmar_devs[i] == NULL)
78286be9f0dSKonstantin Belousov 			continue;
783f9feb091SKonstantin Belousov 		unit = device_get_softc(dmar_devs[i]);
784f9feb091SKonstantin Belousov 		if (dmar_match_by_path(unit, dev_domain, dev_busno,
785f9feb091SKonstantin Belousov 		    dev_path, dev_path_len, &banner))
78686be9f0dSKonstantin Belousov 			break;
78786be9f0dSKonstantin Belousov 	}
788f9feb091SKonstantin Belousov 	if (i == dmar_devcnt)
78986be9f0dSKonstantin Belousov 		return (NULL);
790f9feb091SKonstantin Belousov 
791f9feb091SKonstantin Belousov 	if (verbose) {
792f9feb091SKonstantin Belousov 		device_printf(dev, "pci%d:%d:%d:%d matched dmar%d by %s",
793f9feb091SKonstantin Belousov 		    dev_domain, pci_get_bus(dev), pci_get_slot(dev),
794f9feb091SKonstantin Belousov 		    pci_get_function(dev), unit->unit, banner);
795f9feb091SKonstantin Belousov 		printf(" scope path ");
796f9feb091SKonstantin Belousov 		dmar_print_path(dev_busno, dev_path_len, dev_path);
797f9feb091SKonstantin Belousov 		printf("\n");
79886be9f0dSKonstantin Belousov 	}
799f9feb091SKonstantin Belousov 	return (unit);
80086be9f0dSKonstantin Belousov }
80186be9f0dSKonstantin Belousov 
8020a110d5bSKonstantin Belousov static struct dmar_unit *
8030a110d5bSKonstantin Belousov dmar_find_nonpci(u_int id, u_int entry_type, uint16_t *rid)
8040a110d5bSKonstantin Belousov {
8050a110d5bSKonstantin Belousov 	device_t dmar_dev;
8060a110d5bSKonstantin Belousov 	struct dmar_unit *unit;
8070a110d5bSKonstantin Belousov 	ACPI_DMAR_HARDWARE_UNIT *dmarh;
8080a110d5bSKonstantin Belousov 	ACPI_DMAR_DEVICE_SCOPE *devscope;
8090a110d5bSKonstantin Belousov 	ACPI_DMAR_PCI_PATH *path;
8100a110d5bSKonstantin Belousov 	char *ptr, *ptrend;
811fd15fee1SKonstantin Belousov #ifdef DEV_APIC
812fd15fee1SKonstantin Belousov 	int error;
813fd15fee1SKonstantin Belousov #endif
8140a110d5bSKonstantin Belousov 	int i;
8150a110d5bSKonstantin Belousov 
8160a110d5bSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
8170a110d5bSKonstantin Belousov 		dmar_dev = dmar_devs[i];
8180a110d5bSKonstantin Belousov 		if (dmar_dev == NULL)
8190a110d5bSKonstantin Belousov 			continue;
8200a110d5bSKonstantin Belousov 		unit = (struct dmar_unit *)device_get_softc(dmar_dev);
8210a110d5bSKonstantin Belousov 		dmarh = dmar_find_by_index(i);
8220a110d5bSKonstantin Belousov 		if (dmarh == NULL)
8230a110d5bSKonstantin Belousov 			continue;
8240a110d5bSKonstantin Belousov 		ptr = (char *)dmarh + sizeof(*dmarh);
8250a110d5bSKonstantin Belousov 		ptrend = (char *)dmarh + dmarh->Header.Length;
8260a110d5bSKonstantin Belousov 		for (;;) {
8270a110d5bSKonstantin Belousov 			if (ptr >= ptrend)
8280a110d5bSKonstantin Belousov 				break;
8290a110d5bSKonstantin Belousov 			devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
8300a110d5bSKonstantin Belousov 			ptr += devscope->Length;
8310a110d5bSKonstantin Belousov 			if (devscope->EntryType != entry_type)
8320a110d5bSKonstantin Belousov 				continue;
8330a110d5bSKonstantin Belousov 			if (devscope->EnumerationId != id)
8340a110d5bSKonstantin Belousov 				continue;
835fd15fee1SKonstantin Belousov #ifdef DEV_APIC
836fd15fee1SKonstantin Belousov 			if (entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC) {
837fd15fee1SKonstantin Belousov 				error = ioapic_get_rid(id, rid);
838fd15fee1SKonstantin Belousov 				/*
839fd15fee1SKonstantin Belousov 				 * If our IOAPIC has PCI bindings then
840fd15fee1SKonstantin Belousov 				 * use the PCI device rid.
841fd15fee1SKonstantin Belousov 				 */
842fd15fee1SKonstantin Belousov 				if (error == 0)
843fd15fee1SKonstantin Belousov 					return (unit);
844fd15fee1SKonstantin Belousov 			}
845fd15fee1SKonstantin Belousov #endif
8460a110d5bSKonstantin Belousov 			if (devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE)
8470a110d5bSKonstantin Belousov 			    == 2) {
8480a110d5bSKonstantin Belousov 				if (rid != NULL) {
8490a110d5bSKonstantin Belousov 					path = (ACPI_DMAR_PCI_PATH *)
8500a110d5bSKonstantin Belousov 					    (devscope + 1);
8510a110d5bSKonstantin Belousov 					*rid = PCI_RID(devscope->Bus,
8520a110d5bSKonstantin Belousov 					    path->Device, path->Function);
8530a110d5bSKonstantin Belousov 				}
8540a110d5bSKonstantin Belousov 				return (unit);
855fd15fee1SKonstantin Belousov 			}
8560a110d5bSKonstantin Belousov 			printf(
8570a110d5bSKonstantin Belousov 		           "dmar_find_nonpci: id %d type %d path length != 2\n",
8580a110d5bSKonstantin Belousov 			    id, entry_type);
859fd15fee1SKonstantin Belousov 			break;
8600a110d5bSKonstantin Belousov 		}
8610a110d5bSKonstantin Belousov 	}
8620a110d5bSKonstantin Belousov 	return (NULL);
8630a110d5bSKonstantin Belousov }
8640a110d5bSKonstantin Belousov 
8650a110d5bSKonstantin Belousov 
8660a110d5bSKonstantin Belousov struct dmar_unit *
8670a110d5bSKonstantin Belousov dmar_find_hpet(device_t dev, uint16_t *rid)
8680a110d5bSKonstantin Belousov {
8690a110d5bSKonstantin Belousov 
8702fe1339eSKonstantin Belousov 	return (dmar_find_nonpci(hpet_get_uid(dev), ACPI_DMAR_SCOPE_TYPE_HPET,
8712fe1339eSKonstantin Belousov 	    rid));
8720a110d5bSKonstantin Belousov }
8730a110d5bSKonstantin Belousov 
8740a110d5bSKonstantin Belousov struct dmar_unit *
8750a110d5bSKonstantin Belousov dmar_find_ioapic(u_int apic_id, uint16_t *rid)
8760a110d5bSKonstantin Belousov {
8770a110d5bSKonstantin Belousov 
8780a110d5bSKonstantin Belousov 	return (dmar_find_nonpci(apic_id, ACPI_DMAR_SCOPE_TYPE_IOAPIC, rid));
8790a110d5bSKonstantin Belousov }
8800a110d5bSKonstantin Belousov 
88186be9f0dSKonstantin Belousov struct rmrr_iter_args {
8821abfd355SKonstantin Belousov 	struct dmar_domain *domain;
88386be9f0dSKonstantin Belousov 	int dev_domain;
88486be9f0dSKonstantin Belousov 	int dev_busno;
885f9feb091SKonstantin Belousov 	const ACPI_DMAR_PCI_PATH *dev_path;
88686be9f0dSKonstantin Belousov 	int dev_path_len;
88786be9f0dSKonstantin Belousov 	struct dmar_map_entries_tailq *rmrr_entries;
88886be9f0dSKonstantin Belousov };
88986be9f0dSKonstantin Belousov 
89086be9f0dSKonstantin Belousov static int
89186be9f0dSKonstantin Belousov dmar_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
89286be9f0dSKonstantin Belousov {
89386be9f0dSKonstantin Belousov 	struct rmrr_iter_args *ria;
89486be9f0dSKonstantin Belousov 	ACPI_DMAR_RESERVED_MEMORY *resmem;
89586be9f0dSKonstantin Belousov 	ACPI_DMAR_DEVICE_SCOPE *devscope;
89686be9f0dSKonstantin Belousov 	struct dmar_map_entry *entry;
89786be9f0dSKonstantin Belousov 	char *ptr, *ptrend;
89886be9f0dSKonstantin Belousov 	int match;
89986be9f0dSKonstantin Belousov 
90086be9f0dSKonstantin Belousov 	if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY)
90186be9f0dSKonstantin Belousov 		return (1);
90286be9f0dSKonstantin Belousov 
90386be9f0dSKonstantin Belousov 	ria = arg;
90486be9f0dSKonstantin Belousov 	resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh;
90586be9f0dSKonstantin Belousov 	if (resmem->Segment != ria->dev_domain)
90686be9f0dSKonstantin Belousov 		return (1);
90786be9f0dSKonstantin Belousov 
90886be9f0dSKonstantin Belousov 	ptr = (char *)resmem + sizeof(*resmem);
90986be9f0dSKonstantin Belousov 	ptrend = (char *)resmem + resmem->Header.Length;
91086be9f0dSKonstantin Belousov 	for (;;) {
91186be9f0dSKonstantin Belousov 		if (ptr >= ptrend)
91286be9f0dSKonstantin Belousov 			break;
91386be9f0dSKonstantin Belousov 		devscope = (ACPI_DMAR_DEVICE_SCOPE *)ptr;
91486be9f0dSKonstantin Belousov 		ptr += devscope->Length;
915f9feb091SKonstantin Belousov 		match = dmar_match_devscope(devscope, ria->dev_busno,
91686be9f0dSKonstantin Belousov 		    ria->dev_path, ria->dev_path_len);
91786be9f0dSKonstantin Belousov 		if (match == 1) {
9181abfd355SKonstantin Belousov 			entry = dmar_gas_alloc_entry(ria->domain,
9191abfd355SKonstantin Belousov 			    DMAR_PGF_WAITOK);
92086be9f0dSKonstantin Belousov 			entry->start = resmem->BaseAddress;
92186be9f0dSKonstantin Belousov 			/* The RMRR entry end address is inclusive. */
92286be9f0dSKonstantin Belousov 			entry->end = resmem->EndAddress;
92386be9f0dSKonstantin Belousov 			TAILQ_INSERT_TAIL(ria->rmrr_entries, entry,
92486be9f0dSKonstantin Belousov 			    unroll_link);
92586be9f0dSKonstantin Belousov 		}
92686be9f0dSKonstantin Belousov 	}
92786be9f0dSKonstantin Belousov 
92886be9f0dSKonstantin Belousov 	return (1);
92986be9f0dSKonstantin Belousov }
93086be9f0dSKonstantin Belousov 
93186be9f0dSKonstantin Belousov void
932f9feb091SKonstantin Belousov dmar_dev_parse_rmrr(struct dmar_domain *domain, int dev_domain, int dev_busno,
933f9feb091SKonstantin Belousov     const void *dev_path, int dev_path_len,
93486be9f0dSKonstantin Belousov     struct dmar_map_entries_tailq *rmrr_entries)
93586be9f0dSKonstantin Belousov {
93686be9f0dSKonstantin Belousov 	struct rmrr_iter_args ria;
93786be9f0dSKonstantin Belousov 
9381abfd355SKonstantin Belousov 	ria.domain = domain;
939f9feb091SKonstantin Belousov 	ria.dev_domain = dev_domain;
940f9feb091SKonstantin Belousov 	ria.dev_busno = dev_busno;
941f9feb091SKonstantin Belousov 	ria.dev_path = (const ACPI_DMAR_PCI_PATH *)dev_path;
942f9feb091SKonstantin Belousov 	ria.dev_path_len = dev_path_len;
94386be9f0dSKonstantin Belousov 	ria.rmrr_entries = rmrr_entries;
94486be9f0dSKonstantin Belousov 	dmar_iterate_tbl(dmar_rmrr_iter, &ria);
94586be9f0dSKonstantin Belousov }
94686be9f0dSKonstantin Belousov 
94786be9f0dSKonstantin Belousov struct inst_rmrr_iter_args {
94886be9f0dSKonstantin Belousov 	struct dmar_unit *dmar;
94986be9f0dSKonstantin Belousov };
95086be9f0dSKonstantin Belousov 
95186be9f0dSKonstantin Belousov static device_t
95286be9f0dSKonstantin Belousov dmar_path_dev(int segment, int path_len, int busno,
953f9feb091SKonstantin Belousov     const ACPI_DMAR_PCI_PATH *path, uint16_t *rid)
95486be9f0dSKonstantin Belousov {
955f9feb091SKonstantin Belousov 	device_t dev;
95686be9f0dSKonstantin Belousov 	int i;
95786be9f0dSKonstantin Belousov 
95886be9f0dSKonstantin Belousov 	dev = NULL;
959f9feb091SKonstantin Belousov 	for (i = 0; i < path_len; i++) {
96086be9f0dSKonstantin Belousov 		dev = pci_find_dbsf(segment, busno, path->Device,
96186be9f0dSKonstantin Belousov 		    path->Function);
96286be9f0dSKonstantin Belousov 		if (i != path_len - 1) {
963f9feb091SKonstantin Belousov 			busno = pci_cfgregread(busno, path->Device,
964f9feb091SKonstantin Belousov 			    path->Function, PCIR_SECBUS_1, 1);
965f9feb091SKonstantin Belousov 			path++;
96686be9f0dSKonstantin Belousov 		}
96786be9f0dSKonstantin Belousov 	}
968f9feb091SKonstantin Belousov 	*rid = PCI_RID(busno, path->Device, path->Function);
96986be9f0dSKonstantin Belousov 	return (dev);
97086be9f0dSKonstantin Belousov }
97186be9f0dSKonstantin Belousov 
97286be9f0dSKonstantin Belousov static int
97386be9f0dSKonstantin Belousov dmar_inst_rmrr_iter(ACPI_DMAR_HEADER *dmarh, void *arg)
97486be9f0dSKonstantin Belousov {
97586be9f0dSKonstantin Belousov 	const ACPI_DMAR_RESERVED_MEMORY *resmem;
97686be9f0dSKonstantin Belousov 	const ACPI_DMAR_DEVICE_SCOPE *devscope;
97786be9f0dSKonstantin Belousov 	struct inst_rmrr_iter_args *iria;
97886be9f0dSKonstantin Belousov 	const char *ptr, *ptrend;
97986be9f0dSKonstantin Belousov 	device_t dev;
980f9feb091SKonstantin Belousov 	struct dmar_unit *unit;
981f9feb091SKonstantin Belousov 	int dev_path_len;
982f9feb091SKonstantin Belousov 	uint16_t rid;
983f9feb091SKonstantin Belousov 
984f9feb091SKonstantin Belousov 	iria = arg;
98586be9f0dSKonstantin Belousov 
98686be9f0dSKonstantin Belousov 	if (dmarh->Type != ACPI_DMAR_TYPE_RESERVED_MEMORY)
98786be9f0dSKonstantin Belousov 		return (1);
98886be9f0dSKonstantin Belousov 
98986be9f0dSKonstantin Belousov 	resmem = (ACPI_DMAR_RESERVED_MEMORY *)dmarh;
99086be9f0dSKonstantin Belousov 	if (resmem->Segment != iria->dmar->segment)
99186be9f0dSKonstantin Belousov 		return (1);
99286be9f0dSKonstantin Belousov 
99333552193SDimitry Andric 	ptr = (const char *)resmem + sizeof(*resmem);
99433552193SDimitry Andric 	ptrend = (const char *)resmem + resmem->Header.Length;
99586be9f0dSKonstantin Belousov 	for (;;) {
99686be9f0dSKonstantin Belousov 		if (ptr >= ptrend)
99786be9f0dSKonstantin Belousov 			break;
99833552193SDimitry Andric 		devscope = (const ACPI_DMAR_DEVICE_SCOPE *)ptr;
99986be9f0dSKonstantin Belousov 		ptr += devscope->Length;
100086be9f0dSKonstantin Belousov 		/* XXXKIB bridge */
100186be9f0dSKonstantin Belousov 		if (devscope->EntryType != ACPI_DMAR_SCOPE_TYPE_ENDPOINT)
100286be9f0dSKonstantin Belousov 			continue;
1003f9feb091SKonstantin Belousov 		rid = 0;
1004f9feb091SKonstantin Belousov 		dev_path_len = (devscope->Length -
1005f9feb091SKonstantin Belousov 		    sizeof(ACPI_DMAR_DEVICE_SCOPE)) / 2;
1006f9feb091SKonstantin Belousov 		dev = dmar_path_dev(resmem->Segment, dev_path_len,
1007f9feb091SKonstantin Belousov 		    devscope->Bus,
1008f9feb091SKonstantin Belousov 		    (const ACPI_DMAR_PCI_PATH *)(devscope + 1), &rid);
100986be9f0dSKonstantin Belousov 		if (dev == NULL) {
1010f9feb091SKonstantin Belousov 			if (bootverbose) {
1011f9feb091SKonstantin Belousov 				printf("dmar%d no dev found for RMRR "
1012f9feb091SKonstantin Belousov 				    "[%#jx, %#jx] rid %#x scope path ",
1013f9feb091SKonstantin Belousov 				     iria->dmar->unit,
10142d8bfbdcSKonstantin Belousov 				     (uintmax_t)resmem->BaseAddress,
10152d8bfbdcSKonstantin Belousov 				     (uintmax_t)resmem->EndAddress,
1016f9feb091SKonstantin Belousov 				     rid);
1017f9feb091SKonstantin Belousov 				dmar_print_path(devscope->Bus, dev_path_len,
1018f9feb091SKonstantin Belousov 				    (const ACPI_DMAR_PCI_PATH *)(devscope + 1));
1019f9feb091SKonstantin Belousov 				printf("\n");
1020f9feb091SKonstantin Belousov 			}
1021f9feb091SKonstantin Belousov 			unit = dmar_find_by_scope(resmem->Segment,
1022f9feb091SKonstantin Belousov 			    devscope->Bus,
1023f9feb091SKonstantin Belousov 			    (const ACPI_DMAR_PCI_PATH *)(devscope + 1),
1024f9feb091SKonstantin Belousov 			    dev_path_len);
1025f9feb091SKonstantin Belousov 			if (iria->dmar != unit)
102686be9f0dSKonstantin Belousov 				continue;
1027f9feb091SKonstantin Belousov 			dmar_get_ctx_for_devpath(iria->dmar, rid,
1028f9feb091SKonstantin Belousov 			    resmem->Segment, devscope->Bus,
1029f9feb091SKonstantin Belousov 			    (const ACPI_DMAR_PCI_PATH *)(devscope + 1),
1030f9feb091SKonstantin Belousov 			    dev_path_len, false, true);
1031f9feb091SKonstantin Belousov 		} else {
1032f9feb091SKonstantin Belousov 			unit = dmar_find(dev, false);
1033f9feb091SKonstantin Belousov 			if (iria->dmar != unit)
103486be9f0dSKonstantin Belousov 				continue;
103586be9f0dSKonstantin Belousov 			dmar_instantiate_ctx(iria->dmar, dev, true);
103686be9f0dSKonstantin Belousov 		}
1037f9feb091SKonstantin Belousov 	}
103886be9f0dSKonstantin Belousov 
103986be9f0dSKonstantin Belousov 	return (1);
104086be9f0dSKonstantin Belousov 
104186be9f0dSKonstantin Belousov }
104286be9f0dSKonstantin Belousov 
104386be9f0dSKonstantin Belousov /*
104486be9f0dSKonstantin Belousov  * Pre-create all contexts for the DMAR which have RMRR entries.
104586be9f0dSKonstantin Belousov  */
104686be9f0dSKonstantin Belousov int
104786be9f0dSKonstantin Belousov dmar_instantiate_rmrr_ctxs(struct dmar_unit *dmar)
104886be9f0dSKonstantin Belousov {
104986be9f0dSKonstantin Belousov 	struct inst_rmrr_iter_args iria;
105086be9f0dSKonstantin Belousov 	int error;
105186be9f0dSKonstantin Belousov 
105286be9f0dSKonstantin Belousov 	if (!dmar_barrier_enter(dmar, DMAR_BARRIER_RMRR))
105386be9f0dSKonstantin Belousov 		return (0);
105486be9f0dSKonstantin Belousov 
105586be9f0dSKonstantin Belousov 	error = 0;
105686be9f0dSKonstantin Belousov 	iria.dmar = dmar;
105786be9f0dSKonstantin Belousov 	dmar_iterate_tbl(dmar_inst_rmrr_iter, &iria);
105886be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
10591abfd355SKonstantin Belousov 	if (!LIST_EMPTY(&dmar->domains)) {
106086be9f0dSKonstantin Belousov 		KASSERT((dmar->hw_gcmd & DMAR_GCMD_TE) == 0,
106186be9f0dSKonstantin Belousov 	    ("dmar%d: RMRR not handled but translation is already enabled",
106286be9f0dSKonstantin Belousov 		    dmar->unit));
106386be9f0dSKonstantin Belousov 		error = dmar_enable_translation(dmar);
1064f9feb091SKonstantin Belousov 		if (bootverbose) {
1065f9feb091SKonstantin Belousov 			if (error == 0) {
1066f9feb091SKonstantin Belousov 				printf("dmar%d: enabled translation\n",
1067f9feb091SKonstantin Belousov 				    dmar->unit);
1068f9feb091SKonstantin Belousov 			} else {
1069f9feb091SKonstantin Belousov 				printf("dmar%d: enabling translation failed, "
1070f9feb091SKonstantin Belousov 				    "error %d\n", dmar->unit, error);
1071f9feb091SKonstantin Belousov 			}
1072f9feb091SKonstantin Belousov 		}
107386be9f0dSKonstantin Belousov 	}
107486be9f0dSKonstantin Belousov 	dmar_barrier_exit(dmar, DMAR_BARRIER_RMRR);
107586be9f0dSKonstantin Belousov 	return (error);
107686be9f0dSKonstantin Belousov }
107786be9f0dSKonstantin Belousov 
107886be9f0dSKonstantin Belousov #ifdef DDB
107986be9f0dSKonstantin Belousov #include <ddb/ddb.h>
108086be9f0dSKonstantin Belousov #include <ddb/db_lex.h>
108186be9f0dSKonstantin Belousov 
108286be9f0dSKonstantin Belousov static void
10831abfd355SKonstantin Belousov dmar_print_domain_entry(const struct dmar_map_entry *entry)
108486be9f0dSKonstantin Belousov {
108586be9f0dSKonstantin Belousov 	struct dmar_map_entry *l, *r;
108686be9f0dSKonstantin Belousov 
108786be9f0dSKonstantin Belousov 	db_printf(
108886be9f0dSKonstantin Belousov 	    "    start %jx end %jx free_after %jx free_down %jx flags %x ",
108986be9f0dSKonstantin Belousov 	    entry->start, entry->end, entry->free_after, entry->free_down,
109086be9f0dSKonstantin Belousov 	    entry->flags);
109186be9f0dSKonstantin Belousov 	db_printf("left ");
109286be9f0dSKonstantin Belousov 	l = RB_LEFT(entry, rb_entry);
109386be9f0dSKonstantin Belousov 	if (l == NULL)
109486be9f0dSKonstantin Belousov 		db_printf("NULL ");
109586be9f0dSKonstantin Belousov 	else
109686be9f0dSKonstantin Belousov 		db_printf("%jx ", l->start);
109786be9f0dSKonstantin Belousov 	db_printf("right ");
109886be9f0dSKonstantin Belousov 	r = RB_RIGHT(entry, rb_entry);
109986be9f0dSKonstantin Belousov 	if (r == NULL)
110086be9f0dSKonstantin Belousov 		db_printf("NULL");
110186be9f0dSKonstantin Belousov 	else
110286be9f0dSKonstantin Belousov 		db_printf("%jx", r->start);
110386be9f0dSKonstantin Belousov 	db_printf("\n");
110486be9f0dSKonstantin Belousov }
110586be9f0dSKonstantin Belousov 
110686be9f0dSKonstantin Belousov static void
11071abfd355SKonstantin Belousov dmar_print_ctx(struct dmar_ctx *ctx)
110886be9f0dSKonstantin Belousov {
110986be9f0dSKonstantin Belousov 
111086be9f0dSKonstantin Belousov 	db_printf(
11111abfd355SKonstantin Belousov 	    "    @%p pci%d:%d:%d refs %d flags %x loads %lu unloads %lu\n",
111267499354SRyan Stone 	    ctx, pci_get_bus(ctx->ctx_tag.owner),
111367499354SRyan Stone 	    pci_get_slot(ctx->ctx_tag.owner),
11141abfd355SKonstantin Belousov 	    pci_get_function(ctx->ctx_tag.owner), ctx->refs, ctx->flags,
11151abfd355SKonstantin Belousov 	    ctx->loads, ctx->unloads);
11161abfd355SKonstantin Belousov }
11171abfd355SKonstantin Belousov 
11181abfd355SKonstantin Belousov static void
11191abfd355SKonstantin Belousov dmar_print_domain(struct dmar_domain *domain, bool show_mappings)
11201abfd355SKonstantin Belousov {
11211abfd355SKonstantin Belousov 	struct dmar_map_entry *entry;
11221abfd355SKonstantin Belousov 	struct dmar_ctx *ctx;
11231abfd355SKonstantin Belousov 
11241abfd355SKonstantin Belousov 	db_printf(
11251abfd355SKonstantin Belousov 	    "  @%p dom %d mgaw %d agaw %d pglvl %d end %jx refs %d\n"
11261abfd355SKonstantin Belousov 	    "   ctx_cnt %d flags %x pgobj %p map_ents %u\n",
11271abfd355SKonstantin Belousov 	    domain, domain->domain, domain->mgaw, domain->agaw, domain->pglvl,
11281abfd355SKonstantin Belousov 	    (uintmax_t)domain->end, domain->refs, domain->ctx_cnt,
11291abfd355SKonstantin Belousov 	    domain->flags, domain->pgtbl_obj, domain->entries_cnt);
11301abfd355SKonstantin Belousov 	if (!LIST_EMPTY(&domain->contexts)) {
11311abfd355SKonstantin Belousov 		db_printf("  Contexts:\n");
11321abfd355SKonstantin Belousov 		LIST_FOREACH(ctx, &domain->contexts, link)
11331abfd355SKonstantin Belousov 			dmar_print_ctx(ctx);
11341abfd355SKonstantin Belousov 	}
113586be9f0dSKonstantin Belousov 	if (!show_mappings)
113686be9f0dSKonstantin Belousov 		return;
113786be9f0dSKonstantin Belousov 	db_printf("    mapped:\n");
11381abfd355SKonstantin Belousov 	RB_FOREACH(entry, dmar_gas_entries_tree, &domain->rb_root) {
11391abfd355SKonstantin Belousov 		dmar_print_domain_entry(entry);
114086be9f0dSKonstantin Belousov 		if (db_pager_quit)
114186be9f0dSKonstantin Belousov 			break;
114286be9f0dSKonstantin Belousov 	}
114386be9f0dSKonstantin Belousov 	if (db_pager_quit)
114486be9f0dSKonstantin Belousov 		return;
114586be9f0dSKonstantin Belousov 	db_printf("    unloading:\n");
11461abfd355SKonstantin Belousov 	TAILQ_FOREACH(entry, &domain->unload_entries, dmamap_link) {
11471abfd355SKonstantin Belousov 		dmar_print_domain_entry(entry);
114886be9f0dSKonstantin Belousov 		if (db_pager_quit)
114986be9f0dSKonstantin Belousov 			break;
115086be9f0dSKonstantin Belousov 	}
115186be9f0dSKonstantin Belousov }
115286be9f0dSKonstantin Belousov 
11531abfd355SKonstantin Belousov DB_FUNC(dmar_domain, db_dmar_print_domain, db_show_table, CS_OWN, NULL)
115486be9f0dSKonstantin Belousov {
115586be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
11561abfd355SKonstantin Belousov 	struct dmar_domain *domain;
115786be9f0dSKonstantin Belousov 	struct dmar_ctx *ctx;
115886be9f0dSKonstantin Belousov 	bool show_mappings, valid;
11591abfd355SKonstantin Belousov 	int pci_domain, bus, device, function, i, t;
116086be9f0dSKonstantin Belousov 	db_expr_t radix;
116186be9f0dSKonstantin Belousov 
116286be9f0dSKonstantin Belousov 	valid = false;
116386be9f0dSKonstantin Belousov 	radix = db_radix;
116486be9f0dSKonstantin Belousov 	db_radix = 10;
116586be9f0dSKonstantin Belousov 	t = db_read_token();
116686be9f0dSKonstantin Belousov 	if (t == tSLASH) {
116786be9f0dSKonstantin Belousov 		t = db_read_token();
116886be9f0dSKonstantin Belousov 		if (t != tIDENT) {
116986be9f0dSKonstantin Belousov 			db_printf("Bad modifier\n");
117086be9f0dSKonstantin Belousov 			db_radix = radix;
117186be9f0dSKonstantin Belousov 			db_skip_to_eol();
117286be9f0dSKonstantin Belousov 			return;
117386be9f0dSKonstantin Belousov 		}
117486be9f0dSKonstantin Belousov 		show_mappings = strchr(db_tok_string, 'm') != NULL;
117586be9f0dSKonstantin Belousov 		t = db_read_token();
1176f7f5706fSDimitry Andric 	} else {
1177f7f5706fSDimitry Andric 		show_mappings = false;
117886be9f0dSKonstantin Belousov 	}
117986be9f0dSKonstantin Belousov 	if (t == tNUMBER) {
11801abfd355SKonstantin Belousov 		pci_domain = db_tok_number;
118186be9f0dSKonstantin Belousov 		t = db_read_token();
118286be9f0dSKonstantin Belousov 		if (t == tNUMBER) {
118386be9f0dSKonstantin Belousov 			bus = db_tok_number;
118486be9f0dSKonstantin Belousov 			t = db_read_token();
118586be9f0dSKonstantin Belousov 			if (t == tNUMBER) {
118686be9f0dSKonstantin Belousov 				device = db_tok_number;
118786be9f0dSKonstantin Belousov 				t = db_read_token();
118886be9f0dSKonstantin Belousov 				if (t == tNUMBER) {
118986be9f0dSKonstantin Belousov 					function = db_tok_number;
119086be9f0dSKonstantin Belousov 					valid = true;
119186be9f0dSKonstantin Belousov 				}
119286be9f0dSKonstantin Belousov 			}
119386be9f0dSKonstantin Belousov 		}
119486be9f0dSKonstantin Belousov 	}
119586be9f0dSKonstantin Belousov 			db_radix = radix;
119686be9f0dSKonstantin Belousov 	db_skip_to_eol();
119786be9f0dSKonstantin Belousov 	if (!valid) {
11981abfd355SKonstantin Belousov 		db_printf("usage: show dmar_domain [/m] "
119986be9f0dSKonstantin Belousov 		    "<domain> <bus> <device> <func>\n");
120086be9f0dSKonstantin Belousov 		return;
120186be9f0dSKonstantin Belousov 	}
120286be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
120386be9f0dSKonstantin Belousov 		unit = device_get_softc(dmar_devs[i]);
12041abfd355SKonstantin Belousov 		LIST_FOREACH(domain, &unit->domains, link) {
12051abfd355SKonstantin Belousov 			LIST_FOREACH(ctx, &domain->contexts, link) {
12061abfd355SKonstantin Belousov 				if (pci_domain == unit->segment &&
120767499354SRyan Stone 				    bus == pci_get_bus(ctx->ctx_tag.owner) &&
12081abfd355SKonstantin Belousov 				    device ==
12091abfd355SKonstantin Belousov 				    pci_get_slot(ctx->ctx_tag.owner) &&
12101abfd355SKonstantin Belousov 				    function ==
12111abfd355SKonstantin Belousov 				    pci_get_function(ctx->ctx_tag.owner)) {
12121abfd355SKonstantin Belousov 					dmar_print_domain(domain,
12131abfd355SKonstantin Belousov 					    show_mappings);
121486be9f0dSKonstantin Belousov 					goto out;
121586be9f0dSKonstantin Belousov 				}
121686be9f0dSKonstantin Belousov 			}
121786be9f0dSKonstantin Belousov 		}
12181abfd355SKonstantin Belousov 	}
121986be9f0dSKonstantin Belousov out:;
122086be9f0dSKonstantin Belousov }
122186be9f0dSKonstantin Belousov 
122286be9f0dSKonstantin Belousov static void
12231abfd355SKonstantin Belousov dmar_print_one(int idx, bool show_domains, bool show_mappings)
122486be9f0dSKonstantin Belousov {
122586be9f0dSKonstantin Belousov 	struct dmar_unit *unit;
12261abfd355SKonstantin Belousov 	struct dmar_domain *domain;
122786be9f0dSKonstantin Belousov 	int i, frir;
122886be9f0dSKonstantin Belousov 
122986be9f0dSKonstantin Belousov 	unit = device_get_softc(dmar_devs[idx]);
123086be9f0dSKonstantin Belousov 	db_printf("dmar%d at %p, root at 0x%jx, ver 0x%x\n", unit->unit, unit,
123186be9f0dSKonstantin Belousov 	    dmar_read8(unit, DMAR_RTADDR_REG), dmar_read4(unit, DMAR_VER_REG));
123286be9f0dSKonstantin Belousov 	db_printf("cap 0x%jx ecap 0x%jx gsts 0x%x fsts 0x%x fectl 0x%x\n",
123386be9f0dSKonstantin Belousov 	    (uintmax_t)dmar_read8(unit, DMAR_CAP_REG),
123486be9f0dSKonstantin Belousov 	    (uintmax_t)dmar_read8(unit, DMAR_ECAP_REG),
123586be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_GSTS_REG),
123686be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FSTS_REG),
123786be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FECTL_REG));
12381abfd355SKonstantin Belousov 	if (unit->ir_enabled) {
12391abfd355SKonstantin Belousov 		db_printf("ir is enabled; IRT @%p phys 0x%jx maxcnt %d\n",
12401abfd355SKonstantin Belousov 		    unit->irt, (uintmax_t)unit->irt_phys, unit->irte_cnt);
12411abfd355SKonstantin Belousov 	}
124286be9f0dSKonstantin Belousov 	db_printf("fed 0x%x fea 0x%x feua 0x%x\n",
124386be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FEDATA_REG),
124486be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FEADDR_REG),
124586be9f0dSKonstantin Belousov 	    dmar_read4(unit, DMAR_FEUADDR_REG));
124686be9f0dSKonstantin Belousov 	db_printf("primary fault log:\n");
124786be9f0dSKonstantin Belousov 	for (i = 0; i < DMAR_CAP_NFR(unit->hw_cap); i++) {
124886be9f0dSKonstantin Belousov 		frir = (DMAR_CAP_FRO(unit->hw_cap) + i) * 16;
124986be9f0dSKonstantin Belousov 		db_printf("  %d at 0x%x: %jx %jx\n", i, frir,
125086be9f0dSKonstantin Belousov 		    (uintmax_t)dmar_read8(unit, frir),
125186be9f0dSKonstantin Belousov 		    (uintmax_t)dmar_read8(unit, frir + 8));
125286be9f0dSKonstantin Belousov 	}
125368eeb96aSKonstantin Belousov 	if (DMAR_HAS_QI(unit)) {
125468eeb96aSKonstantin Belousov 		db_printf("ied 0x%x iea 0x%x ieua 0x%x\n",
125568eeb96aSKonstantin Belousov 		    dmar_read4(unit, DMAR_IEDATA_REG),
125668eeb96aSKonstantin Belousov 		    dmar_read4(unit, DMAR_IEADDR_REG),
125768eeb96aSKonstantin Belousov 		    dmar_read4(unit, DMAR_IEUADDR_REG));
125868eeb96aSKonstantin Belousov 		if (unit->qi_enabled) {
125968eeb96aSKonstantin Belousov 			db_printf("qi is enabled: queue @0x%jx (IQA 0x%jx) "
126068eeb96aSKonstantin Belousov 			    "size 0x%jx\n"
126168eeb96aSKonstantin Belousov 		    "  head 0x%x tail 0x%x avail 0x%x status 0x%x ctrl 0x%x\n"
126268eeb96aSKonstantin Belousov 		    "  hw compl 0x%x@%p/phys@%jx next seq 0x%x gen 0x%x\n",
126368eeb96aSKonstantin Belousov 			    (uintmax_t)unit->inv_queue,
126468eeb96aSKonstantin Belousov 			    (uintmax_t)dmar_read8(unit, DMAR_IQA_REG),
126568eeb96aSKonstantin Belousov 			    (uintmax_t)unit->inv_queue_size,
126668eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_IQH_REG),
126768eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_IQT_REG),
126868eeb96aSKonstantin Belousov 			    unit->inv_queue_avail,
126968eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_ICS_REG),
127068eeb96aSKonstantin Belousov 			    dmar_read4(unit, DMAR_IECTL_REG),
127168eeb96aSKonstantin Belousov 			    unit->inv_waitd_seq_hw,
127268eeb96aSKonstantin Belousov 			    &unit->inv_waitd_seq_hw,
127368eeb96aSKonstantin Belousov 			    (uintmax_t)unit->inv_waitd_seq_hw_phys,
127468eeb96aSKonstantin Belousov 			    unit->inv_waitd_seq,
127568eeb96aSKonstantin Belousov 			    unit->inv_waitd_gen);
127668eeb96aSKonstantin Belousov 		} else {
127768eeb96aSKonstantin Belousov 			db_printf("qi is disabled\n");
127868eeb96aSKonstantin Belousov 		}
127968eeb96aSKonstantin Belousov 	}
12801abfd355SKonstantin Belousov 	if (show_domains) {
12811abfd355SKonstantin Belousov 		db_printf("domains:\n");
12821abfd355SKonstantin Belousov 		LIST_FOREACH(domain, &unit->domains, link) {
12831abfd355SKonstantin Belousov 			dmar_print_domain(domain, show_mappings);
128486be9f0dSKonstantin Belousov 			if (db_pager_quit)
128586be9f0dSKonstantin Belousov 				break;
128686be9f0dSKonstantin Belousov 		}
128786be9f0dSKonstantin Belousov 	}
128886be9f0dSKonstantin Belousov }
128986be9f0dSKonstantin Belousov 
129086be9f0dSKonstantin Belousov DB_SHOW_COMMAND(dmar, db_dmar_print)
129186be9f0dSKonstantin Belousov {
12921abfd355SKonstantin Belousov 	bool show_domains, show_mappings;
129386be9f0dSKonstantin Belousov 
12941abfd355SKonstantin Belousov 	show_domains = strchr(modif, 'd') != NULL;
129586be9f0dSKonstantin Belousov 	show_mappings = strchr(modif, 'm') != NULL;
129686be9f0dSKonstantin Belousov 	if (!have_addr) {
12971abfd355SKonstantin Belousov 		db_printf("usage: show dmar [/d] [/m] index\n");
129886be9f0dSKonstantin Belousov 		return;
129986be9f0dSKonstantin Belousov 	}
13001abfd355SKonstantin Belousov 	dmar_print_one((int)addr, show_domains, show_mappings);
130186be9f0dSKonstantin Belousov }
130286be9f0dSKonstantin Belousov 
130386be9f0dSKonstantin Belousov DB_SHOW_ALL_COMMAND(dmars, db_show_all_dmars)
130486be9f0dSKonstantin Belousov {
130586be9f0dSKonstantin Belousov 	int i;
13061abfd355SKonstantin Belousov 	bool show_domains, show_mappings;
130786be9f0dSKonstantin Belousov 
13081abfd355SKonstantin Belousov 	show_domains = strchr(modif, 'd') != NULL;
130986be9f0dSKonstantin Belousov 	show_mappings = strchr(modif, 'm') != NULL;
131086be9f0dSKonstantin Belousov 
131186be9f0dSKonstantin Belousov 	for (i = 0; i < dmar_devcnt; i++) {
13121abfd355SKonstantin Belousov 		dmar_print_one(i, show_domains, show_mappings);
131386be9f0dSKonstantin Belousov 		if (db_pager_quit)
131486be9f0dSKonstantin Belousov 			break;
131586be9f0dSKonstantin Belousov 	}
131686be9f0dSKonstantin Belousov }
131786be9f0dSKonstantin Belousov #endif
1318