xref: /freebsd/sys/x86/iommu/intel_dmar.h (revision ad969cb1cdb00a6f5cbd4f8366cb061a2331c589)
186be9f0dSKonstantin Belousov /*-
20a110d5bSKonstantin Belousov  * Copyright (c) 2013-2015 The FreeBSD Foundation
386be9f0dSKonstantin Belousov  * All rights reserved.
486be9f0dSKonstantin Belousov  *
586be9f0dSKonstantin Belousov  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
686be9f0dSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
786be9f0dSKonstantin Belousov  *
886be9f0dSKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
986be9f0dSKonstantin Belousov  * modification, are permitted provided that the following conditions
1086be9f0dSKonstantin Belousov  * are met:
1186be9f0dSKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
1286be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
1386be9f0dSKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
1486be9f0dSKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
1586be9f0dSKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
1686be9f0dSKonstantin Belousov  *
1786be9f0dSKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1886be9f0dSKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1986be9f0dSKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2086be9f0dSKonstantin Belousov  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2186be9f0dSKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2286be9f0dSKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2386be9f0dSKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2486be9f0dSKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2586be9f0dSKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2686be9f0dSKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2786be9f0dSKonstantin Belousov  * SUCH DAMAGE.
2886be9f0dSKonstantin Belousov  *
2986be9f0dSKonstantin Belousov  * $FreeBSD$
3086be9f0dSKonstantin Belousov  */
3186be9f0dSKonstantin Belousov 
3286be9f0dSKonstantin Belousov #ifndef __X86_IOMMU_INTEL_DMAR_H
3386be9f0dSKonstantin Belousov #define	__X86_IOMMU_INTEL_DMAR_H
3486be9f0dSKonstantin Belousov 
3586be9f0dSKonstantin Belousov /* Host or physical memory address, after translation. */
3686be9f0dSKonstantin Belousov typedef uint64_t dmar_haddr_t;
3786be9f0dSKonstantin Belousov /* Guest or bus address, before translation. */
3886be9f0dSKonstantin Belousov typedef uint64_t dmar_gaddr_t;
3986be9f0dSKonstantin Belousov 
4068eeb96aSKonstantin Belousov struct dmar_qi_genseq {
4168eeb96aSKonstantin Belousov 	u_int gen;
4268eeb96aSKonstantin Belousov 	uint32_t seq;
4368eeb96aSKonstantin Belousov };
4468eeb96aSKonstantin Belousov 
4586be9f0dSKonstantin Belousov struct dmar_map_entry {
4686be9f0dSKonstantin Belousov 	dmar_gaddr_t start;
4786be9f0dSKonstantin Belousov 	dmar_gaddr_t end;
4886be9f0dSKonstantin Belousov 	dmar_gaddr_t free_after;	/* Free space after the entry */
4986be9f0dSKonstantin Belousov 	dmar_gaddr_t free_down;		/* Max free space below the
5086be9f0dSKonstantin Belousov 					   current R/B tree node */
5186be9f0dSKonstantin Belousov 	u_int flags;
5286be9f0dSKonstantin Belousov 	TAILQ_ENTRY(dmar_map_entry) dmamap_link; /* Link for dmamap entries */
531abfd355SKonstantin Belousov 	RB_ENTRY(dmar_map_entry) rb_entry;	 /* Links for domain entries */
5486be9f0dSKonstantin Belousov 	TAILQ_ENTRY(dmar_map_entry) unroll_link; /* Link for unroll after
5586be9f0dSKonstantin Belousov 						    dmamap_load failure */
561abfd355SKonstantin Belousov 	struct dmar_domain *domain;
5768eeb96aSKonstantin Belousov 	struct dmar_qi_genseq gseq;
5886be9f0dSKonstantin Belousov };
5986be9f0dSKonstantin Belousov 
6086be9f0dSKonstantin Belousov RB_HEAD(dmar_gas_entries_tree, dmar_map_entry);
6186be9f0dSKonstantin Belousov RB_PROTOTYPE(dmar_gas_entries_tree, dmar_map_entry, rb_entry,
6286be9f0dSKonstantin Belousov     dmar_gas_cmp_entries);
6386be9f0dSKonstantin Belousov 
6486be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_PLACE	0x0001	/* Fake entry */
6586be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_RMRR	0x0002	/* Permanent, not linked by
6686be9f0dSKonstantin Belousov 					   dmamap_link */
6786be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_MAP	0x0004	/* Busdma created, linked by
6886be9f0dSKonstantin Belousov 					   dmamap_link */
6986be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_UNMAPPED	0x0010	/* No backing pages */
7068eeb96aSKonstantin Belousov #define	DMAR_MAP_ENTRY_QI_NF	0x0020	/* qi task, do not free entry */
7186be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_READ	0x1000	/* Read permitted */
7286be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_WRITE	0x2000	/* Write permitted */
7386be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_SNOOP	0x4000	/* Snoop */
7486be9f0dSKonstantin Belousov #define	DMAR_MAP_ENTRY_TM	0x8000	/* Transient */
7586be9f0dSKonstantin Belousov 
761abfd355SKonstantin Belousov /*
771abfd355SKonstantin Belousov  * Locking annotations:
781abfd355SKonstantin Belousov  * (u) - Protected by dmar unit lock
791abfd355SKonstantin Belousov  * (d) - Protected by domain lock
801abfd355SKonstantin Belousov  * (c) - Immutable after initialization
811abfd355SKonstantin Belousov  */
821abfd355SKonstantin Belousov 
831abfd355SKonstantin Belousov /*
841abfd355SKonstantin Belousov  * The domain abstraction.  Most non-constant members of the domain
85*ad969cb1SKonstantin Belousov  * are protected by owning dmar unit lock, not by the domain lock.
86*ad969cb1SKonstantin Belousov  * Most important, the dmar lock protects the contexts list.
871abfd355SKonstantin Belousov  *
881abfd355SKonstantin Belousov  * The domain lock protects the address map for the domain, and list
891abfd355SKonstantin Belousov  * of unload entries delayed.
901abfd355SKonstantin Belousov  *
911abfd355SKonstantin Belousov  * Page tables pages and pages content is protected by the vm object
921abfd355SKonstantin Belousov  * lock pgtbl_obj, which contains the page tables pages.
931abfd355SKonstantin Belousov  */
941abfd355SKonstantin Belousov struct dmar_domain {
951abfd355SKonstantin Belousov 	int domain;			/* (c) DID, written in context entry */
961abfd355SKonstantin Belousov 	int mgaw;			/* (c) Real max address width */
971abfd355SKonstantin Belousov 	int agaw;			/* (c) Adjusted guest address width */
981abfd355SKonstantin Belousov 	int pglvl;			/* (c) The pagelevel */
991abfd355SKonstantin Belousov 	int awlvl;			/* (c) The pagelevel as the bitmask,
1001abfd355SKonstantin Belousov 					   to set in context entry */
1011abfd355SKonstantin Belousov 	dmar_gaddr_t end;		/* (c) Highest address + 1 in
1021abfd355SKonstantin Belousov 					   the guest AS */
1031abfd355SKonstantin Belousov 	u_int ctx_cnt;			/* (u) Number of contexts owned */
1041abfd355SKonstantin Belousov 	u_int refs;			/* (u) Refs, including ctx */
1051abfd355SKonstantin Belousov 	struct dmar_unit *dmar;		/* (c) */
1061abfd355SKonstantin Belousov 	struct mtx lock;		/* (c) */
1071abfd355SKonstantin Belousov 	LIST_ENTRY(dmar_domain) link;	/* (u) Member in the dmar list */
1081abfd355SKonstantin Belousov 	LIST_HEAD(, dmar_ctx) contexts;	/* (u) */
1091abfd355SKonstantin Belousov 	vm_object_t pgtbl_obj;		/* (c) Page table pages */
1101abfd355SKonstantin Belousov 	u_int flags;			/* (u) */
1111abfd355SKonstantin Belousov 	u_int entries_cnt;		/* (d) */
1121abfd355SKonstantin Belousov 	struct dmar_gas_entries_tree rb_root; /* (d) */
1131abfd355SKonstantin Belousov 	struct dmar_map_entries_tailq unload_entries; /* (d) Entries to
1141abfd355SKonstantin Belousov 							 unload */
1151abfd355SKonstantin Belousov 	struct dmar_map_entry *first_place, *last_place; /* (d) */
1161abfd355SKonstantin Belousov 	struct task unload_task;	/* (c) */
117e164cafcSKonstantin Belousov 	u_int batch_no;
11886be9f0dSKonstantin Belousov };
11986be9f0dSKonstantin Belousov 
1201abfd355SKonstantin Belousov struct dmar_ctx {
1211abfd355SKonstantin Belousov 	struct bus_dma_tag_dmar ctx_tag; /* (c) Root tag */
1221abfd355SKonstantin Belousov 	uint16_t rid;			/* (c) pci RID */
1231abfd355SKonstantin Belousov 	uint64_t last_fault_rec[2];	/* Last fault reported */
1241abfd355SKonstantin Belousov 	struct dmar_domain *domain;	/* (c) */
1251abfd355SKonstantin Belousov 	LIST_ENTRY(dmar_ctx) link;	/* (u) Member in the domain list */
1261abfd355SKonstantin Belousov 	u_int refs;			/* (u) References from tags */
1271abfd355SKonstantin Belousov 	u_int flags;			/* (u) */
1281abfd355SKonstantin Belousov 	u_long loads;			/* atomic updates, for stat only */
1291abfd355SKonstantin Belousov 	u_long unloads;			/* same */
1301abfd355SKonstantin Belousov };
1311abfd355SKonstantin Belousov 
1321abfd355SKonstantin Belousov #define	DMAR_DOMAIN_GAS_INITED		0x0001
1331abfd355SKonstantin Belousov #define	DMAR_DOMAIN_PGTBL_INITED	0x0002
1341abfd355SKonstantin Belousov #define	DMAR_DOMAIN_IDMAP		0x0010	/* Domain uses identity
1351abfd355SKonstantin Belousov 						   page table */
1361abfd355SKonstantin Belousov #define	DMAR_DOMAIN_RMRR		0x0020	/* Domain contains RMRR entry,
1371abfd355SKonstantin Belousov 						   cannot be turned off */
1381abfd355SKonstantin Belousov 
13986be9f0dSKonstantin Belousov /* struct dmar_ctx flags */
14086be9f0dSKonstantin Belousov #define	DMAR_CTX_FAULTED	0x0001	/* Fault was reported,
14186be9f0dSKonstantin Belousov 					   last_fault_rec is valid */
1421abfd355SKonstantin Belousov #define	DMAR_CTX_DISABLED	0x0002	/* Device is disabled, the
14386be9f0dSKonstantin Belousov 					   ephemeral reference is kept
14486be9f0dSKonstantin Belousov 					   to prevent context destruction */
14586be9f0dSKonstantin Belousov 
1461abfd355SKonstantin Belousov #define	DMAR_DOMAIN_PGLOCK(dom)		VM_OBJECT_WLOCK((dom)->pgtbl_obj)
1471abfd355SKonstantin Belousov #define	DMAR_DOMAIN_PGTRYLOCK(dom)	VM_OBJECT_TRYWLOCK((dom)->pgtbl_obj)
1481abfd355SKonstantin Belousov #define	DMAR_DOMAIN_PGUNLOCK(dom)	VM_OBJECT_WUNLOCK((dom)->pgtbl_obj)
1491abfd355SKonstantin Belousov #define	DMAR_DOMAIN_ASSERT_PGLOCKED(dom) \
1501abfd355SKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED((dom)->pgtbl_obj)
15186be9f0dSKonstantin Belousov 
1521abfd355SKonstantin Belousov #define	DMAR_DOMAIN_LOCK(dom)	mtx_lock(&(dom)->lock)
1531abfd355SKonstantin Belousov #define	DMAR_DOMAIN_UNLOCK(dom)	mtx_unlock(&(dom)->lock)
1541abfd355SKonstantin Belousov #define	DMAR_DOMAIN_ASSERT_LOCKED(dom) mtx_assert(&(dom)->lock, MA_OWNED)
15586be9f0dSKonstantin Belousov 
15668eeb96aSKonstantin Belousov struct dmar_msi_data {
15768eeb96aSKonstantin Belousov 	int irq;
15868eeb96aSKonstantin Belousov 	int irq_rid;
15968eeb96aSKonstantin Belousov 	struct resource *irq_res;
16068eeb96aSKonstantin Belousov 	void *intr_handle;
16168eeb96aSKonstantin Belousov 	int (*handler)(void *);
16268eeb96aSKonstantin Belousov 	int msi_data_reg;
16368eeb96aSKonstantin Belousov 	int msi_addr_reg;
16468eeb96aSKonstantin Belousov 	int msi_uaddr_reg;
16568eeb96aSKonstantin Belousov 	void (*enable_intr)(struct dmar_unit *);
16668eeb96aSKonstantin Belousov 	void (*disable_intr)(struct dmar_unit *);
16768eeb96aSKonstantin Belousov 	const char *name;
16868eeb96aSKonstantin Belousov };
16968eeb96aSKonstantin Belousov 
17068eeb96aSKonstantin Belousov #define	DMAR_INTR_FAULT		0
17168eeb96aSKonstantin Belousov #define	DMAR_INTR_QI		1
17268eeb96aSKonstantin Belousov #define	DMAR_INTR_TOTAL		2
17368eeb96aSKonstantin Belousov 
17486be9f0dSKonstantin Belousov struct dmar_unit {
17586be9f0dSKonstantin Belousov 	device_t dev;
17686be9f0dSKonstantin Belousov 	int unit;
17786be9f0dSKonstantin Belousov 	uint16_t segment;
17886be9f0dSKonstantin Belousov 	uint64_t base;
17986be9f0dSKonstantin Belousov 
18086be9f0dSKonstantin Belousov 	/* Resources */
18186be9f0dSKonstantin Belousov 	int reg_rid;
18286be9f0dSKonstantin Belousov 	struct resource *regs;
18368eeb96aSKonstantin Belousov 
18468eeb96aSKonstantin Belousov 	struct dmar_msi_data intrs[DMAR_INTR_TOTAL];
18586be9f0dSKonstantin Belousov 
18686be9f0dSKonstantin Belousov 	/* Hardware registers cache */
18786be9f0dSKonstantin Belousov 	uint32_t hw_ver;
18886be9f0dSKonstantin Belousov 	uint64_t hw_cap;
18986be9f0dSKonstantin Belousov 	uint64_t hw_ecap;
19086be9f0dSKonstantin Belousov 	uint32_t hw_gcmd;
19186be9f0dSKonstantin Belousov 
19286be9f0dSKonstantin Belousov 	/* Data for being a dmar */
19386be9f0dSKonstantin Belousov 	struct mtx lock;
1941abfd355SKonstantin Belousov 	LIST_HEAD(, dmar_domain) domains;
19586be9f0dSKonstantin Belousov 	struct unrhdr *domids;
19686be9f0dSKonstantin Belousov 	vm_object_t ctx_obj;
19786be9f0dSKonstantin Belousov 	u_int barrier_flags;
19886be9f0dSKonstantin Belousov 
19986be9f0dSKonstantin Belousov 	/* Fault handler data */
20086be9f0dSKonstantin Belousov 	struct mtx fault_lock;
20186be9f0dSKonstantin Belousov 	uint64_t *fault_log;
20286be9f0dSKonstantin Belousov 	int fault_log_head;
20386be9f0dSKonstantin Belousov 	int fault_log_tail;
20486be9f0dSKonstantin Belousov 	int fault_log_size;
20586be9f0dSKonstantin Belousov 	struct task fault_task;
20686be9f0dSKonstantin Belousov 	struct taskqueue *fault_taskqueue;
20786be9f0dSKonstantin Belousov 
20868eeb96aSKonstantin Belousov 	/* QI */
20968eeb96aSKonstantin Belousov 	int qi_enabled;
21068eeb96aSKonstantin Belousov 	vm_offset_t inv_queue;
21168eeb96aSKonstantin Belousov 	vm_size_t inv_queue_size;
21268eeb96aSKonstantin Belousov 	uint32_t inv_queue_avail;
21368eeb96aSKonstantin Belousov 	uint32_t inv_queue_tail;
21468eeb96aSKonstantin Belousov 	volatile uint32_t inv_waitd_seq_hw; /* hw writes there on wait
21568eeb96aSKonstantin Belousov 					       descr completion */
21668eeb96aSKonstantin Belousov 	uint64_t inv_waitd_seq_hw_phys;
21768eeb96aSKonstantin Belousov 	uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */
21868eeb96aSKonstantin Belousov 	u_int inv_waitd_gen;	/* seq number generation AKA seq overflows */
21968eeb96aSKonstantin Belousov 	u_int inv_seq_waiters;	/* count of waiters for seq */
22068eeb96aSKonstantin Belousov 	u_int inv_queue_full;	/* informational counter */
22168eeb96aSKonstantin Belousov 
2220a110d5bSKonstantin Belousov 	/* IR */
2230a110d5bSKonstantin Belousov 	int ir_enabled;
2240a110d5bSKonstantin Belousov 	vm_paddr_t irt_phys;
2250a110d5bSKonstantin Belousov 	dmar_irte_t *irt;
2260a110d5bSKonstantin Belousov 	u_int irte_cnt;
2270a110d5bSKonstantin Belousov 	vmem_t *irtids;
2280a110d5bSKonstantin Belousov 
22968eeb96aSKonstantin Belousov 	/* Delayed freeing of map entries queue processing */
23068eeb96aSKonstantin Belousov 	struct dmar_map_entries_tailq tlb_flush_entries;
23168eeb96aSKonstantin Belousov 	struct task qi_task;
23268eeb96aSKonstantin Belousov 	struct taskqueue *qi_taskqueue;
23368eeb96aSKonstantin Belousov 
23486be9f0dSKonstantin Belousov 	/* Busdma delayed map load */
23586be9f0dSKonstantin Belousov 	struct task dmamap_load_task;
23686be9f0dSKonstantin Belousov 	TAILQ_HEAD(, bus_dmamap_dmar) delayed_maps;
23786be9f0dSKonstantin Belousov 	struct taskqueue *delayed_taskqueue;
2380a110d5bSKonstantin Belousov 
2390a110d5bSKonstantin Belousov 	int dma_enabled;
24086be9f0dSKonstantin Belousov };
24186be9f0dSKonstantin Belousov 
24286be9f0dSKonstantin Belousov #define	DMAR_LOCK(dmar)		mtx_lock(&(dmar)->lock)
24386be9f0dSKonstantin Belousov #define	DMAR_UNLOCK(dmar)	mtx_unlock(&(dmar)->lock)
24486be9f0dSKonstantin Belousov #define	DMAR_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->lock, MA_OWNED)
24586be9f0dSKonstantin Belousov 
24686be9f0dSKonstantin Belousov #define	DMAR_FAULT_LOCK(dmar)	mtx_lock_spin(&(dmar)->fault_lock)
24786be9f0dSKonstantin Belousov #define	DMAR_FAULT_UNLOCK(dmar)	mtx_unlock_spin(&(dmar)->fault_lock)
24886be9f0dSKonstantin Belousov #define	DMAR_FAULT_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->fault_lock, MA_OWNED)
24986be9f0dSKonstantin Belousov 
25086be9f0dSKonstantin Belousov #define	DMAR_IS_COHERENT(dmar)	(((dmar)->hw_ecap & DMAR_ECAP_C) != 0)
25168eeb96aSKonstantin Belousov #define	DMAR_HAS_QI(dmar)	(((dmar)->hw_ecap & DMAR_ECAP_QI) != 0)
2520a110d5bSKonstantin Belousov #define	DMAR_X2APIC(dmar) \
2530a110d5bSKonstantin Belousov 	(x2apic_mode && ((dmar)->hw_ecap & DMAR_ECAP_EIM) != 0)
25486be9f0dSKonstantin Belousov 
25586be9f0dSKonstantin Belousov /* Barrier ids */
25686be9f0dSKonstantin Belousov #define	DMAR_BARRIER_RMRR	0
25786be9f0dSKonstantin Belousov #define	DMAR_BARRIER_USEQ	1
25886be9f0dSKonstantin Belousov 
25986be9f0dSKonstantin Belousov struct dmar_unit *dmar_find(device_t dev);
2600a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_hpet(device_t dev, uint16_t *rid);
2610a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_ioapic(u_int apic_id, uint16_t *rid);
26286be9f0dSKonstantin Belousov 
26386be9f0dSKonstantin Belousov u_int dmar_nd2mask(u_int nd);
26486be9f0dSKonstantin Belousov bool dmar_pglvl_supported(struct dmar_unit *unit, int pglvl);
2651abfd355SKonstantin Belousov int domain_set_agaw(struct dmar_domain *domain, int mgaw);
26686be9f0dSKonstantin Belousov int dmar_maxaddr2mgaw(struct dmar_unit *unit, dmar_gaddr_t maxaddr,
26786be9f0dSKonstantin Belousov     bool allow_less);
26886be9f0dSKonstantin Belousov vm_pindex_t pglvl_max_pages(int pglvl);
2691abfd355SKonstantin Belousov int domain_is_sp_lvl(struct dmar_domain *domain, int lvl);
27086be9f0dSKonstantin Belousov dmar_gaddr_t pglvl_page_size(int total_pglvl, int lvl);
2711abfd355SKonstantin Belousov dmar_gaddr_t domain_page_size(struct dmar_domain *domain, int lvl);
27268eeb96aSKonstantin Belousov int calc_am(struct dmar_unit *unit, dmar_gaddr_t base, dmar_gaddr_t size,
27368eeb96aSKonstantin Belousov     dmar_gaddr_t *isizep);
27486be9f0dSKonstantin Belousov struct vm_page *dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags);
27586be9f0dSKonstantin Belousov void dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags);
27686be9f0dSKonstantin Belousov void *dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags,
27786be9f0dSKonstantin Belousov     struct sf_buf **sf);
2786b7c46afSKonstantin Belousov void dmar_unmap_pgtbl(struct sf_buf *sf);
27986be9f0dSKonstantin Belousov int dmar_load_root_entry_ptr(struct dmar_unit *unit);
28086be9f0dSKonstantin Belousov int dmar_inv_ctx_glob(struct dmar_unit *unit);
28186be9f0dSKonstantin Belousov int dmar_inv_iotlb_glob(struct dmar_unit *unit);
28286be9f0dSKonstantin Belousov int dmar_flush_write_bufs(struct dmar_unit *unit);
2836b7c46afSKonstantin Belousov void dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst);
2846b7c46afSKonstantin Belousov void dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst);
2856b7c46afSKonstantin Belousov void dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst);
28686be9f0dSKonstantin Belousov int dmar_enable_translation(struct dmar_unit *unit);
28786be9f0dSKonstantin Belousov int dmar_disable_translation(struct dmar_unit *unit);
2880a110d5bSKonstantin Belousov int dmar_load_irt_ptr(struct dmar_unit *unit);
2890a110d5bSKonstantin Belousov int dmar_enable_ir(struct dmar_unit *unit);
2900a110d5bSKonstantin Belousov int dmar_disable_ir(struct dmar_unit *unit);
29186be9f0dSKonstantin Belousov bool dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id);
29286be9f0dSKonstantin Belousov void dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id);
29386be9f0dSKonstantin Belousov 
29468eeb96aSKonstantin Belousov int dmar_fault_intr(void *arg);
29568eeb96aSKonstantin Belousov void dmar_enable_fault_intr(struct dmar_unit *unit);
29668eeb96aSKonstantin Belousov void dmar_disable_fault_intr(struct dmar_unit *unit);
29786be9f0dSKonstantin Belousov int dmar_init_fault_log(struct dmar_unit *unit);
29886be9f0dSKonstantin Belousov void dmar_fini_fault_log(struct dmar_unit *unit);
29986be9f0dSKonstantin Belousov 
30068eeb96aSKonstantin Belousov int dmar_qi_intr(void *arg);
30168eeb96aSKonstantin Belousov void dmar_enable_qi_intr(struct dmar_unit *unit);
30268eeb96aSKonstantin Belousov void dmar_disable_qi_intr(struct dmar_unit *unit);
30368eeb96aSKonstantin Belousov int dmar_init_qi(struct dmar_unit *unit);
30468eeb96aSKonstantin Belousov void dmar_fini_qi(struct dmar_unit *unit);
3051abfd355SKonstantin Belousov void dmar_qi_invalidate_locked(struct dmar_domain *domain, dmar_gaddr_t start,
30668eeb96aSKonstantin Belousov     dmar_gaddr_t size, struct dmar_qi_genseq *pseq);
30768eeb96aSKonstantin Belousov void dmar_qi_invalidate_ctx_glob_locked(struct dmar_unit *unit);
30868eeb96aSKonstantin Belousov void dmar_qi_invalidate_iotlb_glob_locked(struct dmar_unit *unit);
3090a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec_glob(struct dmar_unit *unit);
3100a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec(struct dmar_unit *unit, u_int start, u_int cnt);
31168eeb96aSKonstantin Belousov 
3121abfd355SKonstantin Belousov vm_object_t domain_get_idmap_pgtbl(struct dmar_domain *domain,
3131abfd355SKonstantin Belousov     dmar_gaddr_t maxaddr);
31486be9f0dSKonstantin Belousov void put_idmap_pgtbl(vm_object_t obj);
3151abfd355SKonstantin Belousov int domain_map_buf(struct dmar_domain *domain, dmar_gaddr_t base,
3161abfd355SKonstantin Belousov     dmar_gaddr_t size, vm_page_t *ma, uint64_t pflags, int flags);
3171abfd355SKonstantin Belousov int domain_unmap_buf(struct dmar_domain *domain, dmar_gaddr_t base,
3181abfd355SKonstantin Belousov     dmar_gaddr_t size, int flags);
3191abfd355SKonstantin Belousov void domain_flush_iotlb_sync(struct dmar_domain *domain, dmar_gaddr_t base,
32068eeb96aSKonstantin Belousov     dmar_gaddr_t size);
3211abfd355SKonstantin Belousov int domain_alloc_pgtbl(struct dmar_domain *domain);
3221abfd355SKonstantin Belousov void domain_free_pgtbl(struct dmar_domain *domain);
32386be9f0dSKonstantin Belousov 
32486be9f0dSKonstantin Belousov struct dmar_ctx *dmar_instantiate_ctx(struct dmar_unit *dmar, device_t dev,
32586be9f0dSKonstantin Belousov     bool rmrr);
3261abfd355SKonstantin Belousov struct dmar_ctx *dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev,
32767499354SRyan Stone     uint16_t rid, bool id_mapped, bool rmrr_init);
3281abfd355SKonstantin Belousov int dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx);
32986be9f0dSKonstantin Belousov void dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx);
33086be9f0dSKonstantin Belousov void dmar_free_ctx(struct dmar_ctx *ctx);
33167499354SRyan Stone struct dmar_ctx *dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid);
3321abfd355SKonstantin Belousov void dmar_domain_unload_entry(struct dmar_map_entry *entry, bool free);
3331abfd355SKonstantin Belousov void dmar_domain_unload(struct dmar_domain *domain,
33486be9f0dSKonstantin Belousov     struct dmar_map_entries_tailq *entries, bool cansleep);
3351abfd355SKonstantin Belousov void dmar_domain_free_entry(struct dmar_map_entry *entry, bool free);
33686be9f0dSKonstantin Belousov 
33786be9f0dSKonstantin Belousov int dmar_init_busdma(struct dmar_unit *unit);
33886be9f0dSKonstantin Belousov void dmar_fini_busdma(struct dmar_unit *unit);
3390a110d5bSKonstantin Belousov device_t dmar_get_requester(device_t dev, uint16_t *rid);
34086be9f0dSKonstantin Belousov 
3411abfd355SKonstantin Belousov void dmar_gas_init_domain(struct dmar_domain *domain);
3421abfd355SKonstantin Belousov void dmar_gas_fini_domain(struct dmar_domain *domain);
3431abfd355SKonstantin Belousov struct dmar_map_entry *dmar_gas_alloc_entry(struct dmar_domain *domain,
3441abfd355SKonstantin Belousov     u_int flags);
3451abfd355SKonstantin Belousov void dmar_gas_free_entry(struct dmar_domain *domain,
3461abfd355SKonstantin Belousov     struct dmar_map_entry *entry);
3471abfd355SKonstantin Belousov void dmar_gas_free_space(struct dmar_domain *domain,
3481abfd355SKonstantin Belousov     struct dmar_map_entry *entry);
3491abfd355SKonstantin Belousov int dmar_gas_map(struct dmar_domain *domain,
3501abfd355SKonstantin Belousov     const struct bus_dma_tag_common *common, dmar_gaddr_t size, int offset,
3511abfd355SKonstantin Belousov     u_int eflags, u_int flags, vm_page_t *ma, struct dmar_map_entry **res);
3521abfd355SKonstantin Belousov void dmar_gas_free_region(struct dmar_domain *domain,
3531abfd355SKonstantin Belousov     struct dmar_map_entry *entry);
3541abfd355SKonstantin Belousov int dmar_gas_map_region(struct dmar_domain *domain,
3551abfd355SKonstantin Belousov     struct dmar_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma);
3561abfd355SKonstantin Belousov int dmar_gas_reserve_region(struct dmar_domain *domain, dmar_gaddr_t start,
35786be9f0dSKonstantin Belousov     dmar_gaddr_t end);
35886be9f0dSKonstantin Belousov 
3591abfd355SKonstantin Belousov void dmar_dev_parse_rmrr(struct dmar_domain *domain, device_t dev,
36086be9f0dSKonstantin Belousov     struct dmar_map_entries_tailq *rmrr_entries);
36186be9f0dSKonstantin Belousov int dmar_instantiate_rmrr_ctxs(struct dmar_unit *dmar);
36286be9f0dSKonstantin Belousov 
36386be9f0dSKonstantin Belousov void dmar_quirks_post_ident(struct dmar_unit *dmar);
36486be9f0dSKonstantin Belousov void dmar_quirks_pre_use(struct dmar_unit *dmar);
36586be9f0dSKonstantin Belousov 
3660a110d5bSKonstantin Belousov int dmar_init_irt(struct dmar_unit *unit);
3670a110d5bSKonstantin Belousov void dmar_fini_irt(struct dmar_unit *unit);
3680a110d5bSKonstantin Belousov 
36986be9f0dSKonstantin Belousov #define	DMAR_GM_CANWAIT	0x0001
37086be9f0dSKonstantin Belousov #define	DMAR_GM_CANSPLIT 0x0002
37186be9f0dSKonstantin Belousov 
37286be9f0dSKonstantin Belousov #define	DMAR_PGF_WAITOK	0x0001
37386be9f0dSKonstantin Belousov #define	DMAR_PGF_ZERO	0x0002
37486be9f0dSKonstantin Belousov #define	DMAR_PGF_ALLOC	0x0004
37586be9f0dSKonstantin Belousov #define	DMAR_PGF_NOALLOC 0x0008
37686be9f0dSKonstantin Belousov #define	DMAR_PGF_OBJL	0x0010
37786be9f0dSKonstantin Belousov 
37886be9f0dSKonstantin Belousov extern dmar_haddr_t dmar_high;
37986be9f0dSKonstantin Belousov extern int haw;
38086be9f0dSKonstantin Belousov extern int dmar_tbl_pagecnt;
38186be9f0dSKonstantin Belousov extern int dmar_match_verbose;
382e164cafcSKonstantin Belousov extern int dmar_batch_coalesce;
38386be9f0dSKonstantin Belousov extern int dmar_check_free;
38486be9f0dSKonstantin Belousov 
38586be9f0dSKonstantin Belousov static inline uint32_t
38686be9f0dSKonstantin Belousov dmar_read4(const struct dmar_unit *unit, int reg)
38786be9f0dSKonstantin Belousov {
38886be9f0dSKonstantin Belousov 
38986be9f0dSKonstantin Belousov 	return (bus_read_4(unit->regs, reg));
39086be9f0dSKonstantin Belousov }
39186be9f0dSKonstantin Belousov 
39286be9f0dSKonstantin Belousov static inline uint64_t
39386be9f0dSKonstantin Belousov dmar_read8(const struct dmar_unit *unit, int reg)
39486be9f0dSKonstantin Belousov {
39586be9f0dSKonstantin Belousov #ifdef __i386__
39686be9f0dSKonstantin Belousov 	uint32_t high, low;
39786be9f0dSKonstantin Belousov 
39886be9f0dSKonstantin Belousov 	low = bus_read_4(unit->regs, reg);
39986be9f0dSKonstantin Belousov 	high = bus_read_4(unit->regs, reg + 4);
40086be9f0dSKonstantin Belousov 	return (low | ((uint64_t)high << 32));
40186be9f0dSKonstantin Belousov #else
40286be9f0dSKonstantin Belousov 	return (bus_read_8(unit->regs, reg));
40386be9f0dSKonstantin Belousov #endif
40486be9f0dSKonstantin Belousov }
40586be9f0dSKonstantin Belousov 
40686be9f0dSKonstantin Belousov static inline void
40786be9f0dSKonstantin Belousov dmar_write4(const struct dmar_unit *unit, int reg, uint32_t val)
40886be9f0dSKonstantin Belousov {
40986be9f0dSKonstantin Belousov 
41086be9f0dSKonstantin Belousov 	KASSERT(reg != DMAR_GCMD_REG || (val & DMAR_GCMD_TE) ==
41186be9f0dSKonstantin Belousov 	    (unit->hw_gcmd & DMAR_GCMD_TE),
41286be9f0dSKonstantin Belousov 	    ("dmar%d clearing TE 0x%08x 0x%08x", unit->unit,
41386be9f0dSKonstantin Belousov 	    unit->hw_gcmd, val));
41486be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg, val);
41586be9f0dSKonstantin Belousov }
41686be9f0dSKonstantin Belousov 
41786be9f0dSKonstantin Belousov static inline void
41886be9f0dSKonstantin Belousov dmar_write8(const struct dmar_unit *unit, int reg, uint64_t val)
41986be9f0dSKonstantin Belousov {
42086be9f0dSKonstantin Belousov 
42186be9f0dSKonstantin Belousov 	KASSERT(reg != DMAR_GCMD_REG, ("8byte GCMD write"));
42286be9f0dSKonstantin Belousov #ifdef __i386__
42386be9f0dSKonstantin Belousov 	uint32_t high, low;
42486be9f0dSKonstantin Belousov 
42586be9f0dSKonstantin Belousov 	low = val;
42686be9f0dSKonstantin Belousov 	high = val >> 32;
42786be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg, low);
42886be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg + 4, high);
42986be9f0dSKonstantin Belousov #else
43086be9f0dSKonstantin Belousov 	bus_write_8(unit->regs, reg, val);
43186be9f0dSKonstantin Belousov #endif
43286be9f0dSKonstantin Belousov }
43386be9f0dSKonstantin Belousov 
43486be9f0dSKonstantin Belousov /*
43586be9f0dSKonstantin Belousov  * dmar_pte_store and dmar_pte_clear ensure that on i386, 32bit writes
43686be9f0dSKonstantin Belousov  * are issued in the correct order.  For store, the lower word,
43786be9f0dSKonstantin Belousov  * containing the P or R and W bits, is set only after the high word
43886be9f0dSKonstantin Belousov  * is written.  For clear, the P bit is cleared first, then the high
43986be9f0dSKonstantin Belousov  * word is cleared.
4400a110d5bSKonstantin Belousov  *
4410a110d5bSKonstantin Belousov  * dmar_pte_update updates the pte.  For amd64, the update is atomic.
4420a110d5bSKonstantin Belousov  * For i386, it first disables the entry by clearing the word
4430a110d5bSKonstantin Belousov  * containing the P bit, and then defer to dmar_pte_store.  The locked
4440a110d5bSKonstantin Belousov  * cmpxchg8b is probably available on any machine having DMAR support,
4450a110d5bSKonstantin Belousov  * but interrupt translation table may be mapped uncached.
44686be9f0dSKonstantin Belousov  */
44786be9f0dSKonstantin Belousov static inline void
4480a110d5bSKonstantin Belousov dmar_pte_store1(volatile uint64_t *dst, uint64_t val)
44986be9f0dSKonstantin Belousov {
45086be9f0dSKonstantin Belousov #ifdef __i386__
45186be9f0dSKonstantin Belousov 	volatile uint32_t *p;
45286be9f0dSKonstantin Belousov 	uint32_t hi, lo;
45386be9f0dSKonstantin Belousov 
45486be9f0dSKonstantin Belousov 	hi = val >> 32;
45586be9f0dSKonstantin Belousov 	lo = val;
45686be9f0dSKonstantin Belousov 	p = (volatile uint32_t *)dst;
45786be9f0dSKonstantin Belousov 	*(p + 1) = hi;
45886be9f0dSKonstantin Belousov 	*p = lo;
45986be9f0dSKonstantin Belousov #else
46086be9f0dSKonstantin Belousov 	*dst = val;
46186be9f0dSKonstantin Belousov #endif
46286be9f0dSKonstantin Belousov }
46386be9f0dSKonstantin Belousov 
46486be9f0dSKonstantin Belousov static inline void
4650a110d5bSKonstantin Belousov dmar_pte_store(volatile uint64_t *dst, uint64_t val)
4660a110d5bSKonstantin Belousov {
4670a110d5bSKonstantin Belousov 
4680a110d5bSKonstantin Belousov 	KASSERT(*dst == 0, ("used pte %p oldval %jx newval %jx",
4690a110d5bSKonstantin Belousov 	    dst, (uintmax_t)*dst, (uintmax_t)val));
4700a110d5bSKonstantin Belousov 	dmar_pte_store1(dst, val);
4710a110d5bSKonstantin Belousov }
4720a110d5bSKonstantin Belousov 
4730a110d5bSKonstantin Belousov static inline void
4740a110d5bSKonstantin Belousov dmar_pte_update(volatile uint64_t *dst, uint64_t val)
4750a110d5bSKonstantin Belousov {
4760a110d5bSKonstantin Belousov 
4770a110d5bSKonstantin Belousov #ifdef __i386__
4780a110d5bSKonstantin Belousov 	volatile uint32_t *p;
4790a110d5bSKonstantin Belousov 
4800a110d5bSKonstantin Belousov 	p = (volatile uint32_t *)dst;
4810a110d5bSKonstantin Belousov 	*p = 0;
4820a110d5bSKonstantin Belousov #endif
4830a110d5bSKonstantin Belousov 	dmar_pte_store1(dst, val);
4840a110d5bSKonstantin Belousov }
4850a110d5bSKonstantin Belousov 
4860a110d5bSKonstantin Belousov static inline void
48786be9f0dSKonstantin Belousov dmar_pte_clear(volatile uint64_t *dst)
48886be9f0dSKonstantin Belousov {
48986be9f0dSKonstantin Belousov #ifdef __i386__
49086be9f0dSKonstantin Belousov 	volatile uint32_t *p;
49186be9f0dSKonstantin Belousov 
49286be9f0dSKonstantin Belousov 	p = (volatile uint32_t *)dst;
49386be9f0dSKonstantin Belousov 	*p = 0;
49486be9f0dSKonstantin Belousov 	*(p + 1) = 0;
49586be9f0dSKonstantin Belousov #else
49686be9f0dSKonstantin Belousov 	*dst = 0;
49786be9f0dSKonstantin Belousov #endif
49886be9f0dSKonstantin Belousov }
49986be9f0dSKonstantin Belousov 
50086be9f0dSKonstantin Belousov static inline bool
50186be9f0dSKonstantin Belousov dmar_test_boundary(dmar_gaddr_t start, dmar_gaddr_t size,
50286be9f0dSKonstantin Belousov     dmar_gaddr_t boundary)
50386be9f0dSKonstantin Belousov {
50486be9f0dSKonstantin Belousov 
50586be9f0dSKonstantin Belousov 	if (boundary == 0)
50686be9f0dSKonstantin Belousov 		return (true);
50786be9f0dSKonstantin Belousov 	return (start + size <= ((start + boundary) & ~(boundary - 1)));
50886be9f0dSKonstantin Belousov }
50986be9f0dSKonstantin Belousov 
51086be9f0dSKonstantin Belousov #ifdef INVARIANTS
51186be9f0dSKonstantin Belousov #define	TD_PREP_PINNED_ASSERT						\
51286be9f0dSKonstantin Belousov 	int old_td_pinned;						\
51386be9f0dSKonstantin Belousov 	old_td_pinned = curthread->td_pinned
51486be9f0dSKonstantin Belousov #define	TD_PINNED_ASSERT						\
51586be9f0dSKonstantin Belousov 	KASSERT(curthread->td_pinned == old_td_pinned,			\
51686be9f0dSKonstantin Belousov 	    ("pin count leak: %d %d %s:%d", curthread->td_pinned,	\
51786be9f0dSKonstantin Belousov 	    old_td_pinned, __FILE__, __LINE__))
51886be9f0dSKonstantin Belousov #else
51986be9f0dSKonstantin Belousov #define	TD_PREP_PINNED_ASSERT
52086be9f0dSKonstantin Belousov #define	TD_PINNED_ASSERT
52186be9f0dSKonstantin Belousov #endif
52286be9f0dSKonstantin Belousov 
52386be9f0dSKonstantin Belousov #endif
524