xref: /freebsd/sys/x86/iommu/intel_dmar.h (revision 6b7c46afecc6a842823f3bcf95825009e9c471ec)
186be9f0dSKonstantin Belousov /*-
286be9f0dSKonstantin Belousov  * Copyright (c) 2013 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 */
5386be9f0dSKonstantin Belousov 	RB_ENTRY(dmar_map_entry) rb_entry;	 /* Links for ctx entries */
5486be9f0dSKonstantin Belousov 	TAILQ_ENTRY(dmar_map_entry) unroll_link; /* Link for unroll after
5586be9f0dSKonstantin Belousov 						    dmamap_load failure */
5668eeb96aSKonstantin Belousov 	struct dmar_ctx *ctx;
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 
7686be9f0dSKonstantin Belousov struct dmar_ctx {
7767499354SRyan Stone 	uint16_t rid;	/* pci RID */
7886be9f0dSKonstantin Belousov 	int domain;	/* DID */
7986be9f0dSKonstantin Belousov 	int mgaw;	/* Real max address width */
8086be9f0dSKonstantin Belousov 	int agaw;	/* Adjusted guest address width */
8186be9f0dSKonstantin Belousov 	int pglvl;	/* The pagelevel */
8286be9f0dSKonstantin Belousov 	int awlvl;	/* The pagelevel as the bitmask, to set in
8386be9f0dSKonstantin Belousov 			   context entry */
8486be9f0dSKonstantin Belousov 	dmar_gaddr_t end;/* Highest address + 1 in the guest AS */
8586be9f0dSKonstantin Belousov 	u_int refs;	/* References to the context, from tags */
8686be9f0dSKonstantin Belousov 	struct dmar_unit *dmar;
8786be9f0dSKonstantin Belousov 	struct bus_dma_tag_dmar ctx_tag; /* Root tag */
8886be9f0dSKonstantin Belousov 	struct mtx lock;
8986be9f0dSKonstantin Belousov 	LIST_ENTRY(dmar_ctx) link;	/* Member in the dmar list */
9086be9f0dSKonstantin Belousov 	vm_object_t pgtbl_obj;		/* Page table pages */
9186be9f0dSKonstantin Belousov 	u_int flags;			/* Protected by dmar lock */
9286be9f0dSKonstantin Belousov 	uint64_t last_fault_rec[2];	/* Last fault reported */
9386be9f0dSKonstantin Belousov 	u_int entries_cnt;
9486be9f0dSKonstantin Belousov 	u_long loads;
9586be9f0dSKonstantin Belousov 	u_long unloads;
9686be9f0dSKonstantin Belousov 	struct dmar_gas_entries_tree rb_root;
9786be9f0dSKonstantin Belousov 	struct dmar_map_entries_tailq unload_entries; /* Entries to unload */
9886be9f0dSKonstantin Belousov 	struct dmar_map_entry *first_place, *last_place;
9986be9f0dSKonstantin Belousov 	struct task unload_task;
10086be9f0dSKonstantin Belousov };
10186be9f0dSKonstantin Belousov 
10286be9f0dSKonstantin Belousov /* struct dmar_ctx flags */
10386be9f0dSKonstantin Belousov #define	DMAR_CTX_FAULTED	0x0001	/* Fault was reported,
10486be9f0dSKonstantin Belousov 					   last_fault_rec is valid */
10586be9f0dSKonstantin Belousov #define	DMAR_CTX_IDMAP		0x0002	/* Context uses identity page table */
10686be9f0dSKonstantin Belousov #define	DMAR_CTX_RMRR		0x0004	/* Context contains RMRR entry,
10786be9f0dSKonstantin Belousov 					   cannot be turned off */
10886be9f0dSKonstantin Belousov #define	DMAR_CTX_DISABLED	0x0008	/* Device is disabled, the
10986be9f0dSKonstantin Belousov 					   ephemeral reference is kept
11086be9f0dSKonstantin Belousov 					   to prevent context destruction */
11186be9f0dSKonstantin Belousov 
11286be9f0dSKonstantin Belousov #define	DMAR_CTX_PGLOCK(ctx)	VM_OBJECT_WLOCK((ctx)->pgtbl_obj)
11386be9f0dSKonstantin Belousov #define	DMAR_CTX_PGTRYLOCK(ctx)	VM_OBJECT_TRYWLOCK((ctx)->pgtbl_obj)
11486be9f0dSKonstantin Belousov #define	DMAR_CTX_PGUNLOCK(ctx)	VM_OBJECT_WUNLOCK((ctx)->pgtbl_obj)
11586be9f0dSKonstantin Belousov #define	DMAR_CTX_ASSERT_PGLOCKED(ctx) \
11686be9f0dSKonstantin Belousov 	VM_OBJECT_ASSERT_WLOCKED((ctx)->pgtbl_obj)
11786be9f0dSKonstantin Belousov 
11886be9f0dSKonstantin Belousov #define	DMAR_CTX_LOCK(ctx)	mtx_lock(&(ctx)->lock)
11986be9f0dSKonstantin Belousov #define	DMAR_CTX_UNLOCK(ctx)	mtx_unlock(&(ctx)->lock)
12086be9f0dSKonstantin Belousov #define	DMAR_CTX_ASSERT_LOCKED(ctx) mtx_assert(&(ctx)->lock, MA_OWNED)
12186be9f0dSKonstantin Belousov 
12268eeb96aSKonstantin Belousov struct dmar_msi_data {
12368eeb96aSKonstantin Belousov 	int irq;
12468eeb96aSKonstantin Belousov 	int irq_rid;
12568eeb96aSKonstantin Belousov 	struct resource *irq_res;
12668eeb96aSKonstantin Belousov 	void *intr_handle;
12768eeb96aSKonstantin Belousov 	int (*handler)(void *);
12868eeb96aSKonstantin Belousov 	int msi_data_reg;
12968eeb96aSKonstantin Belousov 	int msi_addr_reg;
13068eeb96aSKonstantin Belousov 	int msi_uaddr_reg;
13168eeb96aSKonstantin Belousov 	void (*enable_intr)(struct dmar_unit *);
13268eeb96aSKonstantin Belousov 	void (*disable_intr)(struct dmar_unit *);
13368eeb96aSKonstantin Belousov 	const char *name;
13468eeb96aSKonstantin Belousov };
13568eeb96aSKonstantin Belousov 
13668eeb96aSKonstantin Belousov #define	DMAR_INTR_FAULT		0
13768eeb96aSKonstantin Belousov #define	DMAR_INTR_QI		1
13868eeb96aSKonstantin Belousov #define	DMAR_INTR_TOTAL		2
13968eeb96aSKonstantin Belousov 
14086be9f0dSKonstantin Belousov struct dmar_unit {
14186be9f0dSKonstantin Belousov 	device_t dev;
14286be9f0dSKonstantin Belousov 	int unit;
14386be9f0dSKonstantin Belousov 	uint16_t segment;
14486be9f0dSKonstantin Belousov 	uint64_t base;
14586be9f0dSKonstantin Belousov 
14686be9f0dSKonstantin Belousov 	/* Resources */
14786be9f0dSKonstantin Belousov 	int reg_rid;
14886be9f0dSKonstantin Belousov 	struct resource *regs;
14968eeb96aSKonstantin Belousov 
15068eeb96aSKonstantin Belousov 	struct dmar_msi_data intrs[DMAR_INTR_TOTAL];
15186be9f0dSKonstantin Belousov 
15286be9f0dSKonstantin Belousov 	/* Hardware registers cache */
15386be9f0dSKonstantin Belousov 	uint32_t hw_ver;
15486be9f0dSKonstantin Belousov 	uint64_t hw_cap;
15586be9f0dSKonstantin Belousov 	uint64_t hw_ecap;
15686be9f0dSKonstantin Belousov 	uint32_t hw_gcmd;
15786be9f0dSKonstantin Belousov 
15886be9f0dSKonstantin Belousov 	/* Data for being a dmar */
15986be9f0dSKonstantin Belousov 	struct mtx lock;
16086be9f0dSKonstantin Belousov 	LIST_HEAD(, dmar_ctx) contexts;
16186be9f0dSKonstantin Belousov 	struct unrhdr *domids;
16286be9f0dSKonstantin Belousov 	vm_object_t ctx_obj;
16386be9f0dSKonstantin Belousov 	u_int barrier_flags;
16486be9f0dSKonstantin Belousov 
16586be9f0dSKonstantin Belousov 	/* Fault handler data */
16686be9f0dSKonstantin Belousov 	struct mtx fault_lock;
16786be9f0dSKonstantin Belousov 	uint64_t *fault_log;
16886be9f0dSKonstantin Belousov 	int fault_log_head;
16986be9f0dSKonstantin Belousov 	int fault_log_tail;
17086be9f0dSKonstantin Belousov 	int fault_log_size;
17186be9f0dSKonstantin Belousov 	struct task fault_task;
17286be9f0dSKonstantin Belousov 	struct taskqueue *fault_taskqueue;
17386be9f0dSKonstantin Belousov 
17468eeb96aSKonstantin Belousov 	/* QI */
17568eeb96aSKonstantin Belousov 	int qi_enabled;
17668eeb96aSKonstantin Belousov 	vm_offset_t inv_queue;
17768eeb96aSKonstantin Belousov 	vm_size_t inv_queue_size;
17868eeb96aSKonstantin Belousov 	uint32_t inv_queue_avail;
17968eeb96aSKonstantin Belousov 	uint32_t inv_queue_tail;
18068eeb96aSKonstantin Belousov 	volatile uint32_t inv_waitd_seq_hw; /* hw writes there on wait
18168eeb96aSKonstantin Belousov 					       descr completion */
18268eeb96aSKonstantin Belousov 	uint64_t inv_waitd_seq_hw_phys;
18368eeb96aSKonstantin Belousov 	uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */
18468eeb96aSKonstantin Belousov 	u_int inv_waitd_gen;	/* seq number generation AKA seq overflows */
18568eeb96aSKonstantin Belousov 	u_int inv_seq_waiters;	/* count of waiters for seq */
18668eeb96aSKonstantin Belousov 	u_int inv_queue_full;	/* informational counter */
18768eeb96aSKonstantin Belousov 
18868eeb96aSKonstantin Belousov 	/* Delayed freeing of map entries queue processing */
18968eeb96aSKonstantin Belousov 	struct dmar_map_entries_tailq tlb_flush_entries;
19068eeb96aSKonstantin Belousov 	struct task qi_task;
19168eeb96aSKonstantin Belousov 	struct taskqueue *qi_taskqueue;
19268eeb96aSKonstantin Belousov 
19386be9f0dSKonstantin Belousov 	/* Busdma delayed map load */
19486be9f0dSKonstantin Belousov 	struct task dmamap_load_task;
19586be9f0dSKonstantin Belousov 	TAILQ_HEAD(, bus_dmamap_dmar) delayed_maps;
19686be9f0dSKonstantin Belousov 	struct taskqueue *delayed_taskqueue;
19786be9f0dSKonstantin Belousov };
19886be9f0dSKonstantin Belousov 
19986be9f0dSKonstantin Belousov #define	DMAR_LOCK(dmar)		mtx_lock(&(dmar)->lock)
20086be9f0dSKonstantin Belousov #define	DMAR_UNLOCK(dmar)	mtx_unlock(&(dmar)->lock)
20186be9f0dSKonstantin Belousov #define	DMAR_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->lock, MA_OWNED)
20286be9f0dSKonstantin Belousov 
20386be9f0dSKonstantin Belousov #define	DMAR_FAULT_LOCK(dmar)	mtx_lock_spin(&(dmar)->fault_lock)
20486be9f0dSKonstantin Belousov #define	DMAR_FAULT_UNLOCK(dmar)	mtx_unlock_spin(&(dmar)->fault_lock)
20586be9f0dSKonstantin Belousov #define	DMAR_FAULT_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->fault_lock, MA_OWNED)
20686be9f0dSKonstantin Belousov 
20786be9f0dSKonstantin Belousov #define	DMAR_IS_COHERENT(dmar)	(((dmar)->hw_ecap & DMAR_ECAP_C) != 0)
20868eeb96aSKonstantin Belousov #define	DMAR_HAS_QI(dmar)	(((dmar)->hw_ecap & DMAR_ECAP_QI) != 0)
20986be9f0dSKonstantin Belousov 
21086be9f0dSKonstantin Belousov /* Barrier ids */
21186be9f0dSKonstantin Belousov #define	DMAR_BARRIER_RMRR	0
21286be9f0dSKonstantin Belousov #define	DMAR_BARRIER_USEQ	1
21386be9f0dSKonstantin Belousov 
21486be9f0dSKonstantin Belousov struct dmar_unit *dmar_find(device_t dev);
21586be9f0dSKonstantin Belousov 
21686be9f0dSKonstantin Belousov u_int dmar_nd2mask(u_int nd);
21786be9f0dSKonstantin Belousov bool dmar_pglvl_supported(struct dmar_unit *unit, int pglvl);
21886be9f0dSKonstantin Belousov int ctx_set_agaw(struct dmar_ctx *ctx, int mgaw);
21986be9f0dSKonstantin Belousov int dmar_maxaddr2mgaw(struct dmar_unit* unit, dmar_gaddr_t maxaddr,
22086be9f0dSKonstantin Belousov     bool allow_less);
22186be9f0dSKonstantin Belousov vm_pindex_t pglvl_max_pages(int pglvl);
22286be9f0dSKonstantin Belousov int ctx_is_sp_lvl(struct dmar_ctx *ctx, int lvl);
22386be9f0dSKonstantin Belousov dmar_gaddr_t pglvl_page_size(int total_pglvl, int lvl);
22486be9f0dSKonstantin Belousov dmar_gaddr_t ctx_page_size(struct dmar_ctx *ctx, int lvl);
22568eeb96aSKonstantin Belousov int calc_am(struct dmar_unit *unit, dmar_gaddr_t base, dmar_gaddr_t size,
22668eeb96aSKonstantin Belousov     dmar_gaddr_t *isizep);
22786be9f0dSKonstantin Belousov struct vm_page *dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags);
22886be9f0dSKonstantin Belousov void dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags);
22986be9f0dSKonstantin Belousov void *dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags,
23086be9f0dSKonstantin Belousov     struct sf_buf **sf);
231*6b7c46afSKonstantin Belousov void dmar_unmap_pgtbl(struct sf_buf *sf);
23286be9f0dSKonstantin Belousov int dmar_load_root_entry_ptr(struct dmar_unit *unit);
23386be9f0dSKonstantin Belousov int dmar_inv_ctx_glob(struct dmar_unit *unit);
23486be9f0dSKonstantin Belousov int dmar_inv_iotlb_glob(struct dmar_unit *unit);
23586be9f0dSKonstantin Belousov int dmar_flush_write_bufs(struct dmar_unit *unit);
236*6b7c46afSKonstantin Belousov void dmar_flush_pte_to_ram(struct dmar_unit *unit, dmar_pte_t *dst);
237*6b7c46afSKonstantin Belousov void dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst);
238*6b7c46afSKonstantin Belousov void dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst);
23986be9f0dSKonstantin Belousov int dmar_enable_translation(struct dmar_unit *unit);
24086be9f0dSKonstantin Belousov int dmar_disable_translation(struct dmar_unit *unit);
24186be9f0dSKonstantin Belousov bool dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id);
24286be9f0dSKonstantin Belousov void dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id);
24386be9f0dSKonstantin Belousov 
24468eeb96aSKonstantin Belousov int dmar_fault_intr(void *arg);
24568eeb96aSKonstantin Belousov void dmar_enable_fault_intr(struct dmar_unit *unit);
24668eeb96aSKonstantin Belousov void dmar_disable_fault_intr(struct dmar_unit *unit);
24786be9f0dSKonstantin Belousov int dmar_init_fault_log(struct dmar_unit *unit);
24886be9f0dSKonstantin Belousov void dmar_fini_fault_log(struct dmar_unit *unit);
24986be9f0dSKonstantin Belousov 
25068eeb96aSKonstantin Belousov int dmar_qi_intr(void *arg);
25168eeb96aSKonstantin Belousov void dmar_enable_qi_intr(struct dmar_unit *unit);
25268eeb96aSKonstantin Belousov void dmar_disable_qi_intr(struct dmar_unit *unit);
25368eeb96aSKonstantin Belousov int dmar_init_qi(struct dmar_unit *unit);
25468eeb96aSKonstantin Belousov void dmar_fini_qi(struct dmar_unit *unit);
25568eeb96aSKonstantin Belousov void dmar_qi_invalidate_locked(struct dmar_ctx *ctx, dmar_gaddr_t start,
25668eeb96aSKonstantin Belousov     dmar_gaddr_t size, struct dmar_qi_genseq *pseq);
25768eeb96aSKonstantin Belousov void dmar_qi_invalidate_ctx_glob_locked(struct dmar_unit *unit);
25868eeb96aSKonstantin Belousov void dmar_qi_invalidate_iotlb_glob_locked(struct dmar_unit *unit);
25968eeb96aSKonstantin Belousov 
26086be9f0dSKonstantin Belousov vm_object_t ctx_get_idmap_pgtbl(struct dmar_ctx *ctx, dmar_gaddr_t maxaddr);
26186be9f0dSKonstantin Belousov void put_idmap_pgtbl(vm_object_t obj);
26286be9f0dSKonstantin Belousov int ctx_map_buf(struct dmar_ctx *ctx, dmar_gaddr_t base, dmar_gaddr_t size,
26386be9f0dSKonstantin Belousov     vm_page_t *ma, uint64_t pflags, int flags);
26486be9f0dSKonstantin Belousov int ctx_unmap_buf(struct dmar_ctx *ctx, dmar_gaddr_t base, dmar_gaddr_t size,
26586be9f0dSKonstantin Belousov     int flags);
26668eeb96aSKonstantin Belousov void ctx_flush_iotlb_sync(struct dmar_ctx *ctx, dmar_gaddr_t base,
26768eeb96aSKonstantin Belousov     dmar_gaddr_t size);
26886be9f0dSKonstantin Belousov int ctx_alloc_pgtbl(struct dmar_ctx *ctx);
26986be9f0dSKonstantin Belousov void ctx_free_pgtbl(struct dmar_ctx *ctx);
27086be9f0dSKonstantin Belousov 
27186be9f0dSKonstantin Belousov struct dmar_ctx *dmar_instantiate_ctx(struct dmar_unit *dmar, device_t dev,
27286be9f0dSKonstantin Belousov     bool rmrr);
27386be9f0dSKonstantin Belousov struct dmar_ctx *dmar_get_ctx(struct dmar_unit *dmar, device_t dev,
27467499354SRyan Stone     uint16_t rid, bool id_mapped, bool rmrr_init);
27586be9f0dSKonstantin Belousov void dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx);
27686be9f0dSKonstantin Belousov void dmar_free_ctx(struct dmar_ctx *ctx);
27767499354SRyan Stone struct dmar_ctx *dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid);
27868eeb96aSKonstantin Belousov void dmar_ctx_unload_entry(struct dmar_map_entry *entry, bool free);
27986be9f0dSKonstantin Belousov void dmar_ctx_unload(struct dmar_ctx *ctx,
28086be9f0dSKonstantin Belousov     struct dmar_map_entries_tailq *entries, bool cansleep);
28168eeb96aSKonstantin Belousov void dmar_ctx_free_entry(struct dmar_map_entry *entry, bool free);
28286be9f0dSKonstantin Belousov 
28386be9f0dSKonstantin Belousov int dmar_init_busdma(struct dmar_unit *unit);
28486be9f0dSKonstantin Belousov void dmar_fini_busdma(struct dmar_unit *unit);
28586be9f0dSKonstantin Belousov 
28686be9f0dSKonstantin Belousov void dmar_gas_init_ctx(struct dmar_ctx *ctx);
28786be9f0dSKonstantin Belousov void dmar_gas_fini_ctx(struct dmar_ctx *ctx);
28886be9f0dSKonstantin Belousov struct dmar_map_entry *dmar_gas_alloc_entry(struct dmar_ctx *ctx, u_int flags);
28986be9f0dSKonstantin Belousov void dmar_gas_free_entry(struct dmar_ctx *ctx, struct dmar_map_entry *entry);
29086be9f0dSKonstantin Belousov void dmar_gas_free_space(struct dmar_ctx *ctx, struct dmar_map_entry *entry);
29186be9f0dSKonstantin Belousov int dmar_gas_map(struct dmar_ctx *ctx, const struct bus_dma_tag_common *common,
29286be9f0dSKonstantin Belousov     dmar_gaddr_t size, u_int eflags, u_int flags, vm_page_t *ma,
29386be9f0dSKonstantin Belousov     struct dmar_map_entry **res);
29468eeb96aSKonstantin Belousov void dmar_gas_free_region(struct dmar_ctx *ctx, struct dmar_map_entry *entry);
29586be9f0dSKonstantin Belousov int dmar_gas_map_region(struct dmar_ctx *ctx, struct dmar_map_entry *entry,
29686be9f0dSKonstantin Belousov     u_int eflags, u_int flags, vm_page_t *ma);
29786be9f0dSKonstantin Belousov int dmar_gas_reserve_region(struct dmar_ctx *ctx, dmar_gaddr_t start,
29886be9f0dSKonstantin Belousov     dmar_gaddr_t end);
29986be9f0dSKonstantin Belousov 
30086be9f0dSKonstantin Belousov void dmar_ctx_parse_rmrr(struct dmar_ctx *ctx, device_t dev,
30186be9f0dSKonstantin Belousov     struct dmar_map_entries_tailq *rmrr_entries);
30286be9f0dSKonstantin Belousov int dmar_instantiate_rmrr_ctxs(struct dmar_unit *dmar);
30386be9f0dSKonstantin Belousov 
30486be9f0dSKonstantin Belousov void dmar_quirks_post_ident(struct dmar_unit *dmar);
30586be9f0dSKonstantin Belousov void dmar_quirks_pre_use(struct dmar_unit *dmar);
30686be9f0dSKonstantin Belousov 
30786be9f0dSKonstantin Belousov #define	DMAR_GM_CANWAIT	0x0001
30886be9f0dSKonstantin Belousov #define	DMAR_GM_CANSPLIT 0x0002
30986be9f0dSKonstantin Belousov 
31086be9f0dSKonstantin Belousov #define	DMAR_PGF_WAITOK	0x0001
31186be9f0dSKonstantin Belousov #define	DMAR_PGF_ZERO	0x0002
31286be9f0dSKonstantin Belousov #define	DMAR_PGF_ALLOC	0x0004
31386be9f0dSKonstantin Belousov #define	DMAR_PGF_NOALLOC 0x0008
31486be9f0dSKonstantin Belousov #define	DMAR_PGF_OBJL	0x0010
31586be9f0dSKonstantin Belousov 
31686be9f0dSKonstantin Belousov extern dmar_haddr_t dmar_high;
31786be9f0dSKonstantin Belousov extern int haw;
31886be9f0dSKonstantin Belousov extern int dmar_tbl_pagecnt;
31986be9f0dSKonstantin Belousov extern int dmar_match_verbose;
32086be9f0dSKonstantin Belousov extern int dmar_check_free;
32186be9f0dSKonstantin Belousov 
32286be9f0dSKonstantin Belousov static inline uint32_t
32386be9f0dSKonstantin Belousov dmar_read4(const struct dmar_unit *unit, int reg)
32486be9f0dSKonstantin Belousov {
32586be9f0dSKonstantin Belousov 
32686be9f0dSKonstantin Belousov 	return (bus_read_4(unit->regs, reg));
32786be9f0dSKonstantin Belousov }
32886be9f0dSKonstantin Belousov 
32986be9f0dSKonstantin Belousov static inline uint64_t
33086be9f0dSKonstantin Belousov dmar_read8(const struct dmar_unit *unit, int reg)
33186be9f0dSKonstantin Belousov {
33286be9f0dSKonstantin Belousov #ifdef __i386__
33386be9f0dSKonstantin Belousov 	uint32_t high, low;
33486be9f0dSKonstantin Belousov 
33586be9f0dSKonstantin Belousov 	low = bus_read_4(unit->regs, reg);
33686be9f0dSKonstantin Belousov 	high = bus_read_4(unit->regs, reg + 4);
33786be9f0dSKonstantin Belousov 	return (low | ((uint64_t)high << 32));
33886be9f0dSKonstantin Belousov #else
33986be9f0dSKonstantin Belousov 	return (bus_read_8(unit->regs, reg));
34086be9f0dSKonstantin Belousov #endif
34186be9f0dSKonstantin Belousov }
34286be9f0dSKonstantin Belousov 
34386be9f0dSKonstantin Belousov static inline void
34486be9f0dSKonstantin Belousov dmar_write4(const struct dmar_unit *unit, int reg, uint32_t val)
34586be9f0dSKonstantin Belousov {
34686be9f0dSKonstantin Belousov 
34786be9f0dSKonstantin Belousov 	KASSERT(reg != DMAR_GCMD_REG || (val & DMAR_GCMD_TE) ==
34886be9f0dSKonstantin Belousov 	    (unit->hw_gcmd & DMAR_GCMD_TE),
34986be9f0dSKonstantin Belousov 	    ("dmar%d clearing TE 0x%08x 0x%08x", unit->unit,
35086be9f0dSKonstantin Belousov 	    unit->hw_gcmd, val));
35186be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg, val);
35286be9f0dSKonstantin Belousov }
35386be9f0dSKonstantin Belousov 
35486be9f0dSKonstantin Belousov static inline void
35586be9f0dSKonstantin Belousov dmar_write8(const struct dmar_unit *unit, int reg, uint64_t val)
35686be9f0dSKonstantin Belousov {
35786be9f0dSKonstantin Belousov 
35886be9f0dSKonstantin Belousov 	KASSERT(reg != DMAR_GCMD_REG, ("8byte GCMD write"));
35986be9f0dSKonstantin Belousov #ifdef __i386__
36086be9f0dSKonstantin Belousov 	uint32_t high, low;
36186be9f0dSKonstantin Belousov 
36286be9f0dSKonstantin Belousov 	low = val;
36386be9f0dSKonstantin Belousov 	high = val >> 32;
36486be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg, low);
36586be9f0dSKonstantin Belousov 	bus_write_4(unit->regs, reg + 4, high);
36686be9f0dSKonstantin Belousov #else
36786be9f0dSKonstantin Belousov 	bus_write_8(unit->regs, reg, val);
36886be9f0dSKonstantin Belousov #endif
36986be9f0dSKonstantin Belousov }
37086be9f0dSKonstantin Belousov 
37186be9f0dSKonstantin Belousov /*
37286be9f0dSKonstantin Belousov  * dmar_pte_store and dmar_pte_clear ensure that on i386, 32bit writes
37386be9f0dSKonstantin Belousov  * are issued in the correct order.  For store, the lower word,
37486be9f0dSKonstantin Belousov  * containing the P or R and W bits, is set only after the high word
37586be9f0dSKonstantin Belousov  * is written.  For clear, the P bit is cleared first, then the high
37686be9f0dSKonstantin Belousov  * word is cleared.
37786be9f0dSKonstantin Belousov  */
37886be9f0dSKonstantin Belousov static inline void
37986be9f0dSKonstantin Belousov dmar_pte_store(volatile uint64_t *dst, uint64_t val)
38086be9f0dSKonstantin Belousov {
38186be9f0dSKonstantin Belousov 
38286be9f0dSKonstantin Belousov 	KASSERT(*dst == 0, ("used pte %p oldval %jx newval %jx",
38386be9f0dSKonstantin Belousov 	    dst, (uintmax_t)*dst, (uintmax_t)val));
38486be9f0dSKonstantin Belousov #ifdef __i386__
38586be9f0dSKonstantin Belousov 	volatile uint32_t *p;
38686be9f0dSKonstantin Belousov 	uint32_t hi, lo;
38786be9f0dSKonstantin Belousov 
38886be9f0dSKonstantin Belousov 	hi = val >> 32;
38986be9f0dSKonstantin Belousov 	lo = val;
39086be9f0dSKonstantin Belousov 	p = (volatile uint32_t *)dst;
39186be9f0dSKonstantin Belousov 	*(p + 1) = hi;
39286be9f0dSKonstantin Belousov 	*p = lo;
39386be9f0dSKonstantin Belousov #else
39486be9f0dSKonstantin Belousov 	*dst = val;
39586be9f0dSKonstantin Belousov #endif
39686be9f0dSKonstantin Belousov }
39786be9f0dSKonstantin Belousov 
39886be9f0dSKonstantin Belousov static inline void
39986be9f0dSKonstantin Belousov dmar_pte_clear(volatile uint64_t *dst)
40086be9f0dSKonstantin Belousov {
40186be9f0dSKonstantin Belousov #ifdef __i386__
40286be9f0dSKonstantin Belousov 	volatile uint32_t *p;
40386be9f0dSKonstantin Belousov 
40486be9f0dSKonstantin Belousov 	p = (volatile uint32_t *)dst;
40586be9f0dSKonstantin Belousov 	*p = 0;
40686be9f0dSKonstantin Belousov 	*(p + 1) = 0;
40786be9f0dSKonstantin Belousov #else
40886be9f0dSKonstantin Belousov 	*dst = 0;
40986be9f0dSKonstantin Belousov #endif
41086be9f0dSKonstantin Belousov }
41186be9f0dSKonstantin Belousov 
41286be9f0dSKonstantin Belousov static inline bool
41386be9f0dSKonstantin Belousov dmar_test_boundary(dmar_gaddr_t start, dmar_gaddr_t size,
41486be9f0dSKonstantin Belousov     dmar_gaddr_t boundary)
41586be9f0dSKonstantin Belousov {
41686be9f0dSKonstantin Belousov 
41786be9f0dSKonstantin Belousov 	if (boundary == 0)
41886be9f0dSKonstantin Belousov 		return (true);
41986be9f0dSKonstantin Belousov 	return (start + size <= ((start + boundary) & ~(boundary - 1)));
42086be9f0dSKonstantin Belousov }
42186be9f0dSKonstantin Belousov 
42286be9f0dSKonstantin Belousov #ifdef INVARIANTS
42386be9f0dSKonstantin Belousov #define	TD_PREP_PINNED_ASSERT						\
42486be9f0dSKonstantin Belousov 	int old_td_pinned;						\
42586be9f0dSKonstantin Belousov 	old_td_pinned = curthread->td_pinned
42686be9f0dSKonstantin Belousov #define	TD_PINNED_ASSERT						\
42786be9f0dSKonstantin Belousov 	KASSERT(curthread->td_pinned == old_td_pinned,			\
42886be9f0dSKonstantin Belousov 	    ("pin count leak: %d %d %s:%d", curthread->td_pinned,	\
42986be9f0dSKonstantin Belousov 	    old_td_pinned, __FILE__, __LINE__))
43086be9f0dSKonstantin Belousov #else
43186be9f0dSKonstantin Belousov #define	TD_PREP_PINNED_ASSERT
43286be9f0dSKonstantin Belousov #define	TD_PINNED_ASSERT
43386be9f0dSKonstantin Belousov #endif
43486be9f0dSKonstantin Belousov 
43586be9f0dSKonstantin Belousov #endif
436