2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <jroedel@suse.de>
4 * Leo Duran <leo.duran@amd.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define pr_fmt(fmt) "AMD-Vi: " fmt
22 #include <linux/pci.h>
23 #include <linux/acpi.h>
24 #include <linux/list.h>
25 #include <linux/bitmap.h>
26 #include <linux/slab.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/interrupt.h>
29 #include <linux/msi.h>
30 #include <linux/amd-iommu.h>
31 #include <linux/export.h>
32 #include <linux/iommu.h>
33 #include <linux/kmemleak.h>
34 #include <linux/mem_encrypt.h>
35 #include <asm/pci-direct.h>
36 #include <asm/iommu.h>
38 #include <asm/x86_init.h>
39 #include <asm/iommu_table.h>
40 #include <asm/io_apic.h>
41 #include <asm/irq_remapping.h>
43 #include <linux/crash_dump.h>
44 #include "amd_iommu_proto.h"
45 #include "amd_iommu_types.h"
46 #include "irq_remapping.h"
49 * definitions for the ACPI scanning code
51 #define IVRS_HEADER_LENGTH 48
53 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
54 #define ACPI_IVMD_TYPE_ALL 0x20
55 #define ACPI_IVMD_TYPE 0x21
56 #define ACPI_IVMD_TYPE_RANGE 0x22
58 #define IVHD_DEV_ALL 0x01
59 #define IVHD_DEV_SELECT 0x02
60 #define IVHD_DEV_SELECT_RANGE_START 0x03
61 #define IVHD_DEV_RANGE_END 0x04
62 #define IVHD_DEV_ALIAS 0x42
63 #define IVHD_DEV_ALIAS_RANGE 0x43
64 #define IVHD_DEV_EXT_SELECT 0x46
65 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
66 #define IVHD_DEV_SPECIAL 0x48
67 #define IVHD_DEV_ACPI_HID 0xf0
69 #define UID_NOT_PRESENT 0
70 #define UID_IS_INTEGER 1
71 #define UID_IS_CHARACTER 2
73 #define IVHD_SPECIAL_IOAPIC 1
74 #define IVHD_SPECIAL_HPET 2
76 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
77 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
78 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
79 #define IVHD_FLAG_ISOC_EN_MASK 0x08
81 #define IVMD_FLAG_EXCL_RANGE 0x08
82 #define IVMD_FLAG_UNITY_MAP 0x01
84 #define ACPI_DEVFLAG_INITPASS 0x01
85 #define ACPI_DEVFLAG_EXTINT 0x02
86 #define ACPI_DEVFLAG_NMI 0x04
87 #define ACPI_DEVFLAG_SYSMGT1 0x10
88 #define ACPI_DEVFLAG_SYSMGT2 0x20
89 #define ACPI_DEVFLAG_LINT0 0x40
90 #define ACPI_DEVFLAG_LINT1 0x80
91 #define ACPI_DEVFLAG_ATSDIS 0x10000000
93 #define LOOP_TIMEOUT 100000
95 * ACPI table definitions
97 * These data structures are laid over the table to parse the important values
101 extern const struct iommu_ops amd_iommu_ops;
104 * structure describing one IOMMU in the ACPI table. Typically followed by one
105 * or more ivhd_entrys.
118 /* Following only valid on IVHD type 11h and 40h */
119 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
121 } __attribute__((packed));
124 * A device entry describing which devices a specific IOMMU translates and
125 * which requestor ids they use.
137 } __attribute__((packed));
140 * An AMD IOMMU memory definition structure. It defines things like exclusion
141 * ranges for devices and regions that should be unity mapped.
152 } __attribute__((packed));
155 bool amd_iommu_irq_remap __read_mostly;
157 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
158 static int amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
160 static bool amd_iommu_detected;
161 static bool __initdata amd_iommu_disabled;
162 static int amd_iommu_target_ivhd_type;
164 u16 amd_iommu_last_bdf; /* largest PCI device id we have
166 LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
168 bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
170 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
173 /* Array to assign indices to IOMMUs*/
174 struct amd_iommu *amd_iommus[MAX_IOMMUS];
176 /* Number of IOMMUs present in the system */
177 static int amd_iommus_present;
179 /* IOMMUs have a non-present cache? */
180 bool amd_iommu_np_cache __read_mostly;
181 bool amd_iommu_iotlb_sup __read_mostly = true;
183 u32 amd_iommu_max_pasid __read_mostly = ~0;
185 bool amd_iommu_v2_present __read_mostly;
186 static bool amd_iommu_pc_present __read_mostly;
188 bool amd_iommu_force_isolation __read_mostly;
191 * List of protection domains - used during resume
193 LIST_HEAD(amd_iommu_pd_list);
194 spinlock_t amd_iommu_pd_lock;
197 * Pointer to the device table which is shared by all AMD IOMMUs
198 * it is indexed by the PCI device id or the HT unit id and contains
199 * information about the domain the device belongs to as well as the
200 * page table root pointer.
202 struct dev_table_entry *amd_iommu_dev_table;
204 * Pointer to a device table which the content of old device table
205 * will be copied to. It's only be used in kdump kernel.
207 static struct dev_table_entry *old_dev_tbl_cpy;
210 * The alias table is a driver specific data structure which contains the
211 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
212 * More than one device can share the same requestor id.
214 u16 *amd_iommu_alias_table;
217 * The rlookup table is used to find the IOMMU which is responsible
218 * for a specific device. It is also indexed by the PCI device id.
220 struct amd_iommu **amd_iommu_rlookup_table;
221 EXPORT_SYMBOL(amd_iommu_rlookup_table);
224 * This table is used to find the irq remapping table for a given device id
227 struct irq_remap_table **irq_lookup_table;
230 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
231 * to know which ones are already in use.
233 unsigned long *amd_iommu_pd_alloc_bitmap;
235 static u32 dev_table_size; /* size of the device table */
236 static u32 alias_table_size; /* size of the alias table */
237 static u32 rlookup_table_size; /* size if the rlookup table */
239 enum iommu_init_state {
250 IOMMU_CMDLINE_DISABLED,
253 /* Early ioapic and hpet maps from kernel command line */
254 #define EARLY_MAP_SIZE 4
255 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
256 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
257 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
259 static int __initdata early_ioapic_map_size;
260 static int __initdata early_hpet_map_size;
261 static int __initdata early_acpihid_map_size;
263 static bool __initdata cmdline_maps;
265 static enum iommu_init_state init_state = IOMMU_START_STATE;
267 static int amd_iommu_enable_interrupts(void);
268 static int __init iommu_go_to_state(enum iommu_init_state state);
269 static void init_device_table_dma(void);
271 static bool amd_iommu_pre_enabled = true;
273 bool translation_pre_enabled(struct amd_iommu *iommu)
275 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
277 EXPORT_SYMBOL(translation_pre_enabled);
279 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
281 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
284 static void init_translation_status(struct amd_iommu *iommu)
288 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
289 if (ctrl & (1<<CONTROL_IOMMU_EN))
290 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
293 static inline void update_last_devid(u16 devid)
295 if (devid > amd_iommu_last_bdf)
296 amd_iommu_last_bdf = devid;
299 static inline unsigned long tbl_size(int entry_size)
301 unsigned shift = PAGE_SHIFT +
302 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
307 int amd_iommu_get_num_iommus(void)
309 return amd_iommus_present;
312 /* Access to l1 and l2 indexed register spaces */
314 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
318 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
319 pci_read_config_dword(iommu->dev, 0xfc, &val);
323 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
325 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
326 pci_write_config_dword(iommu->dev, 0xfc, val);
327 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
330 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
334 pci_write_config_dword(iommu->dev, 0xf0, address);
335 pci_read_config_dword(iommu->dev, 0xf4, &val);
339 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
341 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
342 pci_write_config_dword(iommu->dev, 0xf4, val);
345 /****************************************************************************
347 * AMD IOMMU MMIO register space handling functions
349 * These functions are used to program the IOMMU device registers in
350 * MMIO space required for that driver.
352 ****************************************************************************/
355 * This function set the exclusion range in the IOMMU. DMA accesses to the
356 * exclusion range are passed through untranslated
358 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
360 u64 start = iommu->exclusion_start & PAGE_MASK;
361 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
364 if (!iommu->exclusion_start)
367 entry = start | MMIO_EXCL_ENABLE_MASK;
368 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
369 &entry, sizeof(entry));
372 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
373 &entry, sizeof(entry));
376 /* Programs the physical address of the device table into the IOMMU hardware */
377 static void iommu_set_device_table(struct amd_iommu *iommu)
381 BUG_ON(iommu->mmio_base == NULL);
383 entry = iommu_virt_to_phys(amd_iommu_dev_table);
384 entry |= (dev_table_size >> 12) - 1;
385 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
386 &entry, sizeof(entry));
389 /* Generic functions to enable/disable certain features of the IOMMU. */
390 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
394 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
395 ctrl |= (1ULL << bit);
396 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
399 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
403 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
404 ctrl &= ~(1ULL << bit);
405 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
408 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
412 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
413 ctrl &= ~CTRL_INV_TO_MASK;
414 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
415 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
418 /* Function to enable the hardware */
419 static void iommu_enable(struct amd_iommu *iommu)
421 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
424 static void iommu_disable(struct amd_iommu *iommu)
426 /* Disable command buffer */
427 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
429 /* Disable event logging and event interrupts */
430 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
431 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
433 /* Disable IOMMU GA_LOG */
434 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
435 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
437 /* Disable IOMMU hardware itself */
438 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
442 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
443 * the system has one.
445 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
447 if (!request_mem_region(address, end, "amd_iommu")) {
448 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
450 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
454 return (u8 __iomem *)ioremap_nocache(address, end);
457 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
459 if (iommu->mmio_base)
460 iounmap(iommu->mmio_base);
461 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
464 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
480 /****************************************************************************
482 * The functions below belong to the first pass of AMD IOMMU ACPI table
483 * parsing. In this pass we try to find out the highest device id this
484 * code has to handle. Upon this information the size of the shared data
485 * structures is determined later.
487 ****************************************************************************/
490 * This function calculates the length of a given IVHD entry
492 static inline int ivhd_entry_length(u8 *ivhd)
494 u32 type = ((struct ivhd_entry *)ivhd)->type;
497 return 0x04 << (*ivhd >> 6);
498 } else if (type == IVHD_DEV_ACPI_HID) {
499 /* For ACPI_HID, offset 21 is uid len */
500 return *((u8 *)ivhd + 21) + 22;
506 * After reading the highest device id from the IOMMU PCI capability header
507 * this function looks if there is a higher device id defined in the ACPI table
509 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
511 u8 *p = (void *)h, *end = (void *)h;
512 struct ivhd_entry *dev;
514 u32 ivhd_size = get_ivhd_header_size(h);
517 pr_err("Unsupported IVHD type %#x\n", h->type);
525 dev = (struct ivhd_entry *)p;
528 /* Use maximum BDF value for DEV_ALL */
529 update_last_devid(0xffff);
531 case IVHD_DEV_SELECT:
532 case IVHD_DEV_RANGE_END:
534 case IVHD_DEV_EXT_SELECT:
535 /* all the above subfield types refer to device ids */
536 update_last_devid(dev->devid);
541 p += ivhd_entry_length(p);
549 static int __init check_ivrs_checksum(struct acpi_table_header *table)
552 u8 checksum = 0, *p = (u8 *)table;
554 for (i = 0; i < table->length; ++i)
557 /* ACPI table corrupt */
558 pr_err(FW_BUG "IVRS invalid checksum\n");
566 * Iterate over all IVHD entries in the ACPI table and find the highest device
567 * id which we need to handle. This is the first of three functions which parse
568 * the ACPI table. So we check the checksum here.
570 static int __init find_last_devid_acpi(struct acpi_table_header *table)
572 u8 *p = (u8 *)table, *end = (u8 *)table;
573 struct ivhd_header *h;
575 p += IVRS_HEADER_LENGTH;
577 end += table->length;
579 h = (struct ivhd_header *)p;
580 if (h->type == amd_iommu_target_ivhd_type) {
581 int ret = find_last_devid_from_ivhd(h);
593 /****************************************************************************
595 * The following functions belong to the code path which parses the ACPI table
596 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
597 * data structures, initialize the device/alias/rlookup table and also
598 * basically initialize the hardware.
600 ****************************************************************************/
603 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
604 * write commands to that buffer later and the IOMMU will execute them
607 static int __init alloc_command_buffer(struct amd_iommu *iommu)
609 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
610 get_order(CMD_BUFFER_SIZE));
612 return iommu->cmd_buf ? 0 : -ENOMEM;
616 * This function resets the command buffer if the IOMMU stopped fetching
619 void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
621 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
623 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
624 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
625 iommu->cmd_buf_head = 0;
626 iommu->cmd_buf_tail = 0;
628 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
632 * This function writes the command buffer address to the hardware and
635 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
639 BUG_ON(iommu->cmd_buf == NULL);
641 entry = iommu_virt_to_phys(iommu->cmd_buf);
642 entry |= MMIO_CMD_SIZE_512;
644 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
645 &entry, sizeof(entry));
647 amd_iommu_reset_cmd_buffer(iommu);
651 * This function disables the command buffer
653 static void iommu_disable_command_buffer(struct amd_iommu *iommu)
655 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
658 static void __init free_command_buffer(struct amd_iommu *iommu)
660 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
663 /* allocates the memory where the IOMMU will log its events to */
664 static int __init alloc_event_buffer(struct amd_iommu *iommu)
666 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
667 get_order(EVT_BUFFER_SIZE));
669 return iommu->evt_buf ? 0 : -ENOMEM;
672 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
676 BUG_ON(iommu->evt_buf == NULL);
678 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
680 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
681 &entry, sizeof(entry));
683 /* set head and tail to zero manually */
684 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
685 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
687 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
691 * This function disables the event log buffer
693 static void iommu_disable_event_buffer(struct amd_iommu *iommu)
695 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
698 static void __init free_event_buffer(struct amd_iommu *iommu)
700 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
703 /* allocates the memory where the IOMMU will log its events to */
704 static int __init alloc_ppr_log(struct amd_iommu *iommu)
706 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
707 get_order(PPR_LOG_SIZE));
709 return iommu->ppr_log ? 0 : -ENOMEM;
712 static void iommu_enable_ppr_log(struct amd_iommu *iommu)
716 if (iommu->ppr_log == NULL)
719 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
721 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
722 &entry, sizeof(entry));
724 /* set head and tail to zero manually */
725 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
726 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
728 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
729 iommu_feature_enable(iommu, CONTROL_PPR_EN);
732 static void __init free_ppr_log(struct amd_iommu *iommu)
734 if (iommu->ppr_log == NULL)
737 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
740 static void free_ga_log(struct amd_iommu *iommu)
742 #ifdef CONFIG_IRQ_REMAP
744 free_pages((unsigned long)iommu->ga_log,
745 get_order(GA_LOG_SIZE));
746 if (iommu->ga_log_tail)
747 free_pages((unsigned long)iommu->ga_log_tail,
752 static int iommu_ga_log_enable(struct amd_iommu *iommu)
754 #ifdef CONFIG_IRQ_REMAP
760 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
762 /* Check if already running */
763 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
766 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
767 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
769 for (i = 0; i < LOOP_TIMEOUT; ++i) {
770 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
771 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
775 if (i >= LOOP_TIMEOUT)
777 #endif /* CONFIG_IRQ_REMAP */
781 #ifdef CONFIG_IRQ_REMAP
782 static int iommu_init_ga_log(struct amd_iommu *iommu)
786 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
789 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
790 get_order(GA_LOG_SIZE));
794 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
796 if (!iommu->ga_log_tail)
799 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
800 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
801 &entry, sizeof(entry));
802 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
803 (BIT_ULL(52)-1)) & ~7ULL;
804 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
805 &entry, sizeof(entry));
806 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
807 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
814 #endif /* CONFIG_IRQ_REMAP */
816 static int iommu_init_ga(struct amd_iommu *iommu)
820 #ifdef CONFIG_IRQ_REMAP
821 /* Note: We have already checked GASup from IVRS table.
822 * Now, we need to make sure that GAMSup is set.
824 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
825 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
826 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
828 ret = iommu_init_ga_log(iommu);
829 #endif /* CONFIG_IRQ_REMAP */
834 static void iommu_enable_xt(struct amd_iommu *iommu)
836 #ifdef CONFIG_IRQ_REMAP
838 * XT mode (32-bit APIC destination ID) requires
839 * GA mode (128-bit IRTE support) as a prerequisite.
841 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
842 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
843 iommu_feature_enable(iommu, CONTROL_XT_EN);
844 #endif /* CONFIG_IRQ_REMAP */
847 static void iommu_enable_gt(struct amd_iommu *iommu)
849 if (!iommu_feature(iommu, FEATURE_GT))
852 iommu_feature_enable(iommu, CONTROL_GT_EN);
855 /* sets a specific bit in the device table entry. */
856 static void set_dev_entry_bit(u16 devid, u8 bit)
858 int i = (bit >> 6) & 0x03;
859 int _bit = bit & 0x3f;
861 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
864 static int get_dev_entry_bit(u16 devid, u8 bit)
866 int i = (bit >> 6) & 0x03;
867 int _bit = bit & 0x3f;
869 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
873 static bool copy_device_table(void)
875 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
876 struct dev_table_entry *old_devtb = NULL;
877 u32 lo, hi, devid, old_devtb_size;
878 phys_addr_t old_devtb_phys;
879 struct amd_iommu *iommu;
880 u16 dom_id, dte_v, irq_v;
884 if (!amd_iommu_pre_enabled)
887 pr_warn("Translation is already enabled - trying to copy translation structures\n");
888 for_each_iommu(iommu) {
889 /* All IOMMUs should use the same device table with the same size */
890 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
891 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
892 entry = (((u64) hi) << 32) + lo;
893 if (last_entry && last_entry != entry) {
894 pr_err("IOMMU:%d should use the same dev table as others!\n",
900 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
901 if (old_devtb_size != dev_table_size) {
902 pr_err("The device table size of IOMMU:%d is not expected!\n",
909 * When SME is enabled in the first kernel, the entry includes the
910 * memory encryption mask(sme_me_mask), we must remove the memory
911 * encryption mask to obtain the true physical address in kdump kernel.
913 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
915 if (old_devtb_phys >= 0x100000000ULL) {
916 pr_err("The address of old device table is above 4G, not trustworthy!\n");
919 old_devtb = (sme_active() && is_kdump_kernel())
920 ? (__force void *)ioremap_encrypted(old_devtb_phys,
922 : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
927 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
928 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
929 get_order(dev_table_size));
930 if (old_dev_tbl_cpy == NULL) {
931 pr_err("Failed to allocate memory for copying old device table!\n");
935 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
936 old_dev_tbl_cpy[devid] = old_devtb[devid];
937 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
938 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
940 if (dte_v && dom_id) {
941 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
942 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
943 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
944 /* If gcr3 table existed, mask it out */
945 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
946 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
947 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
948 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
949 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
951 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
955 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
956 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
957 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
958 if (irq_v && (int_ctl || int_tab_len)) {
959 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
960 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
961 pr_err("Wrong old irq remapping flag: %#x\n", devid);
965 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
973 void amd_iommu_apply_erratum_63(u16 devid)
977 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
978 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
981 set_dev_entry_bit(devid, DEV_ENTRY_IW);
984 /* Writes the specific IOMMU for a device into the rlookup table */
985 static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
987 amd_iommu_rlookup_table[devid] = iommu;
991 * This function takes the device specific flags read from the ACPI
992 * table and sets up the device table entry with that information
994 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
995 u16 devid, u32 flags, u32 ext_flags)
997 if (flags & ACPI_DEVFLAG_INITPASS)
998 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
999 if (flags & ACPI_DEVFLAG_EXTINT)
1000 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
1001 if (flags & ACPI_DEVFLAG_NMI)
1002 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
1003 if (flags & ACPI_DEVFLAG_SYSMGT1)
1004 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
1005 if (flags & ACPI_DEVFLAG_SYSMGT2)
1006 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
1007 if (flags & ACPI_DEVFLAG_LINT0)
1008 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
1009 if (flags & ACPI_DEVFLAG_LINT1)
1010 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
1012 amd_iommu_apply_erratum_63(devid);
1014 set_iommu_for_device(iommu, devid);
1017 static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
1019 struct devid_map *entry;
1020 struct list_head *list;
1022 if (type == IVHD_SPECIAL_IOAPIC)
1024 else if (type == IVHD_SPECIAL_HPET)
1029 list_for_each_entry(entry, list, list) {
1030 if (!(entry->id == id && entry->cmd_line))
1033 pr_info("Command-line override present for %s id %d - ignoring\n",
1034 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1036 *devid = entry->devid;
1041 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1046 entry->devid = *devid;
1047 entry->cmd_line = cmd_line;
1049 list_add_tail(&entry->list, list);
1054 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1057 struct acpihid_map_entry *entry;
1058 struct list_head *list = &acpihid_map;
1060 list_for_each_entry(entry, list, list) {
1061 if (strcmp(entry->hid, hid) ||
1062 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1066 pr_info("Command-line override for hid:%s uid:%s\n",
1068 *devid = entry->devid;
1072 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1076 memcpy(entry->uid, uid, strlen(uid));
1077 memcpy(entry->hid, hid, strlen(hid));
1078 entry->devid = *devid;
1079 entry->cmd_line = cmd_line;
1080 entry->root_devid = (entry->devid & (~0x7));
1082 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1083 entry->cmd_line ? "cmd" : "ivrs",
1084 entry->hid, entry->uid, entry->root_devid);
1086 list_add_tail(&entry->list, list);
1090 static int __init add_early_maps(void)
1094 for (i = 0; i < early_ioapic_map_size; ++i) {
1095 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1096 early_ioapic_map[i].id,
1097 &early_ioapic_map[i].devid,
1098 early_ioapic_map[i].cmd_line);
1103 for (i = 0; i < early_hpet_map_size; ++i) {
1104 ret = add_special_device(IVHD_SPECIAL_HPET,
1105 early_hpet_map[i].id,
1106 &early_hpet_map[i].devid,
1107 early_hpet_map[i].cmd_line);
1112 for (i = 0; i < early_acpihid_map_size; ++i) {
1113 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1114 early_acpihid_map[i].uid,
1115 &early_acpihid_map[i].devid,
1116 early_acpihid_map[i].cmd_line);
1125 * Reads the device exclusion range from ACPI and initializes the IOMMU with
1128 static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1130 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1132 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1137 * We only can configure exclusion ranges per IOMMU, not
1138 * per device. But we can enable the exclusion range per
1139 * device. This is done here
1141 set_dev_entry_bit(devid, DEV_ENTRY_EX);
1142 iommu->exclusion_start = m->range_start;
1143 iommu->exclusion_length = m->range_length;
1148 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1149 * initializes the hardware and our data structures with it.
1151 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1152 struct ivhd_header *h)
1155 u8 *end = p, flags = 0;
1156 u16 devid = 0, devid_start = 0, devid_to = 0;
1157 u32 dev_i, ext_flags = 0;
1159 struct ivhd_entry *e;
1164 ret = add_early_maps();
1169 * First save the recommended feature enable bits from ACPI
1171 iommu->acpi_flags = h->flags;
1174 * Done. Now parse the device entries
1176 ivhd_size = get_ivhd_header_size(h);
1178 pr_err("Unsupported IVHD type %#x\n", h->type);
1188 e = (struct ivhd_entry *)p;
1192 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1194 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1195 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1197 case IVHD_DEV_SELECT:
1199 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1201 PCI_BUS_NUM(e->devid),
1207 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1209 case IVHD_DEV_SELECT_RANGE_START:
1211 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1212 "devid: %02x:%02x.%x flags: %02x\n",
1213 PCI_BUS_NUM(e->devid),
1218 devid_start = e->devid;
1223 case IVHD_DEV_ALIAS:
1225 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1226 "flags: %02x devid_to: %02x:%02x.%x\n",
1227 PCI_BUS_NUM(e->devid),
1231 PCI_BUS_NUM(e->ext >> 8),
1232 PCI_SLOT(e->ext >> 8),
1233 PCI_FUNC(e->ext >> 8));
1236 devid_to = e->ext >> 8;
1237 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1238 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1239 amd_iommu_alias_table[devid] = devid_to;
1241 case IVHD_DEV_ALIAS_RANGE:
1243 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1244 "devid: %02x:%02x.%x flags: %02x "
1245 "devid_to: %02x:%02x.%x\n",
1246 PCI_BUS_NUM(e->devid),
1250 PCI_BUS_NUM(e->ext >> 8),
1251 PCI_SLOT(e->ext >> 8),
1252 PCI_FUNC(e->ext >> 8));
1254 devid_start = e->devid;
1256 devid_to = e->ext >> 8;
1260 case IVHD_DEV_EXT_SELECT:
1262 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1263 "flags: %02x ext: %08x\n",
1264 PCI_BUS_NUM(e->devid),
1270 set_dev_entry_from_acpi(iommu, devid, e->flags,
1273 case IVHD_DEV_EXT_SELECT_RANGE:
1275 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1276 "%02x:%02x.%x flags: %02x ext: %08x\n",
1277 PCI_BUS_NUM(e->devid),
1282 devid_start = e->devid;
1287 case IVHD_DEV_RANGE_END:
1289 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1290 PCI_BUS_NUM(e->devid),
1292 PCI_FUNC(e->devid));
1295 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1297 amd_iommu_alias_table[dev_i] = devid_to;
1298 set_dev_entry_from_acpi(iommu,
1299 devid_to, flags, ext_flags);
1301 set_dev_entry_from_acpi(iommu, dev_i,
1305 case IVHD_DEV_SPECIAL: {
1311 handle = e->ext & 0xff;
1312 devid = (e->ext >> 8) & 0xffff;
1313 type = (e->ext >> 24) & 0xff;
1315 if (type == IVHD_SPECIAL_IOAPIC)
1317 else if (type == IVHD_SPECIAL_HPET)
1322 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1328 ret = add_special_device(type, handle, &devid, false);
1333 * add_special_device might update the devid in case a
1334 * command-line override is present. So call
1335 * set_dev_entry_from_acpi after add_special_device.
1337 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1341 case IVHD_DEV_ACPI_HID: {
1343 u8 hid[ACPIHID_HID_LEN] = {0};
1344 u8 uid[ACPIHID_UID_LEN] = {0};
1347 if (h->type != 0x40) {
1348 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1353 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1354 hid[ACPIHID_HID_LEN - 1] = '\0';
1357 pr_err(FW_BUG "Invalid HID.\n");
1362 case UID_NOT_PRESENT:
1365 pr_warn(FW_BUG "Invalid UID length.\n");
1368 case UID_IS_INTEGER:
1370 sprintf(uid, "%d", e->uid);
1373 case UID_IS_CHARACTER:
1375 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1376 uid[ACPIHID_UID_LEN - 1] = '\0';
1384 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1392 ret = add_acpi_hid_device(hid, uid, &devid, false);
1397 * add_special_device might update the devid in case a
1398 * command-line override is present. So call
1399 * set_dev_entry_from_acpi after add_special_device.
1401 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1409 p += ivhd_entry_length(p);
1415 static void __init free_iommu_one(struct amd_iommu *iommu)
1417 free_command_buffer(iommu);
1418 free_event_buffer(iommu);
1419 free_ppr_log(iommu);
1421 iommu_unmap_mmio_space(iommu);
1424 static void __init free_iommu_all(void)
1426 struct amd_iommu *iommu, *next;
1428 for_each_iommu_safe(iommu, next) {
1429 list_del(&iommu->list);
1430 free_iommu_one(iommu);
1436 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1438 * BIOS should disable L2B micellaneous clock gating by setting
1439 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1441 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1445 if ((boot_cpu_data.x86 != 0x15) ||
1446 (boot_cpu_data.x86_model < 0x10) ||
1447 (boot_cpu_data.x86_model > 0x1f))
1450 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1451 pci_read_config_dword(iommu->dev, 0xf4, &value);
1456 /* Select NB indirect register 0x90 and enable writing */
1457 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1459 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1460 pr_info("Applying erratum 746 workaround for IOMMU at %s\n",
1461 dev_name(&iommu->dev->dev));
1463 /* Clear the enable writing bit */
1464 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1468 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1470 * BIOS should enable ATS write permission check by setting
1471 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1473 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1477 if ((boot_cpu_data.x86 != 0x15) ||
1478 (boot_cpu_data.x86_model < 0x30) ||
1479 (boot_cpu_data.x86_model > 0x3f))
1482 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1483 value = iommu_read_l2(iommu, 0x47);
1488 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1489 iommu_write_l2(iommu, 0x47, value | BIT(0));
1491 pr_info("Applying ATS write check workaround for IOMMU at %s\n",
1492 dev_name(&iommu->dev->dev));
1496 * This function clues the initialization function for one IOMMU
1497 * together and also allocates the command buffer and programs the
1498 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1500 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1504 raw_spin_lock_init(&iommu->lock);
1506 /* Add IOMMU to internal data structures */
1507 list_add_tail(&iommu->list, &amd_iommu_list);
1508 iommu->index = amd_iommus_present++;
1510 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1511 WARN(1, "System has more IOMMUs than supported by this driver\n");
1515 /* Index is fine - add IOMMU to the array */
1516 amd_iommus[iommu->index] = iommu;
1519 * Copy data from ACPI table entry to the iommu struct
1521 iommu->devid = h->devid;
1522 iommu->cap_ptr = h->cap_ptr;
1523 iommu->pci_seg = h->pci_seg;
1524 iommu->mmio_phys = h->mmio_phys;
1528 /* Check if IVHD EFR contains proper max banks/counters */
1529 if ((h->efr_attr != 0) &&
1530 ((h->efr_attr & (0xF << 13)) != 0) &&
1531 ((h->efr_attr & (0x3F << 17)) != 0))
1532 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1534 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1535 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1536 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1537 if (((h->efr_attr & (0x1 << IOMMU_FEAT_XTSUP_SHIFT)) == 0))
1538 amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
1542 if (h->efr_reg & (1 << 9))
1543 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1545 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1546 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1547 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1548 if (((h->efr_reg & (0x1 << IOMMU_EFR_XTSUP_SHIFT)) == 0))
1549 amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
1555 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1556 iommu->mmio_phys_end);
1557 if (!iommu->mmio_base)
1560 if (alloc_command_buffer(iommu))
1563 if (alloc_event_buffer(iommu))
1566 iommu->int_enabled = false;
1568 init_translation_status(iommu);
1569 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1570 iommu_disable(iommu);
1571 clear_translation_pre_enabled(iommu);
1572 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1575 if (amd_iommu_pre_enabled)
1576 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1578 ret = init_iommu_from_acpi(iommu, h);
1582 ret = amd_iommu_create_irq_domain(iommu);
1587 * Make sure IOMMU is not considered to translate itself. The IVRS
1588 * table tells us so, but this is a lie!
1590 amd_iommu_rlookup_table[iommu->devid] = NULL;
1596 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1597 * @ivrs Pointer to the IVRS header
1599 * This function search through all IVDB of the maximum supported IVHD
1601 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1603 u8 *base = (u8 *)ivrs;
1604 struct ivhd_header *ivhd = (struct ivhd_header *)
1605 (base + IVRS_HEADER_LENGTH);
1606 u8 last_type = ivhd->type;
1607 u16 devid = ivhd->devid;
1609 while (((u8 *)ivhd - base < ivrs->length) &&
1610 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1611 u8 *p = (u8 *) ivhd;
1613 if (ivhd->devid == devid)
1614 last_type = ivhd->type;
1615 ivhd = (struct ivhd_header *)(p + ivhd->length);
1622 * Iterates over all IOMMU entries in the ACPI table, allocates the
1623 * IOMMU structure and initializes it with init_iommu_one()
1625 static int __init init_iommu_all(struct acpi_table_header *table)
1627 u8 *p = (u8 *)table, *end = (u8 *)table;
1628 struct ivhd_header *h;
1629 struct amd_iommu *iommu;
1632 end += table->length;
1633 p += IVRS_HEADER_LENGTH;
1636 h = (struct ivhd_header *)p;
1637 if (*p == amd_iommu_target_ivhd_type) {
1639 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1640 "seg: %d flags: %01x info %04x\n",
1641 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
1642 PCI_FUNC(h->devid), h->cap_ptr,
1643 h->pci_seg, h->flags, h->info);
1644 DUMP_printk(" mmio-addr: %016llx\n",
1647 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1651 ret = init_iommu_one(iommu, h);
1663 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1664 u8 fxn, u64 *value, bool is_write);
1666 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1668 u64 val = 0xabcd, val2 = 0;
1670 if (!iommu_feature(iommu, FEATURE_PC))
1673 amd_iommu_pc_present = true;
1675 /* Check if the performance counters can be written to */
1676 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1677 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
1679 pr_err("Unable to write to IOMMU perf counter.\n");
1680 amd_iommu_pc_present = false;
1684 pr_info("IOMMU performance counters supported\n");
1686 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1687 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1688 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1691 static ssize_t amd_iommu_show_cap(struct device *dev,
1692 struct device_attribute *attr,
1695 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1696 return sprintf(buf, "%x\n", iommu->cap);
1698 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1700 static ssize_t amd_iommu_show_features(struct device *dev,
1701 struct device_attribute *attr,
1704 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1705 return sprintf(buf, "%llx\n", iommu->features);
1707 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1709 static struct attribute *amd_iommu_attrs[] = {
1711 &dev_attr_features.attr,
1715 static struct attribute_group amd_iommu_group = {
1716 .name = "amd-iommu",
1717 .attrs = amd_iommu_attrs,
1720 static const struct attribute_group *amd_iommu_groups[] = {
1725 static int __init iommu_init_pci(struct amd_iommu *iommu)
1727 int cap_ptr = iommu->cap_ptr;
1728 u32 range, misc, low, high;
1731 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1732 iommu->devid & 0xff);
1736 /* Prevent binding other PCI device drivers to IOMMU devices */
1737 iommu->dev->match_driver = false;
1739 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1741 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1743 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1746 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1747 amd_iommu_iotlb_sup = false;
1749 /* read extended feature bits */
1750 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1751 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1753 iommu->features = ((u64)high << 32) | low;
1755 if (iommu_feature(iommu, FEATURE_GT)) {
1760 pasmax = iommu->features & FEATURE_PASID_MASK;
1761 pasmax >>= FEATURE_PASID_SHIFT;
1762 max_pasid = (1 << (pasmax + 1)) - 1;
1764 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1766 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
1768 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1769 glxval >>= FEATURE_GLXVAL_SHIFT;
1771 if (amd_iommu_max_glx_val == -1)
1772 amd_iommu_max_glx_val = glxval;
1774 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1777 if (iommu_feature(iommu, FEATURE_GT) &&
1778 iommu_feature(iommu, FEATURE_PPR)) {
1779 iommu->is_iommu_v2 = true;
1780 amd_iommu_v2_present = true;
1783 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1786 ret = iommu_init_ga(iommu);
1790 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1791 amd_iommu_np_cache = true;
1793 init_iommu_perf_ctr(iommu);
1795 if (is_rd890_iommu(iommu->dev)) {
1799 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1803 * Some rd890 systems may not be fully reconfigured by the
1804 * BIOS, so it's necessary for us to store this information so
1805 * it can be reprogrammed on resume
1807 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1808 &iommu->stored_addr_lo);
1809 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1810 &iommu->stored_addr_hi);
1812 /* Low bit locks writes to configuration space */
1813 iommu->stored_addr_lo &= ~1;
1815 for (i = 0; i < 6; i++)
1816 for (j = 0; j < 0x12; j++)
1817 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1819 for (i = 0; i < 0x83; i++)
1820 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1823 amd_iommu_erratum_746_workaround(iommu);
1824 amd_iommu_ats_write_check_workaround(iommu);
1826 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1827 amd_iommu_groups, "ivhd%d", iommu->index);
1828 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1829 iommu_device_register(&iommu->iommu);
1831 return pci_enable_device(iommu->dev);
1834 static void print_iommu_info(void)
1836 static const char * const feat_str[] = {
1837 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1838 "IA", "GA", "HE", "PC"
1840 struct amd_iommu *iommu;
1842 for_each_iommu(iommu) {
1845 pr_info("Found IOMMU at %s cap 0x%hx\n",
1846 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1848 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1849 pr_info("Extended features (%#llx):\n",
1851 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
1852 if (iommu_feature(iommu, (1ULL << i)))
1853 pr_cont(" %s", feat_str[i]);
1856 if (iommu->features & FEATURE_GAM_VAPIC)
1857 pr_cont(" GA_vAPIC");
1862 if (irq_remapping_enabled) {
1863 pr_info("Interrupt remapping enabled\n");
1864 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1865 pr_info("Virtual APIC enabled\n");
1866 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1867 pr_info("X2APIC enabled\n");
1871 static int __init amd_iommu_init_pci(void)
1873 struct amd_iommu *iommu;
1876 for_each_iommu(iommu) {
1877 ret = iommu_init_pci(iommu);
1883 * Order is important here to make sure any unity map requirements are
1884 * fulfilled. The unity mappings are created and written to the device
1885 * table during the amd_iommu_init_api() call.
1887 * After that we call init_device_table_dma() to make sure any
1888 * uninitialized DTE will block DMA, and in the end we flush the caches
1889 * of all IOMMUs to make sure the changes to the device table are
1892 ret = amd_iommu_init_api();
1894 init_device_table_dma();
1896 for_each_iommu(iommu)
1897 iommu_flush_all_caches(iommu);
1905 /****************************************************************************
1907 * The following functions initialize the MSI interrupts for all IOMMUs
1908 * in the system. It's a bit challenging because there could be multiple
1909 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1912 ****************************************************************************/
1914 static int iommu_setup_msi(struct amd_iommu *iommu)
1918 r = pci_enable_msi(iommu->dev);
1922 r = request_threaded_irq(iommu->dev->irq,
1923 amd_iommu_int_handler,
1924 amd_iommu_int_thread,
1929 pci_disable_msi(iommu->dev);
1933 iommu->int_enabled = true;
1938 static int iommu_init_msi(struct amd_iommu *iommu)
1942 if (iommu->int_enabled)
1945 if (iommu->dev->msi_cap)
1946 ret = iommu_setup_msi(iommu);
1954 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1956 if (iommu->ppr_log != NULL)
1957 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1959 iommu_ga_log_enable(iommu);
1964 /****************************************************************************
1966 * The next functions belong to the third pass of parsing the ACPI
1967 * table. In this last pass the memory mapping requirements are
1968 * gathered (like exclusion and unity mapping ranges).
1970 ****************************************************************************/
1972 static void __init free_unity_maps(void)
1974 struct unity_map_entry *entry, *next;
1976 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1977 list_del(&entry->list);
1982 /* called when we find an exclusion range definition in ACPI */
1983 static int __init init_exclusion_range(struct ivmd_header *m)
1988 case ACPI_IVMD_TYPE:
1989 set_device_exclusion_range(m->devid, m);
1991 case ACPI_IVMD_TYPE_ALL:
1992 for (i = 0; i <= amd_iommu_last_bdf; ++i)
1993 set_device_exclusion_range(i, m);
1995 case ACPI_IVMD_TYPE_RANGE:
1996 for (i = m->devid; i <= m->aux; ++i)
1997 set_device_exclusion_range(i, m);
2006 /* called for unity map ACPI definition */
2007 static int __init init_unity_map_range(struct ivmd_header *m)
2009 struct unity_map_entry *e = NULL;
2012 e = kzalloc(sizeof(*e), GFP_KERNEL);
2020 case ACPI_IVMD_TYPE:
2021 s = "IVMD_TYPEi\t\t\t";
2022 e->devid_start = e->devid_end = m->devid;
2024 case ACPI_IVMD_TYPE_ALL:
2025 s = "IVMD_TYPE_ALL\t\t";
2027 e->devid_end = amd_iommu_last_bdf;
2029 case ACPI_IVMD_TYPE_RANGE:
2030 s = "IVMD_TYPE_RANGE\t\t";
2031 e->devid_start = m->devid;
2032 e->devid_end = m->aux;
2035 e->address_start = PAGE_ALIGN(m->range_start);
2036 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2037 e->prot = m->flags >> 1;
2039 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2040 " range_start: %016llx range_end: %016llx flags: %x\n", s,
2041 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2042 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
2043 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2044 e->address_start, e->address_end, m->flags);
2046 list_add_tail(&e->list, &amd_iommu_unity_map);
2051 /* iterates over all memory definitions we find in the ACPI table */
2052 static int __init init_memory_definitions(struct acpi_table_header *table)
2054 u8 *p = (u8 *)table, *end = (u8 *)table;
2055 struct ivmd_header *m;
2057 end += table->length;
2058 p += IVRS_HEADER_LENGTH;
2061 m = (struct ivmd_header *)p;
2062 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2063 init_exclusion_range(m);
2064 else if (m->flags & IVMD_FLAG_UNITY_MAP)
2065 init_unity_map_range(m);
2074 * Init the device table to not allow DMA access for devices
2076 static void init_device_table_dma(void)
2080 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2081 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2082 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
2086 static void __init uninit_device_table_dma(void)
2090 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2091 amd_iommu_dev_table[devid].data[0] = 0ULL;
2092 amd_iommu_dev_table[devid].data[1] = 0ULL;
2096 static void init_device_table(void)
2100 if (!amd_iommu_irq_remap)
2103 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2104 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2107 static void iommu_init_flags(struct amd_iommu *iommu)
2109 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2110 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2111 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2113 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2114 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2115 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2117 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2118 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2119 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2121 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2122 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2123 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2126 * make IOMMU memory accesses cache coherent
2128 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2130 /* Set IOTLB invalidation timeout to 1s */
2131 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2134 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2137 u32 ioc_feature_control;
2138 struct pci_dev *pdev = iommu->root_pdev;
2140 /* RD890 BIOSes may not have completely reconfigured the iommu */
2141 if (!is_rd890_iommu(iommu->dev) || !pdev)
2145 * First, we need to ensure that the iommu is enabled. This is
2146 * controlled by a register in the northbridge
2149 /* Select Northbridge indirect register 0x75 and enable writing */
2150 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2151 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2153 /* Enable the iommu */
2154 if (!(ioc_feature_control & 0x1))
2155 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2157 /* Restore the iommu BAR */
2158 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2159 iommu->stored_addr_lo);
2160 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2161 iommu->stored_addr_hi);
2163 /* Restore the l1 indirect regs for each of the 6 l1s */
2164 for (i = 0; i < 6; i++)
2165 for (j = 0; j < 0x12; j++)
2166 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2168 /* Restore the l2 indirect regs */
2169 for (i = 0; i < 0x83; i++)
2170 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2172 /* Lock PCI setup registers */
2173 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2174 iommu->stored_addr_lo | 1);
2177 static void iommu_enable_ga(struct amd_iommu *iommu)
2179 #ifdef CONFIG_IRQ_REMAP
2180 switch (amd_iommu_guest_ir) {
2181 case AMD_IOMMU_GUEST_IR_VAPIC:
2182 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2184 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2185 iommu_feature_enable(iommu, CONTROL_GA_EN);
2186 iommu->irte_ops = &irte_128_ops;
2189 iommu->irte_ops = &irte_32_ops;
2195 static void early_enable_iommu(struct amd_iommu *iommu)
2197 iommu_disable(iommu);
2198 iommu_init_flags(iommu);
2199 iommu_set_device_table(iommu);
2200 iommu_enable_command_buffer(iommu);
2201 iommu_enable_event_buffer(iommu);
2202 iommu_set_exclusion_range(iommu);
2203 iommu_enable_ga(iommu);
2204 iommu_enable_xt(iommu);
2205 iommu_enable(iommu);
2206 iommu_flush_all_caches(iommu);
2210 * This function finally enables all IOMMUs found in the system after
2211 * they have been initialized.
2213 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2214 * the old content of device table entries. Not this case or copy failed,
2215 * just continue as normal kernel does.
2217 static void early_enable_iommus(void)
2219 struct amd_iommu *iommu;
2222 if (!copy_device_table()) {
2224 * If come here because of failure in copying device table from old
2225 * kernel with all IOMMUs enabled, print error message and try to
2226 * free allocated old_dev_tbl_cpy.
2228 if (amd_iommu_pre_enabled)
2229 pr_err("Failed to copy DEV table from previous kernel.\n");
2230 if (old_dev_tbl_cpy != NULL)
2231 free_pages((unsigned long)old_dev_tbl_cpy,
2232 get_order(dev_table_size));
2234 for_each_iommu(iommu) {
2235 clear_translation_pre_enabled(iommu);
2236 early_enable_iommu(iommu);
2239 pr_info("Copied DEV table from previous kernel.\n");
2240 free_pages((unsigned long)amd_iommu_dev_table,
2241 get_order(dev_table_size));
2242 amd_iommu_dev_table = old_dev_tbl_cpy;
2243 for_each_iommu(iommu) {
2244 iommu_disable_command_buffer(iommu);
2245 iommu_disable_event_buffer(iommu);
2246 iommu_enable_command_buffer(iommu);
2247 iommu_enable_event_buffer(iommu);
2248 iommu_enable_ga(iommu);
2249 iommu_enable_xt(iommu);
2250 iommu_set_device_table(iommu);
2251 iommu_flush_all_caches(iommu);
2255 #ifdef CONFIG_IRQ_REMAP
2256 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2257 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2261 static void enable_iommus_v2(void)
2263 struct amd_iommu *iommu;
2265 for_each_iommu(iommu) {
2266 iommu_enable_ppr_log(iommu);
2267 iommu_enable_gt(iommu);
2271 static void enable_iommus(void)
2273 early_enable_iommus();
2278 static void disable_iommus(void)
2280 struct amd_iommu *iommu;
2282 for_each_iommu(iommu)
2283 iommu_disable(iommu);
2285 #ifdef CONFIG_IRQ_REMAP
2286 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2287 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2292 * Suspend/Resume support
2293 * disable suspend until real resume implemented
2296 static void amd_iommu_resume(void)
2298 struct amd_iommu *iommu;
2300 for_each_iommu(iommu)
2301 iommu_apply_resume_quirks(iommu);
2303 /* re-load the hardware */
2306 amd_iommu_enable_interrupts();
2309 static int amd_iommu_suspend(void)
2311 /* disable IOMMUs to go out of the way for BIOS */
2317 static struct syscore_ops amd_iommu_syscore_ops = {
2318 .suspend = amd_iommu_suspend,
2319 .resume = amd_iommu_resume,
2322 static void __init free_iommu_resources(void)
2324 kmemleak_free(irq_lookup_table);
2325 free_pages((unsigned long)irq_lookup_table,
2326 get_order(rlookup_table_size));
2327 irq_lookup_table = NULL;
2329 kmem_cache_destroy(amd_iommu_irq_cache);
2330 amd_iommu_irq_cache = NULL;
2332 free_pages((unsigned long)amd_iommu_rlookup_table,
2333 get_order(rlookup_table_size));
2334 amd_iommu_rlookup_table = NULL;
2336 free_pages((unsigned long)amd_iommu_alias_table,
2337 get_order(alias_table_size));
2338 amd_iommu_alias_table = NULL;
2340 free_pages((unsigned long)amd_iommu_dev_table,
2341 get_order(dev_table_size));
2342 amd_iommu_dev_table = NULL;
2346 #ifdef CONFIG_GART_IOMMU
2348 * We failed to initialize the AMD IOMMU - try fallback to GART
2356 /* SB IOAPIC is always on this device in AMD systems */
2357 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2359 static bool __init check_ioapic_information(void)
2361 const char *fw_bug = FW_BUG;
2362 bool ret, has_sb_ioapic;
2365 has_sb_ioapic = false;
2369 * If we have map overrides on the kernel command line the
2370 * messages in this function might not describe firmware bugs
2371 * anymore - so be careful
2376 for (idx = 0; idx < nr_ioapics; idx++) {
2377 int devid, id = mpc_ioapic_id(idx);
2379 devid = get_ioapic_devid(id);
2381 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2384 } else if (devid == IOAPIC_SB_DEVID) {
2385 has_sb_ioapic = true;
2390 if (!has_sb_ioapic) {
2392 * We expect the SB IOAPIC to be listed in the IVRS
2393 * table. The system timer is connected to the SB IOAPIC
2394 * and if we don't have it in the list the system will
2395 * panic at boot time. This situation usually happens
2396 * when the BIOS is buggy and provides us the wrong
2397 * device id for the IOAPIC in the system.
2399 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2403 pr_err("Disabling interrupt remapping\n");
2408 static void __init free_dma_resources(void)
2410 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2411 get_order(MAX_DOMAIN_ID/8));
2412 amd_iommu_pd_alloc_bitmap = NULL;
2418 * This is the hardware init function for AMD IOMMU in the system.
2419 * This function is called either from amd_iommu_init or from the interrupt
2420 * remapping setup code.
2422 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2425 * 1 pass) Discover the most comprehensive IVHD type to use.
2427 * 2 pass) Find the highest PCI device id the driver has to handle.
2428 * Upon this information the size of the data structures is
2429 * determined that needs to be allocated.
2431 * 3 pass) Initialize the data structures just allocated with the
2432 * information in the ACPI table about available AMD IOMMUs
2433 * in the system. It also maps the PCI devices in the
2434 * system to specific IOMMUs
2436 * 4 pass) After the basic data structures are allocated and
2437 * initialized we update them with information about memory
2438 * remapping requirements parsed out of the ACPI table in
2441 * After everything is set up the IOMMUs are enabled and the necessary
2442 * hotplug and suspend notifiers are registered.
2444 static int __init early_amd_iommu_init(void)
2446 struct acpi_table_header *ivrs_base;
2448 int i, remap_cache_sz, ret = 0;
2450 if (!amd_iommu_detected)
2453 status = acpi_get_table("IVRS", 0, &ivrs_base);
2454 if (status == AE_NOT_FOUND)
2456 else if (ACPI_FAILURE(status)) {
2457 const char *err = acpi_format_exception(status);
2458 pr_err("IVRS table error: %s\n", err);
2463 * Validate checksum here so we don't need to do it when
2464 * we actually parse the table
2466 ret = check_ivrs_checksum(ivrs_base);
2470 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2471 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2474 * First parse ACPI tables to find the largest Bus/Dev/Func
2475 * we need to handle. Upon this information the shared data
2476 * structures for the IOMMUs in the system will be allocated
2478 ret = find_last_devid_acpi(ivrs_base);
2482 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2483 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2484 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
2486 /* Device table - directly used by all IOMMUs */
2488 amd_iommu_dev_table = (void *)__get_free_pages(
2489 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
2490 get_order(dev_table_size));
2491 if (amd_iommu_dev_table == NULL)
2495 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2496 * IOMMU see for that device
2498 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2499 get_order(alias_table_size));
2500 if (amd_iommu_alias_table == NULL)
2503 /* IOMMU rlookup table - find the IOMMU for a specific device */
2504 amd_iommu_rlookup_table = (void *)__get_free_pages(
2505 GFP_KERNEL | __GFP_ZERO,
2506 get_order(rlookup_table_size));
2507 if (amd_iommu_rlookup_table == NULL)
2510 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2511 GFP_KERNEL | __GFP_ZERO,
2512 get_order(MAX_DOMAIN_ID/8));
2513 if (amd_iommu_pd_alloc_bitmap == NULL)
2517 * let all alias entries point to itself
2519 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2520 amd_iommu_alias_table[i] = i;
2523 * never allocate domain 0 because its used as the non-allocated and
2524 * error value placeholder
2526 __set_bit(0, amd_iommu_pd_alloc_bitmap);
2528 spin_lock_init(&amd_iommu_pd_lock);
2531 * now the data structures are allocated and basically initialized
2532 * start the real acpi table scan
2534 ret = init_iommu_all(ivrs_base);
2538 /* Disable any previously enabled IOMMUs */
2539 if (!is_kdump_kernel() || amd_iommu_disabled)
2542 if (amd_iommu_irq_remap)
2543 amd_iommu_irq_remap = check_ioapic_information();
2545 if (amd_iommu_irq_remap) {
2547 * Interrupt remapping enabled, create kmem_cache for the
2551 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2552 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2554 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
2555 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
2557 IRQ_TABLE_ALIGNMENT,
2559 if (!amd_iommu_irq_cache)
2562 irq_lookup_table = (void *)__get_free_pages(
2563 GFP_KERNEL | __GFP_ZERO,
2564 get_order(rlookup_table_size));
2565 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2567 if (!irq_lookup_table)
2571 ret = init_memory_definitions(ivrs_base);
2575 /* init the device table */
2576 init_device_table();
2579 /* Don't leak any ACPI memory */
2580 acpi_put_table(ivrs_base);
2586 static int amd_iommu_enable_interrupts(void)
2588 struct amd_iommu *iommu;
2591 for_each_iommu(iommu) {
2592 ret = iommu_init_msi(iommu);
2601 static bool detect_ivrs(void)
2603 struct acpi_table_header *ivrs_base;
2606 status = acpi_get_table("IVRS", 0, &ivrs_base);
2607 if (status == AE_NOT_FOUND)
2609 else if (ACPI_FAILURE(status)) {
2610 const char *err = acpi_format_exception(status);
2611 pr_err("IVRS table error: %s\n", err);
2615 acpi_put_table(ivrs_base);
2617 /* Make sure ACS will be enabled during PCI probe */
2623 /****************************************************************************
2625 * AMD IOMMU Initialization State Machine
2627 ****************************************************************************/
2629 static int __init state_next(void)
2633 switch (init_state) {
2634 case IOMMU_START_STATE:
2635 if (!detect_ivrs()) {
2636 init_state = IOMMU_NOT_FOUND;
2639 init_state = IOMMU_IVRS_DETECTED;
2642 case IOMMU_IVRS_DETECTED:
2643 ret = early_amd_iommu_init();
2644 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2645 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2646 pr_info("AMD IOMMU disabled on kernel command-line\n");
2647 free_dma_resources();
2648 free_iommu_resources();
2649 init_state = IOMMU_CMDLINE_DISABLED;
2653 case IOMMU_ACPI_FINISHED:
2654 early_enable_iommus();
2655 x86_platform.iommu_shutdown = disable_iommus;
2656 init_state = IOMMU_ENABLED;
2659 register_syscore_ops(&amd_iommu_syscore_ops);
2660 ret = amd_iommu_init_pci();
2661 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2664 case IOMMU_PCI_INIT:
2665 ret = amd_iommu_enable_interrupts();
2666 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2668 case IOMMU_INTERRUPTS_EN:
2669 ret = amd_iommu_init_dma_ops();
2670 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2673 init_state = IOMMU_INITIALIZED;
2675 case IOMMU_INITIALIZED:
2678 case IOMMU_NOT_FOUND:
2679 case IOMMU_INIT_ERROR:
2680 case IOMMU_CMDLINE_DISABLED:
2681 /* Error states => do nothing */
2692 static int __init iommu_go_to_state(enum iommu_init_state state)
2696 while (init_state != state) {
2697 if (init_state == IOMMU_NOT_FOUND ||
2698 init_state == IOMMU_INIT_ERROR ||
2699 init_state == IOMMU_CMDLINE_DISABLED)
2707 #ifdef CONFIG_IRQ_REMAP
2708 int __init amd_iommu_prepare(void)
2712 amd_iommu_irq_remap = true;
2714 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2717 return amd_iommu_irq_remap ? 0 : -ENODEV;
2720 int __init amd_iommu_enable(void)
2724 ret = iommu_go_to_state(IOMMU_ENABLED);
2728 irq_remapping_enabled = 1;
2729 return amd_iommu_xt_mode;
2732 void amd_iommu_disable(void)
2734 amd_iommu_suspend();
2737 int amd_iommu_reenable(int mode)
2744 int __init amd_iommu_enable_faulting(void)
2746 /* We enable MSI later when PCI is initialized */
2752 * This is the core init function for AMD IOMMU hardware in the system.
2753 * This function is called from the generic x86 DMA layer initialization
2756 static int __init amd_iommu_init(void)
2758 struct amd_iommu *iommu;
2761 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2763 free_dma_resources();
2764 if (!irq_remapping_enabled) {
2766 free_iommu_resources();
2768 uninit_device_table_dma();
2769 for_each_iommu(iommu)
2770 iommu_flush_all_caches(iommu);
2774 for_each_iommu(iommu)
2775 amd_iommu_debugfs_setup(iommu);
2780 static bool amd_iommu_sme_check(void)
2782 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
2785 /* For Fam17h, a specific level of support is required */
2786 if (boot_cpu_data.microcode >= 0x08001205)
2789 if ((boot_cpu_data.microcode >= 0x08001126) &&
2790 (boot_cpu_data.microcode <= 0x080011ff))
2793 pr_notice("IOMMU not currently supported when SME is active\n");
2798 /****************************************************************************
2800 * Early detect code. This code runs at IOMMU detection time in the DMA
2801 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2804 ****************************************************************************/
2805 int __init amd_iommu_detect(void)
2809 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
2812 if (!amd_iommu_sme_check())
2815 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2819 amd_iommu_detected = true;
2821 x86_init.iommu.iommu_init = amd_iommu_init;
2826 /****************************************************************************
2828 * Parsing functions for the AMD IOMMU specific kernel command line
2831 ****************************************************************************/
2833 static int __init parse_amd_iommu_dump(char *str)
2835 amd_iommu_dump = true;
2840 static int __init parse_amd_iommu_intr(char *str)
2842 for (; *str; ++str) {
2843 if (strncmp(str, "legacy", 6) == 0) {
2844 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2847 if (strncmp(str, "vapic", 5) == 0) {
2848 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2855 static int __init parse_amd_iommu_options(char *str)
2857 for (; *str; ++str) {
2858 if (strncmp(str, "fullflush", 9) == 0)
2859 amd_iommu_unmap_flush = true;
2860 if (strncmp(str, "off", 3) == 0)
2861 amd_iommu_disabled = true;
2862 if (strncmp(str, "force_isolation", 15) == 0)
2863 amd_iommu_force_isolation = true;
2869 static int __init parse_ivrs_ioapic(char *str)
2871 unsigned int bus, dev, fn;
2875 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2878 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
2882 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2883 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2888 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2890 cmdline_maps = true;
2891 i = early_ioapic_map_size++;
2892 early_ioapic_map[i].id = id;
2893 early_ioapic_map[i].devid = devid;
2894 early_ioapic_map[i].cmd_line = true;
2899 static int __init parse_ivrs_hpet(char *str)
2901 unsigned int bus, dev, fn;
2905 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2908 pr_err("Invalid command line: ivrs_hpet%s\n", str);
2912 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2913 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
2918 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2920 cmdline_maps = true;
2921 i = early_hpet_map_size++;
2922 early_hpet_map[i].id = id;
2923 early_hpet_map[i].devid = devid;
2924 early_hpet_map[i].cmd_line = true;
2929 static int __init parse_ivrs_acpihid(char *str)
2932 char *hid, *uid, *p;
2933 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2936 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2938 pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);
2943 hid = strsep(&p, ":");
2946 if (!hid || !(*hid) || !uid) {
2947 pr_err("Invalid command line: hid or uid\n");
2951 i = early_acpihid_map_size++;
2952 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2953 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2954 early_acpihid_map[i].devid =
2955 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2956 early_acpihid_map[i].cmd_line = true;
2961 __setup("amd_iommu_dump", parse_amd_iommu_dump);
2962 __setup("amd_iommu=", parse_amd_iommu_options);
2963 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
2964 __setup("ivrs_ioapic", parse_ivrs_ioapic);
2965 __setup("ivrs_hpet", parse_ivrs_hpet);
2966 __setup("ivrs_acpihid", parse_ivrs_acpihid);
2968 IOMMU_INIT_FINISH(amd_iommu_detect,
2969 gart_iommu_hole_init,
2973 bool amd_iommu_v2_supported(void)
2975 return amd_iommu_v2_present;
2977 EXPORT_SYMBOL(amd_iommu_v2_supported);
2979 struct amd_iommu *get_amd_iommu(unsigned int idx)
2982 struct amd_iommu *iommu;
2984 for_each_iommu(iommu)
2989 EXPORT_SYMBOL(get_amd_iommu);
2991 /****************************************************************************
2993 * IOMMU EFR Performance Counter support functionality. This code allows
2994 * access to the IOMMU PC functionality.
2996 ****************************************************************************/
2998 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3000 struct amd_iommu *iommu = get_amd_iommu(idx);
3003 return iommu->max_banks;
3007 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3009 bool amd_iommu_pc_supported(void)
3011 return amd_iommu_pc_present;
3013 EXPORT_SYMBOL(amd_iommu_pc_supported);
3015 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3017 struct amd_iommu *iommu = get_amd_iommu(idx);
3020 return iommu->max_counters;
3024 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3026 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3027 u8 fxn, u64 *value, bool is_write)
3032 /* Make sure the IOMMU PC resource is available */
3033 if (!amd_iommu_pc_present)
3036 /* Check for valid iommu and pc register indexing */
3037 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3040 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3042 /* Limit the offset to the hw defined mmio region aperture */
3043 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3044 (iommu->max_counters << 8) | 0x28);
3045 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3046 (offset > max_offset_lim))
3050 u64 val = *value & GENMASK_ULL(47, 0);
3052 writel((u32)val, iommu->mmio_base + offset);
3053 writel((val >> 32), iommu->mmio_base + offset + 4);
3055 *value = readl(iommu->mmio_base + offset + 4);
3057 *value |= readl(iommu->mmio_base + offset);
3058 *value &= GENMASK_ULL(47, 0);
3064 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3069 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3071 EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3073 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3078 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3080 EXPORT_SYMBOL(amd_iommu_pc_set_reg);