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 #include <linux/pci.h>
21 #include <linux/acpi.h>
22 #include <linux/list.h>
23 #include <linux/bitmap.h>
24 #include <linux/slab.h>
25 #include <linux/syscore_ops.h>
26 #include <linux/interrupt.h>
27 #include <linux/msi.h>
28 #include <linux/amd-iommu.h>
29 #include <linux/export.h>
30 #include <linux/iommu.h>
31 #include <linux/kmemleak.h>
32 #include <asm/pci-direct.h>
33 #include <asm/iommu.h>
35 #include <asm/x86_init.h>
36 #include <asm/iommu_table.h>
37 #include <asm/io_apic.h>
38 #include <asm/irq_remapping.h>
40 #include "amd_iommu_proto.h"
41 #include "amd_iommu_types.h"
42 #include "irq_remapping.h"
45 * definitions for the ACPI scanning code
47 #define IVRS_HEADER_LENGTH 48
49 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
50 #define ACPI_IVMD_TYPE_ALL 0x20
51 #define ACPI_IVMD_TYPE 0x21
52 #define ACPI_IVMD_TYPE_RANGE 0x22
54 #define IVHD_DEV_ALL 0x01
55 #define IVHD_DEV_SELECT 0x02
56 #define IVHD_DEV_SELECT_RANGE_START 0x03
57 #define IVHD_DEV_RANGE_END 0x04
58 #define IVHD_DEV_ALIAS 0x42
59 #define IVHD_DEV_ALIAS_RANGE 0x43
60 #define IVHD_DEV_EXT_SELECT 0x46
61 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
62 #define IVHD_DEV_SPECIAL 0x48
63 #define IVHD_DEV_ACPI_HID 0xf0
65 #define UID_NOT_PRESENT 0
66 #define UID_IS_INTEGER 1
67 #define UID_IS_CHARACTER 2
69 #define IVHD_SPECIAL_IOAPIC 1
70 #define IVHD_SPECIAL_HPET 2
72 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
73 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
74 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
75 #define IVHD_FLAG_ISOC_EN_MASK 0x08
77 #define IVMD_FLAG_EXCL_RANGE 0x08
78 #define IVMD_FLAG_UNITY_MAP 0x01
80 #define ACPI_DEVFLAG_INITPASS 0x01
81 #define ACPI_DEVFLAG_EXTINT 0x02
82 #define ACPI_DEVFLAG_NMI 0x04
83 #define ACPI_DEVFLAG_SYSMGT1 0x10
84 #define ACPI_DEVFLAG_SYSMGT2 0x20
85 #define ACPI_DEVFLAG_LINT0 0x40
86 #define ACPI_DEVFLAG_LINT1 0x80
87 #define ACPI_DEVFLAG_ATSDIS 0x10000000
89 #define LOOP_TIMEOUT 100000
91 * ACPI table definitions
93 * These data structures are laid over the table to parse the important values
97 extern const struct iommu_ops amd_iommu_ops;
100 * structure describing one IOMMU in the ACPI table. Typically followed by one
101 * or more ivhd_entrys.
114 /* Following only valid on IVHD type 11h and 40h */
115 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
117 } __attribute__((packed));
120 * A device entry describing which devices a specific IOMMU translates and
121 * which requestor ids they use.
133 } __attribute__((packed));
136 * An AMD IOMMU memory definition structure. It defines things like exclusion
137 * ranges for devices and regions that should be unity mapped.
148 } __attribute__((packed));
151 bool amd_iommu_irq_remap __read_mostly;
153 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
155 static bool amd_iommu_detected;
156 static bool __initdata amd_iommu_disabled;
157 static int amd_iommu_target_ivhd_type;
159 u16 amd_iommu_last_bdf; /* largest PCI device id we have
161 LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
163 bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
165 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
168 /* Array to assign indices to IOMMUs*/
169 struct amd_iommu *amd_iommus[MAX_IOMMUS];
171 /* Number of IOMMUs present in the system */
172 static int amd_iommus_present;
174 /* IOMMUs have a non-present cache? */
175 bool amd_iommu_np_cache __read_mostly;
176 bool amd_iommu_iotlb_sup __read_mostly = true;
178 u32 amd_iommu_max_pasid __read_mostly = ~0;
180 bool amd_iommu_v2_present __read_mostly;
181 static bool amd_iommu_pc_present __read_mostly;
183 bool amd_iommu_force_isolation __read_mostly;
186 * List of protection domains - used during resume
188 LIST_HEAD(amd_iommu_pd_list);
189 spinlock_t amd_iommu_pd_lock;
192 * Pointer to the device table which is shared by all AMD IOMMUs
193 * it is indexed by the PCI device id or the HT unit id and contains
194 * information about the domain the device belongs to as well as the
195 * page table root pointer.
197 struct dev_table_entry *amd_iommu_dev_table;
199 * Pointer to a device table which the content of old device table
200 * will be copied to. It's only be used in kdump kernel.
202 static struct dev_table_entry *old_dev_tbl_cpy;
205 * The alias table is a driver specific data structure which contains the
206 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
207 * More than one device can share the same requestor id.
209 u16 *amd_iommu_alias_table;
212 * The rlookup table is used to find the IOMMU which is responsible
213 * for a specific device. It is also indexed by the PCI device id.
215 struct amd_iommu **amd_iommu_rlookup_table;
218 * This table is used to find the irq remapping table for a given device id
221 struct irq_remap_table **irq_lookup_table;
224 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
225 * to know which ones are already in use.
227 unsigned long *amd_iommu_pd_alloc_bitmap;
229 static u32 dev_table_size; /* size of the device table */
230 static u32 alias_table_size; /* size of the alias table */
231 static u32 rlookup_table_size; /* size if the rlookup table */
233 enum iommu_init_state {
244 IOMMU_CMDLINE_DISABLED,
247 /* Early ioapic and hpet maps from kernel command line */
248 #define EARLY_MAP_SIZE 4
249 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
250 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
251 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
253 static int __initdata early_ioapic_map_size;
254 static int __initdata early_hpet_map_size;
255 static int __initdata early_acpihid_map_size;
257 static bool __initdata cmdline_maps;
259 static enum iommu_init_state init_state = IOMMU_START_STATE;
261 static int amd_iommu_enable_interrupts(void);
262 static int __init iommu_go_to_state(enum iommu_init_state state);
263 static void init_device_table_dma(void);
265 bool translation_pre_enabled(struct amd_iommu *iommu)
267 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
270 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
272 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
275 static void init_translation_status(struct amd_iommu *iommu)
279 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
280 if (ctrl & (1<<CONTROL_IOMMU_EN))
281 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
284 static inline void update_last_devid(u16 devid)
286 if (devid > amd_iommu_last_bdf)
287 amd_iommu_last_bdf = devid;
290 static inline unsigned long tbl_size(int entry_size)
292 unsigned shift = PAGE_SHIFT +
293 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
298 int amd_iommu_get_num_iommus(void)
300 return amd_iommus_present;
303 /* Access to l1 and l2 indexed register spaces */
305 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
309 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
310 pci_read_config_dword(iommu->dev, 0xfc, &val);
314 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
316 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
317 pci_write_config_dword(iommu->dev, 0xfc, val);
318 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
321 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
325 pci_write_config_dword(iommu->dev, 0xf0, address);
326 pci_read_config_dword(iommu->dev, 0xf4, &val);
330 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
332 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
333 pci_write_config_dword(iommu->dev, 0xf4, val);
336 /****************************************************************************
338 * AMD IOMMU MMIO register space handling functions
340 * These functions are used to program the IOMMU device registers in
341 * MMIO space required for that driver.
343 ****************************************************************************/
346 * This function set the exclusion range in the IOMMU. DMA accesses to the
347 * exclusion range are passed through untranslated
349 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
351 u64 start = iommu->exclusion_start & PAGE_MASK;
352 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
355 if (!iommu->exclusion_start)
358 entry = start | MMIO_EXCL_ENABLE_MASK;
359 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
360 &entry, sizeof(entry));
363 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
364 &entry, sizeof(entry));
367 /* Programs the physical address of the device table into the IOMMU hardware */
368 static void iommu_set_device_table(struct amd_iommu *iommu)
372 BUG_ON(iommu->mmio_base == NULL);
374 entry = virt_to_phys(amd_iommu_dev_table);
375 entry |= (dev_table_size >> 12) - 1;
376 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
377 &entry, sizeof(entry));
380 /* Generic functions to enable/disable certain features of the IOMMU. */
381 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
385 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
387 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
390 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
394 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
396 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
399 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
403 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
404 ctrl &= ~CTRL_INV_TO_MASK;
405 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
406 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
409 /* Function to enable the hardware */
410 static void iommu_enable(struct amd_iommu *iommu)
412 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
415 static void iommu_disable(struct amd_iommu *iommu)
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("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
441 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
445 return (u8 __iomem *)ioremap_nocache(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("AMD-Vi: 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 "AMD-Vi: 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 = (u64)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 = (u64)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 = (u64)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_PPFLOG_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 = (u64)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 = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
794 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
795 &entry, sizeof(entry));
796 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
797 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
804 #endif /* CONFIG_IRQ_REMAP */
806 static int iommu_init_ga(struct amd_iommu *iommu)
810 #ifdef CONFIG_IRQ_REMAP
811 /* Note: We have already checked GASup from IVRS table.
812 * Now, we need to make sure that GAMSup is set.
814 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
815 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
816 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
818 ret = iommu_init_ga_log(iommu);
819 #endif /* CONFIG_IRQ_REMAP */
824 static void iommu_enable_gt(struct amd_iommu *iommu)
826 if (!iommu_feature(iommu, FEATURE_GT))
829 iommu_feature_enable(iommu, CONTROL_GT_EN);
832 /* sets a specific bit in the device table entry. */
833 static void set_dev_entry_bit(u16 devid, u8 bit)
835 int i = (bit >> 6) & 0x03;
836 int _bit = bit & 0x3f;
838 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
841 static int get_dev_entry_bit(u16 devid, u8 bit)
843 int i = (bit >> 6) & 0x03;
844 int _bit = bit & 0x3f;
846 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
850 static bool copy_device_table(void)
852 struct dev_table_entry *old_devtb = NULL;
853 u32 lo, hi, devid, old_devtb_size;
854 phys_addr_t old_devtb_phys;
855 u64 entry, last_entry = 0;
856 struct amd_iommu *iommu;
861 pr_warn("Translation is already enabled - trying to copy translation structures\n");
862 for_each_iommu(iommu) {
863 /* All IOMMUs should use the same device table with the same size */
864 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
865 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
866 entry = (((u64) hi) << 32) + lo;
867 if (last_entry && last_entry != entry) {
868 pr_err("IOMMU:%d should use the same dev table as others!/n",
874 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
875 if (old_devtb_size != dev_table_size) {
876 pr_err("The device table size of IOMMU:%d is not expected!/n",
882 old_devtb_phys = entry & PAGE_MASK;
883 old_devtb = memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
887 gfp_flag = GFP_KERNEL | __GFP_ZERO;
888 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
889 get_order(dev_table_size));
890 if (old_dev_tbl_cpy == NULL) {
891 pr_err("Failed to allocate memory for copying old device table!/n");
895 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
896 old_dev_tbl_cpy[devid] = old_devtb[devid];
897 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
898 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
900 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
907 void amd_iommu_apply_erratum_63(u16 devid)
911 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
912 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
915 set_dev_entry_bit(devid, DEV_ENTRY_IW);
918 /* Writes the specific IOMMU for a device into the rlookup table */
919 static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
921 amd_iommu_rlookup_table[devid] = iommu;
925 * This function takes the device specific flags read from the ACPI
926 * table and sets up the device table entry with that information
928 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
929 u16 devid, u32 flags, u32 ext_flags)
931 if (flags & ACPI_DEVFLAG_INITPASS)
932 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
933 if (flags & ACPI_DEVFLAG_EXTINT)
934 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
935 if (flags & ACPI_DEVFLAG_NMI)
936 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
937 if (flags & ACPI_DEVFLAG_SYSMGT1)
938 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
939 if (flags & ACPI_DEVFLAG_SYSMGT2)
940 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
941 if (flags & ACPI_DEVFLAG_LINT0)
942 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
943 if (flags & ACPI_DEVFLAG_LINT1)
944 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
946 amd_iommu_apply_erratum_63(devid);
948 set_iommu_for_device(iommu, devid);
951 static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
953 struct devid_map *entry;
954 struct list_head *list;
956 if (type == IVHD_SPECIAL_IOAPIC)
958 else if (type == IVHD_SPECIAL_HPET)
963 list_for_each_entry(entry, list, list) {
964 if (!(entry->id == id && entry->cmd_line))
967 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
968 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
970 *devid = entry->devid;
975 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
980 entry->devid = *devid;
981 entry->cmd_line = cmd_line;
983 list_add_tail(&entry->list, list);
988 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
991 struct acpihid_map_entry *entry;
992 struct list_head *list = &acpihid_map;
994 list_for_each_entry(entry, list, list) {
995 if (strcmp(entry->hid, hid) ||
996 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1000 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
1002 *devid = entry->devid;
1006 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1010 memcpy(entry->uid, uid, strlen(uid));
1011 memcpy(entry->hid, hid, strlen(hid));
1012 entry->devid = *devid;
1013 entry->cmd_line = cmd_line;
1014 entry->root_devid = (entry->devid & (~0x7));
1016 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
1017 entry->cmd_line ? "cmd" : "ivrs",
1018 entry->hid, entry->uid, entry->root_devid);
1020 list_add_tail(&entry->list, list);
1024 static int __init add_early_maps(void)
1028 for (i = 0; i < early_ioapic_map_size; ++i) {
1029 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1030 early_ioapic_map[i].id,
1031 &early_ioapic_map[i].devid,
1032 early_ioapic_map[i].cmd_line);
1037 for (i = 0; i < early_hpet_map_size; ++i) {
1038 ret = add_special_device(IVHD_SPECIAL_HPET,
1039 early_hpet_map[i].id,
1040 &early_hpet_map[i].devid,
1041 early_hpet_map[i].cmd_line);
1046 for (i = 0; i < early_acpihid_map_size; ++i) {
1047 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1048 early_acpihid_map[i].uid,
1049 &early_acpihid_map[i].devid,
1050 early_acpihid_map[i].cmd_line);
1059 * Reads the device exclusion range from ACPI and initializes the IOMMU with
1062 static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1064 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1066 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1071 * We only can configure exclusion ranges per IOMMU, not
1072 * per device. But we can enable the exclusion range per
1073 * device. This is done here
1075 set_dev_entry_bit(devid, DEV_ENTRY_EX);
1076 iommu->exclusion_start = m->range_start;
1077 iommu->exclusion_length = m->range_length;
1082 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1083 * initializes the hardware and our data structures with it.
1085 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1086 struct ivhd_header *h)
1089 u8 *end = p, flags = 0;
1090 u16 devid = 0, devid_start = 0, devid_to = 0;
1091 u32 dev_i, ext_flags = 0;
1093 struct ivhd_entry *e;
1098 ret = add_early_maps();
1103 * First save the recommended feature enable bits from ACPI
1105 iommu->acpi_flags = h->flags;
1108 * Done. Now parse the device entries
1110 ivhd_size = get_ivhd_header_size(h);
1112 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1122 e = (struct ivhd_entry *)p;
1126 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1128 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1129 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1131 case IVHD_DEV_SELECT:
1133 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1135 PCI_BUS_NUM(e->devid),
1141 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1143 case IVHD_DEV_SELECT_RANGE_START:
1145 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1146 "devid: %02x:%02x.%x flags: %02x\n",
1147 PCI_BUS_NUM(e->devid),
1152 devid_start = e->devid;
1157 case IVHD_DEV_ALIAS:
1159 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1160 "flags: %02x devid_to: %02x:%02x.%x\n",
1161 PCI_BUS_NUM(e->devid),
1165 PCI_BUS_NUM(e->ext >> 8),
1166 PCI_SLOT(e->ext >> 8),
1167 PCI_FUNC(e->ext >> 8));
1170 devid_to = e->ext >> 8;
1171 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1172 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1173 amd_iommu_alias_table[devid] = devid_to;
1175 case IVHD_DEV_ALIAS_RANGE:
1177 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1178 "devid: %02x:%02x.%x flags: %02x "
1179 "devid_to: %02x:%02x.%x\n",
1180 PCI_BUS_NUM(e->devid),
1184 PCI_BUS_NUM(e->ext >> 8),
1185 PCI_SLOT(e->ext >> 8),
1186 PCI_FUNC(e->ext >> 8));
1188 devid_start = e->devid;
1190 devid_to = e->ext >> 8;
1194 case IVHD_DEV_EXT_SELECT:
1196 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1197 "flags: %02x ext: %08x\n",
1198 PCI_BUS_NUM(e->devid),
1204 set_dev_entry_from_acpi(iommu, devid, e->flags,
1207 case IVHD_DEV_EXT_SELECT_RANGE:
1209 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1210 "%02x:%02x.%x flags: %02x ext: %08x\n",
1211 PCI_BUS_NUM(e->devid),
1216 devid_start = e->devid;
1221 case IVHD_DEV_RANGE_END:
1223 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1224 PCI_BUS_NUM(e->devid),
1226 PCI_FUNC(e->devid));
1229 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1231 amd_iommu_alias_table[dev_i] = devid_to;
1232 set_dev_entry_from_acpi(iommu,
1233 devid_to, flags, ext_flags);
1235 set_dev_entry_from_acpi(iommu, dev_i,
1239 case IVHD_DEV_SPECIAL: {
1245 handle = e->ext & 0xff;
1246 devid = (e->ext >> 8) & 0xffff;
1247 type = (e->ext >> 24) & 0xff;
1249 if (type == IVHD_SPECIAL_IOAPIC)
1251 else if (type == IVHD_SPECIAL_HPET)
1256 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1262 ret = add_special_device(type, handle, &devid, false);
1267 * add_special_device might update the devid in case a
1268 * command-line override is present. So call
1269 * set_dev_entry_from_acpi after add_special_device.
1271 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1275 case IVHD_DEV_ACPI_HID: {
1277 u8 hid[ACPIHID_HID_LEN] = {0};
1278 u8 uid[ACPIHID_UID_LEN] = {0};
1281 if (h->type != 0x40) {
1282 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1287 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1288 hid[ACPIHID_HID_LEN - 1] = '\0';
1291 pr_err(FW_BUG "Invalid HID.\n");
1296 case UID_NOT_PRESENT:
1299 pr_warn(FW_BUG "Invalid UID length.\n");
1302 case UID_IS_INTEGER:
1304 sprintf(uid, "%d", e->uid);
1307 case UID_IS_CHARACTER:
1309 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1310 uid[ACPIHID_UID_LEN - 1] = '\0';
1318 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1326 ret = add_acpi_hid_device(hid, uid, &devid, false);
1331 * add_special_device might update the devid in case a
1332 * command-line override is present. So call
1333 * set_dev_entry_from_acpi after add_special_device.
1335 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1343 p += ivhd_entry_length(p);
1349 static void __init free_iommu_one(struct amd_iommu *iommu)
1351 free_command_buffer(iommu);
1352 free_event_buffer(iommu);
1353 free_ppr_log(iommu);
1355 iommu_unmap_mmio_space(iommu);
1358 static void __init free_iommu_all(void)
1360 struct amd_iommu *iommu, *next;
1362 for_each_iommu_safe(iommu, next) {
1363 list_del(&iommu->list);
1364 free_iommu_one(iommu);
1370 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1372 * BIOS should disable L2B micellaneous clock gating by setting
1373 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1375 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1379 if ((boot_cpu_data.x86 != 0x15) ||
1380 (boot_cpu_data.x86_model < 0x10) ||
1381 (boot_cpu_data.x86_model > 0x1f))
1384 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1385 pci_read_config_dword(iommu->dev, 0xf4, &value);
1390 /* Select NB indirect register 0x90 and enable writing */
1391 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1393 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1394 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1395 dev_name(&iommu->dev->dev));
1397 /* Clear the enable writing bit */
1398 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1402 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1404 * BIOS should enable ATS write permission check by setting
1405 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1407 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1411 if ((boot_cpu_data.x86 != 0x15) ||
1412 (boot_cpu_data.x86_model < 0x30) ||
1413 (boot_cpu_data.x86_model > 0x3f))
1416 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1417 value = iommu_read_l2(iommu, 0x47);
1422 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1423 iommu_write_l2(iommu, 0x47, value | BIT(0));
1425 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1426 dev_name(&iommu->dev->dev));
1430 * This function clues the initialization function for one IOMMU
1431 * together and also allocates the command buffer and programs the
1432 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1434 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1438 spin_lock_init(&iommu->lock);
1440 /* Add IOMMU to internal data structures */
1441 list_add_tail(&iommu->list, &amd_iommu_list);
1442 iommu->index = amd_iommus_present++;
1444 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1445 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1449 /* Index is fine - add IOMMU to the array */
1450 amd_iommus[iommu->index] = iommu;
1453 * Copy data from ACPI table entry to the iommu struct
1455 iommu->devid = h->devid;
1456 iommu->cap_ptr = h->cap_ptr;
1457 iommu->pci_seg = h->pci_seg;
1458 iommu->mmio_phys = h->mmio_phys;
1462 /* Check if IVHD EFR contains proper max banks/counters */
1463 if ((h->efr_attr != 0) &&
1464 ((h->efr_attr & (0xF << 13)) != 0) &&
1465 ((h->efr_attr & (0x3F << 17)) != 0))
1466 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1468 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1469 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1470 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1474 if (h->efr_reg & (1 << 9))
1475 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1477 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1478 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1479 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1485 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1486 iommu->mmio_phys_end);
1487 if (!iommu->mmio_base)
1490 if (alloc_command_buffer(iommu))
1493 if (alloc_event_buffer(iommu))
1496 iommu->int_enabled = false;
1498 init_translation_status(iommu);
1500 if (translation_pre_enabled(iommu))
1501 pr_warn("Translation is already enabled - trying to copy translation structures\n");
1503 ret = init_iommu_from_acpi(iommu, h);
1507 ret = amd_iommu_create_irq_domain(iommu);
1512 * Make sure IOMMU is not considered to translate itself. The IVRS
1513 * table tells us so, but this is a lie!
1515 amd_iommu_rlookup_table[iommu->devid] = NULL;
1521 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1522 * @ivrs Pointer to the IVRS header
1524 * This function search through all IVDB of the maximum supported IVHD
1526 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1528 u8 *base = (u8 *)ivrs;
1529 struct ivhd_header *ivhd = (struct ivhd_header *)
1530 (base + IVRS_HEADER_LENGTH);
1531 u8 last_type = ivhd->type;
1532 u16 devid = ivhd->devid;
1534 while (((u8 *)ivhd - base < ivrs->length) &&
1535 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1536 u8 *p = (u8 *) ivhd;
1538 if (ivhd->devid == devid)
1539 last_type = ivhd->type;
1540 ivhd = (struct ivhd_header *)(p + ivhd->length);
1547 * Iterates over all IOMMU entries in the ACPI table, allocates the
1548 * IOMMU structure and initializes it with init_iommu_one()
1550 static int __init init_iommu_all(struct acpi_table_header *table)
1552 u8 *p = (u8 *)table, *end = (u8 *)table;
1553 struct ivhd_header *h;
1554 struct amd_iommu *iommu;
1557 end += table->length;
1558 p += IVRS_HEADER_LENGTH;
1561 h = (struct ivhd_header *)p;
1562 if (*p == amd_iommu_target_ivhd_type) {
1564 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1565 "seg: %d flags: %01x info %04x\n",
1566 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
1567 PCI_FUNC(h->devid), h->cap_ptr,
1568 h->pci_seg, h->flags, h->info);
1569 DUMP_printk(" mmio-addr: %016llx\n",
1572 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1576 ret = init_iommu_one(iommu, h);
1588 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1589 u8 fxn, u64 *value, bool is_write);
1591 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1593 u64 val = 0xabcd, val2 = 0;
1595 if (!iommu_feature(iommu, FEATURE_PC))
1598 amd_iommu_pc_present = true;
1600 /* Check if the performance counters can be written to */
1601 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1602 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
1604 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1605 amd_iommu_pc_present = false;
1609 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1611 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1612 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1613 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1616 static ssize_t amd_iommu_show_cap(struct device *dev,
1617 struct device_attribute *attr,
1620 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1621 return sprintf(buf, "%x\n", iommu->cap);
1623 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1625 static ssize_t amd_iommu_show_features(struct device *dev,
1626 struct device_attribute *attr,
1629 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1630 return sprintf(buf, "%llx\n", iommu->features);
1632 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1634 static struct attribute *amd_iommu_attrs[] = {
1636 &dev_attr_features.attr,
1640 static struct attribute_group amd_iommu_group = {
1641 .name = "amd-iommu",
1642 .attrs = amd_iommu_attrs,
1645 static const struct attribute_group *amd_iommu_groups[] = {
1650 static int iommu_init_pci(struct amd_iommu *iommu)
1652 int cap_ptr = iommu->cap_ptr;
1653 u32 range, misc, low, high;
1656 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
1657 iommu->devid & 0xff);
1661 /* Prevent binding other PCI device drivers to IOMMU devices */
1662 iommu->dev->match_driver = false;
1664 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1666 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1668 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1671 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1672 amd_iommu_iotlb_sup = false;
1674 /* read extended feature bits */
1675 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1676 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1678 iommu->features = ((u64)high << 32) | low;
1680 if (iommu_feature(iommu, FEATURE_GT)) {
1685 pasmax = iommu->features & FEATURE_PASID_MASK;
1686 pasmax >>= FEATURE_PASID_SHIFT;
1687 max_pasid = (1 << (pasmax + 1)) - 1;
1689 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1691 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
1693 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1694 glxval >>= FEATURE_GLXVAL_SHIFT;
1696 if (amd_iommu_max_glx_val == -1)
1697 amd_iommu_max_glx_val = glxval;
1699 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1702 if (iommu_feature(iommu, FEATURE_GT) &&
1703 iommu_feature(iommu, FEATURE_PPR)) {
1704 iommu->is_iommu_v2 = true;
1705 amd_iommu_v2_present = true;
1708 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1711 ret = iommu_init_ga(iommu);
1715 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1716 amd_iommu_np_cache = true;
1718 init_iommu_perf_ctr(iommu);
1720 if (is_rd890_iommu(iommu->dev)) {
1723 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1727 * Some rd890 systems may not be fully reconfigured by the
1728 * BIOS, so it's necessary for us to store this information so
1729 * it can be reprogrammed on resume
1731 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1732 &iommu->stored_addr_lo);
1733 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1734 &iommu->stored_addr_hi);
1736 /* Low bit locks writes to configuration space */
1737 iommu->stored_addr_lo &= ~1;
1739 for (i = 0; i < 6; i++)
1740 for (j = 0; j < 0x12; j++)
1741 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1743 for (i = 0; i < 0x83; i++)
1744 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1747 amd_iommu_erratum_746_workaround(iommu);
1748 amd_iommu_ats_write_check_workaround(iommu);
1750 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1751 amd_iommu_groups, "ivhd%d", iommu->index);
1752 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1753 iommu_device_register(&iommu->iommu);
1755 return pci_enable_device(iommu->dev);
1758 static void print_iommu_info(void)
1760 static const char * const feat_str[] = {
1761 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1762 "IA", "GA", "HE", "PC"
1764 struct amd_iommu *iommu;
1766 for_each_iommu(iommu) {
1769 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1770 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1772 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1773 pr_info("AMD-Vi: Extended features (%#llx):\n",
1775 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
1776 if (iommu_feature(iommu, (1ULL << i)))
1777 pr_cont(" %s", feat_str[i]);
1780 if (iommu->features & FEATURE_GAM_VAPIC)
1781 pr_cont(" GA_vAPIC");
1786 if (irq_remapping_enabled) {
1787 pr_info("AMD-Vi: Interrupt remapping enabled\n");
1788 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1789 pr_info("AMD-Vi: virtual APIC enabled\n");
1793 static int __init amd_iommu_init_pci(void)
1795 struct amd_iommu *iommu;
1798 for_each_iommu(iommu) {
1799 ret = iommu_init_pci(iommu);
1805 * Order is important here to make sure any unity map requirements are
1806 * fulfilled. The unity mappings are created and written to the device
1807 * table during the amd_iommu_init_api() call.
1809 * After that we call init_device_table_dma() to make sure any
1810 * uninitialized DTE will block DMA, and in the end we flush the caches
1811 * of all IOMMUs to make sure the changes to the device table are
1814 ret = amd_iommu_init_api();
1816 init_device_table_dma();
1818 for_each_iommu(iommu)
1819 iommu_flush_all_caches(iommu);
1827 /****************************************************************************
1829 * The following functions initialize the MSI interrupts for all IOMMUs
1830 * in the system. It's a bit challenging because there could be multiple
1831 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1834 ****************************************************************************/
1836 static int iommu_setup_msi(struct amd_iommu *iommu)
1840 r = pci_enable_msi(iommu->dev);
1844 r = request_threaded_irq(iommu->dev->irq,
1845 amd_iommu_int_handler,
1846 amd_iommu_int_thread,
1851 pci_disable_msi(iommu->dev);
1855 iommu->int_enabled = true;
1860 static int iommu_init_msi(struct amd_iommu *iommu)
1864 if (iommu->int_enabled)
1867 if (iommu->dev->msi_cap)
1868 ret = iommu_setup_msi(iommu);
1876 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1878 if (iommu->ppr_log != NULL)
1879 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1881 iommu_ga_log_enable(iommu);
1886 /****************************************************************************
1888 * The next functions belong to the third pass of parsing the ACPI
1889 * table. In this last pass the memory mapping requirements are
1890 * gathered (like exclusion and unity mapping ranges).
1892 ****************************************************************************/
1894 static void __init free_unity_maps(void)
1896 struct unity_map_entry *entry, *next;
1898 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1899 list_del(&entry->list);
1904 /* called when we find an exclusion range definition in ACPI */
1905 static int __init init_exclusion_range(struct ivmd_header *m)
1910 case ACPI_IVMD_TYPE:
1911 set_device_exclusion_range(m->devid, m);
1913 case ACPI_IVMD_TYPE_ALL:
1914 for (i = 0; i <= amd_iommu_last_bdf; ++i)
1915 set_device_exclusion_range(i, m);
1917 case ACPI_IVMD_TYPE_RANGE:
1918 for (i = m->devid; i <= m->aux; ++i)
1919 set_device_exclusion_range(i, m);
1928 /* called for unity map ACPI definition */
1929 static int __init init_unity_map_range(struct ivmd_header *m)
1931 struct unity_map_entry *e = NULL;
1934 e = kzalloc(sizeof(*e), GFP_KERNEL);
1942 case ACPI_IVMD_TYPE:
1943 s = "IVMD_TYPEi\t\t\t";
1944 e->devid_start = e->devid_end = m->devid;
1946 case ACPI_IVMD_TYPE_ALL:
1947 s = "IVMD_TYPE_ALL\t\t";
1949 e->devid_end = amd_iommu_last_bdf;
1951 case ACPI_IVMD_TYPE_RANGE:
1952 s = "IVMD_TYPE_RANGE\t\t";
1953 e->devid_start = m->devid;
1954 e->devid_end = m->aux;
1957 e->address_start = PAGE_ALIGN(m->range_start);
1958 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1959 e->prot = m->flags >> 1;
1961 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1962 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1963 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1964 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
1965 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1966 e->address_start, e->address_end, m->flags);
1968 list_add_tail(&e->list, &amd_iommu_unity_map);
1973 /* iterates over all memory definitions we find in the ACPI table */
1974 static int __init init_memory_definitions(struct acpi_table_header *table)
1976 u8 *p = (u8 *)table, *end = (u8 *)table;
1977 struct ivmd_header *m;
1979 end += table->length;
1980 p += IVRS_HEADER_LENGTH;
1983 m = (struct ivmd_header *)p;
1984 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1985 init_exclusion_range(m);
1986 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1987 init_unity_map_range(m);
1996 * Init the device table to not allow DMA access for devices and
1997 * suppress all page faults
1999 static void init_device_table_dma(void)
2003 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2004 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2005 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
2009 static void __init uninit_device_table_dma(void)
2013 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2014 amd_iommu_dev_table[devid].data[0] = 0ULL;
2015 amd_iommu_dev_table[devid].data[1] = 0ULL;
2019 static void init_device_table(void)
2023 if (!amd_iommu_irq_remap)
2026 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2027 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2030 static void iommu_init_flags(struct amd_iommu *iommu)
2032 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2033 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2034 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2036 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2037 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2038 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2040 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2041 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2042 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2044 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2045 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2046 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2049 * make IOMMU memory accesses cache coherent
2051 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2053 /* Set IOTLB invalidation timeout to 1s */
2054 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2057 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2060 u32 ioc_feature_control;
2061 struct pci_dev *pdev = iommu->root_pdev;
2063 /* RD890 BIOSes may not have completely reconfigured the iommu */
2064 if (!is_rd890_iommu(iommu->dev) || !pdev)
2068 * First, we need to ensure that the iommu is enabled. This is
2069 * controlled by a register in the northbridge
2072 /* Select Northbridge indirect register 0x75 and enable writing */
2073 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2074 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2076 /* Enable the iommu */
2077 if (!(ioc_feature_control & 0x1))
2078 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2080 /* Restore the iommu BAR */
2081 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2082 iommu->stored_addr_lo);
2083 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2084 iommu->stored_addr_hi);
2086 /* Restore the l1 indirect regs for each of the 6 l1s */
2087 for (i = 0; i < 6; i++)
2088 for (j = 0; j < 0x12; j++)
2089 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2091 /* Restore the l2 indirect regs */
2092 for (i = 0; i < 0x83; i++)
2093 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2095 /* Lock PCI setup registers */
2096 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2097 iommu->stored_addr_lo | 1);
2100 static void iommu_enable_ga(struct amd_iommu *iommu)
2102 #ifdef CONFIG_IRQ_REMAP
2103 switch (amd_iommu_guest_ir) {
2104 case AMD_IOMMU_GUEST_IR_VAPIC:
2105 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2107 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2108 iommu_feature_enable(iommu, CONTROL_GA_EN);
2109 iommu->irte_ops = &irte_128_ops;
2112 iommu->irte_ops = &irte_32_ops;
2118 static void early_enable_iommu(struct amd_iommu *iommu)
2120 iommu_disable(iommu);
2121 iommu_init_flags(iommu);
2122 iommu_set_device_table(iommu);
2123 iommu_enable_command_buffer(iommu);
2124 iommu_enable_event_buffer(iommu);
2125 iommu_set_exclusion_range(iommu);
2126 iommu_enable_ga(iommu);
2127 iommu_enable(iommu);
2128 iommu_flush_all_caches(iommu);
2132 * This function finally enables all IOMMUs found in the system after
2133 * they have been initialized
2135 static void early_enable_iommus(void)
2137 struct amd_iommu *iommu;
2139 for_each_iommu(iommu)
2140 early_enable_iommu(iommu);
2142 #ifdef CONFIG_IRQ_REMAP
2143 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2144 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2148 static void enable_iommus_v2(void)
2150 struct amd_iommu *iommu;
2152 for_each_iommu(iommu) {
2153 iommu_enable_ppr_log(iommu);
2154 iommu_enable_gt(iommu);
2158 static void enable_iommus(void)
2160 early_enable_iommus();
2165 static void disable_iommus(void)
2167 struct amd_iommu *iommu;
2169 for_each_iommu(iommu)
2170 iommu_disable(iommu);
2172 #ifdef CONFIG_IRQ_REMAP
2173 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2174 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2179 * Suspend/Resume support
2180 * disable suspend until real resume implemented
2183 static void amd_iommu_resume(void)
2185 struct amd_iommu *iommu;
2187 for_each_iommu(iommu)
2188 iommu_apply_resume_quirks(iommu);
2190 /* re-load the hardware */
2193 amd_iommu_enable_interrupts();
2196 static int amd_iommu_suspend(void)
2198 /* disable IOMMUs to go out of the way for BIOS */
2204 static struct syscore_ops amd_iommu_syscore_ops = {
2205 .suspend = amd_iommu_suspend,
2206 .resume = amd_iommu_resume,
2209 static void __init free_iommu_resources(void)
2211 kmemleak_free(irq_lookup_table);
2212 free_pages((unsigned long)irq_lookup_table,
2213 get_order(rlookup_table_size));
2214 irq_lookup_table = NULL;
2216 kmem_cache_destroy(amd_iommu_irq_cache);
2217 amd_iommu_irq_cache = NULL;
2219 free_pages((unsigned long)amd_iommu_rlookup_table,
2220 get_order(rlookup_table_size));
2221 amd_iommu_rlookup_table = NULL;
2223 free_pages((unsigned long)amd_iommu_alias_table,
2224 get_order(alias_table_size));
2225 amd_iommu_alias_table = NULL;
2227 free_pages((unsigned long)amd_iommu_dev_table,
2228 get_order(dev_table_size));
2229 amd_iommu_dev_table = NULL;
2233 #ifdef CONFIG_GART_IOMMU
2235 * We failed to initialize the AMD IOMMU - try fallback to GART
2243 /* SB IOAPIC is always on this device in AMD systems */
2244 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2246 static bool __init check_ioapic_information(void)
2248 const char *fw_bug = FW_BUG;
2249 bool ret, has_sb_ioapic;
2252 has_sb_ioapic = false;
2256 * If we have map overrides on the kernel command line the
2257 * messages in this function might not describe firmware bugs
2258 * anymore - so be careful
2263 for (idx = 0; idx < nr_ioapics; idx++) {
2264 int devid, id = mpc_ioapic_id(idx);
2266 devid = get_ioapic_devid(id);
2268 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2271 } else if (devid == IOAPIC_SB_DEVID) {
2272 has_sb_ioapic = true;
2277 if (!has_sb_ioapic) {
2279 * We expect the SB IOAPIC to be listed in the IVRS
2280 * table. The system timer is connected to the SB IOAPIC
2281 * and if we don't have it in the list the system will
2282 * panic at boot time. This situation usually happens
2283 * when the BIOS is buggy and provides us the wrong
2284 * device id for the IOAPIC in the system.
2286 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
2290 pr_err("AMD-Vi: Disabling interrupt remapping\n");
2295 static void __init free_dma_resources(void)
2297 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2298 get_order(MAX_DOMAIN_ID/8));
2299 amd_iommu_pd_alloc_bitmap = NULL;
2305 * This is the hardware init function for AMD IOMMU in the system.
2306 * This function is called either from amd_iommu_init or from the interrupt
2307 * remapping setup code.
2309 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2312 * 1 pass) Discover the most comprehensive IVHD type to use.
2314 * 2 pass) Find the highest PCI device id the driver has to handle.
2315 * Upon this information the size of the data structures is
2316 * determined that needs to be allocated.
2318 * 3 pass) Initialize the data structures just allocated with the
2319 * information in the ACPI table about available AMD IOMMUs
2320 * in the system. It also maps the PCI devices in the
2321 * system to specific IOMMUs
2323 * 4 pass) After the basic data structures are allocated and
2324 * initialized we update them with information about memory
2325 * remapping requirements parsed out of the ACPI table in
2328 * After everything is set up the IOMMUs are enabled and the necessary
2329 * hotplug and suspend notifiers are registered.
2331 static int __init early_amd_iommu_init(void)
2333 struct acpi_table_header *ivrs_base;
2335 int i, remap_cache_sz, ret = 0;
2337 if (!amd_iommu_detected)
2340 status = acpi_get_table("IVRS", 0, &ivrs_base);
2341 if (status == AE_NOT_FOUND)
2343 else if (ACPI_FAILURE(status)) {
2344 const char *err = acpi_format_exception(status);
2345 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2350 * Validate checksum here so we don't need to do it when
2351 * we actually parse the table
2353 ret = check_ivrs_checksum(ivrs_base);
2357 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2358 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2361 * First parse ACPI tables to find the largest Bus/Dev/Func
2362 * we need to handle. Upon this information the shared data
2363 * structures for the IOMMUs in the system will be allocated
2365 ret = find_last_devid_acpi(ivrs_base);
2369 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2370 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2371 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
2373 /* Device table - directly used by all IOMMUs */
2375 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2376 get_order(dev_table_size));
2377 if (amd_iommu_dev_table == NULL)
2381 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2382 * IOMMU see for that device
2384 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2385 get_order(alias_table_size));
2386 if (amd_iommu_alias_table == NULL)
2389 /* IOMMU rlookup table - find the IOMMU for a specific device */
2390 amd_iommu_rlookup_table = (void *)__get_free_pages(
2391 GFP_KERNEL | __GFP_ZERO,
2392 get_order(rlookup_table_size));
2393 if (amd_iommu_rlookup_table == NULL)
2396 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2397 GFP_KERNEL | __GFP_ZERO,
2398 get_order(MAX_DOMAIN_ID/8));
2399 if (amd_iommu_pd_alloc_bitmap == NULL)
2403 * let all alias entries point to itself
2405 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2406 amd_iommu_alias_table[i] = i;
2409 * never allocate domain 0 because its used as the non-allocated and
2410 * error value placeholder
2412 __set_bit(0, amd_iommu_pd_alloc_bitmap);
2414 spin_lock_init(&amd_iommu_pd_lock);
2417 * now the data structures are allocated and basically initialized
2418 * start the real acpi table scan
2420 ret = init_iommu_all(ivrs_base);
2424 /* Disable any previously enabled IOMMUs */
2427 if (amd_iommu_irq_remap)
2428 amd_iommu_irq_remap = check_ioapic_information();
2430 if (amd_iommu_irq_remap) {
2432 * Interrupt remapping enabled, create kmem_cache for the
2436 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2437 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2439 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
2440 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
2442 IRQ_TABLE_ALIGNMENT,
2444 if (!amd_iommu_irq_cache)
2447 irq_lookup_table = (void *)__get_free_pages(
2448 GFP_KERNEL | __GFP_ZERO,
2449 get_order(rlookup_table_size));
2450 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2452 if (!irq_lookup_table)
2456 ret = init_memory_definitions(ivrs_base);
2460 /* init the device table */
2461 init_device_table();
2464 /* Don't leak any ACPI memory */
2465 acpi_put_table(ivrs_base);
2471 static int amd_iommu_enable_interrupts(void)
2473 struct amd_iommu *iommu;
2476 for_each_iommu(iommu) {
2477 ret = iommu_init_msi(iommu);
2486 static bool detect_ivrs(void)
2488 struct acpi_table_header *ivrs_base;
2491 status = acpi_get_table("IVRS", 0, &ivrs_base);
2492 if (status == AE_NOT_FOUND)
2494 else if (ACPI_FAILURE(status)) {
2495 const char *err = acpi_format_exception(status);
2496 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2500 acpi_put_table(ivrs_base);
2502 /* Make sure ACS will be enabled during PCI probe */
2508 /****************************************************************************
2510 * AMD IOMMU Initialization State Machine
2512 ****************************************************************************/
2514 static int __init state_next(void)
2518 switch (init_state) {
2519 case IOMMU_START_STATE:
2520 if (!detect_ivrs()) {
2521 init_state = IOMMU_NOT_FOUND;
2524 init_state = IOMMU_IVRS_DETECTED;
2527 case IOMMU_IVRS_DETECTED:
2528 ret = early_amd_iommu_init();
2529 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2530 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2531 pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
2532 free_dma_resources();
2533 free_iommu_resources();
2534 init_state = IOMMU_CMDLINE_DISABLED;
2538 case IOMMU_ACPI_FINISHED:
2539 early_enable_iommus();
2540 x86_platform.iommu_shutdown = disable_iommus;
2541 init_state = IOMMU_ENABLED;
2544 register_syscore_ops(&amd_iommu_syscore_ops);
2545 ret = amd_iommu_init_pci();
2546 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2549 case IOMMU_PCI_INIT:
2550 ret = amd_iommu_enable_interrupts();
2551 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2553 case IOMMU_INTERRUPTS_EN:
2554 ret = amd_iommu_init_dma_ops();
2555 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2558 init_state = IOMMU_INITIALIZED;
2560 case IOMMU_INITIALIZED:
2563 case IOMMU_NOT_FOUND:
2564 case IOMMU_INIT_ERROR:
2565 case IOMMU_CMDLINE_DISABLED:
2566 /* Error states => do nothing */
2577 static int __init iommu_go_to_state(enum iommu_init_state state)
2581 while (init_state != state) {
2582 if (init_state == IOMMU_NOT_FOUND ||
2583 init_state == IOMMU_INIT_ERROR ||
2584 init_state == IOMMU_CMDLINE_DISABLED)
2592 #ifdef CONFIG_IRQ_REMAP
2593 int __init amd_iommu_prepare(void)
2597 amd_iommu_irq_remap = true;
2599 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2602 return amd_iommu_irq_remap ? 0 : -ENODEV;
2605 int __init amd_iommu_enable(void)
2609 ret = iommu_go_to_state(IOMMU_ENABLED);
2613 irq_remapping_enabled = 1;
2618 void amd_iommu_disable(void)
2620 amd_iommu_suspend();
2623 int amd_iommu_reenable(int mode)
2630 int __init amd_iommu_enable_faulting(void)
2632 /* We enable MSI later when PCI is initialized */
2638 * This is the core init function for AMD IOMMU hardware in the system.
2639 * This function is called from the generic x86 DMA layer initialization
2642 static int __init amd_iommu_init(void)
2646 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2648 free_dma_resources();
2649 if (!irq_remapping_enabled) {
2651 free_iommu_resources();
2653 struct amd_iommu *iommu;
2655 uninit_device_table_dma();
2656 for_each_iommu(iommu)
2657 iommu_flush_all_caches(iommu);
2664 /****************************************************************************
2666 * Early detect code. This code runs at IOMMU detection time in the DMA
2667 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2670 ****************************************************************************/
2671 int __init amd_iommu_detect(void)
2675 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
2678 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2682 amd_iommu_detected = true;
2684 x86_init.iommu.iommu_init = amd_iommu_init;
2689 /****************************************************************************
2691 * Parsing functions for the AMD IOMMU specific kernel command line
2694 ****************************************************************************/
2696 static int __init parse_amd_iommu_dump(char *str)
2698 amd_iommu_dump = true;
2703 static int __init parse_amd_iommu_intr(char *str)
2705 for (; *str; ++str) {
2706 if (strncmp(str, "legacy", 6) == 0) {
2707 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2710 if (strncmp(str, "vapic", 5) == 0) {
2711 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2718 static int __init parse_amd_iommu_options(char *str)
2720 for (; *str; ++str) {
2721 if (strncmp(str, "fullflush", 9) == 0)
2722 amd_iommu_unmap_flush = true;
2723 if (strncmp(str, "off", 3) == 0)
2724 amd_iommu_disabled = true;
2725 if (strncmp(str, "force_isolation", 15) == 0)
2726 amd_iommu_force_isolation = true;
2732 static int __init parse_ivrs_ioapic(char *str)
2734 unsigned int bus, dev, fn;
2738 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2741 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2745 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2746 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2751 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2753 cmdline_maps = true;
2754 i = early_ioapic_map_size++;
2755 early_ioapic_map[i].id = id;
2756 early_ioapic_map[i].devid = devid;
2757 early_ioapic_map[i].cmd_line = true;
2762 static int __init parse_ivrs_hpet(char *str)
2764 unsigned int bus, dev, fn;
2768 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2771 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2775 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2776 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2781 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2783 cmdline_maps = true;
2784 i = early_hpet_map_size++;
2785 early_hpet_map[i].id = id;
2786 early_hpet_map[i].devid = devid;
2787 early_hpet_map[i].cmd_line = true;
2792 static int __init parse_ivrs_acpihid(char *str)
2795 char *hid, *uid, *p;
2796 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2799 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2801 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2806 hid = strsep(&p, ":");
2809 if (!hid || !(*hid) || !uid) {
2810 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2814 i = early_acpihid_map_size++;
2815 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2816 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2817 early_acpihid_map[i].devid =
2818 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2819 early_acpihid_map[i].cmd_line = true;
2824 __setup("amd_iommu_dump", parse_amd_iommu_dump);
2825 __setup("amd_iommu=", parse_amd_iommu_options);
2826 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
2827 __setup("ivrs_ioapic", parse_ivrs_ioapic);
2828 __setup("ivrs_hpet", parse_ivrs_hpet);
2829 __setup("ivrs_acpihid", parse_ivrs_acpihid);
2831 IOMMU_INIT_FINISH(amd_iommu_detect,
2832 gart_iommu_hole_init,
2836 bool amd_iommu_v2_supported(void)
2838 return amd_iommu_v2_present;
2840 EXPORT_SYMBOL(amd_iommu_v2_supported);
2842 struct amd_iommu *get_amd_iommu(unsigned int idx)
2845 struct amd_iommu *iommu;
2847 for_each_iommu(iommu)
2852 EXPORT_SYMBOL(get_amd_iommu);
2854 /****************************************************************************
2856 * IOMMU EFR Performance Counter support functionality. This code allows
2857 * access to the IOMMU PC functionality.
2859 ****************************************************************************/
2861 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
2863 struct amd_iommu *iommu = get_amd_iommu(idx);
2866 return iommu->max_banks;
2870 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2872 bool amd_iommu_pc_supported(void)
2874 return amd_iommu_pc_present;
2876 EXPORT_SYMBOL(amd_iommu_pc_supported);
2878 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
2880 struct amd_iommu *iommu = get_amd_iommu(idx);
2883 return iommu->max_counters;
2887 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2889 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2890 u8 fxn, u64 *value, bool is_write)
2895 /* Make sure the IOMMU PC resource is available */
2896 if (!amd_iommu_pc_present)
2899 /* Check for valid iommu and pc register indexing */
2900 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
2903 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
2905 /* Limit the offset to the hw defined mmio region aperture */
2906 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
2907 (iommu->max_counters << 8) | 0x28);
2908 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2909 (offset > max_offset_lim))
2913 u64 val = *value & GENMASK_ULL(47, 0);
2915 writel((u32)val, iommu->mmio_base + offset);
2916 writel((val >> 32), iommu->mmio_base + offset + 4);
2918 *value = readl(iommu->mmio_base + offset + 4);
2920 *value |= readl(iommu->mmio_base + offset);
2921 *value &= GENMASK_ULL(47, 0);
2927 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
2932 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
2934 EXPORT_SYMBOL(amd_iommu_pc_get_reg);
2936 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
2941 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
2943 EXPORT_SYMBOL(amd_iommu_pc_set_reg);