1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#ifndef INTEL_IOMMU_H
23#define INTEL_IOMMU_H
24
25#include "hw/i386/x86-iommu.h"
26#include "qemu/iova-tree.h"
27#include "qom/object.h"
28
29#define TYPE_INTEL_IOMMU_DEVICE "intel-iommu"
30OBJECT_DECLARE_SIMPLE_TYPE(IntelIOMMUState, INTEL_IOMMU_DEVICE)
31
32#define TYPE_INTEL_IOMMU_MEMORY_REGION "intel-iommu-iommu-memory-region"
33
34
35#define Q35_HOST_BRIDGE_IOMMU_ADDR 0xfed90000ULL
36
37#define VTD_PCI_BUS_MAX 256
38#define VTD_PCI_SLOT_MAX 32
39#define VTD_PCI_FUNC_MAX 8
40#define VTD_PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
41#define VTD_PCI_FUNC(devfn) ((devfn) & 0x07)
42#define VTD_SID_TO_BUS(sid) (((sid) >> 8) & 0xff)
43#define VTD_SID_TO_DEVFN(sid) ((sid) & 0xff)
44
45#define DMAR_REG_SIZE 0x230
46#define VTD_HOST_AW_39BIT 39
47#define VTD_HOST_AW_48BIT 48
48#define VTD_HOST_ADDRESS_WIDTH VTD_HOST_AW_39BIT
49#define VTD_HAW_MASK(aw) ((1ULL << (aw)) - 1)
50
51#define DMAR_REPORT_F_INTR (1)
52
53#define VTD_MSI_ADDR_HI_MASK (0xffffffff00000000ULL)
54#define VTD_MSI_ADDR_HI_SHIFT (32)
55#define VTD_MSI_ADDR_LO_MASK (0x00000000ffffffffULL)
56
57typedef struct VTDContextEntry VTDContextEntry;
58typedef struct VTDContextCacheEntry VTDContextCacheEntry;
59typedef struct VTDAddressSpace VTDAddressSpace;
60typedef struct VTDIOTLBEntry VTDIOTLBEntry;
61typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
62typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
63typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
64typedef struct VTDPASIDEntry VTDPASIDEntry;
65
66
67struct VTDContextEntry {
68 union {
69 struct {
70 uint64_t lo;
71 uint64_t hi;
72 };
73 struct {
74 uint64_t val[4];
75 };
76 };
77};
78
79struct VTDContextCacheEntry {
80
81
82
83 uint32_t context_cache_gen;
84 struct VTDContextEntry context_entry;
85};
86
87
88struct VTDPASIDDirEntry {
89 uint64_t val;
90};
91
92
93struct VTDPASIDEntry {
94 uint64_t val[8];
95};
96
97struct VTDAddressSpace {
98 PCIBus *bus;
99 uint8_t devfn;
100 uint32_t pasid;
101 AddressSpace as;
102 IOMMUMemoryRegion iommu;
103 MemoryRegion root;
104 MemoryRegion nodmar;
105 MemoryRegion iommu_ir;
106 MemoryRegion iommu_ir_fault;
107 IntelIOMMUState *iommu_state;
108 VTDContextCacheEntry context_cache_entry;
109 QLIST_ENTRY(VTDAddressSpace) next;
110
111 IOMMUNotifierFlag notifier_flags;
112 IOVATree *iova_tree;
113};
114
115struct VTDIOTLBEntry {
116 uint64_t gfn;
117 uint16_t domain_id;
118 uint32_t pasid;
119 uint64_t slpte;
120 uint64_t mask;
121 uint8_t access_flags;
122};
123
124
125enum {
126 VTD_SQ_FULL = 0x00,
127 VTD_SQ_IGN_3 = 0x01,
128 VTD_SQ_IGN_2_3 = 0x02,
129 VTD_SQ_IGN_1_3 = 0x03,
130 VTD_SQ_MAX,
131};
132
133
134enum {
135 VTD_SVT_NONE = 0x00,
136 VTD_SVT_ALL = 0x01,
137 VTD_SVT_BUS = 0x02,
138 VTD_SVT_MAX,
139};
140
141
142union VTD_IR_TableEntry {
143 struct {
144#if HOST_BIG_ENDIAN
145 uint32_t __reserved_1:8;
146 uint32_t vector:8;
147 uint32_t irte_mode:1;
148 uint32_t __reserved_0:3;
149 uint32_t __avail:4;
150 uint32_t delivery_mode:3;
151 uint32_t trigger_mode:1;
152 uint32_t redir_hint:1;
153 uint32_t dest_mode:1;
154 uint32_t fault_disable:1;
155 uint32_t present:1;
156#else
157 uint32_t present:1;
158 uint32_t fault_disable:1;
159 uint32_t dest_mode:1;
160 uint32_t redir_hint:1;
161 uint32_t trigger_mode:1;
162 uint32_t delivery_mode:3;
163 uint32_t __avail:4;
164 uint32_t __reserved_0:3;
165 uint32_t irte_mode:1;
166 uint32_t vector:8;
167 uint32_t __reserved_1:8;
168#endif
169 uint32_t dest_id;
170 uint16_t source_id;
171#if HOST_BIG_ENDIAN
172 uint64_t __reserved_2:44;
173 uint64_t sid_vtype:2;
174 uint64_t sid_q:2;
175#else
176 uint64_t sid_q:2;
177 uint64_t sid_vtype:2;
178 uint64_t __reserved_2:44;
179#endif
180 } QEMU_PACKED irte;
181 uint64_t data[2];
182};
183
184#define VTD_IR_INT_FORMAT_COMPAT (0)
185#define VTD_IR_INT_FORMAT_REMAP (1)
186
187
188union VTD_IR_MSIAddress {
189 struct {
190#if HOST_BIG_ENDIAN
191 uint32_t __head:12;
192 uint32_t index_l:15;
193 uint32_t int_mode:1;
194 uint32_t sub_valid:1;
195 uint32_t index_h:1;
196 uint32_t __not_care:2;
197#else
198 uint32_t __not_care:2;
199 uint32_t index_h:1;
200 uint32_t sub_valid:1;
201 uint32_t int_mode:1;
202 uint32_t index_l:15;
203 uint32_t __head:12;
204#endif
205 } QEMU_PACKED addr;
206 uint32_t data;
207};
208
209
210#define VTD_IR_MSI_DATA (0)
211
212
213struct IntelIOMMUState {
214 X86IOMMUState x86_iommu;
215 MemoryRegion csrmem;
216 MemoryRegion mr_nodmar;
217 MemoryRegion mr_ir;
218 MemoryRegion mr_sys_alias;
219 uint8_t csr[DMAR_REG_SIZE];
220 uint8_t wmask[DMAR_REG_SIZE];
221 uint8_t w1cmask[DMAR_REG_SIZE];
222 uint8_t womask[DMAR_REG_SIZE];
223 uint32_t version;
224
225 bool caching_mode;
226 bool scalable_mode;
227 bool snoop_control;
228
229 dma_addr_t root;
230 bool root_scalable;
231 bool dmar_enabled;
232
233 uint16_t iq_head;
234 uint16_t iq_tail;
235 dma_addr_t iq;
236 uint16_t iq_size;
237 bool iq_dw;
238 bool qi_enabled;
239 uint8_t iq_last_desc_type;
240
241
242
243
244 uint16_t next_frcd_reg;
245
246 uint64_t cap;
247 uint64_t ecap;
248
249 uint32_t context_cache_gen;
250 GHashTable *iotlb;
251
252 GHashTable *vtd_address_spaces;
253 VTDAddressSpace *vtd_as_cache[VTD_PCI_BUS_MAX];
254
255 QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
256
257
258 bool intr_enabled;
259 dma_addr_t intr_root;
260 uint32_t intr_size;
261 bool intr_eime;
262 OnOffAuto intr_eim;
263 bool buggy_eim;
264 uint8_t aw_bits;
265 bool dma_drain;
266 bool dma_translation;
267 bool pasid;
268
269
270
271
272
273 QemuMutex iommu_lock;
274};
275
276
277
278
279VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
280 int devfn, unsigned int pasid);
281
282#endif
283