1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 * Leo Duran <leo.duran@amd.com>
6 */
7
8 #ifndef _ASM_X86_AMD_IOMMU_TYPES_H
9 #define _ASM_X86_AMD_IOMMU_TYPES_H
10
11 #include <linux/types.h>
12 #include <linux/mutex.h>
13 #include <linux/msi.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/pci.h>
17 #include <linux/irqreturn.h>
18 #include <linux/io-pgtable.h>
19
20 /*
21 * Maximum number of IOMMUs supported
22 */
23 #define MAX_IOMMUS 32
24
25 /*
26 * some size calculation constants
27 */
28 #define DEV_TABLE_ENTRY_SIZE 32
29 #define ALIAS_TABLE_ENTRY_SIZE 2
30 #define RLOOKUP_TABLE_ENTRY_SIZE (sizeof(void *))
31
32 /* Capability offsets used by the driver */
33 #define MMIO_CAP_HDR_OFFSET 0x00
34 #define MMIO_RANGE_OFFSET 0x0c
35 #define MMIO_MISC_OFFSET 0x10
36
37 /* Masks, shifts and macros to parse the device range capability */
38 #define MMIO_RANGE_LD_MASK 0xff000000
39 #define MMIO_RANGE_FD_MASK 0x00ff0000
40 #define MMIO_RANGE_BUS_MASK 0x0000ff00
41 #define MMIO_RANGE_LD_SHIFT 24
42 #define MMIO_RANGE_FD_SHIFT 16
43 #define MMIO_RANGE_BUS_SHIFT 8
44 #define MMIO_GET_LD(x) (((x) & MMIO_RANGE_LD_MASK) >> MMIO_RANGE_LD_SHIFT)
45 #define MMIO_GET_FD(x) (((x) & MMIO_RANGE_FD_MASK) >> MMIO_RANGE_FD_SHIFT)
46 #define MMIO_GET_BUS(x) (((x) & MMIO_RANGE_BUS_MASK) >> MMIO_RANGE_BUS_SHIFT)
47 #define MMIO_MSI_NUM(x) ((x) & 0x1f)
48
49 /* Flag masks for the AMD IOMMU exclusion range */
50 #define MMIO_EXCL_ENABLE_MASK 0x01ULL
51 #define MMIO_EXCL_ALLOW_MASK 0x02ULL
52
53 /* Used offsets into the MMIO space */
54 #define MMIO_DEV_TABLE_OFFSET 0x0000
55 #define MMIO_CMD_BUF_OFFSET 0x0008
56 #define MMIO_EVT_BUF_OFFSET 0x0010
57 #define MMIO_CONTROL_OFFSET 0x0018
58 #define MMIO_EXCL_BASE_OFFSET 0x0020
59 #define MMIO_EXCL_LIMIT_OFFSET 0x0028
60 #define MMIO_EXT_FEATURES 0x0030
61 #define MMIO_PPR_LOG_OFFSET 0x0038
62 #define MMIO_GA_LOG_BASE_OFFSET 0x00e0
63 #define MMIO_GA_LOG_TAIL_OFFSET 0x00e8
64 #define MMIO_MSI_ADDR_LO_OFFSET 0x015C
65 #define MMIO_MSI_ADDR_HI_OFFSET 0x0160
66 #define MMIO_MSI_DATA_OFFSET 0x0164
67 #define MMIO_INTCAPXT_EVT_OFFSET 0x0170
68 #define MMIO_INTCAPXT_PPR_OFFSET 0x0178
69 #define MMIO_INTCAPXT_GALOG_OFFSET 0x0180
70 #define MMIO_EXT_FEATURES2 0x01A0
71 #define MMIO_CMD_HEAD_OFFSET 0x2000
72 #define MMIO_CMD_TAIL_OFFSET 0x2008
73 #define MMIO_EVT_HEAD_OFFSET 0x2010
74 #define MMIO_EVT_TAIL_OFFSET 0x2018
75 #define MMIO_STATUS_OFFSET 0x2020
76 #define MMIO_PPR_HEAD_OFFSET 0x2030
77 #define MMIO_PPR_TAIL_OFFSET 0x2038
78 #define MMIO_GA_HEAD_OFFSET 0x2040
79 #define MMIO_GA_TAIL_OFFSET 0x2048
80 #define MMIO_CNTR_CONF_OFFSET 0x4000
81 #define MMIO_CNTR_REG_OFFSET 0x40000
82 #define MMIO_REG_END_OFFSET 0x80000
83
84
85
86 /* Extended Feature Bits */
87 #define FEATURE_PREFETCH (1ULL<<0)
88 #define FEATURE_PPR (1ULL<<1)
89 #define FEATURE_X2APIC (1ULL<<2)
90 #define FEATURE_NX (1ULL<<3)
91 #define FEATURE_GT (1ULL<<4)
92 #define FEATURE_IA (1ULL<<6)
93 #define FEATURE_GA (1ULL<<7)
94 #define FEATURE_HE (1ULL<<8)
95 #define FEATURE_PC (1ULL<<9)
96 #define FEATURE_GAM_VAPIC (1ULL<<21)
97 #define FEATURE_GIOSUP (1ULL<<48)
98 #define FEATURE_EPHSUP (1ULL<<50)
99 #define FEATURE_SNP (1ULL<<63)
100
101 #define FEATURE_PASID_SHIFT 32
102 #define FEATURE_PASID_MASK (0x1fULL << FEATURE_PASID_SHIFT)
103
104 #define FEATURE_GLXVAL_SHIFT 14
105 #define FEATURE_GLXVAL_MASK (0x03ULL << FEATURE_GLXVAL_SHIFT)
106
107 /* Extended Feature 2 Bits */
108 #define FEATURE_SNPAVICSUP_SHIFT 5
109 #define FEATURE_SNPAVICSUP_MASK (0x07ULL << FEATURE_SNPAVICSUP_SHIFT)
110 #define FEATURE_SNPAVICSUP_GAM(x) \
111 ((x & FEATURE_SNPAVICSUP_MASK) >> FEATURE_SNPAVICSUP_SHIFT == 0x1)
112
113 /* Note:
114 * The current driver only support 16-bit PASID.
115 * Currently, hardware only implement upto 16-bit PASID
116 * even though the spec says it could have upto 20 bits.
117 */
118 #define PASID_MASK 0x0000ffff
119
120 /* MMIO status bits */
121 #define MMIO_STATUS_EVT_OVERFLOW_INT_MASK (1 << 0)
122 #define MMIO_STATUS_EVT_INT_MASK (1 << 1)
123 #define MMIO_STATUS_COM_WAIT_INT_MASK (1 << 2)
124 #define MMIO_STATUS_PPR_INT_MASK (1 << 6)
125 #define MMIO_STATUS_GALOG_RUN_MASK (1 << 8)
126 #define MMIO_STATUS_GALOG_OVERFLOW_MASK (1 << 9)
127 #define MMIO_STATUS_GALOG_INT_MASK (1 << 10)
128
129 /* event logging constants */
130 #define EVENT_ENTRY_SIZE 0x10
131 #define EVENT_TYPE_SHIFT 28
132 #define EVENT_TYPE_MASK 0xf
133 #define EVENT_TYPE_ILL_DEV 0x1
134 #define EVENT_TYPE_IO_FAULT 0x2
135 #define EVENT_TYPE_DEV_TAB_ERR 0x3
136 #define EVENT_TYPE_PAGE_TAB_ERR 0x4
137 #define EVENT_TYPE_ILL_CMD 0x5
138 #define EVENT_TYPE_CMD_HARD_ERR 0x6
139 #define EVENT_TYPE_IOTLB_INV_TO 0x7
140 #define EVENT_TYPE_INV_DEV_REQ 0x8
141 #define EVENT_TYPE_INV_PPR_REQ 0x9
142 #define EVENT_TYPE_RMP_FAULT 0xd
143 #define EVENT_TYPE_RMP_HW_ERR 0xe
144 #define EVENT_DEVID_MASK 0xffff
145 #define EVENT_DEVID_SHIFT 0
146 #define EVENT_DOMID_MASK_LO 0xffff
147 #define EVENT_DOMID_MASK_HI 0xf0000
148 #define EVENT_FLAGS_MASK 0xfff
149 #define EVENT_FLAGS_SHIFT 0x10
150 #define EVENT_FLAG_RW 0x020
151 #define EVENT_FLAG_I 0x008
152
153 /* feature control bits */
154 #define CONTROL_IOMMU_EN 0
155 #define CONTROL_HT_TUN_EN 1
156 #define CONTROL_EVT_LOG_EN 2
157 #define CONTROL_EVT_INT_EN 3
158 #define CONTROL_COMWAIT_EN 4
159 #define CONTROL_INV_TIMEOUT 5
160 #define CONTROL_PASSPW_EN 8
161 #define CONTROL_RESPASSPW_EN 9
162 #define CONTROL_COHERENT_EN 10
163 #define CONTROL_ISOC_EN 11
164 #define CONTROL_CMDBUF_EN 12
165 #define CONTROL_PPRLOG_EN 13
166 #define CONTROL_PPRINT_EN 14
167 #define CONTROL_PPR_EN 15
168 #define CONTROL_GT_EN 16
169 #define CONTROL_GA_EN 17
170 #define CONTROL_GAM_EN 25
171 #define CONTROL_GALOG_EN 28
172 #define CONTROL_GAINT_EN 29
173 #define CONTROL_XT_EN 50
174 #define CONTROL_INTCAPXT_EN 51
175 #define CONTROL_IRTCACHEDIS 59
176 #define CONTROL_SNPAVIC_EN 61
177
178 #define CTRL_INV_TO_MASK (7 << CONTROL_INV_TIMEOUT)
179 #define CTRL_INV_TO_NONE 0
180 #define CTRL_INV_TO_1MS 1
181 #define CTRL_INV_TO_10MS 2
182 #define CTRL_INV_TO_100MS 3
183 #define CTRL_INV_TO_1S 4
184 #define CTRL_INV_TO_10S 5
185 #define CTRL_INV_TO_100S 6
186
187 /* command specific defines */
188 #define CMD_COMPL_WAIT 0x01
189 #define CMD_INV_DEV_ENTRY 0x02
190 #define CMD_INV_IOMMU_PAGES 0x03
191 #define CMD_INV_IOTLB_PAGES 0x04
192 #define CMD_INV_IRT 0x05
193 #define CMD_COMPLETE_PPR 0x07
194 #define CMD_INV_ALL 0x08
195
196 #define CMD_COMPL_WAIT_STORE_MASK 0x01
197 #define CMD_COMPL_WAIT_INT_MASK 0x02
198 #define CMD_INV_IOMMU_PAGES_SIZE_MASK 0x01
199 #define CMD_INV_IOMMU_PAGES_PDE_MASK 0x02
200 #define CMD_INV_IOMMU_PAGES_GN_MASK 0x04
201
202 #define PPR_STATUS_MASK 0xf
203 #define PPR_STATUS_SHIFT 12
204
205 #define CMD_INV_IOMMU_ALL_PAGES_ADDRESS 0x7fffffffffffffffULL
206
207 /* macros and definitions for device table entries */
208 #define DEV_ENTRY_VALID 0x00
209 #define DEV_ENTRY_TRANSLATION 0x01
210 #define DEV_ENTRY_PPR 0x34
211 #define DEV_ENTRY_IR 0x3d
212 #define DEV_ENTRY_IW 0x3e
213 #define DEV_ENTRY_NO_PAGE_FAULT 0x62
214 #define DEV_ENTRY_EX 0x67
215 #define DEV_ENTRY_SYSMGT1 0x68
216 #define DEV_ENTRY_SYSMGT2 0x69
217 #define DEV_ENTRY_IRQ_TBL_EN 0x80
218 #define DEV_ENTRY_INIT_PASS 0xb8
219 #define DEV_ENTRY_EINT_PASS 0xb9
220 #define DEV_ENTRY_NMI_PASS 0xba
221 #define DEV_ENTRY_LINT0_PASS 0xbe
222 #define DEV_ENTRY_LINT1_PASS 0xbf
223 #define DEV_ENTRY_MODE_MASK 0x07
224 #define DEV_ENTRY_MODE_SHIFT 0x09
225
226 #define MAX_DEV_TABLE_ENTRIES 0xffff
227
228 /* constants to configure the command buffer */
229 #define CMD_BUFFER_SIZE 8192
230 #define CMD_BUFFER_UNINITIALIZED 1
231 #define CMD_BUFFER_ENTRIES 512
232 #define MMIO_CMD_SIZE_SHIFT 56
233 #define MMIO_CMD_SIZE_512 (0x9ULL << MMIO_CMD_SIZE_SHIFT)
234
235 /* constants for event buffer handling */
236 #define EVT_BUFFER_SIZE 8192 /* 512 entries */
237 #define EVT_LEN_MASK (0x9ULL << 56)
238
239 /* Constants for PPR Log handling */
240 #define PPR_LOG_ENTRIES 512
241 #define PPR_LOG_SIZE_SHIFT 56
242 #define PPR_LOG_SIZE_512 (0x9ULL << PPR_LOG_SIZE_SHIFT)
243 #define PPR_ENTRY_SIZE 16
244 #define PPR_LOG_SIZE (PPR_ENTRY_SIZE * PPR_LOG_ENTRIES)
245
246 #define PPR_REQ_TYPE(x) (((x) >> 60) & 0xfULL)
247 #define PPR_FLAGS(x) (((x) >> 48) & 0xfffULL)
248 #define PPR_DEVID(x) ((x) & 0xffffULL)
249 #define PPR_TAG(x) (((x) >> 32) & 0x3ffULL)
250 #define PPR_PASID1(x) (((x) >> 16) & 0xffffULL)
251 #define PPR_PASID2(x) (((x) >> 42) & 0xfULL)
252 #define PPR_PASID(x) ((PPR_PASID2(x) << 16) | PPR_PASID1(x))
253
254 #define PPR_REQ_FAULT 0x01
255
256 /* Constants for GA Log handling */
257 #define GA_LOG_ENTRIES 512
258 #define GA_LOG_SIZE_SHIFT 56
259 #define GA_LOG_SIZE_512 (0x8ULL << GA_LOG_SIZE_SHIFT)
260 #define GA_ENTRY_SIZE 8
261 #define GA_LOG_SIZE (GA_ENTRY_SIZE * GA_LOG_ENTRIES)
262
263 #define GA_TAG(x) (u32)(x & 0xffffffffULL)
264 #define GA_DEVID(x) (u16)(((x) >> 32) & 0xffffULL)
265 #define GA_REQ_TYPE(x) (((x) >> 60) & 0xfULL)
266
267 #define GA_GUEST_NR 0x1
268
269 #define IOMMU_IN_ADDR_BIT_SIZE 52
270 #define IOMMU_OUT_ADDR_BIT_SIZE 52
271
272 /*
273 * This bitmap is used to advertise the page sizes our hardware support
274 * to the IOMMU core, which will then use this information to split
275 * physically contiguous memory regions it is mapping into page sizes
276 * that we support.
277 *
278 * 512GB Pages are not supported due to a hardware bug
279 */
280 #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38))
281 /* 4K, 2MB, 1G page sizes are supported */
282 #define AMD_IOMMU_PGSIZES_V2 (PAGE_SIZE | (1ULL << 21) | (1ULL << 30))
283
284 /* Bit value definition for dte irq remapping fields*/
285 #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
286 #define DTE_IRQ_REMAP_INTCTL_MASK (0x3ULL << 60)
287 #define DTE_IRQ_REMAP_INTCTL (2ULL << 60)
288 #define DTE_IRQ_REMAP_ENABLE 1ULL
289
290 /*
291 * AMD IOMMU hardware only support 512 IRTEs despite
292 * the architectural limitation of 2048 entries.
293 */
294 #define DTE_INTTAB_ALIGNMENT 128
295 #define DTE_INTTABLEN_VALUE 9ULL
296 #define DTE_INTTABLEN (DTE_INTTABLEN_VALUE << 1)
297 #define DTE_INTTABLEN_MASK (0xfULL << 1)
298 #define MAX_IRQS_PER_TABLE (1 << DTE_INTTABLEN_VALUE)
299
300 #define PAGE_MODE_NONE 0x00
301 #define PAGE_MODE_1_LEVEL 0x01
302 #define PAGE_MODE_2_LEVEL 0x02
303 #define PAGE_MODE_3_LEVEL 0x03
304 #define PAGE_MODE_4_LEVEL 0x04
305 #define PAGE_MODE_5_LEVEL 0x05
306 #define PAGE_MODE_6_LEVEL 0x06
307 #define PAGE_MODE_7_LEVEL 0x07
308
309 #define PM_LEVEL_SHIFT(x) (12 + ((x) * 9))
310 #define PM_LEVEL_SIZE(x) (((x) < 6) ? \
311 ((1ULL << PM_LEVEL_SHIFT((x))) - 1): \
312 (0xffffffffffffffffULL))
313 #define PM_LEVEL_INDEX(x, a) (((a) >> PM_LEVEL_SHIFT((x))) & 0x1ffULL)
314 #define PM_LEVEL_ENC(x) (((x) << 9) & 0xe00ULL)
315 #define PM_LEVEL_PDE(x, a) ((a) | PM_LEVEL_ENC((x)) | \
316 IOMMU_PTE_PR | IOMMU_PTE_IR | IOMMU_PTE_IW)
317 #define PM_PTE_LEVEL(pte) (((pte) >> 9) & 0x7ULL)
318
319 #define PM_MAP_4k 0
320 #define PM_ADDR_MASK 0x000ffffffffff000ULL
321 #define PM_MAP_MASK(lvl) (PM_ADDR_MASK & \
322 (~((1ULL << (12 + ((lvl) * 9))) - 1)))
323 #define PM_ALIGNED(lvl, addr) ((PM_MAP_MASK(lvl) & (addr)) == (addr))
324
325 /*
326 * Returns the page table level to use for a given page size
327 * Pagesize is expected to be a power-of-two
328 */
329 #define PAGE_SIZE_LEVEL(pagesize) \
330 ((__ffs(pagesize) - 12) / 9)
331 /*
332 * Returns the number of ptes to use for a given page size
333 * Pagesize is expected to be a power-of-two
334 */
335 #define PAGE_SIZE_PTE_COUNT(pagesize) \
336 (1ULL << ((__ffs(pagesize) - 12) % 9))
337
338 /*
339 * Aligns a given io-virtual address to a given page size
340 * Pagesize is expected to be a power-of-two
341 */
342 #define PAGE_SIZE_ALIGN(address, pagesize) \
343 ((address) & ~((pagesize) - 1))
344 /*
345 * Creates an IOMMU PTE for an address and a given pagesize
346 * The PTE has no permission bits set
347 * Pagesize is expected to be a power-of-two larger than 4096
348 */
349 #define PAGE_SIZE_PTE(address, pagesize) \
350 (((address) | ((pagesize) - 1)) & \
351 (~(pagesize >> 1)) & PM_ADDR_MASK)
352
353 /*
354 * Takes a PTE value with mode=0x07 and returns the page size it maps
355 */
356 #define PTE_PAGE_SIZE(pte) \
357 (1ULL << (1 + ffz(((pte) | 0xfffULL))))
358
359 /*
360 * Takes a page-table level and returns the default page-size for this level
361 */
362 #define PTE_LEVEL_PAGE_SIZE(level) \
363 (1ULL << (12 + (9 * (level))))
364
365 /*
366 * Bit value definition for I/O PTE fields
367 */
368 #define IOMMU_PTE_PR (1ULL << 0)
369 #define IOMMU_PTE_U (1ULL << 59)
370 #define IOMMU_PTE_FC (1ULL << 60)
371 #define IOMMU_PTE_IR (1ULL << 61)
372 #define IOMMU_PTE_IW (1ULL << 62)
373
374 /*
375 * Bit value definition for DTE fields
376 */
377 #define DTE_FLAG_V (1ULL << 0)
378 #define DTE_FLAG_TV (1ULL << 1)
379 #define DTE_FLAG_IR (1ULL << 61)
380 #define DTE_FLAG_IW (1ULL << 62)
381
382 #define DTE_FLAG_IOTLB (1ULL << 32)
383 #define DTE_FLAG_GIOV (1ULL << 54)
384 #define DTE_FLAG_GV (1ULL << 55)
385 #define DTE_FLAG_MASK (0x3ffULL << 32)
386 #define DTE_GLX_SHIFT (56)
387 #define DTE_GLX_MASK (3)
388 #define DEV_DOMID_MASK 0xffffULL
389
390 #define DTE_GCR3_VAL_A(x) (((x) >> 12) & 0x00007ULL)
391 #define DTE_GCR3_VAL_B(x) (((x) >> 15) & 0x0ffffULL)
392 #define DTE_GCR3_VAL_C(x) (((x) >> 31) & 0x1fffffULL)
393
394 #define DTE_GCR3_INDEX_A 0
395 #define DTE_GCR3_INDEX_B 1
396 #define DTE_GCR3_INDEX_C 1
397
398 #define DTE_GCR3_SHIFT_A 58
399 #define DTE_GCR3_SHIFT_B 16
400 #define DTE_GCR3_SHIFT_C 43
401
402 #define GCR3_VALID 0x01ULL
403
404 #define IOMMU_PAGE_MASK (((1ULL << 52) - 1) & ~0xfffULL)
405 #define IOMMU_PTE_PRESENT(pte) ((pte) & IOMMU_PTE_PR)
406 #define IOMMU_PTE_PAGE(pte) (iommu_phys_to_virt((pte) & IOMMU_PAGE_MASK))
407 #define IOMMU_PTE_MODE(pte) (((pte) >> 9) & 0x07)
408
409 #define IOMMU_PROT_MASK 0x03
410 #define IOMMU_PROT_IR 0x01
411 #define IOMMU_PROT_IW 0x02
412
413 #define IOMMU_UNITY_MAP_FLAG_EXCL_RANGE (1 << 2)
414
415 /* IOMMU capabilities */
416 #define IOMMU_CAP_IOTLB 24
417 #define IOMMU_CAP_NPCACHE 26
418 #define IOMMU_CAP_EFR 27
419
420 /* IOMMU IVINFO */
421 #define IOMMU_IVINFO_OFFSET 36
422 #define IOMMU_IVINFO_EFRSUP BIT(0)
423 #define IOMMU_IVINFO_DMA_REMAP BIT(1)
424
425 /* IOMMU Feature Reporting Field (for IVHD type 10h */
426 #define IOMMU_FEAT_GASUP_SHIFT 6
427
428 /* IOMMU Extended Feature Register (EFR) */
429 #define IOMMU_EFR_XTSUP_SHIFT 2
430 #define IOMMU_EFR_GASUP_SHIFT 7
431 #define IOMMU_EFR_MSICAPMMIOSUP_SHIFT 46
432
433 #define MAX_DOMAIN_ID 65536
434
435 /* Protection domain flags */
436 #define PD_DMA_OPS_MASK (1UL << 0) /* domain used for dma_ops */
437 #define PD_DEFAULT_MASK (1UL << 1) /* domain is a default dma_ops
438 domain for an IOMMU */
439 #define PD_PASSTHROUGH_MASK (1UL << 2) /* domain has no page
440 translation */
441 #define PD_IOMMUV2_MASK (1UL << 3) /* domain has gcr3 table */
442 #define PD_GIOV_MASK (1UL << 4) /* domain enable GIOV support */
443
444 extern bool amd_iommu_dump;
445 #define DUMP_printk(format, arg...) \
446 do { \
447 if (amd_iommu_dump) \
448 pr_info("AMD-Vi: " format, ## arg); \
449 } while(0);
450
451 /* global flag if IOMMUs cache non-present entries */
452 extern bool amd_iommu_np_cache;
453 /* Only true if all IOMMUs support device IOTLBs */
454 extern bool amd_iommu_iotlb_sup;
455
456 struct irq_remap_table {
457 raw_spinlock_t lock;
458 unsigned min_index;
459 u32 *table;
460 };
461
462 /* Interrupt remapping feature used? */
463 extern bool amd_iommu_irq_remap;
464
465 extern const struct iommu_ops amd_iommu_ops;
466
467 /* IVRS indicates that pre-boot remapping was enabled */
468 extern bool amdr_ivrs_remap_support;
469
470 /* kmem_cache to get tables with 128 byte alignement */
471 extern struct kmem_cache *amd_iommu_irq_cache;
472
473 #define PCI_SBDF_TO_SEGID(sbdf) (((sbdf) >> 16) & 0xffff)
474 #define PCI_SBDF_TO_DEVID(sbdf) ((sbdf) & 0xffff)
475 #define PCI_SEG_DEVID_TO_SBDF(seg, devid) ((((u32)(seg) & 0xffff) << 16) | \
476 ((devid) & 0xffff))
477
478 /* Make iterating over all pci segment easier */
479 #define for_each_pci_segment(pci_seg) \
480 list_for_each_entry((pci_seg), &amd_iommu_pci_seg_list, list)
481 #define for_each_pci_segment_safe(pci_seg, next) \
482 list_for_each_entry_safe((pci_seg), (next), &amd_iommu_pci_seg_list, list)
483 /*
484 * Make iterating over all IOMMUs easier
485 */
486 #define for_each_iommu(iommu) \
487 list_for_each_entry((iommu), &amd_iommu_list, list)
488 #define for_each_iommu_safe(iommu, next) \
489 list_for_each_entry_safe((iommu), (next), &amd_iommu_list, list)
490
491 #define APERTURE_RANGE_SHIFT 27 /* 128 MB */
492 #define APERTURE_RANGE_SIZE (1ULL << APERTURE_RANGE_SHIFT)
493 #define APERTURE_RANGE_PAGES (APERTURE_RANGE_SIZE >> PAGE_SHIFT)
494 #define APERTURE_MAX_RANGES 32 /* allows 4GB of DMA address space */
495 #define APERTURE_RANGE_INDEX(a) ((a) >> APERTURE_RANGE_SHIFT)
496 #define APERTURE_PAGE_INDEX(a) (((a) >> 21) & 0x3fULL)
497
498 /*
499 * This struct is used to pass information about
500 * incoming PPR faults around.
501 */
502 struct amd_iommu_fault {
503 u64 address; /* IO virtual address of the fault*/
504 u32 pasid; /* Address space identifier */
505 u32 sbdf; /* Originating PCI device id */
506 u16 tag; /* PPR tag */
507 u16 flags; /* Fault flags */
508
509 };
510
511
512 struct amd_iommu;
513 struct iommu_domain;
514 struct irq_domain;
515 struct amd_irte_ops;
516
517 #define AMD_IOMMU_FLAG_TRANS_PRE_ENABLED (1 << 0)
518
519 #define io_pgtable_to_data(x) \
520 container_of((x), struct amd_io_pgtable, iop)
521
522 #define io_pgtable_ops_to_data(x) \
523 io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
524
525 #define io_pgtable_ops_to_domain(x) \
526 container_of(io_pgtable_ops_to_data(x), \
527 struct protection_domain, iop)
528
529 #define io_pgtable_cfg_to_data(x) \
530 container_of((x), struct amd_io_pgtable, pgtbl_cfg)
531
532 struct amd_io_pgtable {
533 struct io_pgtable_cfg pgtbl_cfg;
534 struct io_pgtable iop;
535 int mode;
536 u64 *root;
537 atomic64_t pt_root; /* pgtable root and pgtable mode */
538 u64 *pgd; /* v2 pgtable pgd pointer */
539 };
540
541 /*
542 * This structure contains generic data for IOMMU protection domains
543 * independent of their use.
544 */
545 struct protection_domain {
546 struct list_head dev_list; /* List of all devices in this domain */
547 struct iommu_domain domain; /* generic domain handle used by
548 iommu core code */
549 struct amd_io_pgtable iop;
550 spinlock_t lock; /* mostly used to lock the page table*/
551 u16 id; /* the domain id written to the device table */
552 int glx; /* Number of levels for GCR3 table */
553 u64 *gcr3_tbl; /* Guest CR3 table */
554 unsigned long flags; /* flags to find out type of domain */
555 unsigned dev_cnt; /* devices assigned to this domain */
556 unsigned dev_iommu[MAX_IOMMUS]; /* per-IOMMU reference count */
557 };
558
559 /*
560 * This structure contains information about one PCI segment in the system.
561 */
562 struct amd_iommu_pci_seg {
563 /* List with all PCI segments in the system */
564 struct list_head list;
565
566 /* List of all available dev_data structures */
567 struct llist_head dev_data_list;
568
569 /* PCI segment number */
570 u16 id;
571
572 /* Largest PCI device id we expect translation requests for */
573 u16 last_bdf;
574
575 /* Size of the device table */
576 u32 dev_table_size;
577
578 /* Size of the alias table */
579 u32 alias_table_size;
580
581 /* Size of the rlookup table */
582 u32 rlookup_table_size;
583
584 /*
585 * device table virtual address
586 *
587 * Pointer to the per PCI segment device table.
588 * It is indexed by the PCI device id or the HT unit id and contains
589 * information about the domain the device belongs to as well as the
590 * page table root pointer.
591 */
592 struct dev_table_entry *dev_table;
593
594 /*
595 * The rlookup iommu table is used to find the IOMMU which is
596 * responsible for a specific device. It is indexed by the PCI
597 * device id.
598 */
599 struct amd_iommu **rlookup_table;
600
601 /*
602 * This table is used to find the irq remapping table for a given
603 * device id quickly.
604 */
605 struct irq_remap_table **irq_lookup_table;
606
607 /*
608 * Pointer to a device table which the content of old device table
609 * will be copied to. It's only be used in kdump kernel.
610 */
611 struct dev_table_entry *old_dev_tbl_cpy;
612
613 /*
614 * The alias table is a driver specific data structure which contains the
615 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
616 * More than one device can share the same requestor id.
617 */
618 u16 *alias_table;
619
620 /*
621 * A list of required unity mappings we find in ACPI. It is not locked
622 * because as runtime it is only read. It is created at ACPI table
623 * parsing time.
624 */
625 struct list_head unity_map;
626 };
627
628 /*
629 * Structure where we save information about one hardware AMD IOMMU in the
630 * system.
631 */
632 struct amd_iommu {
633 struct list_head list;
634
635 /* Index within the IOMMU array */
636 int index;
637
638 /* locks the accesses to the hardware */
639 raw_spinlock_t lock;
640
641 /* Pointer to PCI device of this IOMMU */
642 struct pci_dev *dev;
643
644 /* Cache pdev to root device for resume quirks */
645 struct pci_dev *root_pdev;
646
647 /* physical address of MMIO space */
648 u64 mmio_phys;
649
650 /* physical end address of MMIO space */
651 u64 mmio_phys_end;
652
653 /* virtual address of MMIO space */
654 u8 __iomem *mmio_base;
655
656 /* capabilities of that IOMMU read from ACPI */
657 u32 cap;
658
659 /* flags read from acpi table */
660 u8 acpi_flags;
661
662 /* Extended features */
663 u64 features;
664
665 /* Extended features 2 */
666 u64 features2;
667
668 /* IOMMUv2 */
669 bool is_iommu_v2;
670
671 /* PCI device id of the IOMMU device */
672 u16 devid;
673
674 /*
675 * Capability pointer. There could be more than one IOMMU per PCI
676 * device function if there are more than one AMD IOMMU capability
677 * pointers.
678 */
679 u16 cap_ptr;
680
681 /* pci domain of this IOMMU */
682 struct amd_iommu_pci_seg *pci_seg;
683
684 /* start of exclusion range of that IOMMU */
685 u64 exclusion_start;
686 /* length of exclusion range of that IOMMU */
687 u64 exclusion_length;
688
689 /* command buffer virtual address */
690 u8 *cmd_buf;
691 u32 cmd_buf_head;
692 u32 cmd_buf_tail;
693
694 /* event buffer virtual address */
695 u8 *evt_buf;
696
697 /* Base of the PPR log, if present */
698 u8 *ppr_log;
699
700 /* Base of the GA log, if present */
701 u8 *ga_log;
702
703 /* Tail of the GA log, if present */
704 u8 *ga_log_tail;
705
706 /* true if interrupts for this IOMMU are already enabled */
707 bool int_enabled;
708
709 /* if one, we need to send a completion wait command */
710 bool need_sync;
711
712 /* true if disable irte caching */
713 bool irtcachedis_enabled;
714
715 /* Handle for IOMMU core code */
716 struct iommu_device iommu;
717
718 /*
719 * We can't rely on the BIOS to restore all values on reinit, so we
720 * need to stash them
721 */
722
723 /* The iommu BAR */
724 u32 stored_addr_lo;
725 u32 stored_addr_hi;
726
727 /*
728 * Each iommu has 6 l1s, each of which is documented as having 0x12
729 * registers
730 */
731 u32 stored_l1[6][0x12];
732
733 /* The l2 indirect registers */
734 u32 stored_l2[0x83];
735
736 /* The maximum PC banks and counters/bank (PCSup=1) */
737 u8 max_banks;
738 u8 max_counters;
739 #ifdef CONFIG_IRQ_REMAP
740 struct irq_domain *ir_domain;
741 struct irq_domain *msi_domain;
742
743 struct amd_irte_ops *irte_ops;
744 #endif
745
746 u32 flags;
747 volatile u64 *cmd_sem;
748 u64 cmd_sem_val;
749
750 #ifdef CONFIG_AMD_IOMMU_DEBUGFS
751 /* DebugFS Info */
752 struct dentry *debugfs;
753 #endif
754 };
755
dev_to_amd_iommu(struct device * dev)756 static inline struct amd_iommu *dev_to_amd_iommu(struct device *dev)
757 {
758 struct iommu_device *iommu = dev_to_iommu_device(dev);
759
760 return container_of(iommu, struct amd_iommu, iommu);
761 }
762
763 #define ACPIHID_UID_LEN 256
764 #define ACPIHID_HID_LEN 9
765
766 struct acpihid_map_entry {
767 struct list_head list;
768 u8 uid[ACPIHID_UID_LEN];
769 u8 hid[ACPIHID_HID_LEN];
770 u32 devid;
771 u32 root_devid;
772 bool cmd_line;
773 struct iommu_group *group;
774 };
775
776 struct devid_map {
777 struct list_head list;
778 u8 id;
779 u32 devid;
780 bool cmd_line;
781 };
782
783 /*
784 * This struct contains device specific data for the IOMMU
785 */
786 struct iommu_dev_data {
787 /*Protect against attach/detach races */
788 spinlock_t lock;
789
790 struct list_head list; /* For domain->dev_list */
791 struct llist_node dev_data_list; /* For global dev_data_list */
792 struct protection_domain *domain; /* Domain the device is bound to */
793 struct device *dev;
794 u16 devid; /* PCI Device ID */
795 bool iommu_v2; /* Device can make use of IOMMUv2 */
796 struct {
797 bool enabled;
798 int qdep;
799 } ats; /* ATS state */
800 bool pri_tlp; /* PASID TLB required for
801 PPR completions */
802 bool use_vapic; /* Enable device to use vapic mode */
803 bool defer_attach;
804
805 struct ratelimit_state rs; /* Ratelimit IOPF messages */
806 };
807
808 /* Map HPET and IOAPIC ids to the devid used by the IOMMU */
809 extern struct list_head ioapic_map;
810 extern struct list_head hpet_map;
811 extern struct list_head acpihid_map;
812
813 /*
814 * List with all PCI segments in the system. This list is not locked because
815 * it is only written at driver initialization time
816 */
817 extern struct list_head amd_iommu_pci_seg_list;
818
819 /*
820 * List with all IOMMUs in the system. This list is not locked because it is
821 * only written and read at driver initialization or suspend time
822 */
823 extern struct list_head amd_iommu_list;
824
825 /*
826 * Array with pointers to each IOMMU struct
827 * The indices are referenced in the protection domains
828 */
829 extern struct amd_iommu *amd_iommus[MAX_IOMMUS];
830
831 /*
832 * Structure defining one entry in the device table
833 */
834 struct dev_table_entry {
835 u64 data[4];
836 };
837
838 /*
839 * One entry for unity mappings parsed out of the ACPI table.
840 */
841 struct unity_map_entry {
842 struct list_head list;
843
844 /* starting device id this entry is used for (including) */
845 u16 devid_start;
846 /* end device id this entry is used for (including) */
847 u16 devid_end;
848
849 /* start address to unity map (including) */
850 u64 address_start;
851 /* end address to unity map (including) */
852 u64 address_end;
853
854 /* required protection */
855 int prot;
856 };
857
858 /*
859 * Data structures for device handling
860 */
861
862 /* size of the dma_ops aperture as power of 2 */
863 extern unsigned amd_iommu_aperture_order;
864
865 /* allocation bitmap for domain ids */
866 extern unsigned long *amd_iommu_pd_alloc_bitmap;
867
868 /* Smallest max PASID supported by any IOMMU in the system */
869 extern u32 amd_iommu_max_pasid;
870
871 extern bool amd_iommu_v2_present;
872
873 extern bool amd_iommu_force_isolation;
874
875 /* Max levels of glxval supported */
876 extern int amd_iommu_max_glx_val;
877
878 /*
879 * This function flushes all internal caches of
880 * the IOMMU used by this driver.
881 */
882 extern void iommu_flush_all_caches(struct amd_iommu *iommu);
883
get_ioapic_devid(int id)884 static inline int get_ioapic_devid(int id)
885 {
886 struct devid_map *entry;
887
888 list_for_each_entry(entry, &ioapic_map, list) {
889 if (entry->id == id)
890 return entry->devid;
891 }
892
893 return -EINVAL;
894 }
895
get_hpet_devid(int id)896 static inline int get_hpet_devid(int id)
897 {
898 struct devid_map *entry;
899
900 list_for_each_entry(entry, &hpet_map, list) {
901 if (entry->id == id)
902 return entry->devid;
903 }
904
905 return -EINVAL;
906 }
907
908 enum amd_iommu_intr_mode_type {
909 AMD_IOMMU_GUEST_IR_LEGACY,
910
911 /* This mode is not visible to users. It is used when
912 * we cannot fully enable vAPIC and fallback to only support
913 * legacy interrupt remapping via 128-bit IRTE.
914 */
915 AMD_IOMMU_GUEST_IR_LEGACY_GA,
916 AMD_IOMMU_GUEST_IR_VAPIC,
917 };
918
919 #define AMD_IOMMU_GUEST_IR_GA(x) (x == AMD_IOMMU_GUEST_IR_VAPIC || \
920 x == AMD_IOMMU_GUEST_IR_LEGACY_GA)
921
922 #define AMD_IOMMU_GUEST_IR_VAPIC(x) (x == AMD_IOMMU_GUEST_IR_VAPIC)
923
924 union irte {
925 u32 val;
926 struct {
927 u32 valid : 1,
928 no_fault : 1,
929 int_type : 3,
930 rq_eoi : 1,
931 dm : 1,
932 rsvd_1 : 1,
933 destination : 8,
934 vector : 8,
935 rsvd_2 : 8;
936 } fields;
937 };
938
939 #define APICID_TO_IRTE_DEST_LO(x) (x & 0xffffff)
940 #define APICID_TO_IRTE_DEST_HI(x) ((x >> 24) & 0xff)
941
942 union irte_ga_lo {
943 u64 val;
944
945 /* For int remapping */
946 struct {
947 u64 valid : 1,
948 no_fault : 1,
949 /* ------ */
950 int_type : 3,
951 rq_eoi : 1,
952 dm : 1,
953 /* ------ */
954 guest_mode : 1,
955 destination : 24,
956 ga_tag : 32;
957 } fields_remap;
958
959 /* For guest vAPIC */
960 struct {
961 u64 valid : 1,
962 no_fault : 1,
963 /* ------ */
964 ga_log_intr : 1,
965 rsvd1 : 3,
966 is_run : 1,
967 /* ------ */
968 guest_mode : 1,
969 destination : 24,
970 ga_tag : 32;
971 } fields_vapic;
972 };
973
974 union irte_ga_hi {
975 u64 val;
976 struct {
977 u64 vector : 8,
978 rsvd_1 : 4,
979 ga_root_ptr : 40,
980 rsvd_2 : 4,
981 destination : 8;
982 } fields;
983 };
984
985 struct irte_ga {
986 union irte_ga_lo lo;
987 union irte_ga_hi hi;
988 };
989
990 struct irq_2_irte {
991 u16 devid; /* Device ID for IRTE table */
992 u16 index; /* Index into IRTE table*/
993 };
994
995 struct amd_ir_data {
996 u32 cached_ga_tag;
997 struct amd_iommu *iommu;
998 struct irq_2_irte irq_2_irte;
999 struct msi_msg msi_entry;
1000 void *entry; /* Pointer to union irte or struct irte_ga */
1001 void *ref; /* Pointer to the actual irte */
1002
1003 /**
1004 * Store information for activate/de-activate
1005 * Guest virtual APIC mode during runtime.
1006 */
1007 struct irq_cfg *cfg;
1008 int ga_vector;
1009 u64 ga_root_ptr;
1010 u32 ga_tag;
1011 };
1012
1013 struct amd_irte_ops {
1014 void (*prepare)(void *, u32, bool, u8, u32, int);
1015 void (*activate)(struct amd_iommu *iommu, void *, u16, u16);
1016 void (*deactivate)(struct amd_iommu *iommu, void *, u16, u16);
1017 void (*set_affinity)(struct amd_iommu *iommu, void *, u16, u16, u8, u32);
1018 void *(*get)(struct irq_remap_table *, int);
1019 void (*set_allocated)(struct irq_remap_table *, int);
1020 bool (*is_allocated)(struct irq_remap_table *, int);
1021 void (*clear_allocated)(struct irq_remap_table *, int);
1022 };
1023
1024 #ifdef CONFIG_IRQ_REMAP
1025 extern struct amd_irte_ops irte_32_ops;
1026 extern struct amd_irte_ops irte_128_ops;
1027 #endif
1028
1029 #endif /* _ASM_X86_AMD_IOMMU_TYPES_H */
1030