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