1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
5 * Leo Duran <leo.duran@amd.com>
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>
34 #include <linux/crash_dump.h>
36 #include "amd_iommu.h"
37 #include "../irq_remapping.h"
40 * definitions for the ACPI scanning code
42 #define IVRS_HEADER_LENGTH 48
44 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
45 #define ACPI_IVMD_TYPE_ALL 0x20
46 #define ACPI_IVMD_TYPE 0x21
47 #define ACPI_IVMD_TYPE_RANGE 0x22
49 #define IVHD_DEV_ALL 0x01
50 #define IVHD_DEV_SELECT 0x02
51 #define IVHD_DEV_SELECT_RANGE_START 0x03
52 #define IVHD_DEV_RANGE_END 0x04
53 #define IVHD_DEV_ALIAS 0x42
54 #define IVHD_DEV_ALIAS_RANGE 0x43
55 #define IVHD_DEV_EXT_SELECT 0x46
56 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
57 #define IVHD_DEV_SPECIAL 0x48
58 #define IVHD_DEV_ACPI_HID 0xf0
60 #define UID_NOT_PRESENT 0
61 #define UID_IS_INTEGER 1
62 #define UID_IS_CHARACTER 2
64 #define IVHD_SPECIAL_IOAPIC 1
65 #define IVHD_SPECIAL_HPET 2
67 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
68 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
69 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
70 #define IVHD_FLAG_ISOC_EN_MASK 0x08
72 #define IVMD_FLAG_EXCL_RANGE 0x08
73 #define IVMD_FLAG_IW 0x04
74 #define IVMD_FLAG_IR 0x02
75 #define IVMD_FLAG_UNITY_MAP 0x01
77 #define ACPI_DEVFLAG_INITPASS 0x01
78 #define ACPI_DEVFLAG_EXTINT 0x02
79 #define ACPI_DEVFLAG_NMI 0x04
80 #define ACPI_DEVFLAG_SYSMGT1 0x10
81 #define ACPI_DEVFLAG_SYSMGT2 0x20
82 #define ACPI_DEVFLAG_LINT0 0x40
83 #define ACPI_DEVFLAG_LINT1 0x80
84 #define ACPI_DEVFLAG_ATSDIS 0x10000000
86 #define LOOP_TIMEOUT 2000000
88 * ACPI table definitions
90 * These data structures are laid over the table to parse the important values
94 extern const struct iommu_ops amd_iommu_ops;
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.
124 struct_group(ext_hid,
132 } __attribute__((packed));
135 * An AMD IOMMU memory definition structure. It defines things like exclusion
136 * ranges for devices and regions that should be unity mapped.
147 } __attribute__((packed));
150 bool amd_iommu_irq_remap __read_mostly;
152 enum io_pgtable_fmt amd_iommu_pgtable = AMD_IOMMU_V1;
154 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
155 static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
157 static bool amd_iommu_detected;
158 static bool amd_iommu_disabled __initdata;
159 static bool amd_iommu_force_enable __initdata;
160 static int amd_iommu_target_ivhd_type;
162 u16 amd_iommu_last_bdf; /* largest PCI device id we have
164 LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
167 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
170 /* Array to assign indices to IOMMUs*/
171 struct amd_iommu *amd_iommus[MAX_IOMMUS];
173 /* Number of IOMMUs present in the system */
174 static int amd_iommus_present;
176 /* IOMMUs have a non-present cache? */
177 bool amd_iommu_np_cache __read_mostly;
178 bool amd_iommu_iotlb_sup __read_mostly = true;
180 u32 amd_iommu_max_pasid __read_mostly = ~0;
182 bool amd_iommu_v2_present __read_mostly;
183 static bool amd_iommu_pc_present __read_mostly;
184 bool amdr_ivrs_remap_support __read_mostly;
186 bool amd_iommu_force_isolation __read_mostly;
189 * Pointer to the device table which is shared by all AMD IOMMUs
190 * it is indexed by the PCI device id or the HT unit id and contains
191 * information about the domain the device belongs to as well as the
192 * page table root pointer.
194 struct dev_table_entry *amd_iommu_dev_table;
196 * Pointer to a device table which the content of old device table
197 * will be copied to. It's only be used in kdump kernel.
199 static struct dev_table_entry *old_dev_tbl_cpy;
202 * The alias table is a driver specific data structure which contains the
203 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
204 * More than one device can share the same requestor id.
206 u16 *amd_iommu_alias_table;
209 * The rlookup table is used to find the IOMMU which is responsible
210 * for a specific device. It is also indexed by the PCI device id.
212 struct amd_iommu **amd_iommu_rlookup_table;
215 * This table is used to find the irq remapping table for a given device id
218 struct irq_remap_table **irq_lookup_table;
221 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
222 * to know which ones are already in use.
224 unsigned long *amd_iommu_pd_alloc_bitmap;
226 static u32 dev_table_size; /* size of the device table */
227 static u32 alias_table_size; /* size of the alias table */
228 static u32 rlookup_table_size; /* size if the rlookup table */
230 enum iommu_init_state {
240 IOMMU_CMDLINE_DISABLED,
243 /* Early ioapic and hpet maps from kernel command line */
244 #define EARLY_MAP_SIZE 4
245 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
246 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
247 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
249 static int __initdata early_ioapic_map_size;
250 static int __initdata early_hpet_map_size;
251 static int __initdata early_acpihid_map_size;
253 static bool __initdata cmdline_maps;
255 static enum iommu_init_state init_state = IOMMU_START_STATE;
257 static int amd_iommu_enable_interrupts(void);
258 static int __init iommu_go_to_state(enum iommu_init_state state);
259 static void init_device_table_dma(void);
261 static bool amd_iommu_pre_enabled = true;
263 static u32 amd_iommu_ivinfo __initdata;
265 bool translation_pre_enabled(struct amd_iommu *iommu)
267 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
270 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
272 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
275 static void init_translation_status(struct amd_iommu *iommu)
279 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
280 if (ctrl & (1<<CONTROL_IOMMU_EN))
281 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
284 static inline void update_last_devid(u16 devid)
286 if (devid > amd_iommu_last_bdf)
287 amd_iommu_last_bdf = devid;
290 static inline unsigned long tbl_size(int entry_size)
292 unsigned shift = PAGE_SHIFT +
293 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
298 int amd_iommu_get_num_iommus(void)
300 return amd_iommus_present;
303 #ifdef CONFIG_IRQ_REMAP
304 static bool check_feature_on_all_iommus(u64 mask)
307 struct amd_iommu *iommu;
309 for_each_iommu(iommu) {
310 ret = iommu_feature(iommu, mask);
320 * For IVHD type 0x11/0x40, EFR is also available via IVHD.
321 * Default to IVHD EFR since it is available sooner
322 * (i.e. before PCI init).
324 static void __init early_iommu_features_init(struct amd_iommu *iommu,
325 struct ivhd_header *h)
327 if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP)
328 iommu->features = h->efr_reg;
329 if (amd_iommu_ivinfo & IOMMU_IVINFO_DMA_REMAP)
330 amdr_ivrs_remap_support = true;
333 /* Access to l1 and l2 indexed register spaces */
335 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
339 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
340 pci_read_config_dword(iommu->dev, 0xfc, &val);
344 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
346 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
347 pci_write_config_dword(iommu->dev, 0xfc, val);
348 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
351 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
355 pci_write_config_dword(iommu->dev, 0xf0, address);
356 pci_read_config_dword(iommu->dev, 0xf4, &val);
360 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
362 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
363 pci_write_config_dword(iommu->dev, 0xf4, val);
366 /****************************************************************************
368 * AMD IOMMU MMIO register space handling functions
370 * These functions are used to program the IOMMU device registers in
371 * MMIO space required for that driver.
373 ****************************************************************************/
376 * This function set the exclusion range in the IOMMU. DMA accesses to the
377 * exclusion range are passed through untranslated
379 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
381 u64 start = iommu->exclusion_start & PAGE_MASK;
382 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
385 if (!iommu->exclusion_start)
388 entry = start | MMIO_EXCL_ENABLE_MASK;
389 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
390 &entry, sizeof(entry));
393 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
394 &entry, sizeof(entry));
397 static void iommu_set_cwwb_range(struct amd_iommu *iommu)
399 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
400 u64 entry = start & PM_ADDR_MASK;
402 if (!iommu_feature(iommu, FEATURE_SNP))
406 * Re-purpose Exclusion base/limit registers for Completion wait
407 * write-back base/limit.
409 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
410 &entry, sizeof(entry));
413 * Default to 4 Kbytes, which can be specified by setting base
414 * address equal to the limit address.
416 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
417 &entry, sizeof(entry));
420 /* Programs the physical address of the device table into the IOMMU hardware */
421 static void iommu_set_device_table(struct amd_iommu *iommu)
425 BUG_ON(iommu->mmio_base == NULL);
427 entry = iommu_virt_to_phys(amd_iommu_dev_table);
428 entry |= (dev_table_size >> 12) - 1;
429 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
430 &entry, sizeof(entry));
433 /* Generic functions to enable/disable certain features of the IOMMU. */
434 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
438 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
439 ctrl |= (1ULL << bit);
440 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
443 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
447 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
448 ctrl &= ~(1ULL << bit);
449 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
452 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
456 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
457 ctrl &= ~CTRL_INV_TO_MASK;
458 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
459 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
462 /* Function to enable the hardware */
463 static void iommu_enable(struct amd_iommu *iommu)
465 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
468 static void iommu_disable(struct amd_iommu *iommu)
470 if (!iommu->mmio_base)
473 /* Disable command buffer */
474 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
476 /* Disable event logging and event interrupts */
477 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
478 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
480 /* Disable IOMMU GA_LOG */
481 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
482 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
484 /* Disable IOMMU hardware itself */
485 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
489 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
490 * the system has one.
492 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
494 if (!request_mem_region(address, end, "amd_iommu")) {
495 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
497 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
501 return (u8 __iomem *)ioremap(address, end);
504 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
506 if (iommu->mmio_base)
507 iounmap(iommu->mmio_base);
508 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
511 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
527 /****************************************************************************
529 * The functions below belong to the first pass of AMD IOMMU ACPI table
530 * parsing. In this pass we try to find out the highest device id this
531 * code has to handle. Upon this information the size of the shared data
532 * structures is determined later.
534 ****************************************************************************/
537 * This function calculates the length of a given IVHD entry
539 static inline int ivhd_entry_length(u8 *ivhd)
541 u32 type = ((struct ivhd_entry *)ivhd)->type;
544 return 0x04 << (*ivhd >> 6);
545 } else if (type == IVHD_DEV_ACPI_HID) {
546 /* For ACPI_HID, offset 21 is uid len */
547 return *((u8 *)ivhd + 21) + 22;
553 * After reading the highest device id from the IOMMU PCI capability header
554 * this function looks if there is a higher device id defined in the ACPI table
556 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
558 u8 *p = (void *)h, *end = (void *)h;
559 struct ivhd_entry *dev;
561 u32 ivhd_size = get_ivhd_header_size(h);
564 pr_err("Unsupported IVHD type %#x\n", h->type);
572 dev = (struct ivhd_entry *)p;
575 /* Use maximum BDF value for DEV_ALL */
576 update_last_devid(0xffff);
578 case IVHD_DEV_SELECT:
579 case IVHD_DEV_RANGE_END:
581 case IVHD_DEV_EXT_SELECT:
582 /* all the above subfield types refer to device ids */
583 update_last_devid(dev->devid);
588 p += ivhd_entry_length(p);
596 static int __init check_ivrs_checksum(struct acpi_table_header *table)
599 u8 checksum = 0, *p = (u8 *)table;
601 for (i = 0; i < table->length; ++i)
604 /* ACPI table corrupt */
605 pr_err(FW_BUG "IVRS invalid checksum\n");
613 * Iterate over all IVHD entries in the ACPI table and find the highest device
614 * id which we need to handle. This is the first of three functions which parse
615 * the ACPI table. So we check the checksum here.
617 static int __init find_last_devid_acpi(struct acpi_table_header *table)
619 u8 *p = (u8 *)table, *end = (u8 *)table;
620 struct ivhd_header *h;
622 p += IVRS_HEADER_LENGTH;
624 end += table->length;
626 h = (struct ivhd_header *)p;
627 if (h->type == amd_iommu_target_ivhd_type) {
628 int ret = find_last_devid_from_ivhd(h);
640 /****************************************************************************
642 * The following functions belong to the code path which parses the ACPI table
643 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
644 * data structures, initialize the device/alias/rlookup table and also
645 * basically initialize the hardware.
647 ****************************************************************************/
650 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
651 * write commands to that buffer later and the IOMMU will execute them
654 static int __init alloc_command_buffer(struct amd_iommu *iommu)
656 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
657 get_order(CMD_BUFFER_SIZE));
659 return iommu->cmd_buf ? 0 : -ENOMEM;
663 * This function restarts event logging in case the IOMMU experienced
664 * an event log buffer overflow.
666 void amd_iommu_restart_event_logging(struct amd_iommu *iommu)
668 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
669 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
673 * This function resets the command buffer if the IOMMU stopped fetching
676 static void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
678 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
680 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
681 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
682 iommu->cmd_buf_head = 0;
683 iommu->cmd_buf_tail = 0;
685 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
689 * This function writes the command buffer address to the hardware and
692 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
696 BUG_ON(iommu->cmd_buf == NULL);
698 entry = iommu_virt_to_phys(iommu->cmd_buf);
699 entry |= MMIO_CMD_SIZE_512;
701 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
702 &entry, sizeof(entry));
704 amd_iommu_reset_cmd_buffer(iommu);
708 * This function disables the command buffer
710 static void iommu_disable_command_buffer(struct amd_iommu *iommu)
712 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
715 static void __init free_command_buffer(struct amd_iommu *iommu)
717 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
720 static void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
721 gfp_t gfp, size_t size)
723 int order = get_order(size);
724 void *buf = (void *)__get_free_pages(gfp, order);
727 iommu_feature(iommu, FEATURE_SNP) &&
728 set_memory_4k((unsigned long)buf, (1 << order))) {
729 free_pages((unsigned long)buf, order);
736 /* allocates the memory where the IOMMU will log its events to */
737 static int __init alloc_event_buffer(struct amd_iommu *iommu)
739 iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
742 return iommu->evt_buf ? 0 : -ENOMEM;
745 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
749 BUG_ON(iommu->evt_buf == NULL);
751 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
753 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
754 &entry, sizeof(entry));
756 /* set head and tail to zero manually */
757 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
758 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
760 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
764 * This function disables the event log buffer
766 static void iommu_disable_event_buffer(struct amd_iommu *iommu)
768 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
771 static void __init free_event_buffer(struct amd_iommu *iommu)
773 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
776 /* allocates the memory where the IOMMU will log its events to */
777 static int __init alloc_ppr_log(struct amd_iommu *iommu)
779 iommu->ppr_log = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
782 return iommu->ppr_log ? 0 : -ENOMEM;
785 static void iommu_enable_ppr_log(struct amd_iommu *iommu)
789 if (iommu->ppr_log == NULL)
792 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
794 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
795 &entry, sizeof(entry));
797 /* set head and tail to zero manually */
798 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
799 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
801 iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
802 iommu_feature_enable(iommu, CONTROL_PPR_EN);
805 static void __init free_ppr_log(struct amd_iommu *iommu)
807 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
810 static void free_ga_log(struct amd_iommu *iommu)
812 #ifdef CONFIG_IRQ_REMAP
813 free_pages((unsigned long)iommu->ga_log, get_order(GA_LOG_SIZE));
814 free_pages((unsigned long)iommu->ga_log_tail, get_order(8));
818 static int iommu_ga_log_enable(struct amd_iommu *iommu)
820 #ifdef CONFIG_IRQ_REMAP
827 /* Check if already running */
828 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
829 if (WARN_ON(status & (MMIO_STATUS_GALOG_RUN_MASK)))
832 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
833 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
834 &entry, sizeof(entry));
835 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
836 (BIT_ULL(52)-1)) & ~7ULL;
837 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
838 &entry, sizeof(entry));
839 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
840 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
843 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
844 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
846 for (i = 0; i < LOOP_TIMEOUT; ++i) {
847 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
848 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
853 if (WARN_ON(i >= LOOP_TIMEOUT))
855 #endif /* CONFIG_IRQ_REMAP */
859 static int iommu_init_ga_log(struct amd_iommu *iommu)
861 #ifdef CONFIG_IRQ_REMAP
862 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
865 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
866 get_order(GA_LOG_SIZE));
870 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
872 if (!iommu->ga_log_tail)
881 #endif /* CONFIG_IRQ_REMAP */
884 static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
886 iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO, 1);
888 return iommu->cmd_sem ? 0 : -ENOMEM;
891 static void __init free_cwwb_sem(struct amd_iommu *iommu)
894 free_page((unsigned long)iommu->cmd_sem);
897 static void iommu_enable_xt(struct amd_iommu *iommu)
899 #ifdef CONFIG_IRQ_REMAP
901 * XT mode (32-bit APIC destination ID) requires
902 * GA mode (128-bit IRTE support) as a prerequisite.
904 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
905 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
906 iommu_feature_enable(iommu, CONTROL_XT_EN);
907 #endif /* CONFIG_IRQ_REMAP */
910 static void iommu_enable_gt(struct amd_iommu *iommu)
912 if (!iommu_feature(iommu, FEATURE_GT))
915 iommu_feature_enable(iommu, CONTROL_GT_EN);
918 /* sets a specific bit in the device table entry. */
919 static void set_dev_entry_bit(u16 devid, u8 bit)
921 int i = (bit >> 6) & 0x03;
922 int _bit = bit & 0x3f;
924 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
927 static int get_dev_entry_bit(u16 devid, u8 bit)
929 int i = (bit >> 6) & 0x03;
930 int _bit = bit & 0x3f;
932 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
936 static bool copy_device_table(void)
938 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
939 struct dev_table_entry *old_devtb = NULL;
940 u32 lo, hi, devid, old_devtb_size;
941 phys_addr_t old_devtb_phys;
942 struct amd_iommu *iommu;
943 u16 dom_id, dte_v, irq_v;
947 if (!amd_iommu_pre_enabled)
950 pr_warn("Translation is already enabled - trying to copy translation structures\n");
951 for_each_iommu(iommu) {
952 /* All IOMMUs should use the same device table with the same size */
953 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
954 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
955 entry = (((u64) hi) << 32) + lo;
956 if (last_entry && last_entry != entry) {
957 pr_err("IOMMU:%d should use the same dev table as others!\n",
963 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
964 if (old_devtb_size != dev_table_size) {
965 pr_err("The device table size of IOMMU:%d is not expected!\n",
972 * When SME is enabled in the first kernel, the entry includes the
973 * memory encryption mask(sme_me_mask), we must remove the memory
974 * encryption mask to obtain the true physical address in kdump kernel.
976 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
978 if (old_devtb_phys >= 0x100000000ULL) {
979 pr_err("The address of old device table is above 4G, not trustworthy!\n");
982 old_devtb = (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) && is_kdump_kernel())
983 ? (__force void *)ioremap_encrypted(old_devtb_phys,
985 : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
990 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
991 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
992 get_order(dev_table_size));
993 if (old_dev_tbl_cpy == NULL) {
994 pr_err("Failed to allocate memory for copying old device table!\n");
999 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1000 old_dev_tbl_cpy[devid] = old_devtb[devid];
1001 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
1002 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
1004 if (dte_v && dom_id) {
1005 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
1006 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
1007 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
1008 /* If gcr3 table existed, mask it out */
1009 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
1010 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
1011 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
1012 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
1013 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
1015 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
1019 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
1020 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
1021 int_tab_len = old_devtb[devid].data[2] & DTE_INTTABLEN_MASK;
1022 if (irq_v && (int_ctl || int_tab_len)) {
1023 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
1024 (int_tab_len != DTE_INTTABLEN)) {
1025 pr_err("Wrong old irq remapping flag: %#x\n", devid);
1026 memunmap(old_devtb);
1030 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
1033 memunmap(old_devtb);
1038 void amd_iommu_apply_erratum_63(u16 devid)
1042 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
1043 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
1046 set_dev_entry_bit(devid, DEV_ENTRY_IW);
1049 /* Writes the specific IOMMU for a device into the rlookup table */
1050 static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
1052 amd_iommu_rlookup_table[devid] = iommu;
1056 * This function takes the device specific flags read from the ACPI
1057 * table and sets up the device table entry with that information
1059 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
1060 u16 devid, u32 flags, u32 ext_flags)
1062 if (flags & ACPI_DEVFLAG_INITPASS)
1063 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
1064 if (flags & ACPI_DEVFLAG_EXTINT)
1065 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
1066 if (flags & ACPI_DEVFLAG_NMI)
1067 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
1068 if (flags & ACPI_DEVFLAG_SYSMGT1)
1069 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
1070 if (flags & ACPI_DEVFLAG_SYSMGT2)
1071 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
1072 if (flags & ACPI_DEVFLAG_LINT0)
1073 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
1074 if (flags & ACPI_DEVFLAG_LINT1)
1075 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
1077 amd_iommu_apply_erratum_63(devid);
1079 set_iommu_for_device(iommu, devid);
1082 int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
1084 struct devid_map *entry;
1085 struct list_head *list;
1087 if (type == IVHD_SPECIAL_IOAPIC)
1089 else if (type == IVHD_SPECIAL_HPET)
1094 list_for_each_entry(entry, list, list) {
1095 if (!(entry->id == id && entry->cmd_line))
1098 pr_info("Command-line override present for %s id %d - ignoring\n",
1099 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1101 *devid = entry->devid;
1106 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1111 entry->devid = *devid;
1112 entry->cmd_line = cmd_line;
1114 list_add_tail(&entry->list, list);
1119 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1122 struct acpihid_map_entry *entry;
1123 struct list_head *list = &acpihid_map;
1125 list_for_each_entry(entry, list, list) {
1126 if (strcmp(entry->hid, hid) ||
1127 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1131 pr_info("Command-line override for hid:%s uid:%s\n",
1133 *devid = entry->devid;
1137 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1141 memcpy(entry->uid, uid, strlen(uid));
1142 memcpy(entry->hid, hid, strlen(hid));
1143 entry->devid = *devid;
1144 entry->cmd_line = cmd_line;
1145 entry->root_devid = (entry->devid & (~0x7));
1147 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1148 entry->cmd_line ? "cmd" : "ivrs",
1149 entry->hid, entry->uid, entry->root_devid);
1151 list_add_tail(&entry->list, list);
1155 static int __init add_early_maps(void)
1159 for (i = 0; i < early_ioapic_map_size; ++i) {
1160 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1161 early_ioapic_map[i].id,
1162 &early_ioapic_map[i].devid,
1163 early_ioapic_map[i].cmd_line);
1168 for (i = 0; i < early_hpet_map_size; ++i) {
1169 ret = add_special_device(IVHD_SPECIAL_HPET,
1170 early_hpet_map[i].id,
1171 &early_hpet_map[i].devid,
1172 early_hpet_map[i].cmd_line);
1177 for (i = 0; i < early_acpihid_map_size; ++i) {
1178 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1179 early_acpihid_map[i].uid,
1180 &early_acpihid_map[i].devid,
1181 early_acpihid_map[i].cmd_line);
1190 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1191 * initializes the hardware and our data structures with it.
1193 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1194 struct ivhd_header *h)
1197 u8 *end = p, flags = 0;
1198 u16 devid = 0, devid_start = 0, devid_to = 0;
1199 u32 dev_i, ext_flags = 0;
1201 struct ivhd_entry *e;
1206 ret = add_early_maps();
1210 amd_iommu_apply_ivrs_quirks();
1213 * First save the recommended feature enable bits from ACPI
1215 iommu->acpi_flags = h->flags;
1218 * Done. Now parse the device entries
1220 ivhd_size = get_ivhd_header_size(h);
1222 pr_err("Unsupported IVHD type %#x\n", h->type);
1232 e = (struct ivhd_entry *)p;
1236 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1238 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1239 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1241 case IVHD_DEV_SELECT:
1243 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1245 PCI_BUS_NUM(e->devid),
1251 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1253 case IVHD_DEV_SELECT_RANGE_START:
1255 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1256 "devid: %02x:%02x.%x flags: %02x\n",
1257 PCI_BUS_NUM(e->devid),
1262 devid_start = e->devid;
1267 case IVHD_DEV_ALIAS:
1269 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1270 "flags: %02x devid_to: %02x:%02x.%x\n",
1271 PCI_BUS_NUM(e->devid),
1275 PCI_BUS_NUM(e->ext >> 8),
1276 PCI_SLOT(e->ext >> 8),
1277 PCI_FUNC(e->ext >> 8));
1280 devid_to = e->ext >> 8;
1281 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1282 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1283 amd_iommu_alias_table[devid] = devid_to;
1285 case IVHD_DEV_ALIAS_RANGE:
1287 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1288 "devid: %02x:%02x.%x flags: %02x "
1289 "devid_to: %02x:%02x.%x\n",
1290 PCI_BUS_NUM(e->devid),
1294 PCI_BUS_NUM(e->ext >> 8),
1295 PCI_SLOT(e->ext >> 8),
1296 PCI_FUNC(e->ext >> 8));
1298 devid_start = e->devid;
1300 devid_to = e->ext >> 8;
1304 case IVHD_DEV_EXT_SELECT:
1306 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1307 "flags: %02x ext: %08x\n",
1308 PCI_BUS_NUM(e->devid),
1314 set_dev_entry_from_acpi(iommu, devid, e->flags,
1317 case IVHD_DEV_EXT_SELECT_RANGE:
1319 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1320 "%02x:%02x.%x flags: %02x ext: %08x\n",
1321 PCI_BUS_NUM(e->devid),
1326 devid_start = e->devid;
1331 case IVHD_DEV_RANGE_END:
1333 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1334 PCI_BUS_NUM(e->devid),
1336 PCI_FUNC(e->devid));
1339 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1341 amd_iommu_alias_table[dev_i] = devid_to;
1342 set_dev_entry_from_acpi(iommu,
1343 devid_to, flags, ext_flags);
1345 set_dev_entry_from_acpi(iommu, dev_i,
1349 case IVHD_DEV_SPECIAL: {
1355 handle = e->ext & 0xff;
1356 devid = (e->ext >> 8) & 0xffff;
1357 type = (e->ext >> 24) & 0xff;
1359 if (type == IVHD_SPECIAL_IOAPIC)
1361 else if (type == IVHD_SPECIAL_HPET)
1366 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1372 ret = add_special_device(type, handle, &devid, false);
1377 * add_special_device might update the devid in case a
1378 * command-line override is present. So call
1379 * set_dev_entry_from_acpi after add_special_device.
1381 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1385 case IVHD_DEV_ACPI_HID: {
1387 u8 hid[ACPIHID_HID_LEN];
1388 u8 uid[ACPIHID_UID_LEN];
1391 if (h->type != 0x40) {
1392 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1397 BUILD_BUG_ON(sizeof(e->ext_hid) != ACPIHID_HID_LEN - 1);
1398 memcpy(hid, &e->ext_hid, ACPIHID_HID_LEN - 1);
1399 hid[ACPIHID_HID_LEN - 1] = '\0';
1402 pr_err(FW_BUG "Invalid HID.\n");
1408 case UID_NOT_PRESENT:
1411 pr_warn(FW_BUG "Invalid UID length.\n");
1414 case UID_IS_INTEGER:
1416 sprintf(uid, "%d", e->uid);
1419 case UID_IS_CHARACTER:
1421 memcpy(uid, &e->uid, e->uidl);
1422 uid[e->uidl] = '\0';
1430 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1438 ret = add_acpi_hid_device(hid, uid, &devid, false);
1443 * add_special_device might update the devid in case a
1444 * command-line override is present. So call
1445 * set_dev_entry_from_acpi after add_special_device.
1447 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1455 p += ivhd_entry_length(p);
1461 static void __init free_iommu_one(struct amd_iommu *iommu)
1463 free_cwwb_sem(iommu);
1464 free_command_buffer(iommu);
1465 free_event_buffer(iommu);
1466 free_ppr_log(iommu);
1468 iommu_unmap_mmio_space(iommu);
1471 static void __init free_iommu_all(void)
1473 struct amd_iommu *iommu, *next;
1475 for_each_iommu_safe(iommu, next) {
1476 list_del(&iommu->list);
1477 free_iommu_one(iommu);
1483 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1485 * BIOS should disable L2B micellaneous clock gating by setting
1486 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1488 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1492 if ((boot_cpu_data.x86 != 0x15) ||
1493 (boot_cpu_data.x86_model < 0x10) ||
1494 (boot_cpu_data.x86_model > 0x1f))
1497 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1498 pci_read_config_dword(iommu->dev, 0xf4, &value);
1503 /* Select NB indirect register 0x90 and enable writing */
1504 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1506 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1507 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
1509 /* Clear the enable writing bit */
1510 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1514 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1516 * BIOS should enable ATS write permission check by setting
1517 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1519 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1523 if ((boot_cpu_data.x86 != 0x15) ||
1524 (boot_cpu_data.x86_model < 0x30) ||
1525 (boot_cpu_data.x86_model > 0x3f))
1528 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1529 value = iommu_read_l2(iommu, 0x47);
1534 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1535 iommu_write_l2(iommu, 0x47, value | BIT(0));
1537 pci_info(iommu->dev, "Applying ATS write check workaround\n");
1541 * This function glues the initialization function for one IOMMU
1542 * together and also allocates the command buffer and programs the
1543 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1545 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1549 raw_spin_lock_init(&iommu->lock);
1550 iommu->cmd_sem_val = 0;
1552 /* Add IOMMU to internal data structures */
1553 list_add_tail(&iommu->list, &amd_iommu_list);
1554 iommu->index = amd_iommus_present++;
1556 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1557 WARN(1, "System has more IOMMUs than supported by this driver\n");
1561 /* Index is fine - add IOMMU to the array */
1562 amd_iommus[iommu->index] = iommu;
1565 * Copy data from ACPI table entry to the iommu struct
1567 iommu->devid = h->devid;
1568 iommu->cap_ptr = h->cap_ptr;
1569 iommu->pci_seg = h->pci_seg;
1570 iommu->mmio_phys = h->mmio_phys;
1574 /* Check if IVHD EFR contains proper max banks/counters */
1575 if ((h->efr_attr != 0) &&
1576 ((h->efr_attr & (0xF << 13)) != 0) &&
1577 ((h->efr_attr & (0x3F << 17)) != 0))
1578 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1580 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1583 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1584 * GAM also requires GA mode. Therefore, we need to
1585 * check cmpxchg16b support before enabling it.
1587 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1588 ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1589 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1593 if (h->efr_reg & (1 << 9))
1594 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1596 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1599 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1600 * XT, GAM also requires GA mode. Therefore, we need to
1601 * check cmpxchg16b support before enabling them.
1603 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1604 ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
1605 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1609 if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
1610 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1612 early_iommu_features_init(iommu, h);
1619 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1620 iommu->mmio_phys_end);
1621 if (!iommu->mmio_base)
1624 if (alloc_cwwb_sem(iommu))
1627 if (alloc_command_buffer(iommu))
1630 if (alloc_event_buffer(iommu))
1633 iommu->int_enabled = false;
1635 init_translation_status(iommu);
1636 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1637 iommu_disable(iommu);
1638 clear_translation_pre_enabled(iommu);
1639 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1642 if (amd_iommu_pre_enabled)
1643 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1645 ret = init_iommu_from_acpi(iommu, h);
1649 if (amd_iommu_irq_remap) {
1650 ret = amd_iommu_create_irq_domain(iommu);
1656 * Make sure IOMMU is not considered to translate itself. The IVRS
1657 * table tells us so, but this is a lie!
1659 amd_iommu_rlookup_table[iommu->devid] = NULL;
1665 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1666 * @ivrs: Pointer to the IVRS header
1668 * This function search through all IVDB of the maximum supported IVHD
1670 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1672 u8 *base = (u8 *)ivrs;
1673 struct ivhd_header *ivhd = (struct ivhd_header *)
1674 (base + IVRS_HEADER_LENGTH);
1675 u8 last_type = ivhd->type;
1676 u16 devid = ivhd->devid;
1678 while (((u8 *)ivhd - base < ivrs->length) &&
1679 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1680 u8 *p = (u8 *) ivhd;
1682 if (ivhd->devid == devid)
1683 last_type = ivhd->type;
1684 ivhd = (struct ivhd_header *)(p + ivhd->length);
1691 * Iterates over all IOMMU entries in the ACPI table, allocates the
1692 * IOMMU structure and initializes it with init_iommu_one()
1694 static int __init init_iommu_all(struct acpi_table_header *table)
1696 u8 *p = (u8 *)table, *end = (u8 *)table;
1697 struct ivhd_header *h;
1698 struct amd_iommu *iommu;
1701 end += table->length;
1702 p += IVRS_HEADER_LENGTH;
1705 h = (struct ivhd_header *)p;
1706 if (*p == amd_iommu_target_ivhd_type) {
1708 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1709 "seg: %d flags: %01x info %04x\n",
1710 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
1711 PCI_FUNC(h->devid), h->cap_ptr,
1712 h->pci_seg, h->flags, h->info);
1713 DUMP_printk(" mmio-addr: %016llx\n",
1716 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1720 ret = init_iommu_one(iommu, h);
1732 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1735 struct pci_dev *pdev = iommu->dev;
1737 if (!iommu_feature(iommu, FEATURE_PC))
1740 amd_iommu_pc_present = true;
1742 pci_info(pdev, "IOMMU performance counters supported\n");
1744 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1745 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1746 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1751 static ssize_t amd_iommu_show_cap(struct device *dev,
1752 struct device_attribute *attr,
1755 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1756 return sprintf(buf, "%x\n", iommu->cap);
1758 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1760 static ssize_t amd_iommu_show_features(struct device *dev,
1761 struct device_attribute *attr,
1764 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1765 return sprintf(buf, "%llx\n", iommu->features);
1767 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1769 static struct attribute *amd_iommu_attrs[] = {
1771 &dev_attr_features.attr,
1775 static struct attribute_group amd_iommu_group = {
1776 .name = "amd-iommu",
1777 .attrs = amd_iommu_attrs,
1780 static const struct attribute_group *amd_iommu_groups[] = {
1786 * Note: IVHD 0x11 and 0x40 also contains exact copy
1787 * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
1788 * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
1790 static void __init late_iommu_features_init(struct amd_iommu *iommu)
1794 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
1797 /* read extended feature bits */
1798 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1800 if (!iommu->features) {
1801 iommu->features = features;
1806 * Sanity check and warn if EFR values from
1807 * IVHD and MMIO conflict.
1809 if (features != iommu->features)
1810 pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx).\n",
1811 features, iommu->features);
1814 static int __init iommu_init_pci(struct amd_iommu *iommu)
1816 int cap_ptr = iommu->cap_ptr;
1819 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1820 iommu->devid & 0xff);
1824 /* Prevent binding other PCI device drivers to IOMMU devices */
1825 iommu->dev->match_driver = false;
1827 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1830 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1831 amd_iommu_iotlb_sup = false;
1833 late_iommu_features_init(iommu);
1835 if (iommu_feature(iommu, FEATURE_GT)) {
1840 pasmax = iommu->features & FEATURE_PASID_MASK;
1841 pasmax >>= FEATURE_PASID_SHIFT;
1842 max_pasid = (1 << (pasmax + 1)) - 1;
1844 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1846 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
1848 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1849 glxval >>= FEATURE_GLXVAL_SHIFT;
1851 if (amd_iommu_max_glx_val == -1)
1852 amd_iommu_max_glx_val = glxval;
1854 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1857 if (iommu_feature(iommu, FEATURE_GT) &&
1858 iommu_feature(iommu, FEATURE_PPR)) {
1859 iommu->is_iommu_v2 = true;
1860 amd_iommu_v2_present = true;
1863 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1866 ret = iommu_init_ga_log(iommu);
1870 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
1871 pr_info("Using strict mode due to virtualization\n");
1872 iommu_set_dma_strict();
1873 amd_iommu_np_cache = true;
1876 init_iommu_perf_ctr(iommu);
1878 if (is_rd890_iommu(iommu->dev)) {
1882 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1886 * Some rd890 systems may not be fully reconfigured by the
1887 * BIOS, so it's necessary for us to store this information so
1888 * it can be reprogrammed on resume
1890 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1891 &iommu->stored_addr_lo);
1892 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1893 &iommu->stored_addr_hi);
1895 /* Low bit locks writes to configuration space */
1896 iommu->stored_addr_lo &= ~1;
1898 for (i = 0; i < 6; i++)
1899 for (j = 0; j < 0x12; j++)
1900 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1902 for (i = 0; i < 0x83; i++)
1903 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1906 amd_iommu_erratum_746_workaround(iommu);
1907 amd_iommu_ats_write_check_workaround(iommu);
1909 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1910 amd_iommu_groups, "ivhd%d", iommu->index);
1911 iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
1913 return pci_enable_device(iommu->dev);
1916 static void print_iommu_info(void)
1918 static const char * const feat_str[] = {
1919 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1920 "IA", "GA", "HE", "PC"
1922 struct amd_iommu *iommu;
1924 for_each_iommu(iommu) {
1925 struct pci_dev *pdev = iommu->dev;
1928 pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);
1930 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1931 pr_info("Extended features (%#llx):", iommu->features);
1933 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
1934 if (iommu_feature(iommu, (1ULL << i)))
1935 pr_cont(" %s", feat_str[i]);
1938 if (iommu->features & FEATURE_GAM_VAPIC)
1939 pr_cont(" GA_vAPIC");
1944 if (irq_remapping_enabled) {
1945 pr_info("Interrupt remapping enabled\n");
1946 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1947 pr_info("Virtual APIC enabled\n");
1948 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1949 pr_info("X2APIC enabled\n");
1953 static int __init amd_iommu_init_pci(void)
1955 struct amd_iommu *iommu;
1958 for_each_iommu(iommu) {
1959 ret = iommu_init_pci(iommu);
1961 pr_err("IOMMU%d: Failed to initialize IOMMU Hardware (error=%d)!\n",
1965 /* Need to setup range after PCI init */
1966 iommu_set_cwwb_range(iommu);
1970 * Order is important here to make sure any unity map requirements are
1971 * fulfilled. The unity mappings are created and written to the device
1972 * table during the amd_iommu_init_api() call.
1974 * After that we call init_device_table_dma() to make sure any
1975 * uninitialized DTE will block DMA, and in the end we flush the caches
1976 * of all IOMMUs to make sure the changes to the device table are
1979 ret = amd_iommu_init_api();
1981 pr_err("IOMMU: Failed to initialize IOMMU-API interface (error=%d)!\n",
1986 init_device_table_dma();
1988 for_each_iommu(iommu)
1989 iommu_flush_all_caches(iommu);
1997 /****************************************************************************
1999 * The following functions initialize the MSI interrupts for all IOMMUs
2000 * in the system. It's a bit challenging because there could be multiple
2001 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
2004 ****************************************************************************/
2006 static int iommu_setup_msi(struct amd_iommu *iommu)
2010 r = pci_enable_msi(iommu->dev);
2014 r = request_threaded_irq(iommu->dev->irq,
2015 amd_iommu_int_handler,
2016 amd_iommu_int_thread,
2021 pci_disable_msi(iommu->dev);
2032 dest_mode_logical : 1,
2039 } __attribute__ ((packed));
2042 static struct irq_chip intcapxt_controller;
2044 static int intcapxt_irqdomain_activate(struct irq_domain *domain,
2045 struct irq_data *irqd, bool reserve)
2050 static void intcapxt_irqdomain_deactivate(struct irq_domain *domain,
2051 struct irq_data *irqd)
2056 static int intcapxt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2057 unsigned int nr_irqs, void *arg)
2059 struct irq_alloc_info *info = arg;
2062 if (!info || info->type != X86_IRQ_ALLOC_TYPE_AMDVI)
2065 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
2069 for (i = virq; i < virq + nr_irqs; i++) {
2070 struct irq_data *irqd = irq_domain_get_irq_data(domain, i);
2072 irqd->chip = &intcapxt_controller;
2073 irqd->chip_data = info->data;
2074 __irq_set_handler(i, handle_edge_irq, 0, "edge");
2080 static void intcapxt_irqdomain_free(struct irq_domain *domain, unsigned int virq,
2081 unsigned int nr_irqs)
2083 irq_domain_free_irqs_top(domain, virq, nr_irqs);
2087 static void intcapxt_unmask_irq(struct irq_data *irqd)
2089 struct amd_iommu *iommu = irqd->chip_data;
2090 struct irq_cfg *cfg = irqd_cfg(irqd);
2094 xt.dest_mode_logical = apic->dest_mode_logical;
2095 xt.vector = cfg->vector;
2096 xt.destid_0_23 = cfg->dest_apicid & GENMASK(23, 0);
2097 xt.destid_24_31 = cfg->dest_apicid >> 24;
2100 * Current IOMMU implementation uses the same IRQ for all
2101 * 3 IOMMU interrupts.
2103 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2104 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2105 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2108 static void intcapxt_mask_irq(struct irq_data *irqd)
2110 struct amd_iommu *iommu = irqd->chip_data;
2112 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2113 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2114 writeq(0, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2118 static int intcapxt_set_affinity(struct irq_data *irqd,
2119 const struct cpumask *mask, bool force)
2121 struct irq_data *parent = irqd->parent_data;
2124 ret = parent->chip->irq_set_affinity(parent, mask, force);
2125 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
2130 static int intcapxt_set_wake(struct irq_data *irqd, unsigned int on)
2132 return on ? -EOPNOTSUPP : 0;
2135 static struct irq_chip intcapxt_controller = {
2136 .name = "IOMMU-MSI",
2137 .irq_unmask = intcapxt_unmask_irq,
2138 .irq_mask = intcapxt_mask_irq,
2139 .irq_ack = irq_chip_ack_parent,
2140 .irq_retrigger = irq_chip_retrigger_hierarchy,
2141 .irq_set_affinity = intcapxt_set_affinity,
2142 .irq_set_wake = intcapxt_set_wake,
2143 .flags = IRQCHIP_MASK_ON_SUSPEND,
2146 static const struct irq_domain_ops intcapxt_domain_ops = {
2147 .alloc = intcapxt_irqdomain_alloc,
2148 .free = intcapxt_irqdomain_free,
2149 .activate = intcapxt_irqdomain_activate,
2150 .deactivate = intcapxt_irqdomain_deactivate,
2154 static struct irq_domain *iommu_irqdomain;
2156 static struct irq_domain *iommu_get_irqdomain(void)
2158 struct fwnode_handle *fn;
2160 /* No need for locking here (yet) as the init is single-threaded */
2161 if (iommu_irqdomain)
2162 return iommu_irqdomain;
2164 fn = irq_domain_alloc_named_fwnode("AMD-Vi-MSI");
2168 iommu_irqdomain = irq_domain_create_hierarchy(x86_vector_domain, 0, 0,
2169 fn, &intcapxt_domain_ops,
2171 if (!iommu_irqdomain)
2172 irq_domain_free_fwnode(fn);
2174 return iommu_irqdomain;
2177 static int iommu_setup_intcapxt(struct amd_iommu *iommu)
2179 struct irq_domain *domain;
2180 struct irq_alloc_info info;
2183 domain = iommu_get_irqdomain();
2187 init_irq_alloc_info(&info, NULL);
2188 info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
2191 irq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info);
2193 irq_domain_remove(domain);
2197 ret = request_threaded_irq(irq, amd_iommu_int_handler,
2198 amd_iommu_int_thread, 0, "AMD-Vi", iommu);
2200 irq_domain_free_irqs(irq, 1);
2201 irq_domain_remove(domain);
2208 static int iommu_init_irq(struct amd_iommu *iommu)
2212 if (iommu->int_enabled)
2215 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2216 ret = iommu_setup_intcapxt(iommu);
2217 else if (iommu->dev->msi_cap)
2218 ret = iommu_setup_msi(iommu);
2225 iommu->int_enabled = true;
2228 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2229 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2231 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2233 if (iommu->ppr_log != NULL)
2234 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
2236 iommu_ga_log_enable(iommu);
2241 /****************************************************************************
2243 * The next functions belong to the third pass of parsing the ACPI
2244 * table. In this last pass the memory mapping requirements are
2245 * gathered (like exclusion and unity mapping ranges).
2247 ****************************************************************************/
2249 static void __init free_unity_maps(void)
2251 struct unity_map_entry *entry, *next;
2253 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
2254 list_del(&entry->list);
2259 /* called for unity map ACPI definition */
2260 static int __init init_unity_map_range(struct ivmd_header *m)
2262 struct unity_map_entry *e = NULL;
2265 e = kzalloc(sizeof(*e), GFP_KERNEL);
2273 case ACPI_IVMD_TYPE:
2274 s = "IVMD_TYPEi\t\t\t";
2275 e->devid_start = e->devid_end = m->devid;
2277 case ACPI_IVMD_TYPE_ALL:
2278 s = "IVMD_TYPE_ALL\t\t";
2280 e->devid_end = amd_iommu_last_bdf;
2282 case ACPI_IVMD_TYPE_RANGE:
2283 s = "IVMD_TYPE_RANGE\t\t";
2284 e->devid_start = m->devid;
2285 e->devid_end = m->aux;
2288 e->address_start = PAGE_ALIGN(m->range_start);
2289 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2290 e->prot = m->flags >> 1;
2293 * Treat per-device exclusion ranges as r/w unity-mapped regions
2294 * since some buggy BIOSes might lead to the overwritten exclusion
2295 * range (exclusion_start and exclusion_length members). This
2296 * happens when there are multiple exclusion ranges (IVMD entries)
2297 * defined in ACPI table.
2299 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2300 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
2302 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2303 " range_start: %016llx range_end: %016llx flags: %x\n", s,
2304 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2305 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
2306 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2307 e->address_start, e->address_end, m->flags);
2309 list_add_tail(&e->list, &amd_iommu_unity_map);
2314 /* iterates over all memory definitions we find in the ACPI table */
2315 static int __init init_memory_definitions(struct acpi_table_header *table)
2317 u8 *p = (u8 *)table, *end = (u8 *)table;
2318 struct ivmd_header *m;
2320 end += table->length;
2321 p += IVRS_HEADER_LENGTH;
2324 m = (struct ivmd_header *)p;
2325 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
2326 init_unity_map_range(m);
2335 * Init the device table to not allow DMA access for devices
2337 static void init_device_table_dma(void)
2341 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2342 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2343 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
2347 static void __init uninit_device_table_dma(void)
2351 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2352 amd_iommu_dev_table[devid].data[0] = 0ULL;
2353 amd_iommu_dev_table[devid].data[1] = 0ULL;
2357 static void init_device_table(void)
2361 if (!amd_iommu_irq_remap)
2364 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2365 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2368 static void iommu_init_flags(struct amd_iommu *iommu)
2370 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2371 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2372 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2374 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2375 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2376 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2378 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2379 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2380 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2382 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2383 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2384 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2387 * make IOMMU memory accesses cache coherent
2389 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2391 /* Set IOTLB invalidation timeout to 1s */
2392 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2395 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2398 u32 ioc_feature_control;
2399 struct pci_dev *pdev = iommu->root_pdev;
2401 /* RD890 BIOSes may not have completely reconfigured the iommu */
2402 if (!is_rd890_iommu(iommu->dev) || !pdev)
2406 * First, we need to ensure that the iommu is enabled. This is
2407 * controlled by a register in the northbridge
2410 /* Select Northbridge indirect register 0x75 and enable writing */
2411 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2412 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2414 /* Enable the iommu */
2415 if (!(ioc_feature_control & 0x1))
2416 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2418 /* Restore the iommu BAR */
2419 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2420 iommu->stored_addr_lo);
2421 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2422 iommu->stored_addr_hi);
2424 /* Restore the l1 indirect regs for each of the 6 l1s */
2425 for (i = 0; i < 6; i++)
2426 for (j = 0; j < 0x12; j++)
2427 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2429 /* Restore the l2 indirect regs */
2430 for (i = 0; i < 0x83; i++)
2431 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2433 /* Lock PCI setup registers */
2434 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2435 iommu->stored_addr_lo | 1);
2438 static void iommu_enable_ga(struct amd_iommu *iommu)
2440 #ifdef CONFIG_IRQ_REMAP
2441 switch (amd_iommu_guest_ir) {
2442 case AMD_IOMMU_GUEST_IR_VAPIC:
2443 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2445 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2446 iommu_feature_enable(iommu, CONTROL_GA_EN);
2447 iommu->irte_ops = &irte_128_ops;
2450 iommu->irte_ops = &irte_32_ops;
2456 static void early_enable_iommu(struct amd_iommu *iommu)
2458 iommu_disable(iommu);
2459 iommu_init_flags(iommu);
2460 iommu_set_device_table(iommu);
2461 iommu_enable_command_buffer(iommu);
2462 iommu_enable_event_buffer(iommu);
2463 iommu_set_exclusion_range(iommu);
2464 iommu_enable_ga(iommu);
2465 iommu_enable_xt(iommu);
2466 iommu_enable(iommu);
2467 iommu_flush_all_caches(iommu);
2471 * This function finally enables all IOMMUs found in the system after
2472 * they have been initialized.
2474 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2475 * the old content of device table entries. Not this case or copy failed,
2476 * just continue as normal kernel does.
2478 static void early_enable_iommus(void)
2480 struct amd_iommu *iommu;
2483 if (!copy_device_table()) {
2485 * If come here because of failure in copying device table from old
2486 * kernel with all IOMMUs enabled, print error message and try to
2487 * free allocated old_dev_tbl_cpy.
2489 if (amd_iommu_pre_enabled)
2490 pr_err("Failed to copy DEV table from previous kernel.\n");
2491 if (old_dev_tbl_cpy != NULL)
2492 free_pages((unsigned long)old_dev_tbl_cpy,
2493 get_order(dev_table_size));
2495 for_each_iommu(iommu) {
2496 clear_translation_pre_enabled(iommu);
2497 early_enable_iommu(iommu);
2500 pr_info("Copied DEV table from previous kernel.\n");
2501 free_pages((unsigned long)amd_iommu_dev_table,
2502 get_order(dev_table_size));
2503 amd_iommu_dev_table = old_dev_tbl_cpy;
2504 for_each_iommu(iommu) {
2505 iommu_disable_command_buffer(iommu);
2506 iommu_disable_event_buffer(iommu);
2507 iommu_enable_command_buffer(iommu);
2508 iommu_enable_event_buffer(iommu);
2509 iommu_enable_ga(iommu);
2510 iommu_enable_xt(iommu);
2511 iommu_set_device_table(iommu);
2512 iommu_flush_all_caches(iommu);
2516 #ifdef CONFIG_IRQ_REMAP
2518 * Note: We have already checked GASup from IVRS table.
2519 * Now, we need to make sure that GAMSup is set.
2521 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2522 !check_feature_on_all_iommus(FEATURE_GAM_VAPIC))
2523 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2525 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2526 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2530 static void enable_iommus_v2(void)
2532 struct amd_iommu *iommu;
2534 for_each_iommu(iommu) {
2535 iommu_enable_ppr_log(iommu);
2536 iommu_enable_gt(iommu);
2540 static void enable_iommus(void)
2542 early_enable_iommus();
2547 static void disable_iommus(void)
2549 struct amd_iommu *iommu;
2551 for_each_iommu(iommu)
2552 iommu_disable(iommu);
2554 #ifdef CONFIG_IRQ_REMAP
2555 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2556 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2561 * Suspend/Resume support
2562 * disable suspend until real resume implemented
2565 static void amd_iommu_resume(void)
2567 struct amd_iommu *iommu;
2569 for_each_iommu(iommu)
2570 iommu_apply_resume_quirks(iommu);
2572 /* re-load the hardware */
2575 amd_iommu_enable_interrupts();
2578 static int amd_iommu_suspend(void)
2580 /* disable IOMMUs to go out of the way for BIOS */
2586 static struct syscore_ops amd_iommu_syscore_ops = {
2587 .suspend = amd_iommu_suspend,
2588 .resume = amd_iommu_resume,
2591 static void __init free_iommu_resources(void)
2593 kmemleak_free(irq_lookup_table);
2594 free_pages((unsigned long)irq_lookup_table,
2595 get_order(rlookup_table_size));
2596 irq_lookup_table = NULL;
2598 kmem_cache_destroy(amd_iommu_irq_cache);
2599 amd_iommu_irq_cache = NULL;
2601 free_pages((unsigned long)amd_iommu_rlookup_table,
2602 get_order(rlookup_table_size));
2603 amd_iommu_rlookup_table = NULL;
2605 free_pages((unsigned long)amd_iommu_alias_table,
2606 get_order(alias_table_size));
2607 amd_iommu_alias_table = NULL;
2609 free_pages((unsigned long)amd_iommu_dev_table,
2610 get_order(dev_table_size));
2611 amd_iommu_dev_table = NULL;
2616 /* SB IOAPIC is always on this device in AMD systems */
2617 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2619 static bool __init check_ioapic_information(void)
2621 const char *fw_bug = FW_BUG;
2622 bool ret, has_sb_ioapic;
2625 has_sb_ioapic = false;
2629 * If we have map overrides on the kernel command line the
2630 * messages in this function might not describe firmware bugs
2631 * anymore - so be careful
2636 for (idx = 0; idx < nr_ioapics; idx++) {
2637 int devid, id = mpc_ioapic_id(idx);
2639 devid = get_ioapic_devid(id);
2641 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2644 } else if (devid == IOAPIC_SB_DEVID) {
2645 has_sb_ioapic = true;
2650 if (!has_sb_ioapic) {
2652 * We expect the SB IOAPIC to be listed in the IVRS
2653 * table. The system timer is connected to the SB IOAPIC
2654 * and if we don't have it in the list the system will
2655 * panic at boot time. This situation usually happens
2656 * when the BIOS is buggy and provides us the wrong
2657 * device id for the IOAPIC in the system.
2659 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2663 pr_err("Disabling interrupt remapping\n");
2668 static void __init free_dma_resources(void)
2670 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2671 get_order(MAX_DOMAIN_ID/8));
2672 amd_iommu_pd_alloc_bitmap = NULL;
2677 static void __init ivinfo_init(void *ivrs)
2679 amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
2683 * This is the hardware init function for AMD IOMMU in the system.
2684 * This function is called either from amd_iommu_init or from the interrupt
2685 * remapping setup code.
2687 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2690 * 1 pass) Discover the most comprehensive IVHD type to use.
2692 * 2 pass) Find the highest PCI device id the driver has to handle.
2693 * Upon this information the size of the data structures is
2694 * determined that needs to be allocated.
2696 * 3 pass) Initialize the data structures just allocated with the
2697 * information in the ACPI table about available AMD IOMMUs
2698 * in the system. It also maps the PCI devices in the
2699 * system to specific IOMMUs
2701 * 4 pass) After the basic data structures are allocated and
2702 * initialized we update them with information about memory
2703 * remapping requirements parsed out of the ACPI table in
2706 * After everything is set up the IOMMUs are enabled and the necessary
2707 * hotplug and suspend notifiers are registered.
2709 static int __init early_amd_iommu_init(void)
2711 struct acpi_table_header *ivrs_base;
2712 int i, remap_cache_sz, ret;
2715 if (!amd_iommu_detected)
2718 status = acpi_get_table("IVRS", 0, &ivrs_base);
2719 if (status == AE_NOT_FOUND)
2721 else if (ACPI_FAILURE(status)) {
2722 const char *err = acpi_format_exception(status);
2723 pr_err("IVRS table error: %s\n", err);
2728 * Validate checksum here so we don't need to do it when
2729 * we actually parse the table
2731 ret = check_ivrs_checksum(ivrs_base);
2735 ivinfo_init(ivrs_base);
2737 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2738 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2741 * First parse ACPI tables to find the largest Bus/Dev/Func
2742 * we need to handle. Upon this information the shared data
2743 * structures for the IOMMUs in the system will be allocated
2745 ret = find_last_devid_acpi(ivrs_base);
2749 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2750 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2751 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
2753 /* Device table - directly used by all IOMMUs */
2755 amd_iommu_dev_table = (void *)__get_free_pages(
2756 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
2757 get_order(dev_table_size));
2758 if (amd_iommu_dev_table == NULL)
2762 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2763 * IOMMU see for that device
2765 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2766 get_order(alias_table_size));
2767 if (amd_iommu_alias_table == NULL)
2770 /* IOMMU rlookup table - find the IOMMU for a specific device */
2771 amd_iommu_rlookup_table = (void *)__get_free_pages(
2772 GFP_KERNEL | __GFP_ZERO,
2773 get_order(rlookup_table_size));
2774 if (amd_iommu_rlookup_table == NULL)
2777 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2778 GFP_KERNEL | __GFP_ZERO,
2779 get_order(MAX_DOMAIN_ID/8));
2780 if (amd_iommu_pd_alloc_bitmap == NULL)
2784 * let all alias entries point to itself
2786 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2787 amd_iommu_alias_table[i] = i;
2790 * never allocate domain 0 because its used as the non-allocated and
2791 * error value placeholder
2793 __set_bit(0, amd_iommu_pd_alloc_bitmap);
2796 * now the data structures are allocated and basically initialized
2797 * start the real acpi table scan
2799 ret = init_iommu_all(ivrs_base);
2803 /* Disable any previously enabled IOMMUs */
2804 if (!is_kdump_kernel() || amd_iommu_disabled)
2807 if (amd_iommu_irq_remap)
2808 amd_iommu_irq_remap = check_ioapic_information();
2810 if (amd_iommu_irq_remap) {
2812 * Interrupt remapping enabled, create kmem_cache for the
2816 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2817 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2819 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
2820 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
2822 DTE_INTTAB_ALIGNMENT,
2824 if (!amd_iommu_irq_cache)
2827 irq_lookup_table = (void *)__get_free_pages(
2828 GFP_KERNEL | __GFP_ZERO,
2829 get_order(rlookup_table_size));
2830 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2832 if (!irq_lookup_table)
2836 ret = init_memory_definitions(ivrs_base);
2840 /* init the device table */
2841 init_device_table();
2844 /* Don't leak any ACPI memory */
2845 acpi_put_table(ivrs_base);
2850 static int amd_iommu_enable_interrupts(void)
2852 struct amd_iommu *iommu;
2855 for_each_iommu(iommu) {
2856 ret = iommu_init_irq(iommu);
2865 static bool __init detect_ivrs(void)
2867 struct acpi_table_header *ivrs_base;
2871 status = acpi_get_table("IVRS", 0, &ivrs_base);
2872 if (status == AE_NOT_FOUND)
2874 else if (ACPI_FAILURE(status)) {
2875 const char *err = acpi_format_exception(status);
2876 pr_err("IVRS table error: %s\n", err);
2880 acpi_put_table(ivrs_base);
2882 if (amd_iommu_force_enable)
2885 /* Don't use IOMMU if there is Stoney Ridge graphics */
2886 for (i = 0; i < 32; i++) {
2889 pci_id = read_pci_config(0, i, 0, 0);
2890 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
2891 pr_info("Disable IOMMU on Stoney Ridge\n");
2897 /* Make sure ACS will be enabled during PCI probe */
2903 /****************************************************************************
2905 * AMD IOMMU Initialization State Machine
2907 ****************************************************************************/
2909 static int __init state_next(void)
2913 switch (init_state) {
2914 case IOMMU_START_STATE:
2915 if (!detect_ivrs()) {
2916 init_state = IOMMU_NOT_FOUND;
2919 init_state = IOMMU_IVRS_DETECTED;
2922 case IOMMU_IVRS_DETECTED:
2923 if (amd_iommu_disabled) {
2924 init_state = IOMMU_CMDLINE_DISABLED;
2927 ret = early_amd_iommu_init();
2928 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2931 case IOMMU_ACPI_FINISHED:
2932 early_enable_iommus();
2933 x86_platform.iommu_shutdown = disable_iommus;
2934 init_state = IOMMU_ENABLED;
2937 register_syscore_ops(&amd_iommu_syscore_ops);
2938 ret = amd_iommu_init_pci();
2939 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2942 case IOMMU_PCI_INIT:
2943 ret = amd_iommu_enable_interrupts();
2944 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2946 case IOMMU_INTERRUPTS_EN:
2947 init_state = IOMMU_INITIALIZED;
2949 case IOMMU_INITIALIZED:
2952 case IOMMU_NOT_FOUND:
2953 case IOMMU_INIT_ERROR:
2954 case IOMMU_CMDLINE_DISABLED:
2955 /* Error states => do nothing */
2964 free_dma_resources();
2965 if (!irq_remapping_enabled) {
2967 free_iommu_resources();
2969 struct amd_iommu *iommu;
2971 uninit_device_table_dma();
2972 for_each_iommu(iommu)
2973 iommu_flush_all_caches(iommu);
2979 static int __init iommu_go_to_state(enum iommu_init_state state)
2983 while (init_state != state) {
2984 if (init_state == IOMMU_NOT_FOUND ||
2985 init_state == IOMMU_INIT_ERROR ||
2986 init_state == IOMMU_CMDLINE_DISABLED)
2994 #ifdef CONFIG_IRQ_REMAP
2995 int __init amd_iommu_prepare(void)
2999 amd_iommu_irq_remap = true;
3001 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
3003 amd_iommu_irq_remap = false;
3007 return amd_iommu_irq_remap ? 0 : -ENODEV;
3010 int __init amd_iommu_enable(void)
3014 ret = iommu_go_to_state(IOMMU_ENABLED);
3018 irq_remapping_enabled = 1;
3019 return amd_iommu_xt_mode;
3022 void amd_iommu_disable(void)
3024 amd_iommu_suspend();
3027 int amd_iommu_reenable(int mode)
3034 int __init amd_iommu_enable_faulting(void)
3036 /* We enable MSI later when PCI is initialized */
3042 * This is the core init function for AMD IOMMU hardware in the system.
3043 * This function is called from the generic x86 DMA layer initialization
3046 static int __init amd_iommu_init(void)
3048 struct amd_iommu *iommu;
3051 ret = iommu_go_to_state(IOMMU_INITIALIZED);
3052 #ifdef CONFIG_GART_IOMMU
3053 if (ret && list_empty(&amd_iommu_list)) {
3055 * We failed to initialize the AMD IOMMU - try fallback
3056 * to GART if possible.
3062 for_each_iommu(iommu)
3063 amd_iommu_debugfs_setup(iommu);
3068 static bool amd_iommu_sme_check(void)
3070 if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT) ||
3071 (boot_cpu_data.x86 != 0x17))
3074 /* For Fam17h, a specific level of support is required */
3075 if (boot_cpu_data.microcode >= 0x08001205)
3078 if ((boot_cpu_data.microcode >= 0x08001126) &&
3079 (boot_cpu_data.microcode <= 0x080011ff))
3082 pr_notice("IOMMU not currently supported when SME is active\n");
3087 /****************************************************************************
3089 * Early detect code. This code runs at IOMMU detection time in the DMA
3090 * layer. It just looks if there is an IVRS ACPI table to detect AMD
3093 ****************************************************************************/
3094 int __init amd_iommu_detect(void)
3098 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
3101 if (!amd_iommu_sme_check())
3104 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
3108 amd_iommu_detected = true;
3110 x86_init.iommu.iommu_init = amd_iommu_init;
3115 /****************************************************************************
3117 * Parsing functions for the AMD IOMMU specific kernel command line
3120 ****************************************************************************/
3122 static int __init parse_amd_iommu_dump(char *str)
3124 amd_iommu_dump = true;
3129 static int __init parse_amd_iommu_intr(char *str)
3131 for (; *str; ++str) {
3132 if (strncmp(str, "legacy", 6) == 0) {
3133 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
3136 if (strncmp(str, "vapic", 5) == 0) {
3137 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
3144 static int __init parse_amd_iommu_options(char *str)
3146 for (; *str; ++str) {
3147 if (strncmp(str, "fullflush", 9) == 0) {
3148 pr_warn("amd_iommu=fullflush deprecated; use iommu.strict=1 instead\n");
3149 iommu_set_dma_strict();
3151 if (strncmp(str, "force_enable", 12) == 0)
3152 amd_iommu_force_enable = true;
3153 if (strncmp(str, "off", 3) == 0)
3154 amd_iommu_disabled = true;
3155 if (strncmp(str, "force_isolation", 15) == 0)
3156 amd_iommu_force_isolation = true;
3162 static int __init parse_ivrs_ioapic(char *str)
3164 unsigned int bus, dev, fn;
3168 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
3171 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
3175 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
3176 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
3181 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3183 cmdline_maps = true;
3184 i = early_ioapic_map_size++;
3185 early_ioapic_map[i].id = id;
3186 early_ioapic_map[i].devid = devid;
3187 early_ioapic_map[i].cmd_line = true;
3192 static int __init parse_ivrs_hpet(char *str)
3194 unsigned int bus, dev, fn;
3198 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
3201 pr_err("Invalid command line: ivrs_hpet%s\n", str);
3205 if (early_hpet_map_size == EARLY_MAP_SIZE) {
3206 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3211 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3213 cmdline_maps = true;
3214 i = early_hpet_map_size++;
3215 early_hpet_map[i].id = id;
3216 early_hpet_map[i].devid = devid;
3217 early_hpet_map[i].cmd_line = true;
3222 static int __init parse_ivrs_acpihid(char *str)
3225 char *hid, *uid, *p;
3226 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
3229 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
3231 pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);
3236 hid = strsep(&p, ":");
3239 if (!hid || !(*hid) || !uid) {
3240 pr_err("Invalid command line: hid or uid\n");
3244 i = early_acpihid_map_size++;
3245 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3246 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3247 early_acpihid_map[i].devid =
3248 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3249 early_acpihid_map[i].cmd_line = true;
3254 __setup("amd_iommu_dump", parse_amd_iommu_dump);
3255 __setup("amd_iommu=", parse_amd_iommu_options);
3256 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
3257 __setup("ivrs_ioapic", parse_ivrs_ioapic);
3258 __setup("ivrs_hpet", parse_ivrs_hpet);
3259 __setup("ivrs_acpihid", parse_ivrs_acpihid);
3261 bool amd_iommu_v2_supported(void)
3263 return amd_iommu_v2_present;
3265 EXPORT_SYMBOL(amd_iommu_v2_supported);
3267 struct amd_iommu *get_amd_iommu(unsigned int idx)
3270 struct amd_iommu *iommu;
3272 for_each_iommu(iommu)
3278 /****************************************************************************
3280 * IOMMU EFR Performance Counter support functionality. This code allows
3281 * access to the IOMMU PC functionality.
3283 ****************************************************************************/
3285 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3287 struct amd_iommu *iommu = get_amd_iommu(idx);
3290 return iommu->max_banks;
3294 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3296 bool amd_iommu_pc_supported(void)
3298 return amd_iommu_pc_present;
3300 EXPORT_SYMBOL(amd_iommu_pc_supported);
3302 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3304 struct amd_iommu *iommu = get_amd_iommu(idx);
3307 return iommu->max_counters;
3311 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3313 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3314 u8 fxn, u64 *value, bool is_write)
3319 /* Make sure the IOMMU PC resource is available */
3320 if (!amd_iommu_pc_present)
3323 /* Check for valid iommu and pc register indexing */
3324 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3327 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3329 /* Limit the offset to the hw defined mmio region aperture */
3330 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3331 (iommu->max_counters << 8) | 0x28);
3332 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3333 (offset > max_offset_lim))
3337 u64 val = *value & GENMASK_ULL(47, 0);
3339 writel((u32)val, iommu->mmio_base + offset);
3340 writel((val >> 32), iommu->mmio_base + offset + 4);
3342 *value = readl(iommu->mmio_base + offset + 4);
3344 *value |= readl(iommu->mmio_base + offset);
3345 *value &= GENMASK_ULL(47, 0);
3351 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3356 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3359 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3364 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);