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/amd-iommu.h>
20 #include <linux/export.h>
21 #include <linux/iommu.h>
22 #include <linux/kmemleak.h>
23 #include <linux/mem_encrypt.h>
24 #include <asm/pci-direct.h>
25 #include <asm/iommu.h>
27 #include <asm/msidef.h>
29 #include <asm/x86_init.h>
30 #include <asm/iommu_table.h>
31 #include <asm/io_apic.h>
32 #include <asm/irq_remapping.h>
34 #include <linux/crash_dump.h>
35 #include "amd_iommu.h"
36 #include "amd_iommu_proto.h"
37 #include "amd_iommu_types.h"
38 #include "irq_remapping.h"
41 * definitions for the ACPI scanning code
43 #define IVRS_HEADER_LENGTH 48
45 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
46 #define ACPI_IVMD_TYPE_ALL 0x20
47 #define ACPI_IVMD_TYPE 0x21
48 #define ACPI_IVMD_TYPE_RANGE 0x22
50 #define IVHD_DEV_ALL 0x01
51 #define IVHD_DEV_SELECT 0x02
52 #define IVHD_DEV_SELECT_RANGE_START 0x03
53 #define IVHD_DEV_RANGE_END 0x04
54 #define IVHD_DEV_ALIAS 0x42
55 #define IVHD_DEV_ALIAS_RANGE 0x43
56 #define IVHD_DEV_EXT_SELECT 0x46
57 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
58 #define IVHD_DEV_SPECIAL 0x48
59 #define IVHD_DEV_ACPI_HID 0xf0
61 #define UID_NOT_PRESENT 0
62 #define UID_IS_INTEGER 1
63 #define UID_IS_CHARACTER 2
65 #define IVHD_SPECIAL_IOAPIC 1
66 #define IVHD_SPECIAL_HPET 2
68 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
69 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
70 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
71 #define IVHD_FLAG_ISOC_EN_MASK 0x08
73 #define IVMD_FLAG_EXCL_RANGE 0x08
74 #define IVMD_FLAG_IW 0x04
75 #define IVMD_FLAG_IR 0x02
76 #define IVMD_FLAG_UNITY_MAP 0x01
78 #define ACPI_DEVFLAG_INITPASS 0x01
79 #define ACPI_DEVFLAG_EXTINT 0x02
80 #define ACPI_DEVFLAG_NMI 0x04
81 #define ACPI_DEVFLAG_SYSMGT1 0x10
82 #define ACPI_DEVFLAG_SYSMGT2 0x20
83 #define ACPI_DEVFLAG_LINT0 0x40
84 #define ACPI_DEVFLAG_LINT1 0x80
85 #define ACPI_DEVFLAG_ATSDIS 0x10000000
87 #define LOOP_TIMEOUT 100000
89 * ACPI table definitions
91 * These data structures are laid over the table to parse the important values
95 extern const struct iommu_ops amd_iommu_ops;
98 * structure describing one IOMMU in the ACPI table. Typically followed by one
99 * or more ivhd_entrys.
112 /* Following only valid on IVHD type 11h and 40h */
113 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
115 } __attribute__((packed));
118 * A device entry describing which devices a specific IOMMU translates and
119 * which requestor ids they use.
131 } __attribute__((packed));
134 * An AMD IOMMU memory definition structure. It defines things like exclusion
135 * ranges for devices and regions that should be unity mapped.
146 } __attribute__((packed));
149 bool amd_iommu_irq_remap __read_mostly;
151 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
152 static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
154 static bool amd_iommu_detected;
155 static bool __initdata amd_iommu_disabled;
156 static int amd_iommu_target_ivhd_type;
158 u16 amd_iommu_last_bdf; /* largest PCI device id we have
160 LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
162 bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
164 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
167 /* Array to assign indices to IOMMUs*/
168 struct amd_iommu *amd_iommus[MAX_IOMMUS];
170 /* Number of IOMMUs present in the system */
171 static int amd_iommus_present;
173 /* IOMMUs have a non-present cache? */
174 bool amd_iommu_np_cache __read_mostly;
175 bool amd_iommu_iotlb_sup __read_mostly = true;
177 u32 amd_iommu_max_pasid __read_mostly = ~0;
179 bool amd_iommu_v2_present __read_mostly;
180 static bool amd_iommu_pc_present __read_mostly;
182 bool amd_iommu_force_isolation __read_mostly;
185 * Pointer to the device table which is shared by all AMD IOMMUs
186 * it is indexed by the PCI device id or the HT unit id and contains
187 * information about the domain the device belongs to as well as the
188 * page table root pointer.
190 struct dev_table_entry *amd_iommu_dev_table;
192 * Pointer to a device table which the content of old device table
193 * will be copied to. It's only be used in kdump kernel.
195 static struct dev_table_entry *old_dev_tbl_cpy;
198 * The alias table is a driver specific data structure which contains the
199 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
200 * More than one device can share the same requestor id.
202 u16 *amd_iommu_alias_table;
205 * The rlookup table is used to find the IOMMU which is responsible
206 * for a specific device. It is also indexed by the PCI device id.
208 struct amd_iommu **amd_iommu_rlookup_table;
209 EXPORT_SYMBOL(amd_iommu_rlookup_table);
212 * This table is used to find the irq remapping table for a given device id
215 struct irq_remap_table **irq_lookup_table;
218 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
219 * to know which ones are already in use.
221 unsigned long *amd_iommu_pd_alloc_bitmap;
223 static u32 dev_table_size; /* size of the device table */
224 static u32 alias_table_size; /* size of the alias table */
225 static u32 rlookup_table_size; /* size if the rlookup table */
227 enum iommu_init_state {
238 IOMMU_CMDLINE_DISABLED,
241 /* Early ioapic and hpet maps from kernel command line */
242 #define EARLY_MAP_SIZE 4
243 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
244 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
245 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
247 static int __initdata early_ioapic_map_size;
248 static int __initdata early_hpet_map_size;
249 static int __initdata early_acpihid_map_size;
251 static bool __initdata cmdline_maps;
253 static enum iommu_init_state init_state = IOMMU_START_STATE;
255 static int amd_iommu_enable_interrupts(void);
256 static int __init iommu_go_to_state(enum iommu_init_state state);
257 static void init_device_table_dma(void);
259 static bool amd_iommu_pre_enabled = true;
261 bool translation_pre_enabled(struct amd_iommu *iommu)
263 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
265 EXPORT_SYMBOL(translation_pre_enabled);
267 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
269 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
272 static void init_translation_status(struct amd_iommu *iommu)
276 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
277 if (ctrl & (1<<CONTROL_IOMMU_EN))
278 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
281 static inline void update_last_devid(u16 devid)
283 if (devid > amd_iommu_last_bdf)
284 amd_iommu_last_bdf = devid;
287 static inline unsigned long tbl_size(int entry_size)
289 unsigned shift = PAGE_SHIFT +
290 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
295 int amd_iommu_get_num_iommus(void)
297 return amd_iommus_present;
300 /* Access to l1 and l2 indexed register spaces */
302 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
306 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
307 pci_read_config_dword(iommu->dev, 0xfc, &val);
311 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
313 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
314 pci_write_config_dword(iommu->dev, 0xfc, val);
315 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
318 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
322 pci_write_config_dword(iommu->dev, 0xf0, address);
323 pci_read_config_dword(iommu->dev, 0xf4, &val);
327 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
329 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
330 pci_write_config_dword(iommu->dev, 0xf4, val);
333 /****************************************************************************
335 * AMD IOMMU MMIO register space handling functions
337 * These functions are used to program the IOMMU device registers in
338 * MMIO space required for that driver.
340 ****************************************************************************/
343 * This function set the exclusion range in the IOMMU. DMA accesses to the
344 * exclusion range are passed through untranslated
346 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
348 u64 start = iommu->exclusion_start & PAGE_MASK;
349 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
352 if (!iommu->exclusion_start)
355 entry = start | MMIO_EXCL_ENABLE_MASK;
356 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
357 &entry, sizeof(entry));
360 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
361 &entry, sizeof(entry));
364 /* Programs the physical address of the device table into the IOMMU hardware */
365 static void iommu_set_device_table(struct amd_iommu *iommu)
369 BUG_ON(iommu->mmio_base == NULL);
371 entry = iommu_virt_to_phys(amd_iommu_dev_table);
372 entry |= (dev_table_size >> 12) - 1;
373 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
374 &entry, sizeof(entry));
377 /* Generic functions to enable/disable certain features of the IOMMU. */
378 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
382 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
383 ctrl |= (1ULL << bit);
384 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
387 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
391 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
392 ctrl &= ~(1ULL << bit);
393 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
396 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
400 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
401 ctrl &= ~CTRL_INV_TO_MASK;
402 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
403 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
406 /* Function to enable the hardware */
407 static void iommu_enable(struct amd_iommu *iommu)
409 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
412 static void iommu_disable(struct amd_iommu *iommu)
414 if (!iommu->mmio_base)
417 /* Disable command buffer */
418 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
420 /* Disable event logging and event interrupts */
421 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
422 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
424 /* Disable IOMMU GA_LOG */
425 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
426 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
428 /* Disable IOMMU hardware itself */
429 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
433 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
434 * the system has one.
436 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
438 if (!request_mem_region(address, end, "amd_iommu")) {
439 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
441 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
445 return (u8 __iomem *)ioremap(address, end);
448 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
450 if (iommu->mmio_base)
451 iounmap(iommu->mmio_base);
452 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
455 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
471 /****************************************************************************
473 * The functions below belong to the first pass of AMD IOMMU ACPI table
474 * parsing. In this pass we try to find out the highest device id this
475 * code has to handle. Upon this information the size of the shared data
476 * structures is determined later.
478 ****************************************************************************/
481 * This function calculates the length of a given IVHD entry
483 static inline int ivhd_entry_length(u8 *ivhd)
485 u32 type = ((struct ivhd_entry *)ivhd)->type;
488 return 0x04 << (*ivhd >> 6);
489 } else if (type == IVHD_DEV_ACPI_HID) {
490 /* For ACPI_HID, offset 21 is uid len */
491 return *((u8 *)ivhd + 21) + 22;
497 * After reading the highest device id from the IOMMU PCI capability header
498 * this function looks if there is a higher device id defined in the ACPI table
500 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
502 u8 *p = (void *)h, *end = (void *)h;
503 struct ivhd_entry *dev;
505 u32 ivhd_size = get_ivhd_header_size(h);
508 pr_err("Unsupported IVHD type %#x\n", h->type);
516 dev = (struct ivhd_entry *)p;
519 /* Use maximum BDF value for DEV_ALL */
520 update_last_devid(0xffff);
522 case IVHD_DEV_SELECT:
523 case IVHD_DEV_RANGE_END:
525 case IVHD_DEV_EXT_SELECT:
526 /* all the above subfield types refer to device ids */
527 update_last_devid(dev->devid);
532 p += ivhd_entry_length(p);
540 static int __init check_ivrs_checksum(struct acpi_table_header *table)
543 u8 checksum = 0, *p = (u8 *)table;
545 for (i = 0; i < table->length; ++i)
548 /* ACPI table corrupt */
549 pr_err(FW_BUG "IVRS invalid checksum\n");
557 * Iterate over all IVHD entries in the ACPI table and find the highest device
558 * id which we need to handle. This is the first of three functions which parse
559 * the ACPI table. So we check the checksum here.
561 static int __init find_last_devid_acpi(struct acpi_table_header *table)
563 u8 *p = (u8 *)table, *end = (u8 *)table;
564 struct ivhd_header *h;
566 p += IVRS_HEADER_LENGTH;
568 end += table->length;
570 h = (struct ivhd_header *)p;
571 if (h->type == amd_iommu_target_ivhd_type) {
572 int ret = find_last_devid_from_ivhd(h);
584 /****************************************************************************
586 * The following functions belong to the code path which parses the ACPI table
587 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
588 * data structures, initialize the device/alias/rlookup table and also
589 * basically initialize the hardware.
591 ****************************************************************************/
594 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
595 * write commands to that buffer later and the IOMMU will execute them
598 static int __init alloc_command_buffer(struct amd_iommu *iommu)
600 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
601 get_order(CMD_BUFFER_SIZE));
603 return iommu->cmd_buf ? 0 : -ENOMEM;
607 * This function resets the command buffer if the IOMMU stopped fetching
610 void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
612 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
614 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
615 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
616 iommu->cmd_buf_head = 0;
617 iommu->cmd_buf_tail = 0;
619 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
623 * This function writes the command buffer address to the hardware and
626 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
630 BUG_ON(iommu->cmd_buf == NULL);
632 entry = iommu_virt_to_phys(iommu->cmd_buf);
633 entry |= MMIO_CMD_SIZE_512;
635 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
636 &entry, sizeof(entry));
638 amd_iommu_reset_cmd_buffer(iommu);
642 * This function disables the command buffer
644 static void iommu_disable_command_buffer(struct amd_iommu *iommu)
646 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
649 static void __init free_command_buffer(struct amd_iommu *iommu)
651 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
654 /* allocates the memory where the IOMMU will log its events to */
655 static int __init alloc_event_buffer(struct amd_iommu *iommu)
657 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
658 get_order(EVT_BUFFER_SIZE));
660 return iommu->evt_buf ? 0 : -ENOMEM;
663 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
667 BUG_ON(iommu->evt_buf == NULL);
669 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
671 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
672 &entry, sizeof(entry));
674 /* set head and tail to zero manually */
675 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
676 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
678 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
682 * This function disables the event log buffer
684 static void iommu_disable_event_buffer(struct amd_iommu *iommu)
686 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
689 static void __init free_event_buffer(struct amd_iommu *iommu)
691 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
694 /* allocates the memory where the IOMMU will log its events to */
695 static int __init alloc_ppr_log(struct amd_iommu *iommu)
697 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
698 get_order(PPR_LOG_SIZE));
700 return iommu->ppr_log ? 0 : -ENOMEM;
703 static void iommu_enable_ppr_log(struct amd_iommu *iommu)
707 if (iommu->ppr_log == NULL)
710 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
712 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
713 &entry, sizeof(entry));
715 /* set head and tail to zero manually */
716 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
717 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
719 iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
720 iommu_feature_enable(iommu, CONTROL_PPR_EN);
723 static void __init free_ppr_log(struct amd_iommu *iommu)
725 if (iommu->ppr_log == NULL)
728 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
731 static void free_ga_log(struct amd_iommu *iommu)
733 #ifdef CONFIG_IRQ_REMAP
735 free_pages((unsigned long)iommu->ga_log,
736 get_order(GA_LOG_SIZE));
737 if (iommu->ga_log_tail)
738 free_pages((unsigned long)iommu->ga_log_tail,
743 static int iommu_ga_log_enable(struct amd_iommu *iommu)
745 #ifdef CONFIG_IRQ_REMAP
751 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
753 /* Check if already running */
754 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
757 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
758 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
760 for (i = 0; i < LOOP_TIMEOUT; ++i) {
761 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
762 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
766 if (i >= LOOP_TIMEOUT)
768 #endif /* CONFIG_IRQ_REMAP */
772 #ifdef CONFIG_IRQ_REMAP
773 static int iommu_init_ga_log(struct amd_iommu *iommu)
777 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
780 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
781 get_order(GA_LOG_SIZE));
785 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
787 if (!iommu->ga_log_tail)
790 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
791 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
792 &entry, sizeof(entry));
793 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
794 (BIT_ULL(52)-1)) & ~7ULL;
795 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
796 &entry, sizeof(entry));
797 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
798 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
805 #endif /* CONFIG_IRQ_REMAP */
807 static int iommu_init_ga(struct amd_iommu *iommu)
811 #ifdef CONFIG_IRQ_REMAP
812 /* Note: We have already checked GASup from IVRS table.
813 * Now, we need to make sure that GAMSup is set.
815 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
816 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
817 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
819 ret = iommu_init_ga_log(iommu);
820 #endif /* CONFIG_IRQ_REMAP */
825 static void iommu_enable_xt(struct amd_iommu *iommu)
827 #ifdef CONFIG_IRQ_REMAP
829 * XT mode (32-bit APIC destination ID) requires
830 * GA mode (128-bit IRTE support) as a prerequisite.
832 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
833 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
834 iommu_feature_enable(iommu, CONTROL_XT_EN);
835 #endif /* CONFIG_IRQ_REMAP */
838 static void iommu_enable_gt(struct amd_iommu *iommu)
840 if (!iommu_feature(iommu, FEATURE_GT))
843 iommu_feature_enable(iommu, CONTROL_GT_EN);
846 /* sets a specific bit in the device table entry. */
847 static void set_dev_entry_bit(u16 devid, u8 bit)
849 int i = (bit >> 6) & 0x03;
850 int _bit = bit & 0x3f;
852 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
855 static int get_dev_entry_bit(u16 devid, u8 bit)
857 int i = (bit >> 6) & 0x03;
858 int _bit = bit & 0x3f;
860 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
864 static bool copy_device_table(void)
866 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
867 struct dev_table_entry *old_devtb = NULL;
868 u32 lo, hi, devid, old_devtb_size;
869 phys_addr_t old_devtb_phys;
870 struct amd_iommu *iommu;
871 u16 dom_id, dte_v, irq_v;
875 if (!amd_iommu_pre_enabled)
878 pr_warn("Translation is already enabled - trying to copy translation structures\n");
879 for_each_iommu(iommu) {
880 /* All IOMMUs should use the same device table with the same size */
881 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
882 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
883 entry = (((u64) hi) << 32) + lo;
884 if (last_entry && last_entry != entry) {
885 pr_err("IOMMU:%d should use the same dev table as others!\n",
891 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
892 if (old_devtb_size != dev_table_size) {
893 pr_err("The device table size of IOMMU:%d is not expected!\n",
900 * When SME is enabled in the first kernel, the entry includes the
901 * memory encryption mask(sme_me_mask), we must remove the memory
902 * encryption mask to obtain the true physical address in kdump kernel.
904 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
906 if (old_devtb_phys >= 0x100000000ULL) {
907 pr_err("The address of old device table is above 4G, not trustworthy!\n");
910 old_devtb = (sme_active() && is_kdump_kernel())
911 ? (__force void *)ioremap_encrypted(old_devtb_phys,
913 : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
918 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
919 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
920 get_order(dev_table_size));
921 if (old_dev_tbl_cpy == NULL) {
922 pr_err("Failed to allocate memory for copying old device table!\n");
926 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
927 old_dev_tbl_cpy[devid] = old_devtb[devid];
928 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
929 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
931 if (dte_v && dom_id) {
932 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
933 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
934 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
935 /* If gcr3 table existed, mask it out */
936 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
937 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
938 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
939 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
940 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
942 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
946 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
947 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
948 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
949 if (irq_v && (int_ctl || int_tab_len)) {
950 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
951 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
952 pr_err("Wrong old irq remapping flag: %#x\n", devid);
956 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
964 void amd_iommu_apply_erratum_63(u16 devid)
968 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
969 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
972 set_dev_entry_bit(devid, DEV_ENTRY_IW);
975 /* Writes the specific IOMMU for a device into the rlookup table */
976 static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
978 amd_iommu_rlookup_table[devid] = iommu;
982 * This function takes the device specific flags read from the ACPI
983 * table and sets up the device table entry with that information
985 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
986 u16 devid, u32 flags, u32 ext_flags)
988 if (flags & ACPI_DEVFLAG_INITPASS)
989 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
990 if (flags & ACPI_DEVFLAG_EXTINT)
991 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
992 if (flags & ACPI_DEVFLAG_NMI)
993 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
994 if (flags & ACPI_DEVFLAG_SYSMGT1)
995 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
996 if (flags & ACPI_DEVFLAG_SYSMGT2)
997 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
998 if (flags & ACPI_DEVFLAG_LINT0)
999 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
1000 if (flags & ACPI_DEVFLAG_LINT1)
1001 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
1003 amd_iommu_apply_erratum_63(devid);
1005 set_iommu_for_device(iommu, devid);
1008 int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
1010 struct devid_map *entry;
1011 struct list_head *list;
1013 if (type == IVHD_SPECIAL_IOAPIC)
1015 else if (type == IVHD_SPECIAL_HPET)
1020 list_for_each_entry(entry, list, list) {
1021 if (!(entry->id == id && entry->cmd_line))
1024 pr_info("Command-line override present for %s id %d - ignoring\n",
1025 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1027 *devid = entry->devid;
1032 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1037 entry->devid = *devid;
1038 entry->cmd_line = cmd_line;
1040 list_add_tail(&entry->list, list);
1045 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1048 struct acpihid_map_entry *entry;
1049 struct list_head *list = &acpihid_map;
1051 list_for_each_entry(entry, list, list) {
1052 if (strcmp(entry->hid, hid) ||
1053 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1057 pr_info("Command-line override for hid:%s uid:%s\n",
1059 *devid = entry->devid;
1063 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1067 memcpy(entry->uid, uid, strlen(uid));
1068 memcpy(entry->hid, hid, strlen(hid));
1069 entry->devid = *devid;
1070 entry->cmd_line = cmd_line;
1071 entry->root_devid = (entry->devid & (~0x7));
1073 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1074 entry->cmd_line ? "cmd" : "ivrs",
1075 entry->hid, entry->uid, entry->root_devid);
1077 list_add_tail(&entry->list, list);
1081 static int __init add_early_maps(void)
1085 for (i = 0; i < early_ioapic_map_size; ++i) {
1086 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1087 early_ioapic_map[i].id,
1088 &early_ioapic_map[i].devid,
1089 early_ioapic_map[i].cmd_line);
1094 for (i = 0; i < early_hpet_map_size; ++i) {
1095 ret = add_special_device(IVHD_SPECIAL_HPET,
1096 early_hpet_map[i].id,
1097 &early_hpet_map[i].devid,
1098 early_hpet_map[i].cmd_line);
1103 for (i = 0; i < early_acpihid_map_size; ++i) {
1104 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1105 early_acpihid_map[i].uid,
1106 &early_acpihid_map[i].devid,
1107 early_acpihid_map[i].cmd_line);
1116 * Reads the device exclusion range from ACPI and initializes the IOMMU with
1119 static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1121 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1125 * Treat per-device exclusion ranges as r/w unity-mapped regions
1126 * since some buggy BIOSes might lead to the overwritten exclusion
1127 * range (exclusion_start and exclusion_length members). This
1128 * happens when there are multiple exclusion ranges (IVMD entries)
1129 * defined in ACPI table.
1131 m->flags = (IVMD_FLAG_IW | IVMD_FLAG_IR | IVMD_FLAG_UNITY_MAP);
1135 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1136 * initializes the hardware and our data structures with it.
1138 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1139 struct ivhd_header *h)
1142 u8 *end = p, flags = 0;
1143 u16 devid = 0, devid_start = 0, devid_to = 0;
1144 u32 dev_i, ext_flags = 0;
1146 struct ivhd_entry *e;
1151 ret = add_early_maps();
1155 amd_iommu_apply_ivrs_quirks();
1158 * First save the recommended feature enable bits from ACPI
1160 iommu->acpi_flags = h->flags;
1163 * Done. Now parse the device entries
1165 ivhd_size = get_ivhd_header_size(h);
1167 pr_err("Unsupported IVHD type %#x\n", h->type);
1177 e = (struct ivhd_entry *)p;
1181 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1183 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1184 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1186 case IVHD_DEV_SELECT:
1188 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1190 PCI_BUS_NUM(e->devid),
1196 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1198 case IVHD_DEV_SELECT_RANGE_START:
1200 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1201 "devid: %02x:%02x.%x flags: %02x\n",
1202 PCI_BUS_NUM(e->devid),
1207 devid_start = e->devid;
1212 case IVHD_DEV_ALIAS:
1214 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1215 "flags: %02x devid_to: %02x:%02x.%x\n",
1216 PCI_BUS_NUM(e->devid),
1220 PCI_BUS_NUM(e->ext >> 8),
1221 PCI_SLOT(e->ext >> 8),
1222 PCI_FUNC(e->ext >> 8));
1225 devid_to = e->ext >> 8;
1226 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1227 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1228 amd_iommu_alias_table[devid] = devid_to;
1230 case IVHD_DEV_ALIAS_RANGE:
1232 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1233 "devid: %02x:%02x.%x flags: %02x "
1234 "devid_to: %02x:%02x.%x\n",
1235 PCI_BUS_NUM(e->devid),
1239 PCI_BUS_NUM(e->ext >> 8),
1240 PCI_SLOT(e->ext >> 8),
1241 PCI_FUNC(e->ext >> 8));
1243 devid_start = e->devid;
1245 devid_to = e->ext >> 8;
1249 case IVHD_DEV_EXT_SELECT:
1251 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1252 "flags: %02x ext: %08x\n",
1253 PCI_BUS_NUM(e->devid),
1259 set_dev_entry_from_acpi(iommu, devid, e->flags,
1262 case IVHD_DEV_EXT_SELECT_RANGE:
1264 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1265 "%02x:%02x.%x flags: %02x ext: %08x\n",
1266 PCI_BUS_NUM(e->devid),
1271 devid_start = e->devid;
1276 case IVHD_DEV_RANGE_END:
1278 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1279 PCI_BUS_NUM(e->devid),
1281 PCI_FUNC(e->devid));
1284 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1286 amd_iommu_alias_table[dev_i] = devid_to;
1287 set_dev_entry_from_acpi(iommu,
1288 devid_to, flags, ext_flags);
1290 set_dev_entry_from_acpi(iommu, dev_i,
1294 case IVHD_DEV_SPECIAL: {
1300 handle = e->ext & 0xff;
1301 devid = (e->ext >> 8) & 0xffff;
1302 type = (e->ext >> 24) & 0xff;
1304 if (type == IVHD_SPECIAL_IOAPIC)
1306 else if (type == IVHD_SPECIAL_HPET)
1311 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1317 ret = add_special_device(type, handle, &devid, false);
1322 * add_special_device might update the devid in case a
1323 * command-line override is present. So call
1324 * set_dev_entry_from_acpi after add_special_device.
1326 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1330 case IVHD_DEV_ACPI_HID: {
1332 u8 hid[ACPIHID_HID_LEN] = {0};
1333 u8 uid[ACPIHID_UID_LEN] = {0};
1336 if (h->type != 0x40) {
1337 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1342 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1343 hid[ACPIHID_HID_LEN - 1] = '\0';
1346 pr_err(FW_BUG "Invalid HID.\n");
1351 case UID_NOT_PRESENT:
1354 pr_warn(FW_BUG "Invalid UID length.\n");
1357 case UID_IS_INTEGER:
1359 sprintf(uid, "%d", e->uid);
1362 case UID_IS_CHARACTER:
1364 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1365 uid[ACPIHID_UID_LEN - 1] = '\0';
1373 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1381 ret = add_acpi_hid_device(hid, uid, &devid, false);
1386 * add_special_device might update the devid in case a
1387 * command-line override is present. So call
1388 * set_dev_entry_from_acpi after add_special_device.
1390 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1398 p += ivhd_entry_length(p);
1404 static void __init free_iommu_one(struct amd_iommu *iommu)
1406 free_command_buffer(iommu);
1407 free_event_buffer(iommu);
1408 free_ppr_log(iommu);
1410 iommu_unmap_mmio_space(iommu);
1413 static void __init free_iommu_all(void)
1415 struct amd_iommu *iommu, *next;
1417 for_each_iommu_safe(iommu, next) {
1418 list_del(&iommu->list);
1419 free_iommu_one(iommu);
1425 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1427 * BIOS should disable L2B micellaneous clock gating by setting
1428 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1430 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1434 if ((boot_cpu_data.x86 != 0x15) ||
1435 (boot_cpu_data.x86_model < 0x10) ||
1436 (boot_cpu_data.x86_model > 0x1f))
1439 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1440 pci_read_config_dword(iommu->dev, 0xf4, &value);
1445 /* Select NB indirect register 0x90 and enable writing */
1446 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1448 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1449 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
1451 /* Clear the enable writing bit */
1452 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1456 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1458 * BIOS should enable ATS write permission check by setting
1459 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1461 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1465 if ((boot_cpu_data.x86 != 0x15) ||
1466 (boot_cpu_data.x86_model < 0x30) ||
1467 (boot_cpu_data.x86_model > 0x3f))
1470 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1471 value = iommu_read_l2(iommu, 0x47);
1476 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1477 iommu_write_l2(iommu, 0x47, value | BIT(0));
1479 pci_info(iommu->dev, "Applying ATS write check workaround\n");
1483 * This function clues the initialization function for one IOMMU
1484 * together and also allocates the command buffer and programs the
1485 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1487 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1491 raw_spin_lock_init(&iommu->lock);
1493 /* Add IOMMU to internal data structures */
1494 list_add_tail(&iommu->list, &amd_iommu_list);
1495 iommu->index = amd_iommus_present++;
1497 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1498 WARN(1, "System has more IOMMUs than supported by this driver\n");
1502 /* Index is fine - add IOMMU to the array */
1503 amd_iommus[iommu->index] = iommu;
1506 * Copy data from ACPI table entry to the iommu struct
1508 iommu->devid = h->devid;
1509 iommu->cap_ptr = h->cap_ptr;
1510 iommu->pci_seg = h->pci_seg;
1511 iommu->mmio_phys = h->mmio_phys;
1515 /* Check if IVHD EFR contains proper max banks/counters */
1516 if ((h->efr_attr != 0) &&
1517 ((h->efr_attr & (0xF << 13)) != 0) &&
1518 ((h->efr_attr & (0x3F << 17)) != 0))
1519 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1521 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1522 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1523 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1527 if (h->efr_reg & (1 << 9))
1528 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1530 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1531 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1532 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1534 * Note: Since iommu_update_intcapxt() leverages
1535 * the IOMMU MMIO access to MSI capability block registers
1536 * for MSI address lo/hi/data, we need to check both
1537 * EFR[XtSup] and EFR[MsiCapMmioSup] for x2APIC support.
1539 if ((h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT)) &&
1540 (h->efr_reg & BIT(IOMMU_EFR_MSICAPMMIOSUP_SHIFT)))
1541 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1547 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1548 iommu->mmio_phys_end);
1549 if (!iommu->mmio_base)
1552 if (alloc_command_buffer(iommu))
1555 if (alloc_event_buffer(iommu))
1558 iommu->int_enabled = false;
1560 init_translation_status(iommu);
1561 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1562 iommu_disable(iommu);
1563 clear_translation_pre_enabled(iommu);
1564 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1567 if (amd_iommu_pre_enabled)
1568 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1570 ret = init_iommu_from_acpi(iommu, h);
1574 ret = amd_iommu_create_irq_domain(iommu);
1579 * Make sure IOMMU is not considered to translate itself. The IVRS
1580 * table tells us so, but this is a lie!
1582 amd_iommu_rlookup_table[iommu->devid] = NULL;
1588 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1589 * @ivrs Pointer to the IVRS header
1591 * This function search through all IVDB of the maximum supported IVHD
1593 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1595 u8 *base = (u8 *)ivrs;
1596 struct ivhd_header *ivhd = (struct ivhd_header *)
1597 (base + IVRS_HEADER_LENGTH);
1598 u8 last_type = ivhd->type;
1599 u16 devid = ivhd->devid;
1601 while (((u8 *)ivhd - base < ivrs->length) &&
1602 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1603 u8 *p = (u8 *) ivhd;
1605 if (ivhd->devid == devid)
1606 last_type = ivhd->type;
1607 ivhd = (struct ivhd_header *)(p + ivhd->length);
1614 * Iterates over all IOMMU entries in the ACPI table, allocates the
1615 * IOMMU structure and initializes it with init_iommu_one()
1617 static int __init init_iommu_all(struct acpi_table_header *table)
1619 u8 *p = (u8 *)table, *end = (u8 *)table;
1620 struct ivhd_header *h;
1621 struct amd_iommu *iommu;
1624 end += table->length;
1625 p += IVRS_HEADER_LENGTH;
1628 h = (struct ivhd_header *)p;
1629 if (*p == amd_iommu_target_ivhd_type) {
1631 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1632 "seg: %d flags: %01x info %04x\n",
1633 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
1634 PCI_FUNC(h->devid), h->cap_ptr,
1635 h->pci_seg, h->flags, h->info);
1636 DUMP_printk(" mmio-addr: %016llx\n",
1639 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1643 ret = init_iommu_one(iommu, h);
1655 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1656 u8 fxn, u64 *value, bool is_write);
1658 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1660 struct pci_dev *pdev = iommu->dev;
1661 u64 val = 0xabcd, val2 = 0, save_reg = 0;
1663 if (!iommu_feature(iommu, FEATURE_PC))
1666 amd_iommu_pc_present = true;
1668 /* save the value to restore, if writable */
1669 if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, false))
1672 /* Check if the performance counters can be written to */
1673 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1674 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
1679 if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, true))
1682 pci_info(pdev, "IOMMU performance counters supported\n");
1684 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1685 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1686 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1691 pci_err(pdev, "Unable to read/write to IOMMU perf counter.\n");
1692 amd_iommu_pc_present = false;
1696 static ssize_t amd_iommu_show_cap(struct device *dev,
1697 struct device_attribute *attr,
1700 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1701 return sprintf(buf, "%x\n", iommu->cap);
1703 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1705 static ssize_t amd_iommu_show_features(struct device *dev,
1706 struct device_attribute *attr,
1709 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1710 return sprintf(buf, "%llx\n", iommu->features);
1712 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1714 static struct attribute *amd_iommu_attrs[] = {
1716 &dev_attr_features.attr,
1720 static struct attribute_group amd_iommu_group = {
1721 .name = "amd-iommu",
1722 .attrs = amd_iommu_attrs,
1725 static const struct attribute_group *amd_iommu_groups[] = {
1730 static int __init iommu_init_pci(struct amd_iommu *iommu)
1732 int cap_ptr = iommu->cap_ptr;
1735 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1736 iommu->devid & 0xff);
1740 /* Prevent binding other PCI device drivers to IOMMU devices */
1741 iommu->dev->match_driver = false;
1743 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1746 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1747 amd_iommu_iotlb_sup = false;
1749 /* read extended feature bits */
1750 iommu->features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1752 if (iommu_feature(iommu, FEATURE_GT)) {
1757 pasmax = iommu->features & FEATURE_PASID_MASK;
1758 pasmax >>= FEATURE_PASID_SHIFT;
1759 max_pasid = (1 << (pasmax + 1)) - 1;
1761 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1763 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
1765 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1766 glxval >>= FEATURE_GLXVAL_SHIFT;
1768 if (amd_iommu_max_glx_val == -1)
1769 amd_iommu_max_glx_val = glxval;
1771 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1774 if (iommu_feature(iommu, FEATURE_GT) &&
1775 iommu_feature(iommu, FEATURE_PPR)) {
1776 iommu->is_iommu_v2 = true;
1777 amd_iommu_v2_present = true;
1780 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1783 ret = iommu_init_ga(iommu);
1787 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1788 amd_iommu_np_cache = true;
1790 init_iommu_perf_ctr(iommu);
1792 if (is_rd890_iommu(iommu->dev)) {
1796 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1800 * Some rd890 systems may not be fully reconfigured by the
1801 * BIOS, so it's necessary for us to store this information so
1802 * it can be reprogrammed on resume
1804 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1805 &iommu->stored_addr_lo);
1806 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1807 &iommu->stored_addr_hi);
1809 /* Low bit locks writes to configuration space */
1810 iommu->stored_addr_lo &= ~1;
1812 for (i = 0; i < 6; i++)
1813 for (j = 0; j < 0x12; j++)
1814 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1816 for (i = 0; i < 0x83; i++)
1817 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1820 amd_iommu_erratum_746_workaround(iommu);
1821 amd_iommu_ats_write_check_workaround(iommu);
1823 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1824 amd_iommu_groups, "ivhd%d", iommu->index);
1825 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1826 iommu_device_register(&iommu->iommu);
1828 return pci_enable_device(iommu->dev);
1831 static void print_iommu_info(void)
1833 static const char * const feat_str[] = {
1834 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1835 "IA", "GA", "HE", "PC"
1837 struct amd_iommu *iommu;
1839 for_each_iommu(iommu) {
1840 struct pci_dev *pdev = iommu->dev;
1843 pci_info(pdev, "Found IOMMU cap 0x%hx\n", iommu->cap_ptr);
1845 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1846 pci_info(pdev, "Extended features (%#llx):\n",
1848 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
1849 if (iommu_feature(iommu, (1ULL << i)))
1850 pr_cont(" %s", feat_str[i]);
1853 if (iommu->features & FEATURE_GAM_VAPIC)
1854 pr_cont(" GA_vAPIC");
1859 if (irq_remapping_enabled) {
1860 pr_info("Interrupt remapping enabled\n");
1861 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1862 pr_info("Virtual APIC enabled\n");
1863 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1864 pr_info("X2APIC enabled\n");
1868 static int __init amd_iommu_init_pci(void)
1870 struct amd_iommu *iommu;
1873 for_each_iommu(iommu) {
1874 ret = iommu_init_pci(iommu);
1880 * Order is important here to make sure any unity map requirements are
1881 * fulfilled. The unity mappings are created and written to the device
1882 * table during the amd_iommu_init_api() call.
1884 * After that we call init_device_table_dma() to make sure any
1885 * uninitialized DTE will block DMA, and in the end we flush the caches
1886 * of all IOMMUs to make sure the changes to the device table are
1889 ret = amd_iommu_init_api();
1891 init_device_table_dma();
1893 for_each_iommu(iommu)
1894 iommu_flush_all_caches(iommu);
1902 /****************************************************************************
1904 * The following functions initialize the MSI interrupts for all IOMMUs
1905 * in the system. It's a bit challenging because there could be multiple
1906 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1909 ****************************************************************************/
1911 static int iommu_setup_msi(struct amd_iommu *iommu)
1915 r = pci_enable_msi(iommu->dev);
1919 r = request_threaded_irq(iommu->dev->irq,
1920 amd_iommu_int_handler,
1921 amd_iommu_int_thread,
1926 pci_disable_msi(iommu->dev);
1930 iommu->int_enabled = true;
1935 #define XT_INT_DEST_MODE(x) (((x) & 0x1ULL) << 2)
1936 #define XT_INT_DEST_LO(x) (((x) & 0xFFFFFFULL) << 8)
1937 #define XT_INT_VEC(x) (((x) & 0xFFULL) << 32)
1938 #define XT_INT_DEST_HI(x) ((((x) >> 24) & 0xFFULL) << 56)
1941 * Setup the IntCapXT registers with interrupt routing information
1942 * based on the PCI MSI capability block registers, accessed via
1943 * MMIO MSI address low/hi and MSI data registers.
1945 static void iommu_update_intcapxt(struct amd_iommu *iommu)
1948 u32 addr_lo = readl(iommu->mmio_base + MMIO_MSI_ADDR_LO_OFFSET);
1949 u32 addr_hi = readl(iommu->mmio_base + MMIO_MSI_ADDR_HI_OFFSET);
1950 u32 data = readl(iommu->mmio_base + MMIO_MSI_DATA_OFFSET);
1951 bool dm = (addr_lo >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
1952 u32 dest = ((addr_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xFF);
1954 if (x2apic_enabled())
1955 dest |= MSI_ADDR_EXT_DEST_ID(addr_hi);
1957 val = XT_INT_VEC(data & 0xFF) |
1958 XT_INT_DEST_MODE(dm) |
1959 XT_INT_DEST_LO(dest) |
1960 XT_INT_DEST_HI(dest);
1963 * Current IOMMU implemtation uses the same IRQ for all
1964 * 3 IOMMU interrupts.
1966 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
1967 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
1968 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
1971 static void _irq_notifier_notify(struct irq_affinity_notify *notify,
1972 const cpumask_t *mask)
1974 struct amd_iommu *iommu;
1976 for_each_iommu(iommu) {
1977 if (iommu->dev->irq == notify->irq) {
1978 iommu_update_intcapxt(iommu);
1984 static void _irq_notifier_release(struct kref *ref)
1988 static int iommu_init_intcapxt(struct amd_iommu *iommu)
1991 struct irq_affinity_notify *notify = &iommu->intcapxt_notify;
1994 * IntCapXT requires XTSup=1 and MsiCapMmioSup=1,
1995 * which can be inferred from amd_iommu_xt_mode.
1997 if (amd_iommu_xt_mode != IRQ_REMAP_X2APIC_MODE)
2001 * Also, we need to setup notifier to update the IntCapXT registers
2002 * whenever the irq affinity is changed from user-space.
2004 notify->irq = iommu->dev->irq;
2005 notify->notify = _irq_notifier_notify,
2006 notify->release = _irq_notifier_release,
2007 ret = irq_set_affinity_notifier(iommu->dev->irq, notify);
2009 pr_err("Failed to register irq affinity notifier (devid=%#x, irq %d)\n",
2010 iommu->devid, iommu->dev->irq);
2014 iommu_update_intcapxt(iommu);
2015 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2019 static int iommu_init_msi(struct amd_iommu *iommu)
2023 if (iommu->int_enabled)
2026 if (iommu->dev->msi_cap)
2027 ret = iommu_setup_msi(iommu);
2035 ret = iommu_init_intcapxt(iommu);
2039 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2041 if (iommu->ppr_log != NULL)
2042 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
2044 iommu_ga_log_enable(iommu);
2049 /****************************************************************************
2051 * The next functions belong to the third pass of parsing the ACPI
2052 * table. In this last pass the memory mapping requirements are
2053 * gathered (like exclusion and unity mapping ranges).
2055 ****************************************************************************/
2057 static void __init free_unity_maps(void)
2059 struct unity_map_entry *entry, *next;
2061 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
2062 list_del(&entry->list);
2067 /* called when we find an exclusion range definition in ACPI */
2068 static int __init init_exclusion_range(struct ivmd_header *m)
2073 case ACPI_IVMD_TYPE:
2074 set_device_exclusion_range(m->devid, m);
2076 case ACPI_IVMD_TYPE_ALL:
2077 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2078 set_device_exclusion_range(i, m);
2080 case ACPI_IVMD_TYPE_RANGE:
2081 for (i = m->devid; i <= m->aux; ++i)
2082 set_device_exclusion_range(i, m);
2091 /* called for unity map ACPI definition */
2092 static int __init init_unity_map_range(struct ivmd_header *m)
2094 struct unity_map_entry *e = NULL;
2097 e = kzalloc(sizeof(*e), GFP_KERNEL);
2101 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2102 init_exclusion_range(m);
2108 case ACPI_IVMD_TYPE:
2109 s = "IVMD_TYPEi\t\t\t";
2110 e->devid_start = e->devid_end = m->devid;
2112 case ACPI_IVMD_TYPE_ALL:
2113 s = "IVMD_TYPE_ALL\t\t";
2115 e->devid_end = amd_iommu_last_bdf;
2117 case ACPI_IVMD_TYPE_RANGE:
2118 s = "IVMD_TYPE_RANGE\t\t";
2119 e->devid_start = m->devid;
2120 e->devid_end = m->aux;
2123 e->address_start = PAGE_ALIGN(m->range_start);
2124 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2125 e->prot = m->flags >> 1;
2127 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2128 " range_start: %016llx range_end: %016llx flags: %x\n", s,
2129 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2130 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
2131 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2132 e->address_start, e->address_end, m->flags);
2134 list_add_tail(&e->list, &amd_iommu_unity_map);
2139 /* iterates over all memory definitions we find in the ACPI table */
2140 static int __init init_memory_definitions(struct acpi_table_header *table)
2142 u8 *p = (u8 *)table, *end = (u8 *)table;
2143 struct ivmd_header *m;
2145 end += table->length;
2146 p += IVRS_HEADER_LENGTH;
2149 m = (struct ivmd_header *)p;
2150 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
2151 init_unity_map_range(m);
2160 * Init the device table to not allow DMA access for devices
2162 static void init_device_table_dma(void)
2166 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2167 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2168 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
2172 static void __init uninit_device_table_dma(void)
2176 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2177 amd_iommu_dev_table[devid].data[0] = 0ULL;
2178 amd_iommu_dev_table[devid].data[1] = 0ULL;
2182 static void init_device_table(void)
2186 if (!amd_iommu_irq_remap)
2189 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2190 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2193 static void iommu_init_flags(struct amd_iommu *iommu)
2195 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2196 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2197 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2199 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2200 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2201 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2203 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2204 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2205 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2207 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2208 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2209 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2212 * make IOMMU memory accesses cache coherent
2214 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2216 /* Set IOTLB invalidation timeout to 1s */
2217 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2220 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2223 u32 ioc_feature_control;
2224 struct pci_dev *pdev = iommu->root_pdev;
2226 /* RD890 BIOSes may not have completely reconfigured the iommu */
2227 if (!is_rd890_iommu(iommu->dev) || !pdev)
2231 * First, we need to ensure that the iommu is enabled. This is
2232 * controlled by a register in the northbridge
2235 /* Select Northbridge indirect register 0x75 and enable writing */
2236 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2237 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2239 /* Enable the iommu */
2240 if (!(ioc_feature_control & 0x1))
2241 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2243 /* Restore the iommu BAR */
2244 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2245 iommu->stored_addr_lo);
2246 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2247 iommu->stored_addr_hi);
2249 /* Restore the l1 indirect regs for each of the 6 l1s */
2250 for (i = 0; i < 6; i++)
2251 for (j = 0; j < 0x12; j++)
2252 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2254 /* Restore the l2 indirect regs */
2255 for (i = 0; i < 0x83; i++)
2256 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2258 /* Lock PCI setup registers */
2259 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2260 iommu->stored_addr_lo | 1);
2263 static void iommu_enable_ga(struct amd_iommu *iommu)
2265 #ifdef CONFIG_IRQ_REMAP
2266 switch (amd_iommu_guest_ir) {
2267 case AMD_IOMMU_GUEST_IR_VAPIC:
2268 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2270 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2271 iommu_feature_enable(iommu, CONTROL_GA_EN);
2272 iommu->irte_ops = &irte_128_ops;
2275 iommu->irte_ops = &irte_32_ops;
2281 static void early_enable_iommu(struct amd_iommu *iommu)
2283 iommu_disable(iommu);
2284 iommu_init_flags(iommu);
2285 iommu_set_device_table(iommu);
2286 iommu_enable_command_buffer(iommu);
2287 iommu_enable_event_buffer(iommu);
2288 iommu_set_exclusion_range(iommu);
2289 iommu_enable_ga(iommu);
2290 iommu_enable_xt(iommu);
2291 iommu_enable(iommu);
2292 iommu_flush_all_caches(iommu);
2296 * This function finally enables all IOMMUs found in the system after
2297 * they have been initialized.
2299 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2300 * the old content of device table entries. Not this case or copy failed,
2301 * just continue as normal kernel does.
2303 static void early_enable_iommus(void)
2305 struct amd_iommu *iommu;
2308 if (!copy_device_table()) {
2310 * If come here because of failure in copying device table from old
2311 * kernel with all IOMMUs enabled, print error message and try to
2312 * free allocated old_dev_tbl_cpy.
2314 if (amd_iommu_pre_enabled)
2315 pr_err("Failed to copy DEV table from previous kernel.\n");
2316 if (old_dev_tbl_cpy != NULL)
2317 free_pages((unsigned long)old_dev_tbl_cpy,
2318 get_order(dev_table_size));
2320 for_each_iommu(iommu) {
2321 clear_translation_pre_enabled(iommu);
2322 early_enable_iommu(iommu);
2325 pr_info("Copied DEV table from previous kernel.\n");
2326 free_pages((unsigned long)amd_iommu_dev_table,
2327 get_order(dev_table_size));
2328 amd_iommu_dev_table = old_dev_tbl_cpy;
2329 for_each_iommu(iommu) {
2330 iommu_disable_command_buffer(iommu);
2331 iommu_disable_event_buffer(iommu);
2332 iommu_enable_command_buffer(iommu);
2333 iommu_enable_event_buffer(iommu);
2334 iommu_enable_ga(iommu);
2335 iommu_enable_xt(iommu);
2336 iommu_set_device_table(iommu);
2337 iommu_flush_all_caches(iommu);
2341 #ifdef CONFIG_IRQ_REMAP
2342 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2343 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2347 static void enable_iommus_v2(void)
2349 struct amd_iommu *iommu;
2351 for_each_iommu(iommu) {
2352 iommu_enable_ppr_log(iommu);
2353 iommu_enable_gt(iommu);
2357 static void enable_iommus(void)
2359 early_enable_iommus();
2364 static void disable_iommus(void)
2366 struct amd_iommu *iommu;
2368 for_each_iommu(iommu)
2369 iommu_disable(iommu);
2371 #ifdef CONFIG_IRQ_REMAP
2372 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2373 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2378 * Suspend/Resume support
2379 * disable suspend until real resume implemented
2382 static void amd_iommu_resume(void)
2384 struct amd_iommu *iommu;
2386 for_each_iommu(iommu)
2387 iommu_apply_resume_quirks(iommu);
2389 /* re-load the hardware */
2392 amd_iommu_enable_interrupts();
2395 static int amd_iommu_suspend(void)
2397 /* disable IOMMUs to go out of the way for BIOS */
2403 static struct syscore_ops amd_iommu_syscore_ops = {
2404 .suspend = amd_iommu_suspend,
2405 .resume = amd_iommu_resume,
2408 static void __init free_iommu_resources(void)
2410 kmemleak_free(irq_lookup_table);
2411 free_pages((unsigned long)irq_lookup_table,
2412 get_order(rlookup_table_size));
2413 irq_lookup_table = NULL;
2415 kmem_cache_destroy(amd_iommu_irq_cache);
2416 amd_iommu_irq_cache = NULL;
2418 free_pages((unsigned long)amd_iommu_rlookup_table,
2419 get_order(rlookup_table_size));
2420 amd_iommu_rlookup_table = NULL;
2422 free_pages((unsigned long)amd_iommu_alias_table,
2423 get_order(alias_table_size));
2424 amd_iommu_alias_table = NULL;
2426 free_pages((unsigned long)amd_iommu_dev_table,
2427 get_order(dev_table_size));
2428 amd_iommu_dev_table = NULL;
2433 /* SB IOAPIC is always on this device in AMD systems */
2434 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2436 static bool __init check_ioapic_information(void)
2438 const char *fw_bug = FW_BUG;
2439 bool ret, has_sb_ioapic;
2442 has_sb_ioapic = false;
2446 * If we have map overrides on the kernel command line the
2447 * messages in this function might not describe firmware bugs
2448 * anymore - so be careful
2453 for (idx = 0; idx < nr_ioapics; idx++) {
2454 int devid, id = mpc_ioapic_id(idx);
2456 devid = get_ioapic_devid(id);
2458 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2461 } else if (devid == IOAPIC_SB_DEVID) {
2462 has_sb_ioapic = true;
2467 if (!has_sb_ioapic) {
2469 * We expect the SB IOAPIC to be listed in the IVRS
2470 * table. The system timer is connected to the SB IOAPIC
2471 * and if we don't have it in the list the system will
2472 * panic at boot time. This situation usually happens
2473 * when the BIOS is buggy and provides us the wrong
2474 * device id for the IOAPIC in the system.
2476 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2480 pr_err("Disabling interrupt remapping\n");
2485 static void __init free_dma_resources(void)
2487 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2488 get_order(MAX_DOMAIN_ID/8));
2489 amd_iommu_pd_alloc_bitmap = NULL;
2495 * This is the hardware init function for AMD IOMMU in the system.
2496 * This function is called either from amd_iommu_init or from the interrupt
2497 * remapping setup code.
2499 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2502 * 1 pass) Discover the most comprehensive IVHD type to use.
2504 * 2 pass) Find the highest PCI device id the driver has to handle.
2505 * Upon this information the size of the data structures is
2506 * determined that needs to be allocated.
2508 * 3 pass) Initialize the data structures just allocated with the
2509 * information in the ACPI table about available AMD IOMMUs
2510 * in the system. It also maps the PCI devices in the
2511 * system to specific IOMMUs
2513 * 4 pass) After the basic data structures are allocated and
2514 * initialized we update them with information about memory
2515 * remapping requirements parsed out of the ACPI table in
2518 * After everything is set up the IOMMUs are enabled and the necessary
2519 * hotplug and suspend notifiers are registered.
2521 static int __init early_amd_iommu_init(void)
2523 struct acpi_table_header *ivrs_base;
2525 int i, remap_cache_sz, ret = 0;
2527 if (!amd_iommu_detected)
2530 status = acpi_get_table("IVRS", 0, &ivrs_base);
2531 if (status == AE_NOT_FOUND)
2533 else if (ACPI_FAILURE(status)) {
2534 const char *err = acpi_format_exception(status);
2535 pr_err("IVRS table error: %s\n", err);
2540 * Validate checksum here so we don't need to do it when
2541 * we actually parse the table
2543 ret = check_ivrs_checksum(ivrs_base);
2547 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2548 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2551 * First parse ACPI tables to find the largest Bus/Dev/Func
2552 * we need to handle. Upon this information the shared data
2553 * structures for the IOMMUs in the system will be allocated
2555 ret = find_last_devid_acpi(ivrs_base);
2559 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2560 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2561 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
2563 /* Device table - directly used by all IOMMUs */
2565 amd_iommu_dev_table = (void *)__get_free_pages(
2566 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
2567 get_order(dev_table_size));
2568 if (amd_iommu_dev_table == NULL)
2572 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2573 * IOMMU see for that device
2575 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2576 get_order(alias_table_size));
2577 if (amd_iommu_alias_table == NULL)
2580 /* IOMMU rlookup table - find the IOMMU for a specific device */
2581 amd_iommu_rlookup_table = (void *)__get_free_pages(
2582 GFP_KERNEL | __GFP_ZERO,
2583 get_order(rlookup_table_size));
2584 if (amd_iommu_rlookup_table == NULL)
2587 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2588 GFP_KERNEL | __GFP_ZERO,
2589 get_order(MAX_DOMAIN_ID/8));
2590 if (amd_iommu_pd_alloc_bitmap == NULL)
2594 * let all alias entries point to itself
2596 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2597 amd_iommu_alias_table[i] = i;
2600 * never allocate domain 0 because its used as the non-allocated and
2601 * error value placeholder
2603 __set_bit(0, amd_iommu_pd_alloc_bitmap);
2606 * now the data structures are allocated and basically initialized
2607 * start the real acpi table scan
2609 ret = init_iommu_all(ivrs_base);
2613 /* Disable any previously enabled IOMMUs */
2614 if (!is_kdump_kernel() || amd_iommu_disabled)
2617 if (amd_iommu_irq_remap)
2618 amd_iommu_irq_remap = check_ioapic_information();
2620 if (amd_iommu_irq_remap) {
2622 * Interrupt remapping enabled, create kmem_cache for the
2626 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2627 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2629 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
2630 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
2632 IRQ_TABLE_ALIGNMENT,
2634 if (!amd_iommu_irq_cache)
2637 irq_lookup_table = (void *)__get_free_pages(
2638 GFP_KERNEL | __GFP_ZERO,
2639 get_order(rlookup_table_size));
2640 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2642 if (!irq_lookup_table)
2646 ret = init_memory_definitions(ivrs_base);
2650 /* init the device table */
2651 init_device_table();
2654 /* Don't leak any ACPI memory */
2655 acpi_put_table(ivrs_base);
2661 static int amd_iommu_enable_interrupts(void)
2663 struct amd_iommu *iommu;
2666 for_each_iommu(iommu) {
2667 ret = iommu_init_msi(iommu);
2676 static bool detect_ivrs(void)
2678 struct acpi_table_header *ivrs_base;
2681 status = acpi_get_table("IVRS", 0, &ivrs_base);
2682 if (status == AE_NOT_FOUND)
2684 else if (ACPI_FAILURE(status)) {
2685 const char *err = acpi_format_exception(status);
2686 pr_err("IVRS table error: %s\n", err);
2690 acpi_put_table(ivrs_base);
2692 /* Make sure ACS will be enabled during PCI probe */
2698 /****************************************************************************
2700 * AMD IOMMU Initialization State Machine
2702 ****************************************************************************/
2704 static int __init state_next(void)
2708 switch (init_state) {
2709 case IOMMU_START_STATE:
2710 if (!detect_ivrs()) {
2711 init_state = IOMMU_NOT_FOUND;
2714 init_state = IOMMU_IVRS_DETECTED;
2717 case IOMMU_IVRS_DETECTED:
2718 ret = early_amd_iommu_init();
2719 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2720 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2721 pr_info("AMD IOMMU disabled on kernel command-line\n");
2722 init_state = IOMMU_CMDLINE_DISABLED;
2726 case IOMMU_ACPI_FINISHED:
2727 early_enable_iommus();
2728 x86_platform.iommu_shutdown = disable_iommus;
2729 init_state = IOMMU_ENABLED;
2732 register_syscore_ops(&amd_iommu_syscore_ops);
2733 ret = amd_iommu_init_pci();
2734 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2737 case IOMMU_PCI_INIT:
2738 ret = amd_iommu_enable_interrupts();
2739 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2741 case IOMMU_INTERRUPTS_EN:
2742 ret = amd_iommu_init_dma_ops();
2743 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2746 init_state = IOMMU_INITIALIZED;
2748 case IOMMU_INITIALIZED:
2751 case IOMMU_NOT_FOUND:
2752 case IOMMU_INIT_ERROR:
2753 case IOMMU_CMDLINE_DISABLED:
2754 /* Error states => do nothing */
2763 free_dma_resources();
2764 if (!irq_remapping_enabled) {
2766 free_iommu_resources();
2768 struct amd_iommu *iommu;
2770 uninit_device_table_dma();
2771 for_each_iommu(iommu)
2772 iommu_flush_all_caches(iommu);
2778 static int __init iommu_go_to_state(enum iommu_init_state state)
2782 while (init_state != state) {
2783 if (init_state == IOMMU_NOT_FOUND ||
2784 init_state == IOMMU_INIT_ERROR ||
2785 init_state == IOMMU_CMDLINE_DISABLED)
2793 #ifdef CONFIG_IRQ_REMAP
2794 int __init amd_iommu_prepare(void)
2798 amd_iommu_irq_remap = true;
2800 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2803 return amd_iommu_irq_remap ? 0 : -ENODEV;
2806 int __init amd_iommu_enable(void)
2810 ret = iommu_go_to_state(IOMMU_ENABLED);
2814 irq_remapping_enabled = 1;
2815 return amd_iommu_xt_mode;
2818 void amd_iommu_disable(void)
2820 amd_iommu_suspend();
2823 int amd_iommu_reenable(int mode)
2830 int __init amd_iommu_enable_faulting(void)
2832 /* We enable MSI later when PCI is initialized */
2838 * This is the core init function for AMD IOMMU hardware in the system.
2839 * This function is called from the generic x86 DMA layer initialization
2842 static int __init amd_iommu_init(void)
2844 struct amd_iommu *iommu;
2847 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2848 #ifdef CONFIG_GART_IOMMU
2849 if (ret && list_empty(&amd_iommu_list)) {
2851 * We failed to initialize the AMD IOMMU - try fallback
2852 * to GART if possible.
2858 for_each_iommu(iommu)
2859 amd_iommu_debugfs_setup(iommu);
2864 static bool amd_iommu_sme_check(void)
2866 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
2869 /* For Fam17h, a specific level of support is required */
2870 if (boot_cpu_data.microcode >= 0x08001205)
2873 if ((boot_cpu_data.microcode >= 0x08001126) &&
2874 (boot_cpu_data.microcode <= 0x080011ff))
2877 pr_notice("IOMMU not currently supported when SME is active\n");
2882 /****************************************************************************
2884 * Early detect code. This code runs at IOMMU detection time in the DMA
2885 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2888 ****************************************************************************/
2889 int __init amd_iommu_detect(void)
2893 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
2896 if (!amd_iommu_sme_check())
2899 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2903 amd_iommu_detected = true;
2905 x86_init.iommu.iommu_init = amd_iommu_init;
2910 /****************************************************************************
2912 * Parsing functions for the AMD IOMMU specific kernel command line
2915 ****************************************************************************/
2917 static int __init parse_amd_iommu_dump(char *str)
2919 amd_iommu_dump = true;
2924 static int __init parse_amd_iommu_intr(char *str)
2926 for (; *str; ++str) {
2927 if (strncmp(str, "legacy", 6) == 0) {
2928 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2931 if (strncmp(str, "vapic", 5) == 0) {
2932 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2939 static int __init parse_amd_iommu_options(char *str)
2941 for (; *str; ++str) {
2942 if (strncmp(str, "fullflush", 9) == 0)
2943 amd_iommu_unmap_flush = true;
2944 if (strncmp(str, "off", 3) == 0)
2945 amd_iommu_disabled = true;
2946 if (strncmp(str, "force_isolation", 15) == 0)
2947 amd_iommu_force_isolation = true;
2953 static int __init parse_ivrs_ioapic(char *str)
2955 unsigned int bus, dev, fn;
2959 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2962 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
2966 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2967 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2972 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2974 cmdline_maps = true;
2975 i = early_ioapic_map_size++;
2976 early_ioapic_map[i].id = id;
2977 early_ioapic_map[i].devid = devid;
2978 early_ioapic_map[i].cmd_line = true;
2983 static int __init parse_ivrs_hpet(char *str)
2985 unsigned int bus, dev, fn;
2989 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2992 pr_err("Invalid command line: ivrs_hpet%s\n", str);
2996 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2997 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3002 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3004 cmdline_maps = true;
3005 i = early_hpet_map_size++;
3006 early_hpet_map[i].id = id;
3007 early_hpet_map[i].devid = devid;
3008 early_hpet_map[i].cmd_line = true;
3013 static int __init parse_ivrs_acpihid(char *str)
3016 char *hid, *uid, *p;
3017 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
3020 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
3022 pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);
3027 hid = strsep(&p, ":");
3030 if (!hid || !(*hid) || !uid) {
3031 pr_err("Invalid command line: hid or uid\n");
3035 i = early_acpihid_map_size++;
3036 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3037 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3038 early_acpihid_map[i].devid =
3039 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3040 early_acpihid_map[i].cmd_line = true;
3045 __setup("amd_iommu_dump", parse_amd_iommu_dump);
3046 __setup("amd_iommu=", parse_amd_iommu_options);
3047 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
3048 __setup("ivrs_ioapic", parse_ivrs_ioapic);
3049 __setup("ivrs_hpet", parse_ivrs_hpet);
3050 __setup("ivrs_acpihid", parse_ivrs_acpihid);
3052 IOMMU_INIT_FINISH(amd_iommu_detect,
3053 gart_iommu_hole_init,
3057 bool amd_iommu_v2_supported(void)
3059 return amd_iommu_v2_present;
3061 EXPORT_SYMBOL(amd_iommu_v2_supported);
3063 struct amd_iommu *get_amd_iommu(unsigned int idx)
3066 struct amd_iommu *iommu;
3068 for_each_iommu(iommu)
3073 EXPORT_SYMBOL(get_amd_iommu);
3075 /****************************************************************************
3077 * IOMMU EFR Performance Counter support functionality. This code allows
3078 * access to the IOMMU PC functionality.
3080 ****************************************************************************/
3082 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3084 struct amd_iommu *iommu = get_amd_iommu(idx);
3087 return iommu->max_banks;
3091 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3093 bool amd_iommu_pc_supported(void)
3095 return amd_iommu_pc_present;
3097 EXPORT_SYMBOL(amd_iommu_pc_supported);
3099 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3101 struct amd_iommu *iommu = get_amd_iommu(idx);
3104 return iommu->max_counters;
3108 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3110 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3111 u8 fxn, u64 *value, bool is_write)
3116 /* Make sure the IOMMU PC resource is available */
3117 if (!amd_iommu_pc_present)
3120 /* Check for valid iommu and pc register indexing */
3121 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3124 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3126 /* Limit the offset to the hw defined mmio region aperture */
3127 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3128 (iommu->max_counters << 8) | 0x28);
3129 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3130 (offset > max_offset_lim))
3134 u64 val = *value & GENMASK_ULL(47, 0);
3136 writel((u32)val, iommu->mmio_base + offset);
3137 writel((val >> 32), iommu->mmio_base + offset + 4);
3139 *value = readl(iommu->mmio_base + offset + 4);
3141 *value |= readl(iommu->mmio_base + offset);
3142 *value &= GENMASK_ULL(47, 0);
3148 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3153 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3155 EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3157 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3162 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3164 EXPORT_SYMBOL(amd_iommu_pc_set_reg);