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