xref: /freebsd/sys/x86/iommu/intel_dmar.h (revision 24e38af60a637073b5164837959098acaa3d8daa)
186be9f0dSKonstantin Belousov /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni  *
40a110d5bSKonstantin Belousov  * Copyright (c) 2013-2015 The FreeBSD Foundation
586be9f0dSKonstantin Belousov  *
686be9f0dSKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
786be9f0dSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
886be9f0dSKonstantin Belousov  *
986be9f0dSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
1086be9f0dSKonstantin Belousov  * modification, are permitted provided that the following conditions
1186be9f0dSKonstantin Belousov  * are met:
1286be9f0dSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
1386be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
1486be9f0dSKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
1586be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
1686be9f0dSKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
1786be9f0dSKonstantin Belousov  *
1886be9f0dSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986be9f0dSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086be9f0dSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186be9f0dSKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286be9f0dSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386be9f0dSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486be9f0dSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586be9f0dSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686be9f0dSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786be9f0dSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886be9f0dSKonstantin Belousov  * SUCH DAMAGE.
2986be9f0dSKonstantin Belousov  */
3086be9f0dSKonstantin Belousov 
3186be9f0dSKonstantin Belousov #ifndef __X86_IOMMU_INTEL_DMAR_H
3286be9f0dSKonstantin Belousov #define	__X86_IOMMU_INTEL_DMAR_H
3386be9f0dSKonstantin Belousov 
341238a28dSRuslan Bukin #include <dev/iommu/iommu.h>
3586be9f0dSKonstantin Belousov 
3659e37c8aSRuslan Bukin struct dmar_unit;
3768eeb96aSKonstantin Belousov 
381abfd355SKonstantin Belousov /*
391abfd355SKonstantin Belousov  * Locking annotations:
4059e37c8aSRuslan Bukin  * (u) - Protected by iommu unit lock
411abfd355SKonstantin Belousov  * (d) - Protected by domain lock
421abfd355SKonstantin Belousov  * (c) - Immutable after initialization
431abfd355SKonstantin Belousov  */
441abfd355SKonstantin Belousov 
451abfd355SKonstantin Belousov /*
461abfd355SKonstantin Belousov  * The domain abstraction.  Most non-constant members of the domain
47ad969cb1SKonstantin Belousov  * are protected by owning dmar unit lock, not by the domain lock.
48ad969cb1SKonstantin Belousov  * Most important, the dmar lock protects the contexts list.
491abfd355SKonstantin Belousov  *
501abfd355SKonstantin Belousov  * The domain lock protects the address map for the domain, and list
511abfd355SKonstantin Belousov  * of unload entries delayed.
521abfd355SKonstantin Belousov  *
531abfd355SKonstantin Belousov  * Page tables pages and pages content is protected by the vm object
541abfd355SKonstantin Belousov  * lock pgtbl_obj, which contains the page tables pages.
551abfd355SKonstantin Belousov  */
561abfd355SKonstantin Belousov struct dmar_domain {
5759e37c8aSRuslan Bukin 	struct iommu_domain iodom;
581abfd355SKonstantin Belousov 	int domain;			/* (c) DID, written in context entry */
591abfd355SKonstantin Belousov 	int mgaw;			/* (c) Real max address width */
601abfd355SKonstantin Belousov 	int agaw;			/* (c) Adjusted guest address width */
611abfd355SKonstantin Belousov 	int pglvl;			/* (c) The pagelevel */
621abfd355SKonstantin Belousov 	int awlvl;			/* (c) The pagelevel as the bitmask,
631abfd355SKonstantin Belousov 					   to set in context entry */
641abfd355SKonstantin Belousov 	u_int ctx_cnt;			/* (u) Number of contexts owned */
651abfd355SKonstantin Belousov 	u_int refs;			/* (u) Refs, including ctx */
661abfd355SKonstantin Belousov 	struct dmar_unit *dmar;		/* (c) */
671abfd355SKonstantin Belousov 	LIST_ENTRY(dmar_domain) link;	/* (u) Member in the dmar list */
681abfd355SKonstantin Belousov 	LIST_HEAD(, dmar_ctx) contexts;	/* (u) */
691abfd355SKonstantin Belousov 	vm_object_t pgtbl_obj;		/* (c) Page table pages */
70e164cafcSKonstantin Belousov 	u_int batch_no;
7186be9f0dSKonstantin Belousov };
7286be9f0dSKonstantin Belousov 
731abfd355SKonstantin Belousov struct dmar_ctx {
7459e37c8aSRuslan Bukin 	struct iommu_ctx context;
751abfd355SKonstantin Belousov 	uint64_t last_fault_rec[2];	/* Last fault reported */
761abfd355SKonstantin Belousov 	LIST_ENTRY(dmar_ctx) link;	/* (u) Member in the domain list */
771abfd355SKonstantin Belousov 	u_int refs;			/* (u) References from tags */
781abfd355SKonstantin Belousov };
791abfd355SKonstantin Belousov 
801abfd355SKonstantin Belousov #define	DMAR_DOMAIN_PGLOCK(dom)		VM_OBJECT_WLOCK((dom)->pgtbl_obj)
811abfd355SKonstantin Belousov #define	DMAR_DOMAIN_PGTRYLOCK(dom)	VM_OBJECT_TRYWLOCK((dom)->pgtbl_obj)
821abfd355SKonstantin Belousov #define	DMAR_DOMAIN_PGUNLOCK(dom)	VM_OBJECT_WUNLOCK((dom)->pgtbl_obj)
831abfd355SKonstantin Belousov #define	DMAR_DOMAIN_ASSERT_PGLOCKED(dom) \
841abfd355SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED((dom)->pgtbl_obj)
8586be9f0dSKonstantin Belousov 
8659e37c8aSRuslan Bukin #define	DMAR_DOMAIN_LOCK(dom)	mtx_lock(&(dom)->iodom.lock)
8759e37c8aSRuslan Bukin #define	DMAR_DOMAIN_UNLOCK(dom)	mtx_unlock(&(dom)->iodom.lock)
8859e37c8aSRuslan Bukin #define	DMAR_DOMAIN_ASSERT_LOCKED(dom) mtx_assert(&(dom)->iodom.lock, MA_OWNED)
8986be9f0dSKonstantin Belousov 
9078b51754SRuslan Bukin #define	DMAR2IOMMU(dmar)	&((dmar)->iommu)
9178b51754SRuslan Bukin #define	IOMMU2DMAR(dmar)	\
9278b51754SRuslan Bukin 	__containerof((dmar), struct dmar_unit, iommu)
9378b51754SRuslan Bukin 
9478b51754SRuslan Bukin #define	DOM2IODOM(domain)	&((domain)->iodom)
9578b51754SRuslan Bukin #define	IODOM2DOM(domain)	\
9678b51754SRuslan Bukin 	__containerof((domain), struct dmar_domain, iodom)
9778b51754SRuslan Bukin 
9878b51754SRuslan Bukin #define	CTX2IOCTX(ctx)		&((ctx)->context)
9978b51754SRuslan Bukin #define	IOCTX2CTX(ctx)		\
10078b51754SRuslan Bukin 	__containerof((ctx), struct dmar_ctx, context)
10178b51754SRuslan Bukin 
10278b51754SRuslan Bukin #define	CTX2DOM(ctx)		IODOM2DOM((ctx)->context.domain)
10378b51754SRuslan Bukin #define	CTX2DMAR(ctx)		(CTX2DOM(ctx)->dmar)
10478b51754SRuslan Bukin #define	DOM2DMAR(domain)	((domain)->dmar)
10578b51754SRuslan Bukin 
10668eeb96aSKonstantin Belousov struct dmar_msi_data {
10768eeb96aSKonstantin Belousov 	int irq;
10868eeb96aSKonstantin Belousov 	int irq_rid;
10968eeb96aSKonstantin Belousov 	struct resource *irq_res;
11068eeb96aSKonstantin Belousov 	void *intr_handle;
11168eeb96aSKonstantin Belousov 	int (*handler)(void *);
11268eeb96aSKonstantin Belousov 	int msi_data_reg;
11368eeb96aSKonstantin Belousov 	int msi_addr_reg;
11468eeb96aSKonstantin Belousov 	int msi_uaddr_reg;
11568eeb96aSKonstantin Belousov 	void (*enable_intr)(struct dmar_unit *);
11668eeb96aSKonstantin Belousov 	void (*disable_intr)(struct dmar_unit *);
11768eeb96aSKonstantin Belousov 	const char *name;
11868eeb96aSKonstantin Belousov };
11968eeb96aSKonstantin Belousov 
12068eeb96aSKonstantin Belousov #define	DMAR_INTR_FAULT		0
12168eeb96aSKonstantin Belousov #define	DMAR_INTR_QI		1
12268eeb96aSKonstantin Belousov #define	DMAR_INTR_TOTAL		2
12368eeb96aSKonstantin Belousov 
12486be9f0dSKonstantin Belousov struct dmar_unit {
12559e37c8aSRuslan Bukin 	struct iommu_unit iommu;
12686be9f0dSKonstantin Belousov 	device_t dev;
12786be9f0dSKonstantin Belousov 	uint16_t segment;
12886be9f0dSKonstantin Belousov 	uint64_t base;
12986be9f0dSKonstantin Belousov 
13086be9f0dSKonstantin Belousov 	/* Resources */
13186be9f0dSKonstantin Belousov 	int reg_rid;
13286be9f0dSKonstantin Belousov 	struct resource *regs;
13368eeb96aSKonstantin Belousov 
13468eeb96aSKonstantin Belousov 	struct dmar_msi_data intrs[DMAR_INTR_TOTAL];
13586be9f0dSKonstantin Belousov 
13686be9f0dSKonstantin Belousov 	/* Hardware registers cache */
13786be9f0dSKonstantin Belousov 	uint32_t hw_ver;
13886be9f0dSKonstantin Belousov 	uint64_t hw_cap;
13986be9f0dSKonstantin Belousov 	uint64_t hw_ecap;
14086be9f0dSKonstantin Belousov 	uint32_t hw_gcmd;
14186be9f0dSKonstantin Belousov 
14286be9f0dSKonstantin Belousov 	/* Data for being a dmar */
1431abfd355SKonstantin Belousov 	LIST_HEAD(, dmar_domain) domains;
14486be9f0dSKonstantin Belousov 	struct unrhdr *domids;
14586be9f0dSKonstantin Belousov 	vm_object_t ctx_obj;
14686be9f0dSKonstantin Belousov 	u_int barrier_flags;
14786be9f0dSKonstantin Belousov 
14886be9f0dSKonstantin Belousov 	/* Fault handler data */
14986be9f0dSKonstantin Belousov 	struct mtx fault_lock;
15086be9f0dSKonstantin Belousov 	uint64_t *fault_log;
15186be9f0dSKonstantin Belousov 	int fault_log_head;
15286be9f0dSKonstantin Belousov 	int fault_log_tail;
15386be9f0dSKonstantin Belousov 	int fault_log_size;
15486be9f0dSKonstantin Belousov 	struct task fault_task;
15586be9f0dSKonstantin Belousov 	struct taskqueue *fault_taskqueue;
15686be9f0dSKonstantin Belousov 
15768eeb96aSKonstantin Belousov 	/* QI */
15868eeb96aSKonstantin Belousov 	int qi_enabled;
159f49fd63aSJohn Baldwin 	char *inv_queue;
16068eeb96aSKonstantin Belousov 	vm_size_t inv_queue_size;
16168eeb96aSKonstantin Belousov 	uint32_t inv_queue_avail;
16268eeb96aSKonstantin Belousov 	uint32_t inv_queue_tail;
16368eeb96aSKonstantin Belousov 	volatile uint32_t inv_waitd_seq_hw; /* hw writes there on wait
16468eeb96aSKonstantin Belousov 					       descr completion */
16568eeb96aSKonstantin Belousov 	uint64_t inv_waitd_seq_hw_phys;
16668eeb96aSKonstantin Belousov 	uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */
16768eeb96aSKonstantin Belousov 	u_int inv_waitd_gen;	/* seq number generation AKA seq overflows */
16868eeb96aSKonstantin Belousov 	u_int inv_seq_waiters;	/* count of waiters for seq */
16968eeb96aSKonstantin Belousov 	u_int inv_queue_full;	/* informational counter */
17068eeb96aSKonstantin Belousov 
1710a110d5bSKonstantin Belousov 	/* IR */
1720a110d5bSKonstantin Belousov 	int ir_enabled;
1730a110d5bSKonstantin Belousov 	vm_paddr_t irt_phys;
1740a110d5bSKonstantin Belousov 	dmar_irte_t *irt;
1750a110d5bSKonstantin Belousov 	u_int irte_cnt;
1760a110d5bSKonstantin Belousov 	vmem_t *irtids;
1770a110d5bSKonstantin Belousov 
17842736dc4SAlan Cox 	/*
17942736dc4SAlan Cox 	 * Delayed freeing of map entries queue processing:
18042736dc4SAlan Cox 	 *
18142736dc4SAlan Cox 	 * tlb_flush_head and tlb_flush_tail are used to implement a FIFO
18242736dc4SAlan Cox 	 * queue that supports concurrent dequeues and enqueues.  However,
18342736dc4SAlan Cox 	 * there can only be a single dequeuer (accessing tlb_flush_head) and
18442736dc4SAlan Cox 	 * a single enqueuer (accessing tlb_flush_tail) at a time.  Since the
18542736dc4SAlan Cox 	 * unit's qi_task is the only dequeuer, it can access tlb_flush_head
18642736dc4SAlan Cox 	 * without any locking.  In contrast, there may be multiple enqueuers,
18742736dc4SAlan Cox 	 * so the enqueuers acquire the iommu unit lock to serialize their
18842736dc4SAlan Cox 	 * accesses to tlb_flush_tail.
18942736dc4SAlan Cox 	 *
19042736dc4SAlan Cox 	 * In this FIFO queue implementation, the key to enabling concurrent
19142736dc4SAlan Cox 	 * dequeues and enqueues is that the dequeuer never needs to access
19242736dc4SAlan Cox 	 * tlb_flush_tail and the enqueuer never needs to access
19342736dc4SAlan Cox 	 * tlb_flush_head.  In particular, tlb_flush_head and tlb_flush_tail
19442736dc4SAlan Cox 	 * are never NULL, so neither a dequeuer nor an enqueuer ever needs to
19542736dc4SAlan Cox 	 * update both.  Instead, tlb_flush_head always points to a "zombie"
19642736dc4SAlan Cox 	 * struct, which previously held the last dequeued item.  Thus, the
19742736dc4SAlan Cox 	 * zombie's next field actually points to the struct holding the first
19842736dc4SAlan Cox 	 * item in the queue.  When an item is dequeued, the current zombie is
19942736dc4SAlan Cox 	 * finally freed, and the struct that held the just dequeued item
20042736dc4SAlan Cox 	 * becomes the new zombie.  When the queue is empty, tlb_flush_tail
20142736dc4SAlan Cox 	 * also points to the zombie.
20242736dc4SAlan Cox 	 */
20342736dc4SAlan Cox 	struct iommu_map_entry *tlb_flush_head;
20442736dc4SAlan Cox 	struct iommu_map_entry *tlb_flush_tail;
20568eeb96aSKonstantin Belousov 	struct task qi_task;
20668eeb96aSKonstantin Belousov 	struct taskqueue *qi_taskqueue;
20786be9f0dSKonstantin Belousov };
20886be9f0dSKonstantin Belousov 
20959e37c8aSRuslan Bukin #define	DMAR_LOCK(dmar)		mtx_lock(&(dmar)->iommu.lock)
21059e37c8aSRuslan Bukin #define	DMAR_UNLOCK(dmar)	mtx_unlock(&(dmar)->iommu.lock)
21159e37c8aSRuslan Bukin #define	DMAR_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->iommu.lock, MA_OWNED)
21286be9f0dSKonstantin Belousov 
21386be9f0dSKonstantin Belousov #define	DMAR_FAULT_LOCK(dmar)	mtx_lock_spin(&(dmar)->fault_lock)
21486be9f0dSKonstantin Belousov #define	DMAR_FAULT_UNLOCK(dmar)	mtx_unlock_spin(&(dmar)->fault_lock)
21586be9f0dSKonstantin Belousov #define	DMAR_FAULT_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->fault_lock, MA_OWNED)
21686be9f0dSKonstantin Belousov 
21786be9f0dSKonstantin Belousov #define	DMAR_IS_COHERENT(dmar)	(((dmar)->hw_ecap & DMAR_ECAP_C) != 0)
21868eeb96aSKonstantin Belousov #define	DMAR_HAS_QI(dmar)	(((dmar)->hw_ecap & DMAR_ECAP_QI) != 0)
2190a110d5bSKonstantin Belousov #define	DMAR_X2APIC(dmar) \
2200a110d5bSKonstantin Belousov 	(x2apic_mode && ((dmar)->hw_ecap & DMAR_ECAP_EIM) != 0)
22186be9f0dSKonstantin Belousov 
22286be9f0dSKonstantin Belousov /* Barrier ids */
22386be9f0dSKonstantin Belousov #define	DMAR_BARRIER_RMRR	0
22486be9f0dSKonstantin Belousov #define	DMAR_BARRIER_USEQ	1
22586be9f0dSKonstantin Belousov 
226f9feb091SKonstantin Belousov struct dmar_unit *dmar_find(device_t dev, bool verbose);
2270a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_hpet(device_t dev, uint16_t *rid);
2280a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_ioapic(u_int apic_id, uint16_t *rid);
22986be9f0dSKonstantin Belousov 
23086be9f0dSKonstantin Belousov u_int dmar_nd2mask(u_int nd);
23186be9f0dSKonstantin Belousov bool dmar_pglvl_supported(struct dmar_unit *unit, int pglvl);
2321abfd355SKonstantin Belousov int domain_set_agaw(struct dmar_domain *domain, int mgaw);
23359e37c8aSRuslan Bukin int dmar_maxaddr2mgaw(struct dmar_unit *unit, iommu_gaddr_t maxaddr,
23486be9f0dSKonstantin Belousov     bool allow_less);
23586be9f0dSKonstantin Belousov vm_pindex_t pglvl_max_pages(int pglvl);
2361abfd355SKonstantin Belousov int domain_is_sp_lvl(struct dmar_domain *domain, int lvl);
23759e37c8aSRuslan Bukin iommu_gaddr_t pglvl_page_size(int total_pglvl, int lvl);
23859e37c8aSRuslan Bukin iommu_gaddr_t domain_page_size(struct dmar_domain *domain, int lvl);
23959e37c8aSRuslan Bukin int calc_am(struct dmar_unit *unit, iommu_gaddr_t base, iommu_gaddr_t size,
24059e37c8aSRuslan Bukin     iommu_gaddr_t *isizep);
24186be9f0dSKonstantin Belousov struct vm_page *dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags);
24286be9f0dSKonstantin Belousov void dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags);
24386be9f0dSKonstantin Belousov void *dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags,
24486be9f0dSKonstantin Belousov     struct sf_buf **sf);
2456b7c46afSKonstantin Belousov void dmar_unmap_pgtbl(struct sf_buf *sf);
24686be9f0dSKonstantin Belousov int dmar_load_root_entry_ptr(struct dmar_unit *unit);
24786be9f0dSKonstantin Belousov int dmar_inv_ctx_glob(struct dmar_unit *unit);
24886be9f0dSKonstantin Belousov int dmar_inv_iotlb_glob(struct dmar_unit *unit);
24986be9f0dSKonstantin Belousov int dmar_flush_write_bufs(struct dmar_unit *unit);
2506b7c46afSKonstantin Belousov void dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst);
2516b7c46afSKonstantin Belousov void dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst);
2526b7c46afSKonstantin Belousov void dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst);
25306e6ca6dSKornel Duleba int dmar_disable_protected_regions(struct dmar_unit *unit);
25486be9f0dSKonstantin Belousov int dmar_enable_translation(struct dmar_unit *unit);
25586be9f0dSKonstantin Belousov int dmar_disable_translation(struct dmar_unit *unit);
2560a110d5bSKonstantin Belousov int dmar_load_irt_ptr(struct dmar_unit *unit);
2570a110d5bSKonstantin Belousov int dmar_enable_ir(struct dmar_unit *unit);
2580a110d5bSKonstantin Belousov int dmar_disable_ir(struct dmar_unit *unit);
25986be9f0dSKonstantin Belousov bool dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id);
26086be9f0dSKonstantin Belousov void dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id);
261476358b3SKonstantin Belousov uint64_t dmar_get_timeout(void);
262476358b3SKonstantin Belousov void dmar_update_timeout(uint64_t newval);
26386be9f0dSKonstantin Belousov 
26468eeb96aSKonstantin Belousov int dmar_fault_intr(void *arg);
26568eeb96aSKonstantin Belousov void dmar_enable_fault_intr(struct dmar_unit *unit);
26668eeb96aSKonstantin Belousov void dmar_disable_fault_intr(struct dmar_unit *unit);
26786be9f0dSKonstantin Belousov int dmar_init_fault_log(struct dmar_unit *unit);
26886be9f0dSKonstantin Belousov void dmar_fini_fault_log(struct dmar_unit *unit);
26986be9f0dSKonstantin Belousov 
27068eeb96aSKonstantin Belousov int dmar_qi_intr(void *arg);
27168eeb96aSKonstantin Belousov void dmar_enable_qi_intr(struct dmar_unit *unit);
27268eeb96aSKonstantin Belousov void dmar_disable_qi_intr(struct dmar_unit *unit);
27368eeb96aSKonstantin Belousov int dmar_init_qi(struct dmar_unit *unit);
27468eeb96aSKonstantin Belousov void dmar_fini_qi(struct dmar_unit *unit);
27542736dc4SAlan Cox void dmar_qi_invalidate_locked(struct dmar_domain *domain,
27642736dc4SAlan Cox     struct iommu_map_entry *entry, bool emit_wait);
277c2515634SAlan Cox void dmar_qi_invalidate_sync(struct dmar_domain *domain, iommu_gaddr_t start,
278c2515634SAlan Cox     iommu_gaddr_t size, bool cansleep);
27968eeb96aSKonstantin Belousov void dmar_qi_invalidate_ctx_glob_locked(struct dmar_unit *unit);
28068eeb96aSKonstantin Belousov void dmar_qi_invalidate_iotlb_glob_locked(struct dmar_unit *unit);
2810a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec_glob(struct dmar_unit *unit);
2820a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec(struct dmar_unit *unit, u_int start, u_int cnt);
28368eeb96aSKonstantin Belousov 
2841abfd355SKonstantin Belousov vm_object_t domain_get_idmap_pgtbl(struct dmar_domain *domain,
28559e37c8aSRuslan Bukin     iommu_gaddr_t maxaddr);
28686be9f0dSKonstantin Belousov void put_idmap_pgtbl(vm_object_t obj);
28759e37c8aSRuslan Bukin void domain_flush_iotlb_sync(struct dmar_domain *domain, iommu_gaddr_t base,
28859e37c8aSRuslan Bukin     iommu_gaddr_t size);
2891abfd355SKonstantin Belousov int domain_alloc_pgtbl(struct dmar_domain *domain);
2901abfd355SKonstantin Belousov void domain_free_pgtbl(struct dmar_domain *domain);
29116696f60SRuslan Bukin extern const struct iommu_domain_map_ops dmar_domain_map_ops;
29286be9f0dSKonstantin Belousov 
293f9feb091SKonstantin Belousov int dmar_dev_depth(device_t child);
294f9feb091SKonstantin Belousov void dmar_dev_path(device_t child, int *busno, void *path1, int depth);
295f9feb091SKonstantin Belousov 
2961abfd355SKonstantin Belousov struct dmar_ctx *dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev,
29767499354SRyan Stone     uint16_t rid, bool id_mapped, bool rmrr_init);
298f9feb091SKonstantin Belousov struct dmar_ctx *dmar_get_ctx_for_devpath(struct dmar_unit *dmar, uint16_t rid,
299f9feb091SKonstantin Belousov     int dev_domain, int dev_busno, const void *dev_path, int dev_path_len,
300f9feb091SKonstantin Belousov     bool id_mapped, bool rmrr_init);
3011abfd355SKonstantin Belousov int dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx);
30286be9f0dSKonstantin Belousov void dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx);
30386be9f0dSKonstantin Belousov void dmar_free_ctx(struct dmar_ctx *ctx);
30467499354SRyan Stone struct dmar_ctx *dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid);
30559e37c8aSRuslan Bukin void dmar_domain_free_entry(struct iommu_map_entry *entry, bool free);
30686be9f0dSKonstantin Belousov 
307f9feb091SKonstantin Belousov void dmar_dev_parse_rmrr(struct dmar_domain *domain, int dev_domain,
308f9feb091SKonstantin Belousov     int dev_busno, const void *dev_path, int dev_path_len,
30959e37c8aSRuslan Bukin     struct iommu_map_entries_tailq *rmrr_entries);
31059e37c8aSRuslan Bukin int dmar_instantiate_rmrr_ctxs(struct iommu_unit *dmar);
31186be9f0dSKonstantin Belousov 
31286be9f0dSKonstantin Belousov void dmar_quirks_post_ident(struct dmar_unit *dmar);
31359e37c8aSRuslan Bukin void dmar_quirks_pre_use(struct iommu_unit *dmar);
31486be9f0dSKonstantin Belousov 
3150a110d5bSKonstantin Belousov int dmar_init_irt(struct dmar_unit *unit);
3160a110d5bSKonstantin Belousov void dmar_fini_irt(struct dmar_unit *unit);
3170a110d5bSKonstantin Belousov 
31859e37c8aSRuslan Bukin extern iommu_haddr_t dmar_high;
31986be9f0dSKonstantin Belousov extern int haw;
32086be9f0dSKonstantin Belousov extern int dmar_tbl_pagecnt;
321e164cafcSKonstantin Belousov extern int dmar_batch_coalesce;
322*24e38af6SKonstantin Belousov extern int dmar_rmrr_enable;
32386be9f0dSKonstantin Belousov 
32486be9f0dSKonstantin Belousov static inline uint32_t
32586be9f0dSKonstantin Belousov dmar_read4(const struct dmar_unit *unit, int reg)
32686be9f0dSKonstantin Belousov {
32786be9f0dSKonstantin Belousov 
32886be9f0dSKonstantin Belousov 	return (bus_read_4(unit->regs, reg));
32986be9f0dSKonstantin Belousov }
33086be9f0dSKonstantin Belousov 
33186be9f0dSKonstantin Belousov static inline uint64_t
33286be9f0dSKonstantin Belousov dmar_read8(const struct dmar_unit *unit, int reg)
33386be9f0dSKonstantin Belousov {
33486be9f0dSKonstantin Belousov #ifdef __i386__
33586be9f0dSKonstantin Belousov 	uint32_t high, low;
33686be9f0dSKonstantin Belousov 
33786be9f0dSKonstantin Belousov 	low = bus_read_4(unit->regs, reg);
33886be9f0dSKonstantin Belousov 	high = bus_read_4(unit->regs, reg + 4);
33986be9f0dSKonstantin Belousov 	return (low | ((uint64_t)high << 32));
34086be9f0dSKonstantin Belousov #else
34186be9f0dSKonstantin Belousov 	return (bus_read_8(unit->regs, reg));
34286be9f0dSKonstantin Belousov #endif
34386be9f0dSKonstantin Belousov }
34486be9f0dSKonstantin Belousov 
34586be9f0dSKonstantin Belousov static inline void
34686be9f0dSKonstantin Belousov dmar_write4(const struct dmar_unit *unit, int reg, uint32_t val)
34786be9f0dSKonstantin Belousov {
34886be9f0dSKonstantin Belousov 
34986be9f0dSKonstantin Belousov 	KASSERT(reg != DMAR_GCMD_REG || (val & DMAR_GCMD_TE) ==
35086be9f0dSKonstantin Belousov 	    (unit->hw_gcmd & DMAR_GCMD_TE),
35159e37c8aSRuslan Bukin 	    ("dmar%d clearing TE 0x%08x 0x%08x", unit->iommu.unit,
35286be9f0dSKonstantin Belousov 	    unit->hw_gcmd, val));
35386be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg, val);
35486be9f0dSKonstantin Belousov }
35586be9f0dSKonstantin Belousov 
35686be9f0dSKonstantin Belousov static inline void
35786be9f0dSKonstantin Belousov dmar_write8(const struct dmar_unit *unit, int reg, uint64_t val)
35886be9f0dSKonstantin Belousov {
35986be9f0dSKonstantin Belousov 
36086be9f0dSKonstantin Belousov 	KASSERT(reg != DMAR_GCMD_REG, ("8byte GCMD write"));
36186be9f0dSKonstantin Belousov #ifdef __i386__
36286be9f0dSKonstantin Belousov 	uint32_t high, low;
36386be9f0dSKonstantin Belousov 
36486be9f0dSKonstantin Belousov 	low = val;
36586be9f0dSKonstantin Belousov 	high = val >> 32;
36686be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg, low);
36786be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg + 4, high);
36886be9f0dSKonstantin Belousov #else
36986be9f0dSKonstantin Belousov 	bus_write_8(unit->regs, reg, val);
37086be9f0dSKonstantin Belousov #endif
37186be9f0dSKonstantin Belousov }
37286be9f0dSKonstantin Belousov 
37386be9f0dSKonstantin Belousov /*
37486be9f0dSKonstantin Belousov  * dmar_pte_store and dmar_pte_clear ensure that on i386, 32bit writes
37586be9f0dSKonstantin Belousov  * are issued in the correct order.  For store, the lower word,
37686be9f0dSKonstantin Belousov  * containing the P or R and W bits, is set only after the high word
37786be9f0dSKonstantin Belousov  * is written.  For clear, the P bit is cleared first, then the high
37886be9f0dSKonstantin Belousov  * word is cleared.
3790a110d5bSKonstantin Belousov  *
3800a110d5bSKonstantin Belousov  * dmar_pte_update updates the pte.  For amd64, the update is atomic.
3810a110d5bSKonstantin Belousov  * For i386, it first disables the entry by clearing the word
3820a110d5bSKonstantin Belousov  * containing the P bit, and then defer to dmar_pte_store.  The locked
3830a110d5bSKonstantin Belousov  * cmpxchg8b is probably available on any machine having DMAR support,
3840a110d5bSKonstantin Belousov  * but interrupt translation table may be mapped uncached.
38586be9f0dSKonstantin Belousov  */
38686be9f0dSKonstantin Belousov static inline void
3870a110d5bSKonstantin Belousov dmar_pte_store1(volatile uint64_t *dst, uint64_t val)
38886be9f0dSKonstantin Belousov {
38986be9f0dSKonstantin Belousov #ifdef __i386__
39086be9f0dSKonstantin Belousov 	volatile uint32_t *p;
39186be9f0dSKonstantin Belousov 	uint32_t hi, lo;
39286be9f0dSKonstantin Belousov 
39386be9f0dSKonstantin Belousov 	hi = val >> 32;
39486be9f0dSKonstantin Belousov 	lo = val;
39586be9f0dSKonstantin Belousov 	p = (volatile uint32_t *)dst;
39686be9f0dSKonstantin Belousov 	*(p + 1) = hi;
39786be9f0dSKonstantin Belousov 	*p = lo;
39886be9f0dSKonstantin Belousov #else
39986be9f0dSKonstantin Belousov 	*dst = val;
40086be9f0dSKonstantin Belousov #endif
40186be9f0dSKonstantin Belousov }
40286be9f0dSKonstantin Belousov 
40386be9f0dSKonstantin Belousov static inline void
4040a110d5bSKonstantin Belousov dmar_pte_store(volatile uint64_t *dst, uint64_t val)
4050a110d5bSKonstantin Belousov {
4060a110d5bSKonstantin Belousov 
4070a110d5bSKonstantin Belousov 	KASSERT(*dst == 0, ("used pte %p oldval %jx newval %jx",
4080a110d5bSKonstantin Belousov 	    dst, (uintmax_t)*dst, (uintmax_t)val));
4090a110d5bSKonstantin Belousov 	dmar_pte_store1(dst, val);
4100a110d5bSKonstantin Belousov }
4110a110d5bSKonstantin Belousov 
4120a110d5bSKonstantin Belousov static inline void
4130a110d5bSKonstantin Belousov dmar_pte_update(volatile uint64_t *dst, uint64_t val)
4140a110d5bSKonstantin Belousov {
4150a110d5bSKonstantin Belousov 
4160a110d5bSKonstantin Belousov #ifdef __i386__
4170a110d5bSKonstantin Belousov 	volatile uint32_t *p;
4180a110d5bSKonstantin Belousov 
4190a110d5bSKonstantin Belousov 	p = (volatile uint32_t *)dst;
4200a110d5bSKonstantin Belousov 	*p = 0;
4210a110d5bSKonstantin Belousov #endif
4220a110d5bSKonstantin Belousov 	dmar_pte_store1(dst, val);
4230a110d5bSKonstantin Belousov }
4240a110d5bSKonstantin Belousov 
4250a110d5bSKonstantin Belousov static inline void
42686be9f0dSKonstantin Belousov dmar_pte_clear(volatile uint64_t *dst)
42786be9f0dSKonstantin Belousov {
42886be9f0dSKonstantin Belousov #ifdef __i386__
42986be9f0dSKonstantin Belousov 	volatile uint32_t *p;
43086be9f0dSKonstantin Belousov 
43186be9f0dSKonstantin Belousov 	p = (volatile uint32_t *)dst;
43286be9f0dSKonstantin Belousov 	*p = 0;
43386be9f0dSKonstantin Belousov 	*(p + 1) = 0;
43486be9f0dSKonstantin Belousov #else
43586be9f0dSKonstantin Belousov 	*dst = 0;
43686be9f0dSKonstantin Belousov #endif
43786be9f0dSKonstantin Belousov }
43886be9f0dSKonstantin Belousov 
439476358b3SKonstantin Belousov extern struct timespec dmar_hw_timeout;
440476358b3SKonstantin Belousov 
441476358b3SKonstantin Belousov #define	DMAR_WAIT_UNTIL(cond)					\
442476358b3SKonstantin Belousov {								\
443476358b3SKonstantin Belousov 	struct timespec last, curr;				\
444476358b3SKonstantin Belousov 	bool forever;						\
445476358b3SKonstantin Belousov 								\
446476358b3SKonstantin Belousov 	if (dmar_hw_timeout.tv_sec == 0 &&			\
447476358b3SKonstantin Belousov 	    dmar_hw_timeout.tv_nsec == 0) {			\
448476358b3SKonstantin Belousov 		forever = true;					\
449476358b3SKonstantin Belousov 	} else {						\
450476358b3SKonstantin Belousov 		forever = false;				\
451476358b3SKonstantin Belousov 		nanouptime(&curr);				\
4526040822cSAlan Somers 		timespecadd(&curr, &dmar_hw_timeout, &last);	\
453476358b3SKonstantin Belousov 	}							\
454476358b3SKonstantin Belousov 	for (;;) {						\
455476358b3SKonstantin Belousov 		if (cond) {					\
456476358b3SKonstantin Belousov 			error = 0;				\
457476358b3SKonstantin Belousov 			break;					\
458476358b3SKonstantin Belousov 		}						\
459476358b3SKonstantin Belousov 		nanouptime(&curr);				\
460476358b3SKonstantin Belousov 		if (!forever && timespeccmp(&last, &curr, <)) {	\
461476358b3SKonstantin Belousov 			error = ETIMEDOUT;			\
462476358b3SKonstantin Belousov 			break;					\
463476358b3SKonstantin Belousov 		}						\
464476358b3SKonstantin Belousov 		cpu_spinwait();					\
465476358b3SKonstantin Belousov 	}							\
466476358b3SKonstantin Belousov }
467476358b3SKonstantin Belousov 
46886be9f0dSKonstantin Belousov #ifdef INVARIANTS
46986be9f0dSKonstantin Belousov #define	TD_PREP_PINNED_ASSERT						\
47086be9f0dSKonstantin Belousov 	int old_td_pinned;						\
47186be9f0dSKonstantin Belousov 	old_td_pinned = curthread->td_pinned
47286be9f0dSKonstantin Belousov #define	TD_PINNED_ASSERT						\
47386be9f0dSKonstantin Belousov 	KASSERT(curthread->td_pinned == old_td_pinned,			\
47486be9f0dSKonstantin Belousov 	    ("pin count leak: %d %d %s:%d", curthread->td_pinned,	\
47586be9f0dSKonstantin Belousov 	    old_td_pinned, __FILE__, __LINE__))
47686be9f0dSKonstantin Belousov #else
47786be9f0dSKonstantin Belousov #define	TD_PREP_PINNED_ASSERT
47886be9f0dSKonstantin Belousov #define	TD_PINNED_ASSERT
47986be9f0dSKonstantin Belousov #endif
48086be9f0dSKonstantin Belousov 
48186be9f0dSKonstantin Belousov #endif
482