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 <asm/pci-direct.h>
32 #include <asm/iommu.h>
34 #include <asm/x86_init.h>
35 #include <asm/iommu_table.h>
36 #include <asm/io_apic.h>
37 #include <asm/irq_remapping.h>
39 #include "amd_iommu_proto.h"
40 #include "amd_iommu_types.h"
41 #include "irq_remapping.h"
44 * definitions for the ACPI scanning code
46 #define IVRS_HEADER_LENGTH 48
48 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
49 #define ACPI_IVMD_TYPE_ALL 0x20
50 #define ACPI_IVMD_TYPE 0x21
51 #define ACPI_IVMD_TYPE_RANGE 0x22
53 #define IVHD_DEV_ALL 0x01
54 #define IVHD_DEV_SELECT 0x02
55 #define IVHD_DEV_SELECT_RANGE_START 0x03
56 #define IVHD_DEV_RANGE_END 0x04
57 #define IVHD_DEV_ALIAS 0x42
58 #define IVHD_DEV_ALIAS_RANGE 0x43
59 #define IVHD_DEV_EXT_SELECT 0x46
60 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
61 #define IVHD_DEV_SPECIAL 0x48
62 #define IVHD_DEV_ACPI_HID 0xf0
64 #define UID_NOT_PRESENT 0
65 #define UID_IS_INTEGER 1
66 #define UID_IS_CHARACTER 2
68 #define IVHD_SPECIAL_IOAPIC 1
69 #define IVHD_SPECIAL_HPET 2
71 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
72 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
73 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
74 #define IVHD_FLAG_ISOC_EN_MASK 0x08
76 #define IVMD_FLAG_EXCL_RANGE 0x08
77 #define IVMD_FLAG_UNITY_MAP 0x01
79 #define ACPI_DEVFLAG_INITPASS 0x01
80 #define ACPI_DEVFLAG_EXTINT 0x02
81 #define ACPI_DEVFLAG_NMI 0x04
82 #define ACPI_DEVFLAG_SYSMGT1 0x10
83 #define ACPI_DEVFLAG_SYSMGT2 0x20
84 #define ACPI_DEVFLAG_LINT0 0x40
85 #define ACPI_DEVFLAG_LINT1 0x80
86 #define ACPI_DEVFLAG_ATSDIS 0x10000000
88 #define LOOP_TIMEOUT 100000
90 * ACPI table definitions
92 * These data structures are laid over the table to parse the important values
97 * structure describing one IOMMU in the ACPI table. Typically followed by one
98 * or more ivhd_entrys.
111 /* Following only valid on IVHD type 11h and 40h */
112 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
114 } __attribute__((packed));
117 * A device entry describing which devices a specific IOMMU translates and
118 * which requestor ids they use.
130 } __attribute__((packed));
133 * An AMD IOMMU memory definition structure. It defines things like exclusion
134 * ranges for devices and regions that should be unity mapped.
145 } __attribute__((packed));
148 bool amd_iommu_irq_remap __read_mostly;
150 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
152 static bool amd_iommu_detected;
153 static bool __initdata amd_iommu_disabled;
154 static int amd_iommu_target_ivhd_type;
156 u16 amd_iommu_last_bdf; /* largest PCI device id we have
158 LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
160 bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
162 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
165 /* Array to assign indices to IOMMUs*/
166 struct amd_iommu *amd_iommus[MAX_IOMMUS];
167 int amd_iommus_present;
169 /* IOMMUs have a non-present cache? */
170 bool amd_iommu_np_cache __read_mostly;
171 bool amd_iommu_iotlb_sup __read_mostly = true;
173 u32 amd_iommu_max_pasid __read_mostly = ~0;
175 bool amd_iommu_v2_present __read_mostly;
176 static bool amd_iommu_pc_present __read_mostly;
178 bool amd_iommu_force_isolation __read_mostly;
181 * List of protection domains - used during resume
183 LIST_HEAD(amd_iommu_pd_list);
184 spinlock_t amd_iommu_pd_lock;
187 * Pointer to the device table which is shared by all AMD IOMMUs
188 * it is indexed by the PCI device id or the HT unit id and contains
189 * information about the domain the device belongs to as well as the
190 * page table root pointer.
192 struct dev_table_entry *amd_iommu_dev_table;
195 * The alias table is a driver specific data structure which contains the
196 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
197 * More than one device can share the same requestor id.
199 u16 *amd_iommu_alias_table;
202 * The rlookup table is used to find the IOMMU which is responsible
203 * for a specific device. It is also indexed by the PCI device id.
205 struct amd_iommu **amd_iommu_rlookup_table;
208 * This table is used to find the irq remapping table for a given device id
211 struct irq_remap_table **irq_lookup_table;
214 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
215 * to know which ones are already in use.
217 unsigned long *amd_iommu_pd_alloc_bitmap;
219 static u32 dev_table_size; /* size of the device table */
220 static u32 alias_table_size; /* size of the alias table */
221 static u32 rlookup_table_size; /* size if the rlookup table */
223 enum iommu_init_state {
236 /* Early ioapic and hpet maps from kernel command line */
237 #define EARLY_MAP_SIZE 4
238 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
239 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
240 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
242 static int __initdata early_ioapic_map_size;
243 static int __initdata early_hpet_map_size;
244 static int __initdata early_acpihid_map_size;
246 static bool __initdata cmdline_maps;
248 static enum iommu_init_state init_state = IOMMU_START_STATE;
250 static int amd_iommu_enable_interrupts(void);
251 static int __init iommu_go_to_state(enum iommu_init_state state);
252 static void init_device_table_dma(void);
254 static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
255 u8 bank, u8 cntr, u8 fxn,
256 u64 *value, bool is_write);
258 static inline void update_last_devid(u16 devid)
260 if (devid > amd_iommu_last_bdf)
261 amd_iommu_last_bdf = devid;
264 static inline unsigned long tbl_size(int entry_size)
266 unsigned shift = PAGE_SHIFT +
267 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
272 /* Access to l1 and l2 indexed register spaces */
274 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
278 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
279 pci_read_config_dword(iommu->dev, 0xfc, &val);
283 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
285 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
286 pci_write_config_dword(iommu->dev, 0xfc, val);
287 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
290 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
294 pci_write_config_dword(iommu->dev, 0xf0, address);
295 pci_read_config_dword(iommu->dev, 0xf4, &val);
299 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
301 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
302 pci_write_config_dword(iommu->dev, 0xf4, val);
305 /****************************************************************************
307 * AMD IOMMU MMIO register space handling functions
309 * These functions are used to program the IOMMU device registers in
310 * MMIO space required for that driver.
312 ****************************************************************************/
315 * This function set the exclusion range in the IOMMU. DMA accesses to the
316 * exclusion range are passed through untranslated
318 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
320 u64 start = iommu->exclusion_start & PAGE_MASK;
321 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
324 if (!iommu->exclusion_start)
327 entry = start | MMIO_EXCL_ENABLE_MASK;
328 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
329 &entry, sizeof(entry));
332 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
333 &entry, sizeof(entry));
336 /* Programs the physical address of the device table into the IOMMU hardware */
337 static void iommu_set_device_table(struct amd_iommu *iommu)
341 BUG_ON(iommu->mmio_base == NULL);
343 entry = virt_to_phys(amd_iommu_dev_table);
344 entry |= (dev_table_size >> 12) - 1;
345 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
346 &entry, sizeof(entry));
349 /* Generic functions to enable/disable certain features of the IOMMU. */
350 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
354 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
356 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
359 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
363 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
365 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
368 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
372 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
373 ctrl &= ~CTRL_INV_TO_MASK;
374 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
375 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
378 /* Function to enable the hardware */
379 static void iommu_enable(struct amd_iommu *iommu)
381 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
384 static void iommu_disable(struct amd_iommu *iommu)
386 if (!iommu->mmio_base)
389 /* Disable command buffer */
390 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
392 /* Disable event logging and event interrupts */
393 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
394 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
396 /* Disable IOMMU GA_LOG */
397 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
398 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
400 /* Disable IOMMU hardware itself */
401 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
405 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
406 * the system has one.
408 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
410 if (!request_mem_region(address, end, "amd_iommu")) {
411 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
413 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
417 return (u8 __iomem *)ioremap_nocache(address, end);
420 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
422 if (iommu->mmio_base)
423 iounmap(iommu->mmio_base);
424 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
427 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
443 /****************************************************************************
445 * The functions below belong to the first pass of AMD IOMMU ACPI table
446 * parsing. In this pass we try to find out the highest device id this
447 * code has to handle. Upon this information the size of the shared data
448 * structures is determined later.
450 ****************************************************************************/
453 * This function calculates the length of a given IVHD entry
455 static inline int ivhd_entry_length(u8 *ivhd)
457 u32 type = ((struct ivhd_entry *)ivhd)->type;
460 return 0x04 << (*ivhd >> 6);
461 } else if (type == IVHD_DEV_ACPI_HID) {
462 /* For ACPI_HID, offset 21 is uid len */
463 return *((u8 *)ivhd + 21) + 22;
469 * After reading the highest device id from the IOMMU PCI capability header
470 * this function looks if there is a higher device id defined in the ACPI table
472 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
474 u8 *p = (void *)h, *end = (void *)h;
475 struct ivhd_entry *dev;
477 u32 ivhd_size = get_ivhd_header_size(h);
480 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
488 dev = (struct ivhd_entry *)p;
491 /* Use maximum BDF value for DEV_ALL */
492 update_last_devid(0xffff);
494 case IVHD_DEV_SELECT:
495 case IVHD_DEV_RANGE_END:
497 case IVHD_DEV_EXT_SELECT:
498 /* all the above subfield types refer to device ids */
499 update_last_devid(dev->devid);
504 p += ivhd_entry_length(p);
512 static int __init check_ivrs_checksum(struct acpi_table_header *table)
515 u8 checksum = 0, *p = (u8 *)table;
517 for (i = 0; i < table->length; ++i)
520 /* ACPI table corrupt */
521 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
529 * Iterate over all IVHD entries in the ACPI table and find the highest device
530 * id which we need to handle. This is the first of three functions which parse
531 * the ACPI table. So we check the checksum here.
533 static int __init find_last_devid_acpi(struct acpi_table_header *table)
535 u8 *p = (u8 *)table, *end = (u8 *)table;
536 struct ivhd_header *h;
538 p += IVRS_HEADER_LENGTH;
540 end += table->length;
542 h = (struct ivhd_header *)p;
543 if (h->type == amd_iommu_target_ivhd_type) {
544 int ret = find_last_devid_from_ivhd(h);
556 /****************************************************************************
558 * The following functions belong to the code path which parses the ACPI table
559 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
560 * data structures, initialize the device/alias/rlookup table and also
561 * basically initialize the hardware.
563 ****************************************************************************/
566 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
567 * write commands to that buffer later and the IOMMU will execute them
570 static int __init alloc_command_buffer(struct amd_iommu *iommu)
572 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
573 get_order(CMD_BUFFER_SIZE));
575 return iommu->cmd_buf ? 0 : -ENOMEM;
579 * This function resets the command buffer if the IOMMU stopped fetching
582 void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
584 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
586 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
587 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
589 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
593 * This function writes the command buffer address to the hardware and
596 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
600 BUG_ON(iommu->cmd_buf == NULL);
602 entry = (u64)virt_to_phys(iommu->cmd_buf);
603 entry |= MMIO_CMD_SIZE_512;
605 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
606 &entry, sizeof(entry));
608 amd_iommu_reset_cmd_buffer(iommu);
611 static void __init free_command_buffer(struct amd_iommu *iommu)
613 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
616 /* allocates the memory where the IOMMU will log its events to */
617 static int __init alloc_event_buffer(struct amd_iommu *iommu)
619 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
620 get_order(EVT_BUFFER_SIZE));
622 return iommu->evt_buf ? 0 : -ENOMEM;
625 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
629 BUG_ON(iommu->evt_buf == NULL);
631 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
633 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
634 &entry, sizeof(entry));
636 /* set head and tail to zero manually */
637 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
638 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
640 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
643 static void __init free_event_buffer(struct amd_iommu *iommu)
645 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
648 /* allocates the memory where the IOMMU will log its events to */
649 static int __init alloc_ppr_log(struct amd_iommu *iommu)
651 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
652 get_order(PPR_LOG_SIZE));
654 return iommu->ppr_log ? 0 : -ENOMEM;
657 static void iommu_enable_ppr_log(struct amd_iommu *iommu)
661 if (iommu->ppr_log == NULL)
664 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
666 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
667 &entry, sizeof(entry));
669 /* set head and tail to zero manually */
670 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
671 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
673 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
674 iommu_feature_enable(iommu, CONTROL_PPR_EN);
677 static void __init free_ppr_log(struct amd_iommu *iommu)
679 if (iommu->ppr_log == NULL)
682 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
685 static void free_ga_log(struct amd_iommu *iommu)
687 #ifdef CONFIG_IRQ_REMAP
689 free_pages((unsigned long)iommu->ga_log,
690 get_order(GA_LOG_SIZE));
691 if (iommu->ga_log_tail)
692 free_pages((unsigned long)iommu->ga_log_tail,
697 static int iommu_ga_log_enable(struct amd_iommu *iommu)
699 #ifdef CONFIG_IRQ_REMAP
705 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
707 /* Check if already running */
708 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
711 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
712 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
714 for (i = 0; i < LOOP_TIMEOUT; ++i) {
715 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
716 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
720 if (i >= LOOP_TIMEOUT)
722 #endif /* CONFIG_IRQ_REMAP */
726 #ifdef CONFIG_IRQ_REMAP
727 static int iommu_init_ga_log(struct amd_iommu *iommu)
731 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
734 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
735 get_order(GA_LOG_SIZE));
739 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
741 if (!iommu->ga_log_tail)
744 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
745 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
746 &entry, sizeof(entry));
747 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
748 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
749 &entry, sizeof(entry));
750 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
751 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
758 #endif /* CONFIG_IRQ_REMAP */
760 static int iommu_init_ga(struct amd_iommu *iommu)
764 #ifdef CONFIG_IRQ_REMAP
765 /* Note: We have already checked GASup from IVRS table.
766 * Now, we need to make sure that GAMSup is set.
768 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
769 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
770 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
772 ret = iommu_init_ga_log(iommu);
773 #endif /* CONFIG_IRQ_REMAP */
778 static void iommu_enable_gt(struct amd_iommu *iommu)
780 if (!iommu_feature(iommu, FEATURE_GT))
783 iommu_feature_enable(iommu, CONTROL_GT_EN);
786 /* sets a specific bit in the device table entry. */
787 static void set_dev_entry_bit(u16 devid, u8 bit)
789 int i = (bit >> 6) & 0x03;
790 int _bit = bit & 0x3f;
792 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
795 static int get_dev_entry_bit(u16 devid, u8 bit)
797 int i = (bit >> 6) & 0x03;
798 int _bit = bit & 0x3f;
800 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
804 void amd_iommu_apply_erratum_63(u16 devid)
808 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
809 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
812 set_dev_entry_bit(devid, DEV_ENTRY_IW);
815 /* Writes the specific IOMMU for a device into the rlookup table */
816 static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
818 amd_iommu_rlookup_table[devid] = iommu;
822 * This function takes the device specific flags read from the ACPI
823 * table and sets up the device table entry with that information
825 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
826 u16 devid, u32 flags, u32 ext_flags)
828 if (flags & ACPI_DEVFLAG_INITPASS)
829 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
830 if (flags & ACPI_DEVFLAG_EXTINT)
831 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
832 if (flags & ACPI_DEVFLAG_NMI)
833 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
834 if (flags & ACPI_DEVFLAG_SYSMGT1)
835 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
836 if (flags & ACPI_DEVFLAG_SYSMGT2)
837 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
838 if (flags & ACPI_DEVFLAG_LINT0)
839 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
840 if (flags & ACPI_DEVFLAG_LINT1)
841 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
843 amd_iommu_apply_erratum_63(devid);
845 set_iommu_for_device(iommu, devid);
848 static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
850 struct devid_map *entry;
851 struct list_head *list;
853 if (type == IVHD_SPECIAL_IOAPIC)
855 else if (type == IVHD_SPECIAL_HPET)
860 list_for_each_entry(entry, list, list) {
861 if (!(entry->id == id && entry->cmd_line))
864 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
865 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
867 *devid = entry->devid;
872 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
877 entry->devid = *devid;
878 entry->cmd_line = cmd_line;
880 list_add_tail(&entry->list, list);
885 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
888 struct acpihid_map_entry *entry;
889 struct list_head *list = &acpihid_map;
891 list_for_each_entry(entry, list, list) {
892 if (strcmp(entry->hid, hid) ||
893 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
897 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
899 *devid = entry->devid;
903 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
907 memcpy(entry->uid, uid, strlen(uid));
908 memcpy(entry->hid, hid, strlen(hid));
909 entry->devid = *devid;
910 entry->cmd_line = cmd_line;
911 entry->root_devid = (entry->devid & (~0x7));
913 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
914 entry->cmd_line ? "cmd" : "ivrs",
915 entry->hid, entry->uid, entry->root_devid);
917 list_add_tail(&entry->list, list);
921 static int __init add_early_maps(void)
925 for (i = 0; i < early_ioapic_map_size; ++i) {
926 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
927 early_ioapic_map[i].id,
928 &early_ioapic_map[i].devid,
929 early_ioapic_map[i].cmd_line);
934 for (i = 0; i < early_hpet_map_size; ++i) {
935 ret = add_special_device(IVHD_SPECIAL_HPET,
936 early_hpet_map[i].id,
937 &early_hpet_map[i].devid,
938 early_hpet_map[i].cmd_line);
943 for (i = 0; i < early_acpihid_map_size; ++i) {
944 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
945 early_acpihid_map[i].uid,
946 &early_acpihid_map[i].devid,
947 early_acpihid_map[i].cmd_line);
956 * Reads the device exclusion range from ACPI and initializes the IOMMU with
959 static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
961 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
963 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
968 * We only can configure exclusion ranges per IOMMU, not
969 * per device. But we can enable the exclusion range per
970 * device. This is done here
972 set_dev_entry_bit(devid, DEV_ENTRY_EX);
973 iommu->exclusion_start = m->range_start;
974 iommu->exclusion_length = m->range_length;
979 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
980 * initializes the hardware and our data structures with it.
982 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
983 struct ivhd_header *h)
986 u8 *end = p, flags = 0;
987 u16 devid = 0, devid_start = 0, devid_to = 0;
988 u32 dev_i, ext_flags = 0;
990 struct ivhd_entry *e;
995 ret = add_early_maps();
1000 * First save the recommended feature enable bits from ACPI
1002 iommu->acpi_flags = h->flags;
1005 * Done. Now parse the device entries
1007 ivhd_size = get_ivhd_header_size(h);
1009 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1019 e = (struct ivhd_entry *)p;
1023 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1025 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1026 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1028 case IVHD_DEV_SELECT:
1030 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1032 PCI_BUS_NUM(e->devid),
1038 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1040 case IVHD_DEV_SELECT_RANGE_START:
1042 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1043 "devid: %02x:%02x.%x flags: %02x\n",
1044 PCI_BUS_NUM(e->devid),
1049 devid_start = e->devid;
1054 case IVHD_DEV_ALIAS:
1056 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1057 "flags: %02x devid_to: %02x:%02x.%x\n",
1058 PCI_BUS_NUM(e->devid),
1062 PCI_BUS_NUM(e->ext >> 8),
1063 PCI_SLOT(e->ext >> 8),
1064 PCI_FUNC(e->ext >> 8));
1067 devid_to = e->ext >> 8;
1068 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1069 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1070 amd_iommu_alias_table[devid] = devid_to;
1072 case IVHD_DEV_ALIAS_RANGE:
1074 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1075 "devid: %02x:%02x.%x flags: %02x "
1076 "devid_to: %02x:%02x.%x\n",
1077 PCI_BUS_NUM(e->devid),
1081 PCI_BUS_NUM(e->ext >> 8),
1082 PCI_SLOT(e->ext >> 8),
1083 PCI_FUNC(e->ext >> 8));
1085 devid_start = e->devid;
1087 devid_to = e->ext >> 8;
1091 case IVHD_DEV_EXT_SELECT:
1093 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1094 "flags: %02x ext: %08x\n",
1095 PCI_BUS_NUM(e->devid),
1101 set_dev_entry_from_acpi(iommu, devid, e->flags,
1104 case IVHD_DEV_EXT_SELECT_RANGE:
1106 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1107 "%02x:%02x.%x flags: %02x ext: %08x\n",
1108 PCI_BUS_NUM(e->devid),
1113 devid_start = e->devid;
1118 case IVHD_DEV_RANGE_END:
1120 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1121 PCI_BUS_NUM(e->devid),
1123 PCI_FUNC(e->devid));
1126 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1128 amd_iommu_alias_table[dev_i] = devid_to;
1129 set_dev_entry_from_acpi(iommu,
1130 devid_to, flags, ext_flags);
1132 set_dev_entry_from_acpi(iommu, dev_i,
1136 case IVHD_DEV_SPECIAL: {
1142 handle = e->ext & 0xff;
1143 devid = (e->ext >> 8) & 0xffff;
1144 type = (e->ext >> 24) & 0xff;
1146 if (type == IVHD_SPECIAL_IOAPIC)
1148 else if (type == IVHD_SPECIAL_HPET)
1153 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1159 ret = add_special_device(type, handle, &devid, false);
1164 * add_special_device might update the devid in case a
1165 * command-line override is present. So call
1166 * set_dev_entry_from_acpi after add_special_device.
1168 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1172 case IVHD_DEV_ACPI_HID: {
1174 u8 hid[ACPIHID_HID_LEN];
1175 u8 uid[ACPIHID_UID_LEN];
1178 if (h->type != 0x40) {
1179 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1184 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1185 hid[ACPIHID_HID_LEN - 1] = '\0';
1188 pr_err(FW_BUG "Invalid HID.\n");
1194 case UID_NOT_PRESENT:
1197 pr_warn(FW_BUG "Invalid UID length.\n");
1200 case UID_IS_INTEGER:
1202 sprintf(uid, "%d", e->uid);
1205 case UID_IS_CHARACTER:
1207 memcpy(uid, &e->uid, e->uidl);
1208 uid[e->uidl] = '\0';
1216 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1224 ret = add_acpi_hid_device(hid, uid, &devid, false);
1229 * add_special_device might update the devid in case a
1230 * command-line override is present. So call
1231 * set_dev_entry_from_acpi after add_special_device.
1233 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1241 p += ivhd_entry_length(p);
1247 static void __init free_iommu_one(struct amd_iommu *iommu)
1249 free_command_buffer(iommu);
1250 free_event_buffer(iommu);
1251 free_ppr_log(iommu);
1253 iommu_unmap_mmio_space(iommu);
1256 static void __init free_iommu_all(void)
1258 struct amd_iommu *iommu, *next;
1260 for_each_iommu_safe(iommu, next) {
1261 list_del(&iommu->list);
1262 free_iommu_one(iommu);
1268 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1270 * BIOS should disable L2B micellaneous clock gating by setting
1271 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1273 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1277 if ((boot_cpu_data.x86 != 0x15) ||
1278 (boot_cpu_data.x86_model < 0x10) ||
1279 (boot_cpu_data.x86_model > 0x1f))
1282 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1283 pci_read_config_dword(iommu->dev, 0xf4, &value);
1288 /* Select NB indirect register 0x90 and enable writing */
1289 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1291 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1292 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1293 dev_name(&iommu->dev->dev));
1295 /* Clear the enable writing bit */
1296 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1300 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1302 * BIOS should enable ATS write permission check by setting
1303 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1305 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1309 if ((boot_cpu_data.x86 != 0x15) ||
1310 (boot_cpu_data.x86_model < 0x30) ||
1311 (boot_cpu_data.x86_model > 0x3f))
1314 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1315 value = iommu_read_l2(iommu, 0x47);
1320 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1321 iommu_write_l2(iommu, 0x47, value | BIT(0));
1323 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1324 dev_name(&iommu->dev->dev));
1328 * This function clues the initialization function for one IOMMU
1329 * together and also allocates the command buffer and programs the
1330 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1332 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1336 spin_lock_init(&iommu->lock);
1338 /* Add IOMMU to internal data structures */
1339 list_add_tail(&iommu->list, &amd_iommu_list);
1340 iommu->index = amd_iommus_present++;
1342 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1343 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1347 /* Index is fine - add IOMMU to the array */
1348 amd_iommus[iommu->index] = iommu;
1351 * Copy data from ACPI table entry to the iommu struct
1353 iommu->devid = h->devid;
1354 iommu->cap_ptr = h->cap_ptr;
1355 iommu->pci_seg = h->pci_seg;
1356 iommu->mmio_phys = h->mmio_phys;
1360 /* Check if IVHD EFR contains proper max banks/counters */
1361 if ((h->efr_attr != 0) &&
1362 ((h->efr_attr & (0xF << 13)) != 0) &&
1363 ((h->efr_attr & (0x3F << 17)) != 0))
1364 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1366 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1367 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1368 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1372 if (h->efr_reg & (1 << 9))
1373 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1375 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1376 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1377 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1383 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1384 iommu->mmio_phys_end);
1385 if (!iommu->mmio_base)
1388 if (alloc_command_buffer(iommu))
1391 if (alloc_event_buffer(iommu))
1394 iommu->int_enabled = false;
1396 ret = init_iommu_from_acpi(iommu, h);
1400 ret = amd_iommu_create_irq_domain(iommu);
1405 * Make sure IOMMU is not considered to translate itself. The IVRS
1406 * table tells us so, but this is a lie!
1408 amd_iommu_rlookup_table[iommu->devid] = NULL;
1414 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1415 * @ivrs Pointer to the IVRS header
1417 * This function search through all IVDB of the maximum supported IVHD
1419 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1421 u8 *base = (u8 *)ivrs;
1422 struct ivhd_header *ivhd = (struct ivhd_header *)
1423 (base + IVRS_HEADER_LENGTH);
1424 u8 last_type = ivhd->type;
1425 u16 devid = ivhd->devid;
1427 while (((u8 *)ivhd - base < ivrs->length) &&
1428 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1429 u8 *p = (u8 *) ivhd;
1431 if (ivhd->devid == devid)
1432 last_type = ivhd->type;
1433 ivhd = (struct ivhd_header *)(p + ivhd->length);
1440 * Iterates over all IOMMU entries in the ACPI table, allocates the
1441 * IOMMU structure and initializes it with init_iommu_one()
1443 static int __init init_iommu_all(struct acpi_table_header *table)
1445 u8 *p = (u8 *)table, *end = (u8 *)table;
1446 struct ivhd_header *h;
1447 struct amd_iommu *iommu;
1450 end += table->length;
1451 p += IVRS_HEADER_LENGTH;
1454 h = (struct ivhd_header *)p;
1455 if (*p == amd_iommu_target_ivhd_type) {
1457 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1458 "seg: %d flags: %01x info %04x\n",
1459 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
1460 PCI_FUNC(h->devid), h->cap_ptr,
1461 h->pci_seg, h->flags, h->info);
1462 DUMP_printk(" mmio-addr: %016llx\n",
1465 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1469 ret = init_iommu_one(iommu, h);
1482 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1484 u64 val = 0xabcd, val2 = 0;
1486 if (!iommu_feature(iommu, FEATURE_PC))
1489 amd_iommu_pc_present = true;
1491 /* Check if the performance counters can be written to */
1492 if ((0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val, true)) ||
1493 (0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val2, false)) ||
1495 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1496 amd_iommu_pc_present = false;
1500 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1502 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1503 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1504 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1507 static ssize_t amd_iommu_show_cap(struct device *dev,
1508 struct device_attribute *attr,
1511 struct amd_iommu *iommu = dev_get_drvdata(dev);
1512 return sprintf(buf, "%x\n", iommu->cap);
1514 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1516 static ssize_t amd_iommu_show_features(struct device *dev,
1517 struct device_attribute *attr,
1520 struct amd_iommu *iommu = dev_get_drvdata(dev);
1521 return sprintf(buf, "%llx\n", iommu->features);
1523 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1525 static struct attribute *amd_iommu_attrs[] = {
1527 &dev_attr_features.attr,
1531 static struct attribute_group amd_iommu_group = {
1532 .name = "amd-iommu",
1533 .attrs = amd_iommu_attrs,
1536 static const struct attribute_group *amd_iommu_groups[] = {
1541 static int __init iommu_init_pci(struct amd_iommu *iommu)
1543 int cap_ptr = iommu->cap_ptr;
1544 u32 range, misc, low, high;
1547 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
1548 iommu->devid & 0xff);
1552 /* Prevent binding other PCI device drivers to IOMMU devices */
1553 iommu->dev->match_driver = false;
1555 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1557 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1559 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1562 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1563 amd_iommu_iotlb_sup = false;
1565 /* read extended feature bits */
1566 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1567 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1569 iommu->features = ((u64)high << 32) | low;
1571 if (iommu_feature(iommu, FEATURE_GT)) {
1576 pasmax = iommu->features & FEATURE_PASID_MASK;
1577 pasmax >>= FEATURE_PASID_SHIFT;
1578 max_pasid = (1 << (pasmax + 1)) - 1;
1580 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1582 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
1584 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1585 glxval >>= FEATURE_GLXVAL_SHIFT;
1587 if (amd_iommu_max_glx_val == -1)
1588 amd_iommu_max_glx_val = glxval;
1590 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1593 if (iommu_feature(iommu, FEATURE_GT) &&
1594 iommu_feature(iommu, FEATURE_PPR)) {
1595 iommu->is_iommu_v2 = true;
1596 amd_iommu_v2_present = true;
1599 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1602 ret = iommu_init_ga(iommu);
1606 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1607 amd_iommu_np_cache = true;
1609 init_iommu_perf_ctr(iommu);
1611 if (is_rd890_iommu(iommu->dev)) {
1614 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1618 * Some rd890 systems may not be fully reconfigured by the
1619 * BIOS, so it's necessary for us to store this information so
1620 * it can be reprogrammed on resume
1622 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1623 &iommu->stored_addr_lo);
1624 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1625 &iommu->stored_addr_hi);
1627 /* Low bit locks writes to configuration space */
1628 iommu->stored_addr_lo &= ~1;
1630 for (i = 0; i < 6; i++)
1631 for (j = 0; j < 0x12; j++)
1632 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1634 for (i = 0; i < 0x83; i++)
1635 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1638 amd_iommu_erratum_746_workaround(iommu);
1639 amd_iommu_ats_write_check_workaround(iommu);
1641 iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
1642 amd_iommu_groups, "ivhd%d",
1645 return pci_enable_device(iommu->dev);
1648 static void print_iommu_info(void)
1650 static const char * const feat_str[] = {
1651 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1652 "IA", "GA", "HE", "PC"
1654 struct amd_iommu *iommu;
1656 for_each_iommu(iommu) {
1659 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1660 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1662 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1663 pr_info("AMD-Vi: Extended features (%#llx):\n",
1665 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
1666 if (iommu_feature(iommu, (1ULL << i)))
1667 pr_cont(" %s", feat_str[i]);
1670 if (iommu->features & FEATURE_GAM_VAPIC)
1671 pr_cont(" GA_vAPIC");
1676 if (irq_remapping_enabled) {
1677 pr_info("AMD-Vi: Interrupt remapping enabled\n");
1678 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1679 pr_info("AMD-Vi: virtual APIC enabled\n");
1683 static int __init amd_iommu_init_pci(void)
1685 struct amd_iommu *iommu;
1688 for_each_iommu(iommu) {
1689 ret = iommu_init_pci(iommu);
1695 * Order is important here to make sure any unity map requirements are
1696 * fulfilled. The unity mappings are created and written to the device
1697 * table during the amd_iommu_init_api() call.
1699 * After that we call init_device_table_dma() to make sure any
1700 * uninitialized DTE will block DMA, and in the end we flush the caches
1701 * of all IOMMUs to make sure the changes to the device table are
1704 ret = amd_iommu_init_api();
1706 init_device_table_dma();
1708 for_each_iommu(iommu)
1709 iommu_flush_all_caches(iommu);
1717 /****************************************************************************
1719 * The following functions initialize the MSI interrupts for all IOMMUs
1720 * in the system. It's a bit challenging because there could be multiple
1721 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1724 ****************************************************************************/
1726 static int iommu_setup_msi(struct amd_iommu *iommu)
1730 r = pci_enable_msi(iommu->dev);
1734 r = request_threaded_irq(iommu->dev->irq,
1735 amd_iommu_int_handler,
1736 amd_iommu_int_thread,
1741 pci_disable_msi(iommu->dev);
1745 iommu->int_enabled = true;
1750 static int iommu_init_msi(struct amd_iommu *iommu)
1754 if (iommu->int_enabled)
1757 if (iommu->dev->msi_cap)
1758 ret = iommu_setup_msi(iommu);
1766 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1768 if (iommu->ppr_log != NULL)
1769 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1771 iommu_ga_log_enable(iommu);
1776 /****************************************************************************
1778 * The next functions belong to the third pass of parsing the ACPI
1779 * table. In this last pass the memory mapping requirements are
1780 * gathered (like exclusion and unity mapping ranges).
1782 ****************************************************************************/
1784 static void __init free_unity_maps(void)
1786 struct unity_map_entry *entry, *next;
1788 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1789 list_del(&entry->list);
1794 /* called when we find an exclusion range definition in ACPI */
1795 static int __init init_exclusion_range(struct ivmd_header *m)
1800 case ACPI_IVMD_TYPE:
1801 set_device_exclusion_range(m->devid, m);
1803 case ACPI_IVMD_TYPE_ALL:
1804 for (i = 0; i <= amd_iommu_last_bdf; ++i)
1805 set_device_exclusion_range(i, m);
1807 case ACPI_IVMD_TYPE_RANGE:
1808 for (i = m->devid; i <= m->aux; ++i)
1809 set_device_exclusion_range(i, m);
1818 /* called for unity map ACPI definition */
1819 static int __init init_unity_map_range(struct ivmd_header *m)
1821 struct unity_map_entry *e = NULL;
1824 e = kzalloc(sizeof(*e), GFP_KERNEL);
1832 case ACPI_IVMD_TYPE:
1833 s = "IVMD_TYPEi\t\t\t";
1834 e->devid_start = e->devid_end = m->devid;
1836 case ACPI_IVMD_TYPE_ALL:
1837 s = "IVMD_TYPE_ALL\t\t";
1839 e->devid_end = amd_iommu_last_bdf;
1841 case ACPI_IVMD_TYPE_RANGE:
1842 s = "IVMD_TYPE_RANGE\t\t";
1843 e->devid_start = m->devid;
1844 e->devid_end = m->aux;
1847 e->address_start = PAGE_ALIGN(m->range_start);
1848 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1849 e->prot = m->flags >> 1;
1851 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1852 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1853 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1854 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
1855 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1856 e->address_start, e->address_end, m->flags);
1858 list_add_tail(&e->list, &amd_iommu_unity_map);
1863 /* iterates over all memory definitions we find in the ACPI table */
1864 static int __init init_memory_definitions(struct acpi_table_header *table)
1866 u8 *p = (u8 *)table, *end = (u8 *)table;
1867 struct ivmd_header *m;
1869 end += table->length;
1870 p += IVRS_HEADER_LENGTH;
1873 m = (struct ivmd_header *)p;
1874 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1875 init_exclusion_range(m);
1876 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1877 init_unity_map_range(m);
1886 * Init the device table to not allow DMA access for devices and
1887 * suppress all page faults
1889 static void init_device_table_dma(void)
1893 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1894 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1895 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
1899 static void __init uninit_device_table_dma(void)
1903 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1904 amd_iommu_dev_table[devid].data[0] = 0ULL;
1905 amd_iommu_dev_table[devid].data[1] = 0ULL;
1909 static void init_device_table(void)
1913 if (!amd_iommu_irq_remap)
1916 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1917 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1920 static void iommu_init_flags(struct amd_iommu *iommu)
1922 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1923 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1924 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1926 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1927 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1928 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1930 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1931 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1932 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1934 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1935 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1936 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1939 * make IOMMU memory accesses cache coherent
1941 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
1943 /* Set IOTLB invalidation timeout to 1s */
1944 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
1947 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
1950 u32 ioc_feature_control;
1951 struct pci_dev *pdev = iommu->root_pdev;
1953 /* RD890 BIOSes may not have completely reconfigured the iommu */
1954 if (!is_rd890_iommu(iommu->dev) || !pdev)
1958 * First, we need to ensure that the iommu is enabled. This is
1959 * controlled by a register in the northbridge
1962 /* Select Northbridge indirect register 0x75 and enable writing */
1963 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1964 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1966 /* Enable the iommu */
1967 if (!(ioc_feature_control & 0x1))
1968 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1970 /* Restore the iommu BAR */
1971 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1972 iommu->stored_addr_lo);
1973 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1974 iommu->stored_addr_hi);
1976 /* Restore the l1 indirect regs for each of the 6 l1s */
1977 for (i = 0; i < 6; i++)
1978 for (j = 0; j < 0x12; j++)
1979 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1981 /* Restore the l2 indirect regs */
1982 for (i = 0; i < 0x83; i++)
1983 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1985 /* Lock PCI setup registers */
1986 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1987 iommu->stored_addr_lo | 1);
1990 static void iommu_enable_ga(struct amd_iommu *iommu)
1992 #ifdef CONFIG_IRQ_REMAP
1993 switch (amd_iommu_guest_ir) {
1994 case AMD_IOMMU_GUEST_IR_VAPIC:
1995 iommu_feature_enable(iommu, CONTROL_GAM_EN);
1997 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
1998 iommu_feature_enable(iommu, CONTROL_GA_EN);
1999 iommu->irte_ops = &irte_128_ops;
2002 iommu->irte_ops = &irte_32_ops;
2009 * This function finally enables all IOMMUs found in the system after
2010 * they have been initialized
2012 static void early_enable_iommus(void)
2014 struct amd_iommu *iommu;
2016 for_each_iommu(iommu) {
2017 iommu_disable(iommu);
2018 iommu_init_flags(iommu);
2019 iommu_set_device_table(iommu);
2020 iommu_enable_command_buffer(iommu);
2021 iommu_enable_event_buffer(iommu);
2022 iommu_set_exclusion_range(iommu);
2023 iommu_enable_ga(iommu);
2024 iommu_enable(iommu);
2025 iommu_flush_all_caches(iommu);
2028 #ifdef CONFIG_IRQ_REMAP
2029 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2030 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2034 static void enable_iommus_v2(void)
2036 struct amd_iommu *iommu;
2038 for_each_iommu(iommu) {
2039 iommu_enable_ppr_log(iommu);
2040 iommu_enable_gt(iommu);
2044 static void enable_iommus(void)
2046 early_enable_iommus();
2051 static void disable_iommus(void)
2053 struct amd_iommu *iommu;
2055 for_each_iommu(iommu)
2056 iommu_disable(iommu);
2058 #ifdef CONFIG_IRQ_REMAP
2059 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2060 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2065 * Suspend/Resume support
2066 * disable suspend until real resume implemented
2069 static void amd_iommu_resume(void)
2071 struct amd_iommu *iommu;
2073 for_each_iommu(iommu)
2074 iommu_apply_resume_quirks(iommu);
2076 /* re-load the hardware */
2079 amd_iommu_enable_interrupts();
2082 static int amd_iommu_suspend(void)
2084 /* disable IOMMUs to go out of the way for BIOS */
2090 static struct syscore_ops amd_iommu_syscore_ops = {
2091 .suspend = amd_iommu_suspend,
2092 .resume = amd_iommu_resume,
2095 static void __init free_on_init_error(void)
2097 free_pages((unsigned long)irq_lookup_table,
2098 get_order(rlookup_table_size));
2100 kmem_cache_destroy(amd_iommu_irq_cache);
2101 amd_iommu_irq_cache = NULL;
2103 free_pages((unsigned long)amd_iommu_rlookup_table,
2104 get_order(rlookup_table_size));
2106 free_pages((unsigned long)amd_iommu_alias_table,
2107 get_order(alias_table_size));
2109 free_pages((unsigned long)amd_iommu_dev_table,
2110 get_order(dev_table_size));
2114 #ifdef CONFIG_GART_IOMMU
2116 * We failed to initialize the AMD IOMMU - try fallback to GART
2124 /* SB IOAPIC is always on this device in AMD systems */
2125 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2127 static bool __init check_ioapic_information(void)
2129 const char *fw_bug = FW_BUG;
2130 bool ret, has_sb_ioapic;
2133 has_sb_ioapic = false;
2137 * If we have map overrides on the kernel command line the
2138 * messages in this function might not describe firmware bugs
2139 * anymore - so be careful
2144 for (idx = 0; idx < nr_ioapics; idx++) {
2145 int devid, id = mpc_ioapic_id(idx);
2147 devid = get_ioapic_devid(id);
2149 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2152 } else if (devid == IOAPIC_SB_DEVID) {
2153 has_sb_ioapic = true;
2158 if (!has_sb_ioapic) {
2160 * We expect the SB IOAPIC to be listed in the IVRS
2161 * table. The system timer is connected to the SB IOAPIC
2162 * and if we don't have it in the list the system will
2163 * panic at boot time. This situation usually happens
2164 * when the BIOS is buggy and provides us the wrong
2165 * device id for the IOAPIC in the system.
2167 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
2171 pr_err("AMD-Vi: Disabling interrupt remapping\n");
2176 static void __init free_dma_resources(void)
2178 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2179 get_order(MAX_DOMAIN_ID/8));
2185 * This is the hardware init function for AMD IOMMU in the system.
2186 * This function is called either from amd_iommu_init or from the interrupt
2187 * remapping setup code.
2189 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2192 * 1 pass) Discover the most comprehensive IVHD type to use.
2194 * 2 pass) Find the highest PCI device id the driver has to handle.
2195 * Upon this information the size of the data structures is
2196 * determined that needs to be allocated.
2198 * 3 pass) Initialize the data structures just allocated with the
2199 * information in the ACPI table about available AMD IOMMUs
2200 * in the system. It also maps the PCI devices in the
2201 * system to specific IOMMUs
2203 * 4 pass) After the basic data structures are allocated and
2204 * initialized we update them with information about memory
2205 * remapping requirements parsed out of the ACPI table in
2208 * After everything is set up the IOMMUs are enabled and the necessary
2209 * hotplug and suspend notifiers are registered.
2211 static int __init early_amd_iommu_init(void)
2213 struct acpi_table_header *ivrs_base;
2214 acpi_size ivrs_size;
2216 int i, remap_cache_sz, ret = 0;
2218 if (!amd_iommu_detected)
2221 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2222 if (status == AE_NOT_FOUND)
2224 else if (ACPI_FAILURE(status)) {
2225 const char *err = acpi_format_exception(status);
2226 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2231 * Validate checksum here so we don't need to do it when
2232 * we actually parse the table
2234 ret = check_ivrs_checksum(ivrs_base);
2238 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2239 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2242 * First parse ACPI tables to find the largest Bus/Dev/Func
2243 * we need to handle. Upon this information the shared data
2244 * structures for the IOMMUs in the system will be allocated
2246 ret = find_last_devid_acpi(ivrs_base);
2250 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2251 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2252 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
2254 /* Device table - directly used by all IOMMUs */
2256 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2257 get_order(dev_table_size));
2258 if (amd_iommu_dev_table == NULL)
2262 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2263 * IOMMU see for that device
2265 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2266 get_order(alias_table_size));
2267 if (amd_iommu_alias_table == NULL)
2270 /* IOMMU rlookup table - find the IOMMU for a specific device */
2271 amd_iommu_rlookup_table = (void *)__get_free_pages(
2272 GFP_KERNEL | __GFP_ZERO,
2273 get_order(rlookup_table_size));
2274 if (amd_iommu_rlookup_table == NULL)
2277 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2278 GFP_KERNEL | __GFP_ZERO,
2279 get_order(MAX_DOMAIN_ID/8));
2280 if (amd_iommu_pd_alloc_bitmap == NULL)
2284 * let all alias entries point to itself
2286 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2287 amd_iommu_alias_table[i] = i;
2290 * never allocate domain 0 because its used as the non-allocated and
2291 * error value placeholder
2293 __set_bit(0, amd_iommu_pd_alloc_bitmap);
2295 spin_lock_init(&amd_iommu_pd_lock);
2298 * now the data structures are allocated and basically initialized
2299 * start the real acpi table scan
2301 ret = init_iommu_all(ivrs_base);
2305 if (amd_iommu_irq_remap)
2306 amd_iommu_irq_remap = check_ioapic_information();
2308 if (amd_iommu_irq_remap) {
2310 * Interrupt remapping enabled, create kmem_cache for the
2314 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2315 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2317 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
2318 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
2320 IRQ_TABLE_ALIGNMENT,
2322 if (!amd_iommu_irq_cache)
2325 irq_lookup_table = (void *)__get_free_pages(
2326 GFP_KERNEL | __GFP_ZERO,
2327 get_order(rlookup_table_size));
2328 if (!irq_lookup_table)
2332 ret = init_memory_definitions(ivrs_base);
2336 /* init the device table */
2337 init_device_table();
2340 /* Don't leak any ACPI memory */
2341 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2347 static int amd_iommu_enable_interrupts(void)
2349 struct amd_iommu *iommu;
2352 for_each_iommu(iommu) {
2353 ret = iommu_init_msi(iommu);
2362 static bool detect_ivrs(void)
2364 struct acpi_table_header *ivrs_base;
2365 acpi_size ivrs_size;
2368 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2369 if (status == AE_NOT_FOUND)
2371 else if (ACPI_FAILURE(status)) {
2372 const char *err = acpi_format_exception(status);
2373 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2377 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2379 /* Make sure ACS will be enabled during PCI probe */
2385 /****************************************************************************
2387 * AMD IOMMU Initialization State Machine
2389 ****************************************************************************/
2391 static int __init state_next(void)
2395 switch (init_state) {
2396 case IOMMU_START_STATE:
2397 if (!detect_ivrs()) {
2398 init_state = IOMMU_NOT_FOUND;
2401 init_state = IOMMU_IVRS_DETECTED;
2404 case IOMMU_IVRS_DETECTED:
2405 ret = early_amd_iommu_init();
2406 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2408 case IOMMU_ACPI_FINISHED:
2409 early_enable_iommus();
2410 register_syscore_ops(&amd_iommu_syscore_ops);
2411 x86_platform.iommu_shutdown = disable_iommus;
2412 init_state = IOMMU_ENABLED;
2415 ret = amd_iommu_init_pci();
2416 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2419 case IOMMU_PCI_INIT:
2420 ret = amd_iommu_enable_interrupts();
2421 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2423 case IOMMU_INTERRUPTS_EN:
2424 ret = amd_iommu_init_dma_ops();
2425 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2428 init_state = IOMMU_INITIALIZED;
2430 case IOMMU_INITIALIZED:
2433 case IOMMU_NOT_FOUND:
2434 case IOMMU_INIT_ERROR:
2435 /* Error states => do nothing */
2446 static int __init iommu_go_to_state(enum iommu_init_state state)
2450 while (init_state != state) {
2452 if (init_state == IOMMU_NOT_FOUND ||
2453 init_state == IOMMU_INIT_ERROR)
2460 #ifdef CONFIG_IRQ_REMAP
2461 int __init amd_iommu_prepare(void)
2465 amd_iommu_irq_remap = true;
2467 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2470 return amd_iommu_irq_remap ? 0 : -ENODEV;
2473 int __init amd_iommu_enable(void)
2477 ret = iommu_go_to_state(IOMMU_ENABLED);
2481 irq_remapping_enabled = 1;
2486 void amd_iommu_disable(void)
2488 amd_iommu_suspend();
2491 int amd_iommu_reenable(int mode)
2498 int __init amd_iommu_enable_faulting(void)
2500 /* We enable MSI later when PCI is initialized */
2506 * This is the core init function for AMD IOMMU hardware in the system.
2507 * This function is called from the generic x86 DMA layer initialization
2510 static int __init amd_iommu_init(void)
2514 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2516 free_dma_resources();
2517 if (!irq_remapping_enabled) {
2519 free_on_init_error();
2521 struct amd_iommu *iommu;
2523 uninit_device_table_dma();
2524 for_each_iommu(iommu)
2525 iommu_flush_all_caches(iommu);
2532 /****************************************************************************
2534 * Early detect code. This code runs at IOMMU detection time in the DMA
2535 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2538 ****************************************************************************/
2539 int __init amd_iommu_detect(void)
2543 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
2546 if (amd_iommu_disabled)
2549 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2553 amd_iommu_detected = true;
2555 x86_init.iommu.iommu_init = amd_iommu_init;
2560 /****************************************************************************
2562 * Parsing functions for the AMD IOMMU specific kernel command line
2565 ****************************************************************************/
2567 static int __init parse_amd_iommu_dump(char *str)
2569 amd_iommu_dump = true;
2574 static int __init parse_amd_iommu_intr(char *str)
2576 for (; *str; ++str) {
2577 if (strncmp(str, "legacy", 6) == 0) {
2578 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2581 if (strncmp(str, "vapic", 5) == 0) {
2582 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2589 static int __init parse_amd_iommu_options(char *str)
2591 for (; *str; ++str) {
2592 if (strncmp(str, "fullflush", 9) == 0)
2593 amd_iommu_unmap_flush = true;
2594 if (strncmp(str, "off", 3) == 0)
2595 amd_iommu_disabled = true;
2596 if (strncmp(str, "force_isolation", 15) == 0)
2597 amd_iommu_force_isolation = true;
2603 static int __init parse_ivrs_ioapic(char *str)
2605 unsigned int bus, dev, fn;
2609 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2612 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2616 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2617 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2622 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2624 cmdline_maps = true;
2625 i = early_ioapic_map_size++;
2626 early_ioapic_map[i].id = id;
2627 early_ioapic_map[i].devid = devid;
2628 early_ioapic_map[i].cmd_line = true;
2633 static int __init parse_ivrs_hpet(char *str)
2635 unsigned int bus, dev, fn;
2639 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2642 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2646 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2647 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2652 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2654 cmdline_maps = true;
2655 i = early_hpet_map_size++;
2656 early_hpet_map[i].id = id;
2657 early_hpet_map[i].devid = devid;
2658 early_hpet_map[i].cmd_line = true;
2663 static int __init parse_ivrs_acpihid(char *str)
2666 char *hid, *uid, *p;
2667 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2670 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2672 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2677 hid = strsep(&p, ":");
2680 if (!hid || !(*hid) || !uid) {
2681 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2685 i = early_acpihid_map_size++;
2686 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2687 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2688 early_acpihid_map[i].devid =
2689 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2690 early_acpihid_map[i].cmd_line = true;
2695 __setup("amd_iommu_dump", parse_amd_iommu_dump);
2696 __setup("amd_iommu=", parse_amd_iommu_options);
2697 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
2698 __setup("ivrs_ioapic", parse_ivrs_ioapic);
2699 __setup("ivrs_hpet", parse_ivrs_hpet);
2700 __setup("ivrs_acpihid", parse_ivrs_acpihid);
2702 IOMMU_INIT_FINISH(amd_iommu_detect,
2703 gart_iommu_hole_init,
2707 bool amd_iommu_v2_supported(void)
2709 return amd_iommu_v2_present;
2711 EXPORT_SYMBOL(amd_iommu_v2_supported);
2713 /****************************************************************************
2715 * IOMMU EFR Performance Counter support functionality. This code allows
2716 * access to the IOMMU PC functionality.
2718 ****************************************************************************/
2720 u8 amd_iommu_pc_get_max_banks(u16 devid)
2722 struct amd_iommu *iommu;
2725 /* locate the iommu governing the devid */
2726 iommu = amd_iommu_rlookup_table[devid];
2728 ret = iommu->max_banks;
2732 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2734 bool amd_iommu_pc_supported(void)
2736 return amd_iommu_pc_present;
2738 EXPORT_SYMBOL(amd_iommu_pc_supported);
2740 u8 amd_iommu_pc_get_max_counters(u16 devid)
2742 struct amd_iommu *iommu;
2745 /* locate the iommu governing the devid */
2746 iommu = amd_iommu_rlookup_table[devid];
2748 ret = iommu->max_counters;
2752 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2754 static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
2755 u8 bank, u8 cntr, u8 fxn,
2756 u64 *value, bool is_write)
2761 /* Check for valid iommu and pc register indexing */
2762 if (WARN_ON((fxn > 0x28) || (fxn & 7)))
2765 offset = (u32)(((0x40|bank) << 12) | (cntr << 8) | fxn);
2767 /* Limit the offset to the hw defined mmio region aperture */
2768 max_offset_lim = (u32)(((0x40|iommu->max_banks) << 12) |
2769 (iommu->max_counters << 8) | 0x28);
2770 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2771 (offset > max_offset_lim))
2775 writel((u32)*value, iommu->mmio_base + offset);
2776 writel((*value >> 32), iommu->mmio_base + offset + 4);
2778 *value = readl(iommu->mmio_base + offset + 4);
2780 *value = readl(iommu->mmio_base + offset);
2785 EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val);
2787 int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn,
2788 u64 *value, bool is_write)
2790 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
2792 /* Make sure the IOMMU PC resource is available */
2793 if (!amd_iommu_pc_present || iommu == NULL)
2796 return iommu_pc_get_set_reg_val(iommu, bank, cntr, fxn,