186be9f0dSKonstantin Belousov /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni *
486be9f0dSKonstantin Belousov * Copyright (c) 2013 The FreeBSD Foundation
586be9f0dSKonstantin Belousov *
686be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
786be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation.
886be9f0dSKonstantin Belousov *
986be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without
1086be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions
1186be9f0dSKonstantin Belousov * are met:
1286be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright
1386be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer.
1486be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright
1586be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the
1686be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution.
1786be9f0dSKonstantin Belousov *
1886be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886be9f0dSKonstantin Belousov * SUCH DAMAGE.
2986be9f0dSKonstantin Belousov */
3086be9f0dSKonstantin Belousov
3186be9f0dSKonstantin Belousov #include <sys/param.h>
3286be9f0dSKonstantin Belousov #include <sys/bus.h>
3386be9f0dSKonstantin Belousov #include <sys/kernel.h>
3486be9f0dSKonstantin Belousov #include <sys/lock.h>
3586be9f0dSKonstantin Belousov #include <sys/malloc.h>
3686be9f0dSKonstantin Belousov #include <sys/memdesc.h>
3786be9f0dSKonstantin Belousov #include <sys/mutex.h>
3886be9f0dSKonstantin Belousov #include <sys/proc.h>
3986be9f0dSKonstantin Belousov #include <sys/queue.h>
4086be9f0dSKonstantin Belousov #include <sys/rman.h>
4186be9f0dSKonstantin Belousov #include <sys/rwlock.h>
4286be9f0dSKonstantin Belousov #include <sys/sched.h>
4386be9f0dSKonstantin Belousov #include <sys/sf_buf.h>
4486be9f0dSKonstantin Belousov #include <sys/sysctl.h>
4586be9f0dSKonstantin Belousov #include <sys/systm.h>
4686be9f0dSKonstantin Belousov #include <sys/taskqueue.h>
47476358b3SKonstantin Belousov #include <sys/time.h>
4886be9f0dSKonstantin Belousov #include <sys/tree.h>
490a110d5bSKonstantin Belousov #include <sys/vmem.h>
5086be9f0dSKonstantin Belousov #include <vm/vm.h>
5186be9f0dSKonstantin Belousov #include <vm/vm_extern.h>
5286be9f0dSKonstantin Belousov #include <vm/vm_kern.h>
5386be9f0dSKonstantin Belousov #include <vm/vm_object.h>
5486be9f0dSKonstantin Belousov #include <vm/vm_page.h>
5586be9f0dSKonstantin Belousov #include <vm/vm_map.h>
5686be9f0dSKonstantin Belousov #include <vm/vm_pageout.h>
57c8597a1fSRuslan Bukin #include <dev/pci/pcireg.h>
58c8597a1fSRuslan Bukin #include <dev/pci/pcivar.h>
5986be9f0dSKonstantin Belousov #include <machine/bus.h>
6086be9f0dSKonstantin Belousov #include <machine/cpu.h>
610a110d5bSKonstantin Belousov #include <machine/intr_machdep.h>
620a110d5bSKonstantin Belousov #include <x86/include/apicvar.h>
6386be9f0dSKonstantin Belousov #include <x86/include/busdma_impl.h>
64f2b2f317SRuslan Bukin #include <dev/iommu/busdma_iommu.h>
65c8597a1fSRuslan Bukin #include <x86/iommu/intel_reg.h>
6640d951bcSKonstantin Belousov #include <x86/iommu/x86_iommu.h>
6786be9f0dSKonstantin Belousov #include <x86/iommu/intel_dmar.h>
6886be9f0dSKonstantin Belousov
6986be9f0dSKonstantin Belousov u_int
dmar_nd2mask(u_int nd)7086be9f0dSKonstantin Belousov dmar_nd2mask(u_int nd)
7186be9f0dSKonstantin Belousov {
7286be9f0dSKonstantin Belousov static const u_int masks[] = {
7386be9f0dSKonstantin Belousov 0x000f, /* nd == 0 */
7486be9f0dSKonstantin Belousov 0x002f, /* nd == 1 */
7586be9f0dSKonstantin Belousov 0x00ff, /* nd == 2 */
7686be9f0dSKonstantin Belousov 0x02ff, /* nd == 3 */
7786be9f0dSKonstantin Belousov 0x0fff, /* nd == 4 */
7886be9f0dSKonstantin Belousov 0x2fff, /* nd == 5 */
7986be9f0dSKonstantin Belousov 0xffff, /* nd == 6 */
8086be9f0dSKonstantin Belousov 0x0000, /* nd == 7 reserved */
8186be9f0dSKonstantin Belousov };
8286be9f0dSKonstantin Belousov
8386be9f0dSKonstantin Belousov KASSERT(nd <= 6, ("number of domains %d", nd));
8486be9f0dSKonstantin Belousov return (masks[nd]);
8586be9f0dSKonstantin Belousov }
8686be9f0dSKonstantin Belousov
8786be9f0dSKonstantin Belousov static const struct sagaw_bits_tag {
8886be9f0dSKonstantin Belousov int agaw;
8986be9f0dSKonstantin Belousov int cap;
9086be9f0dSKonstantin Belousov int awlvl;
9186be9f0dSKonstantin Belousov int pglvl;
9286be9f0dSKonstantin Belousov } sagaw_bits[] = {
9386be9f0dSKonstantin Belousov {.agaw = 30, .cap = DMAR_CAP_SAGAW_2LVL, .awlvl = DMAR_CTX2_AW_2LVL,
9486be9f0dSKonstantin Belousov .pglvl = 2},
9586be9f0dSKonstantin Belousov {.agaw = 39, .cap = DMAR_CAP_SAGAW_3LVL, .awlvl = DMAR_CTX2_AW_3LVL,
9686be9f0dSKonstantin Belousov .pglvl = 3},
9786be9f0dSKonstantin Belousov {.agaw = 48, .cap = DMAR_CAP_SAGAW_4LVL, .awlvl = DMAR_CTX2_AW_4LVL,
9886be9f0dSKonstantin Belousov .pglvl = 4},
9986be9f0dSKonstantin Belousov {.agaw = 57, .cap = DMAR_CAP_SAGAW_5LVL, .awlvl = DMAR_CTX2_AW_5LVL,
1006f378116SJason A. Harmening .pglvl = 5}
1016f378116SJason A. Harmening /*
1026f378116SJason A. Harmening * 6-level paging (DMAR_CAP_SAGAW_6LVL) is not supported on any
1036f378116SJason A. Harmening * current VT-d hardware and its SAGAW field value is listed as
1046f378116SJason A. Harmening * reserved in the VT-d spec. If support is added in the future,
1056f378116SJason A. Harmening * this structure and the logic in dmar_maxaddr2mgaw() will need
1066f378116SJason A. Harmening * to change to avoid attempted comparison against 1ULL << 64.
1076f378116SJason A. Harmening */
10886be9f0dSKonstantin Belousov };
10986be9f0dSKonstantin Belousov
11086be9f0dSKonstantin Belousov bool
dmar_pglvl_supported(struct dmar_unit * unit,int pglvl)11186be9f0dSKonstantin Belousov dmar_pglvl_supported(struct dmar_unit *unit, int pglvl)
11286be9f0dSKonstantin Belousov {
11386be9f0dSKonstantin Belousov int i;
11486be9f0dSKonstantin Belousov
1151abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) {
11686be9f0dSKonstantin Belousov if (sagaw_bits[i].pglvl != pglvl)
11786be9f0dSKonstantin Belousov continue;
11886be9f0dSKonstantin Belousov if ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0)
11986be9f0dSKonstantin Belousov return (true);
12086be9f0dSKonstantin Belousov }
12186be9f0dSKonstantin Belousov return (false);
12286be9f0dSKonstantin Belousov }
12386be9f0dSKonstantin Belousov
12486be9f0dSKonstantin Belousov int
domain_set_agaw(struct dmar_domain * domain,int mgaw)1251abfd355SKonstantin Belousov domain_set_agaw(struct dmar_domain *domain, int mgaw)
12686be9f0dSKonstantin Belousov {
12786be9f0dSKonstantin Belousov int sagaw, i;
12886be9f0dSKonstantin Belousov
1291abfd355SKonstantin Belousov domain->mgaw = mgaw;
1301abfd355SKonstantin Belousov sagaw = DMAR_CAP_SAGAW(domain->dmar->hw_cap);
1311abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) {
13286be9f0dSKonstantin Belousov if (sagaw_bits[i].agaw >= mgaw) {
1331abfd355SKonstantin Belousov domain->agaw = sagaw_bits[i].agaw;
1341abfd355SKonstantin Belousov domain->pglvl = sagaw_bits[i].pglvl;
1351abfd355SKonstantin Belousov domain->awlvl = sagaw_bits[i].awlvl;
13686be9f0dSKonstantin Belousov return (0);
13786be9f0dSKonstantin Belousov }
13886be9f0dSKonstantin Belousov }
139*164fdee1SKonstantin Belousov device_printf(domain->dmar->iommu.dev,
1401abfd355SKonstantin Belousov "context request mgaw %d: no agaw found, sagaw %x\n",
1411abfd355SKonstantin Belousov mgaw, sagaw);
14286be9f0dSKonstantin Belousov return (EINVAL);
14386be9f0dSKonstantin Belousov }
14486be9f0dSKonstantin Belousov
14586be9f0dSKonstantin Belousov /*
14686be9f0dSKonstantin Belousov * Find a best fit mgaw for the given maxaddr:
14786be9f0dSKonstantin Belousov * - if allow_less is false, must find sagaw which maps all requested
14886be9f0dSKonstantin Belousov * addresses (used by identity mappings);
14986be9f0dSKonstantin Belousov * - if allow_less is true, and no supported sagaw can map all requested
15086be9f0dSKonstantin Belousov * address space, accept the biggest sagaw, whatever is it.
15186be9f0dSKonstantin Belousov */
15286be9f0dSKonstantin Belousov int
dmar_maxaddr2mgaw(struct dmar_unit * unit,iommu_gaddr_t maxaddr,bool allow_less)15359e37c8aSRuslan Bukin dmar_maxaddr2mgaw(struct dmar_unit *unit, iommu_gaddr_t maxaddr, bool allow_less)
15486be9f0dSKonstantin Belousov {
15586be9f0dSKonstantin Belousov int i;
15686be9f0dSKonstantin Belousov
1571abfd355SKonstantin Belousov for (i = 0; i < nitems(sagaw_bits); i++) {
15886be9f0dSKonstantin Belousov if ((1ULL << sagaw_bits[i].agaw) >= maxaddr &&
15986be9f0dSKonstantin Belousov (DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap) != 0)
16086be9f0dSKonstantin Belousov break;
16186be9f0dSKonstantin Belousov }
1621abfd355SKonstantin Belousov if (allow_less && i == nitems(sagaw_bits)) {
16386be9f0dSKonstantin Belousov do {
16486be9f0dSKonstantin Belousov i--;
16586be9f0dSKonstantin Belousov } while ((DMAR_CAP_SAGAW(unit->hw_cap) & sagaw_bits[i].cap)
16686be9f0dSKonstantin Belousov == 0);
16786be9f0dSKonstantin Belousov }
1681abfd355SKonstantin Belousov if (i < nitems(sagaw_bits))
16986be9f0dSKonstantin Belousov return (sagaw_bits[i].agaw);
17086be9f0dSKonstantin Belousov KASSERT(0, ("no mgaw for maxaddr %jx allow_less %d",
17186be9f0dSKonstantin Belousov (uintmax_t) maxaddr, allow_less));
17286be9f0dSKonstantin Belousov return (-1);
17386be9f0dSKonstantin Belousov }
17486be9f0dSKonstantin Belousov
17586be9f0dSKonstantin Belousov /*
17686be9f0dSKonstantin Belousov * Return true if the page table level lvl supports the superpage for
17786be9f0dSKonstantin Belousov * the context ctx.
17886be9f0dSKonstantin Belousov */
17986be9f0dSKonstantin Belousov int
domain_is_sp_lvl(struct dmar_domain * domain,int lvl)1801abfd355SKonstantin Belousov domain_is_sp_lvl(struct dmar_domain *domain, int lvl)
18186be9f0dSKonstantin Belousov {
18286be9f0dSKonstantin Belousov int alvl, cap_sps;
18386be9f0dSKonstantin Belousov static const int sagaw_sp[] = {
18486be9f0dSKonstantin Belousov DMAR_CAP_SPS_2M,
18586be9f0dSKonstantin Belousov DMAR_CAP_SPS_1G,
18686be9f0dSKonstantin Belousov DMAR_CAP_SPS_512G,
18786be9f0dSKonstantin Belousov DMAR_CAP_SPS_1T
18886be9f0dSKonstantin Belousov };
18986be9f0dSKonstantin Belousov
1901abfd355SKonstantin Belousov alvl = domain->pglvl - lvl - 1;
1911abfd355SKonstantin Belousov cap_sps = DMAR_CAP_SPS(domain->dmar->hw_cap);
1921abfd355SKonstantin Belousov return (alvl < nitems(sagaw_sp) && (sagaw_sp[alvl] & cap_sps) != 0);
19386be9f0dSKonstantin Belousov }
19486be9f0dSKonstantin Belousov
19559e37c8aSRuslan Bukin iommu_gaddr_t
domain_page_size(struct dmar_domain * domain,int lvl)1961abfd355SKonstantin Belousov domain_page_size(struct dmar_domain *domain, int lvl)
19786be9f0dSKonstantin Belousov {
19886be9f0dSKonstantin Belousov
1991abfd355SKonstantin Belousov return (pglvl_page_size(domain->pglvl, lvl));
20086be9f0dSKonstantin Belousov }
20186be9f0dSKonstantin Belousov
20268eeb96aSKonstantin Belousov int
calc_am(struct dmar_unit * unit,iommu_gaddr_t base,iommu_gaddr_t size,iommu_gaddr_t * isizep)20359e37c8aSRuslan Bukin calc_am(struct dmar_unit *unit, iommu_gaddr_t base, iommu_gaddr_t size,
20459e37c8aSRuslan Bukin iommu_gaddr_t *isizep)
20568eeb96aSKonstantin Belousov {
20659e37c8aSRuslan Bukin iommu_gaddr_t isize;
20768eeb96aSKonstantin Belousov int am;
20868eeb96aSKonstantin Belousov
20968eeb96aSKonstantin Belousov for (am = DMAR_CAP_MAMV(unit->hw_cap);; am--) {
21040d951bcSKonstantin Belousov isize = 1ULL << (am + IOMMU_PAGE_SHIFT);
21168eeb96aSKonstantin Belousov if ((base & (isize - 1)) == 0 && size >= isize)
21268eeb96aSKonstantin Belousov break;
21368eeb96aSKonstantin Belousov if (am == 0)
21468eeb96aSKonstantin Belousov break;
21568eeb96aSKonstantin Belousov }
21668eeb96aSKonstantin Belousov *isizep = isize;
21768eeb96aSKonstantin Belousov return (am);
21868eeb96aSKonstantin Belousov }
21968eeb96aSKonstantin Belousov
22086be9f0dSKonstantin Belousov int haw;
22186be9f0dSKonstantin Belousov int dmar_tbl_pagecnt;
22286be9f0dSKonstantin Belousov
2236b7c46afSKonstantin Belousov static void
dmar_flush_transl_to_ram(struct dmar_unit * unit,void * dst,size_t sz)2246b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(struct dmar_unit *unit, void *dst, size_t sz)
2256b7c46afSKonstantin Belousov {
2266b7c46afSKonstantin Belousov
2276b7c46afSKonstantin Belousov if (DMAR_IS_COHERENT(unit))
2286b7c46afSKonstantin Belousov return;
22986be9f0dSKonstantin Belousov /*
23086be9f0dSKonstantin Belousov * If DMAR does not snoop paging structures accesses, flush
23186be9f0dSKonstantin Belousov * CPU cache to memory.
23286be9f0dSKonstantin Belousov */
233d12c4465SKonstantin Belousov pmap_force_invalidate_cache_range((uintptr_t)dst, (uintptr_t)dst + sz);
2346b7c46afSKonstantin Belousov }
2356b7c46afSKonstantin Belousov
2366b7c46afSKonstantin Belousov void
dmar_flush_pte_to_ram(struct dmar_unit * unit,iommu_pte_t * dst)23740d951bcSKonstantin Belousov dmar_flush_pte_to_ram(struct dmar_unit *unit, iommu_pte_t *dst)
2386b7c46afSKonstantin Belousov {
2396b7c46afSKonstantin Belousov
2406b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
2416b7c46afSKonstantin Belousov }
2426b7c46afSKonstantin Belousov
2436b7c46afSKonstantin Belousov void
dmar_flush_ctx_to_ram(struct dmar_unit * unit,dmar_ctx_entry_t * dst)2446b7c46afSKonstantin Belousov dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst)
2456b7c46afSKonstantin Belousov {
2466b7c46afSKonstantin Belousov
2476b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
2486b7c46afSKonstantin Belousov }
2496b7c46afSKonstantin Belousov
2506b7c46afSKonstantin Belousov void
dmar_flush_root_to_ram(struct dmar_unit * unit,dmar_root_entry_t * dst)2516b7c46afSKonstantin Belousov dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst)
2526b7c46afSKonstantin Belousov {
2536b7c46afSKonstantin Belousov
2546b7c46afSKonstantin Belousov dmar_flush_transl_to_ram(unit, dst, sizeof(*dst));
25586be9f0dSKonstantin Belousov }
25686be9f0dSKonstantin Belousov
25786be9f0dSKonstantin Belousov /*
25886be9f0dSKonstantin Belousov * Load the root entry pointer into the hardware, busily waiting for
25986be9f0dSKonstantin Belousov * the completion.
26086be9f0dSKonstantin Belousov */
26186be9f0dSKonstantin Belousov int
dmar_load_root_entry_ptr(struct dmar_unit * unit)26286be9f0dSKonstantin Belousov dmar_load_root_entry_ptr(struct dmar_unit *unit)
26386be9f0dSKonstantin Belousov {
26486be9f0dSKonstantin Belousov vm_page_t root_entry;
265476358b3SKonstantin Belousov int error;
26686be9f0dSKonstantin Belousov
26786be9f0dSKonstantin Belousov /*
26886be9f0dSKonstantin Belousov * Access to the GCMD register must be serialized while the
26986be9f0dSKonstantin Belousov * command is submitted.
27086be9f0dSKonstantin Belousov */
27186be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
27286be9f0dSKonstantin Belousov
2735a49ae8eSKonstantin Belousov VM_OBJECT_RLOCK(unit->ctx_obj);
27486be9f0dSKonstantin Belousov root_entry = vm_page_lookup(unit->ctx_obj, 0);
2755a49ae8eSKonstantin Belousov VM_OBJECT_RUNLOCK(unit->ctx_obj);
27686be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_RTADDR_REG, VM_PAGE_TO_PHYS(root_entry));
27786be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SRTP);
278476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_RTPS)
279476358b3SKonstantin Belousov != 0));
280476358b3SKonstantin Belousov return (error);
28186be9f0dSKonstantin Belousov }
28286be9f0dSKonstantin Belousov
28386be9f0dSKonstantin Belousov /*
28486be9f0dSKonstantin Belousov * Globally invalidate the context entries cache, busily waiting for
28586be9f0dSKonstantin Belousov * the completion.
28686be9f0dSKonstantin Belousov */
28786be9f0dSKonstantin Belousov int
dmar_inv_ctx_glob(struct dmar_unit * unit)28886be9f0dSKonstantin Belousov dmar_inv_ctx_glob(struct dmar_unit *unit)
28986be9f0dSKonstantin Belousov {
290476358b3SKonstantin Belousov int error;
29186be9f0dSKonstantin Belousov
29286be9f0dSKonstantin Belousov /*
29386be9f0dSKonstantin Belousov * Access to the CCMD register must be serialized while the
29486be9f0dSKonstantin Belousov * command is submitted.
29586be9f0dSKonstantin Belousov */
29686be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
29768eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled"));
29886be9f0dSKonstantin Belousov
29986be9f0dSKonstantin Belousov /*
30086be9f0dSKonstantin Belousov * The DMAR_CCMD_ICC bit in the upper dword should be written
30186be9f0dSKonstantin Belousov * after the low dword write is completed. Amd64
30286be9f0dSKonstantin Belousov * dmar_write8() does not have this issue, i386 dmar_write8()
30386be9f0dSKonstantin Belousov * writes the upper dword last.
30486be9f0dSKonstantin Belousov */
30586be9f0dSKonstantin Belousov dmar_write8(unit, DMAR_CCMD_REG, DMAR_CCMD_ICC | DMAR_CCMD_CIRG_GLOB);
306476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_CCMD_REG + 4) & DMAR_CCMD_ICC32)
307476358b3SKonstantin Belousov == 0));
308476358b3SKonstantin Belousov return (error);
30986be9f0dSKonstantin Belousov }
31086be9f0dSKonstantin Belousov
31186be9f0dSKonstantin Belousov /*
31286be9f0dSKonstantin Belousov * Globally invalidate the IOTLB, busily waiting for the completion.
31386be9f0dSKonstantin Belousov */
31486be9f0dSKonstantin Belousov int
dmar_inv_iotlb_glob(struct dmar_unit * unit)31586be9f0dSKonstantin Belousov dmar_inv_iotlb_glob(struct dmar_unit *unit)
31686be9f0dSKonstantin Belousov {
317476358b3SKonstantin Belousov int error, reg;
31886be9f0dSKonstantin Belousov
31986be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
32068eeb96aSKonstantin Belousov KASSERT(!unit->qi_enabled, ("QI enabled"));
32186be9f0dSKonstantin Belousov
32286be9f0dSKonstantin Belousov reg = 16 * DMAR_ECAP_IRO(unit->hw_ecap);
32386be9f0dSKonstantin Belousov /* See a comment about DMAR_CCMD_ICC in dmar_inv_ctx_glob. */
32486be9f0dSKonstantin Belousov dmar_write8(unit, reg + DMAR_IOTLB_REG_OFF, DMAR_IOTLB_IVT |
32586be9f0dSKonstantin Belousov DMAR_IOTLB_IIRG_GLB | DMAR_IOTLB_DR | DMAR_IOTLB_DW);
326476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, reg + DMAR_IOTLB_REG_OFF + 4) &
327476358b3SKonstantin Belousov DMAR_IOTLB_IVT32) == 0));
328476358b3SKonstantin Belousov return (error);
32986be9f0dSKonstantin Belousov }
33086be9f0dSKonstantin Belousov
33186be9f0dSKonstantin Belousov /*
33286be9f0dSKonstantin Belousov * Flush the chipset write buffers. See 11.1 "Write Buffer Flushing"
33386be9f0dSKonstantin Belousov * in the architecture specification.
33486be9f0dSKonstantin Belousov */
33586be9f0dSKonstantin Belousov int
dmar_flush_write_bufs(struct dmar_unit * unit)33686be9f0dSKonstantin Belousov dmar_flush_write_bufs(struct dmar_unit *unit)
33786be9f0dSKonstantin Belousov {
338476358b3SKonstantin Belousov int error;
33986be9f0dSKonstantin Belousov
34086be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
34186be9f0dSKonstantin Belousov
34286be9f0dSKonstantin Belousov /*
34386be9f0dSKonstantin Belousov * DMAR_GCMD_WBF is only valid when CAP_RWBF is reported.
34486be9f0dSKonstantin Belousov */
34586be9f0dSKonstantin Belousov KASSERT((unit->hw_cap & DMAR_CAP_RWBF) != 0,
34659e37c8aSRuslan Bukin ("dmar%d: no RWBF", unit->iommu.unit));
34786be9f0dSKonstantin Belousov
34886be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_WBF);
349476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_WBFS)
350476358b3SKonstantin Belousov != 0));
351476358b3SKonstantin Belousov return (error);
35286be9f0dSKonstantin Belousov }
35386be9f0dSKonstantin Belousov
35406e6ca6dSKornel Duleba /*
35506e6ca6dSKornel Duleba * Some BIOSes protect memory region they reside in by using DMAR to
35606e6ca6dSKornel Duleba * prevent devices from doing any DMA transactions to that part of RAM.
35706e6ca6dSKornel Duleba * AMI refers to this as "DMA Control Guarantee".
35806e6ca6dSKornel Duleba * We need to disable this when address translation is enabled.
35906e6ca6dSKornel Duleba */
36006e6ca6dSKornel Duleba int
dmar_disable_protected_regions(struct dmar_unit * unit)36106e6ca6dSKornel Duleba dmar_disable_protected_regions(struct dmar_unit *unit)
36206e6ca6dSKornel Duleba {
36306e6ca6dSKornel Duleba uint32_t reg;
36406e6ca6dSKornel Duleba int error;
36506e6ca6dSKornel Duleba
36606e6ca6dSKornel Duleba DMAR_ASSERT_LOCKED(unit);
36706e6ca6dSKornel Duleba
36806e6ca6dSKornel Duleba /* Check if we support the feature. */
36906e6ca6dSKornel Duleba if ((unit->hw_cap & (DMAR_CAP_PLMR | DMAR_CAP_PHMR)) == 0)
37006e6ca6dSKornel Duleba return (0);
37106e6ca6dSKornel Duleba
37206e6ca6dSKornel Duleba reg = dmar_read4(unit, DMAR_PMEN_REG);
37306f659c3SKornel Duleba if ((reg & DMAR_PMEN_EPM) == 0)
37406f659c3SKornel Duleba return (0);
37506f659c3SKornel Duleba
37606e6ca6dSKornel Duleba reg &= ~DMAR_PMEN_EPM;
37706e6ca6dSKornel Duleba dmar_write4(unit, DMAR_PMEN_REG, reg);
37806e6ca6dSKornel Duleba DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_PMEN_REG) & DMAR_PMEN_PRS)
37906e6ca6dSKornel Duleba != 0));
38006e6ca6dSKornel Duleba
38106e6ca6dSKornel Duleba return (error);
38206e6ca6dSKornel Duleba }
38306e6ca6dSKornel Duleba
38486be9f0dSKonstantin Belousov int
dmar_enable_translation(struct dmar_unit * unit)38586be9f0dSKonstantin Belousov dmar_enable_translation(struct dmar_unit *unit)
38686be9f0dSKonstantin Belousov {
387476358b3SKonstantin Belousov int error;
38886be9f0dSKonstantin Belousov
38986be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
39086be9f0dSKonstantin Belousov unit->hw_gcmd |= DMAR_GCMD_TE;
39186be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
392476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES)
393476358b3SKonstantin Belousov != 0));
394476358b3SKonstantin Belousov return (error);
39586be9f0dSKonstantin Belousov }
39686be9f0dSKonstantin Belousov
39786be9f0dSKonstantin Belousov int
dmar_disable_translation(struct dmar_unit * unit)39886be9f0dSKonstantin Belousov dmar_disable_translation(struct dmar_unit *unit)
39986be9f0dSKonstantin Belousov {
400476358b3SKonstantin Belousov int error;
40186be9f0dSKonstantin Belousov
40286be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
40386be9f0dSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_TE;
40486be9f0dSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
405476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_TES)
406476358b3SKonstantin Belousov == 0));
407476358b3SKonstantin Belousov return (error);
40886be9f0dSKonstantin Belousov }
40986be9f0dSKonstantin Belousov
4100a110d5bSKonstantin Belousov int
dmar_load_irt_ptr(struct dmar_unit * unit)4110a110d5bSKonstantin Belousov dmar_load_irt_ptr(struct dmar_unit *unit)
4120a110d5bSKonstantin Belousov {
4130a110d5bSKonstantin Belousov uint64_t irta, s;
414476358b3SKonstantin Belousov int error;
4150a110d5bSKonstantin Belousov
4160a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
4170a110d5bSKonstantin Belousov irta = unit->irt_phys;
4180a110d5bSKonstantin Belousov if (DMAR_X2APIC(unit))
4190a110d5bSKonstantin Belousov irta |= DMAR_IRTA_EIME;
4200a110d5bSKonstantin Belousov s = fls(unit->irte_cnt) - 2;
4210a110d5bSKonstantin Belousov KASSERT(unit->irte_cnt >= 2 && s <= DMAR_IRTA_S_MASK &&
4220a110d5bSKonstantin Belousov powerof2(unit->irte_cnt),
4230a110d5bSKonstantin Belousov ("IRTA_REG_S overflow %x", unit->irte_cnt));
4240a110d5bSKonstantin Belousov irta |= s;
4250a110d5bSKonstantin Belousov dmar_write8(unit, DMAR_IRTA_REG, irta);
4260a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd | DMAR_GCMD_SIRTP);
427476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRTPS)
428476358b3SKonstantin Belousov != 0));
429476358b3SKonstantin Belousov return (error);
4300a110d5bSKonstantin Belousov }
4310a110d5bSKonstantin Belousov
4320a110d5bSKonstantin Belousov int
dmar_enable_ir(struct dmar_unit * unit)4330a110d5bSKonstantin Belousov dmar_enable_ir(struct dmar_unit *unit)
4340a110d5bSKonstantin Belousov {
435476358b3SKonstantin Belousov int error;
4360a110d5bSKonstantin Belousov
4370a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
4380a110d5bSKonstantin Belousov unit->hw_gcmd |= DMAR_GCMD_IRE;
4390a110d5bSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_CFI;
4400a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
441476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES)
442476358b3SKonstantin Belousov != 0));
443476358b3SKonstantin Belousov return (error);
4440a110d5bSKonstantin Belousov }
4450a110d5bSKonstantin Belousov
4460a110d5bSKonstantin Belousov int
dmar_disable_ir(struct dmar_unit * unit)4470a110d5bSKonstantin Belousov dmar_disable_ir(struct dmar_unit *unit)
4480a110d5bSKonstantin Belousov {
449476358b3SKonstantin Belousov int error;
4500a110d5bSKonstantin Belousov
4510a110d5bSKonstantin Belousov DMAR_ASSERT_LOCKED(unit);
4520a110d5bSKonstantin Belousov unit->hw_gcmd &= ~DMAR_GCMD_IRE;
4530a110d5bSKonstantin Belousov dmar_write4(unit, DMAR_GCMD_REG, unit->hw_gcmd);
454476358b3SKonstantin Belousov DMAR_WAIT_UNTIL(((dmar_read4(unit, DMAR_GSTS_REG) & DMAR_GSTS_IRES)
455476358b3SKonstantin Belousov == 0));
456476358b3SKonstantin Belousov return (error);
4570a110d5bSKonstantin Belousov }
4580a110d5bSKonstantin Belousov
45986be9f0dSKonstantin Belousov #define BARRIER_F \
46086be9f0dSKonstantin Belousov u_int f_done, f_inproc, f_wakeup; \
46186be9f0dSKonstantin Belousov \
46286be9f0dSKonstantin Belousov f_done = 1 << (barrier_id * 3); \
46386be9f0dSKonstantin Belousov f_inproc = 1 << (barrier_id * 3 + 1); \
46486be9f0dSKonstantin Belousov f_wakeup = 1 << (barrier_id * 3 + 2)
46586be9f0dSKonstantin Belousov
46686be9f0dSKonstantin Belousov bool
dmar_barrier_enter(struct dmar_unit * dmar,u_int barrier_id)46786be9f0dSKonstantin Belousov dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id)
46886be9f0dSKonstantin Belousov {
46986be9f0dSKonstantin Belousov BARRIER_F;
47086be9f0dSKonstantin Belousov
47186be9f0dSKonstantin Belousov DMAR_LOCK(dmar);
47286be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_done) != 0) {
47386be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
47486be9f0dSKonstantin Belousov return (false);
47586be9f0dSKonstantin Belousov }
47686be9f0dSKonstantin Belousov
47786be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_inproc) != 0) {
47886be9f0dSKonstantin Belousov while ((dmar->barrier_flags & f_inproc) != 0) {
47986be9f0dSKonstantin Belousov dmar->barrier_flags |= f_wakeup;
48059e37c8aSRuslan Bukin msleep(&dmar->barrier_flags, &dmar->iommu.lock, 0,
48186be9f0dSKonstantin Belousov "dmarb", 0);
48286be9f0dSKonstantin Belousov }
48386be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & f_done) != 0,
48459e37c8aSRuslan Bukin ("dmar%d barrier %d missing done", dmar->iommu.unit,
48559e37c8aSRuslan Bukin barrier_id));
48686be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
48786be9f0dSKonstantin Belousov return (false);
48886be9f0dSKonstantin Belousov }
48986be9f0dSKonstantin Belousov
49086be9f0dSKonstantin Belousov dmar->barrier_flags |= f_inproc;
49186be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
49286be9f0dSKonstantin Belousov return (true);
49386be9f0dSKonstantin Belousov }
49486be9f0dSKonstantin Belousov
49586be9f0dSKonstantin Belousov void
dmar_barrier_exit(struct dmar_unit * dmar,u_int barrier_id)49686be9f0dSKonstantin Belousov dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id)
49786be9f0dSKonstantin Belousov {
49886be9f0dSKonstantin Belousov BARRIER_F;
49986be9f0dSKonstantin Belousov
50086be9f0dSKonstantin Belousov DMAR_ASSERT_LOCKED(dmar);
50186be9f0dSKonstantin Belousov KASSERT((dmar->barrier_flags & (f_done | f_inproc)) == f_inproc,
50259e37c8aSRuslan Bukin ("dmar%d barrier %d missed entry", dmar->iommu.unit, barrier_id));
50386be9f0dSKonstantin Belousov dmar->barrier_flags |= f_done;
50486be9f0dSKonstantin Belousov if ((dmar->barrier_flags & f_wakeup) != 0)
50586be9f0dSKonstantin Belousov wakeup(&dmar->barrier_flags);
50686be9f0dSKonstantin Belousov dmar->barrier_flags &= ~(f_inproc | f_wakeup);
50786be9f0dSKonstantin Belousov DMAR_UNLOCK(dmar);
50886be9f0dSKonstantin Belousov }
50986be9f0dSKonstantin Belousov
510476358b3SKonstantin Belousov struct timespec dmar_hw_timeout = {
511476358b3SKonstantin Belousov .tv_sec = 0,
512476358b3SKonstantin Belousov .tv_nsec = 1000000
513476358b3SKonstantin Belousov };
514476358b3SKonstantin Belousov
515476358b3SKonstantin Belousov static const uint64_t d = 1000000000;
516476358b3SKonstantin Belousov
517476358b3SKonstantin Belousov void
dmar_update_timeout(uint64_t newval)518476358b3SKonstantin Belousov dmar_update_timeout(uint64_t newval)
519476358b3SKonstantin Belousov {
520476358b3SKonstantin Belousov
521476358b3SKonstantin Belousov /* XXXKIB not atomic */
522476358b3SKonstantin Belousov dmar_hw_timeout.tv_sec = newval / d;
523476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec = newval % d;
524476358b3SKonstantin Belousov }
525476358b3SKonstantin Belousov
526476358b3SKonstantin Belousov uint64_t
dmar_get_timeout(void)527476358b3SKonstantin Belousov dmar_get_timeout(void)
528476358b3SKonstantin Belousov {
529476358b3SKonstantin Belousov
530476358b3SKonstantin Belousov return ((uint64_t)dmar_hw_timeout.tv_sec * d +
531476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec);
532476358b3SKonstantin Belousov }
533476358b3SKonstantin Belousov
534476358b3SKonstantin Belousov static int
dmar_timeout_sysctl(SYSCTL_HANDLER_ARGS)535476358b3SKonstantin Belousov dmar_timeout_sysctl(SYSCTL_HANDLER_ARGS)
536476358b3SKonstantin Belousov {
537476358b3SKonstantin Belousov uint64_t val;
538476358b3SKonstantin Belousov int error;
539476358b3SKonstantin Belousov
540476358b3SKonstantin Belousov val = dmar_get_timeout();
541476358b3SKonstantin Belousov error = sysctl_handle_long(oidp, &val, 0, req);
542476358b3SKonstantin Belousov if (error != 0 || req->newptr == NULL)
543476358b3SKonstantin Belousov return (error);
544476358b3SKonstantin Belousov dmar_update_timeout(val);
545476358b3SKonstantin Belousov return (error);
546476358b3SKonstantin Belousov }
54786be9f0dSKonstantin Belousov
548357149f0SRuslan Bukin SYSCTL_PROC(_hw_iommu_dmar, OID_AUTO, timeout,
549476358b3SKonstantin Belousov CTLTYPE_U64 | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
550476358b3SKonstantin Belousov dmar_timeout_sysctl, "QU",
551476358b3SKonstantin Belousov "Timeout for command wait, in nanoseconds");
552