1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
8 #define pr_fmt(fmt) "AMD-Vi: " fmt
9 #define dev_fmt(fmt) pr_fmt(fmt)
11 #include <linux/pci.h>
12 #include <linux/acpi.h>
13 #include <linux/list.h>
14 #include <linux/bitmap.h>
15 #include <linux/slab.h>
16 #include <linux/syscore_ops.h>
17 #include <linux/interrupt.h>
18 #include <linux/msi.h>
19 #include <linux/irq.h>
20 #include <linux/amd-iommu.h>
21 #include <linux/export.h>
22 #include <linux/kmemleak.h>
23 #include <linux/cc_platform.h>
24 #include <linux/iopoll.h>
25 #include <asm/pci-direct.h>
26 #include <asm/iommu.h>
29 #include <asm/x86_init.h>
30 #include <asm/io_apic.h>
31 #include <asm/irq_remapping.h>
32 #include <asm/set_memory.h>
34 #include <linux/crash_dump.h>
36 #include "amd_iommu.h"
37 #include "../irq_remapping.h"
40 * definitions for the ACPI scanning code
42 #define IVRS_HEADER_LENGTH 48
44 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
45 #define ACPI_IVMD_TYPE_ALL 0x20
46 #define ACPI_IVMD_TYPE 0x21
47 #define ACPI_IVMD_TYPE_RANGE 0x22
49 #define IVHD_DEV_ALL 0x01
50 #define IVHD_DEV_SELECT 0x02
51 #define IVHD_DEV_SELECT_RANGE_START 0x03
52 #define IVHD_DEV_RANGE_END 0x04
53 #define IVHD_DEV_ALIAS 0x42
54 #define IVHD_DEV_ALIAS_RANGE 0x43
55 #define IVHD_DEV_EXT_SELECT 0x46
56 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
57 #define IVHD_DEV_SPECIAL 0x48
58 #define IVHD_DEV_ACPI_HID 0xf0
60 #define UID_NOT_PRESENT 0
61 #define UID_IS_INTEGER 1
62 #define UID_IS_CHARACTER 2
64 #define IVHD_SPECIAL_IOAPIC 1
65 #define IVHD_SPECIAL_HPET 2
67 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
68 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
69 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
70 #define IVHD_FLAG_ISOC_EN_MASK 0x08
72 #define IVMD_FLAG_EXCL_RANGE 0x08
73 #define IVMD_FLAG_IW 0x04
74 #define IVMD_FLAG_IR 0x02
75 #define IVMD_FLAG_UNITY_MAP 0x01
77 #define ACPI_DEVFLAG_INITPASS 0x01
78 #define ACPI_DEVFLAG_EXTINT 0x02
79 #define ACPI_DEVFLAG_NMI 0x04
80 #define ACPI_DEVFLAG_SYSMGT1 0x10
81 #define ACPI_DEVFLAG_SYSMGT2 0x20
82 #define ACPI_DEVFLAG_LINT0 0x40
83 #define ACPI_DEVFLAG_LINT1 0x80
84 #define ACPI_DEVFLAG_ATSDIS 0x10000000
86 #define LOOP_TIMEOUT 2000000
88 #define IVRS_GET_SBDF_ID(seg, bus, dev, fn) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \
89 | ((dev & 0x1f) << 3) | (fn & 0x7))
92 * ACPI table definitions
94 * These data structures are laid over the table to parse the important values
99 * structure describing one IOMMU in the ACPI table. Typically followed by one
100 * or more ivhd_entrys.
113 /* Following only valid on IVHD type 11h and 40h */
114 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
116 } __attribute__((packed));
119 * A device entry describing which devices a specific IOMMU translates and
120 * which requestor ids they use.
126 struct_group(ext_hid,
134 } __attribute__((packed));
137 * An AMD IOMMU memory definition structure. It defines things like exclusion
138 * ranges for devices and regions that should be unity mapped.
150 } __attribute__((packed));
153 bool amd_iommu_irq_remap __read_mostly;
155 enum io_pgtable_fmt amd_iommu_pgtable = AMD_IOMMU_V1;
156 /* Guest page table level */
157 int amd_iommu_gpt_level = PAGE_MODE_4_LEVEL;
159 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
160 static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
162 static bool amd_iommu_detected;
163 static bool amd_iommu_disabled __initdata;
164 static bool amd_iommu_force_enable __initdata;
165 static bool amd_iommu_irtcachedis;
166 static int amd_iommu_target_ivhd_type;
168 /* Global EFR and EFR2 registers */
172 /* SNP is enabled on the system? */
173 bool amd_iommu_snp_en;
174 EXPORT_SYMBOL(amd_iommu_snp_en);
176 LIST_HEAD(amd_iommu_pci_seg_list); /* list of all PCI segments */
177 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
180 /* Array to assign indices to IOMMUs*/
181 struct amd_iommu *amd_iommus[MAX_IOMMUS];
183 /* Number of IOMMUs present in the system */
184 static int amd_iommus_present;
186 /* IOMMUs have a non-present cache? */
187 bool amd_iommu_np_cache __read_mostly;
188 bool amd_iommu_iotlb_sup __read_mostly = true;
190 u32 amd_iommu_max_pasid __read_mostly = ~0;
192 bool amd_iommu_v2_present __read_mostly;
193 static bool amd_iommu_pc_present __read_mostly;
194 bool amdr_ivrs_remap_support __read_mostly;
196 bool amd_iommu_force_isolation __read_mostly;
199 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
200 * to know which ones are already in use.
202 unsigned long *amd_iommu_pd_alloc_bitmap;
204 enum iommu_init_state {
214 IOMMU_CMDLINE_DISABLED,
217 /* Early ioapic and hpet maps from kernel command line */
218 #define EARLY_MAP_SIZE 4
219 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
220 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
221 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
223 static int __initdata early_ioapic_map_size;
224 static int __initdata early_hpet_map_size;
225 static int __initdata early_acpihid_map_size;
227 static bool __initdata cmdline_maps;
229 static enum iommu_init_state init_state = IOMMU_START_STATE;
231 static int amd_iommu_enable_interrupts(void);
232 static int __init iommu_go_to_state(enum iommu_init_state state);
233 static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg);
235 static bool amd_iommu_pre_enabled = true;
237 static u32 amd_iommu_ivinfo __initdata;
239 bool translation_pre_enabled(struct amd_iommu *iommu)
241 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
244 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
246 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
249 static void init_translation_status(struct amd_iommu *iommu)
253 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
254 if (ctrl & (1<<CONTROL_IOMMU_EN))
255 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
258 static inline unsigned long tbl_size(int entry_size, int last_bdf)
260 unsigned shift = PAGE_SHIFT +
261 get_order((last_bdf + 1) * entry_size);
266 int amd_iommu_get_num_iommus(void)
268 return amd_iommus_present;
272 * Iterate through all the IOMMUs to get common EFR
273 * masks among all IOMMUs and warn if found inconsistency.
275 static void get_global_efr(void)
277 struct amd_iommu *iommu;
279 for_each_iommu(iommu) {
280 u64 tmp = iommu->features;
281 u64 tmp2 = iommu->features2;
283 if (list_is_first(&iommu->list, &amd_iommu_list)) {
285 amd_iommu_efr2 = tmp2;
289 if (amd_iommu_efr == tmp &&
290 amd_iommu_efr2 == tmp2)
294 "Found inconsistent EFR/EFR2 %#llx,%#llx (global %#llx,%#llx) on iommu%d (%04x:%02x:%02x.%01x).\n",
295 tmp, tmp2, amd_iommu_efr, amd_iommu_efr2,
296 iommu->index, iommu->pci_seg->id,
297 PCI_BUS_NUM(iommu->devid), PCI_SLOT(iommu->devid),
298 PCI_FUNC(iommu->devid));
300 amd_iommu_efr &= tmp;
301 amd_iommu_efr2 &= tmp2;
304 pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);
307 static bool check_feature_on_all_iommus(u64 mask)
309 return !!(amd_iommu_efr & mask);
312 static inline int check_feature_gpt_level(void)
314 return ((amd_iommu_efr >> FEATURE_GATS_SHIFT) & FEATURE_GATS_MASK);
318 * For IVHD type 0x11/0x40, EFR is also available via IVHD.
319 * Default to IVHD EFR since it is available sooner
320 * (i.e. before PCI init).
322 static void __init early_iommu_features_init(struct amd_iommu *iommu,
323 struct ivhd_header *h)
325 if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP) {
326 iommu->features = h->efr_reg;
327 iommu->features2 = h->efr_reg2;
329 if (amd_iommu_ivinfo & IOMMU_IVINFO_DMA_REMAP)
330 amdr_ivrs_remap_support = true;
333 /* Access to l1 and l2 indexed register spaces */
335 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
339 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
340 pci_read_config_dword(iommu->dev, 0xfc, &val);
344 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
346 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
347 pci_write_config_dword(iommu->dev, 0xfc, val);
348 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
351 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
355 pci_write_config_dword(iommu->dev, 0xf0, address);
356 pci_read_config_dword(iommu->dev, 0xf4, &val);
360 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
362 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
363 pci_write_config_dword(iommu->dev, 0xf4, val);
366 /****************************************************************************
368 * AMD IOMMU MMIO register space handling functions
370 * These functions are used to program the IOMMU device registers in
371 * MMIO space required for that driver.
373 ****************************************************************************/
376 * This function set the exclusion range in the IOMMU. DMA accesses to the
377 * exclusion range are passed through untranslated
379 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
381 u64 start = iommu->exclusion_start & PAGE_MASK;
382 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
385 if (!iommu->exclusion_start)
388 entry = start | MMIO_EXCL_ENABLE_MASK;
389 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
390 &entry, sizeof(entry));
393 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
394 &entry, sizeof(entry));
397 static void iommu_set_cwwb_range(struct amd_iommu *iommu)
399 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
400 u64 entry = start & PM_ADDR_MASK;
402 if (!check_feature_on_all_iommus(FEATURE_SNP))
406 * Re-purpose Exclusion base/limit registers for Completion wait
407 * write-back base/limit.
409 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
410 &entry, sizeof(entry));
413 * Default to 4 Kbytes, which can be specified by setting base
414 * address equal to the limit address.
416 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
417 &entry, sizeof(entry));
420 /* Programs the physical address of the device table into the IOMMU hardware */
421 static void iommu_set_device_table(struct amd_iommu *iommu)
424 u32 dev_table_size = iommu->pci_seg->dev_table_size;
425 void *dev_table = (void *)get_dev_table(iommu);
427 BUG_ON(iommu->mmio_base == NULL);
429 entry = iommu_virt_to_phys(dev_table);
430 entry |= (dev_table_size >> 12) - 1;
431 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
432 &entry, sizeof(entry));
435 /* Generic functions to enable/disable certain features of the IOMMU. */
436 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
440 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
441 ctrl |= (1ULL << bit);
442 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
445 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
449 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
450 ctrl &= ~(1ULL << bit);
451 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
454 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
458 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
459 ctrl &= ~CTRL_INV_TO_MASK;
460 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
461 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
464 /* Function to enable the hardware */
465 static void iommu_enable(struct amd_iommu *iommu)
467 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
470 static void iommu_disable(struct amd_iommu *iommu)
472 if (!iommu->mmio_base)
475 /* Disable command buffer */
476 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
478 /* Disable event logging and event interrupts */
479 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
480 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
482 /* Disable IOMMU GA_LOG */
483 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
484 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
486 /* Disable IOMMU hardware itself */
487 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
489 /* Clear IRTE cache disabling bit */
490 iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
494 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
495 * the system has one.
497 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
499 if (!request_mem_region(address, end, "amd_iommu")) {
500 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
502 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
506 return (u8 __iomem *)ioremap(address, end);
509 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
511 if (iommu->mmio_base)
512 iounmap(iommu->mmio_base);
513 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
516 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
532 /****************************************************************************
534 * The functions below belong to the first pass of AMD IOMMU ACPI table
535 * parsing. In this pass we try to find out the highest device id this
536 * code has to handle. Upon this information the size of the shared data
537 * structures is determined later.
539 ****************************************************************************/
542 * This function calculates the length of a given IVHD entry
544 static inline int ivhd_entry_length(u8 *ivhd)
546 u32 type = ((struct ivhd_entry *)ivhd)->type;
549 return 0x04 << (*ivhd >> 6);
550 } else if (type == IVHD_DEV_ACPI_HID) {
551 /* For ACPI_HID, offset 21 is uid len */
552 return *((u8 *)ivhd + 21) + 22;
558 * After reading the highest device id from the IOMMU PCI capability header
559 * this function looks if there is a higher device id defined in the ACPI table
561 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
563 u8 *p = (void *)h, *end = (void *)h;
564 struct ivhd_entry *dev;
565 int last_devid = -EINVAL;
567 u32 ivhd_size = get_ivhd_header_size(h);
570 pr_err("Unsupported IVHD type %#x\n", h->type);
578 dev = (struct ivhd_entry *)p;
581 /* Use maximum BDF value for DEV_ALL */
583 case IVHD_DEV_SELECT:
584 case IVHD_DEV_RANGE_END:
586 case IVHD_DEV_EXT_SELECT:
587 /* all the above subfield types refer to device ids */
588 if (dev->devid > last_devid)
589 last_devid = dev->devid;
594 p += ivhd_entry_length(p);
602 static int __init check_ivrs_checksum(struct acpi_table_header *table)
605 u8 checksum = 0, *p = (u8 *)table;
607 for (i = 0; i < table->length; ++i)
610 /* ACPI table corrupt */
611 pr_err(FW_BUG "IVRS invalid checksum\n");
619 * Iterate over all IVHD entries in the ACPI table and find the highest device
620 * id which we need to handle. This is the first of three functions which parse
621 * the ACPI table. So we check the checksum here.
623 static int __init find_last_devid_acpi(struct acpi_table_header *table, u16 pci_seg)
625 u8 *p = (u8 *)table, *end = (u8 *)table;
626 struct ivhd_header *h;
627 int last_devid, last_bdf = 0;
629 p += IVRS_HEADER_LENGTH;
631 end += table->length;
633 h = (struct ivhd_header *)p;
634 if (h->pci_seg == pci_seg &&
635 h->type == amd_iommu_target_ivhd_type) {
636 last_devid = find_last_devid_from_ivhd(h);
640 if (last_devid > last_bdf)
641 last_bdf = last_devid;
650 /****************************************************************************
652 * The following functions belong to the code path which parses the ACPI table
653 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
654 * data structures, initialize the per PCI segment device/alias/rlookup table
655 * and also basically initialize the hardware.
657 ****************************************************************************/
659 /* Allocate per PCI segment device table */
660 static inline int __init alloc_dev_table(struct amd_iommu_pci_seg *pci_seg)
662 pci_seg->dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
663 get_order(pci_seg->dev_table_size));
664 if (!pci_seg->dev_table)
670 static inline void free_dev_table(struct amd_iommu_pci_seg *pci_seg)
672 free_pages((unsigned long)pci_seg->dev_table,
673 get_order(pci_seg->dev_table_size));
674 pci_seg->dev_table = NULL;
677 /* Allocate per PCI segment IOMMU rlookup table. */
678 static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
680 pci_seg->rlookup_table = (void *)__get_free_pages(
681 GFP_KERNEL | __GFP_ZERO,
682 get_order(pci_seg->rlookup_table_size));
683 if (pci_seg->rlookup_table == NULL)
689 static inline void free_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
691 free_pages((unsigned long)pci_seg->rlookup_table,
692 get_order(pci_seg->rlookup_table_size));
693 pci_seg->rlookup_table = NULL;
696 static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
698 pci_seg->irq_lookup_table = (void *)__get_free_pages(
699 GFP_KERNEL | __GFP_ZERO,
700 get_order(pci_seg->rlookup_table_size));
701 kmemleak_alloc(pci_seg->irq_lookup_table,
702 pci_seg->rlookup_table_size, 1, GFP_KERNEL);
703 if (pci_seg->irq_lookup_table == NULL)
709 static inline void free_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
711 kmemleak_free(pci_seg->irq_lookup_table);
712 free_pages((unsigned long)pci_seg->irq_lookup_table,
713 get_order(pci_seg->rlookup_table_size));
714 pci_seg->irq_lookup_table = NULL;
717 static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)
721 pci_seg->alias_table = (void *)__get_free_pages(GFP_KERNEL,
722 get_order(pci_seg->alias_table_size));
723 if (!pci_seg->alias_table)
727 * let all alias entries point to itself
729 for (i = 0; i <= pci_seg->last_bdf; ++i)
730 pci_seg->alias_table[i] = i;
735 static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)
737 free_pages((unsigned long)pci_seg->alias_table,
738 get_order(pci_seg->alias_table_size));
739 pci_seg->alias_table = NULL;
743 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
744 * write commands to that buffer later and the IOMMU will execute them
747 static int __init alloc_command_buffer(struct amd_iommu *iommu)
749 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
750 get_order(CMD_BUFFER_SIZE));
752 return iommu->cmd_buf ? 0 : -ENOMEM;
756 * This function restarts event logging in case the IOMMU experienced
757 * an event log buffer overflow.
759 void amd_iommu_restart_event_logging(struct amd_iommu *iommu)
761 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
762 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
766 * This function restarts event logging in case the IOMMU experienced
767 * an GA log overflow.
769 void amd_iommu_restart_ga_log(struct amd_iommu *iommu)
773 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
774 if (status & MMIO_STATUS_GALOG_RUN_MASK)
777 pr_info_ratelimited("IOMMU GA Log restarting\n");
779 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
780 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
782 writel(MMIO_STATUS_GALOG_OVERFLOW_MASK,
783 iommu->mmio_base + MMIO_STATUS_OFFSET);
785 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
786 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
790 * This function resets the command buffer if the IOMMU stopped fetching
793 static void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
795 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
797 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
798 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
799 iommu->cmd_buf_head = 0;
800 iommu->cmd_buf_tail = 0;
802 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
806 * This function writes the command buffer address to the hardware and
809 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
813 BUG_ON(iommu->cmd_buf == NULL);
815 entry = iommu_virt_to_phys(iommu->cmd_buf);
816 entry |= MMIO_CMD_SIZE_512;
818 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
819 &entry, sizeof(entry));
821 amd_iommu_reset_cmd_buffer(iommu);
825 * This function disables the command buffer
827 static void iommu_disable_command_buffer(struct amd_iommu *iommu)
829 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
832 static void __init free_command_buffer(struct amd_iommu *iommu)
834 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
837 static void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
838 gfp_t gfp, size_t size)
840 int order = get_order(size);
841 void *buf = (void *)__get_free_pages(gfp, order);
844 check_feature_on_all_iommus(FEATURE_SNP) &&
845 set_memory_4k((unsigned long)buf, (1 << order))) {
846 free_pages((unsigned long)buf, order);
853 /* allocates the memory where the IOMMU will log its events to */
854 static int __init alloc_event_buffer(struct amd_iommu *iommu)
856 iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
859 return iommu->evt_buf ? 0 : -ENOMEM;
862 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
866 BUG_ON(iommu->evt_buf == NULL);
868 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
870 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
871 &entry, sizeof(entry));
873 /* set head and tail to zero manually */
874 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
875 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
877 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
881 * This function disables the event log buffer
883 static void iommu_disable_event_buffer(struct amd_iommu *iommu)
885 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
888 static void __init free_event_buffer(struct amd_iommu *iommu)
890 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
893 /* allocates the memory where the IOMMU will log its events to */
894 static int __init alloc_ppr_log(struct amd_iommu *iommu)
896 iommu->ppr_log = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
899 return iommu->ppr_log ? 0 : -ENOMEM;
902 static void iommu_enable_ppr_log(struct amd_iommu *iommu)
906 if (iommu->ppr_log == NULL)
909 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
911 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
912 &entry, sizeof(entry));
914 /* set head and tail to zero manually */
915 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
916 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
918 iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
919 iommu_feature_enable(iommu, CONTROL_PPR_EN);
922 static void __init free_ppr_log(struct amd_iommu *iommu)
924 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
927 static void free_ga_log(struct amd_iommu *iommu)
929 #ifdef CONFIG_IRQ_REMAP
930 free_pages((unsigned long)iommu->ga_log, get_order(GA_LOG_SIZE));
931 free_pages((unsigned long)iommu->ga_log_tail, get_order(8));
935 #ifdef CONFIG_IRQ_REMAP
936 static int iommu_ga_log_enable(struct amd_iommu *iommu)
944 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
945 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
946 &entry, sizeof(entry));
947 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
948 (BIT_ULL(52)-1)) & ~7ULL;
949 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
950 &entry, sizeof(entry));
951 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
952 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
955 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
956 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
958 for (i = 0; i < LOOP_TIMEOUT; ++i) {
959 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
960 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
965 if (WARN_ON(i >= LOOP_TIMEOUT))
971 static int iommu_init_ga_log(struct amd_iommu *iommu)
973 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
976 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
977 get_order(GA_LOG_SIZE));
981 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
983 if (!iommu->ga_log_tail)
991 #endif /* CONFIG_IRQ_REMAP */
993 static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
995 iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO, 1);
997 return iommu->cmd_sem ? 0 : -ENOMEM;
1000 static void __init free_cwwb_sem(struct amd_iommu *iommu)
1003 free_page((unsigned long)iommu->cmd_sem);
1006 static void iommu_enable_xt(struct amd_iommu *iommu)
1008 #ifdef CONFIG_IRQ_REMAP
1010 * XT mode (32-bit APIC destination ID) requires
1011 * GA mode (128-bit IRTE support) as a prerequisite.
1013 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
1014 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1015 iommu_feature_enable(iommu, CONTROL_XT_EN);
1016 #endif /* CONFIG_IRQ_REMAP */
1019 static void iommu_enable_gt(struct amd_iommu *iommu)
1021 if (!iommu_feature(iommu, FEATURE_GT))
1024 iommu_feature_enable(iommu, CONTROL_GT_EN);
1027 /* sets a specific bit in the device table entry. */
1028 static void __set_dev_entry_bit(struct dev_table_entry *dev_table,
1031 int i = (bit >> 6) & 0x03;
1032 int _bit = bit & 0x3f;
1034 dev_table[devid].data[i] |= (1UL << _bit);
1037 static void set_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
1039 struct dev_table_entry *dev_table = get_dev_table(iommu);
1041 return __set_dev_entry_bit(dev_table, devid, bit);
1044 static int __get_dev_entry_bit(struct dev_table_entry *dev_table,
1047 int i = (bit >> 6) & 0x03;
1048 int _bit = bit & 0x3f;
1050 return (dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
1053 static int get_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
1055 struct dev_table_entry *dev_table = get_dev_table(iommu);
1057 return __get_dev_entry_bit(dev_table, devid, bit);
1060 static bool __copy_device_table(struct amd_iommu *iommu)
1062 u64 int_ctl, int_tab_len, entry = 0;
1063 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
1064 struct dev_table_entry *old_devtb = NULL;
1065 u32 lo, hi, devid, old_devtb_size;
1066 phys_addr_t old_devtb_phys;
1067 u16 dom_id, dte_v, irq_v;
1071 /* Each IOMMU use separate device table with the same size */
1072 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
1073 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
1074 entry = (((u64) hi) << 32) + lo;
1076 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
1077 if (old_devtb_size != pci_seg->dev_table_size) {
1078 pr_err("The device table size of IOMMU:%d is not expected!\n",
1084 * When SME is enabled in the first kernel, the entry includes the
1085 * memory encryption mask(sme_me_mask), we must remove the memory
1086 * encryption mask to obtain the true physical address in kdump kernel.
1088 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
1090 if (old_devtb_phys >= 0x100000000ULL) {
1091 pr_err("The address of old device table is above 4G, not trustworthy!\n");
1094 old_devtb = (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && is_kdump_kernel())
1095 ? (__force void *)ioremap_encrypted(old_devtb_phys,
1096 pci_seg->dev_table_size)
1097 : memremap(old_devtb_phys, pci_seg->dev_table_size, MEMREMAP_WB);
1102 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
1103 pci_seg->old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
1104 get_order(pci_seg->dev_table_size));
1105 if (pci_seg->old_dev_tbl_cpy == NULL) {
1106 pr_err("Failed to allocate memory for copying old device table!\n");
1107 memunmap(old_devtb);
1111 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
1112 pci_seg->old_dev_tbl_cpy[devid] = old_devtb[devid];
1113 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
1114 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
1116 if (dte_v && dom_id) {
1117 pci_seg->old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
1118 pci_seg->old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
1119 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
1120 /* If gcr3 table existed, mask it out */
1121 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
1122 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1123 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1124 pci_seg->old_dev_tbl_cpy[devid].data[1] &= ~tmp;
1125 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
1127 pci_seg->old_dev_tbl_cpy[devid].data[0] &= ~tmp;
1131 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
1132 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
1133 int_tab_len = old_devtb[devid].data[2] & DTE_INTTABLEN_MASK;
1134 if (irq_v && (int_ctl || int_tab_len)) {
1135 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
1136 (int_tab_len != DTE_INTTABLEN)) {
1137 pr_err("Wrong old irq remapping flag: %#x\n", devid);
1138 memunmap(old_devtb);
1142 pci_seg->old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
1145 memunmap(old_devtb);
1150 static bool copy_device_table(void)
1152 struct amd_iommu *iommu;
1153 struct amd_iommu_pci_seg *pci_seg;
1155 if (!amd_iommu_pre_enabled)
1158 pr_warn("Translation is already enabled - trying to copy translation structures\n");
1161 * All IOMMUs within PCI segment shares common device table.
1162 * Hence copy device table only once per PCI segment.
1164 for_each_pci_segment(pci_seg) {
1165 for_each_iommu(iommu) {
1166 if (pci_seg->id != iommu->pci_seg->id)
1168 if (!__copy_device_table(iommu))
1177 void amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid)
1181 sysmgt = get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1) |
1182 (get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2) << 1);
1185 set_dev_entry_bit(iommu, devid, DEV_ENTRY_IW);
1189 * This function takes the device specific flags read from the ACPI
1190 * table and sets up the device table entry with that information
1192 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
1193 u16 devid, u32 flags, u32 ext_flags)
1195 if (flags & ACPI_DEVFLAG_INITPASS)
1196 set_dev_entry_bit(iommu, devid, DEV_ENTRY_INIT_PASS);
1197 if (flags & ACPI_DEVFLAG_EXTINT)
1198 set_dev_entry_bit(iommu, devid, DEV_ENTRY_EINT_PASS);
1199 if (flags & ACPI_DEVFLAG_NMI)
1200 set_dev_entry_bit(iommu, devid, DEV_ENTRY_NMI_PASS);
1201 if (flags & ACPI_DEVFLAG_SYSMGT1)
1202 set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1);
1203 if (flags & ACPI_DEVFLAG_SYSMGT2)
1204 set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2);
1205 if (flags & ACPI_DEVFLAG_LINT0)
1206 set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT0_PASS);
1207 if (flags & ACPI_DEVFLAG_LINT1)
1208 set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT1_PASS);
1210 amd_iommu_apply_erratum_63(iommu, devid);
1212 amd_iommu_set_rlookup_table(iommu, devid);
1215 int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line)
1217 struct devid_map *entry;
1218 struct list_head *list;
1220 if (type == IVHD_SPECIAL_IOAPIC)
1222 else if (type == IVHD_SPECIAL_HPET)
1227 list_for_each_entry(entry, list, list) {
1228 if (!(entry->id == id && entry->cmd_line))
1231 pr_info("Command-line override present for %s id %d - ignoring\n",
1232 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1234 *devid = entry->devid;
1239 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1244 entry->devid = *devid;
1245 entry->cmd_line = cmd_line;
1247 list_add_tail(&entry->list, list);
1252 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u32 *devid,
1255 struct acpihid_map_entry *entry;
1256 struct list_head *list = &acpihid_map;
1258 list_for_each_entry(entry, list, list) {
1259 if (strcmp(entry->hid, hid) ||
1260 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1264 pr_info("Command-line override for hid:%s uid:%s\n",
1266 *devid = entry->devid;
1270 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1274 memcpy(entry->uid, uid, strlen(uid));
1275 memcpy(entry->hid, hid, strlen(hid));
1276 entry->devid = *devid;
1277 entry->cmd_line = cmd_line;
1278 entry->root_devid = (entry->devid & (~0x7));
1280 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1281 entry->cmd_line ? "cmd" : "ivrs",
1282 entry->hid, entry->uid, entry->root_devid);
1284 list_add_tail(&entry->list, list);
1288 static int __init add_early_maps(void)
1292 for (i = 0; i < early_ioapic_map_size; ++i) {
1293 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1294 early_ioapic_map[i].id,
1295 &early_ioapic_map[i].devid,
1296 early_ioapic_map[i].cmd_line);
1301 for (i = 0; i < early_hpet_map_size; ++i) {
1302 ret = add_special_device(IVHD_SPECIAL_HPET,
1303 early_hpet_map[i].id,
1304 &early_hpet_map[i].devid,
1305 early_hpet_map[i].cmd_line);
1310 for (i = 0; i < early_acpihid_map_size; ++i) {
1311 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1312 early_acpihid_map[i].uid,
1313 &early_acpihid_map[i].devid,
1314 early_acpihid_map[i].cmd_line);
1323 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1324 * initializes the hardware and our data structures with it.
1326 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1327 struct ivhd_header *h)
1330 u8 *end = p, flags = 0;
1331 u16 devid = 0, devid_start = 0, devid_to = 0, seg_id;
1332 u32 dev_i, ext_flags = 0;
1334 struct ivhd_entry *e;
1335 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
1340 ret = add_early_maps();
1344 amd_iommu_apply_ivrs_quirks();
1347 * First save the recommended feature enable bits from ACPI
1349 iommu->acpi_flags = h->flags;
1352 * Done. Now parse the device entries
1354 ivhd_size = get_ivhd_header_size(h);
1356 pr_err("Unsupported IVHD type %#x\n", h->type);
1366 e = (struct ivhd_entry *)p;
1367 seg_id = pci_seg->id;
1372 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1374 for (dev_i = 0; dev_i <= pci_seg->last_bdf; ++dev_i)
1375 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1377 case IVHD_DEV_SELECT:
1379 DUMP_printk(" DEV_SELECT\t\t\t devid: %04x:%02x:%02x.%x "
1381 seg_id, PCI_BUS_NUM(e->devid),
1387 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1389 case IVHD_DEV_SELECT_RANGE_START:
1391 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1392 "devid: %04x:%02x:%02x.%x flags: %02x\n",
1393 seg_id, PCI_BUS_NUM(e->devid),
1398 devid_start = e->devid;
1403 case IVHD_DEV_ALIAS:
1405 DUMP_printk(" DEV_ALIAS\t\t\t devid: %04x:%02x:%02x.%x "
1406 "flags: %02x devid_to: %02x:%02x.%x\n",
1407 seg_id, PCI_BUS_NUM(e->devid),
1411 PCI_BUS_NUM(e->ext >> 8),
1412 PCI_SLOT(e->ext >> 8),
1413 PCI_FUNC(e->ext >> 8));
1416 devid_to = e->ext >> 8;
1417 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1418 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1419 pci_seg->alias_table[devid] = devid_to;
1421 case IVHD_DEV_ALIAS_RANGE:
1423 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1424 "devid: %04x:%02x:%02x.%x flags: %02x "
1425 "devid_to: %04x:%02x:%02x.%x\n",
1426 seg_id, PCI_BUS_NUM(e->devid),
1430 seg_id, PCI_BUS_NUM(e->ext >> 8),
1431 PCI_SLOT(e->ext >> 8),
1432 PCI_FUNC(e->ext >> 8));
1434 devid_start = e->devid;
1436 devid_to = e->ext >> 8;
1440 case IVHD_DEV_EXT_SELECT:
1442 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %04x:%02x:%02x.%x "
1443 "flags: %02x ext: %08x\n",
1444 seg_id, PCI_BUS_NUM(e->devid),
1450 set_dev_entry_from_acpi(iommu, devid, e->flags,
1453 case IVHD_DEV_EXT_SELECT_RANGE:
1455 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1456 "%04x:%02x:%02x.%x flags: %02x ext: %08x\n",
1457 seg_id, PCI_BUS_NUM(e->devid),
1462 devid_start = e->devid;
1467 case IVHD_DEV_RANGE_END:
1469 DUMP_printk(" DEV_RANGE_END\t\t devid: %04x:%02x:%02x.%x\n",
1470 seg_id, PCI_BUS_NUM(e->devid),
1472 PCI_FUNC(e->devid));
1475 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1477 pci_seg->alias_table[dev_i] = devid_to;
1478 set_dev_entry_from_acpi(iommu,
1479 devid_to, flags, ext_flags);
1481 set_dev_entry_from_acpi(iommu, dev_i,
1485 case IVHD_DEV_SPECIAL: {
1491 handle = e->ext & 0xff;
1492 devid = PCI_SEG_DEVID_TO_SBDF(seg_id, (e->ext >> 8));
1493 type = (e->ext >> 24) & 0xff;
1495 if (type == IVHD_SPECIAL_IOAPIC)
1497 else if (type == IVHD_SPECIAL_HPET)
1502 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x\n",
1504 seg_id, PCI_BUS_NUM(devid),
1508 ret = add_special_device(type, handle, &devid, false);
1513 * add_special_device might update the devid in case a
1514 * command-line override is present. So call
1515 * set_dev_entry_from_acpi after add_special_device.
1517 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1521 case IVHD_DEV_ACPI_HID: {
1523 u8 hid[ACPIHID_HID_LEN];
1524 u8 uid[ACPIHID_UID_LEN];
1527 if (h->type != 0x40) {
1528 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1533 BUILD_BUG_ON(sizeof(e->ext_hid) != ACPIHID_HID_LEN - 1);
1534 memcpy(hid, &e->ext_hid, ACPIHID_HID_LEN - 1);
1535 hid[ACPIHID_HID_LEN - 1] = '\0';
1538 pr_err(FW_BUG "Invalid HID.\n");
1544 case UID_NOT_PRESENT:
1547 pr_warn(FW_BUG "Invalid UID length.\n");
1550 case UID_IS_INTEGER:
1552 sprintf(uid, "%d", e->uid);
1555 case UID_IS_CHARACTER:
1557 memcpy(uid, &e->uid, e->uidl);
1558 uid[e->uidl] = '\0';
1565 devid = PCI_SEG_DEVID_TO_SBDF(seg_id, e->devid);
1566 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x\n",
1574 ret = add_acpi_hid_device(hid, uid, &devid, false);
1579 * add_special_device might update the devid in case a
1580 * command-line override is present. So call
1581 * set_dev_entry_from_acpi after add_special_device.
1583 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1591 p += ivhd_entry_length(p);
1597 /* Allocate PCI segment data structure */
1598 static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
1599 struct acpi_table_header *ivrs_base)
1601 struct amd_iommu_pci_seg *pci_seg;
1605 * First parse ACPI tables to find the largest Bus/Dev/Func we need to
1606 * handle in this PCI segment. Upon this information the shared data
1607 * structures for the PCI segments in the system will be allocated.
1609 last_bdf = find_last_devid_acpi(ivrs_base, id);
1613 pci_seg = kzalloc(sizeof(struct amd_iommu_pci_seg), GFP_KERNEL);
1614 if (pci_seg == NULL)
1617 pci_seg->last_bdf = last_bdf;
1618 DUMP_printk("PCI segment : 0x%0x, last bdf : 0x%04x\n", id, last_bdf);
1619 pci_seg->dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE, last_bdf);
1620 pci_seg->alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE, last_bdf);
1621 pci_seg->rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE, last_bdf);
1624 init_llist_head(&pci_seg->dev_data_list);
1625 INIT_LIST_HEAD(&pci_seg->unity_map);
1626 list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
1628 if (alloc_dev_table(pci_seg))
1630 if (alloc_alias_table(pci_seg))
1632 if (alloc_rlookup_table(pci_seg))
1638 static struct amd_iommu_pci_seg *__init get_pci_segment(u16 id,
1639 struct acpi_table_header *ivrs_base)
1641 struct amd_iommu_pci_seg *pci_seg;
1643 for_each_pci_segment(pci_seg) {
1644 if (pci_seg->id == id)
1648 return alloc_pci_segment(id, ivrs_base);
1651 static void __init free_pci_segments(void)
1653 struct amd_iommu_pci_seg *pci_seg, *next;
1655 for_each_pci_segment_safe(pci_seg, next) {
1656 list_del(&pci_seg->list);
1657 free_irq_lookup_table(pci_seg);
1658 free_rlookup_table(pci_seg);
1659 free_alias_table(pci_seg);
1660 free_dev_table(pci_seg);
1665 static void __init free_iommu_one(struct amd_iommu *iommu)
1667 free_cwwb_sem(iommu);
1668 free_command_buffer(iommu);
1669 free_event_buffer(iommu);
1670 free_ppr_log(iommu);
1672 iommu_unmap_mmio_space(iommu);
1675 static void __init free_iommu_all(void)
1677 struct amd_iommu *iommu, *next;
1679 for_each_iommu_safe(iommu, next) {
1680 list_del(&iommu->list);
1681 free_iommu_one(iommu);
1687 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1689 * BIOS should disable L2B micellaneous clock gating by setting
1690 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1692 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1696 if ((boot_cpu_data.x86 != 0x15) ||
1697 (boot_cpu_data.x86_model < 0x10) ||
1698 (boot_cpu_data.x86_model > 0x1f))
1701 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1702 pci_read_config_dword(iommu->dev, 0xf4, &value);
1707 /* Select NB indirect register 0x90 and enable writing */
1708 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1710 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1711 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
1713 /* Clear the enable writing bit */
1714 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1718 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1720 * BIOS should enable ATS write permission check by setting
1721 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1723 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1727 if ((boot_cpu_data.x86 != 0x15) ||
1728 (boot_cpu_data.x86_model < 0x30) ||
1729 (boot_cpu_data.x86_model > 0x3f))
1732 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1733 value = iommu_read_l2(iommu, 0x47);
1738 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1739 iommu_write_l2(iommu, 0x47, value | BIT(0));
1741 pci_info(iommu->dev, "Applying ATS write check workaround\n");
1745 * This function glues the initialization function for one IOMMU
1746 * together and also allocates the command buffer and programs the
1747 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1749 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
1750 struct acpi_table_header *ivrs_base)
1752 struct amd_iommu_pci_seg *pci_seg;
1754 pci_seg = get_pci_segment(h->pci_seg, ivrs_base);
1755 if (pci_seg == NULL)
1757 iommu->pci_seg = pci_seg;
1759 raw_spin_lock_init(&iommu->lock);
1760 atomic64_set(&iommu->cmd_sem_val, 0);
1762 /* Add IOMMU to internal data structures */
1763 list_add_tail(&iommu->list, &amd_iommu_list);
1764 iommu->index = amd_iommus_present++;
1766 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1767 WARN(1, "System has more IOMMUs than supported by this driver\n");
1771 /* Index is fine - add IOMMU to the array */
1772 amd_iommus[iommu->index] = iommu;
1775 * Copy data from ACPI table entry to the iommu struct
1777 iommu->devid = h->devid;
1778 iommu->cap_ptr = h->cap_ptr;
1779 iommu->mmio_phys = h->mmio_phys;
1783 /* Check if IVHD EFR contains proper max banks/counters */
1784 if ((h->efr_attr != 0) &&
1785 ((h->efr_attr & (0xF << 13)) != 0) &&
1786 ((h->efr_attr & (0x3F << 17)) != 0))
1787 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1789 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1792 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1793 * GAM also requires GA mode. Therefore, we need to
1794 * check cmpxchg16b support before enabling it.
1796 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1797 ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1798 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1802 if (h->efr_reg & (1 << 9))
1803 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1805 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1808 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1809 * XT, GAM also requires GA mode. Therefore, we need to
1810 * check cmpxchg16b support before enabling them.
1812 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1813 ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
1814 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1818 if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
1819 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1821 early_iommu_features_init(iommu, h);
1828 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1829 iommu->mmio_phys_end);
1830 if (!iommu->mmio_base)
1833 return init_iommu_from_acpi(iommu, h);
1836 static int __init init_iommu_one_late(struct amd_iommu *iommu)
1840 if (alloc_cwwb_sem(iommu))
1843 if (alloc_command_buffer(iommu))
1846 if (alloc_event_buffer(iommu))
1849 iommu->int_enabled = false;
1851 init_translation_status(iommu);
1852 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1853 iommu_disable(iommu);
1854 clear_translation_pre_enabled(iommu);
1855 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1858 if (amd_iommu_pre_enabled)
1859 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1861 if (amd_iommu_irq_remap) {
1862 ret = amd_iommu_create_irq_domain(iommu);
1868 * Make sure IOMMU is not considered to translate itself. The IVRS
1869 * table tells us so, but this is a lie!
1871 iommu->pci_seg->rlookup_table[iommu->devid] = NULL;
1877 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1878 * @ivrs: Pointer to the IVRS header
1880 * This function search through all IVDB of the maximum supported IVHD
1882 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1884 u8 *base = (u8 *)ivrs;
1885 struct ivhd_header *ivhd = (struct ivhd_header *)
1886 (base + IVRS_HEADER_LENGTH);
1887 u8 last_type = ivhd->type;
1888 u16 devid = ivhd->devid;
1890 while (((u8 *)ivhd - base < ivrs->length) &&
1891 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1892 u8 *p = (u8 *) ivhd;
1894 if (ivhd->devid == devid)
1895 last_type = ivhd->type;
1896 ivhd = (struct ivhd_header *)(p + ivhd->length);
1903 * Iterates over all IOMMU entries in the ACPI table, allocates the
1904 * IOMMU structure and initializes it with init_iommu_one()
1906 static int __init init_iommu_all(struct acpi_table_header *table)
1908 u8 *p = (u8 *)table, *end = (u8 *)table;
1909 struct ivhd_header *h;
1910 struct amd_iommu *iommu;
1913 end += table->length;
1914 p += IVRS_HEADER_LENGTH;
1916 /* Phase 1: Process all IVHD blocks */
1918 h = (struct ivhd_header *)p;
1919 if (*p == amd_iommu_target_ivhd_type) {
1921 DUMP_printk("device: %04x:%02x:%02x.%01x cap: %04x "
1922 "flags: %01x info %04x\n",
1923 h->pci_seg, PCI_BUS_NUM(h->devid),
1924 PCI_SLOT(h->devid), PCI_FUNC(h->devid),
1925 h->cap_ptr, h->flags, h->info);
1926 DUMP_printk(" mmio-addr: %016llx\n",
1929 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1933 ret = init_iommu_one(iommu, h, table);
1942 /* Phase 2 : Early feature support check */
1945 /* Phase 3 : Enabling IOMMU features */
1946 for_each_iommu(iommu) {
1947 ret = init_iommu_one_late(iommu);
1955 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1958 struct pci_dev *pdev = iommu->dev;
1960 if (!iommu_feature(iommu, FEATURE_PC))
1963 amd_iommu_pc_present = true;
1965 pci_info(pdev, "IOMMU performance counters supported\n");
1967 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1968 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1969 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1974 static ssize_t amd_iommu_show_cap(struct device *dev,
1975 struct device_attribute *attr,
1978 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1979 return sysfs_emit(buf, "%x\n", iommu->cap);
1981 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1983 static ssize_t amd_iommu_show_features(struct device *dev,
1984 struct device_attribute *attr,
1987 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1988 return sysfs_emit(buf, "%llx:%llx\n", iommu->features2, iommu->features);
1990 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1992 static struct attribute *amd_iommu_attrs[] = {
1994 &dev_attr_features.attr,
1998 static struct attribute_group amd_iommu_group = {
1999 .name = "amd-iommu",
2000 .attrs = amd_iommu_attrs,
2003 static const struct attribute_group *amd_iommu_groups[] = {
2009 * Note: IVHD 0x11 and 0x40 also contains exact copy
2010 * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
2011 * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
2013 static void __init late_iommu_features_init(struct amd_iommu *iommu)
2015 u64 features, features2;
2017 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
2020 /* read extended feature bits */
2021 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
2022 features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);
2024 if (!iommu->features) {
2025 iommu->features = features;
2026 iommu->features2 = features2;
2031 * Sanity check and warn if EFR values from
2032 * IVHD and MMIO conflict.
2034 if (features != iommu->features ||
2035 features2 != iommu->features2) {
2037 "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",
2038 features, iommu->features,
2039 features2, iommu->features2);
2043 static int __init iommu_init_pci(struct amd_iommu *iommu)
2045 int cap_ptr = iommu->cap_ptr;
2048 iommu->dev = pci_get_domain_bus_and_slot(iommu->pci_seg->id,
2049 PCI_BUS_NUM(iommu->devid),
2050 iommu->devid & 0xff);
2054 /* Prevent binding other PCI device drivers to IOMMU devices */
2055 iommu->dev->match_driver = false;
2057 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
2060 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
2061 amd_iommu_iotlb_sup = false;
2063 late_iommu_features_init(iommu);
2065 if (iommu_feature(iommu, FEATURE_GT)) {
2070 pasmax = iommu->features & FEATURE_PASID_MASK;
2071 pasmax >>= FEATURE_PASID_SHIFT;
2072 max_pasid = (1 << (pasmax + 1)) - 1;
2074 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
2076 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
2078 glxval = iommu->features & FEATURE_GLXVAL_MASK;
2079 glxval >>= FEATURE_GLXVAL_SHIFT;
2081 if (amd_iommu_max_glx_val == -1)
2082 amd_iommu_max_glx_val = glxval;
2084 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
2087 if (iommu_feature(iommu, FEATURE_GT) &&
2088 iommu_feature(iommu, FEATURE_PPR)) {
2089 iommu->is_iommu_v2 = true;
2090 amd_iommu_v2_present = true;
2093 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
2096 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
2097 pr_info("Using strict mode due to virtualization\n");
2098 iommu_set_dma_strict();
2099 amd_iommu_np_cache = true;
2102 init_iommu_perf_ctr(iommu);
2104 if (amd_iommu_pgtable == AMD_IOMMU_V2) {
2105 if (!iommu_feature(iommu, FEATURE_GIOSUP) ||
2106 !iommu_feature(iommu, FEATURE_GT)) {
2107 pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");
2108 amd_iommu_pgtable = AMD_IOMMU_V1;
2109 } else if (iommu_default_passthrough()) {
2110 pr_warn("V2 page table doesn't support passthrough mode. Fallback to v1.\n");
2111 amd_iommu_pgtable = AMD_IOMMU_V1;
2115 if (is_rd890_iommu(iommu->dev)) {
2119 pci_get_domain_bus_and_slot(iommu->pci_seg->id,
2120 iommu->dev->bus->number,
2124 * Some rd890 systems may not be fully reconfigured by the
2125 * BIOS, so it's necessary for us to store this information so
2126 * it can be reprogrammed on resume
2128 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
2129 &iommu->stored_addr_lo);
2130 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
2131 &iommu->stored_addr_hi);
2133 /* Low bit locks writes to configuration space */
2134 iommu->stored_addr_lo &= ~1;
2136 for (i = 0; i < 6; i++)
2137 for (j = 0; j < 0x12; j++)
2138 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
2140 for (i = 0; i < 0x83; i++)
2141 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
2144 amd_iommu_erratum_746_workaround(iommu);
2145 amd_iommu_ats_write_check_workaround(iommu);
2147 ret = iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
2148 amd_iommu_groups, "ivhd%d", iommu->index);
2152 iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
2154 return pci_enable_device(iommu->dev);
2157 static void print_iommu_info(void)
2159 static const char * const feat_str[] = {
2160 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
2161 "IA", "GA", "HE", "PC"
2163 struct amd_iommu *iommu;
2165 for_each_iommu(iommu) {
2166 struct pci_dev *pdev = iommu->dev;
2169 pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);
2171 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
2172 pr_info("Extended features (%#llx, %#llx):", iommu->features, iommu->features2);
2174 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
2175 if (iommu_feature(iommu, (1ULL << i)))
2176 pr_cont(" %s", feat_str[i]);
2179 if (iommu->features & FEATURE_GAM_VAPIC)
2180 pr_cont(" GA_vAPIC");
2182 if (iommu->features & FEATURE_SNP)
2188 if (irq_remapping_enabled) {
2189 pr_info("Interrupt remapping enabled\n");
2190 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2191 pr_info("X2APIC enabled\n");
2193 if (amd_iommu_pgtable == AMD_IOMMU_V2) {
2194 pr_info("V2 page table enabled (Paging mode : %d level)\n",
2195 amd_iommu_gpt_level);
2199 static int __init amd_iommu_init_pci(void)
2201 struct amd_iommu *iommu;
2202 struct amd_iommu_pci_seg *pci_seg;
2205 for_each_iommu(iommu) {
2206 ret = iommu_init_pci(iommu);
2208 pr_err("IOMMU%d: Failed to initialize IOMMU Hardware (error=%d)!\n",
2212 /* Need to setup range after PCI init */
2213 iommu_set_cwwb_range(iommu);
2217 * Order is important here to make sure any unity map requirements are
2218 * fulfilled. The unity mappings are created and written to the device
2219 * table during the iommu_init_pci() call.
2221 * After that we call init_device_table_dma() to make sure any
2222 * uninitialized DTE will block DMA, and in the end we flush the caches
2223 * of all IOMMUs to make sure the changes to the device table are
2226 for_each_pci_segment(pci_seg)
2227 init_device_table_dma(pci_seg);
2229 for_each_iommu(iommu)
2230 iommu_flush_all_caches(iommu);
2238 /****************************************************************************
2240 * The following functions initialize the MSI interrupts for all IOMMUs
2241 * in the system. It's a bit challenging because there could be multiple
2242 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
2245 ****************************************************************************/
2247 static int iommu_setup_msi(struct amd_iommu *iommu)
2251 r = pci_enable_msi(iommu->dev);
2255 r = request_threaded_irq(iommu->dev->irq,
2256 amd_iommu_int_handler,
2257 amd_iommu_int_thread,
2262 pci_disable_msi(iommu->dev);
2273 dest_mode_logical : 1,
2280 } __attribute__ ((packed));
2283 static struct irq_chip intcapxt_controller;
2285 static int intcapxt_irqdomain_activate(struct irq_domain *domain,
2286 struct irq_data *irqd, bool reserve)
2291 static void intcapxt_irqdomain_deactivate(struct irq_domain *domain,
2292 struct irq_data *irqd)
2297 static int intcapxt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2298 unsigned int nr_irqs, void *arg)
2300 struct irq_alloc_info *info = arg;
2303 if (!info || info->type != X86_IRQ_ALLOC_TYPE_AMDVI)
2306 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
2310 for (i = virq; i < virq + nr_irqs; i++) {
2311 struct irq_data *irqd = irq_domain_get_irq_data(domain, i);
2313 irqd->chip = &intcapxt_controller;
2314 irqd->chip_data = info->data;
2315 __irq_set_handler(i, handle_edge_irq, 0, "edge");
2321 static void intcapxt_irqdomain_free(struct irq_domain *domain, unsigned int virq,
2322 unsigned int nr_irqs)
2324 irq_domain_free_irqs_top(domain, virq, nr_irqs);
2328 static void intcapxt_unmask_irq(struct irq_data *irqd)
2330 struct amd_iommu *iommu = irqd->chip_data;
2331 struct irq_cfg *cfg = irqd_cfg(irqd);
2335 xt.dest_mode_logical = apic->dest_mode_logical;
2336 xt.vector = cfg->vector;
2337 xt.destid_0_23 = cfg->dest_apicid & GENMASK(23, 0);
2338 xt.destid_24_31 = cfg->dest_apicid >> 24;
2341 * Current IOMMU implementation uses the same IRQ for all
2342 * 3 IOMMU interrupts.
2344 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2345 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2346 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2349 static void intcapxt_mask_irq(struct irq_data *irqd)
2351 struct amd_iommu *iommu = irqd->chip_data;
2353 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2354 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2355 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2359 static int intcapxt_set_affinity(struct irq_data *irqd,
2360 const struct cpumask *mask, bool force)
2362 struct irq_data *parent = irqd->parent_data;
2365 ret = parent->chip->irq_set_affinity(parent, mask, force);
2366 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
2371 static int intcapxt_set_wake(struct irq_data *irqd, unsigned int on)
2373 return on ? -EOPNOTSUPP : 0;
2376 static struct irq_chip intcapxt_controller = {
2377 .name = "IOMMU-MSI",
2378 .irq_unmask = intcapxt_unmask_irq,
2379 .irq_mask = intcapxt_mask_irq,
2380 .irq_ack = irq_chip_ack_parent,
2381 .irq_retrigger = irq_chip_retrigger_hierarchy,
2382 .irq_set_affinity = intcapxt_set_affinity,
2383 .irq_set_wake = intcapxt_set_wake,
2384 .flags = IRQCHIP_MASK_ON_SUSPEND,
2387 static const struct irq_domain_ops intcapxt_domain_ops = {
2388 .alloc = intcapxt_irqdomain_alloc,
2389 .free = intcapxt_irqdomain_free,
2390 .activate = intcapxt_irqdomain_activate,
2391 .deactivate = intcapxt_irqdomain_deactivate,
2395 static struct irq_domain *iommu_irqdomain;
2397 static struct irq_domain *iommu_get_irqdomain(void)
2399 struct fwnode_handle *fn;
2401 /* No need for locking here (yet) as the init is single-threaded */
2402 if (iommu_irqdomain)
2403 return iommu_irqdomain;
2405 fn = irq_domain_alloc_named_fwnode("AMD-Vi-MSI");
2409 iommu_irqdomain = irq_domain_create_hierarchy(x86_vector_domain, 0, 0,
2410 fn, &intcapxt_domain_ops,
2412 if (!iommu_irqdomain)
2413 irq_domain_free_fwnode(fn);
2415 return iommu_irqdomain;
2418 static int iommu_setup_intcapxt(struct amd_iommu *iommu)
2420 struct irq_domain *domain;
2421 struct irq_alloc_info info;
2423 int node = dev_to_node(&iommu->dev->dev);
2425 domain = iommu_get_irqdomain();
2429 init_irq_alloc_info(&info, NULL);
2430 info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
2433 irq = irq_domain_alloc_irqs(domain, 1, node, &info);
2435 irq_domain_remove(domain);
2439 ret = request_threaded_irq(irq, amd_iommu_int_handler,
2440 amd_iommu_int_thread, 0, "AMD-Vi", iommu);
2442 irq_domain_free_irqs(irq, 1);
2443 irq_domain_remove(domain);
2450 static int iommu_init_irq(struct amd_iommu *iommu)
2454 if (iommu->int_enabled)
2457 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2458 ret = iommu_setup_intcapxt(iommu);
2459 else if (iommu->dev->msi_cap)
2460 ret = iommu_setup_msi(iommu);
2467 iommu->int_enabled = true;
2470 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2471 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2473 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2475 if (iommu->ppr_log != NULL)
2476 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
2480 /****************************************************************************
2482 * The next functions belong to the third pass of parsing the ACPI
2483 * table. In this last pass the memory mapping requirements are
2484 * gathered (like exclusion and unity mapping ranges).
2486 ****************************************************************************/
2488 static void __init free_unity_maps(void)
2490 struct unity_map_entry *entry, *next;
2491 struct amd_iommu_pci_seg *p, *pci_seg;
2493 for_each_pci_segment_safe(pci_seg, p) {
2494 list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
2495 list_del(&entry->list);
2501 /* called for unity map ACPI definition */
2502 static int __init init_unity_map_range(struct ivmd_header *m,
2503 struct acpi_table_header *ivrs_base)
2505 struct unity_map_entry *e = NULL;
2506 struct amd_iommu_pci_seg *pci_seg;
2509 pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
2510 if (pci_seg == NULL)
2513 e = kzalloc(sizeof(*e), GFP_KERNEL);
2521 case ACPI_IVMD_TYPE:
2522 s = "IVMD_TYPEi\t\t\t";
2523 e->devid_start = e->devid_end = m->devid;
2525 case ACPI_IVMD_TYPE_ALL:
2526 s = "IVMD_TYPE_ALL\t\t";
2528 e->devid_end = pci_seg->last_bdf;
2530 case ACPI_IVMD_TYPE_RANGE:
2531 s = "IVMD_TYPE_RANGE\t\t";
2532 e->devid_start = m->devid;
2533 e->devid_end = m->aux;
2536 e->address_start = PAGE_ALIGN(m->range_start);
2537 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2538 e->prot = m->flags >> 1;
2541 * Treat per-device exclusion ranges as r/w unity-mapped regions
2542 * since some buggy BIOSes might lead to the overwritten exclusion
2543 * range (exclusion_start and exclusion_length members). This
2544 * happens when there are multiple exclusion ranges (IVMD entries)
2545 * defined in ACPI table.
2547 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2548 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
2550 DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "
2551 "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"
2552 " flags: %x\n", s, m->pci_seg,
2553 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2554 PCI_FUNC(e->devid_start), m->pci_seg,
2555 PCI_BUS_NUM(e->devid_end),
2556 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2557 e->address_start, e->address_end, m->flags);
2559 list_add_tail(&e->list, &pci_seg->unity_map);
2564 /* iterates over all memory definitions we find in the ACPI table */
2565 static int __init init_memory_definitions(struct acpi_table_header *table)
2567 u8 *p = (u8 *)table, *end = (u8 *)table;
2568 struct ivmd_header *m;
2570 end += table->length;
2571 p += IVRS_HEADER_LENGTH;
2574 m = (struct ivmd_header *)p;
2575 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
2576 init_unity_map_range(m, table);
2585 * Init the device table to not allow DMA access for devices
2587 static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
2590 struct dev_table_entry *dev_table = pci_seg->dev_table;
2592 if (dev_table == NULL)
2595 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
2596 __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_VALID);
2597 if (!amd_iommu_snp_en)
2598 __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_TRANSLATION);
2602 static void __init uninit_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
2605 struct dev_table_entry *dev_table = pci_seg->dev_table;
2607 if (dev_table == NULL)
2610 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
2611 dev_table[devid].data[0] = 0ULL;
2612 dev_table[devid].data[1] = 0ULL;
2616 static void init_device_table(void)
2618 struct amd_iommu_pci_seg *pci_seg;
2621 if (!amd_iommu_irq_remap)
2624 for_each_pci_segment(pci_seg) {
2625 for (devid = 0; devid <= pci_seg->last_bdf; ++devid)
2626 __set_dev_entry_bit(pci_seg->dev_table,
2627 devid, DEV_ENTRY_IRQ_TBL_EN);
2631 static void iommu_init_flags(struct amd_iommu *iommu)
2633 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2634 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2635 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2637 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2638 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2639 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2641 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2642 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2643 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2645 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2646 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2647 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2650 * make IOMMU memory accesses cache coherent
2652 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2654 /* Set IOTLB invalidation timeout to 1s */
2655 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2658 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2661 u32 ioc_feature_control;
2662 struct pci_dev *pdev = iommu->root_pdev;
2664 /* RD890 BIOSes may not have completely reconfigured the iommu */
2665 if (!is_rd890_iommu(iommu->dev) || !pdev)
2669 * First, we need to ensure that the iommu is enabled. This is
2670 * controlled by a register in the northbridge
2673 /* Select Northbridge indirect register 0x75 and enable writing */
2674 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2675 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2677 /* Enable the iommu */
2678 if (!(ioc_feature_control & 0x1))
2679 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2681 /* Restore the iommu BAR */
2682 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2683 iommu->stored_addr_lo);
2684 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2685 iommu->stored_addr_hi);
2687 /* Restore the l1 indirect regs for each of the 6 l1s */
2688 for (i = 0; i < 6; i++)
2689 for (j = 0; j < 0x12; j++)
2690 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2692 /* Restore the l2 indirect regs */
2693 for (i = 0; i < 0x83; i++)
2694 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2696 /* Lock PCI setup registers */
2697 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2698 iommu->stored_addr_lo | 1);
2701 static void iommu_enable_ga(struct amd_iommu *iommu)
2703 #ifdef CONFIG_IRQ_REMAP
2704 switch (amd_iommu_guest_ir) {
2705 case AMD_IOMMU_GUEST_IR_VAPIC:
2706 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2707 iommu_feature_enable(iommu, CONTROL_GA_EN);
2708 iommu->irte_ops = &irte_128_ops;
2711 iommu->irte_ops = &irte_32_ops;
2717 static void iommu_disable_irtcachedis(struct amd_iommu *iommu)
2719 iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
2722 static void iommu_enable_irtcachedis(struct amd_iommu *iommu)
2726 if (!amd_iommu_irtcachedis)
2731 * The support for IRTCacheDis feature is dertermined by
2732 * checking if the bit is writable.
2734 iommu_feature_enable(iommu, CONTROL_IRTCACHEDIS);
2735 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
2736 ctrl &= (1ULL << CONTROL_IRTCACHEDIS);
2738 iommu->irtcachedis_enabled = true;
2739 pr_info("iommu%d (%#06x) : IRT cache is %s\n",
2740 iommu->index, iommu->devid,
2741 iommu->irtcachedis_enabled ? "disabled" : "enabled");
2744 static void early_enable_iommu(struct amd_iommu *iommu)
2746 iommu_disable(iommu);
2747 iommu_init_flags(iommu);
2748 iommu_set_device_table(iommu);
2749 iommu_enable_command_buffer(iommu);
2750 iommu_enable_event_buffer(iommu);
2751 iommu_set_exclusion_range(iommu);
2752 iommu_enable_ga(iommu);
2753 iommu_enable_xt(iommu);
2754 iommu_enable_irtcachedis(iommu);
2755 iommu_enable(iommu);
2756 iommu_flush_all_caches(iommu);
2760 * This function finally enables all IOMMUs found in the system after
2761 * they have been initialized.
2763 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2764 * the old content of device table entries. Not this case or copy failed,
2765 * just continue as normal kernel does.
2767 static void early_enable_iommus(void)
2769 struct amd_iommu *iommu;
2770 struct amd_iommu_pci_seg *pci_seg;
2772 if (!copy_device_table()) {
2774 * If come here because of failure in copying device table from old
2775 * kernel with all IOMMUs enabled, print error message and try to
2776 * free allocated old_dev_tbl_cpy.
2778 if (amd_iommu_pre_enabled)
2779 pr_err("Failed to copy DEV table from previous kernel.\n");
2781 for_each_pci_segment(pci_seg) {
2782 if (pci_seg->old_dev_tbl_cpy != NULL) {
2783 free_pages((unsigned long)pci_seg->old_dev_tbl_cpy,
2784 get_order(pci_seg->dev_table_size));
2785 pci_seg->old_dev_tbl_cpy = NULL;
2789 for_each_iommu(iommu) {
2790 clear_translation_pre_enabled(iommu);
2791 early_enable_iommu(iommu);
2794 pr_info("Copied DEV table from previous kernel.\n");
2796 for_each_pci_segment(pci_seg) {
2797 free_pages((unsigned long)pci_seg->dev_table,
2798 get_order(pci_seg->dev_table_size));
2799 pci_seg->dev_table = pci_seg->old_dev_tbl_cpy;
2802 for_each_iommu(iommu) {
2803 iommu_disable_command_buffer(iommu);
2804 iommu_disable_event_buffer(iommu);
2805 iommu_disable_irtcachedis(iommu);
2806 iommu_enable_command_buffer(iommu);
2807 iommu_enable_event_buffer(iommu);
2808 iommu_enable_ga(iommu);
2809 iommu_enable_xt(iommu);
2810 iommu_enable_irtcachedis(iommu);
2811 iommu_set_device_table(iommu);
2812 iommu_flush_all_caches(iommu);
2817 static void enable_iommus_v2(void)
2819 struct amd_iommu *iommu;
2821 for_each_iommu(iommu) {
2822 iommu_enable_ppr_log(iommu);
2823 iommu_enable_gt(iommu);
2827 static void enable_iommus_vapic(void)
2829 #ifdef CONFIG_IRQ_REMAP
2831 struct amd_iommu *iommu;
2833 for_each_iommu(iommu) {
2835 * Disable GALog if already running. It could have been enabled
2836 * in the previous boot before kdump.
2838 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
2839 if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
2842 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
2843 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
2846 * Need to set and poll check the GALOGRun bit to zero before
2847 * we can set/ modify GA Log registers safely.
2849 for (i = 0; i < LOOP_TIMEOUT; ++i) {
2850 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
2851 if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
2856 if (WARN_ON(i >= LOOP_TIMEOUT))
2860 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2861 !check_feature_on_all_iommus(FEATURE_GAM_VAPIC)) {
2862 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2866 if (amd_iommu_snp_en &&
2867 !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {
2868 pr_warn("Force to disable Virtual APIC due to SNP\n");
2869 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2873 /* Enabling GAM and SNPAVIC support */
2874 for_each_iommu(iommu) {
2875 if (iommu_init_ga_log(iommu) ||
2876 iommu_ga_log_enable(iommu))
2879 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2880 if (amd_iommu_snp_en)
2881 iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);
2884 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2885 pr_info("Virtual APIC enabled\n");
2889 static void enable_iommus(void)
2891 early_enable_iommus();
2892 enable_iommus_vapic();
2896 static void disable_iommus(void)
2898 struct amd_iommu *iommu;
2900 for_each_iommu(iommu)
2901 iommu_disable(iommu);
2903 #ifdef CONFIG_IRQ_REMAP
2904 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2905 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2910 * Suspend/Resume support
2911 * disable suspend until real resume implemented
2914 static void amd_iommu_resume(void)
2916 struct amd_iommu *iommu;
2918 for_each_iommu(iommu)
2919 iommu_apply_resume_quirks(iommu);
2921 /* re-load the hardware */
2924 amd_iommu_enable_interrupts();
2927 static int amd_iommu_suspend(void)
2929 /* disable IOMMUs to go out of the way for BIOS */
2935 static struct syscore_ops amd_iommu_syscore_ops = {
2936 .suspend = amd_iommu_suspend,
2937 .resume = amd_iommu_resume,
2940 static void __init free_iommu_resources(void)
2942 kmem_cache_destroy(amd_iommu_irq_cache);
2943 amd_iommu_irq_cache = NULL;
2946 free_pci_segments();
2949 /* SB IOAPIC is always on this device in AMD systems */
2950 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2952 static bool __init check_ioapic_information(void)
2954 const char *fw_bug = FW_BUG;
2955 bool ret, has_sb_ioapic;
2958 has_sb_ioapic = false;
2962 * If we have map overrides on the kernel command line the
2963 * messages in this function might not describe firmware bugs
2964 * anymore - so be careful
2969 for (idx = 0; idx < nr_ioapics; idx++) {
2970 int devid, id = mpc_ioapic_id(idx);
2972 devid = get_ioapic_devid(id);
2974 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2977 } else if (devid == IOAPIC_SB_DEVID) {
2978 has_sb_ioapic = true;
2983 if (!has_sb_ioapic) {
2985 * We expect the SB IOAPIC to be listed in the IVRS
2986 * table. The system timer is connected to the SB IOAPIC
2987 * and if we don't have it in the list the system will
2988 * panic at boot time. This situation usually happens
2989 * when the BIOS is buggy and provides us the wrong
2990 * device id for the IOAPIC in the system.
2992 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2996 pr_err("Disabling interrupt remapping\n");
3001 static void __init free_dma_resources(void)
3003 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
3004 get_order(MAX_DOMAIN_ID/8));
3005 amd_iommu_pd_alloc_bitmap = NULL;
3010 static void __init ivinfo_init(void *ivrs)
3012 amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
3016 * This is the hardware init function for AMD IOMMU in the system.
3017 * This function is called either from amd_iommu_init or from the interrupt
3018 * remapping setup code.
3020 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
3023 * 1 pass) Discover the most comprehensive IVHD type to use.
3025 * 2 pass) Find the highest PCI device id the driver has to handle.
3026 * Upon this information the size of the data structures is
3027 * determined that needs to be allocated.
3029 * 3 pass) Initialize the data structures just allocated with the
3030 * information in the ACPI table about available AMD IOMMUs
3031 * in the system. It also maps the PCI devices in the
3032 * system to specific IOMMUs
3034 * 4 pass) After the basic data structures are allocated and
3035 * initialized we update them with information about memory
3036 * remapping requirements parsed out of the ACPI table in
3039 * After everything is set up the IOMMUs are enabled and the necessary
3040 * hotplug and suspend notifiers are registered.
3042 static int __init early_amd_iommu_init(void)
3044 struct acpi_table_header *ivrs_base;
3045 int remap_cache_sz, ret;
3048 if (!amd_iommu_detected)
3051 status = acpi_get_table("IVRS", 0, &ivrs_base);
3052 if (status == AE_NOT_FOUND)
3054 else if (ACPI_FAILURE(status)) {
3055 const char *err = acpi_format_exception(status);
3056 pr_err("IVRS table error: %s\n", err);
3061 * Validate checksum here so we don't need to do it when
3062 * we actually parse the table
3064 ret = check_ivrs_checksum(ivrs_base);
3068 ivinfo_init(ivrs_base);
3070 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
3071 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
3073 /* Device table - directly used by all IOMMUs */
3076 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
3077 GFP_KERNEL | __GFP_ZERO,
3078 get_order(MAX_DOMAIN_ID/8));
3079 if (amd_iommu_pd_alloc_bitmap == NULL)
3083 * never allocate domain 0 because its used as the non-allocated and
3084 * error value placeholder
3086 __set_bit(0, amd_iommu_pd_alloc_bitmap);
3089 * now the data structures are allocated and basically initialized
3090 * start the real acpi table scan
3092 ret = init_iommu_all(ivrs_base);
3096 /* 5 level guest page table */
3097 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
3098 check_feature_gpt_level() == GUEST_PGTABLE_5_LEVEL)
3099 amd_iommu_gpt_level = PAGE_MODE_5_LEVEL;
3101 /* Disable any previously enabled IOMMUs */
3102 if (!is_kdump_kernel() || amd_iommu_disabled)
3105 if (amd_iommu_irq_remap)
3106 amd_iommu_irq_remap = check_ioapic_information();
3108 if (amd_iommu_irq_remap) {
3109 struct amd_iommu_pci_seg *pci_seg;
3111 * Interrupt remapping enabled, create kmem_cache for the
3115 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3116 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
3118 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
3119 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
3121 DTE_INTTAB_ALIGNMENT,
3123 if (!amd_iommu_irq_cache)
3126 for_each_pci_segment(pci_seg) {
3127 if (alloc_irq_lookup_table(pci_seg))
3132 ret = init_memory_definitions(ivrs_base);
3136 /* init the device table */
3137 init_device_table();
3140 /* Don't leak any ACPI memory */
3141 acpi_put_table(ivrs_base);
3146 static int amd_iommu_enable_interrupts(void)
3148 struct amd_iommu *iommu;
3151 for_each_iommu(iommu) {
3152 ret = iommu_init_irq(iommu);
3161 static bool __init detect_ivrs(void)
3163 struct acpi_table_header *ivrs_base;
3167 status = acpi_get_table("IVRS", 0, &ivrs_base);
3168 if (status == AE_NOT_FOUND)
3170 else if (ACPI_FAILURE(status)) {
3171 const char *err = acpi_format_exception(status);
3172 pr_err("IVRS table error: %s\n", err);
3176 acpi_put_table(ivrs_base);
3178 if (amd_iommu_force_enable)
3181 /* Don't use IOMMU if there is Stoney Ridge graphics */
3182 for (i = 0; i < 32; i++) {
3185 pci_id = read_pci_config(0, i, 0, 0);
3186 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
3187 pr_info("Disable IOMMU on Stoney Ridge\n");
3193 /* Make sure ACS will be enabled during PCI probe */
3199 /****************************************************************************
3201 * AMD IOMMU Initialization State Machine
3203 ****************************************************************************/
3205 static int __init state_next(void)
3209 switch (init_state) {
3210 case IOMMU_START_STATE:
3211 if (!detect_ivrs()) {
3212 init_state = IOMMU_NOT_FOUND;
3215 init_state = IOMMU_IVRS_DETECTED;
3218 case IOMMU_IVRS_DETECTED:
3219 if (amd_iommu_disabled) {
3220 init_state = IOMMU_CMDLINE_DISABLED;
3223 ret = early_amd_iommu_init();
3224 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
3227 case IOMMU_ACPI_FINISHED:
3228 early_enable_iommus();
3229 x86_platform.iommu_shutdown = disable_iommus;
3230 init_state = IOMMU_ENABLED;
3233 register_syscore_ops(&amd_iommu_syscore_ops);
3234 ret = amd_iommu_init_pci();
3235 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
3236 enable_iommus_vapic();
3239 case IOMMU_PCI_INIT:
3240 ret = amd_iommu_enable_interrupts();
3241 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
3243 case IOMMU_INTERRUPTS_EN:
3244 init_state = IOMMU_INITIALIZED;
3246 case IOMMU_INITIALIZED:
3249 case IOMMU_NOT_FOUND:
3250 case IOMMU_INIT_ERROR:
3251 case IOMMU_CMDLINE_DISABLED:
3252 /* Error states => do nothing */
3261 free_dma_resources();
3262 if (!irq_remapping_enabled) {
3264 free_iommu_resources();
3266 struct amd_iommu *iommu;
3267 struct amd_iommu_pci_seg *pci_seg;
3269 for_each_pci_segment(pci_seg)
3270 uninit_device_table_dma(pci_seg);
3272 for_each_iommu(iommu)
3273 iommu_flush_all_caches(iommu);
3279 static int __init iommu_go_to_state(enum iommu_init_state state)
3283 while (init_state != state) {
3284 if (init_state == IOMMU_NOT_FOUND ||
3285 init_state == IOMMU_INIT_ERROR ||
3286 init_state == IOMMU_CMDLINE_DISABLED)
3294 #ifdef CONFIG_IRQ_REMAP
3295 int __init amd_iommu_prepare(void)
3299 amd_iommu_irq_remap = true;
3301 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
3303 amd_iommu_irq_remap = false;
3307 return amd_iommu_irq_remap ? 0 : -ENODEV;
3310 int __init amd_iommu_enable(void)
3314 ret = iommu_go_to_state(IOMMU_ENABLED);
3318 irq_remapping_enabled = 1;
3319 return amd_iommu_xt_mode;
3322 void amd_iommu_disable(void)
3324 amd_iommu_suspend();
3327 int amd_iommu_reenable(int mode)
3334 int __init amd_iommu_enable_faulting(void)
3336 /* We enable MSI later when PCI is initialized */
3342 * This is the core init function for AMD IOMMU hardware in the system.
3343 * This function is called from the generic x86 DMA layer initialization
3346 static int __init amd_iommu_init(void)
3348 struct amd_iommu *iommu;
3351 ret = iommu_go_to_state(IOMMU_INITIALIZED);
3352 #ifdef CONFIG_GART_IOMMU
3353 if (ret && list_empty(&amd_iommu_list)) {
3355 * We failed to initialize the AMD IOMMU - try fallback
3356 * to GART if possible.
3362 for_each_iommu(iommu)
3363 amd_iommu_debugfs_setup(iommu);
3368 static bool amd_iommu_sme_check(void)
3370 if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
3371 (boot_cpu_data.x86 != 0x17))
3374 /* For Fam17h, a specific level of support is required */
3375 if (boot_cpu_data.microcode >= 0x08001205)
3378 if ((boot_cpu_data.microcode >= 0x08001126) &&
3379 (boot_cpu_data.microcode <= 0x080011ff))
3382 pr_notice("IOMMU not currently supported when SME is active\n");
3387 /****************************************************************************
3389 * Early detect code. This code runs at IOMMU detection time in the DMA
3390 * layer. It just looks if there is an IVRS ACPI table to detect AMD
3393 ****************************************************************************/
3394 int __init amd_iommu_detect(void)
3398 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
3401 if (!amd_iommu_sme_check())
3404 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
3408 amd_iommu_detected = true;
3410 x86_init.iommu.iommu_init = amd_iommu_init;
3415 /****************************************************************************
3417 * Parsing functions for the AMD IOMMU specific kernel command line
3420 ****************************************************************************/
3422 static int __init parse_amd_iommu_dump(char *str)
3424 amd_iommu_dump = true;
3429 static int __init parse_amd_iommu_intr(char *str)
3431 for (; *str; ++str) {
3432 if (strncmp(str, "legacy", 6) == 0) {
3433 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
3436 if (strncmp(str, "vapic", 5) == 0) {
3437 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
3444 static int __init parse_amd_iommu_options(char *str)
3450 if (strncmp(str, "fullflush", 9) == 0) {
3451 pr_warn("amd_iommu=fullflush deprecated; use iommu.strict=1 instead\n");
3452 iommu_set_dma_strict();
3453 } else if (strncmp(str, "force_enable", 12) == 0) {
3454 amd_iommu_force_enable = true;
3455 } else if (strncmp(str, "off", 3) == 0) {
3456 amd_iommu_disabled = true;
3457 } else if (strncmp(str, "force_isolation", 15) == 0) {
3458 amd_iommu_force_isolation = true;
3459 } else if (strncmp(str, "pgtbl_v1", 8) == 0) {
3460 amd_iommu_pgtable = AMD_IOMMU_V1;
3461 } else if (strncmp(str, "pgtbl_v2", 8) == 0) {
3462 amd_iommu_pgtable = AMD_IOMMU_V2;
3463 } else if (strncmp(str, "irtcachedis", 11) == 0) {
3464 amd_iommu_irtcachedis = true;
3466 pr_notice("Unknown option - '%s'\n", str);
3469 str += strcspn(str, ",");
3477 static int __init parse_ivrs_ioapic(char *str)
3479 u32 seg = 0, bus, dev, fn;
3483 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3484 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3487 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3488 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3489 pr_warn("ivrs_ioapic%s option format deprecated; use ivrs_ioapic=%d@%04x:%02x:%02x.%d instead\n",
3490 str, id, seg, bus, dev, fn);
3494 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
3498 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
3499 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
3504 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3506 cmdline_maps = true;
3507 i = early_ioapic_map_size++;
3508 early_ioapic_map[i].id = id;
3509 early_ioapic_map[i].devid = devid;
3510 early_ioapic_map[i].cmd_line = true;
3515 static int __init parse_ivrs_hpet(char *str)
3517 u32 seg = 0, bus, dev, fn;
3521 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3522 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3525 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3526 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3527 pr_warn("ivrs_hpet%s option format deprecated; use ivrs_hpet=%d@%04x:%02x:%02x.%d instead\n",
3528 str, id, seg, bus, dev, fn);
3532 pr_err("Invalid command line: ivrs_hpet%s\n", str);
3536 if (early_hpet_map_size == EARLY_MAP_SIZE) {
3537 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3542 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3544 cmdline_maps = true;
3545 i = early_hpet_map_size++;
3546 early_hpet_map[i].id = id;
3547 early_hpet_map[i].devid = devid;
3548 early_hpet_map[i].cmd_line = true;
3553 #define ACPIID_LEN (ACPIHID_UID_LEN + ACPIHID_HID_LEN)
3555 static int __init parse_ivrs_acpihid(char *str)
3557 u32 seg = 0, bus, dev, fn;
3558 char *hid, *uid, *p, *addr;
3559 char acpiid[ACPIID_LEN] = {0};
3562 addr = strchr(str, '@');
3564 addr = strchr(str, '=');
3570 if (strlen(addr) > ACPIID_LEN)
3573 if (sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid) == 4 ||
3574 sscanf(str, "[%x:%x:%x.%x]=%s", &seg, &bus, &dev, &fn, acpiid) == 5) {
3575 pr_warn("ivrs_acpihid%s option format deprecated; use ivrs_acpihid=%s@%04x:%02x:%02x.%d instead\n",
3576 str, acpiid, seg, bus, dev, fn);
3582 /* We have the '@', make it the terminator to get just the acpiid */
3585 if (strlen(str) > ACPIID_LEN + 1)
3588 if (sscanf(str, "=%s", acpiid) != 1)
3591 if (sscanf(addr, "%x:%x.%x", &bus, &dev, &fn) == 3 ||
3592 sscanf(addr, "%x:%x:%x.%x", &seg, &bus, &dev, &fn) == 4)
3596 pr_err("Invalid command line: ivrs_acpihid%s\n", str);
3601 hid = strsep(&p, ":");
3604 if (!hid || !(*hid) || !uid) {
3605 pr_err("Invalid command line: hid or uid\n");
3610 * Ignore leading zeroes after ':', so e.g., AMDI0095:00
3611 * will match AMDI0095:0 in the second strcmp in acpi_dev_hid_uid_match
3613 while (*uid == '0' && *(uid + 1))
3616 i = early_acpihid_map_size++;
3617 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3618 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3619 early_acpihid_map[i].devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3620 early_acpihid_map[i].cmd_line = true;
3625 __setup("amd_iommu_dump", parse_amd_iommu_dump);
3626 __setup("amd_iommu=", parse_amd_iommu_options);
3627 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
3628 __setup("ivrs_ioapic", parse_ivrs_ioapic);
3629 __setup("ivrs_hpet", parse_ivrs_hpet);
3630 __setup("ivrs_acpihid", parse_ivrs_acpihid);
3632 bool amd_iommu_v2_supported(void)
3634 /* CPU page table size should match IOMMU guest page table size */
3635 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
3636 amd_iommu_gpt_level != PAGE_MODE_5_LEVEL)
3640 * Since DTE[Mode]=0 is prohibited on SNP-enabled system
3641 * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without
3642 * setting up IOMMUv1 page table.
3644 return amd_iommu_v2_present && !amd_iommu_snp_en;
3646 EXPORT_SYMBOL(amd_iommu_v2_supported);
3648 struct amd_iommu *get_amd_iommu(unsigned int idx)
3651 struct amd_iommu *iommu;
3653 for_each_iommu(iommu)
3659 /****************************************************************************
3661 * IOMMU EFR Performance Counter support functionality. This code allows
3662 * access to the IOMMU PC functionality.
3664 ****************************************************************************/
3666 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3668 struct amd_iommu *iommu = get_amd_iommu(idx);
3671 return iommu->max_banks;
3675 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3677 bool amd_iommu_pc_supported(void)
3679 return amd_iommu_pc_present;
3681 EXPORT_SYMBOL(amd_iommu_pc_supported);
3683 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3685 struct amd_iommu *iommu = get_amd_iommu(idx);
3688 return iommu->max_counters;
3692 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3694 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3695 u8 fxn, u64 *value, bool is_write)
3700 /* Make sure the IOMMU PC resource is available */
3701 if (!amd_iommu_pc_present)
3704 /* Check for valid iommu and pc register indexing */
3705 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3708 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3710 /* Limit the offset to the hw defined mmio region aperture */
3711 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3712 (iommu->max_counters << 8) | 0x28);
3713 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3714 (offset > max_offset_lim))
3718 u64 val = *value & GENMASK_ULL(47, 0);
3720 writel((u32)val, iommu->mmio_base + offset);
3721 writel((val >> 32), iommu->mmio_base + offset + 4);
3723 *value = readl(iommu->mmio_base + offset + 4);
3725 *value |= readl(iommu->mmio_base + offset);
3726 *value &= GENMASK_ULL(47, 0);
3732 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3737 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3740 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3745 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3748 #ifdef CONFIG_AMD_MEM_ENCRYPT
3749 int amd_iommu_snp_enable(void)
3752 * The SNP support requires that IOMMU must be enabled, and is
3753 * not configured in the passthrough mode.
3755 if (no_iommu || iommu_default_passthrough()) {
3756 pr_err("SNP: IOMMU is disabled or configured in passthrough mode, SNP cannot be supported");
3761 * Prevent enabling SNP after IOMMU_ENABLED state because this process
3762 * affect how IOMMU driver sets up data structures and configures
3765 if (init_state > IOMMU_ENABLED) {
3766 pr_err("SNP: Too late to enable SNP for IOMMU.\n");
3770 amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP);
3771 if (!amd_iommu_snp_en)
3774 pr_info("SNP enabled\n");
3776 /* Enforce IOMMU v1 pagetable when SNP is enabled. */
3777 if (amd_iommu_pgtable != AMD_IOMMU_V1) {
3778 pr_warn("Force to using AMD IOMMU v1 page table due to SNP\n");
3779 amd_iommu_pgtable = AMD_IOMMU_V1;