1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 * Leo Duran <leo.duran@amd.com>
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 int amd_iommu_target_ivhd_type;
167 /* Global EFR and EFR2 registers */
171 /* SNP is enabled on the system? */
172 bool amd_iommu_snp_en;
173 EXPORT_SYMBOL(amd_iommu_snp_en);
175 LIST_HEAD(amd_iommu_pci_seg_list); /* list of all PCI segments */
176 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
179 /* Array to assign indices to IOMMUs*/
180 struct amd_iommu *amd_iommus[MAX_IOMMUS];
182 /* Number of IOMMUs present in the system */
183 static int amd_iommus_present;
185 /* IOMMUs have a non-present cache? */
186 bool amd_iommu_np_cache __read_mostly;
187 bool amd_iommu_iotlb_sup __read_mostly = true;
189 u32 amd_iommu_max_pasid __read_mostly = ~0;
191 bool amd_iommu_v2_present __read_mostly;
192 static bool amd_iommu_pc_present __read_mostly;
193 bool amdr_ivrs_remap_support __read_mostly;
195 bool amd_iommu_force_isolation __read_mostly;
198 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
199 * to know which ones are already in use.
201 unsigned long *amd_iommu_pd_alloc_bitmap;
203 enum iommu_init_state {
213 IOMMU_CMDLINE_DISABLED,
216 /* Early ioapic and hpet maps from kernel command line */
217 #define EARLY_MAP_SIZE 4
218 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
219 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
220 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
222 static int __initdata early_ioapic_map_size;
223 static int __initdata early_hpet_map_size;
224 static int __initdata early_acpihid_map_size;
226 static bool __initdata cmdline_maps;
228 static enum iommu_init_state init_state = IOMMU_START_STATE;
230 static int amd_iommu_enable_interrupts(void);
231 static int __init iommu_go_to_state(enum iommu_init_state state);
232 static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg);
234 static bool amd_iommu_pre_enabled = true;
236 static u32 amd_iommu_ivinfo __initdata;
238 bool translation_pre_enabled(struct amd_iommu *iommu)
240 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
243 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
245 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
248 static void init_translation_status(struct amd_iommu *iommu)
252 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
253 if (ctrl & (1<<CONTROL_IOMMU_EN))
254 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
257 static inline unsigned long tbl_size(int entry_size, int last_bdf)
259 unsigned shift = PAGE_SHIFT +
260 get_order((last_bdf + 1) * entry_size);
265 int amd_iommu_get_num_iommus(void)
267 return amd_iommus_present;
271 * Iterate through all the IOMMUs to get common EFR
272 * masks among all IOMMUs and warn if found inconsistency.
274 static void get_global_efr(void)
276 struct amd_iommu *iommu;
278 for_each_iommu(iommu) {
279 u64 tmp = iommu->features;
280 u64 tmp2 = iommu->features2;
282 if (list_is_first(&iommu->list, &amd_iommu_list)) {
284 amd_iommu_efr2 = tmp2;
288 if (amd_iommu_efr == tmp &&
289 amd_iommu_efr2 == tmp2)
293 "Found inconsistent EFR/EFR2 %#llx,%#llx (global %#llx,%#llx) on iommu%d (%04x:%02x:%02x.%01x).\n",
294 tmp, tmp2, amd_iommu_efr, amd_iommu_efr2,
295 iommu->index, iommu->pci_seg->id,
296 PCI_BUS_NUM(iommu->devid), PCI_SLOT(iommu->devid),
297 PCI_FUNC(iommu->devid));
299 amd_iommu_efr &= tmp;
300 amd_iommu_efr2 &= tmp2;
303 pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);
306 static bool check_feature_on_all_iommus(u64 mask)
308 return !!(amd_iommu_efr & mask);
311 static inline int check_feature_gpt_level(void)
313 return ((amd_iommu_efr >> FEATURE_GATS_SHIFT) & FEATURE_GATS_MASK);
317 * For IVHD type 0x11/0x40, EFR is also available via IVHD.
318 * Default to IVHD EFR since it is available sooner
319 * (i.e. before PCI init).
321 static void __init early_iommu_features_init(struct amd_iommu *iommu,
322 struct ivhd_header *h)
324 if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP) {
325 iommu->features = h->efr_reg;
326 iommu->features2 = h->efr_reg2;
328 if (amd_iommu_ivinfo & IOMMU_IVINFO_DMA_REMAP)
329 amdr_ivrs_remap_support = true;
332 /* Access to l1 and l2 indexed register spaces */
334 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
338 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
339 pci_read_config_dword(iommu->dev, 0xfc, &val);
343 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
345 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
346 pci_write_config_dword(iommu->dev, 0xfc, val);
347 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
350 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
354 pci_write_config_dword(iommu->dev, 0xf0, address);
355 pci_read_config_dword(iommu->dev, 0xf4, &val);
359 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
361 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
362 pci_write_config_dword(iommu->dev, 0xf4, val);
365 /****************************************************************************
367 * AMD IOMMU MMIO register space handling functions
369 * These functions are used to program the IOMMU device registers in
370 * MMIO space required for that driver.
372 ****************************************************************************/
375 * This function set the exclusion range in the IOMMU. DMA accesses to the
376 * exclusion range are passed through untranslated
378 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
380 u64 start = iommu->exclusion_start & PAGE_MASK;
381 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
384 if (!iommu->exclusion_start)
387 entry = start | MMIO_EXCL_ENABLE_MASK;
388 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
389 &entry, sizeof(entry));
392 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
393 &entry, sizeof(entry));
396 static void iommu_set_cwwb_range(struct amd_iommu *iommu)
398 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
399 u64 entry = start & PM_ADDR_MASK;
401 if (!check_feature_on_all_iommus(FEATURE_SNP))
405 * Re-purpose Exclusion base/limit registers for Completion wait
406 * write-back base/limit.
408 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
409 &entry, sizeof(entry));
412 * Default to 4 Kbytes, which can be specified by setting base
413 * address equal to the limit address.
415 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
416 &entry, sizeof(entry));
419 /* Programs the physical address of the device table into the IOMMU hardware */
420 static void iommu_set_device_table(struct amd_iommu *iommu)
423 u32 dev_table_size = iommu->pci_seg->dev_table_size;
424 void *dev_table = (void *)get_dev_table(iommu);
426 BUG_ON(iommu->mmio_base == NULL);
428 entry = iommu_virt_to_phys(dev_table);
429 entry |= (dev_table_size >> 12) - 1;
430 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
431 &entry, sizeof(entry));
434 /* Generic functions to enable/disable certain features of the IOMMU. */
435 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
439 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
440 ctrl |= (1ULL << bit);
441 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
444 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
448 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
449 ctrl &= ~(1ULL << bit);
450 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
453 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
457 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
458 ctrl &= ~CTRL_INV_TO_MASK;
459 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
460 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
463 /* Function to enable the hardware */
464 static void iommu_enable(struct amd_iommu *iommu)
466 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
469 static void iommu_disable(struct amd_iommu *iommu)
471 if (!iommu->mmio_base)
474 /* Disable command buffer */
475 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
477 /* Disable event logging and event interrupts */
478 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
479 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
481 /* Disable IOMMU GA_LOG */
482 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
483 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
485 /* Disable IOMMU hardware itself */
486 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
490 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
491 * the system has one.
493 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
495 if (!request_mem_region(address, end, "amd_iommu")) {
496 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
498 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
502 return (u8 __iomem *)ioremap(address, end);
505 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
507 if (iommu->mmio_base)
508 iounmap(iommu->mmio_base);
509 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
512 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
528 /****************************************************************************
530 * The functions below belong to the first pass of AMD IOMMU ACPI table
531 * parsing. In this pass we try to find out the highest device id this
532 * code has to handle. Upon this information the size of the shared data
533 * structures is determined later.
535 ****************************************************************************/
538 * This function calculates the length of a given IVHD entry
540 static inline int ivhd_entry_length(u8 *ivhd)
542 u32 type = ((struct ivhd_entry *)ivhd)->type;
545 return 0x04 << (*ivhd >> 6);
546 } else if (type == IVHD_DEV_ACPI_HID) {
547 /* For ACPI_HID, offset 21 is uid len */
548 return *((u8 *)ivhd + 21) + 22;
554 * After reading the highest device id from the IOMMU PCI capability header
555 * this function looks if there is a higher device id defined in the ACPI table
557 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
559 u8 *p = (void *)h, *end = (void *)h;
560 struct ivhd_entry *dev;
561 int last_devid = -EINVAL;
563 u32 ivhd_size = get_ivhd_header_size(h);
566 pr_err("Unsupported IVHD type %#x\n", h->type);
574 dev = (struct ivhd_entry *)p;
577 /* Use maximum BDF value for DEV_ALL */
579 case IVHD_DEV_SELECT:
580 case IVHD_DEV_RANGE_END:
582 case IVHD_DEV_EXT_SELECT:
583 /* all the above subfield types refer to device ids */
584 if (dev->devid > last_devid)
585 last_devid = dev->devid;
590 p += ivhd_entry_length(p);
598 static int __init check_ivrs_checksum(struct acpi_table_header *table)
601 u8 checksum = 0, *p = (u8 *)table;
603 for (i = 0; i < table->length; ++i)
606 /* ACPI table corrupt */
607 pr_err(FW_BUG "IVRS invalid checksum\n");
615 * Iterate over all IVHD entries in the ACPI table and find the highest device
616 * id which we need to handle. This is the first of three functions which parse
617 * the ACPI table. So we check the checksum here.
619 static int __init find_last_devid_acpi(struct acpi_table_header *table, u16 pci_seg)
621 u8 *p = (u8 *)table, *end = (u8 *)table;
622 struct ivhd_header *h;
623 int last_devid, last_bdf = 0;
625 p += IVRS_HEADER_LENGTH;
627 end += table->length;
629 h = (struct ivhd_header *)p;
630 if (h->pci_seg == pci_seg &&
631 h->type == amd_iommu_target_ivhd_type) {
632 last_devid = find_last_devid_from_ivhd(h);
636 if (last_devid > last_bdf)
637 last_bdf = last_devid;
646 /****************************************************************************
648 * The following functions belong to the code path which parses the ACPI table
649 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
650 * data structures, initialize the per PCI segment device/alias/rlookup table
651 * and also basically initialize the hardware.
653 ****************************************************************************/
655 /* Allocate per PCI segment device table */
656 static inline int __init alloc_dev_table(struct amd_iommu_pci_seg *pci_seg)
658 pci_seg->dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
659 get_order(pci_seg->dev_table_size));
660 if (!pci_seg->dev_table)
666 static inline void free_dev_table(struct amd_iommu_pci_seg *pci_seg)
668 free_pages((unsigned long)pci_seg->dev_table,
669 get_order(pci_seg->dev_table_size));
670 pci_seg->dev_table = NULL;
673 /* Allocate per PCI segment IOMMU rlookup table. */
674 static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
676 pci_seg->rlookup_table = (void *)__get_free_pages(
677 GFP_KERNEL | __GFP_ZERO,
678 get_order(pci_seg->rlookup_table_size));
679 if (pci_seg->rlookup_table == NULL)
685 static inline void free_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
687 free_pages((unsigned long)pci_seg->rlookup_table,
688 get_order(pci_seg->rlookup_table_size));
689 pci_seg->rlookup_table = NULL;
692 static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
694 pci_seg->irq_lookup_table = (void *)__get_free_pages(
695 GFP_KERNEL | __GFP_ZERO,
696 get_order(pci_seg->rlookup_table_size));
697 kmemleak_alloc(pci_seg->irq_lookup_table,
698 pci_seg->rlookup_table_size, 1, GFP_KERNEL);
699 if (pci_seg->irq_lookup_table == NULL)
705 static inline void free_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
707 kmemleak_free(pci_seg->irq_lookup_table);
708 free_pages((unsigned long)pci_seg->irq_lookup_table,
709 get_order(pci_seg->rlookup_table_size));
710 pci_seg->irq_lookup_table = NULL;
713 static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)
717 pci_seg->alias_table = (void *)__get_free_pages(GFP_KERNEL,
718 get_order(pci_seg->alias_table_size));
719 if (!pci_seg->alias_table)
723 * let all alias entries point to itself
725 for (i = 0; i <= pci_seg->last_bdf; ++i)
726 pci_seg->alias_table[i] = i;
731 static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)
733 free_pages((unsigned long)pci_seg->alias_table,
734 get_order(pci_seg->alias_table_size));
735 pci_seg->alias_table = NULL;
739 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
740 * write commands to that buffer later and the IOMMU will execute them
743 static int __init alloc_command_buffer(struct amd_iommu *iommu)
745 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
746 get_order(CMD_BUFFER_SIZE));
748 return iommu->cmd_buf ? 0 : -ENOMEM;
752 * This function restarts event logging in case the IOMMU experienced
753 * an event log buffer overflow.
755 void amd_iommu_restart_event_logging(struct amd_iommu *iommu)
757 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
758 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
762 * This function resets the command buffer if the IOMMU stopped fetching
765 static void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
767 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
769 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
770 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
771 iommu->cmd_buf_head = 0;
772 iommu->cmd_buf_tail = 0;
774 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
778 * This function writes the command buffer address to the hardware and
781 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
785 BUG_ON(iommu->cmd_buf == NULL);
787 entry = iommu_virt_to_phys(iommu->cmd_buf);
788 entry |= MMIO_CMD_SIZE_512;
790 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
791 &entry, sizeof(entry));
793 amd_iommu_reset_cmd_buffer(iommu);
797 * This function disables the command buffer
799 static void iommu_disable_command_buffer(struct amd_iommu *iommu)
801 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
804 static void __init free_command_buffer(struct amd_iommu *iommu)
806 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
809 static void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
810 gfp_t gfp, size_t size)
812 int order = get_order(size);
813 void *buf = (void *)__get_free_pages(gfp, order);
816 check_feature_on_all_iommus(FEATURE_SNP) &&
817 set_memory_4k((unsigned long)buf, (1 << order))) {
818 free_pages((unsigned long)buf, order);
825 /* allocates the memory where the IOMMU will log its events to */
826 static int __init alloc_event_buffer(struct amd_iommu *iommu)
828 iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
831 return iommu->evt_buf ? 0 : -ENOMEM;
834 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
838 BUG_ON(iommu->evt_buf == NULL);
840 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
842 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
843 &entry, sizeof(entry));
845 /* set head and tail to zero manually */
846 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
847 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
849 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
853 * This function disables the event log buffer
855 static void iommu_disable_event_buffer(struct amd_iommu *iommu)
857 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
860 static void __init free_event_buffer(struct amd_iommu *iommu)
862 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
865 /* allocates the memory where the IOMMU will log its events to */
866 static int __init alloc_ppr_log(struct amd_iommu *iommu)
868 iommu->ppr_log = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
871 return iommu->ppr_log ? 0 : -ENOMEM;
874 static void iommu_enable_ppr_log(struct amd_iommu *iommu)
878 if (iommu->ppr_log == NULL)
881 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
883 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
884 &entry, sizeof(entry));
886 /* set head and tail to zero manually */
887 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
888 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
890 iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
891 iommu_feature_enable(iommu, CONTROL_PPR_EN);
894 static void __init free_ppr_log(struct amd_iommu *iommu)
896 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
899 static void free_ga_log(struct amd_iommu *iommu)
901 #ifdef CONFIG_IRQ_REMAP
902 free_pages((unsigned long)iommu->ga_log, get_order(GA_LOG_SIZE));
903 free_pages((unsigned long)iommu->ga_log_tail, get_order(8));
907 #ifdef CONFIG_IRQ_REMAP
908 static int iommu_ga_log_enable(struct amd_iommu *iommu)
916 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
917 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
918 &entry, sizeof(entry));
919 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
920 (BIT_ULL(52)-1)) & ~7ULL;
921 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
922 &entry, sizeof(entry));
923 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
924 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
927 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
928 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
930 for (i = 0; i < LOOP_TIMEOUT; ++i) {
931 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
932 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
937 if (WARN_ON(i >= LOOP_TIMEOUT))
943 static int iommu_init_ga_log(struct amd_iommu *iommu)
945 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
948 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
949 get_order(GA_LOG_SIZE));
953 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
955 if (!iommu->ga_log_tail)
963 #endif /* CONFIG_IRQ_REMAP */
965 static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
967 iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO, 1);
969 return iommu->cmd_sem ? 0 : -ENOMEM;
972 static void __init free_cwwb_sem(struct amd_iommu *iommu)
975 free_page((unsigned long)iommu->cmd_sem);
978 static void iommu_enable_xt(struct amd_iommu *iommu)
980 #ifdef CONFIG_IRQ_REMAP
982 * XT mode (32-bit APIC destination ID) requires
983 * GA mode (128-bit IRTE support) as a prerequisite.
985 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
986 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
987 iommu_feature_enable(iommu, CONTROL_XT_EN);
988 #endif /* CONFIG_IRQ_REMAP */
991 static void iommu_enable_gt(struct amd_iommu *iommu)
993 if (!iommu_feature(iommu, FEATURE_GT))
996 iommu_feature_enable(iommu, CONTROL_GT_EN);
999 /* sets a specific bit in the device table entry. */
1000 static void __set_dev_entry_bit(struct dev_table_entry *dev_table,
1003 int i = (bit >> 6) & 0x03;
1004 int _bit = bit & 0x3f;
1006 dev_table[devid].data[i] |= (1UL << _bit);
1009 static void set_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
1011 struct dev_table_entry *dev_table = get_dev_table(iommu);
1013 return __set_dev_entry_bit(dev_table, devid, bit);
1016 static int __get_dev_entry_bit(struct dev_table_entry *dev_table,
1019 int i = (bit >> 6) & 0x03;
1020 int _bit = bit & 0x3f;
1022 return (dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
1025 static int get_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
1027 struct dev_table_entry *dev_table = get_dev_table(iommu);
1029 return __get_dev_entry_bit(dev_table, devid, bit);
1032 static bool __copy_device_table(struct amd_iommu *iommu)
1034 u64 int_ctl, int_tab_len, entry = 0;
1035 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
1036 struct dev_table_entry *old_devtb = NULL;
1037 u32 lo, hi, devid, old_devtb_size;
1038 phys_addr_t old_devtb_phys;
1039 u16 dom_id, dte_v, irq_v;
1043 /* Each IOMMU use separate device table with the same size */
1044 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
1045 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
1046 entry = (((u64) hi) << 32) + lo;
1048 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
1049 if (old_devtb_size != pci_seg->dev_table_size) {
1050 pr_err("The device table size of IOMMU:%d is not expected!\n",
1056 * When SME is enabled in the first kernel, the entry includes the
1057 * memory encryption mask(sme_me_mask), we must remove the memory
1058 * encryption mask to obtain the true physical address in kdump kernel.
1060 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
1062 if (old_devtb_phys >= 0x100000000ULL) {
1063 pr_err("The address of old device table is above 4G, not trustworthy!\n");
1066 old_devtb = (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && is_kdump_kernel())
1067 ? (__force void *)ioremap_encrypted(old_devtb_phys,
1068 pci_seg->dev_table_size)
1069 : memremap(old_devtb_phys, pci_seg->dev_table_size, MEMREMAP_WB);
1074 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
1075 pci_seg->old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
1076 get_order(pci_seg->dev_table_size));
1077 if (pci_seg->old_dev_tbl_cpy == NULL) {
1078 pr_err("Failed to allocate memory for copying old device table!\n");
1079 memunmap(old_devtb);
1083 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
1084 pci_seg->old_dev_tbl_cpy[devid] = old_devtb[devid];
1085 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
1086 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
1088 if (dte_v && dom_id) {
1089 pci_seg->old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
1090 pci_seg->old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
1091 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
1092 /* If gcr3 table existed, mask it out */
1093 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
1094 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1095 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1096 pci_seg->old_dev_tbl_cpy[devid].data[1] &= ~tmp;
1097 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
1099 pci_seg->old_dev_tbl_cpy[devid].data[0] &= ~tmp;
1103 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
1104 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
1105 int_tab_len = old_devtb[devid].data[2] & DTE_INTTABLEN_MASK;
1106 if (irq_v && (int_ctl || int_tab_len)) {
1107 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
1108 (int_tab_len != DTE_INTTABLEN)) {
1109 pr_err("Wrong old irq remapping flag: %#x\n", devid);
1110 memunmap(old_devtb);
1114 pci_seg->old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
1117 memunmap(old_devtb);
1122 static bool copy_device_table(void)
1124 struct amd_iommu *iommu;
1125 struct amd_iommu_pci_seg *pci_seg;
1127 if (!amd_iommu_pre_enabled)
1130 pr_warn("Translation is already enabled - trying to copy translation structures\n");
1133 * All IOMMUs within PCI segment shares common device table.
1134 * Hence copy device table only once per PCI segment.
1136 for_each_pci_segment(pci_seg) {
1137 for_each_iommu(iommu) {
1138 if (pci_seg->id != iommu->pci_seg->id)
1140 if (!__copy_device_table(iommu))
1149 void amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid)
1153 sysmgt = get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1) |
1154 (get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2) << 1);
1157 set_dev_entry_bit(iommu, devid, DEV_ENTRY_IW);
1161 * This function takes the device specific flags read from the ACPI
1162 * table and sets up the device table entry with that information
1164 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
1165 u16 devid, u32 flags, u32 ext_flags)
1167 if (flags & ACPI_DEVFLAG_INITPASS)
1168 set_dev_entry_bit(iommu, devid, DEV_ENTRY_INIT_PASS);
1169 if (flags & ACPI_DEVFLAG_EXTINT)
1170 set_dev_entry_bit(iommu, devid, DEV_ENTRY_EINT_PASS);
1171 if (flags & ACPI_DEVFLAG_NMI)
1172 set_dev_entry_bit(iommu, devid, DEV_ENTRY_NMI_PASS);
1173 if (flags & ACPI_DEVFLAG_SYSMGT1)
1174 set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1);
1175 if (flags & ACPI_DEVFLAG_SYSMGT2)
1176 set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2);
1177 if (flags & ACPI_DEVFLAG_LINT0)
1178 set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT0_PASS);
1179 if (flags & ACPI_DEVFLAG_LINT1)
1180 set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT1_PASS);
1182 amd_iommu_apply_erratum_63(iommu, devid);
1184 amd_iommu_set_rlookup_table(iommu, devid);
1187 int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line)
1189 struct devid_map *entry;
1190 struct list_head *list;
1192 if (type == IVHD_SPECIAL_IOAPIC)
1194 else if (type == IVHD_SPECIAL_HPET)
1199 list_for_each_entry(entry, list, list) {
1200 if (!(entry->id == id && entry->cmd_line))
1203 pr_info("Command-line override present for %s id %d - ignoring\n",
1204 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1206 *devid = entry->devid;
1211 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1216 entry->devid = *devid;
1217 entry->cmd_line = cmd_line;
1219 list_add_tail(&entry->list, list);
1224 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u32 *devid,
1227 struct acpihid_map_entry *entry;
1228 struct list_head *list = &acpihid_map;
1230 list_for_each_entry(entry, list, list) {
1231 if (strcmp(entry->hid, hid) ||
1232 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1236 pr_info("Command-line override for hid:%s uid:%s\n",
1238 *devid = entry->devid;
1242 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1246 memcpy(entry->uid, uid, strlen(uid));
1247 memcpy(entry->hid, hid, strlen(hid));
1248 entry->devid = *devid;
1249 entry->cmd_line = cmd_line;
1250 entry->root_devid = (entry->devid & (~0x7));
1252 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1253 entry->cmd_line ? "cmd" : "ivrs",
1254 entry->hid, entry->uid, entry->root_devid);
1256 list_add_tail(&entry->list, list);
1260 static int __init add_early_maps(void)
1264 for (i = 0; i < early_ioapic_map_size; ++i) {
1265 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1266 early_ioapic_map[i].id,
1267 &early_ioapic_map[i].devid,
1268 early_ioapic_map[i].cmd_line);
1273 for (i = 0; i < early_hpet_map_size; ++i) {
1274 ret = add_special_device(IVHD_SPECIAL_HPET,
1275 early_hpet_map[i].id,
1276 &early_hpet_map[i].devid,
1277 early_hpet_map[i].cmd_line);
1282 for (i = 0; i < early_acpihid_map_size; ++i) {
1283 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1284 early_acpihid_map[i].uid,
1285 &early_acpihid_map[i].devid,
1286 early_acpihid_map[i].cmd_line);
1295 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1296 * initializes the hardware and our data structures with it.
1298 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1299 struct ivhd_header *h)
1302 u8 *end = p, flags = 0;
1303 u16 devid = 0, devid_start = 0, devid_to = 0, seg_id;
1304 u32 dev_i, ext_flags = 0;
1306 struct ivhd_entry *e;
1307 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
1312 ret = add_early_maps();
1316 amd_iommu_apply_ivrs_quirks();
1319 * First save the recommended feature enable bits from ACPI
1321 iommu->acpi_flags = h->flags;
1324 * Done. Now parse the device entries
1326 ivhd_size = get_ivhd_header_size(h);
1328 pr_err("Unsupported IVHD type %#x\n", h->type);
1338 e = (struct ivhd_entry *)p;
1339 seg_id = pci_seg->id;
1344 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1346 for (dev_i = 0; dev_i <= pci_seg->last_bdf; ++dev_i)
1347 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1349 case IVHD_DEV_SELECT:
1351 DUMP_printk(" DEV_SELECT\t\t\t devid: %04x:%02x:%02x.%x "
1353 seg_id, PCI_BUS_NUM(e->devid),
1359 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1361 case IVHD_DEV_SELECT_RANGE_START:
1363 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1364 "devid: %04x:%02x:%02x.%x flags: %02x\n",
1365 seg_id, PCI_BUS_NUM(e->devid),
1370 devid_start = e->devid;
1375 case IVHD_DEV_ALIAS:
1377 DUMP_printk(" DEV_ALIAS\t\t\t devid: %04x:%02x:%02x.%x "
1378 "flags: %02x devid_to: %02x:%02x.%x\n",
1379 seg_id, PCI_BUS_NUM(e->devid),
1383 PCI_BUS_NUM(e->ext >> 8),
1384 PCI_SLOT(e->ext >> 8),
1385 PCI_FUNC(e->ext >> 8));
1388 devid_to = e->ext >> 8;
1389 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1390 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1391 pci_seg->alias_table[devid] = devid_to;
1393 case IVHD_DEV_ALIAS_RANGE:
1395 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1396 "devid: %04x:%02x:%02x.%x flags: %02x "
1397 "devid_to: %04x:%02x:%02x.%x\n",
1398 seg_id, PCI_BUS_NUM(e->devid),
1402 seg_id, PCI_BUS_NUM(e->ext >> 8),
1403 PCI_SLOT(e->ext >> 8),
1404 PCI_FUNC(e->ext >> 8));
1406 devid_start = e->devid;
1408 devid_to = e->ext >> 8;
1412 case IVHD_DEV_EXT_SELECT:
1414 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %04x:%02x:%02x.%x "
1415 "flags: %02x ext: %08x\n",
1416 seg_id, PCI_BUS_NUM(e->devid),
1422 set_dev_entry_from_acpi(iommu, devid, e->flags,
1425 case IVHD_DEV_EXT_SELECT_RANGE:
1427 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1428 "%04x:%02x:%02x.%x flags: %02x ext: %08x\n",
1429 seg_id, PCI_BUS_NUM(e->devid),
1434 devid_start = e->devid;
1439 case IVHD_DEV_RANGE_END:
1441 DUMP_printk(" DEV_RANGE_END\t\t devid: %04x:%02x:%02x.%x\n",
1442 seg_id, PCI_BUS_NUM(e->devid),
1444 PCI_FUNC(e->devid));
1447 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1449 pci_seg->alias_table[dev_i] = devid_to;
1450 set_dev_entry_from_acpi(iommu,
1451 devid_to, flags, ext_flags);
1453 set_dev_entry_from_acpi(iommu, dev_i,
1457 case IVHD_DEV_SPECIAL: {
1463 handle = e->ext & 0xff;
1464 devid = PCI_SEG_DEVID_TO_SBDF(seg_id, (e->ext >> 8));
1465 type = (e->ext >> 24) & 0xff;
1467 if (type == IVHD_SPECIAL_IOAPIC)
1469 else if (type == IVHD_SPECIAL_HPET)
1474 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x\n",
1476 seg_id, PCI_BUS_NUM(devid),
1480 ret = add_special_device(type, handle, &devid, false);
1485 * add_special_device might update the devid in case a
1486 * command-line override is present. So call
1487 * set_dev_entry_from_acpi after add_special_device.
1489 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1493 case IVHD_DEV_ACPI_HID: {
1495 u8 hid[ACPIHID_HID_LEN];
1496 u8 uid[ACPIHID_UID_LEN];
1499 if (h->type != 0x40) {
1500 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1505 BUILD_BUG_ON(sizeof(e->ext_hid) != ACPIHID_HID_LEN - 1);
1506 memcpy(hid, &e->ext_hid, ACPIHID_HID_LEN - 1);
1507 hid[ACPIHID_HID_LEN - 1] = '\0';
1510 pr_err(FW_BUG "Invalid HID.\n");
1516 case UID_NOT_PRESENT:
1519 pr_warn(FW_BUG "Invalid UID length.\n");
1522 case UID_IS_INTEGER:
1524 sprintf(uid, "%d", e->uid);
1527 case UID_IS_CHARACTER:
1529 memcpy(uid, &e->uid, e->uidl);
1530 uid[e->uidl] = '\0';
1537 devid = PCI_SEG_DEVID_TO_SBDF(seg_id, e->devid);
1538 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x\n",
1546 ret = add_acpi_hid_device(hid, uid, &devid, false);
1551 * add_special_device might update the devid in case a
1552 * command-line override is present. So call
1553 * set_dev_entry_from_acpi after add_special_device.
1555 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1563 p += ivhd_entry_length(p);
1569 /* Allocate PCI segment data structure */
1570 static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
1571 struct acpi_table_header *ivrs_base)
1573 struct amd_iommu_pci_seg *pci_seg;
1577 * First parse ACPI tables to find the largest Bus/Dev/Func we need to
1578 * handle in this PCI segment. Upon this information the shared data
1579 * structures for the PCI segments in the system will be allocated.
1581 last_bdf = find_last_devid_acpi(ivrs_base, id);
1585 pci_seg = kzalloc(sizeof(struct amd_iommu_pci_seg), GFP_KERNEL);
1586 if (pci_seg == NULL)
1589 pci_seg->last_bdf = last_bdf;
1590 DUMP_printk("PCI segment : 0x%0x, last bdf : 0x%04x\n", id, last_bdf);
1591 pci_seg->dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE, last_bdf);
1592 pci_seg->alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE, last_bdf);
1593 pci_seg->rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE, last_bdf);
1596 init_llist_head(&pci_seg->dev_data_list);
1597 INIT_LIST_HEAD(&pci_seg->unity_map);
1598 list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
1600 if (alloc_dev_table(pci_seg))
1602 if (alloc_alias_table(pci_seg))
1604 if (alloc_rlookup_table(pci_seg))
1610 static struct amd_iommu_pci_seg *__init get_pci_segment(u16 id,
1611 struct acpi_table_header *ivrs_base)
1613 struct amd_iommu_pci_seg *pci_seg;
1615 for_each_pci_segment(pci_seg) {
1616 if (pci_seg->id == id)
1620 return alloc_pci_segment(id, ivrs_base);
1623 static void __init free_pci_segments(void)
1625 struct amd_iommu_pci_seg *pci_seg, *next;
1627 for_each_pci_segment_safe(pci_seg, next) {
1628 list_del(&pci_seg->list);
1629 free_irq_lookup_table(pci_seg);
1630 free_rlookup_table(pci_seg);
1631 free_alias_table(pci_seg);
1632 free_dev_table(pci_seg);
1637 static void __init free_iommu_one(struct amd_iommu *iommu)
1639 free_cwwb_sem(iommu);
1640 free_command_buffer(iommu);
1641 free_event_buffer(iommu);
1642 free_ppr_log(iommu);
1644 iommu_unmap_mmio_space(iommu);
1647 static void __init free_iommu_all(void)
1649 struct amd_iommu *iommu, *next;
1651 for_each_iommu_safe(iommu, next) {
1652 list_del(&iommu->list);
1653 free_iommu_one(iommu);
1659 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1661 * BIOS should disable L2B micellaneous clock gating by setting
1662 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1664 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1668 if ((boot_cpu_data.x86 != 0x15) ||
1669 (boot_cpu_data.x86_model < 0x10) ||
1670 (boot_cpu_data.x86_model > 0x1f))
1673 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1674 pci_read_config_dword(iommu->dev, 0xf4, &value);
1679 /* Select NB indirect register 0x90 and enable writing */
1680 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1682 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1683 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
1685 /* Clear the enable writing bit */
1686 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1690 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1692 * BIOS should enable ATS write permission check by setting
1693 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1695 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1699 if ((boot_cpu_data.x86 != 0x15) ||
1700 (boot_cpu_data.x86_model < 0x30) ||
1701 (boot_cpu_data.x86_model > 0x3f))
1704 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1705 value = iommu_read_l2(iommu, 0x47);
1710 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1711 iommu_write_l2(iommu, 0x47, value | BIT(0));
1713 pci_info(iommu->dev, "Applying ATS write check workaround\n");
1717 * This function glues the initialization function for one IOMMU
1718 * together and also allocates the command buffer and programs the
1719 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1721 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
1722 struct acpi_table_header *ivrs_base)
1724 struct amd_iommu_pci_seg *pci_seg;
1726 pci_seg = get_pci_segment(h->pci_seg, ivrs_base);
1727 if (pci_seg == NULL)
1729 iommu->pci_seg = pci_seg;
1731 raw_spin_lock_init(&iommu->lock);
1732 iommu->cmd_sem_val = 0;
1734 /* Add IOMMU to internal data structures */
1735 list_add_tail(&iommu->list, &amd_iommu_list);
1736 iommu->index = amd_iommus_present++;
1738 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1739 WARN(1, "System has more IOMMUs than supported by this driver\n");
1743 /* Index is fine - add IOMMU to the array */
1744 amd_iommus[iommu->index] = iommu;
1747 * Copy data from ACPI table entry to the iommu struct
1749 iommu->devid = h->devid;
1750 iommu->cap_ptr = h->cap_ptr;
1751 iommu->mmio_phys = h->mmio_phys;
1755 /* Check if IVHD EFR contains proper max banks/counters */
1756 if ((h->efr_attr != 0) &&
1757 ((h->efr_attr & (0xF << 13)) != 0) &&
1758 ((h->efr_attr & (0x3F << 17)) != 0))
1759 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1761 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1764 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1765 * GAM also requires GA mode. Therefore, we need to
1766 * check cmpxchg16b support before enabling it.
1768 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1769 ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1770 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1774 if (h->efr_reg & (1 << 9))
1775 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1777 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1780 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1781 * XT, GAM also requires GA mode. Therefore, we need to
1782 * check cmpxchg16b support before enabling them.
1784 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1785 ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
1786 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1790 if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
1791 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1793 early_iommu_features_init(iommu, h);
1800 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1801 iommu->mmio_phys_end);
1802 if (!iommu->mmio_base)
1805 return init_iommu_from_acpi(iommu, h);
1808 static int __init init_iommu_one_late(struct amd_iommu *iommu)
1812 if (alloc_cwwb_sem(iommu))
1815 if (alloc_command_buffer(iommu))
1818 if (alloc_event_buffer(iommu))
1821 iommu->int_enabled = false;
1823 init_translation_status(iommu);
1824 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1825 iommu_disable(iommu);
1826 clear_translation_pre_enabled(iommu);
1827 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1830 if (amd_iommu_pre_enabled)
1831 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1833 if (amd_iommu_irq_remap) {
1834 ret = amd_iommu_create_irq_domain(iommu);
1840 * Make sure IOMMU is not considered to translate itself. The IVRS
1841 * table tells us so, but this is a lie!
1843 iommu->pci_seg->rlookup_table[iommu->devid] = NULL;
1849 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1850 * @ivrs: Pointer to the IVRS header
1852 * This function search through all IVDB of the maximum supported IVHD
1854 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1856 u8 *base = (u8 *)ivrs;
1857 struct ivhd_header *ivhd = (struct ivhd_header *)
1858 (base + IVRS_HEADER_LENGTH);
1859 u8 last_type = ivhd->type;
1860 u16 devid = ivhd->devid;
1862 while (((u8 *)ivhd - base < ivrs->length) &&
1863 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1864 u8 *p = (u8 *) ivhd;
1866 if (ivhd->devid == devid)
1867 last_type = ivhd->type;
1868 ivhd = (struct ivhd_header *)(p + ivhd->length);
1875 * Iterates over all IOMMU entries in the ACPI table, allocates the
1876 * IOMMU structure and initializes it with init_iommu_one()
1878 static int __init init_iommu_all(struct acpi_table_header *table)
1880 u8 *p = (u8 *)table, *end = (u8 *)table;
1881 struct ivhd_header *h;
1882 struct amd_iommu *iommu;
1885 end += table->length;
1886 p += IVRS_HEADER_LENGTH;
1888 /* Phase 1: Process all IVHD blocks */
1890 h = (struct ivhd_header *)p;
1891 if (*p == amd_iommu_target_ivhd_type) {
1893 DUMP_printk("device: %04x:%02x:%02x.%01x cap: %04x "
1894 "flags: %01x info %04x\n",
1895 h->pci_seg, PCI_BUS_NUM(h->devid),
1896 PCI_SLOT(h->devid), PCI_FUNC(h->devid),
1897 h->cap_ptr, h->flags, h->info);
1898 DUMP_printk(" mmio-addr: %016llx\n",
1901 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1905 ret = init_iommu_one(iommu, h, table);
1914 /* Phase 2 : Early feature support check */
1917 /* Phase 3 : Enabling IOMMU features */
1918 for_each_iommu(iommu) {
1919 ret = init_iommu_one_late(iommu);
1927 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1930 struct pci_dev *pdev = iommu->dev;
1932 if (!iommu_feature(iommu, FEATURE_PC))
1935 amd_iommu_pc_present = true;
1937 pci_info(pdev, "IOMMU performance counters supported\n");
1939 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1940 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1941 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1946 static ssize_t amd_iommu_show_cap(struct device *dev,
1947 struct device_attribute *attr,
1950 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1951 return sysfs_emit(buf, "%x\n", iommu->cap);
1953 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1955 static ssize_t amd_iommu_show_features(struct device *dev,
1956 struct device_attribute *attr,
1959 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1960 return sysfs_emit(buf, "%llx:%llx\n", iommu->features2, iommu->features);
1962 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1964 static struct attribute *amd_iommu_attrs[] = {
1966 &dev_attr_features.attr,
1970 static struct attribute_group amd_iommu_group = {
1971 .name = "amd-iommu",
1972 .attrs = amd_iommu_attrs,
1975 static const struct attribute_group *amd_iommu_groups[] = {
1981 * Note: IVHD 0x11 and 0x40 also contains exact copy
1982 * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
1983 * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
1985 static void __init late_iommu_features_init(struct amd_iommu *iommu)
1987 u64 features, features2;
1989 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
1992 /* read extended feature bits */
1993 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1994 features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);
1996 if (!iommu->features) {
1997 iommu->features = features;
1998 iommu->features2 = features2;
2003 * Sanity check and warn if EFR values from
2004 * IVHD and MMIO conflict.
2006 if (features != iommu->features ||
2007 features2 != iommu->features2) {
2009 "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",
2010 features, iommu->features,
2011 features2, iommu->features2);
2015 static int __init iommu_init_pci(struct amd_iommu *iommu)
2017 int cap_ptr = iommu->cap_ptr;
2020 iommu->dev = pci_get_domain_bus_and_slot(iommu->pci_seg->id,
2021 PCI_BUS_NUM(iommu->devid),
2022 iommu->devid & 0xff);
2026 /* Prevent binding other PCI device drivers to IOMMU devices */
2027 iommu->dev->match_driver = false;
2029 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
2032 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
2033 amd_iommu_iotlb_sup = false;
2035 late_iommu_features_init(iommu);
2037 if (iommu_feature(iommu, FEATURE_GT)) {
2042 pasmax = iommu->features & FEATURE_PASID_MASK;
2043 pasmax >>= FEATURE_PASID_SHIFT;
2044 max_pasid = (1 << (pasmax + 1)) - 1;
2046 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
2048 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
2050 glxval = iommu->features & FEATURE_GLXVAL_MASK;
2051 glxval >>= FEATURE_GLXVAL_SHIFT;
2053 if (amd_iommu_max_glx_val == -1)
2054 amd_iommu_max_glx_val = glxval;
2056 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
2059 if (iommu_feature(iommu, FEATURE_GT) &&
2060 iommu_feature(iommu, FEATURE_PPR)) {
2061 iommu->is_iommu_v2 = true;
2062 amd_iommu_v2_present = true;
2065 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
2068 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
2069 pr_info("Using strict mode due to virtualization\n");
2070 iommu_set_dma_strict();
2071 amd_iommu_np_cache = true;
2074 init_iommu_perf_ctr(iommu);
2076 if (amd_iommu_pgtable == AMD_IOMMU_V2) {
2077 if (!iommu_feature(iommu, FEATURE_GIOSUP) ||
2078 !iommu_feature(iommu, FEATURE_GT)) {
2079 pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");
2080 amd_iommu_pgtable = AMD_IOMMU_V1;
2081 } else if (iommu_default_passthrough()) {
2082 pr_warn("V2 page table doesn't support passthrough mode. Fallback to v1.\n");
2083 amd_iommu_pgtable = AMD_IOMMU_V1;
2087 if (is_rd890_iommu(iommu->dev)) {
2091 pci_get_domain_bus_and_slot(iommu->pci_seg->id,
2092 iommu->dev->bus->number,
2096 * Some rd890 systems may not be fully reconfigured by the
2097 * BIOS, so it's necessary for us to store this information so
2098 * it can be reprogrammed on resume
2100 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
2101 &iommu->stored_addr_lo);
2102 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
2103 &iommu->stored_addr_hi);
2105 /* Low bit locks writes to configuration space */
2106 iommu->stored_addr_lo &= ~1;
2108 for (i = 0; i < 6; i++)
2109 for (j = 0; j < 0x12; j++)
2110 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
2112 for (i = 0; i < 0x83; i++)
2113 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
2116 amd_iommu_erratum_746_workaround(iommu);
2117 amd_iommu_ats_write_check_workaround(iommu);
2119 ret = iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
2120 amd_iommu_groups, "ivhd%d", iommu->index);
2124 iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
2126 return pci_enable_device(iommu->dev);
2129 static void print_iommu_info(void)
2131 static const char * const feat_str[] = {
2132 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
2133 "IA", "GA", "HE", "PC"
2135 struct amd_iommu *iommu;
2137 for_each_iommu(iommu) {
2138 struct pci_dev *pdev = iommu->dev;
2141 pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);
2143 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
2144 pr_info("Extended features (%#llx, %#llx):", iommu->features, iommu->features2);
2146 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
2147 if (iommu_feature(iommu, (1ULL << i)))
2148 pr_cont(" %s", feat_str[i]);
2151 if (iommu->features & FEATURE_GAM_VAPIC)
2152 pr_cont(" GA_vAPIC");
2154 if (iommu->features & FEATURE_SNP)
2160 if (irq_remapping_enabled) {
2161 pr_info("Interrupt remapping enabled\n");
2162 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2163 pr_info("X2APIC enabled\n");
2165 if (amd_iommu_pgtable == AMD_IOMMU_V2) {
2166 pr_info("V2 page table enabled (Paging mode : %d level)\n",
2167 amd_iommu_gpt_level);
2171 static int __init amd_iommu_init_pci(void)
2173 struct amd_iommu *iommu;
2174 struct amd_iommu_pci_seg *pci_seg;
2177 for_each_iommu(iommu) {
2178 ret = iommu_init_pci(iommu);
2180 pr_err("IOMMU%d: Failed to initialize IOMMU Hardware (error=%d)!\n",
2184 /* Need to setup range after PCI init */
2185 iommu_set_cwwb_range(iommu);
2189 * Order is important here to make sure any unity map requirements are
2190 * fulfilled. The unity mappings are created and written to the device
2191 * table during the iommu_init_pci() call.
2193 * After that we call init_device_table_dma() to make sure any
2194 * uninitialized DTE will block DMA, and in the end we flush the caches
2195 * of all IOMMUs to make sure the changes to the device table are
2198 for_each_pci_segment(pci_seg)
2199 init_device_table_dma(pci_seg);
2201 for_each_iommu(iommu)
2202 iommu_flush_all_caches(iommu);
2210 /****************************************************************************
2212 * The following functions initialize the MSI interrupts for all IOMMUs
2213 * in the system. It's a bit challenging because there could be multiple
2214 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
2217 ****************************************************************************/
2219 static int iommu_setup_msi(struct amd_iommu *iommu)
2223 r = pci_enable_msi(iommu->dev);
2227 r = request_threaded_irq(iommu->dev->irq,
2228 amd_iommu_int_handler,
2229 amd_iommu_int_thread,
2234 pci_disable_msi(iommu->dev);
2245 dest_mode_logical : 1,
2252 } __attribute__ ((packed));
2255 static struct irq_chip intcapxt_controller;
2257 static int intcapxt_irqdomain_activate(struct irq_domain *domain,
2258 struct irq_data *irqd, bool reserve)
2263 static void intcapxt_irqdomain_deactivate(struct irq_domain *domain,
2264 struct irq_data *irqd)
2269 static int intcapxt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2270 unsigned int nr_irqs, void *arg)
2272 struct irq_alloc_info *info = arg;
2275 if (!info || info->type != X86_IRQ_ALLOC_TYPE_AMDVI)
2278 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
2282 for (i = virq; i < virq + nr_irqs; i++) {
2283 struct irq_data *irqd = irq_domain_get_irq_data(domain, i);
2285 irqd->chip = &intcapxt_controller;
2286 irqd->chip_data = info->data;
2287 __irq_set_handler(i, handle_edge_irq, 0, "edge");
2293 static void intcapxt_irqdomain_free(struct irq_domain *domain, unsigned int virq,
2294 unsigned int nr_irqs)
2296 irq_domain_free_irqs_top(domain, virq, nr_irqs);
2300 static void intcapxt_unmask_irq(struct irq_data *irqd)
2302 struct amd_iommu *iommu = irqd->chip_data;
2303 struct irq_cfg *cfg = irqd_cfg(irqd);
2307 xt.dest_mode_logical = apic->dest_mode_logical;
2308 xt.vector = cfg->vector;
2309 xt.destid_0_23 = cfg->dest_apicid & GENMASK(23, 0);
2310 xt.destid_24_31 = cfg->dest_apicid >> 24;
2313 * Current IOMMU implementation uses the same IRQ for all
2314 * 3 IOMMU interrupts.
2316 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2317 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2318 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2321 static void intcapxt_mask_irq(struct irq_data *irqd)
2323 struct amd_iommu *iommu = irqd->chip_data;
2325 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2326 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2327 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2331 static int intcapxt_set_affinity(struct irq_data *irqd,
2332 const struct cpumask *mask, bool force)
2334 struct irq_data *parent = irqd->parent_data;
2337 ret = parent->chip->irq_set_affinity(parent, mask, force);
2338 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
2343 static int intcapxt_set_wake(struct irq_data *irqd, unsigned int on)
2345 return on ? -EOPNOTSUPP : 0;
2348 static struct irq_chip intcapxt_controller = {
2349 .name = "IOMMU-MSI",
2350 .irq_unmask = intcapxt_unmask_irq,
2351 .irq_mask = intcapxt_mask_irq,
2352 .irq_ack = irq_chip_ack_parent,
2353 .irq_retrigger = irq_chip_retrigger_hierarchy,
2354 .irq_set_affinity = intcapxt_set_affinity,
2355 .irq_set_wake = intcapxt_set_wake,
2356 .flags = IRQCHIP_MASK_ON_SUSPEND,
2359 static const struct irq_domain_ops intcapxt_domain_ops = {
2360 .alloc = intcapxt_irqdomain_alloc,
2361 .free = intcapxt_irqdomain_free,
2362 .activate = intcapxt_irqdomain_activate,
2363 .deactivate = intcapxt_irqdomain_deactivate,
2367 static struct irq_domain *iommu_irqdomain;
2369 static struct irq_domain *iommu_get_irqdomain(void)
2371 struct fwnode_handle *fn;
2373 /* No need for locking here (yet) as the init is single-threaded */
2374 if (iommu_irqdomain)
2375 return iommu_irqdomain;
2377 fn = irq_domain_alloc_named_fwnode("AMD-Vi-MSI");
2381 iommu_irqdomain = irq_domain_create_hierarchy(x86_vector_domain, 0, 0,
2382 fn, &intcapxt_domain_ops,
2384 if (!iommu_irqdomain)
2385 irq_domain_free_fwnode(fn);
2387 return iommu_irqdomain;
2390 static int iommu_setup_intcapxt(struct amd_iommu *iommu)
2392 struct irq_domain *domain;
2393 struct irq_alloc_info info;
2395 int node = dev_to_node(&iommu->dev->dev);
2397 domain = iommu_get_irqdomain();
2401 init_irq_alloc_info(&info, NULL);
2402 info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
2405 irq = irq_domain_alloc_irqs(domain, 1, node, &info);
2407 irq_domain_remove(domain);
2411 ret = request_threaded_irq(irq, amd_iommu_int_handler,
2412 amd_iommu_int_thread, 0, "AMD-Vi", iommu);
2414 irq_domain_free_irqs(irq, 1);
2415 irq_domain_remove(domain);
2422 static int iommu_init_irq(struct amd_iommu *iommu)
2426 if (iommu->int_enabled)
2429 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2430 ret = iommu_setup_intcapxt(iommu);
2431 else if (iommu->dev->msi_cap)
2432 ret = iommu_setup_msi(iommu);
2439 iommu->int_enabled = true;
2442 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2443 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2445 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2447 if (iommu->ppr_log != NULL)
2448 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
2452 /****************************************************************************
2454 * The next functions belong to the third pass of parsing the ACPI
2455 * table. In this last pass the memory mapping requirements are
2456 * gathered (like exclusion and unity mapping ranges).
2458 ****************************************************************************/
2460 static void __init free_unity_maps(void)
2462 struct unity_map_entry *entry, *next;
2463 struct amd_iommu_pci_seg *p, *pci_seg;
2465 for_each_pci_segment_safe(pci_seg, p) {
2466 list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
2467 list_del(&entry->list);
2473 /* called for unity map ACPI definition */
2474 static int __init init_unity_map_range(struct ivmd_header *m,
2475 struct acpi_table_header *ivrs_base)
2477 struct unity_map_entry *e = NULL;
2478 struct amd_iommu_pci_seg *pci_seg;
2481 pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
2482 if (pci_seg == NULL)
2485 e = kzalloc(sizeof(*e), GFP_KERNEL);
2493 case ACPI_IVMD_TYPE:
2494 s = "IVMD_TYPEi\t\t\t";
2495 e->devid_start = e->devid_end = m->devid;
2497 case ACPI_IVMD_TYPE_ALL:
2498 s = "IVMD_TYPE_ALL\t\t";
2500 e->devid_end = pci_seg->last_bdf;
2502 case ACPI_IVMD_TYPE_RANGE:
2503 s = "IVMD_TYPE_RANGE\t\t";
2504 e->devid_start = m->devid;
2505 e->devid_end = m->aux;
2508 e->address_start = PAGE_ALIGN(m->range_start);
2509 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2510 e->prot = m->flags >> 1;
2513 * Treat per-device exclusion ranges as r/w unity-mapped regions
2514 * since some buggy BIOSes might lead to the overwritten exclusion
2515 * range (exclusion_start and exclusion_length members). This
2516 * happens when there are multiple exclusion ranges (IVMD entries)
2517 * defined in ACPI table.
2519 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2520 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
2522 DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "
2523 "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"
2524 " flags: %x\n", s, m->pci_seg,
2525 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2526 PCI_FUNC(e->devid_start), m->pci_seg,
2527 PCI_BUS_NUM(e->devid_end),
2528 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2529 e->address_start, e->address_end, m->flags);
2531 list_add_tail(&e->list, &pci_seg->unity_map);
2536 /* iterates over all memory definitions we find in the ACPI table */
2537 static int __init init_memory_definitions(struct acpi_table_header *table)
2539 u8 *p = (u8 *)table, *end = (u8 *)table;
2540 struct ivmd_header *m;
2542 end += table->length;
2543 p += IVRS_HEADER_LENGTH;
2546 m = (struct ivmd_header *)p;
2547 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
2548 init_unity_map_range(m, table);
2557 * Init the device table to not allow DMA access for devices
2559 static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
2562 struct dev_table_entry *dev_table = pci_seg->dev_table;
2564 if (dev_table == NULL)
2567 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
2568 __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_VALID);
2569 if (!amd_iommu_snp_en)
2570 __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_TRANSLATION);
2574 static void __init uninit_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
2577 struct dev_table_entry *dev_table = pci_seg->dev_table;
2579 if (dev_table == NULL)
2582 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
2583 dev_table[devid].data[0] = 0ULL;
2584 dev_table[devid].data[1] = 0ULL;
2588 static void init_device_table(void)
2590 struct amd_iommu_pci_seg *pci_seg;
2593 if (!amd_iommu_irq_remap)
2596 for_each_pci_segment(pci_seg) {
2597 for (devid = 0; devid <= pci_seg->last_bdf; ++devid)
2598 __set_dev_entry_bit(pci_seg->dev_table,
2599 devid, DEV_ENTRY_IRQ_TBL_EN);
2603 static void iommu_init_flags(struct amd_iommu *iommu)
2605 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2606 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2607 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2609 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2610 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2611 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2613 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2614 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2615 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2617 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2618 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2619 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2622 * make IOMMU memory accesses cache coherent
2624 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2626 /* Set IOTLB invalidation timeout to 1s */
2627 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2630 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2633 u32 ioc_feature_control;
2634 struct pci_dev *pdev = iommu->root_pdev;
2636 /* RD890 BIOSes may not have completely reconfigured the iommu */
2637 if (!is_rd890_iommu(iommu->dev) || !pdev)
2641 * First, we need to ensure that the iommu is enabled. This is
2642 * controlled by a register in the northbridge
2645 /* Select Northbridge indirect register 0x75 and enable writing */
2646 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2647 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2649 /* Enable the iommu */
2650 if (!(ioc_feature_control & 0x1))
2651 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2653 /* Restore the iommu BAR */
2654 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2655 iommu->stored_addr_lo);
2656 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2657 iommu->stored_addr_hi);
2659 /* Restore the l1 indirect regs for each of the 6 l1s */
2660 for (i = 0; i < 6; i++)
2661 for (j = 0; j < 0x12; j++)
2662 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2664 /* Restore the l2 indirect regs */
2665 for (i = 0; i < 0x83; i++)
2666 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2668 /* Lock PCI setup registers */
2669 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2670 iommu->stored_addr_lo | 1);
2673 static void iommu_enable_ga(struct amd_iommu *iommu)
2675 #ifdef CONFIG_IRQ_REMAP
2676 switch (amd_iommu_guest_ir) {
2677 case AMD_IOMMU_GUEST_IR_VAPIC:
2678 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2679 iommu_feature_enable(iommu, CONTROL_GA_EN);
2680 iommu->irte_ops = &irte_128_ops;
2683 iommu->irte_ops = &irte_32_ops;
2689 static void early_enable_iommu(struct amd_iommu *iommu)
2691 iommu_disable(iommu);
2692 iommu_init_flags(iommu);
2693 iommu_set_device_table(iommu);
2694 iommu_enable_command_buffer(iommu);
2695 iommu_enable_event_buffer(iommu);
2696 iommu_set_exclusion_range(iommu);
2697 iommu_enable_ga(iommu);
2698 iommu_enable_xt(iommu);
2699 iommu_enable(iommu);
2700 iommu_flush_all_caches(iommu);
2704 * This function finally enables all IOMMUs found in the system after
2705 * they have been initialized.
2707 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2708 * the old content of device table entries. Not this case or copy failed,
2709 * just continue as normal kernel does.
2711 static void early_enable_iommus(void)
2713 struct amd_iommu *iommu;
2714 struct amd_iommu_pci_seg *pci_seg;
2716 if (!copy_device_table()) {
2718 * If come here because of failure in copying device table from old
2719 * kernel with all IOMMUs enabled, print error message and try to
2720 * free allocated old_dev_tbl_cpy.
2722 if (amd_iommu_pre_enabled)
2723 pr_err("Failed to copy DEV table from previous kernel.\n");
2725 for_each_pci_segment(pci_seg) {
2726 if (pci_seg->old_dev_tbl_cpy != NULL) {
2727 free_pages((unsigned long)pci_seg->old_dev_tbl_cpy,
2728 get_order(pci_seg->dev_table_size));
2729 pci_seg->old_dev_tbl_cpy = NULL;
2733 for_each_iommu(iommu) {
2734 clear_translation_pre_enabled(iommu);
2735 early_enable_iommu(iommu);
2738 pr_info("Copied DEV table from previous kernel.\n");
2740 for_each_pci_segment(pci_seg) {
2741 free_pages((unsigned long)pci_seg->dev_table,
2742 get_order(pci_seg->dev_table_size));
2743 pci_seg->dev_table = pci_seg->old_dev_tbl_cpy;
2746 for_each_iommu(iommu) {
2747 iommu_disable_command_buffer(iommu);
2748 iommu_disable_event_buffer(iommu);
2749 iommu_enable_command_buffer(iommu);
2750 iommu_enable_event_buffer(iommu);
2751 iommu_enable_ga(iommu);
2752 iommu_enable_xt(iommu);
2753 iommu_set_device_table(iommu);
2754 iommu_flush_all_caches(iommu);
2759 static void enable_iommus_v2(void)
2761 struct amd_iommu *iommu;
2763 for_each_iommu(iommu) {
2764 iommu_enable_ppr_log(iommu);
2765 iommu_enable_gt(iommu);
2769 static void enable_iommus_vapic(void)
2771 #ifdef CONFIG_IRQ_REMAP
2773 struct amd_iommu *iommu;
2775 for_each_iommu(iommu) {
2777 * Disable GALog if already running. It could have been enabled
2778 * in the previous boot before kdump.
2780 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
2781 if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
2784 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
2785 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
2788 * Need to set and poll check the GALOGRun bit to zero before
2789 * we can set/ modify GA Log registers safely.
2791 for (i = 0; i < LOOP_TIMEOUT; ++i) {
2792 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
2793 if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
2798 if (WARN_ON(i >= LOOP_TIMEOUT))
2802 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2803 !check_feature_on_all_iommus(FEATURE_GAM_VAPIC)) {
2804 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2808 if (amd_iommu_snp_en &&
2809 !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {
2810 pr_warn("Force to disable Virtual APIC due to SNP\n");
2811 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2815 /* Enabling GAM and SNPAVIC support */
2816 for_each_iommu(iommu) {
2817 if (iommu_init_ga_log(iommu) ||
2818 iommu_ga_log_enable(iommu))
2821 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2822 if (amd_iommu_snp_en)
2823 iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);
2826 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2827 pr_info("Virtual APIC enabled\n");
2831 static void enable_iommus(void)
2833 early_enable_iommus();
2834 enable_iommus_vapic();
2838 static void disable_iommus(void)
2840 struct amd_iommu *iommu;
2842 for_each_iommu(iommu)
2843 iommu_disable(iommu);
2845 #ifdef CONFIG_IRQ_REMAP
2846 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2847 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2852 * Suspend/Resume support
2853 * disable suspend until real resume implemented
2856 static void amd_iommu_resume(void)
2858 struct amd_iommu *iommu;
2860 for_each_iommu(iommu)
2861 iommu_apply_resume_quirks(iommu);
2863 /* re-load the hardware */
2866 amd_iommu_enable_interrupts();
2869 static int amd_iommu_suspend(void)
2871 /* disable IOMMUs to go out of the way for BIOS */
2877 static struct syscore_ops amd_iommu_syscore_ops = {
2878 .suspend = amd_iommu_suspend,
2879 .resume = amd_iommu_resume,
2882 static void __init free_iommu_resources(void)
2884 kmem_cache_destroy(amd_iommu_irq_cache);
2885 amd_iommu_irq_cache = NULL;
2888 free_pci_segments();
2891 /* SB IOAPIC is always on this device in AMD systems */
2892 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2894 static bool __init check_ioapic_information(void)
2896 const char *fw_bug = FW_BUG;
2897 bool ret, has_sb_ioapic;
2900 has_sb_ioapic = false;
2904 * If we have map overrides on the kernel command line the
2905 * messages in this function might not describe firmware bugs
2906 * anymore - so be careful
2911 for (idx = 0; idx < nr_ioapics; idx++) {
2912 int devid, id = mpc_ioapic_id(idx);
2914 devid = get_ioapic_devid(id);
2916 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2919 } else if (devid == IOAPIC_SB_DEVID) {
2920 has_sb_ioapic = true;
2925 if (!has_sb_ioapic) {
2927 * We expect the SB IOAPIC to be listed in the IVRS
2928 * table. The system timer is connected to the SB IOAPIC
2929 * and if we don't have it in the list the system will
2930 * panic at boot time. This situation usually happens
2931 * when the BIOS is buggy and provides us the wrong
2932 * device id for the IOAPIC in the system.
2934 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2938 pr_err("Disabling interrupt remapping\n");
2943 static void __init free_dma_resources(void)
2945 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2946 get_order(MAX_DOMAIN_ID/8));
2947 amd_iommu_pd_alloc_bitmap = NULL;
2952 static void __init ivinfo_init(void *ivrs)
2954 amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
2958 * This is the hardware init function for AMD IOMMU in the system.
2959 * This function is called either from amd_iommu_init or from the interrupt
2960 * remapping setup code.
2962 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2965 * 1 pass) Discover the most comprehensive IVHD type to use.
2967 * 2 pass) Find the highest PCI device id the driver has to handle.
2968 * Upon this information the size of the data structures is
2969 * determined that needs to be allocated.
2971 * 3 pass) Initialize the data structures just allocated with the
2972 * information in the ACPI table about available AMD IOMMUs
2973 * in the system. It also maps the PCI devices in the
2974 * system to specific IOMMUs
2976 * 4 pass) After the basic data structures are allocated and
2977 * initialized we update them with information about memory
2978 * remapping requirements parsed out of the ACPI table in
2981 * After everything is set up the IOMMUs are enabled and the necessary
2982 * hotplug and suspend notifiers are registered.
2984 static int __init early_amd_iommu_init(void)
2986 struct acpi_table_header *ivrs_base;
2987 int remap_cache_sz, ret;
2990 if (!amd_iommu_detected)
2993 status = acpi_get_table("IVRS", 0, &ivrs_base);
2994 if (status == AE_NOT_FOUND)
2996 else if (ACPI_FAILURE(status)) {
2997 const char *err = acpi_format_exception(status);
2998 pr_err("IVRS table error: %s\n", err);
3003 * Validate checksum here so we don't need to do it when
3004 * we actually parse the table
3006 ret = check_ivrs_checksum(ivrs_base);
3010 ivinfo_init(ivrs_base);
3012 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
3013 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
3015 /* Device table - directly used by all IOMMUs */
3018 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
3019 GFP_KERNEL | __GFP_ZERO,
3020 get_order(MAX_DOMAIN_ID/8));
3021 if (amd_iommu_pd_alloc_bitmap == NULL)
3025 * never allocate domain 0 because its used as the non-allocated and
3026 * error value placeholder
3028 __set_bit(0, amd_iommu_pd_alloc_bitmap);
3031 * now the data structures are allocated and basically initialized
3032 * start the real acpi table scan
3034 ret = init_iommu_all(ivrs_base);
3038 /* 5 level guest page table */
3039 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
3040 check_feature_gpt_level() == GUEST_PGTABLE_5_LEVEL)
3041 amd_iommu_gpt_level = PAGE_MODE_5_LEVEL;
3043 /* Disable any previously enabled IOMMUs */
3044 if (!is_kdump_kernel() || amd_iommu_disabled)
3047 if (amd_iommu_irq_remap)
3048 amd_iommu_irq_remap = check_ioapic_information();
3050 if (amd_iommu_irq_remap) {
3051 struct amd_iommu_pci_seg *pci_seg;
3053 * Interrupt remapping enabled, create kmem_cache for the
3057 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3058 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
3060 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
3061 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
3063 DTE_INTTAB_ALIGNMENT,
3065 if (!amd_iommu_irq_cache)
3068 for_each_pci_segment(pci_seg) {
3069 if (alloc_irq_lookup_table(pci_seg))
3074 ret = init_memory_definitions(ivrs_base);
3078 /* init the device table */
3079 init_device_table();
3082 /* Don't leak any ACPI memory */
3083 acpi_put_table(ivrs_base);
3088 static int amd_iommu_enable_interrupts(void)
3090 struct amd_iommu *iommu;
3093 for_each_iommu(iommu) {
3094 ret = iommu_init_irq(iommu);
3103 static bool __init detect_ivrs(void)
3105 struct acpi_table_header *ivrs_base;
3109 status = acpi_get_table("IVRS", 0, &ivrs_base);
3110 if (status == AE_NOT_FOUND)
3112 else if (ACPI_FAILURE(status)) {
3113 const char *err = acpi_format_exception(status);
3114 pr_err("IVRS table error: %s\n", err);
3118 acpi_put_table(ivrs_base);
3120 if (amd_iommu_force_enable)
3123 /* Don't use IOMMU if there is Stoney Ridge graphics */
3124 for (i = 0; i < 32; i++) {
3127 pci_id = read_pci_config(0, i, 0, 0);
3128 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
3129 pr_info("Disable IOMMU on Stoney Ridge\n");
3135 /* Make sure ACS will be enabled during PCI probe */
3141 /****************************************************************************
3143 * AMD IOMMU Initialization State Machine
3145 ****************************************************************************/
3147 static int __init state_next(void)
3151 switch (init_state) {
3152 case IOMMU_START_STATE:
3153 if (!detect_ivrs()) {
3154 init_state = IOMMU_NOT_FOUND;
3157 init_state = IOMMU_IVRS_DETECTED;
3160 case IOMMU_IVRS_DETECTED:
3161 if (amd_iommu_disabled) {
3162 init_state = IOMMU_CMDLINE_DISABLED;
3165 ret = early_amd_iommu_init();
3166 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
3169 case IOMMU_ACPI_FINISHED:
3170 early_enable_iommus();
3171 x86_platform.iommu_shutdown = disable_iommus;
3172 init_state = IOMMU_ENABLED;
3175 register_syscore_ops(&amd_iommu_syscore_ops);
3176 ret = amd_iommu_init_pci();
3177 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
3178 enable_iommus_vapic();
3181 case IOMMU_PCI_INIT:
3182 ret = amd_iommu_enable_interrupts();
3183 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
3185 case IOMMU_INTERRUPTS_EN:
3186 init_state = IOMMU_INITIALIZED;
3188 case IOMMU_INITIALIZED:
3191 case IOMMU_NOT_FOUND:
3192 case IOMMU_INIT_ERROR:
3193 case IOMMU_CMDLINE_DISABLED:
3194 /* Error states => do nothing */
3203 free_dma_resources();
3204 if (!irq_remapping_enabled) {
3206 free_iommu_resources();
3208 struct amd_iommu *iommu;
3209 struct amd_iommu_pci_seg *pci_seg;
3211 for_each_pci_segment(pci_seg)
3212 uninit_device_table_dma(pci_seg);
3214 for_each_iommu(iommu)
3215 iommu_flush_all_caches(iommu);
3221 static int __init iommu_go_to_state(enum iommu_init_state state)
3225 while (init_state != state) {
3226 if (init_state == IOMMU_NOT_FOUND ||
3227 init_state == IOMMU_INIT_ERROR ||
3228 init_state == IOMMU_CMDLINE_DISABLED)
3236 #ifdef CONFIG_IRQ_REMAP
3237 int __init amd_iommu_prepare(void)
3241 amd_iommu_irq_remap = true;
3243 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
3245 amd_iommu_irq_remap = false;
3249 return amd_iommu_irq_remap ? 0 : -ENODEV;
3252 int __init amd_iommu_enable(void)
3256 ret = iommu_go_to_state(IOMMU_ENABLED);
3260 irq_remapping_enabled = 1;
3261 return amd_iommu_xt_mode;
3264 void amd_iommu_disable(void)
3266 amd_iommu_suspend();
3269 int amd_iommu_reenable(int mode)
3276 int __init amd_iommu_enable_faulting(void)
3278 /* We enable MSI later when PCI is initialized */
3284 * This is the core init function for AMD IOMMU hardware in the system.
3285 * This function is called from the generic x86 DMA layer initialization
3288 static int __init amd_iommu_init(void)
3290 struct amd_iommu *iommu;
3293 ret = iommu_go_to_state(IOMMU_INITIALIZED);
3294 #ifdef CONFIG_GART_IOMMU
3295 if (ret && list_empty(&amd_iommu_list)) {
3297 * We failed to initialize the AMD IOMMU - try fallback
3298 * to GART if possible.
3304 for_each_iommu(iommu)
3305 amd_iommu_debugfs_setup(iommu);
3310 static bool amd_iommu_sme_check(void)
3312 if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
3313 (boot_cpu_data.x86 != 0x17))
3316 /* For Fam17h, a specific level of support is required */
3317 if (boot_cpu_data.microcode >= 0x08001205)
3320 if ((boot_cpu_data.microcode >= 0x08001126) &&
3321 (boot_cpu_data.microcode <= 0x080011ff))
3324 pr_notice("IOMMU not currently supported when SME is active\n");
3329 /****************************************************************************
3331 * Early detect code. This code runs at IOMMU detection time in the DMA
3332 * layer. It just looks if there is an IVRS ACPI table to detect AMD
3335 ****************************************************************************/
3336 int __init amd_iommu_detect(void)
3340 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
3343 if (!amd_iommu_sme_check())
3346 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
3350 amd_iommu_detected = true;
3352 x86_init.iommu.iommu_init = amd_iommu_init;
3357 /****************************************************************************
3359 * Parsing functions for the AMD IOMMU specific kernel command line
3362 ****************************************************************************/
3364 static int __init parse_amd_iommu_dump(char *str)
3366 amd_iommu_dump = true;
3371 static int __init parse_amd_iommu_intr(char *str)
3373 for (; *str; ++str) {
3374 if (strncmp(str, "legacy", 6) == 0) {
3375 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
3378 if (strncmp(str, "vapic", 5) == 0) {
3379 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
3386 static int __init parse_amd_iommu_options(char *str)
3392 if (strncmp(str, "fullflush", 9) == 0) {
3393 pr_warn("amd_iommu=fullflush deprecated; use iommu.strict=1 instead\n");
3394 iommu_set_dma_strict();
3395 } else if (strncmp(str, "force_enable", 12) == 0) {
3396 amd_iommu_force_enable = true;
3397 } else if (strncmp(str, "off", 3) == 0) {
3398 amd_iommu_disabled = true;
3399 } else if (strncmp(str, "force_isolation", 15) == 0) {
3400 amd_iommu_force_isolation = true;
3401 } else if (strncmp(str, "pgtbl_v1", 8) == 0) {
3402 amd_iommu_pgtable = AMD_IOMMU_V1;
3403 } else if (strncmp(str, "pgtbl_v2", 8) == 0) {
3404 amd_iommu_pgtable = AMD_IOMMU_V2;
3406 pr_notice("Unknown option - '%s'\n", str);
3409 str += strcspn(str, ",");
3417 static int __init parse_ivrs_ioapic(char *str)
3419 u32 seg = 0, bus, dev, fn;
3423 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3424 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3427 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3428 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3429 pr_warn("ivrs_ioapic%s option format deprecated; use ivrs_ioapic=%d@%04x:%02x:%02x.%d instead\n",
3430 str, id, seg, bus, dev, fn);
3434 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
3438 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
3439 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
3444 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3446 cmdline_maps = true;
3447 i = early_ioapic_map_size++;
3448 early_ioapic_map[i].id = id;
3449 early_ioapic_map[i].devid = devid;
3450 early_ioapic_map[i].cmd_line = true;
3455 static int __init parse_ivrs_hpet(char *str)
3457 u32 seg = 0, bus, dev, fn;
3461 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3462 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3465 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3466 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3467 pr_warn("ivrs_hpet%s option format deprecated; use ivrs_hpet=%d@%04x:%02x:%02x.%d instead\n",
3468 str, id, seg, bus, dev, fn);
3472 pr_err("Invalid command line: ivrs_hpet%s\n", str);
3476 if (early_hpet_map_size == EARLY_MAP_SIZE) {
3477 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3482 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3484 cmdline_maps = true;
3485 i = early_hpet_map_size++;
3486 early_hpet_map[i].id = id;
3487 early_hpet_map[i].devid = devid;
3488 early_hpet_map[i].cmd_line = true;
3493 #define ACPIID_LEN (ACPIHID_UID_LEN + ACPIHID_HID_LEN)
3495 static int __init parse_ivrs_acpihid(char *str)
3497 u32 seg = 0, bus, dev, fn;
3498 char *hid, *uid, *p, *addr;
3499 char acpiid[ACPIID_LEN] = {0};
3502 addr = strchr(str, '@');
3504 addr = strchr(str, '=');
3510 if (strlen(addr) > ACPIID_LEN)
3513 if (sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid) == 4 ||
3514 sscanf(str, "[%x:%x:%x.%x]=%s", &seg, &bus, &dev, &fn, acpiid) == 5) {
3515 pr_warn("ivrs_acpihid%s option format deprecated; use ivrs_acpihid=%s@%04x:%02x:%02x.%d instead\n",
3516 str, acpiid, seg, bus, dev, fn);
3522 /* We have the '@', make it the terminator to get just the acpiid */
3525 if (strlen(str) > ACPIID_LEN + 1)
3528 if (sscanf(str, "=%s", acpiid) != 1)
3531 if (sscanf(addr, "%x:%x.%x", &bus, &dev, &fn) == 3 ||
3532 sscanf(addr, "%x:%x:%x.%x", &seg, &bus, &dev, &fn) == 4)
3536 pr_err("Invalid command line: ivrs_acpihid%s\n", str);
3541 hid = strsep(&p, ":");
3544 if (!hid || !(*hid) || !uid) {
3545 pr_err("Invalid command line: hid or uid\n");
3550 * Ignore leading zeroes after ':', so e.g., AMDI0095:00
3551 * will match AMDI0095:0 in the second strcmp in acpi_dev_hid_uid_match
3553 while (*uid == '0' && *(uid + 1))
3556 i = early_acpihid_map_size++;
3557 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3558 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3559 early_acpihid_map[i].devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3560 early_acpihid_map[i].cmd_line = true;
3565 __setup("amd_iommu_dump", parse_amd_iommu_dump);
3566 __setup("amd_iommu=", parse_amd_iommu_options);
3567 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
3568 __setup("ivrs_ioapic", parse_ivrs_ioapic);
3569 __setup("ivrs_hpet", parse_ivrs_hpet);
3570 __setup("ivrs_acpihid", parse_ivrs_acpihid);
3572 bool amd_iommu_v2_supported(void)
3574 /* CPU page table size should match IOMMU guest page table size */
3575 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
3576 amd_iommu_gpt_level != PAGE_MODE_5_LEVEL)
3580 * Since DTE[Mode]=0 is prohibited on SNP-enabled system
3581 * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without
3582 * setting up IOMMUv1 page table.
3584 return amd_iommu_v2_present && !amd_iommu_snp_en;
3586 EXPORT_SYMBOL(amd_iommu_v2_supported);
3588 struct amd_iommu *get_amd_iommu(unsigned int idx)
3591 struct amd_iommu *iommu;
3593 for_each_iommu(iommu)
3599 /****************************************************************************
3601 * IOMMU EFR Performance Counter support functionality. This code allows
3602 * access to the IOMMU PC functionality.
3604 ****************************************************************************/
3606 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3608 struct amd_iommu *iommu = get_amd_iommu(idx);
3611 return iommu->max_banks;
3615 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3617 bool amd_iommu_pc_supported(void)
3619 return amd_iommu_pc_present;
3621 EXPORT_SYMBOL(amd_iommu_pc_supported);
3623 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3625 struct amd_iommu *iommu = get_amd_iommu(idx);
3628 return iommu->max_counters;
3632 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3634 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3635 u8 fxn, u64 *value, bool is_write)
3640 /* Make sure the IOMMU PC resource is available */
3641 if (!amd_iommu_pc_present)
3644 /* Check for valid iommu and pc register indexing */
3645 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3648 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3650 /* Limit the offset to the hw defined mmio region aperture */
3651 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3652 (iommu->max_counters << 8) | 0x28);
3653 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3654 (offset > max_offset_lim))
3658 u64 val = *value & GENMASK_ULL(47, 0);
3660 writel((u32)val, iommu->mmio_base + offset);
3661 writel((val >> 32), iommu->mmio_base + offset + 4);
3663 *value = readl(iommu->mmio_base + offset + 4);
3665 *value |= readl(iommu->mmio_base + offset);
3666 *value &= GENMASK_ULL(47, 0);
3672 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3677 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3680 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3685 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3688 #ifdef CONFIG_AMD_MEM_ENCRYPT
3689 int amd_iommu_snp_enable(void)
3692 * The SNP support requires that IOMMU must be enabled, and is
3693 * not configured in the passthrough mode.
3695 if (no_iommu || iommu_default_passthrough()) {
3696 pr_err("SNP: IOMMU is disabled or configured in passthrough mode, SNP cannot be supported");
3701 * Prevent enabling SNP after IOMMU_ENABLED state because this process
3702 * affect how IOMMU driver sets up data structures and configures
3705 if (init_state > IOMMU_ENABLED) {
3706 pr_err("SNP: Too late to enable SNP for IOMMU.\n");
3710 amd_iommu_snp_en = check_feature_on_all_iommus(FEATURE_SNP);
3711 if (!amd_iommu_snp_en)
3714 pr_info("SNP enabled\n");
3716 /* Enforce IOMMU v1 pagetable when SNP is enabled. */
3717 if (amd_iommu_pgtable != AMD_IOMMU_V1) {
3718 pr_warn("Force to using AMD IOMMU v1 page table due to SNP\n");
3719 amd_iommu_pgtable = AMD_IOMMU_V1;