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