1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#ifndef AMD_IOMMU_H
22#define AMD_IOMMU_H
23
24#include "hw/pci/pci.h"
25#include "hw/i386/x86-iommu.h"
26
27
28#define AMDVI_CAPAB_BAR_LOW 0x04
29#define AMDVI_CAPAB_BAR_HIGH 0x08
30#define AMDVI_CAPAB_RANGE 0x0C
31#define AMDVI_CAPAB_MISC 0x10
32
33#define AMDVI_CAPAB_SIZE 0x18
34#define AMDVI_CAPAB_REG_SIZE 0x04
35
36
37#define AMDVI_CAPAB_ID_SEC 0xf
38#define AMDVI_CAPAB_FLAT_EXT (1 << 28)
39#define AMDVI_CAPAB_EFR_SUP (1 << 27)
40#define AMDVI_CAPAB_FLAG_NPCACHE (1 << 26)
41#define AMDVI_CAPAB_FLAG_HTTUNNEL (1 << 25)
42#define AMDVI_CAPAB_FLAG_IOTLBSUP (1 << 24)
43#define AMDVI_CAPAB_INIT_TYPE (3 << 16)
44
45
46#define AMDVI_MMIO_REGS_HIGH 7
47#define AMDVI_MMIO_REGS_LOW 8
48
49
50#define AMDVI_MMIO_DEVICE_TABLE 0x0000
51#define AMDVI_MMIO_COMMAND_BASE 0x0008
52#define AMDVI_MMIO_EVENT_BASE 0x0010
53#define AMDVI_MMIO_CONTROL 0x0018
54#define AMDVI_MMIO_EXCL_BASE 0x0020
55#define AMDVI_MMIO_EXCL_LIMIT 0x0028
56#define AMDVI_MMIO_EXT_FEATURES 0x0030
57#define AMDVI_MMIO_COMMAND_HEAD 0x2000
58#define AMDVI_MMIO_COMMAND_TAIL 0x2008
59#define AMDVI_MMIO_EVENT_HEAD 0x2010
60#define AMDVI_MMIO_EVENT_TAIL 0x2018
61#define AMDVI_MMIO_STATUS 0x2020
62#define AMDVI_MMIO_PPR_BASE 0x0038
63#define AMDVI_MMIO_PPR_HEAD 0x2030
64#define AMDVI_MMIO_PPR_TAIL 0x2038
65
66#define AMDVI_MMIO_SIZE 0x4000
67
68#define AMDVI_MMIO_DEVTAB_SIZE_MASK ((1ULL << 12) - 1)
69#define AMDVI_MMIO_DEVTAB_BASE_MASK (((1ULL << 52) - 1) & ~ \
70 AMDVI_MMIO_DEVTAB_SIZE_MASK)
71#define AMDVI_MMIO_DEVTAB_ENTRY_SIZE 32
72#define AMDVI_MMIO_DEVTAB_SIZE_UNIT 4096
73
74
75#define AMDVI_MMIO_CMDBUF_SIZE_BYTE (AMDVI_MMIO_COMMAND_BASE + 7)
76#define AMDVI_MMIO_CMDBUF_SIZE_MASK 0x0f
77#define AMDVI_MMIO_CMDBUF_BASE_MASK AMDVI_MMIO_DEVTAB_BASE_MASK
78#define AMDVI_MMIO_CMDBUF_HEAD_MASK (((1ULL << 19) - 1) & ~0x0f)
79#define AMDVI_MMIO_CMDBUF_TAIL_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
80
81#define AMDVI_MMIO_EVTLOG_SIZE_BYTE (AMDVI_MMIO_EVENT_BASE + 7)
82#define AMDVI_MMIO_EVTLOG_SIZE_MASK AMDVI_MMIO_CMDBUF_SIZE_MASK
83#define AMDVI_MMIO_EVTLOG_BASE_MASK AMDVI_MMIO_CMDBUF_BASE_MASK
84#define AMDVI_MMIO_EVTLOG_HEAD_MASK (((1ULL << 19) - 1) & ~0x0f)
85#define AMDVI_MMIO_EVTLOG_TAIL_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
86
87#define AMDVI_MMIO_PPRLOG_SIZE_BYTE (AMDVI_MMIO_EVENT_BASE + 7)
88#define AMDVI_MMIO_PPRLOG_HEAD_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
89#define AMDVI_MMIO_PPRLOG_TAIL_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
90#define AMDVI_MMIO_PPRLOG_BASE_MASK AMDVI_MMIO_EVTLOG_BASE_MASK
91#define AMDVI_MMIO_PPRLOG_SIZE_MASK AMDVI_MMIO_EVTLOG_SIZE_MASK
92
93#define AMDVI_MMIO_EXCL_ENABLED_MASK (1ULL << 0)
94#define AMDVI_MMIO_EXCL_ALLOW_MASK (1ULL << 1)
95#define AMDVI_MMIO_EXCL_LIMIT_MASK AMDVI_MMIO_DEVTAB_BASE_MASK
96#define AMDVI_MMIO_EXCL_LIMIT_LOW 0xfff
97
98
99#define AMDVI_MMIO_CONTROL_AMDVIEN (1ULL << 0)
100#define AMDVI_MMIO_CONTROL_HTTUNEN (1ULL << 1)
101#define AMDVI_MMIO_CONTROL_EVENTLOGEN (1ULL << 2)
102#define AMDVI_MMIO_CONTROL_EVENTINTEN (1ULL << 3)
103#define AMDVI_MMIO_CONTROL_COMWAITINTEN (1ULL << 4)
104#define AMDVI_MMIO_CONTROL_CMDBUFLEN (1ULL << 12)
105#define AMDVI_MMIO_CONTROL_GAEN (1ULL << 17)
106
107
108#define AMDVI_MMIO_STATUS_CMDBUF_RUN (1 << 4)
109#define AMDVI_MMIO_STATUS_EVT_RUN (1 << 3)
110#define AMDVI_MMIO_STATUS_COMP_INT (1 << 2)
111#define AMDVI_MMIO_STATUS_EVT_OVF (1 << 0)
112
113#define AMDVI_CMDBUF_ID_BYTE 0x07
114#define AMDVI_CMDBUF_ID_RSHIFT 4
115
116#define AMDVI_CMD_COMPLETION_WAIT 0x01
117#define AMDVI_CMD_INVAL_DEVTAB_ENTRY 0x02
118#define AMDVI_CMD_INVAL_AMDVI_PAGES 0x03
119#define AMDVI_CMD_INVAL_IOTLB_PAGES 0x04
120#define AMDVI_CMD_INVAL_INTR_TABLE 0x05
121#define AMDVI_CMD_PREFETCH_AMDVI_PAGES 0x06
122#define AMDVI_CMD_COMPLETE_PPR_REQUEST 0x07
123#define AMDVI_CMD_INVAL_AMDVI_ALL 0x08
124
125#define AMDVI_DEVTAB_ENTRY_SIZE 32
126
127
128#define AMDVI_DEV_VALID (1ULL << 0)
129#define AMDVI_DEV_TRANSLATION_VALID (1ULL << 1)
130#define AMDVI_DEV_MODE_MASK 0x7
131#define AMDVI_DEV_MODE_RSHIFT 9
132#define AMDVI_DEV_PT_ROOT_MASK 0xffffffffff000
133#define AMDVI_DEV_PT_ROOT_RSHIFT 12
134#define AMDVI_DEV_PERM_SHIFT 61
135#define AMDVI_DEV_PERM_READ (1ULL << 61)
136#define AMDVI_DEV_PERM_WRITE (1ULL << 62)
137
138
139#define AMDVI_DEV_DOMID_ID_MASK ((1ULL << 16) - 1)
140
141
142#define AMDVI_EVENT_ILLEGAL_DEVTAB_ENTRY (0x1U << 12)
143#define AMDVI_EVENT_IOPF (0x2U << 12)
144#define AMDVI_EVENT_IOPF_I (1U << 3)
145#define AMDVI_EVENT_DEV_TAB_HW_ERROR (0x3U << 12)
146#define AMDVI_EVENT_PAGE_TAB_HW_ERROR (0x4U << 12)
147#define AMDVI_EVENT_ILLEGAL_COMMAND_ERROR (0x5U << 12)
148#define AMDVI_EVENT_COMMAND_HW_ERROR (0x6U << 12)
149
150#define AMDVI_EVENT_LEN 16
151#define AMDVI_PERM_READ (1 << 0)
152#define AMDVI_PERM_WRITE (1 << 1)
153
154#define AMDVI_FEATURE_PREFETCH (1ULL << 0)
155#define AMDVI_FEATURE_PPR (1ULL << 1)
156#define AMDVI_FEATURE_GT (1ULL << 4)
157#define AMDVI_FEATURE_IA (1ULL << 6)
158#define AMDVI_FEATURE_GA (1ULL << 7)
159#define AMDVI_FEATURE_HE (1ULL << 8)
160#define AMDVI_FEATURE_PC (1ULL << 9)
161
162
163#define AMDVI_DTE_LOWER_QUAD_RESERVED 0x80300000000000fc
164#define AMDVI_DTE_MIDDLE_QUAD_RESERVED 0x0000000000000100
165#define AMDVI_DTE_UPPER_QUAD_RESERVED 0x08f0000000000000
166
167
168#define AMDVI_GATS_MODE (2ULL << 12)
169#define AMDVI_HATS_MODE (2ULL << 10)
170
171
172#define AMDVI_IOTLB_MAX_SIZE 1024
173#define AMDVI_DEVID_SHIFT 36
174
175
176#define AMDVI_EXT_FEATURES (AMDVI_FEATURE_PREFETCH | AMDVI_FEATURE_PPR | \
177 AMDVI_FEATURE_IA | AMDVI_FEATURE_GT | AMDVI_FEATURE_HE | \
178 AMDVI_GATS_MODE | AMDVI_HATS_MODE | AMDVI_FEATURE_GA)
179
180
181#define AMDVI_CAPAB_FEATURES (AMDVI_CAPAB_FLAT_EXT | \
182 AMDVI_CAPAB_FLAG_NPCACHE | AMDVI_CAPAB_FLAG_IOTLBSUP \
183 | AMDVI_CAPAB_ID_SEC | AMDVI_CAPAB_INIT_TYPE | \
184 AMDVI_CAPAB_FLAG_HTTUNNEL | AMDVI_CAPAB_EFR_SUP)
185
186
187#define AMDVI_BASE_ADDR 0xfed80000
188
189
190#define AMDVI_PAGE_SHIFT 12
191#define AMDVI_PAGE_SIZE (1ULL << AMDVI_PAGE_SHIFT)
192
193#define AMDVI_PAGE_SHIFT_4K 12
194#define AMDVI_PAGE_MASK_4K (~((1ULL << AMDVI_PAGE_SHIFT_4K) - 1))
195
196#define AMDVI_MAX_VA_ADDR (48UL << 5)
197#define AMDVI_MAX_PH_ADDR (40UL << 8)
198#define AMDVI_MAX_GVA_ADDR (48UL << 15)
199
200
201#define AMDVI_COMPLETION_DATA_SIZE 8
202
203#define AMDVI_COMMAND_SIZE 16
204
205#define AMDVI_COMPLETION_DATA_SIZE 8
206
207#define AMDVI_COMMAND_SIZE 16
208
209#define AMDVI_INT_ADDR_FIRST 0xfee00000
210#define AMDVI_INT_ADDR_LAST 0xfeefffff
211#define AMDVI_INT_ADDR_SIZE (AMDVI_INT_ADDR_LAST - AMDVI_INT_ADDR_FIRST + 1)
212#define AMDVI_MSI_ADDR_HI_MASK (0xffffffff00000000ULL)
213#define AMDVI_MSI_ADDR_LO_MASK (0x00000000ffffffffULL)
214
215
216#define AMDVI_IOAPIC_SB_DEVID PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
217
218
219#define AMDVI_IR_ERR 0x1
220#define AMDVI_IR_GET_IRTE 0x2
221#define AMDVI_IR_TARGET_ABORT 0x3
222
223
224#define AMDVI_IR_REMAP_ENABLE 1ULL
225#define AMDVI_IR_INTCTL_SHIFT 60
226#define AMDVI_IR_INTCTL_ABORT 0
227#define AMDVI_IR_INTCTL_PASS 1
228#define AMDVI_IR_INTCTL_REMAP 2
229
230#define AMDVI_IR_PHYS_ADDR_MASK (((1ULL << 45) - 1) << 6)
231
232
233#define AMDVI_IRTE_OFFSET 0x7ff
234
235
236#define AMDVI_IOAPIC_INT_TYPE_FIXED 0x0
237#define AMDVI_IOAPIC_INT_TYPE_ARBITRATED 0x1
238#define AMDVI_IOAPIC_INT_TYPE_SMI 0x2
239#define AMDVI_IOAPIC_INT_TYPE_NMI 0x4
240#define AMDVI_IOAPIC_INT_TYPE_INIT 0x5
241#define AMDVI_IOAPIC_INT_TYPE_EINT 0x7
242
243
244#define AMDVI_DEV_INT_PASS_MASK (1ULL << 56)
245#define AMDVI_DEV_EINT_PASS_MASK (1ULL << 57)
246#define AMDVI_DEV_NMI_PASS_MASK (1ULL << 58)
247#define AMDVI_DEV_LINT0_PASS_MASK (1ULL << 62)
248#define AMDVI_DEV_LINT1_PASS_MASK (1ULL << 63)
249
250
251union irte {
252 uint32_t val;
253 struct {
254 uint32_t valid:1,
255 no_fault:1,
256 int_type:3,
257 rq_eoi:1,
258 dm:1,
259 guest_mode:1,
260 destination:8,
261 vector:8,
262 rsvd:8;
263 } fields;
264};
265
266
267union irte_ga_lo {
268 uint64_t val;
269
270
271 struct {
272 uint64_t valid:1,
273 no_fault:1,
274
275 int_type:3,
276 rq_eoi:1,
277 dm:1,
278
279 guest_mode:1,
280 destination:8,
281 rsvd_1:48;
282 } fields_remap;
283};
284
285union irte_ga_hi {
286 uint64_t val;
287 struct {
288 uint64_t vector:8,
289 rsvd_2:56;
290 } fields;
291};
292
293struct irte_ga {
294 union irte_ga_lo lo;
295 union irte_ga_hi hi;
296};
297
298#define TYPE_AMD_IOMMU_DEVICE "amd-iommu"
299#define AMD_IOMMU_DEVICE(obj)\
300 OBJECT_CHECK(AMDVIState, (obj), TYPE_AMD_IOMMU_DEVICE)
301
302#define TYPE_AMD_IOMMU_PCI "AMDVI-PCI"
303
304#define TYPE_AMD_IOMMU_MEMORY_REGION "amd-iommu-iommu-memory-region"
305
306typedef struct AMDVIAddressSpace AMDVIAddressSpace;
307
308
309typedef struct AMDVIPCIState {
310 PCIDevice dev;
311} AMDVIPCIState;
312
313typedef struct AMDVIState {
314 X86IOMMUState iommu;
315 AMDVIPCIState pci;
316
317 uint32_t version;
318 uint32_t capab_offset;
319
320 uint64_t mmio_addr;
321
322 uint32_t devid;
323
324 bool enabled;
325 bool ats_enabled;
326 bool cmdbuf_enabled;
327 bool evtlog_enabled;
328 bool excl_enabled;
329
330 hwaddr devtab;
331 size_t devtab_len;
332
333 hwaddr cmdbuf;
334 uint64_t cmdbuf_len;
335 uint32_t cmdbuf_head;
336 uint32_t cmdbuf_tail;
337 bool completion_wait_intr;
338
339 hwaddr evtlog;
340 bool evtlog_intr;
341 uint32_t evtlog_len;
342 uint32_t evtlog_head;
343 uint32_t evtlog_tail;
344
345
346 hwaddr excl_base;
347 hwaddr excl_limit;
348 bool excl_allow;
349 bool excl_enable;
350
351 hwaddr ppr_log;
352 uint32_t pprlog_len;
353 uint32_t pprlog_head;
354 uint32_t pprlog_tail;
355
356 MemoryRegion mmio;
357 uint8_t mmior[AMDVI_MMIO_SIZE];
358 uint8_t w1cmask[AMDVI_MMIO_SIZE];
359 uint8_t romask[AMDVI_MMIO_SIZE];
360 bool mmio_enabled;
361
362
363 AMDVIAddressSpace **address_spaces[PCI_BUS_MAX];
364
365
366 GHashTable *iotlb;
367
368
369 bool ga_enabled;
370} AMDVIState;
371
372#endif
373