xref: /freebsd/sys/x86/iommu/intel_utils.c (revision 357149f037beef26ff80e4a7e716e7d07a8a312f)
186be9f0dSKonstantin Belousov /*-
2ebf5747bSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3ebf5747bSPedro F. Giffuni  *
486be9f0dSKonstantin Belousov  * Copyright (c) 2013 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 <sys/param.h>
3686be9f0dSKonstantin Belousov #include <sys/bus.h>
3786be9f0dSKonstantin Belousov #include <sys/kernel.h>
3886be9f0dSKonstantin Belousov #include <sys/lock.h>
3986be9f0dSKonstantin Belousov #include <sys/malloc.h>
4086be9f0dSKonstantin Belousov #include <sys/memdesc.h>
4186be9f0dSKonstantin Belousov #include <sys/mutex.h>
4286be9f0dSKonstantin Belousov #include <sys/proc.h>
4386be9f0dSKonstantin Belousov #include <sys/queue.h>
4486be9f0dSKonstantin Belousov #include <sys/rman.h>
4586be9f0dSKonstantin Belousov #include <sys/rwlock.h>
4686be9f0dSKonstantin Belousov #include <sys/sched.h>
4786be9f0dSKonstantin Belousov #include <sys/sf_buf.h>
4886be9f0dSKonstantin Belousov #include <sys/sysctl.h>
4986be9f0dSKonstantin Belousov #include <sys/systm.h>
5086be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
51476358b3SKonstantin Belousov #include <sys/time.h>
5286be9f0dSKonstantin Belousov #include <sys/tree.h>
530a110d5bSKonstantin Belousov #include <sys/vmem.h>
5467499354SRyan Stone #include <dev/pci/pcivar.h>
5586be9f0dSKonstantin Belousov #include <vm/vm.h>
5686be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
5786be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
5886be9f0dSKonstantin Belousov #include <vm/vm_object.h>
5986be9f0dSKonstantin Belousov #include <vm/vm_page.h>
6086be9f0dSKonstantin Belousov #include <vm/vm_map.h>
6186be9f0dSKonstantin Belousov #include <vm/vm_pageout.h>
6286be9f0dSKonstantin Belousov #include <machine/bus.h>
6386be9f0dSKonstantin Belousov #include <machine/cpu.h>
640a110d5bSKonstantin Belousov #include <machine/intr_machdep.h>
650a110d5bSKonstantin Belousov #include <x86/include/apicvar.h>
6686be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h>
6786be9f0dSKonstantin Belousov #include <x86/iommu/intel_reg.h>
68f2b2f317SRuslan Bukin #include <dev/iommu/busdma_iommu.h>
69685666aaSKonstantin Belousov #include <dev/pci/pcireg.h>
7086be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
7186be9f0dSKonstantin Belousov 
7286be9f0dSKonstantin Belousov u_int
7386be9f0dSKonstantin Belousov dmar_nd2mask(u_int nd)
7486be9f0dSKonstantin Belousov {
7586be9f0dSKonstantin Belousov 	static const u_int masks[] = {
7686be9f0dSKonstantin Belousov 		0x000f,	/* nd == 0 */
7786be9f0dSKonstantin Belousov 		0x002f,	/* nd == 1 */
7886be9f0dSKonstantin Belousov 		0x00ff,	/* nd == 2 */
7986be9f0dSKonstantin Belousov 		0x02ff,	/* nd == 3 */
8086be9f0dSKonstantin Belousov 		0x0fff,	/* nd == 4 */
8186be9f0dSKonstantin Belousov 		0x2fff,	/* nd == 5 */
8286be9f0dSKonstantin Belousov 		0xffff,	/* nd == 6 */
8386be9f0dSKonstantin Belousov 		0x0000,	/* nd == 7 reserved */
8486be9f0dSKonstantin Belousov 	};
8586be9f0dSKonstantin Belousov 
8686be9f0dSKonstantin Belousov 	KASSERT(nd <= 6, ("number of domains %d", nd));
8786be9f0dSKonstantin Belousov 	return (masks[nd]);
8886be9f0dSKonstantin Belousov }
8986be9f0dSKonstantin Belousov 
9086be9f0dSKonstantin Belousov static const struct sagaw_bits_tag {
9186be9f0dSKonstantin Belousov 	int agaw;
9286be9f0dSKonstantin Belousov 	int cap;
9386be9f0dSKonstantin Belousov 	int awlvl;
9486be9f0dSKonstantin Belousov 	int pglvl;
9586be9f0dSKonstantin Belousov } sagaw_bits[] = {
9686be9f0dSKonstantin Belousov 	{.agaw = 30, .cap = DMAR_CAP_SAGAW_2LVL, .awlvl = DMAR_CTX2_AW_2LVL,
9786be9f0dSKonstantin Belousov 	    .pglvl = 2},
9886be9f0dSKonstantin Belousov 	{.agaw = 39, .cap = DMAR_CAP_SAGAW_3LVL, .awlvl = DMAR_CTX2_AW_3LVL,
9986be9f0dSKonstantin Belousov 	    .pglvl = 3},
10086be9f0dSKonstantin Belousov 	{.agaw = 48, .cap = DMAR_CAP_SAGAW_4LVL, .awlvl = DMAR_CTX2_AW_4LVL,
10186be9f0dSKonstantin Belousov 	    .pglvl = 4},
10286be9f0dSKonstantin Belousov 	{.agaw = 57, .cap = DMAR_CAP_SAGAW_5LVL, .awlvl = DMAR_CTX2_AW_5LVL,
10386be9f0dSKonstantin Belousov 	    .pglvl = 5},
10486be9f0dSKonstantin Belousov 	{.agaw = 64, .cap = DMAR_CAP_SAGAW_6LVL, .awlvl = DMAR_CTX2_AW_6LVL,
10586be9f0dSKonstantin Belousov 	    .pglvl = 6}
10686be9f0dSKonstantin Belousov };
10786be9f0dSKonstantin Belousov 
10886be9f0dSKonstantin Belousov bool
10986be9f0dSKonstantin Belousov dmar_pglvl_supported(struct dmar_unit *unit, int pglvl)
11086be9f0dSKonstantin Belousov {
11186be9f0dSKonstantin Belousov 	int i;
11286be9f0dSKonstantin Belousov 
1131abfd355SKonstantin Belousov 	for (i = 0; i < nitems(sagaw_bits); i++) {
11486be9f0dSKonstantin Belousov 		if (sagaw_bits[i].pglvl != pglvl)
11586be9f0dSKonstantin Belousov 			continue;
11686be9f0dSKonstantin Belousov 		if ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0)
11786be9f0dSKonstantin Belousov 			return (true);
11886be9f0dSKonstantin Belousov 	}
11986be9f0dSKonstantin Belousov 	return (false);
12086be9f0dSKonstantin Belousov }
12186be9f0dSKonstantin Belousov 
12286be9f0dSKonstantin Belousov int
1231abfd355SKonstantin Belousov domain_set_agaw(struct dmar_domain *domain, int mgaw)
12486be9f0dSKonstantin Belousov {
12586be9f0dSKonstantin Belousov 	int sagaw, i;
12686be9f0dSKonstantin Belousov 
1271abfd355SKonstantin Belousov 	domain->mgaw = mgaw;
1281abfd355SKonstantin Belousov 	sagaw = DMAR_CAP_SAGAW(domain->dmar->hw_cap);
1291abfd355SKonstantin Belousov 	for (i = 0; i < nitems(sagaw_bits); i++) {
13086be9f0dSKonstantin Belousov 		if (sagaw_bits[i].agaw >= mgaw) {
1311abfd355SKonstantin Belousov 			domain->agaw = sagaw_bits[i].agaw;
1321abfd355SKonstantin Belousov 			domain->pglvl = sagaw_bits[i].pglvl;
1331abfd355SKonstantin Belousov 			domain->awlvl = sagaw_bits[i].awlvl;
13486be9f0dSKonstantin Belousov 			return (0);
13586be9f0dSKonstantin Belousov 		}
13686be9f0dSKonstantin Belousov 	}
1371abfd355SKonstantin Belousov 	device_printf(domain->dmar->dev,
1381abfd355SKonstantin Belousov 	    "context request mgaw %d: no agaw found, sagaw %x\n",
1391abfd355SKonstantin Belousov 	    mgaw, sagaw);
14086be9f0dSKonstantin Belousov 	return (EINVAL);
14186be9f0dSKonstantin Belousov }
14286be9f0dSKonstantin Belousov 
14386be9f0dSKonstantin Belousov /*
14486be9f0dSKonstantin Belousov  * Find a best fit mgaw for the given maxaddr:
14586be9f0dSKonstantin Belousov  *   - if allow_less is false, must find sagaw which maps all requested
14686be9f0dSKonstantin Belousov  *     addresses (used by identity mappings);
14786be9f0dSKonstantin Belousov  *   - if allow_less is true, and no supported sagaw can map all requested
14886be9f0dSKonstantin Belousov  *     address space, accept the biggest sagaw, whatever is it.
14986be9f0dSKonstantin Belousov  */
15086be9f0dSKonstantin Belousov int
15159e37c8aSRuslan Bukin dmar_maxaddr2mgaw(struct dmar_unit *unit, iommu_gaddr_t maxaddr, bool allow_less)
15286be9f0dSKonstantin Belousov {
15386be9f0dSKonstantin Belousov 	int i;
15486be9f0dSKonstantin Belousov 
1551abfd355SKonstantin Belousov 	for (i = 0; i < nitems(sagaw_bits); i++) {
15686be9f0dSKonstantin Belousov 		if ((1ULL << sagaw_bits[i].agaw) >= maxaddr &&
15786be9f0dSKonstantin Belousov 		    (DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0)
15886be9f0dSKonstantin Belousov 			break;
15986be9f0dSKonstantin Belousov 	}
1601abfd355SKonstantin Belousov 	if (allow_less && i == nitems(sagaw_bits)) {
16186be9f0dSKonstantin Belousov 		do {
16286be9f0dSKonstantin Belousov 			i--;
16386be9f0dSKonstantin Belousov 		} while ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap)
16486be9f0dSKonstantin Belousov 		    == 0);
16586be9f0dSKonstantin Belousov 	}
1661abfd355SKonstantin Belousov 	if (i < nitems(sagaw_bits))
16786be9f0dSKonstantin Belousov 		return (sagaw_bits[i].agaw);
16886be9f0dSKonstantin Belousov 	KASSERT(0, ("no mgaw for maxaddr %jx allow_less %d",
16986be9f0dSKonstantin Belousov 	    (uintmax_t) maxaddr, allow_less));
17086be9f0dSKonstantin Belousov 	return (-1);
17186be9f0dSKonstantin Belousov }
17286be9f0dSKonstantin Belousov 
17386be9f0dSKonstantin Belousov /*
17486be9f0dSKonstantin Belousov  * Calculate the total amount of page table pages needed to map the
17586be9f0dSKonstantin Belousov  * whole bus address space on the context with the selected agaw.
17686be9f0dSKonstantin Belousov  */
17786be9f0dSKonstantin Belousov vm_pindex_t
17886be9f0dSKonstantin Belousov pglvl_max_pages(int pglvl)
17986be9f0dSKonstantin Belousov {
18086be9f0dSKonstantin Belousov 	vm_pindex_t res;
18186be9f0dSKonstantin Belousov 	int i;
18286be9f0dSKonstantin Belousov 
18386be9f0dSKonstantin Belousov 	for (res = 0, i = pglvl; i > 0; i--) {
18486be9f0dSKonstantin Belousov 		res *= DMAR_NPTEPG;
18586be9f0dSKonstantin Belousov 		res++;
18686be9f0dSKonstantin Belousov 	}
18786be9f0dSKonstantin Belousov 	return (res);
18886be9f0dSKonstantin Belousov }
18986be9f0dSKonstantin Belousov 
19086be9f0dSKonstantin Belousov /*
19186be9f0dSKonstantin Belousov  * Return true if the page table level lvl supports the superpage for
19286be9f0dSKonstantin Belousov  * the context ctx.
19386be9f0dSKonstantin Belousov  */
19486be9f0dSKonstantin Belousov int
1951abfd355SKonstantin Belousov domain_is_sp_lvl(struct dmar_domain *domain, int lvl)
19686be9f0dSKonstantin Belousov {
19786be9f0dSKonstantin Belousov 	int alvl, cap_sps;
19886be9f0dSKonstantin Belousov 	static const int sagaw_sp[] = {
19986be9f0dSKonstantin Belousov 		DMAR_CAP_SPS_2M,
20086be9f0dSKonstantin Belousov 		DMAR_CAP_SPS_1G,
20186be9f0dSKonstantin Belousov 		DMAR_CAP_SPS_512G,
20286be9f0dSKonstantin Belousov 		DMAR_CAP_SPS_1T
20386be9f0dSKonstantin Belousov 	};
20486be9f0dSKonstantin Belousov 
2051abfd355SKonstantin Belousov 	alvl = domain->pglvl - lvl - 1;
2061abfd355SKonstantin Belousov 	cap_sps = DMAR_CAP_SPS(domain->dmar->hw_cap);
2071abfd355SKonstantin Belousov 	return (alvl < nitems(sagaw_sp) && (sagaw_sp[alvl] & cap_sps) != 0);
20886be9f0dSKonstantin Belousov }
20986be9f0dSKonstantin Belousov 
21059e37c8aSRuslan Bukin iommu_gaddr_t
21186be9f0dSKonstantin Belousov pglvl_page_size(int total_pglvl, int lvl)
21286be9f0dSKonstantin Belousov {
21386be9f0dSKonstantin Belousov 	int rlvl;
21459e37c8aSRuslan Bukin 	static const iommu_gaddr_t pg_sz[] = {
21559e37c8aSRuslan Bukin 		(iommu_gaddr_t)DMAR_PAGE_SIZE,
21659e37c8aSRuslan Bukin 		(iommu_gaddr_t)DMAR_PAGE_SIZE << DMAR_NPTEPGSHIFT,
21759e37c8aSRuslan Bukin 		(iommu_gaddr_t)DMAR_PAGE_SIZE << (2 * DMAR_NPTEPGSHIFT),
21859e37c8aSRuslan Bukin 		(iommu_gaddr_t)DMAR_PAGE_SIZE << (3 * DMAR_NPTEPGSHIFT),
21959e37c8aSRuslan Bukin 		(iommu_gaddr_t)DMAR_PAGE_SIZE << (4 * DMAR_NPTEPGSHIFT),
22059e37c8aSRuslan Bukin 		(iommu_gaddr_t)DMAR_PAGE_SIZE << (5 * DMAR_NPTEPGSHIFT)
22186be9f0dSKonstantin Belousov 	};
22286be9f0dSKonstantin Belousov 
22386be9f0dSKonstantin Belousov 	KASSERT(lvl >= 0 && lvl < total_pglvl,
22486be9f0dSKonstantin Belousov 	    ("total %d lvl %d", total_pglvl, lvl));
22586be9f0dSKonstantin Belousov 	rlvl = total_pglvl - lvl - 1;
2261abfd355SKonstantin Belousov 	KASSERT(rlvl < nitems(pg_sz), ("sizeof pg_sz lvl %d", lvl));
22786be9f0dSKonstantin Belousov 	return (pg_sz[rlvl]);
22886be9f0dSKonstantin Belousov }
22986be9f0dSKonstantin Belousov 
23059e37c8aSRuslan Bukin iommu_gaddr_t
2311abfd355SKonstantin Belousov domain_page_size(struct dmar_domain *domain, int lvl)
23286be9f0dSKonstantin Belousov {
23386be9f0dSKonstantin Belousov 
2341abfd355SKonstantin Belousov 	return (pglvl_page_size(domain->pglvl, lvl));
23586be9f0dSKonstantin Belousov }
23686be9f0dSKonstantin Belousov 
23768eeb96aSKonstantin Belousov int
23859e37c8aSRuslan Bukin calc_am(struct dmar_unit *unit, iommu_gaddr_t base, iommu_gaddr_t size,
23959e37c8aSRuslan Bukin     iommu_gaddr_t *isizep)
24068eeb96aSKonstantin Belousov {
24159e37c8aSRuslan Bukin 	iommu_gaddr_t isize;
24268eeb96aSKonstantin Belousov 	int am;
24368eeb96aSKonstantin Belousov 
24468eeb96aSKonstantin Belousov 	for (am = DMAR_CAP_MAMV(unit->hw_cap);; am--) {
24568eeb96aSKonstantin Belousov 		isize = 1ULL << (am + DMAR_PAGE_SHIFT);
24668eeb96aSKonstantin Belousov 		if ((base & (isize - 1)) == 0 && size >= isize)
24768eeb96aSKonstantin Belousov 			break;
24868eeb96aSKonstantin Belousov 		if (am == 0)
24968eeb96aSKonstantin Belousov 			break;
25068eeb96aSKonstantin Belousov 	}
25168eeb96aSKonstantin Belousov 	*isizep = isize;
25268eeb96aSKonstantin Belousov 	return (am);
25368eeb96aSKonstantin Belousov }
25468eeb96aSKonstantin Belousov 
25559e37c8aSRuslan Bukin iommu_haddr_t dmar_high;
25686be9f0dSKonstantin Belousov int haw;
25786be9f0dSKonstantin Belousov int dmar_tbl_pagecnt;
25886be9f0dSKonstantin Belousov 
25986be9f0dSKonstantin Belousov vm_page_t
26086be9f0dSKonstantin Belousov dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags)
26186be9f0dSKonstantin Belousov {
26286be9f0dSKonstantin Belousov 	vm_page_t m;
263dc00696aSKonstantin Belousov 	int zeroed, aflags;
26486be9f0dSKonstantin Belousov 
26586be9f0dSKonstantin Belousov 	zeroed = (flags & DMAR_PGF_ZERO) != 0 ? VM_ALLOC_ZERO : 0;
266dc00696aSKonstantin Belousov 	aflags = zeroed | VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM | VM_ALLOC_NODUMP |
267dc00696aSKonstantin Belousov 	    ((flags & DMAR_PGF_WAITOK) != 0 ? VM_ALLOC_WAITFAIL :
268dc00696aSKonstantin Belousov 	    VM_ALLOC_NOWAIT);
26986be9f0dSKonstantin Belousov 	for (;;) {
27086be9f0dSKonstantin Belousov 		if ((flags & DMAR_PGF_OBJL) == 0)
27186be9f0dSKonstantin Belousov 			VM_OBJECT_WLOCK(obj);
27286be9f0dSKonstantin Belousov 		m = vm_page_lookup(obj, idx);
27386be9f0dSKonstantin Belousov 		if ((flags & DMAR_PGF_NOALLOC) != 0 || m != NULL) {
27486be9f0dSKonstantin Belousov 			if ((flags & DMAR_PGF_OBJL) == 0)
27586be9f0dSKonstantin Belousov 				VM_OBJECT_WUNLOCK(obj);
27686be9f0dSKonstantin Belousov 			break;
27786be9f0dSKonstantin Belousov 		}
278dc00696aSKonstantin Belousov 		m = vm_page_alloc_contig(obj, idx, aflags, 1, 0,
27986be9f0dSKonstantin Belousov 		    dmar_high, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT);
28086be9f0dSKonstantin Belousov 		if ((flags & DMAR_PGF_OBJL) == 0)
28186be9f0dSKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
28286be9f0dSKonstantin Belousov 		if (m != NULL) {
28386be9f0dSKonstantin Belousov 			if (zeroed && (m->flags & PG_ZERO) == 0)
28486be9f0dSKonstantin Belousov 				pmap_zero_page(m);
28586be9f0dSKonstantin Belousov 			atomic_add_int(&dmar_tbl_pagecnt, 1);
28686be9f0dSKonstantin Belousov 			break;
28786be9f0dSKonstantin Belousov 		}
28886be9f0dSKonstantin Belousov 		if ((flags & DMAR_PGF_WAITOK) == 0)
28986be9f0dSKonstantin Belousov 			break;
29086be9f0dSKonstantin Belousov 	}
29186be9f0dSKonstantin Belousov 	return (m);
29286be9f0dSKonstantin Belousov }
29386be9f0dSKonstantin Belousov 
29486be9f0dSKonstantin Belousov void
29586be9f0dSKonstantin Belousov dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags)
29686be9f0dSKonstantin Belousov {
29786be9f0dSKonstantin Belousov 	vm_page_t m;
29886be9f0dSKonstantin Belousov 
29986be9f0dSKonstantin Belousov 	if ((flags & DMAR_PGF_OBJL) == 0)
30086be9f0dSKonstantin Belousov 		VM_OBJECT_WLOCK(obj);
3010f9e06e1SJeff Roberson 	m = vm_page_grab(obj, idx, VM_ALLOC_NOCREAT);
30286be9f0dSKonstantin Belousov 	if (m != NULL) {
30386be9f0dSKonstantin Belousov 		vm_page_free(m);
30486be9f0dSKonstantin Belousov 		atomic_subtract_int(&dmar_tbl_pagecnt, 1);
30586be9f0dSKonstantin Belousov 	}
30686be9f0dSKonstantin Belousov 	if ((flags & DMAR_PGF_OBJL) == 0)
30786be9f0dSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
30886be9f0dSKonstantin Belousov }
30986be9f0dSKonstantin Belousov 
31086be9f0dSKonstantin Belousov void *
31186be9f0dSKonstantin Belousov dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags,
31286be9f0dSKonstantin Belousov     struct sf_buf **sf)
31386be9f0dSKonstantin Belousov {
31486be9f0dSKonstantin Belousov 	vm_page_t m;
31586be9f0dSKonstantin Belousov 	bool allocated;
31686be9f0dSKonstantin Belousov 
31786be9f0dSKonstantin Belousov 	if ((flags & DMAR_PGF_OBJL) == 0)
31886be9f0dSKonstantin Belousov 		VM_OBJECT_WLOCK(obj);
31986be9f0dSKonstantin Belousov 	m = vm_page_lookup(obj, idx);
32086be9f0dSKonstantin Belousov 	if (m == NULL && (flags & DMAR_PGF_ALLOC) != 0) {
32186be9f0dSKonstantin Belousov 		m = dmar_pgalloc(obj, idx, flags | DMAR_PGF_OBJL);
32286be9f0dSKonstantin Belousov 		allocated = true;
32386be9f0dSKonstantin Belousov 	} else
32486be9f0dSKonstantin Belousov 		allocated = false;
32586be9f0dSKonstantin Belousov 	if (m == NULL) {
32686be9f0dSKonstantin Belousov 		if ((flags & DMAR_PGF_OBJL) == 0)
32786be9f0dSKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
32886be9f0dSKonstantin Belousov 		return (NULL);
32986be9f0dSKonstantin Belousov 	}
33086be9f0dSKonstantin Belousov 	/* Sleepable allocations cannot fail. */
33186be9f0dSKonstantin Belousov 	if ((flags & DMAR_PGF_WAITOK) != 0)
33286be9f0dSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
33386be9f0dSKonstantin Belousov 	sched_pin();
33486be9f0dSKonstantin Belousov 	*sf = sf_buf_alloc(m, SFB_CPUPRIVATE | ((flags & DMAR_PGF_WAITOK)
33586be9f0dSKonstantin Belousov 	    == 0 ? SFB_NOWAIT : 0));
33686be9f0dSKonstantin Belousov 	if (*sf == NULL) {
33786be9f0dSKonstantin Belousov 		sched_unpin();
33886be9f0dSKonstantin Belousov 		if (allocated) {
33986be9f0dSKonstantin Belousov 			VM_OBJECT_ASSERT_WLOCKED(obj);
34086be9f0dSKonstantin Belousov 			dmar_pgfree(obj, m->pindex, flags | DMAR_PGF_OBJL);
34186be9f0dSKonstantin Belousov 		}
34286be9f0dSKonstantin Belousov 		if ((flags & DMAR_PGF_OBJL) == 0)
34386be9f0dSKonstantin Belousov 			VM_OBJECT_WUNLOCK(obj);
34486be9f0dSKonstantin Belousov 		return (NULL);
34586be9f0dSKonstantin Belousov 	}
34686be9f0dSKonstantin Belousov 	if ((flags & (DMAR_PGF_WAITOK | DMAR_PGF_OBJL)) ==
34786be9f0dSKonstantin Belousov 	    (DMAR_PGF_WAITOK | DMAR_PGF_OBJL))
34886be9f0dSKonstantin Belousov 		VM_OBJECT_WLOCK(obj);
34986be9f0dSKonstantin Belousov 	else if ((flags & (DMAR_PGF_WAITOK | DMAR_PGF_OBJL)) == 0)
35086be9f0dSKonstantin Belousov 		VM_OBJECT_WUNLOCK(obj);
35186be9f0dSKonstantin Belousov 	return ((void *)sf_buf_kva(*sf));
35286be9f0dSKonstantin Belousov }
35386be9f0dSKonstantin Belousov 
35486be9f0dSKonstantin Belousov void
3556b7c46afSKonstantin Belousov dmar_unmap_pgtbl(struct sf_buf *sf)
35686be9f0dSKonstantin Belousov {
35786be9f0dSKonstantin Belousov 
35886be9f0dSKonstantin Belousov 	sf_buf_free(sf);
35986be9f0dSKonstantin Belousov 	sched_unpin();
3606b7c46afSKonstantin Belousov }
36186be9f0dSKonstantin Belousov 
3626b7c46afSKonstantin Belousov static void
3636b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(struct dmar_unit *unit, void *dst, size_t sz)
3646b7c46afSKonstantin Belousov {
3656b7c46afSKonstantin Belousov 
3666b7c46afSKonstantin Belousov 	if (DMAR_IS_COHERENT(unit))
3676b7c46afSKonstantin Belousov 		return;
36886be9f0dSKonstantin Belousov 	/*
36986be9f0dSKonstantin Belousov 	 * If DMAR does not snoop paging structures accesses, flush
37086be9f0dSKonstantin Belousov 	 * CPU cache to memory.
37186be9f0dSKonstantin Belousov 	 */
372d12c4465SKonstantin Belousov 	pmap_force_invalidate_cache_range((uintptr_t)dst, (uintptr_t)dst + sz);
3736b7c46afSKonstantin Belousov }
3746b7c46afSKonstantin Belousov 
3756b7c46afSKonstantin Belousov void
3766b7c46afSKonstantin Belousov dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst)
3776b7c46afSKonstantin Belousov {
3786b7c46afSKonstantin Belousov 
3796b7c46afSKonstantin Belousov 	dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
3806b7c46afSKonstantin Belousov }
3816b7c46afSKonstantin Belousov 
3826b7c46afSKonstantin Belousov void
3836b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst)
3846b7c46afSKonstantin Belousov {
3856b7c46afSKonstantin Belousov 
3866b7c46afSKonstantin Belousov 	dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
3876b7c46afSKonstantin Belousov }
3886b7c46afSKonstantin Belousov 
3896b7c46afSKonstantin Belousov void
3906b7c46afSKonstantin Belousov dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst)
3916b7c46afSKonstantin Belousov {
3926b7c46afSKonstantin Belousov 
3936b7c46afSKonstantin Belousov 	dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
39486be9f0dSKonstantin Belousov }
39586be9f0dSKonstantin Belousov 
39686be9f0dSKonstantin Belousov /*
39786be9f0dSKonstantin Belousov  * Load the root entry pointer into the hardware, busily waiting for
39886be9f0dSKonstantin Belousov  * the completion.
39986be9f0dSKonstantin Belousov  */
40086be9f0dSKonstantin Belousov int
40186be9f0dSKonstantin Belousov dmar_load_root_entry_ptr(struct dmar_unit *unit)
40286be9f0dSKonstantin Belousov {
40386be9f0dSKonstantin Belousov 	vm_page_t root_entry;
404476358b3SKonstantin Belousov 	int error;
40586be9f0dSKonstantin Belousov 
40686be9f0dSKonstantin Belousov 	/*
40786be9f0dSKonstantin Belousov 	 * Access to the GCMD register must be serialized while the
40886be9f0dSKonstantin Belousov 	 * command is submitted.
40986be9f0dSKonstantin Belousov 	 */
41086be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
41186be9f0dSKonstantin Belousov 
4125a49ae8eSKonstantin Belousov 	VM_OBJECT_RLOCK(unit->ctx_obj);
41386be9f0dSKonstantin Belousov 	root_entry = vm_page_lookup(unit->ctx_obj, 0);
4145a49ae8eSKonstantin Belousov 	VM_OBJECT_RUNLOCK(unit->ctx_obj);
41586be9f0dSKonstantin Belousov 	dmar_write8(unit, DMAR_RTADDR_REG, VM_PAGE_TO_PHYS(root_entry));
41686be9f0dSKonstantin Belousov 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SRTP);
417476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_RTPS)
418476358b3SKonstantin Belousov 	    != 0));
419476358b3SKonstantin Belousov 	return (error);
42086be9f0dSKonstantin Belousov }
42186be9f0dSKonstantin Belousov 
42286be9f0dSKonstantin Belousov /*
42386be9f0dSKonstantin Belousov  * Globally invalidate the context entries cache, busily waiting for
42486be9f0dSKonstantin Belousov  * the completion.
42586be9f0dSKonstantin Belousov  */
42686be9f0dSKonstantin Belousov int
42786be9f0dSKonstantin Belousov dmar_inv_ctx_glob(struct dmar_unit *unit)
42886be9f0dSKonstantin Belousov {
429476358b3SKonstantin Belousov 	int error;
43086be9f0dSKonstantin Belousov 
43186be9f0dSKonstantin Belousov 	/*
43286be9f0dSKonstantin Belousov 	 * Access to the CCMD register must be serialized while the
43386be9f0dSKonstantin Belousov 	 * command is submitted.
43486be9f0dSKonstantin Belousov 	 */
43586be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
43668eeb96aSKonstantin Belousov 	KASSERT(!unit->qi_enabled, ("QI enabled"));
43786be9f0dSKonstantin Belousov 
43886be9f0dSKonstantin Belousov 	/*
43986be9f0dSKonstantin Belousov 	 * The DMAR_CCMD_ICC bit in the upper dword should be written
44086be9f0dSKonstantin Belousov 	 * after the low dword write is completed.  Amd64
44186be9f0dSKonstantin Belousov 	 * dmar_write8() does not have this issue, i386 dmar_write8()
44286be9f0dSKonstantin Belousov 	 * writes the upper dword last.
44386be9f0dSKonstantin Belousov 	 */
44486be9f0dSKonstantin Belousov 	dmar_write8(unit, DMAR_CCMD_REG, DMAR_CCMD_ICC | DMAR_CCMD_CIRG_GLOB);
445476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_CCMD_REG + 4) & DMAR_CCMD_ICC32)
446476358b3SKonstantin Belousov 	    == 0));
447476358b3SKonstantin Belousov 	return (error);
44886be9f0dSKonstantin Belousov }
44986be9f0dSKonstantin Belousov 
45086be9f0dSKonstantin Belousov /*
45186be9f0dSKonstantin Belousov  * Globally invalidate the IOTLB, busily waiting for the completion.
45286be9f0dSKonstantin Belousov  */
45386be9f0dSKonstantin Belousov int
45486be9f0dSKonstantin Belousov dmar_inv_iotlb_glob(struct dmar_unit *unit)
45586be9f0dSKonstantin Belousov {
456476358b3SKonstantin Belousov 	int error, reg;
45786be9f0dSKonstantin Belousov 
45886be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
45968eeb96aSKonstantin Belousov 	KASSERT(!unit->qi_enabled, ("QI enabled"));
46086be9f0dSKonstantin Belousov 
46186be9f0dSKonstantin Belousov 	reg = 16 * DMAR_ECAP_IRO(unit->hw_ecap);
46286be9f0dSKonstantin Belousov 	/* See a comment about DMAR_CCMD_ICC in dmar_inv_ctx_glob. */
46386be9f0dSKonstantin Belousov 	dmar_write8(unit, reg + DMAR_IOTLB_REG_OFF, DMAR_IOTLB_IVT |
46486be9f0dSKonstantin Belousov 	    DMAR_IOTLB_IIRG_GLB | DMAR_IOTLB_DR | DMAR_IOTLB_DW);
465476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, reg + DMAR_IOTLB_REG_OFF + 4) &
466476358b3SKonstantin Belousov 	    DMAR_IOTLB_IVT32) == 0));
467476358b3SKonstantin Belousov 	return (error);
46886be9f0dSKonstantin Belousov }
46986be9f0dSKonstantin Belousov 
47086be9f0dSKonstantin Belousov /*
47186be9f0dSKonstantin Belousov  * Flush the chipset write buffers.  See 11.1 "Write Buffer Flushing"
47286be9f0dSKonstantin Belousov  * in the architecture specification.
47386be9f0dSKonstantin Belousov  */
47486be9f0dSKonstantin Belousov int
47586be9f0dSKonstantin Belousov dmar_flush_write_bufs(struct dmar_unit *unit)
47686be9f0dSKonstantin Belousov {
477476358b3SKonstantin Belousov 	int error;
47886be9f0dSKonstantin Belousov 
47986be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
48086be9f0dSKonstantin Belousov 
48186be9f0dSKonstantin Belousov 	/*
48286be9f0dSKonstantin Belousov 	 * DMAR_GCMD_WBF is only valid when CAP_RWBF is reported.
48386be9f0dSKonstantin Belousov 	 */
48486be9f0dSKonstantin Belousov 	KASSERT((unit->hw_cap & DMAR_CAP_RWBF) != 0,
48559e37c8aSRuslan Bukin 	    ("dmar%d: no RWBF", unit->iommu.unit));
48686be9f0dSKonstantin Belousov 
48786be9f0dSKonstantin Belousov 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_WBF);
488476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_WBFS)
489476358b3SKonstantin Belousov 	    != 0));
490476358b3SKonstantin Belousov 	return (error);
49186be9f0dSKonstantin Belousov }
49286be9f0dSKonstantin Belousov 
49386be9f0dSKonstantin Belousov int
49486be9f0dSKonstantin Belousov dmar_enable_translation(struct dmar_unit *unit)
49586be9f0dSKonstantin Belousov {
496476358b3SKonstantin Belousov 	int error;
49786be9f0dSKonstantin Belousov 
49886be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
49986be9f0dSKonstantin Belousov 	unit->hw_gcmd |= DMAR_GCMD_TE;
50086be9f0dSKonstantin Belousov 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
501476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES)
502476358b3SKonstantin Belousov 	    != 0));
503476358b3SKonstantin Belousov 	return (error);
50486be9f0dSKonstantin Belousov }
50586be9f0dSKonstantin Belousov 
50686be9f0dSKonstantin Belousov int
50786be9f0dSKonstantin Belousov dmar_disable_translation(struct dmar_unit *unit)
50886be9f0dSKonstantin Belousov {
509476358b3SKonstantin Belousov 	int error;
51086be9f0dSKonstantin Belousov 
51186be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
51286be9f0dSKonstantin Belousov 	unit->hw_gcmd &= ~DMAR_GCMD_TE;
51386be9f0dSKonstantin Belousov 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
514476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES)
515476358b3SKonstantin Belousov 	    == 0));
516476358b3SKonstantin Belousov 	return (error);
51786be9f0dSKonstantin Belousov }
51886be9f0dSKonstantin Belousov 
5190a110d5bSKonstantin Belousov int
5200a110d5bSKonstantin Belousov dmar_load_irt_ptr(struct dmar_unit *unit)
5210a110d5bSKonstantin Belousov {
5220a110d5bSKonstantin Belousov 	uint64_t irta, s;
523476358b3SKonstantin Belousov 	int error;
5240a110d5bSKonstantin Belousov 
5250a110d5bSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
5260a110d5bSKonstantin Belousov 	irta = unit->irt_phys;
5270a110d5bSKonstantin Belousov 	if (DMAR_X2APIC(unit))
5280a110d5bSKonstantin Belousov 		irta |= DMAR_IRTA_EIME;
5290a110d5bSKonstantin Belousov 	s = fls(unit->irte_cnt) - 2;
5300a110d5bSKonstantin Belousov 	KASSERT(unit->irte_cnt >= 2 && s <= DMAR_IRTA_S_MASK &&
5310a110d5bSKonstantin Belousov 	    powerof2(unit->irte_cnt),
5320a110d5bSKonstantin Belousov 	    ("IRTA_REG_S overflow %x", unit->irte_cnt));
5330a110d5bSKonstantin Belousov 	irta |= s;
5340a110d5bSKonstantin Belousov 	dmar_write8(unit, DMAR_IRTA_REG, irta);
5350a110d5bSKonstantin Belousov 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SIRTP);
536476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRTPS)
537476358b3SKonstantin Belousov 	    != 0));
538476358b3SKonstantin Belousov 	return (error);
5390a110d5bSKonstantin Belousov }
5400a110d5bSKonstantin Belousov 
5410a110d5bSKonstantin Belousov int
5420a110d5bSKonstantin Belousov dmar_enable_ir(struct dmar_unit *unit)
5430a110d5bSKonstantin Belousov {
544476358b3SKonstantin Belousov 	int error;
5450a110d5bSKonstantin Belousov 
5460a110d5bSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
5470a110d5bSKonstantin Belousov 	unit->hw_gcmd |= DMAR_GCMD_IRE;
5480a110d5bSKonstantin Belousov 	unit->hw_gcmd &= ~DMAR_GCMD_CFI;
5490a110d5bSKonstantin Belousov 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
550476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES)
551476358b3SKonstantin Belousov 	    != 0));
552476358b3SKonstantin Belousov 	return (error);
5530a110d5bSKonstantin Belousov }
5540a110d5bSKonstantin Belousov 
5550a110d5bSKonstantin Belousov int
5560a110d5bSKonstantin Belousov dmar_disable_ir(struct dmar_unit *unit)
5570a110d5bSKonstantin Belousov {
558476358b3SKonstantin Belousov 	int error;
5590a110d5bSKonstantin Belousov 
5600a110d5bSKonstantin Belousov 	DMAR_ASSERT_LOCKED(unit);
5610a110d5bSKonstantin Belousov 	unit->hw_gcmd &= ~DMAR_GCMD_IRE;
5620a110d5bSKonstantin Belousov 	dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
563476358b3SKonstantin Belousov 	DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES)
564476358b3SKonstantin Belousov 	    == 0));
565476358b3SKonstantin Belousov 	return (error);
5660a110d5bSKonstantin Belousov }
5670a110d5bSKonstantin Belousov 
56886be9f0dSKonstantin Belousov #define BARRIER_F				\
56986be9f0dSKonstantin Belousov 	u_int f_done, f_inproc, f_wakeup;	\
57086be9f0dSKonstantin Belousov 						\
57186be9f0dSKonstantin Belousov 	f_done = 1 << (barrier_id * 3);		\
57286be9f0dSKonstantin Belousov 	f_inproc = 1 << (barrier_id * 3 + 1);	\
57386be9f0dSKonstantin Belousov 	f_wakeup = 1 << (barrier_id * 3 + 2)
57486be9f0dSKonstantin Belousov 
57586be9f0dSKonstantin Belousov bool
57686be9f0dSKonstantin Belousov dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id)
57786be9f0dSKonstantin Belousov {
57886be9f0dSKonstantin Belousov 	BARRIER_F;
57986be9f0dSKonstantin Belousov 
58086be9f0dSKonstantin Belousov 	DMAR_LOCK(dmar);
58186be9f0dSKonstantin Belousov 	if ((dmar->barrier_flags & f_done) != 0) {
58286be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
58386be9f0dSKonstantin Belousov 		return (false);
58486be9f0dSKonstantin Belousov 	}
58586be9f0dSKonstantin Belousov 
58686be9f0dSKonstantin Belousov 	if ((dmar->barrier_flags & f_inproc) != 0) {
58786be9f0dSKonstantin Belousov 		while ((dmar->barrier_flags & f_inproc) != 0) {
58886be9f0dSKonstantin Belousov 			dmar->barrier_flags |= f_wakeup;
58959e37c8aSRuslan Bukin 			msleep(&dmar->barrier_flags, &dmar->iommu.lock, 0,
59086be9f0dSKonstantin Belousov 			    "dmarb", 0);
59186be9f0dSKonstantin Belousov 		}
59286be9f0dSKonstantin Belousov 		KASSERT((dmar->barrier_flags & f_done) != 0,
59359e37c8aSRuslan Bukin 		    ("dmar%d barrier %d missing done", dmar->iommu.unit,
59459e37c8aSRuslan Bukin 		    barrier_id));
59586be9f0dSKonstantin Belousov 		DMAR_UNLOCK(dmar);
59686be9f0dSKonstantin Belousov 		return (false);
59786be9f0dSKonstantin Belousov 	}
59886be9f0dSKonstantin Belousov 
59986be9f0dSKonstantin Belousov 	dmar->barrier_flags |= f_inproc;
60086be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
60186be9f0dSKonstantin Belousov 	return (true);
60286be9f0dSKonstantin Belousov }
60386be9f0dSKonstantin Belousov 
60486be9f0dSKonstantin Belousov void
60586be9f0dSKonstantin Belousov dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id)
60686be9f0dSKonstantin Belousov {
60786be9f0dSKonstantin Belousov 	BARRIER_F;
60886be9f0dSKonstantin Belousov 
60986be9f0dSKonstantin Belousov 	DMAR_ASSERT_LOCKED(dmar);
61086be9f0dSKonstantin Belousov 	KASSERT((dmar->barrier_flags & (f_done | f_inproc)) == f_inproc,
61159e37c8aSRuslan Bukin 	    ("dmar%d barrier %d missed entry", dmar->iommu.unit, barrier_id));
61286be9f0dSKonstantin Belousov 	dmar->barrier_flags |= f_done;
61386be9f0dSKonstantin Belousov 	if ((dmar->barrier_flags & f_wakeup) != 0)
61486be9f0dSKonstantin Belousov 		wakeup(&dmar->barrier_flags);
61586be9f0dSKonstantin Belousov 	dmar->barrier_flags &= ~(f_inproc | f_wakeup);
61686be9f0dSKonstantin Belousov 	DMAR_UNLOCK(dmar);
61786be9f0dSKonstantin Belousov }
61886be9f0dSKonstantin Belousov 
619e164cafcSKonstantin Belousov int dmar_batch_coalesce = 100;
620476358b3SKonstantin Belousov struct timespec dmar_hw_timeout = {
621476358b3SKonstantin Belousov 	.tv_sec = 0,
622476358b3SKonstantin Belousov 	.tv_nsec = 1000000
623476358b3SKonstantin Belousov };
624476358b3SKonstantin Belousov 
625476358b3SKonstantin Belousov static const uint64_t d = 1000000000;
626476358b3SKonstantin Belousov 
627476358b3SKonstantin Belousov void
628476358b3SKonstantin Belousov dmar_update_timeout(uint64_t newval)
629476358b3SKonstantin Belousov {
630476358b3SKonstantin Belousov 
631476358b3SKonstantin Belousov 	/* XXXKIB not atomic */
632476358b3SKonstantin Belousov 	dmar_hw_timeout.tv_sec = newval / d;
633476358b3SKonstantin Belousov 	dmar_hw_timeout.tv_nsec = newval % d;
634476358b3SKonstantin Belousov }
635476358b3SKonstantin Belousov 
636476358b3SKonstantin Belousov uint64_t
637476358b3SKonstantin Belousov dmar_get_timeout(void)
638476358b3SKonstantin Belousov {
639476358b3SKonstantin Belousov 
640476358b3SKonstantin Belousov 	return ((uint64_t)dmar_hw_timeout.tv_sec * d +
641476358b3SKonstantin Belousov 	    dmar_hw_timeout.tv_nsec);
642476358b3SKonstantin Belousov }
643476358b3SKonstantin Belousov 
644476358b3SKonstantin Belousov static int
645476358b3SKonstantin Belousov dmar_timeout_sysctl(SYSCTL_HANDLER_ARGS)
646476358b3SKonstantin Belousov {
647476358b3SKonstantin Belousov 	uint64_t val;
648476358b3SKonstantin Belousov 	int error;
649476358b3SKonstantin Belousov 
650476358b3SKonstantin Belousov 	val = dmar_get_timeout();
651476358b3SKonstantin Belousov 	error = sysctl_handle_long(oidp, &val, 0, req);
652476358b3SKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
653476358b3SKonstantin Belousov 		return (error);
654476358b3SKonstantin Belousov 	dmar_update_timeout(val);
655476358b3SKonstantin Belousov 	return (error);
656476358b3SKonstantin Belousov }
65786be9f0dSKonstantin Belousov 
658*357149f0SRuslan Bukin static SYSCTL_NODE(_hw_iommu, OID_AUTO, dmar, CTLFLAG_RD | CTLFLAG_MPSAFE,
659*357149f0SRuslan Bukin     NULL, "");
660*357149f0SRuslan Bukin SYSCTL_INT(_hw_iommu_dmar, OID_AUTO, tbl_pagecnt, CTLFLAG_RD,
66186be9f0dSKonstantin Belousov     &dmar_tbl_pagecnt, 0,
66286be9f0dSKonstantin Belousov     "Count of pages used for DMAR pagetables");
663*357149f0SRuslan Bukin SYSCTL_INT(_hw_iommu_dmar, OID_AUTO, batch_coalesce, CTLFLAG_RWTUN,
664e164cafcSKonstantin Belousov     &dmar_batch_coalesce, 0,
665e164cafcSKonstantin Belousov     "Number of qi batches between interrupt");
666*357149f0SRuslan Bukin SYSCTL_PROC(_hw_iommu_dmar, OID_AUTO, timeout,
667476358b3SKonstantin Belousov     CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
668476358b3SKonstantin Belousov     dmar_timeout_sysctl, "QU",
669476358b3SKonstantin Belousov     "Timeout for command wait, in nanoseconds");
670