xref: /freebsd/sys/dev/iommu/iommu.h (revision db0110a5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 The FreeBSD Foundation
5  *
6  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef _DEV_IOMMU_IOMMU_H_
34 #define _DEV_IOMMU_IOMMU_H_
35 
36 #include <dev/iommu/iommu_types.h>
37 
38 struct bus_dma_tag_common;
39 struct iommu_map_entry;
40 TAILQ_HEAD(iommu_map_entries_tailq, iommu_map_entry);
41 
42 RB_HEAD(iommu_gas_entries_tree, iommu_map_entry);
43 RB_PROTOTYPE(iommu_gas_entries_tree, iommu_map_entry, rb_entry,
44     iommu_gas_cmp_entries);
45 
46 struct iommu_qi_genseq {
47 	u_int gen;
48 	uint32_t seq;
49 };
50 
51 struct iommu_map_entry {
52 	iommu_gaddr_t start;
53 	iommu_gaddr_t end;
54 	iommu_gaddr_t first;		/* Least start in subtree */
55 	iommu_gaddr_t last;		/* Greatest end in subtree */
56 	iommu_gaddr_t free_down;	/* Max free space below the
57 					   current R/B tree node */
58 	u_int flags;
59 	TAILQ_ENTRY(iommu_map_entry) dmamap_link; /* Link for dmamap entries */
60 	RB_ENTRY(iommu_map_entry) rb_entry;	 /* Links for domain entries */
61 	struct iommu_domain *domain;
62 	struct iommu_qi_genseq gseq;
63 };
64 
65 struct iommu_unit {
66 	struct mtx lock;
67 	device_t dev;
68 	int unit;
69 
70 	int dma_enabled;
71 
72 	/* Busdma delayed map load */
73 	struct task dmamap_load_task;
74 	TAILQ_HEAD(, bus_dmamap_iommu) delayed_maps;
75 	struct taskqueue *delayed_taskqueue;
76 
77 	/*
78 	 * Bitmap of buses for which context must ignore slot:func,
79 	 * duplicating the page table pointer into all context table
80 	 * entries.  This is a client-controlled quirk to support some
81 	 * NTBs.
82 	 */
83 	uint32_t buswide_ctxs[(PCI_BUSMAX + 1) / NBBY / sizeof(uint32_t)];
84 };
85 
86 struct iommu_domain_map_ops {
87 	int (*map)(struct iommu_domain *domain, iommu_gaddr_t base,
88 	    iommu_gaddr_t size, vm_page_t *ma, uint64_t pflags, int flags);
89 	int (*unmap)(struct iommu_domain *domain, iommu_gaddr_t base,
90 	    iommu_gaddr_t size, int flags);
91 };
92 
93 /*
94  * Locking annotations:
95  * (u) - Protected by iommu unit lock
96  * (d) - Protected by domain lock
97  * (c) - Immutable after initialization
98  */
99 
100 struct iommu_domain {
101 	struct iommu_unit *iommu;	/* (c) */
102 	const struct iommu_domain_map_ops *ops;
103 	struct mtx lock;		/* (c) */
104 	struct task unload_task;	/* (c) */
105 	u_int entries_cnt;		/* (d) */
106 	struct iommu_map_entries_tailq unload_entries; /* (d) Entries to
107 							 unload */
108 	struct iommu_gas_entries_tree rb_root; /* (d) */
109 	iommu_gaddr_t end;		/* (c) Highest address + 1 in
110 					   the guest AS */
111 	struct iommu_map_entry *first_place, *last_place; /* (d) */
112 	struct iommu_map_entry *msi_entry; /* (d) Arch-specific */
113 	iommu_gaddr_t msi_base;		/* (d) Arch-specific */
114 	vm_paddr_t msi_phys;		/* (d) Arch-specific */
115 	u_int flags;			/* (u) */
116 };
117 
118 struct iommu_ctx {
119 	struct iommu_domain *domain;	/* (c) */
120 	struct bus_dma_tag_iommu *tag;	/* (c) Root tag */
121 	u_long loads;			/* atomic updates, for stat only */
122 	u_long unloads;			/* same */
123 	u_int flags;			/* (u) */
124 	uint16_t rid;			/* (c) pci RID */
125 };
126 
127 /* struct iommu_ctx flags */
128 #define	IOMMU_CTX_FAULTED	0x0001	/* Fault was reported,
129 					   last_fault_rec is valid */
130 #define	IOMMU_CTX_DISABLED	0x0002	/* Device is disabled, the
131 					   ephemeral reference is kept
132 					   to prevent context destruction */
133 
134 #define	IOMMU_DOMAIN_GAS_INITED		0x0001
135 #define	IOMMU_DOMAIN_PGTBL_INITED	0x0002
136 #define	IOMMU_DOMAIN_IDMAP		0x0010	/* Domain uses identity
137 						   page table */
138 #define	IOMMU_DOMAIN_RMRR		0x0020	/* Domain contains RMRR entry,
139 						   cannot be turned off */
140 
141 #define	IOMMU_LOCK(unit)		mtx_lock(&(unit)->lock)
142 #define	IOMMU_UNLOCK(unit)		mtx_unlock(&(unit)->lock)
143 #define	IOMMU_ASSERT_LOCKED(unit)	mtx_assert(&(unit)->lock, MA_OWNED)
144 
145 #define	IOMMU_DOMAIN_LOCK(dom)		mtx_lock(&(dom)->lock)
146 #define	IOMMU_DOMAIN_UNLOCK(dom)	mtx_unlock(&(dom)->lock)
147 #define	IOMMU_DOMAIN_ASSERT_LOCKED(dom)	mtx_assert(&(dom)->lock, MA_OWNED)
148 
149 void iommu_free_ctx(struct iommu_ctx *ctx);
150 void iommu_free_ctx_locked(struct iommu_unit *iommu, struct iommu_ctx *ctx);
151 struct iommu_ctx *iommu_get_ctx(struct iommu_unit *, device_t dev,
152     uint16_t rid, bool id_mapped, bool rmrr_init);
153 struct iommu_unit *iommu_find(device_t dev, bool verbose);
154 void iommu_domain_unload_entry(struct iommu_map_entry *entry, bool free);
155 void iommu_domain_unload(struct iommu_domain *domain,
156     struct iommu_map_entries_tailq *entries, bool cansleep);
157 
158 struct iommu_ctx *iommu_instantiate_ctx(struct iommu_unit *iommu,
159     device_t dev, bool rmrr);
160 device_t iommu_get_requester(device_t dev, uint16_t *rid);
161 int iommu_init_busdma(struct iommu_unit *unit);
162 void iommu_fini_busdma(struct iommu_unit *unit);
163 
164 void iommu_gas_init_domain(struct iommu_domain *domain);
165 void iommu_gas_fini_domain(struct iommu_domain *domain);
166 struct iommu_map_entry *iommu_gas_alloc_entry(struct iommu_domain *domain,
167     u_int flags);
168 void iommu_gas_free_entry(struct iommu_domain *domain,
169     struct iommu_map_entry *entry);
170 void iommu_gas_free_space(struct iommu_domain *domain,
171     struct iommu_map_entry *entry);
172 int iommu_gas_map(struct iommu_domain *domain,
173     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
174     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res);
175 void iommu_gas_free_region(struct iommu_domain *domain,
176     struct iommu_map_entry *entry);
177 int iommu_gas_map_region(struct iommu_domain *domain,
178     struct iommu_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma);
179 int iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start,
180     iommu_gaddr_t end, struct iommu_map_entry **entry0);
181 int iommu_gas_reserve_region_extend(struct iommu_domain *domain,
182     iommu_gaddr_t start, iommu_gaddr_t end);
183 
184 void iommu_set_buswide_ctx(struct iommu_unit *unit, u_int busno);
185 bool iommu_is_buswide_ctx(struct iommu_unit *unit, u_int busno);
186 void iommu_domain_init(struct iommu_unit *unit, struct iommu_domain *domain,
187     const struct iommu_domain_map_ops *ops);
188 void iommu_domain_fini(struct iommu_domain *domain);
189 
190 bool bus_dma_iommu_set_buswide(device_t dev);
191 int bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map,
192     vm_paddr_t start, vm_size_t length, int flags);
193 
194 bus_dma_tag_t iommu_get_dma_tag(device_t dev, device_t child);
195 struct iommu_ctx *iommu_get_dev_ctx(device_t dev);
196 struct iommu_domain *iommu_get_ctx_domain(struct iommu_ctx *ctx);
197 
198 SYSCTL_DECL(_hw_iommu);
199 
200 #endif /* !_DEV_IOMMU_IOMMU_H_ */
201