1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
8 #define pr_fmt(fmt) "AMD-Vi: " fmt
9 #define dev_fmt(fmt) pr_fmt(fmt)
11 #include <linux/pci.h>
12 #include <linux/acpi.h>
13 #include <linux/list.h>
14 #include <linux/bitmap.h>
15 #include <linux/slab.h>
16 #include <linux/syscore_ops.h>
17 #include <linux/interrupt.h>
18 #include <linux/msi.h>
19 #include <linux/irq.h>
20 #include <linux/amd-iommu.h>
21 #include <linux/export.h>
22 #include <linux/kmemleak.h>
23 #include <linux/cc_platform.h>
24 #include <linux/iopoll.h>
25 #include <asm/pci-direct.h>
26 #include <asm/iommu.h>
29 #include <asm/x86_init.h>
30 #include <asm/io_apic.h>
31 #include <asm/irq_remapping.h>
32 #include <asm/set_memory.h>
35 #include <linux/crash_dump.h>
37 #include "amd_iommu.h"
38 #include "../irq_remapping.h"
39 #include "../iommu-pages.h"
42 * definitions for the ACPI scanning code
44 #define IVRS_HEADER_LENGTH 48
46 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
47 #define ACPI_IVMD_TYPE_ALL 0x20
48 #define ACPI_IVMD_TYPE 0x21
49 #define ACPI_IVMD_TYPE_RANGE 0x22
51 #define IVHD_DEV_ALL 0x01
52 #define IVHD_DEV_SELECT 0x02
53 #define IVHD_DEV_SELECT_RANGE_START 0x03
54 #define IVHD_DEV_RANGE_END 0x04
55 #define IVHD_DEV_ALIAS 0x42
56 #define IVHD_DEV_ALIAS_RANGE 0x43
57 #define IVHD_DEV_EXT_SELECT 0x46
58 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
59 #define IVHD_DEV_SPECIAL 0x48
60 #define IVHD_DEV_ACPI_HID 0xf0
62 #define UID_NOT_PRESENT 0
63 #define UID_IS_INTEGER 1
64 #define UID_IS_CHARACTER 2
66 #define IVHD_SPECIAL_IOAPIC 1
67 #define IVHD_SPECIAL_HPET 2
69 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
70 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
71 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
72 #define IVHD_FLAG_ISOC_EN_MASK 0x08
74 #define IVMD_FLAG_EXCL_RANGE 0x08
75 #define IVMD_FLAG_IW 0x04
76 #define IVMD_FLAG_IR 0x02
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 IVRS_GET_SBDF_ID(seg, bus, dev, fn) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \
89 | ((dev & 0x1f) << 3) | (fn & 0x7))
92 * ACPI table definitions
94 * These data structures are laid over the table to parse the important values
99 * structure describing one IOMMU in the ACPI table. Typically followed by one
100 * or more ivhd_entrys.
113 /* Following only valid on IVHD type 11h and 40h */
114 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
116 } __attribute__((packed));
119 * A device entry describing which devices a specific IOMMU translates and
120 * which requestor ids they use.
126 struct_group(ext_hid,
134 } __attribute__((packed));
137 * An AMD IOMMU memory definition structure. It defines things like exclusion
138 * ranges for devices and regions that should be unity mapped.
150 } __attribute__((packed));
153 bool amd_iommu_irq_remap __read_mostly;
155 enum io_pgtable_fmt amd_iommu_pgtable = AMD_IOMMU_V1;
156 /* Guest page table level */
157 int amd_iommu_gpt_level = PAGE_MODE_4_LEVEL;
159 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
160 static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
162 static bool amd_iommu_detected;
163 static bool amd_iommu_disabled __initdata;
164 static bool amd_iommu_force_enable __initdata;
165 static bool amd_iommu_irtcachedis;
166 static int amd_iommu_target_ivhd_type;
168 /* Global EFR and EFR2 registers */
172 /* SNP is enabled on the system? */
173 bool amd_iommu_snp_en;
174 EXPORT_SYMBOL(amd_iommu_snp_en);
176 LIST_HEAD(amd_iommu_pci_seg_list); /* list of all PCI segments */
177 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
180 /* Array to assign indices to IOMMUs*/
181 struct amd_iommu *amd_iommus[MAX_IOMMUS];
183 /* Number of IOMMUs present in the system */
184 static int amd_iommus_present;
186 /* IOMMUs have a non-present cache? */
187 bool amd_iommu_np_cache __read_mostly;
188 bool amd_iommu_iotlb_sup __read_mostly = true;
190 static bool amd_iommu_pc_present __read_mostly;
191 bool amdr_ivrs_remap_support __read_mostly;
193 bool amd_iommu_force_isolation __read_mostly;
196 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
197 * to know which ones are already in use.
199 unsigned long *amd_iommu_pd_alloc_bitmap;
201 enum iommu_init_state {
211 IOMMU_CMDLINE_DISABLED,
214 /* Early ioapic and hpet maps from kernel command line */
215 #define EARLY_MAP_SIZE 4
216 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
217 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
218 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
220 static int __initdata early_ioapic_map_size;
221 static int __initdata early_hpet_map_size;
222 static int __initdata early_acpihid_map_size;
224 static bool __initdata cmdline_maps;
226 static enum iommu_init_state init_state = IOMMU_START_STATE;
228 static int amd_iommu_enable_interrupts(void);
229 static int __init iommu_go_to_state(enum iommu_init_state state);
230 static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg);
232 static bool amd_iommu_pre_enabled = true;
234 static u32 amd_iommu_ivinfo __initdata;
236 bool translation_pre_enabled(struct amd_iommu *iommu)
238 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
241 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
243 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
246 static void init_translation_status(struct amd_iommu *iommu)
250 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
251 if (ctrl & (1<<CONTROL_IOMMU_EN))
252 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
255 static inline unsigned long tbl_size(int entry_size, int last_bdf)
257 unsigned shift = PAGE_SHIFT +
258 get_order((last_bdf + 1) * entry_size);
263 int amd_iommu_get_num_iommus(void)
265 return amd_iommus_present;
269 * Iterate through all the IOMMUs to get common EFR
270 * masks among all IOMMUs and warn if found inconsistency.
272 static __init void get_global_efr(void)
274 struct amd_iommu *iommu;
276 for_each_iommu(iommu) {
277 u64 tmp = iommu->features;
278 u64 tmp2 = iommu->features2;
280 if (list_is_first(&iommu->list, &amd_iommu_list)) {
282 amd_iommu_efr2 = tmp2;
286 if (amd_iommu_efr == tmp &&
287 amd_iommu_efr2 == tmp2)
291 "Found inconsistent EFR/EFR2 %#llx,%#llx (global %#llx,%#llx) on iommu%d (%04x:%02x:%02x.%01x).\n",
292 tmp, tmp2, amd_iommu_efr, amd_iommu_efr2,
293 iommu->index, iommu->pci_seg->id,
294 PCI_BUS_NUM(iommu->devid), PCI_SLOT(iommu->devid),
295 PCI_FUNC(iommu->devid));
297 amd_iommu_efr &= tmp;
298 amd_iommu_efr2 &= tmp2;
301 pr_info("Using global IVHD EFR:%#llx, EFR2:%#llx\n", amd_iommu_efr, amd_iommu_efr2);
305 * For IVHD type 0x11/0x40, EFR is also available via IVHD.
306 * Default to IVHD EFR since it is available sooner
307 * (i.e. before PCI init).
309 static void __init early_iommu_features_init(struct amd_iommu *iommu,
310 struct ivhd_header *h)
312 if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP) {
313 iommu->features = h->efr_reg;
314 iommu->features2 = h->efr_reg2;
316 if (amd_iommu_ivinfo & IOMMU_IVINFO_DMA_REMAP)
317 amdr_ivrs_remap_support = true;
320 /* Access to l1 and l2 indexed register spaces */
322 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
326 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
327 pci_read_config_dword(iommu->dev, 0xfc, &val);
331 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
333 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
334 pci_write_config_dword(iommu->dev, 0xfc, val);
335 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
338 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
342 pci_write_config_dword(iommu->dev, 0xf0, address);
343 pci_read_config_dword(iommu->dev, 0xf4, &val);
347 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
349 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
350 pci_write_config_dword(iommu->dev, 0xf4, val);
353 /****************************************************************************
355 * AMD IOMMU MMIO register space handling functions
357 * These functions are used to program the IOMMU device registers in
358 * MMIO space required for that driver.
360 ****************************************************************************/
363 * This function set the exclusion range in the IOMMU. DMA accesses to the
364 * exclusion range are passed through untranslated
366 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
368 u64 start = iommu->exclusion_start & PAGE_MASK;
369 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
372 if (!iommu->exclusion_start)
375 entry = start | MMIO_EXCL_ENABLE_MASK;
376 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
377 &entry, sizeof(entry));
380 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
381 &entry, sizeof(entry));
384 static void iommu_set_cwwb_range(struct amd_iommu *iommu)
386 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
387 u64 entry = start & PM_ADDR_MASK;
389 if (!check_feature(FEATURE_SNP))
393 * Re-purpose Exclusion base/limit registers for Completion wait
394 * write-back base/limit.
396 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
397 &entry, sizeof(entry));
400 * Default to 4 Kbytes, which can be specified by setting base
401 * address equal to the limit address.
403 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
404 &entry, sizeof(entry));
407 /* Programs the physical address of the device table into the IOMMU hardware */
408 static void iommu_set_device_table(struct amd_iommu *iommu)
411 u32 dev_table_size = iommu->pci_seg->dev_table_size;
412 void *dev_table = (void *)get_dev_table(iommu);
414 BUG_ON(iommu->mmio_base == NULL);
416 entry = iommu_virt_to_phys(dev_table);
417 entry |= (dev_table_size >> 12) - 1;
418 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
419 &entry, sizeof(entry));
422 /* Generic functions to enable/disable certain features of the IOMMU. */
423 void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
427 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
428 ctrl |= (1ULL << bit);
429 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
432 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
436 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
437 ctrl &= ~(1ULL << bit);
438 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
441 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
445 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
446 ctrl &= ~CTRL_INV_TO_MASK;
447 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
448 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
451 /* Function to enable the hardware */
452 static void iommu_enable(struct amd_iommu *iommu)
454 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
457 static void iommu_disable(struct amd_iommu *iommu)
459 if (!iommu->mmio_base)
462 /* Disable command buffer */
463 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
465 /* Disable event logging and event interrupts */
466 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
467 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
469 /* Disable IOMMU GA_LOG */
470 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
471 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
473 /* Disable IOMMU PPR logging */
474 iommu_feature_disable(iommu, CONTROL_PPRLOG_EN);
475 iommu_feature_disable(iommu, CONTROL_PPRINT_EN);
477 /* Disable IOMMU hardware itself */
478 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
480 /* Clear IRTE cache disabling bit */
481 iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
485 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
486 * the system has one.
488 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
490 if (!request_mem_region(address, end, "amd_iommu")) {
491 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
493 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
497 return (u8 __iomem *)ioremap(address, end);
500 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
502 if (iommu->mmio_base)
503 iounmap(iommu->mmio_base);
504 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
507 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
523 /****************************************************************************
525 * The functions below belong to the first pass of AMD IOMMU ACPI table
526 * parsing. In this pass we try to find out the highest device id this
527 * code has to handle. Upon this information the size of the shared data
528 * structures is determined later.
530 ****************************************************************************/
533 * This function calculates the length of a given IVHD entry
535 static inline int ivhd_entry_length(u8 *ivhd)
537 u32 type = ((struct ivhd_entry *)ivhd)->type;
540 return 0x04 << (*ivhd >> 6);
541 } else if (type == IVHD_DEV_ACPI_HID) {
542 /* For ACPI_HID, offset 21 is uid len */
543 return *((u8 *)ivhd + 21) + 22;
549 * After reading the highest device id from the IOMMU PCI capability header
550 * this function looks if there is a higher device id defined in the ACPI table
552 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
554 u8 *p = (void *)h, *end = (void *)h;
555 struct ivhd_entry *dev;
556 int last_devid = -EINVAL;
558 u32 ivhd_size = get_ivhd_header_size(h);
561 pr_err("Unsupported IVHD type %#x\n", h->type);
569 dev = (struct ivhd_entry *)p;
572 /* Use maximum BDF value for DEV_ALL */
574 case IVHD_DEV_SELECT:
575 case IVHD_DEV_RANGE_END:
577 case IVHD_DEV_EXT_SELECT:
578 /* all the above subfield types refer to device ids */
579 if (dev->devid > last_devid)
580 last_devid = dev->devid;
585 p += ivhd_entry_length(p);
593 static int __init check_ivrs_checksum(struct acpi_table_header *table)
596 u8 checksum = 0, *p = (u8 *)table;
598 for (i = 0; i < table->length; ++i)
601 /* ACPI table corrupt */
602 pr_err(FW_BUG "IVRS invalid checksum\n");
610 * Iterate over all IVHD entries in the ACPI table and find the highest device
611 * id which we need to handle. This is the first of three functions which parse
612 * the ACPI table. So we check the checksum here.
614 static int __init find_last_devid_acpi(struct acpi_table_header *table, u16 pci_seg)
616 u8 *p = (u8 *)table, *end = (u8 *)table;
617 struct ivhd_header *h;
618 int last_devid, last_bdf = 0;
620 p += IVRS_HEADER_LENGTH;
622 end += table->length;
624 h = (struct ivhd_header *)p;
625 if (h->pci_seg == pci_seg &&
626 h->type == amd_iommu_target_ivhd_type) {
627 last_devid = find_last_devid_from_ivhd(h);
631 if (last_devid > last_bdf)
632 last_bdf = last_devid;
641 /****************************************************************************
643 * The following functions belong to the code path which parses the ACPI table
644 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
645 * data structures, initialize the per PCI segment device/alias/rlookup table
646 * and also basically initialize the hardware.
648 ****************************************************************************/
650 /* Allocate per PCI segment device table */
651 static inline int __init alloc_dev_table(struct amd_iommu_pci_seg *pci_seg)
653 pci_seg->dev_table = iommu_alloc_pages(GFP_KERNEL | GFP_DMA32,
654 get_order(pci_seg->dev_table_size));
655 if (!pci_seg->dev_table)
661 static inline void free_dev_table(struct amd_iommu_pci_seg *pci_seg)
663 iommu_free_pages(pci_seg->dev_table,
664 get_order(pci_seg->dev_table_size));
665 pci_seg->dev_table = NULL;
668 /* Allocate per PCI segment IOMMU rlookup table. */
669 static inline int __init alloc_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
671 pci_seg->rlookup_table = iommu_alloc_pages(GFP_KERNEL,
672 get_order(pci_seg->rlookup_table_size));
673 if (pci_seg->rlookup_table == NULL)
679 static inline void free_rlookup_table(struct amd_iommu_pci_seg *pci_seg)
681 iommu_free_pages(pci_seg->rlookup_table,
682 get_order(pci_seg->rlookup_table_size));
683 pci_seg->rlookup_table = NULL;
686 static inline int __init alloc_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
688 pci_seg->irq_lookup_table = iommu_alloc_pages(GFP_KERNEL,
689 get_order(pci_seg->rlookup_table_size));
690 kmemleak_alloc(pci_seg->irq_lookup_table,
691 pci_seg->rlookup_table_size, 1, GFP_KERNEL);
692 if (pci_seg->irq_lookup_table == NULL)
698 static inline void free_irq_lookup_table(struct amd_iommu_pci_seg *pci_seg)
700 kmemleak_free(pci_seg->irq_lookup_table);
701 iommu_free_pages(pci_seg->irq_lookup_table,
702 get_order(pci_seg->rlookup_table_size));
703 pci_seg->irq_lookup_table = NULL;
706 static int __init alloc_alias_table(struct amd_iommu_pci_seg *pci_seg)
710 pci_seg->alias_table = iommu_alloc_pages(GFP_KERNEL,
711 get_order(pci_seg->alias_table_size));
712 if (!pci_seg->alias_table)
716 * let all alias entries point to itself
718 for (i = 0; i <= pci_seg->last_bdf; ++i)
719 pci_seg->alias_table[i] = i;
724 static void __init free_alias_table(struct amd_iommu_pci_seg *pci_seg)
726 iommu_free_pages(pci_seg->alias_table,
727 get_order(pci_seg->alias_table_size));
728 pci_seg->alias_table = NULL;
732 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
733 * write commands to that buffer later and the IOMMU will execute them
736 static int __init alloc_command_buffer(struct amd_iommu *iommu)
738 iommu->cmd_buf = iommu_alloc_pages(GFP_KERNEL,
739 get_order(CMD_BUFFER_SIZE));
741 return iommu->cmd_buf ? 0 : -ENOMEM;
745 * Interrupt handler has processed all pending events and adjusted head
746 * and tail pointer. Reset overflow mask and restart logging again.
748 void amd_iommu_restart_log(struct amd_iommu *iommu, const char *evt_type,
749 u8 cntrl_intr, u8 cntrl_log,
750 u32 status_run_mask, u32 status_overflow_mask)
754 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
755 if (status & status_run_mask)
758 pr_info_ratelimited("IOMMU %s log restarting\n", evt_type);
760 iommu_feature_disable(iommu, cntrl_log);
761 iommu_feature_disable(iommu, cntrl_intr);
763 writel(status_overflow_mask, iommu->mmio_base + MMIO_STATUS_OFFSET);
765 iommu_feature_enable(iommu, cntrl_intr);
766 iommu_feature_enable(iommu, cntrl_log);
770 * This function restarts event logging in case the IOMMU experienced
771 * an event log buffer overflow.
773 void amd_iommu_restart_event_logging(struct amd_iommu *iommu)
775 amd_iommu_restart_log(iommu, "Event", CONTROL_EVT_INT_EN,
776 CONTROL_EVT_LOG_EN, MMIO_STATUS_EVT_RUN_MASK,
777 MMIO_STATUS_EVT_OVERFLOW_MASK);
781 * This function restarts event logging in case the IOMMU experienced
784 void amd_iommu_restart_ga_log(struct amd_iommu *iommu)
786 amd_iommu_restart_log(iommu, "GA", CONTROL_GAINT_EN,
787 CONTROL_GALOG_EN, MMIO_STATUS_GALOG_RUN_MASK,
788 MMIO_STATUS_GALOG_OVERFLOW_MASK);
792 * This function resets the command buffer if the IOMMU stopped fetching
795 static void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
797 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
799 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
800 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
801 iommu->cmd_buf_head = 0;
802 iommu->cmd_buf_tail = 0;
804 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
808 * This function writes the command buffer address to the hardware and
811 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
815 BUG_ON(iommu->cmd_buf == NULL);
817 entry = iommu_virt_to_phys(iommu->cmd_buf);
818 entry |= MMIO_CMD_SIZE_512;
820 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
821 &entry, sizeof(entry));
823 amd_iommu_reset_cmd_buffer(iommu);
827 * This function disables the command buffer
829 static void iommu_disable_command_buffer(struct amd_iommu *iommu)
831 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
834 static void __init free_command_buffer(struct amd_iommu *iommu)
836 iommu_free_pages(iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
839 void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu, gfp_t gfp,
842 int order = get_order(size);
843 void *buf = iommu_alloc_pages(gfp, order);
846 check_feature(FEATURE_SNP) &&
847 set_memory_4k((unsigned long)buf, (1 << order))) {
848 iommu_free_pages(buf, order);
855 /* allocates the memory where the IOMMU will log its events to */
856 static int __init alloc_event_buffer(struct amd_iommu *iommu)
858 iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL,
861 return iommu->evt_buf ? 0 : -ENOMEM;
864 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
868 BUG_ON(iommu->evt_buf == NULL);
870 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
872 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
873 &entry, sizeof(entry));
875 /* set head and tail to zero manually */
876 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
877 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
879 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
883 * This function disables the event log buffer
885 static void iommu_disable_event_buffer(struct amd_iommu *iommu)
887 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
890 static void __init free_event_buffer(struct amd_iommu *iommu)
892 iommu_free_pages(iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
895 static void free_ga_log(struct amd_iommu *iommu)
897 #ifdef CONFIG_IRQ_REMAP
898 iommu_free_pages(iommu->ga_log, get_order(GA_LOG_SIZE));
899 iommu_free_pages(iommu->ga_log_tail, get_order(8));
903 #ifdef CONFIG_IRQ_REMAP
904 static int iommu_ga_log_enable(struct amd_iommu *iommu)
912 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
913 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
914 &entry, sizeof(entry));
915 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
916 (BIT_ULL(52)-1)) & ~7ULL;
917 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
918 &entry, sizeof(entry));
919 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
920 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
923 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
924 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
926 for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) {
927 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
928 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
933 if (WARN_ON(i >= MMIO_STATUS_TIMEOUT))
939 static int iommu_init_ga_log(struct amd_iommu *iommu)
941 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
944 iommu->ga_log = iommu_alloc_pages(GFP_KERNEL, get_order(GA_LOG_SIZE));
948 iommu->ga_log_tail = iommu_alloc_pages(GFP_KERNEL, get_order(8));
949 if (!iommu->ga_log_tail)
957 #endif /* CONFIG_IRQ_REMAP */
959 static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
961 iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL, 1);
963 return iommu->cmd_sem ? 0 : -ENOMEM;
966 static void __init free_cwwb_sem(struct amd_iommu *iommu)
969 iommu_free_page((void *)iommu->cmd_sem);
972 static void iommu_enable_xt(struct amd_iommu *iommu)
974 #ifdef CONFIG_IRQ_REMAP
976 * XT mode (32-bit APIC destination ID) requires
977 * GA mode (128-bit IRTE support) as a prerequisite.
979 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
980 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
981 iommu_feature_enable(iommu, CONTROL_XT_EN);
982 #endif /* CONFIG_IRQ_REMAP */
985 static void iommu_enable_gt(struct amd_iommu *iommu)
987 if (!check_feature(FEATURE_GT))
990 iommu_feature_enable(iommu, CONTROL_GT_EN);
993 /* sets a specific bit in the device table entry. */
994 static void __set_dev_entry_bit(struct dev_table_entry *dev_table,
997 int i = (bit >> 6) & 0x03;
998 int _bit = bit & 0x3f;
1000 dev_table[devid].data[i] |= (1UL << _bit);
1003 static void set_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
1005 struct dev_table_entry *dev_table = get_dev_table(iommu);
1007 return __set_dev_entry_bit(dev_table, devid, bit);
1010 static int __get_dev_entry_bit(struct dev_table_entry *dev_table,
1013 int i = (bit >> 6) & 0x03;
1014 int _bit = bit & 0x3f;
1016 return (dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
1019 static int get_dev_entry_bit(struct amd_iommu *iommu, u16 devid, u8 bit)
1021 struct dev_table_entry *dev_table = get_dev_table(iommu);
1023 return __get_dev_entry_bit(dev_table, devid, bit);
1026 static bool __copy_device_table(struct amd_iommu *iommu)
1028 u64 int_ctl, int_tab_len, entry = 0;
1029 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
1030 struct dev_table_entry *old_devtb = NULL;
1031 u32 lo, hi, devid, old_devtb_size;
1032 phys_addr_t old_devtb_phys;
1033 u16 dom_id, dte_v, irq_v;
1036 /* Each IOMMU use separate device table with the same size */
1037 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
1038 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
1039 entry = (((u64) hi) << 32) + lo;
1041 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
1042 if (old_devtb_size != pci_seg->dev_table_size) {
1043 pr_err("The device table size of IOMMU:%d is not expected!\n",
1049 * When SME is enabled in the first kernel, the entry includes the
1050 * memory encryption mask(sme_me_mask), we must remove the memory
1051 * encryption mask to obtain the true physical address in kdump kernel.
1053 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
1055 if (old_devtb_phys >= 0x100000000ULL) {
1056 pr_err("The address of old device table is above 4G, not trustworthy!\n");
1059 old_devtb = (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && is_kdump_kernel())
1060 ? (__force void *)ioremap_encrypted(old_devtb_phys,
1061 pci_seg->dev_table_size)
1062 : memremap(old_devtb_phys, pci_seg->dev_table_size, MEMREMAP_WB);
1067 pci_seg->old_dev_tbl_cpy = iommu_alloc_pages(GFP_KERNEL | GFP_DMA32,
1068 get_order(pci_seg->dev_table_size));
1069 if (pci_seg->old_dev_tbl_cpy == NULL) {
1070 pr_err("Failed to allocate memory for copying old device table!\n");
1071 memunmap(old_devtb);
1075 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
1076 pci_seg->old_dev_tbl_cpy[devid] = old_devtb[devid];
1077 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
1078 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
1080 if (dte_v && dom_id) {
1081 pci_seg->old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
1082 pci_seg->old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
1083 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
1084 /* If gcr3 table existed, mask it out */
1085 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
1086 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1087 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1088 pci_seg->old_dev_tbl_cpy[devid].data[1] &= ~tmp;
1089 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
1091 pci_seg->old_dev_tbl_cpy[devid].data[0] &= ~tmp;
1095 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
1096 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
1097 int_tab_len = old_devtb[devid].data[2] & DTE_INTTABLEN_MASK;
1098 if (irq_v && (int_ctl || int_tab_len)) {
1099 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
1100 (int_tab_len != DTE_INTTABLEN)) {
1101 pr_err("Wrong old irq remapping flag: %#x\n", devid);
1102 memunmap(old_devtb);
1106 pci_seg->old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
1109 memunmap(old_devtb);
1114 static bool copy_device_table(void)
1116 struct amd_iommu *iommu;
1117 struct amd_iommu_pci_seg *pci_seg;
1119 if (!amd_iommu_pre_enabled)
1122 pr_warn("Translation is already enabled - trying to copy translation structures\n");
1125 * All IOMMUs within PCI segment shares common device table.
1126 * Hence copy device table only once per PCI segment.
1128 for_each_pci_segment(pci_seg) {
1129 for_each_iommu(iommu) {
1130 if (pci_seg->id != iommu->pci_seg->id)
1132 if (!__copy_device_table(iommu))
1141 void amd_iommu_apply_erratum_63(struct amd_iommu *iommu, u16 devid)
1145 sysmgt = get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1) |
1146 (get_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2) << 1);
1149 set_dev_entry_bit(iommu, devid, DEV_ENTRY_IW);
1153 * This function takes the device specific flags read from the ACPI
1154 * table and sets up the device table entry with that information
1156 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
1157 u16 devid, u32 flags, u32 ext_flags)
1159 if (flags & ACPI_DEVFLAG_INITPASS)
1160 set_dev_entry_bit(iommu, devid, DEV_ENTRY_INIT_PASS);
1161 if (flags & ACPI_DEVFLAG_EXTINT)
1162 set_dev_entry_bit(iommu, devid, DEV_ENTRY_EINT_PASS);
1163 if (flags & ACPI_DEVFLAG_NMI)
1164 set_dev_entry_bit(iommu, devid, DEV_ENTRY_NMI_PASS);
1165 if (flags & ACPI_DEVFLAG_SYSMGT1)
1166 set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT1);
1167 if (flags & ACPI_DEVFLAG_SYSMGT2)
1168 set_dev_entry_bit(iommu, devid, DEV_ENTRY_SYSMGT2);
1169 if (flags & ACPI_DEVFLAG_LINT0)
1170 set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT0_PASS);
1171 if (flags & ACPI_DEVFLAG_LINT1)
1172 set_dev_entry_bit(iommu, devid, DEV_ENTRY_LINT1_PASS);
1174 amd_iommu_apply_erratum_63(iommu, devid);
1176 amd_iommu_set_rlookup_table(iommu, devid);
1179 int __init add_special_device(u8 type, u8 id, u32 *devid, bool cmd_line)
1181 struct devid_map *entry;
1182 struct list_head *list;
1184 if (type == IVHD_SPECIAL_IOAPIC)
1186 else if (type == IVHD_SPECIAL_HPET)
1191 list_for_each_entry(entry, list, list) {
1192 if (!(entry->id == id && entry->cmd_line))
1195 pr_info("Command-line override present for %s id %d - ignoring\n",
1196 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1198 *devid = entry->devid;
1203 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1208 entry->devid = *devid;
1209 entry->cmd_line = cmd_line;
1211 list_add_tail(&entry->list, list);
1216 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u32 *devid,
1219 struct acpihid_map_entry *entry;
1220 struct list_head *list = &acpihid_map;
1222 list_for_each_entry(entry, list, list) {
1223 if (strcmp(entry->hid, hid) ||
1224 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1228 pr_info("Command-line override for hid:%s uid:%s\n",
1230 *devid = entry->devid;
1234 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1238 memcpy(entry->uid, uid, strlen(uid));
1239 memcpy(entry->hid, hid, strlen(hid));
1240 entry->devid = *devid;
1241 entry->cmd_line = cmd_line;
1242 entry->root_devid = (entry->devid & (~0x7));
1244 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1245 entry->cmd_line ? "cmd" : "ivrs",
1246 entry->hid, entry->uid, entry->root_devid);
1248 list_add_tail(&entry->list, list);
1252 static int __init add_early_maps(void)
1256 for (i = 0; i < early_ioapic_map_size; ++i) {
1257 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1258 early_ioapic_map[i].id,
1259 &early_ioapic_map[i].devid,
1260 early_ioapic_map[i].cmd_line);
1265 for (i = 0; i < early_hpet_map_size; ++i) {
1266 ret = add_special_device(IVHD_SPECIAL_HPET,
1267 early_hpet_map[i].id,
1268 &early_hpet_map[i].devid,
1269 early_hpet_map[i].cmd_line);
1274 for (i = 0; i < early_acpihid_map_size; ++i) {
1275 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1276 early_acpihid_map[i].uid,
1277 &early_acpihid_map[i].devid,
1278 early_acpihid_map[i].cmd_line);
1287 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1288 * initializes the hardware and our data structures with it.
1290 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1291 struct ivhd_header *h)
1294 u8 *end = p, flags = 0;
1295 u16 devid = 0, devid_start = 0, devid_to = 0, seg_id;
1296 u32 dev_i, ext_flags = 0;
1298 struct ivhd_entry *e;
1299 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg;
1304 ret = add_early_maps();
1308 amd_iommu_apply_ivrs_quirks();
1311 * First save the recommended feature enable bits from ACPI
1313 iommu->acpi_flags = h->flags;
1316 * Done. Now parse the device entries
1318 ivhd_size = get_ivhd_header_size(h);
1320 pr_err("Unsupported IVHD type %#x\n", h->type);
1330 e = (struct ivhd_entry *)p;
1331 seg_id = pci_seg->id;
1336 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1338 for (dev_i = 0; dev_i <= pci_seg->last_bdf; ++dev_i)
1339 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1341 case IVHD_DEV_SELECT:
1343 DUMP_printk(" DEV_SELECT\t\t\t devid: %04x:%02x:%02x.%x "
1345 seg_id, PCI_BUS_NUM(e->devid),
1351 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1353 case IVHD_DEV_SELECT_RANGE_START:
1355 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1356 "devid: %04x:%02x:%02x.%x flags: %02x\n",
1357 seg_id, PCI_BUS_NUM(e->devid),
1362 devid_start = e->devid;
1367 case IVHD_DEV_ALIAS:
1369 DUMP_printk(" DEV_ALIAS\t\t\t devid: %04x:%02x:%02x.%x "
1370 "flags: %02x devid_to: %02x:%02x.%x\n",
1371 seg_id, PCI_BUS_NUM(e->devid),
1375 PCI_BUS_NUM(e->ext >> 8),
1376 PCI_SLOT(e->ext >> 8),
1377 PCI_FUNC(e->ext >> 8));
1380 devid_to = e->ext >> 8;
1381 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1382 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1383 pci_seg->alias_table[devid] = devid_to;
1385 case IVHD_DEV_ALIAS_RANGE:
1387 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1388 "devid: %04x:%02x:%02x.%x flags: %02x "
1389 "devid_to: %04x:%02x:%02x.%x\n",
1390 seg_id, PCI_BUS_NUM(e->devid),
1394 seg_id, PCI_BUS_NUM(e->ext >> 8),
1395 PCI_SLOT(e->ext >> 8),
1396 PCI_FUNC(e->ext >> 8));
1398 devid_start = e->devid;
1400 devid_to = e->ext >> 8;
1404 case IVHD_DEV_EXT_SELECT:
1406 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %04x:%02x:%02x.%x "
1407 "flags: %02x ext: %08x\n",
1408 seg_id, PCI_BUS_NUM(e->devid),
1414 set_dev_entry_from_acpi(iommu, devid, e->flags,
1417 case IVHD_DEV_EXT_SELECT_RANGE:
1419 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1420 "%04x:%02x:%02x.%x flags: %02x ext: %08x\n",
1421 seg_id, PCI_BUS_NUM(e->devid),
1426 devid_start = e->devid;
1431 case IVHD_DEV_RANGE_END:
1433 DUMP_printk(" DEV_RANGE_END\t\t devid: %04x:%02x:%02x.%x\n",
1434 seg_id, PCI_BUS_NUM(e->devid),
1436 PCI_FUNC(e->devid));
1439 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1441 pci_seg->alias_table[dev_i] = devid_to;
1442 set_dev_entry_from_acpi(iommu,
1443 devid_to, flags, ext_flags);
1445 set_dev_entry_from_acpi(iommu, dev_i,
1449 case IVHD_DEV_SPECIAL: {
1455 handle = e->ext & 0xff;
1456 devid = PCI_SEG_DEVID_TO_SBDF(seg_id, (e->ext >> 8));
1457 type = (e->ext >> 24) & 0xff;
1459 if (type == IVHD_SPECIAL_IOAPIC)
1461 else if (type == IVHD_SPECIAL_HPET)
1466 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %04x:%02x:%02x.%x\n",
1468 seg_id, PCI_BUS_NUM(devid),
1472 ret = add_special_device(type, handle, &devid, false);
1477 * add_special_device might update the devid in case a
1478 * command-line override is present. So call
1479 * set_dev_entry_from_acpi after add_special_device.
1481 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1485 case IVHD_DEV_ACPI_HID: {
1487 u8 hid[ACPIHID_HID_LEN];
1488 u8 uid[ACPIHID_UID_LEN];
1491 if (h->type != 0x40) {
1492 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1497 BUILD_BUG_ON(sizeof(e->ext_hid) != ACPIHID_HID_LEN - 1);
1498 memcpy(hid, &e->ext_hid, ACPIHID_HID_LEN - 1);
1499 hid[ACPIHID_HID_LEN - 1] = '\0';
1502 pr_err(FW_BUG "Invalid HID.\n");
1508 case UID_NOT_PRESENT:
1511 pr_warn(FW_BUG "Invalid UID length.\n");
1514 case UID_IS_INTEGER:
1516 sprintf(uid, "%d", e->uid);
1519 case UID_IS_CHARACTER:
1521 memcpy(uid, &e->uid, e->uidl);
1522 uid[e->uidl] = '\0';
1529 devid = PCI_SEG_DEVID_TO_SBDF(seg_id, e->devid);
1530 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %04x:%02x:%02x.%x\n",
1538 ret = add_acpi_hid_device(hid, uid, &devid, false);
1543 * add_special_device might update the devid in case a
1544 * command-line override is present. So call
1545 * set_dev_entry_from_acpi after add_special_device.
1547 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1555 p += ivhd_entry_length(p);
1561 /* Allocate PCI segment data structure */
1562 static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id,
1563 struct acpi_table_header *ivrs_base)
1565 struct amd_iommu_pci_seg *pci_seg;
1569 * First parse ACPI tables to find the largest Bus/Dev/Func we need to
1570 * handle in this PCI segment. Upon this information the shared data
1571 * structures for the PCI segments in the system will be allocated.
1573 last_bdf = find_last_devid_acpi(ivrs_base, id);
1577 pci_seg = kzalloc(sizeof(struct amd_iommu_pci_seg), GFP_KERNEL);
1578 if (pci_seg == NULL)
1581 pci_seg->last_bdf = last_bdf;
1582 DUMP_printk("PCI segment : 0x%0x, last bdf : 0x%04x\n", id, last_bdf);
1583 pci_seg->dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE, last_bdf);
1584 pci_seg->alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE, last_bdf);
1585 pci_seg->rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE, last_bdf);
1588 init_llist_head(&pci_seg->dev_data_list);
1589 INIT_LIST_HEAD(&pci_seg->unity_map);
1590 list_add_tail(&pci_seg->list, &amd_iommu_pci_seg_list);
1592 if (alloc_dev_table(pci_seg))
1594 if (alloc_alias_table(pci_seg))
1596 if (alloc_rlookup_table(pci_seg))
1602 static struct amd_iommu_pci_seg *__init get_pci_segment(u16 id,
1603 struct acpi_table_header *ivrs_base)
1605 struct amd_iommu_pci_seg *pci_seg;
1607 for_each_pci_segment(pci_seg) {
1608 if (pci_seg->id == id)
1612 return alloc_pci_segment(id, ivrs_base);
1615 static void __init free_pci_segments(void)
1617 struct amd_iommu_pci_seg *pci_seg, *next;
1619 for_each_pci_segment_safe(pci_seg, next) {
1620 list_del(&pci_seg->list);
1621 free_irq_lookup_table(pci_seg);
1622 free_rlookup_table(pci_seg);
1623 free_alias_table(pci_seg);
1624 free_dev_table(pci_seg);
1629 static void __init free_sysfs(struct amd_iommu *iommu)
1631 if (iommu->iommu.dev) {
1632 iommu_device_unregister(&iommu->iommu);
1633 iommu_device_sysfs_remove(&iommu->iommu);
1637 static void __init free_iommu_one(struct amd_iommu *iommu)
1640 free_cwwb_sem(iommu);
1641 free_command_buffer(iommu);
1642 free_event_buffer(iommu);
1643 amd_iommu_free_ppr_log(iommu);
1645 iommu_unmap_mmio_space(iommu);
1646 amd_iommu_iopf_uninit(iommu);
1649 static void __init free_iommu_all(void)
1651 struct amd_iommu *iommu, *next;
1653 for_each_iommu_safe(iommu, next) {
1654 list_del(&iommu->list);
1655 free_iommu_one(iommu);
1661 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1663 * BIOS should disable L2B micellaneous clock gating by setting
1664 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1666 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1670 if ((boot_cpu_data.x86 != 0x15) ||
1671 (boot_cpu_data.x86_model < 0x10) ||
1672 (boot_cpu_data.x86_model > 0x1f))
1675 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1676 pci_read_config_dword(iommu->dev, 0xf4, &value);
1681 /* Select NB indirect register 0x90 and enable writing */
1682 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1684 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1685 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
1687 /* Clear the enable writing bit */
1688 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1692 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1694 * BIOS should enable ATS write permission check by setting
1695 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1697 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1701 if ((boot_cpu_data.x86 != 0x15) ||
1702 (boot_cpu_data.x86_model < 0x30) ||
1703 (boot_cpu_data.x86_model > 0x3f))
1706 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1707 value = iommu_read_l2(iommu, 0x47);
1712 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1713 iommu_write_l2(iommu, 0x47, value | BIT(0));
1715 pci_info(iommu->dev, "Applying ATS write check workaround\n");
1719 * This function glues the initialization function for one IOMMU
1720 * together and also allocates the command buffer and programs the
1721 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1723 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h,
1724 struct acpi_table_header *ivrs_base)
1726 struct amd_iommu_pci_seg *pci_seg;
1728 pci_seg = get_pci_segment(h->pci_seg, ivrs_base);
1729 if (pci_seg == NULL)
1731 iommu->pci_seg = pci_seg;
1733 raw_spin_lock_init(&iommu->lock);
1734 atomic64_set(&iommu->cmd_sem_val, 0);
1736 /* Add IOMMU to internal data structures */
1737 list_add_tail(&iommu->list, &amd_iommu_list);
1738 iommu->index = amd_iommus_present++;
1740 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1741 WARN(1, "System has more IOMMUs than supported by this driver\n");
1745 /* Index is fine - add IOMMU to the array */
1746 amd_iommus[iommu->index] = iommu;
1749 * Copy data from ACPI table entry to the iommu struct
1751 iommu->devid = h->devid;
1752 iommu->cap_ptr = h->cap_ptr;
1753 iommu->mmio_phys = h->mmio_phys;
1757 /* Check if IVHD EFR contains proper max banks/counters */
1758 if ((h->efr_attr != 0) &&
1759 ((h->efr_attr & (0xF << 13)) != 0) &&
1760 ((h->efr_attr & (0x3F << 17)) != 0))
1761 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1763 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1766 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1767 * GAM also requires GA mode. Therefore, we need to
1768 * check cmpxchg16b support before enabling it.
1770 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1771 ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1772 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1776 if (h->efr_reg & (1 << 9))
1777 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1779 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1782 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1783 * XT, GAM also requires GA mode. Therefore, we need to
1784 * check cmpxchg16b support before enabling them.
1786 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1787 ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
1788 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1792 if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
1793 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1795 early_iommu_features_init(iommu, h);
1802 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1803 iommu->mmio_phys_end);
1804 if (!iommu->mmio_base)
1807 return init_iommu_from_acpi(iommu, h);
1810 static int __init init_iommu_one_late(struct amd_iommu *iommu)
1814 if (alloc_cwwb_sem(iommu))
1817 if (alloc_command_buffer(iommu))
1820 if (alloc_event_buffer(iommu))
1823 iommu->int_enabled = false;
1825 init_translation_status(iommu);
1826 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1827 iommu_disable(iommu);
1828 clear_translation_pre_enabled(iommu);
1829 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1832 if (amd_iommu_pre_enabled)
1833 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1835 if (amd_iommu_irq_remap) {
1836 ret = amd_iommu_create_irq_domain(iommu);
1842 * Make sure IOMMU is not considered to translate itself. The IVRS
1843 * table tells us so, but this is a lie!
1845 iommu->pci_seg->rlookup_table[iommu->devid] = NULL;
1851 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1852 * @ivrs: Pointer to the IVRS header
1854 * This function search through all IVDB of the maximum supported IVHD
1856 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1858 u8 *base = (u8 *)ivrs;
1859 struct ivhd_header *ivhd = (struct ivhd_header *)
1860 (base + IVRS_HEADER_LENGTH);
1861 u8 last_type = ivhd->type;
1862 u16 devid = ivhd->devid;
1864 while (((u8 *)ivhd - base < ivrs->length) &&
1865 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1866 u8 *p = (u8 *) ivhd;
1868 if (ivhd->devid == devid)
1869 last_type = ivhd->type;
1870 ivhd = (struct ivhd_header *)(p + ivhd->length);
1877 * Iterates over all IOMMU entries in the ACPI table, allocates the
1878 * IOMMU structure and initializes it with init_iommu_one()
1880 static int __init init_iommu_all(struct acpi_table_header *table)
1882 u8 *p = (u8 *)table, *end = (u8 *)table;
1883 struct ivhd_header *h;
1884 struct amd_iommu *iommu;
1887 end += table->length;
1888 p += IVRS_HEADER_LENGTH;
1890 /* Phase 1: Process all IVHD blocks */
1892 h = (struct ivhd_header *)p;
1893 if (*p == amd_iommu_target_ivhd_type) {
1895 DUMP_printk("device: %04x:%02x:%02x.%01x cap: %04x "
1896 "flags: %01x info %04x\n",
1897 h->pci_seg, PCI_BUS_NUM(h->devid),
1898 PCI_SLOT(h->devid), PCI_FUNC(h->devid),
1899 h->cap_ptr, h->flags, h->info);
1900 DUMP_printk(" mmio-addr: %016llx\n",
1903 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1907 ret = init_iommu_one(iommu, h, table);
1916 /* Phase 2 : Early feature support check */
1919 /* Phase 3 : Enabling IOMMU features */
1920 for_each_iommu(iommu) {
1921 ret = init_iommu_one_late(iommu);
1929 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1932 struct pci_dev *pdev = iommu->dev;
1934 if (!check_feature(FEATURE_PC))
1937 amd_iommu_pc_present = true;
1939 pci_info(pdev, "IOMMU performance counters supported\n");
1941 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1942 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1943 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1948 static ssize_t amd_iommu_show_cap(struct device *dev,
1949 struct device_attribute *attr,
1952 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1953 return sysfs_emit(buf, "%x\n", iommu->cap);
1955 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1957 static ssize_t amd_iommu_show_features(struct device *dev,
1958 struct device_attribute *attr,
1961 return sysfs_emit(buf, "%llx:%llx\n", amd_iommu_efr, amd_iommu_efr2);
1963 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1965 static struct attribute *amd_iommu_attrs[] = {
1967 &dev_attr_features.attr,
1971 static struct attribute_group amd_iommu_group = {
1972 .name = "amd-iommu",
1973 .attrs = amd_iommu_attrs,
1976 static const struct attribute_group *amd_iommu_groups[] = {
1982 * Note: IVHD 0x11 and 0x40 also contains exact copy
1983 * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
1984 * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
1986 static void __init late_iommu_features_init(struct amd_iommu *iommu)
1988 u64 features, features2;
1990 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
1993 /* read extended feature bits */
1994 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1995 features2 = readq(iommu->mmio_base + MMIO_EXT_FEATURES2);
1997 if (!amd_iommu_efr) {
1998 amd_iommu_efr = features;
1999 amd_iommu_efr2 = features2;
2004 * Sanity check and warn if EFR values from
2005 * IVHD and MMIO conflict.
2007 if (features != amd_iommu_efr ||
2008 features2 != amd_iommu_efr2) {
2010 "EFR mismatch. Use IVHD EFR (%#llx : %#llx), EFR2 (%#llx : %#llx).\n",
2011 features, amd_iommu_efr,
2012 features2, amd_iommu_efr2);
2016 static int __init iommu_init_pci(struct amd_iommu *iommu)
2018 int cap_ptr = iommu->cap_ptr;
2021 iommu->dev = pci_get_domain_bus_and_slot(iommu->pci_seg->id,
2022 PCI_BUS_NUM(iommu->devid),
2023 iommu->devid & 0xff);
2027 /* Prevent binding other PCI device drivers to IOMMU devices */
2028 iommu->dev->match_driver = false;
2030 /* ACPI _PRT won't have an IRQ for IOMMU */
2031 iommu->dev->irq_managed = 1;
2033 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
2036 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
2037 amd_iommu_iotlb_sup = false;
2039 late_iommu_features_init(iommu);
2041 if (check_feature(FEATURE_GT)) {
2045 pasmax = amd_iommu_efr & FEATURE_PASID_MASK;
2046 pasmax >>= FEATURE_PASID_SHIFT;
2047 iommu->iommu.max_pasids = (1 << (pasmax + 1)) - 1;
2049 BUG_ON(iommu->iommu.max_pasids & ~PASID_MASK);
2051 glxval = amd_iommu_efr & FEATURE_GLXVAL_MASK;
2052 glxval >>= FEATURE_GLXVAL_SHIFT;
2054 if (amd_iommu_max_glx_val == -1)
2055 amd_iommu_max_glx_val = glxval;
2057 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
2059 iommu_enable_gt(iommu);
2062 if (check_feature(FEATURE_PPR) && amd_iommu_alloc_ppr_log(iommu))
2065 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
2066 pr_info("Using strict mode due to virtualization\n");
2067 iommu_set_dma_strict();
2068 amd_iommu_np_cache = true;
2071 init_iommu_perf_ctr(iommu);
2073 if (amd_iommu_pgtable == AMD_IOMMU_V2) {
2074 if (!check_feature(FEATURE_GIOSUP) ||
2075 !check_feature(FEATURE_GT)) {
2076 pr_warn("Cannot enable v2 page table for DMA-API. Fallback to v1.\n");
2077 amd_iommu_pgtable = AMD_IOMMU_V1;
2081 if (is_rd890_iommu(iommu->dev)) {
2085 pci_get_domain_bus_and_slot(iommu->pci_seg->id,
2086 iommu->dev->bus->number,
2090 * Some rd890 systems may not be fully reconfigured by the
2091 * BIOS, so it's necessary for us to store this information so
2092 * it can be reprogrammed on resume
2094 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
2095 &iommu->stored_addr_lo);
2096 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
2097 &iommu->stored_addr_hi);
2099 /* Low bit locks writes to configuration space */
2100 iommu->stored_addr_lo &= ~1;
2102 for (i = 0; i < 6; i++)
2103 for (j = 0; j < 0x12; j++)
2104 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
2106 for (i = 0; i < 0x83; i++)
2107 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
2110 amd_iommu_erratum_746_workaround(iommu);
2111 amd_iommu_ats_write_check_workaround(iommu);
2113 ret = iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
2114 amd_iommu_groups, "ivhd%d", iommu->index);
2119 * Allocate per IOMMU IOPF queue here so that in attach device path,
2120 * PRI capable device can be added to IOPF queue
2122 if (amd_iommu_gt_ppr_supported()) {
2123 ret = amd_iommu_iopf_init(iommu);
2128 iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
2130 return pci_enable_device(iommu->dev);
2133 static void print_iommu_info(void)
2136 static const char * const feat_str[] = {
2137 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
2138 "IA", "GA", "HE", "PC"
2141 if (amd_iommu_efr) {
2142 pr_info("Extended features (%#llx, %#llx):", amd_iommu_efr, amd_iommu_efr2);
2144 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
2145 if (check_feature(1ULL << i))
2146 pr_cont(" %s", feat_str[i]);
2149 if (check_feature(FEATURE_GAM_VAPIC))
2150 pr_cont(" GA_vAPIC");
2152 if (check_feature(FEATURE_SNP))
2158 if (irq_remapping_enabled) {
2159 pr_info("Interrupt remapping enabled\n");
2160 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2161 pr_info("X2APIC enabled\n");
2163 if (amd_iommu_pgtable == AMD_IOMMU_V2) {
2164 pr_info("V2 page table enabled (Paging mode : %d level)\n",
2165 amd_iommu_gpt_level);
2169 static int __init amd_iommu_init_pci(void)
2171 struct amd_iommu *iommu;
2172 struct amd_iommu_pci_seg *pci_seg;
2175 for_each_iommu(iommu) {
2176 ret = iommu_init_pci(iommu);
2178 pr_err("IOMMU%d: Failed to initialize IOMMU Hardware (error=%d)!\n",
2182 /* Need to setup range after PCI init */
2183 iommu_set_cwwb_range(iommu);
2187 * Order is important here to make sure any unity map requirements are
2188 * fulfilled. The unity mappings are created and written to the device
2189 * table during the iommu_init_pci() call.
2191 * After that we call init_device_table_dma() to make sure any
2192 * uninitialized DTE will block DMA, and in the end we flush the caches
2193 * of all IOMMUs to make sure the changes to the device table are
2196 for_each_pci_segment(pci_seg)
2197 init_device_table_dma(pci_seg);
2199 for_each_iommu(iommu)
2200 amd_iommu_flush_all_caches(iommu);
2208 /****************************************************************************
2210 * The following functions initialize the MSI interrupts for all IOMMUs
2211 * in the system. It's a bit challenging because there could be multiple
2212 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
2215 ****************************************************************************/
2217 static int iommu_setup_msi(struct amd_iommu *iommu)
2221 r = pci_enable_msi(iommu->dev);
2225 r = request_threaded_irq(iommu->dev->irq,
2226 amd_iommu_int_handler,
2227 amd_iommu_int_thread,
2232 pci_disable_msi(iommu->dev);
2243 dest_mode_logical : 1,
2250 } __attribute__ ((packed));
2253 static struct irq_chip intcapxt_controller;
2255 static int intcapxt_irqdomain_activate(struct irq_domain *domain,
2256 struct irq_data *irqd, bool reserve)
2261 static void intcapxt_irqdomain_deactivate(struct irq_domain *domain,
2262 struct irq_data *irqd)
2267 static int intcapxt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2268 unsigned int nr_irqs, void *arg)
2270 struct irq_alloc_info *info = arg;
2273 if (!info || info->type != X86_IRQ_ALLOC_TYPE_AMDVI)
2276 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
2280 for (i = virq; i < virq + nr_irqs; i++) {
2281 struct irq_data *irqd = irq_domain_get_irq_data(domain, i);
2283 irqd->chip = &intcapxt_controller;
2284 irqd->hwirq = info->hwirq;
2285 irqd->chip_data = info->data;
2286 __irq_set_handler(i, handle_edge_irq, 0, "edge");
2292 static void intcapxt_irqdomain_free(struct irq_domain *domain, unsigned int virq,
2293 unsigned int nr_irqs)
2295 irq_domain_free_irqs_top(domain, virq, nr_irqs);
2299 static void intcapxt_unmask_irq(struct irq_data *irqd)
2301 struct amd_iommu *iommu = irqd->chip_data;
2302 struct irq_cfg *cfg = irqd_cfg(irqd);
2306 xt.dest_mode_logical = apic->dest_mode_logical;
2307 xt.vector = cfg->vector;
2308 xt.destid_0_23 = cfg->dest_apicid & GENMASK(23, 0);
2309 xt.destid_24_31 = cfg->dest_apicid >> 24;
2311 writeq(xt.capxt, iommu->mmio_base + irqd->hwirq);
2314 static void intcapxt_mask_irq(struct irq_data *irqd)
2316 struct amd_iommu *iommu = irqd->chip_data;
2318 writeq(0, iommu->mmio_base + irqd->hwirq);
2322 static int intcapxt_set_affinity(struct irq_data *irqd,
2323 const struct cpumask *mask, bool force)
2325 struct irq_data *parent = irqd->parent_data;
2328 ret = parent->chip->irq_set_affinity(parent, mask, force);
2329 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
2334 static int intcapxt_set_wake(struct irq_data *irqd, unsigned int on)
2336 return on ? -EOPNOTSUPP : 0;
2339 static struct irq_chip intcapxt_controller = {
2340 .name = "IOMMU-MSI",
2341 .irq_unmask = intcapxt_unmask_irq,
2342 .irq_mask = intcapxt_mask_irq,
2343 .irq_ack = irq_chip_ack_parent,
2344 .irq_retrigger = irq_chip_retrigger_hierarchy,
2345 .irq_set_affinity = intcapxt_set_affinity,
2346 .irq_set_wake = intcapxt_set_wake,
2347 .flags = IRQCHIP_MASK_ON_SUSPEND,
2350 static const struct irq_domain_ops intcapxt_domain_ops = {
2351 .alloc = intcapxt_irqdomain_alloc,
2352 .free = intcapxt_irqdomain_free,
2353 .activate = intcapxt_irqdomain_activate,
2354 .deactivate = intcapxt_irqdomain_deactivate,
2358 static struct irq_domain *iommu_irqdomain;
2360 static struct irq_domain *iommu_get_irqdomain(void)
2362 struct fwnode_handle *fn;
2364 /* No need for locking here (yet) as the init is single-threaded */
2365 if (iommu_irqdomain)
2366 return iommu_irqdomain;
2368 fn = irq_domain_alloc_named_fwnode("AMD-Vi-MSI");
2372 iommu_irqdomain = irq_domain_create_hierarchy(x86_vector_domain, 0, 0,
2373 fn, &intcapxt_domain_ops,
2375 if (!iommu_irqdomain)
2376 irq_domain_free_fwnode(fn);
2378 return iommu_irqdomain;
2381 static int __iommu_setup_intcapxt(struct amd_iommu *iommu, const char *devname,
2382 int hwirq, irq_handler_t thread_fn)
2384 struct irq_domain *domain;
2385 struct irq_alloc_info info;
2387 int node = dev_to_node(&iommu->dev->dev);
2389 domain = iommu_get_irqdomain();
2393 init_irq_alloc_info(&info, NULL);
2394 info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
2398 irq = irq_domain_alloc_irqs(domain, 1, node, &info);
2400 irq_domain_remove(domain);
2404 ret = request_threaded_irq(irq, amd_iommu_int_handler,
2405 thread_fn, 0, devname, iommu);
2407 irq_domain_free_irqs(irq, 1);
2408 irq_domain_remove(domain);
2415 static int iommu_setup_intcapxt(struct amd_iommu *iommu)
2419 snprintf(iommu->evt_irq_name, sizeof(iommu->evt_irq_name),
2420 "AMD-Vi%d-Evt", iommu->index);
2421 ret = __iommu_setup_intcapxt(iommu, iommu->evt_irq_name,
2422 MMIO_INTCAPXT_EVT_OFFSET,
2423 amd_iommu_int_thread_evtlog);
2427 snprintf(iommu->ppr_irq_name, sizeof(iommu->ppr_irq_name),
2428 "AMD-Vi%d-PPR", iommu->index);
2429 ret = __iommu_setup_intcapxt(iommu, iommu->ppr_irq_name,
2430 MMIO_INTCAPXT_PPR_OFFSET,
2431 amd_iommu_int_thread_pprlog);
2435 #ifdef CONFIG_IRQ_REMAP
2436 snprintf(iommu->ga_irq_name, sizeof(iommu->ga_irq_name),
2437 "AMD-Vi%d-GA", iommu->index);
2438 ret = __iommu_setup_intcapxt(iommu, iommu->ga_irq_name,
2439 MMIO_INTCAPXT_GALOG_OFFSET,
2440 amd_iommu_int_thread_galog);
2446 static int iommu_init_irq(struct amd_iommu *iommu)
2450 if (iommu->int_enabled)
2453 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2454 ret = iommu_setup_intcapxt(iommu);
2455 else if (iommu->dev->msi_cap)
2456 ret = iommu_setup_msi(iommu);
2463 iommu->int_enabled = true;
2466 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2467 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2469 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2474 /****************************************************************************
2476 * The next functions belong to the third pass of parsing the ACPI
2477 * table. In this last pass the memory mapping requirements are
2478 * gathered (like exclusion and unity mapping ranges).
2480 ****************************************************************************/
2482 static void __init free_unity_maps(void)
2484 struct unity_map_entry *entry, *next;
2485 struct amd_iommu_pci_seg *p, *pci_seg;
2487 for_each_pci_segment_safe(pci_seg, p) {
2488 list_for_each_entry_safe(entry, next, &pci_seg->unity_map, list) {
2489 list_del(&entry->list);
2495 /* called for unity map ACPI definition */
2496 static int __init init_unity_map_range(struct ivmd_header *m,
2497 struct acpi_table_header *ivrs_base)
2499 struct unity_map_entry *e = NULL;
2500 struct amd_iommu_pci_seg *pci_seg;
2503 pci_seg = get_pci_segment(m->pci_seg, ivrs_base);
2504 if (pci_seg == NULL)
2507 e = kzalloc(sizeof(*e), GFP_KERNEL);
2515 case ACPI_IVMD_TYPE:
2516 s = "IVMD_TYPEi\t\t\t";
2517 e->devid_start = e->devid_end = m->devid;
2519 case ACPI_IVMD_TYPE_ALL:
2520 s = "IVMD_TYPE_ALL\t\t";
2522 e->devid_end = pci_seg->last_bdf;
2524 case ACPI_IVMD_TYPE_RANGE:
2525 s = "IVMD_TYPE_RANGE\t\t";
2526 e->devid_start = m->devid;
2527 e->devid_end = m->aux;
2530 e->address_start = PAGE_ALIGN(m->range_start);
2531 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2532 e->prot = m->flags >> 1;
2535 * Treat per-device exclusion ranges as r/w unity-mapped regions
2536 * since some buggy BIOSes might lead to the overwritten exclusion
2537 * range (exclusion_start and exclusion_length members). This
2538 * happens when there are multiple exclusion ranges (IVMD entries)
2539 * defined in ACPI table.
2541 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2542 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
2544 DUMP_printk("%s devid_start: %04x:%02x:%02x.%x devid_end: "
2545 "%04x:%02x:%02x.%x range_start: %016llx range_end: %016llx"
2546 " flags: %x\n", s, m->pci_seg,
2547 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2548 PCI_FUNC(e->devid_start), m->pci_seg,
2549 PCI_BUS_NUM(e->devid_end),
2550 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2551 e->address_start, e->address_end, m->flags);
2553 list_add_tail(&e->list, &pci_seg->unity_map);
2558 /* iterates over all memory definitions we find in the ACPI table */
2559 static int __init init_memory_definitions(struct acpi_table_header *table)
2561 u8 *p = (u8 *)table, *end = (u8 *)table;
2562 struct ivmd_header *m;
2564 end += table->length;
2565 p += IVRS_HEADER_LENGTH;
2568 m = (struct ivmd_header *)p;
2569 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
2570 init_unity_map_range(m, table);
2579 * Init the device table to not allow DMA access for devices
2581 static void init_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
2584 struct dev_table_entry *dev_table = pci_seg->dev_table;
2586 if (dev_table == NULL)
2589 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
2590 __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_VALID);
2591 if (!amd_iommu_snp_en)
2592 __set_dev_entry_bit(dev_table, devid, DEV_ENTRY_TRANSLATION);
2596 static void __init uninit_device_table_dma(struct amd_iommu_pci_seg *pci_seg)
2599 struct dev_table_entry *dev_table = pci_seg->dev_table;
2601 if (dev_table == NULL)
2604 for (devid = 0; devid <= pci_seg->last_bdf; ++devid) {
2605 dev_table[devid].data[0] = 0ULL;
2606 dev_table[devid].data[1] = 0ULL;
2610 static void init_device_table(void)
2612 struct amd_iommu_pci_seg *pci_seg;
2615 if (!amd_iommu_irq_remap)
2618 for_each_pci_segment(pci_seg) {
2619 for (devid = 0; devid <= pci_seg->last_bdf; ++devid)
2620 __set_dev_entry_bit(pci_seg->dev_table,
2621 devid, DEV_ENTRY_IRQ_TBL_EN);
2625 static void iommu_init_flags(struct amd_iommu *iommu)
2627 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2628 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2629 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2631 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2632 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2633 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2635 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2636 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2637 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2639 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2640 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2641 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2644 * make IOMMU memory accesses cache coherent
2646 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2648 /* Set IOTLB invalidation timeout to 1s */
2649 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2652 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2655 u32 ioc_feature_control;
2656 struct pci_dev *pdev = iommu->root_pdev;
2658 /* RD890 BIOSes may not have completely reconfigured the iommu */
2659 if (!is_rd890_iommu(iommu->dev) || !pdev)
2663 * First, we need to ensure that the iommu is enabled. This is
2664 * controlled by a register in the northbridge
2667 /* Select Northbridge indirect register 0x75 and enable writing */
2668 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2669 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2671 /* Enable the iommu */
2672 if (!(ioc_feature_control & 0x1))
2673 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2675 /* Restore the iommu BAR */
2676 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2677 iommu->stored_addr_lo);
2678 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2679 iommu->stored_addr_hi);
2681 /* Restore the l1 indirect regs for each of the 6 l1s */
2682 for (i = 0; i < 6; i++)
2683 for (j = 0; j < 0x12; j++)
2684 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2686 /* Restore the l2 indirect regs */
2687 for (i = 0; i < 0x83; i++)
2688 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2690 /* Lock PCI setup registers */
2691 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2692 iommu->stored_addr_lo | 1);
2695 static void iommu_enable_ga(struct amd_iommu *iommu)
2697 #ifdef CONFIG_IRQ_REMAP
2698 switch (amd_iommu_guest_ir) {
2699 case AMD_IOMMU_GUEST_IR_VAPIC:
2700 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2701 iommu_feature_enable(iommu, CONTROL_GA_EN);
2702 iommu->irte_ops = &irte_128_ops;
2705 iommu->irte_ops = &irte_32_ops;
2711 static void iommu_disable_irtcachedis(struct amd_iommu *iommu)
2713 iommu_feature_disable(iommu, CONTROL_IRTCACHEDIS);
2716 static void iommu_enable_irtcachedis(struct amd_iommu *iommu)
2720 if (!amd_iommu_irtcachedis)
2725 * The support for IRTCacheDis feature is dertermined by
2726 * checking if the bit is writable.
2728 iommu_feature_enable(iommu, CONTROL_IRTCACHEDIS);
2729 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
2730 ctrl &= (1ULL << CONTROL_IRTCACHEDIS);
2732 iommu->irtcachedis_enabled = true;
2733 pr_info("iommu%d (%#06x) : IRT cache is %s\n",
2734 iommu->index, iommu->devid,
2735 iommu->irtcachedis_enabled ? "disabled" : "enabled");
2738 static void early_enable_iommu(struct amd_iommu *iommu)
2740 iommu_disable(iommu);
2741 iommu_init_flags(iommu);
2742 iommu_set_device_table(iommu);
2743 iommu_enable_command_buffer(iommu);
2744 iommu_enable_event_buffer(iommu);
2745 iommu_set_exclusion_range(iommu);
2746 iommu_enable_gt(iommu);
2747 iommu_enable_ga(iommu);
2748 iommu_enable_xt(iommu);
2749 iommu_enable_irtcachedis(iommu);
2750 iommu_enable(iommu);
2751 amd_iommu_flush_all_caches(iommu);
2755 * This function finally enables all IOMMUs found in the system after
2756 * they have been initialized.
2758 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2759 * the old content of device table entries. Not this case or copy failed,
2760 * just continue as normal kernel does.
2762 static void early_enable_iommus(void)
2764 struct amd_iommu *iommu;
2765 struct amd_iommu_pci_seg *pci_seg;
2767 if (!copy_device_table()) {
2769 * If come here because of failure in copying device table from old
2770 * kernel with all IOMMUs enabled, print error message and try to
2771 * free allocated old_dev_tbl_cpy.
2773 if (amd_iommu_pre_enabled)
2774 pr_err("Failed to copy DEV table from previous kernel.\n");
2776 for_each_pci_segment(pci_seg) {
2777 if (pci_seg->old_dev_tbl_cpy != NULL) {
2778 iommu_free_pages(pci_seg->old_dev_tbl_cpy,
2779 get_order(pci_seg->dev_table_size));
2780 pci_seg->old_dev_tbl_cpy = NULL;
2784 for_each_iommu(iommu) {
2785 clear_translation_pre_enabled(iommu);
2786 early_enable_iommu(iommu);
2789 pr_info("Copied DEV table from previous kernel.\n");
2791 for_each_pci_segment(pci_seg) {
2792 iommu_free_pages(pci_seg->dev_table,
2793 get_order(pci_seg->dev_table_size));
2794 pci_seg->dev_table = pci_seg->old_dev_tbl_cpy;
2797 for_each_iommu(iommu) {
2798 iommu_disable_command_buffer(iommu);
2799 iommu_disable_event_buffer(iommu);
2800 iommu_disable_irtcachedis(iommu);
2801 iommu_enable_command_buffer(iommu);
2802 iommu_enable_event_buffer(iommu);
2803 iommu_enable_ga(iommu);
2804 iommu_enable_xt(iommu);
2805 iommu_enable_irtcachedis(iommu);
2806 iommu_set_device_table(iommu);
2807 amd_iommu_flush_all_caches(iommu);
2812 static void enable_iommus_ppr(void)
2814 struct amd_iommu *iommu;
2816 if (!amd_iommu_gt_ppr_supported())
2819 for_each_iommu(iommu)
2820 amd_iommu_enable_ppr_log(iommu);
2823 static void enable_iommus_vapic(void)
2825 #ifdef CONFIG_IRQ_REMAP
2827 struct amd_iommu *iommu;
2829 for_each_iommu(iommu) {
2831 * Disable GALog if already running. It could have been enabled
2832 * in the previous boot before kdump.
2834 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
2835 if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
2838 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
2839 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
2842 * Need to set and poll check the GALOGRun bit to zero before
2843 * we can set/ modify GA Log registers safely.
2845 for (i = 0; i < MMIO_STATUS_TIMEOUT; ++i) {
2846 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
2847 if (!(status & MMIO_STATUS_GALOG_RUN_MASK))
2852 if (WARN_ON(i >= MMIO_STATUS_TIMEOUT))
2856 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2857 !check_feature(FEATURE_GAM_VAPIC)) {
2858 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2862 if (amd_iommu_snp_en &&
2863 !FEATURE_SNPAVICSUP_GAM(amd_iommu_efr2)) {
2864 pr_warn("Force to disable Virtual APIC due to SNP\n");
2865 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2869 /* Enabling GAM and SNPAVIC support */
2870 for_each_iommu(iommu) {
2871 if (iommu_init_ga_log(iommu) ||
2872 iommu_ga_log_enable(iommu))
2875 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2876 if (amd_iommu_snp_en)
2877 iommu_feature_enable(iommu, CONTROL_SNPAVIC_EN);
2880 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2881 pr_info("Virtual APIC enabled\n");
2885 static void enable_iommus(void)
2887 early_enable_iommus();
2890 static void disable_iommus(void)
2892 struct amd_iommu *iommu;
2894 for_each_iommu(iommu)
2895 iommu_disable(iommu);
2897 #ifdef CONFIG_IRQ_REMAP
2898 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2899 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2904 * Suspend/Resume support
2905 * disable suspend until real resume implemented
2908 static void amd_iommu_resume(void)
2910 struct amd_iommu *iommu;
2912 for_each_iommu(iommu)
2913 iommu_apply_resume_quirks(iommu);
2915 /* re-load the hardware */
2918 amd_iommu_enable_interrupts();
2921 static int amd_iommu_suspend(void)
2923 /* disable IOMMUs to go out of the way for BIOS */
2929 static struct syscore_ops amd_iommu_syscore_ops = {
2930 .suspend = amd_iommu_suspend,
2931 .resume = amd_iommu_resume,
2934 static void __init free_iommu_resources(void)
2936 kmem_cache_destroy(amd_iommu_irq_cache);
2937 amd_iommu_irq_cache = NULL;
2940 free_pci_segments();
2943 /* SB IOAPIC is always on this device in AMD systems */
2944 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2946 static bool __init check_ioapic_information(void)
2948 const char *fw_bug = FW_BUG;
2949 bool ret, has_sb_ioapic;
2952 has_sb_ioapic = false;
2956 * If we have map overrides on the kernel command line the
2957 * messages in this function might not describe firmware bugs
2958 * anymore - so be careful
2963 for (idx = 0; idx < nr_ioapics; idx++) {
2964 int devid, id = mpc_ioapic_id(idx);
2966 devid = get_ioapic_devid(id);
2968 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2971 } else if (devid == IOAPIC_SB_DEVID) {
2972 has_sb_ioapic = true;
2977 if (!has_sb_ioapic) {
2979 * We expect the SB IOAPIC to be listed in the IVRS
2980 * table. The system timer is connected to the SB IOAPIC
2981 * and if we don't have it in the list the system will
2982 * panic at boot time. This situation usually happens
2983 * when the BIOS is buggy and provides us the wrong
2984 * device id for the IOAPIC in the system.
2986 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2990 pr_err("Disabling interrupt remapping\n");
2995 static void __init free_dma_resources(void)
2997 iommu_free_pages(amd_iommu_pd_alloc_bitmap,
2998 get_order(MAX_DOMAIN_ID / 8));
2999 amd_iommu_pd_alloc_bitmap = NULL;
3004 static void __init ivinfo_init(void *ivrs)
3006 amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
3010 * This is the hardware init function for AMD IOMMU in the system.
3011 * This function is called either from amd_iommu_init or from the interrupt
3012 * remapping setup code.
3014 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
3017 * 1 pass) Discover the most comprehensive IVHD type to use.
3019 * 2 pass) Find the highest PCI device id the driver has to handle.
3020 * Upon this information the size of the data structures is
3021 * determined that needs to be allocated.
3023 * 3 pass) Initialize the data structures just allocated with the
3024 * information in the ACPI table about available AMD IOMMUs
3025 * in the system. It also maps the PCI devices in the
3026 * system to specific IOMMUs
3028 * 4 pass) After the basic data structures are allocated and
3029 * initialized we update them with information about memory
3030 * remapping requirements parsed out of the ACPI table in
3033 * After everything is set up the IOMMUs are enabled and the necessary
3034 * hotplug and suspend notifiers are registered.
3036 static int __init early_amd_iommu_init(void)
3038 struct acpi_table_header *ivrs_base;
3039 int remap_cache_sz, ret;
3042 if (!amd_iommu_detected)
3045 status = acpi_get_table("IVRS", 0, &ivrs_base);
3046 if (status == AE_NOT_FOUND)
3048 else if (ACPI_FAILURE(status)) {
3049 const char *err = acpi_format_exception(status);
3050 pr_err("IVRS table error: %s\n", err);
3055 * Validate checksum here so we don't need to do it when
3056 * we actually parse the table
3058 ret = check_ivrs_checksum(ivrs_base);
3062 ivinfo_init(ivrs_base);
3064 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
3065 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
3067 /* Device table - directly used by all IOMMUs */
3070 amd_iommu_pd_alloc_bitmap = iommu_alloc_pages(GFP_KERNEL,
3071 get_order(MAX_DOMAIN_ID / 8));
3072 if (amd_iommu_pd_alloc_bitmap == NULL)
3076 * never allocate domain 0 because its used as the non-allocated and
3077 * error value placeholder
3079 __set_bit(0, amd_iommu_pd_alloc_bitmap);
3082 * now the data structures are allocated and basically initialized
3083 * start the real acpi table scan
3085 ret = init_iommu_all(ivrs_base);
3089 /* 5 level guest page table */
3090 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
3091 check_feature_gpt_level() == GUEST_PGTABLE_5_LEVEL)
3092 amd_iommu_gpt_level = PAGE_MODE_5_LEVEL;
3094 /* Disable any previously enabled IOMMUs */
3095 if (!is_kdump_kernel() || amd_iommu_disabled)
3098 if (amd_iommu_irq_remap)
3099 amd_iommu_irq_remap = check_ioapic_information();
3101 if (amd_iommu_irq_remap) {
3102 struct amd_iommu_pci_seg *pci_seg;
3104 * Interrupt remapping enabled, create kmem_cache for the
3108 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
3109 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
3111 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
3112 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
3114 DTE_INTTAB_ALIGNMENT,
3116 if (!amd_iommu_irq_cache)
3119 for_each_pci_segment(pci_seg) {
3120 if (alloc_irq_lookup_table(pci_seg))
3125 ret = init_memory_definitions(ivrs_base);
3129 /* init the device table */
3130 init_device_table();
3133 /* Don't leak any ACPI memory */
3134 acpi_put_table(ivrs_base);
3139 static int amd_iommu_enable_interrupts(void)
3141 struct amd_iommu *iommu;
3144 for_each_iommu(iommu) {
3145 ret = iommu_init_irq(iommu);
3151 * Interrupt handler is ready to process interrupts. Enable
3152 * PPR and GA log interrupt for all IOMMUs.
3154 enable_iommus_vapic();
3155 enable_iommus_ppr();
3161 static bool __init detect_ivrs(void)
3163 struct acpi_table_header *ivrs_base;
3167 status = acpi_get_table("IVRS", 0, &ivrs_base);
3168 if (status == AE_NOT_FOUND)
3170 else if (ACPI_FAILURE(status)) {
3171 const char *err = acpi_format_exception(status);
3172 pr_err("IVRS table error: %s\n", err);
3176 acpi_put_table(ivrs_base);
3178 if (amd_iommu_force_enable)
3181 /* Don't use IOMMU if there is Stoney Ridge graphics */
3182 for (i = 0; i < 32; i++) {
3185 pci_id = read_pci_config(0, i, 0, 0);
3186 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
3187 pr_info("Disable IOMMU on Stoney Ridge\n");
3193 /* Make sure ACS will be enabled during PCI probe */
3199 static void iommu_snp_enable(void)
3201 #ifdef CONFIG_KVM_AMD_SEV
3202 if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
3205 * The SNP support requires that IOMMU must be enabled, and is
3206 * configured with V1 page table (DTE[Mode] = 0 is not supported).
3208 if (no_iommu || iommu_default_passthrough()) {
3209 pr_warn("SNP: IOMMU disabled or configured in passthrough mode, SNP cannot be supported.\n");
3213 if (amd_iommu_pgtable != AMD_IOMMU_V1) {
3214 pr_warn("SNP: IOMMU is configured with V2 page table mode, SNP cannot be supported.\n");
3218 amd_iommu_snp_en = check_feature(FEATURE_SNP);
3219 if (!amd_iommu_snp_en) {
3220 pr_warn("SNP: IOMMU SNP feature not enabled, SNP cannot be supported.\n");
3224 pr_info("IOMMU SNP support enabled.\n");
3228 cc_platform_clear(CC_ATTR_HOST_SEV_SNP);
3232 /****************************************************************************
3234 * AMD IOMMU Initialization State Machine
3236 ****************************************************************************/
3238 static int __init state_next(void)
3242 switch (init_state) {
3243 case IOMMU_START_STATE:
3244 if (!detect_ivrs()) {
3245 init_state = IOMMU_NOT_FOUND;
3248 init_state = IOMMU_IVRS_DETECTED;
3251 case IOMMU_IVRS_DETECTED:
3252 if (amd_iommu_disabled) {
3253 init_state = IOMMU_CMDLINE_DISABLED;
3256 ret = early_amd_iommu_init();
3257 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
3260 case IOMMU_ACPI_FINISHED:
3261 early_enable_iommus();
3262 x86_platform.iommu_shutdown = disable_iommus;
3263 init_state = IOMMU_ENABLED;
3266 register_syscore_ops(&amd_iommu_syscore_ops);
3268 ret = amd_iommu_init_pci();
3269 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
3271 case IOMMU_PCI_INIT:
3272 ret = amd_iommu_enable_interrupts();
3273 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
3275 case IOMMU_INTERRUPTS_EN:
3276 init_state = IOMMU_INITIALIZED;
3278 case IOMMU_INITIALIZED:
3281 case IOMMU_NOT_FOUND:
3282 case IOMMU_INIT_ERROR:
3283 case IOMMU_CMDLINE_DISABLED:
3284 /* Error states => do nothing */
3293 free_dma_resources();
3294 if (!irq_remapping_enabled) {
3296 free_iommu_resources();
3298 struct amd_iommu *iommu;
3299 struct amd_iommu_pci_seg *pci_seg;
3301 for_each_pci_segment(pci_seg)
3302 uninit_device_table_dma(pci_seg);
3304 for_each_iommu(iommu)
3305 amd_iommu_flush_all_caches(iommu);
3311 static int __init iommu_go_to_state(enum iommu_init_state state)
3315 while (init_state != state) {
3316 if (init_state == IOMMU_NOT_FOUND ||
3317 init_state == IOMMU_INIT_ERROR ||
3318 init_state == IOMMU_CMDLINE_DISABLED)
3326 #ifdef CONFIG_IRQ_REMAP
3327 int __init amd_iommu_prepare(void)
3331 amd_iommu_irq_remap = true;
3333 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
3335 amd_iommu_irq_remap = false;
3339 return amd_iommu_irq_remap ? 0 : -ENODEV;
3342 int __init amd_iommu_enable(void)
3346 ret = iommu_go_to_state(IOMMU_ENABLED);
3350 irq_remapping_enabled = 1;
3351 return amd_iommu_xt_mode;
3354 void amd_iommu_disable(void)
3356 amd_iommu_suspend();
3359 int amd_iommu_reenable(int mode)
3366 int amd_iommu_enable_faulting(unsigned int cpu)
3368 /* We enable MSI later when PCI is initialized */
3374 * This is the core init function for AMD IOMMU hardware in the system.
3375 * This function is called from the generic x86 DMA layer initialization
3378 static int __init amd_iommu_init(void)
3380 struct amd_iommu *iommu;
3383 ret = iommu_go_to_state(IOMMU_INITIALIZED);
3384 #ifdef CONFIG_GART_IOMMU
3385 if (ret && list_empty(&amd_iommu_list)) {
3387 * We failed to initialize the AMD IOMMU - try fallback
3388 * to GART if possible.
3394 for_each_iommu(iommu)
3395 amd_iommu_debugfs_setup(iommu);
3400 static bool amd_iommu_sme_check(void)
3402 if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
3403 (boot_cpu_data.x86 != 0x17))
3406 /* For Fam17h, a specific level of support is required */
3407 if (boot_cpu_data.microcode >= 0x08001205)
3410 if ((boot_cpu_data.microcode >= 0x08001126) &&
3411 (boot_cpu_data.microcode <= 0x080011ff))
3414 pr_notice("IOMMU not currently supported when SME is active\n");
3419 /****************************************************************************
3421 * Early detect code. This code runs at IOMMU detection time in the DMA
3422 * layer. It just looks if there is an IVRS ACPI table to detect AMD
3425 ****************************************************************************/
3426 int __init amd_iommu_detect(void)
3430 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
3433 if (!amd_iommu_sme_check())
3436 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
3440 amd_iommu_detected = true;
3442 x86_init.iommu.iommu_init = amd_iommu_init;
3447 /****************************************************************************
3449 * Parsing functions for the AMD IOMMU specific kernel command line
3452 ****************************************************************************/
3454 static int __init parse_amd_iommu_dump(char *str)
3456 amd_iommu_dump = true;
3461 static int __init parse_amd_iommu_intr(char *str)
3463 for (; *str; ++str) {
3464 if (strncmp(str, "legacy", 6) == 0) {
3465 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
3468 if (strncmp(str, "vapic", 5) == 0) {
3469 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
3476 static int __init parse_amd_iommu_options(char *str)
3482 if (strncmp(str, "fullflush", 9) == 0) {
3483 pr_warn("amd_iommu=fullflush deprecated; use iommu.strict=1 instead\n");
3484 iommu_set_dma_strict();
3485 } else if (strncmp(str, "force_enable", 12) == 0) {
3486 amd_iommu_force_enable = true;
3487 } else if (strncmp(str, "off", 3) == 0) {
3488 amd_iommu_disabled = true;
3489 } else if (strncmp(str, "force_isolation", 15) == 0) {
3490 amd_iommu_force_isolation = true;
3491 } else if (strncmp(str, "pgtbl_v1", 8) == 0) {
3492 amd_iommu_pgtable = AMD_IOMMU_V1;
3493 } else if (strncmp(str, "pgtbl_v2", 8) == 0) {
3494 amd_iommu_pgtable = AMD_IOMMU_V2;
3495 } else if (strncmp(str, "irtcachedis", 11) == 0) {
3496 amd_iommu_irtcachedis = true;
3498 pr_notice("Unknown option - '%s'\n", str);
3501 str += strcspn(str, ",");
3509 static int __init parse_ivrs_ioapic(char *str)
3511 u32 seg = 0, bus, dev, fn;
3515 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3516 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3519 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3520 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3521 pr_warn("ivrs_ioapic%s option format deprecated; use ivrs_ioapic=%d@%04x:%02x:%02x.%d instead\n",
3522 str, id, seg, bus, dev, fn);
3526 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
3530 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
3531 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
3536 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3538 cmdline_maps = true;
3539 i = early_ioapic_map_size++;
3540 early_ioapic_map[i].id = id;
3541 early_ioapic_map[i].devid = devid;
3542 early_ioapic_map[i].cmd_line = true;
3547 static int __init parse_ivrs_hpet(char *str)
3549 u32 seg = 0, bus, dev, fn;
3553 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3554 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3557 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3558 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3559 pr_warn("ivrs_hpet%s option format deprecated; use ivrs_hpet=%d@%04x:%02x:%02x.%d instead\n",
3560 str, id, seg, bus, dev, fn);
3564 pr_err("Invalid command line: ivrs_hpet%s\n", str);
3568 if (early_hpet_map_size == EARLY_MAP_SIZE) {
3569 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3574 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3576 cmdline_maps = true;
3577 i = early_hpet_map_size++;
3578 early_hpet_map[i].id = id;
3579 early_hpet_map[i].devid = devid;
3580 early_hpet_map[i].cmd_line = true;
3585 #define ACPIID_LEN (ACPIHID_UID_LEN + ACPIHID_HID_LEN)
3587 static int __init parse_ivrs_acpihid(char *str)
3589 u32 seg = 0, bus, dev, fn;
3590 char *hid, *uid, *p, *addr;
3591 char acpiid[ACPIID_LEN] = {0};
3594 addr = strchr(str, '@');
3596 addr = strchr(str, '=');
3602 if (strlen(addr) > ACPIID_LEN)
3605 if (sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid) == 4 ||
3606 sscanf(str, "[%x:%x:%x.%x]=%s", &seg, &bus, &dev, &fn, acpiid) == 5) {
3607 pr_warn("ivrs_acpihid%s option format deprecated; use ivrs_acpihid=%s@%04x:%02x:%02x.%d instead\n",
3608 str, acpiid, seg, bus, dev, fn);
3614 /* We have the '@', make it the terminator to get just the acpiid */
3617 if (strlen(str) > ACPIID_LEN + 1)
3620 if (sscanf(str, "=%s", acpiid) != 1)
3623 if (sscanf(addr, "%x:%x.%x", &bus, &dev, &fn) == 3 ||
3624 sscanf(addr, "%x:%x:%x.%x", &seg, &bus, &dev, &fn) == 4)
3628 pr_err("Invalid command line: ivrs_acpihid%s\n", str);
3633 hid = strsep(&p, ":");
3636 if (!hid || !(*hid) || !uid) {
3637 pr_err("Invalid command line: hid or uid\n");
3642 * Ignore leading zeroes after ':', so e.g., AMDI0095:00
3643 * will match AMDI0095:0 in the second strcmp in acpi_dev_hid_uid_match
3645 while (*uid == '0' && *(uid + 1))
3648 i = early_acpihid_map_size++;
3649 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3650 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3651 early_acpihid_map[i].devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3652 early_acpihid_map[i].cmd_line = true;
3657 __setup("amd_iommu_dump", parse_amd_iommu_dump);
3658 __setup("amd_iommu=", parse_amd_iommu_options);
3659 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
3660 __setup("ivrs_ioapic", parse_ivrs_ioapic);
3661 __setup("ivrs_hpet", parse_ivrs_hpet);
3662 __setup("ivrs_acpihid", parse_ivrs_acpihid);
3664 bool amd_iommu_pasid_supported(void)
3666 /* CPU page table size should match IOMMU guest page table size */
3667 if (cpu_feature_enabled(X86_FEATURE_LA57) &&
3668 amd_iommu_gpt_level != PAGE_MODE_5_LEVEL)
3672 * Since DTE[Mode]=0 is prohibited on SNP-enabled system
3673 * (i.e. EFR[SNPSup]=1), IOMMUv2 page table cannot be used without
3674 * setting up IOMMUv1 page table.
3676 return amd_iommu_gt_ppr_supported() && !amd_iommu_snp_en;
3679 struct amd_iommu *get_amd_iommu(unsigned int idx)
3682 struct amd_iommu *iommu;
3684 for_each_iommu(iommu)
3690 /****************************************************************************
3692 * IOMMU EFR Performance Counter support functionality. This code allows
3693 * access to the IOMMU PC functionality.
3695 ****************************************************************************/
3697 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3699 struct amd_iommu *iommu = get_amd_iommu(idx);
3702 return iommu->max_banks;
3707 bool amd_iommu_pc_supported(void)
3709 return amd_iommu_pc_present;
3712 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3714 struct amd_iommu *iommu = get_amd_iommu(idx);
3717 return iommu->max_counters;
3722 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3723 u8 fxn, u64 *value, bool is_write)
3728 /* Make sure the IOMMU PC resource is available */
3729 if (!amd_iommu_pc_present)
3732 /* Check for valid iommu and pc register indexing */
3733 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3736 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3738 /* Limit the offset to the hw defined mmio region aperture */
3739 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3740 (iommu->max_counters << 8) | 0x28);
3741 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3742 (offset > max_offset_lim))
3746 u64 val = *value & GENMASK_ULL(47, 0);
3748 writel((u32)val, iommu->mmio_base + offset);
3749 writel((val >> 32), iommu->mmio_base + offset + 4);
3751 *value = readl(iommu->mmio_base + offset + 4);
3753 *value |= readl(iommu->mmio_base + offset);
3754 *value &= GENMASK_ULL(47, 0);
3760 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3765 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3768 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3773 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3776 #ifdef CONFIG_KVM_AMD_SEV
3777 static int iommu_page_make_shared(void *page)
3779 unsigned long paddr, pfn;
3781 paddr = iommu_virt_to_phys(page);
3782 /* Cbit maybe set in the paddr */
3783 pfn = __sme_clr(paddr) >> PAGE_SHIFT;
3785 if (!(pfn % PTRS_PER_PMD)) {
3789 ret = snp_lookup_rmpentry(pfn, &assigned, &level);
3791 pr_warn("IOMMU PFN %lx RMP lookup failed, ret %d\n", pfn, ret);
3796 pr_warn("IOMMU PFN %lx not assigned in RMP table\n", pfn);
3800 if (level > PG_LEVEL_4K) {
3805 pr_warn("PSMASH failed for IOMMU PFN %lx huge RMP entry, ret: %d, level: %d\n",
3812 return rmp_make_shared(pfn, PG_LEVEL_4K);
3815 static int iommu_make_shared(void *va, size_t size)
3823 for (page = va; page < (va + size); page += PAGE_SIZE) {
3824 ret = iommu_page_make_shared(page);
3832 int amd_iommu_snp_disable(void)
3834 struct amd_iommu *iommu;
3837 if (!amd_iommu_snp_en)
3840 for_each_iommu(iommu) {
3841 ret = iommu_make_shared(iommu->evt_buf, EVT_BUFFER_SIZE);
3845 ret = iommu_make_shared(iommu->ppr_log, PPR_LOG_SIZE);
3849 ret = iommu_make_shared((void *)iommu->cmd_sem, PAGE_SIZE);
3856 EXPORT_SYMBOL_GPL(amd_iommu_snp_disable);