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/amd-iommu.h>
20 #include <linux/export.h>
21 #include <linux/kmemleak.h>
22 #include <linux/mem_encrypt.h>
23 #include <linux/iopoll.h>
24 #include <asm/pci-direct.h>
25 #include <asm/iommu.h>
27 #include <asm/msidef.h>
29 #include <asm/x86_init.h>
30 #include <asm/iommu_table.h>
31 #include <asm/io_apic.h>
32 #include <asm/irq_remapping.h>
33 #include <asm/set_memory.h>
35 #include <linux/crash_dump.h>
37 #include "amd_iommu.h"
38 #include "../irq_remapping.h"
41 * definitions for the ACPI scanning code
43 #define IVRS_HEADER_LENGTH 48
45 #define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
46 #define ACPI_IVMD_TYPE_ALL 0x20
47 #define ACPI_IVMD_TYPE 0x21
48 #define ACPI_IVMD_TYPE_RANGE 0x22
50 #define IVHD_DEV_ALL 0x01
51 #define IVHD_DEV_SELECT 0x02
52 #define IVHD_DEV_SELECT_RANGE_START 0x03
53 #define IVHD_DEV_RANGE_END 0x04
54 #define IVHD_DEV_ALIAS 0x42
55 #define IVHD_DEV_ALIAS_RANGE 0x43
56 #define IVHD_DEV_EXT_SELECT 0x46
57 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
58 #define IVHD_DEV_SPECIAL 0x48
59 #define IVHD_DEV_ACPI_HID 0xf0
61 #define UID_NOT_PRESENT 0
62 #define UID_IS_INTEGER 1
63 #define UID_IS_CHARACTER 2
65 #define IVHD_SPECIAL_IOAPIC 1
66 #define IVHD_SPECIAL_HPET 2
68 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
69 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
70 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
71 #define IVHD_FLAG_ISOC_EN_MASK 0x08
73 #define IVMD_FLAG_EXCL_RANGE 0x08
74 #define IVMD_FLAG_IW 0x04
75 #define IVMD_FLAG_IR 0x02
76 #define IVMD_FLAG_UNITY_MAP 0x01
78 #define ACPI_DEVFLAG_INITPASS 0x01
79 #define ACPI_DEVFLAG_EXTINT 0x02
80 #define ACPI_DEVFLAG_NMI 0x04
81 #define ACPI_DEVFLAG_SYSMGT1 0x10
82 #define ACPI_DEVFLAG_SYSMGT2 0x20
83 #define ACPI_DEVFLAG_LINT0 0x40
84 #define ACPI_DEVFLAG_LINT1 0x80
85 #define ACPI_DEVFLAG_ATSDIS 0x10000000
87 #define LOOP_TIMEOUT 2000000
89 #define IVRS_GET_SBDF_ID(seg, bus, dev, fd) (((seg & 0xffff) << 16) | ((bus & 0xff) << 8) \
90 | ((dev & 0x1f) << 3) | (fn & 0x7))
93 * ACPI table definitions
95 * These data structures are laid over the table to parse the important values
99 extern const struct iommu_ops amd_iommu_ops;
102 * structure describing one IOMMU in the ACPI table. Typically followed by one
103 * or more ivhd_entrys.
116 /* Following only valid on IVHD type 11h and 40h */
117 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
119 } __attribute__((packed));
122 * A device entry describing which devices a specific IOMMU translates and
123 * which requestor ids they use.
135 } __attribute__((packed));
138 * An AMD IOMMU memory definition structure. It defines things like exclusion
139 * ranges for devices and regions that should be unity mapped.
150 } __attribute__((packed));
153 bool amd_iommu_irq_remap __read_mostly;
155 int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
156 static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
158 static bool amd_iommu_detected;
159 static bool __initdata amd_iommu_disabled;
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
166 bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
168 LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
171 /* Array to assign indices to IOMMUs*/
172 struct amd_iommu *amd_iommus[MAX_IOMMUS];
174 /* Number of IOMMUs present in the system */
175 static int amd_iommus_present;
177 /* IOMMUs have a non-present cache? */
178 bool amd_iommu_np_cache __read_mostly;
179 bool amd_iommu_iotlb_sup __read_mostly = true;
181 u32 amd_iommu_max_pasid __read_mostly = ~0;
183 bool amd_iommu_v2_present __read_mostly;
184 static bool amd_iommu_pc_present __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;
213 EXPORT_SYMBOL(amd_iommu_rlookup_table);
216 * This table is used to find the irq remapping table for a given device id
219 struct irq_remap_table **irq_lookup_table;
222 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
223 * to know which ones are already in use.
225 unsigned long *amd_iommu_pd_alloc_bitmap;
227 static u32 dev_table_size; /* size of the device table */
228 static u32 alias_table_size; /* size of the alias table */
229 static u32 rlookup_table_size; /* size if the rlookup table */
231 enum iommu_init_state {
242 IOMMU_CMDLINE_DISABLED,
245 /* Early ioapic and hpet maps from kernel command line */
246 #define EARLY_MAP_SIZE 4
247 static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
248 static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
249 static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
251 static int __initdata early_ioapic_map_size;
252 static int __initdata early_hpet_map_size;
253 static int __initdata early_acpihid_map_size;
255 static bool __initdata cmdline_maps;
257 static enum iommu_init_state init_state = IOMMU_START_STATE;
259 static int amd_iommu_enable_interrupts(void);
260 static int __init iommu_go_to_state(enum iommu_init_state state);
261 static void init_device_table_dma(void);
263 static bool amd_iommu_pre_enabled = true;
265 static u32 amd_iommu_ivinfo __initdata;
267 bool translation_pre_enabled(struct amd_iommu *iommu)
269 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
271 EXPORT_SYMBOL(translation_pre_enabled);
273 static void clear_translation_pre_enabled(struct amd_iommu *iommu)
275 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
278 static void init_translation_status(struct amd_iommu *iommu)
282 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
283 if (ctrl & (1<<CONTROL_IOMMU_EN))
284 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
287 static inline void update_last_devid(u16 devid)
289 if (devid > amd_iommu_last_bdf)
290 amd_iommu_last_bdf = devid;
293 static inline unsigned long tbl_size(int entry_size)
295 unsigned shift = PAGE_SHIFT +
296 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
301 int amd_iommu_get_num_iommus(void)
303 return amd_iommus_present;
306 #ifdef CONFIG_IRQ_REMAP
307 static bool check_feature_on_all_iommus(u64 mask)
310 struct amd_iommu *iommu;
312 for_each_iommu(iommu) {
313 ret = iommu_feature(iommu, mask);
323 * For IVHD type 0x11/0x40, EFR is also available via IVHD.
324 * Default to IVHD EFR since it is available sooner
325 * (i.e. before PCI init).
327 static void __init early_iommu_features_init(struct amd_iommu *iommu,
328 struct ivhd_header *h)
330 if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP)
331 iommu->features = h->efr_reg;
334 /* Access to l1 and l2 indexed register spaces */
336 static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
340 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
341 pci_read_config_dword(iommu->dev, 0xfc, &val);
345 static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
347 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
348 pci_write_config_dword(iommu->dev, 0xfc, val);
349 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
352 static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
356 pci_write_config_dword(iommu->dev, 0xf0, address);
357 pci_read_config_dword(iommu->dev, 0xf4, &val);
361 static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
363 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
364 pci_write_config_dword(iommu->dev, 0xf4, val);
367 /****************************************************************************
369 * AMD IOMMU MMIO register space handling functions
371 * These functions are used to program the IOMMU device registers in
372 * MMIO space required for that driver.
374 ****************************************************************************/
377 * This function set the exclusion range in the IOMMU. DMA accesses to the
378 * exclusion range are passed through untranslated
380 static void iommu_set_exclusion_range(struct amd_iommu *iommu)
382 u64 start = iommu->exclusion_start & PAGE_MASK;
383 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
386 if (!iommu->exclusion_start)
389 entry = start | MMIO_EXCL_ENABLE_MASK;
390 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
391 &entry, sizeof(entry));
394 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
395 &entry, sizeof(entry));
398 static void iommu_set_cwwb_range(struct amd_iommu *iommu)
400 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
401 u64 entry = start & PM_ADDR_MASK;
403 if (!iommu_feature(iommu, FEATURE_SNP))
407 * Re-purpose Exclusion base/limit registers for Completion wait
408 * write-back base/limit.
410 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
411 &entry, sizeof(entry));
414 * Default to 4 Kbytes, which can be specified by setting base
415 * address equal to the limit address.
417 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
418 &entry, sizeof(entry));
421 /* Programs the physical address of the device table into the IOMMU hardware */
422 static void iommu_set_device_table(struct amd_iommu *iommu)
426 BUG_ON(iommu->mmio_base == NULL);
428 entry = iommu_virt_to_phys(amd_iommu_dev_table);
429 entry |= (dev_table_size >> 12) - 1;
430 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
431 &entry, sizeof(entry));
434 /* Generic functions to enable/disable certain features of the IOMMU. */
435 static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
439 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
440 ctrl |= (1ULL << bit);
441 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
444 static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
448 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
449 ctrl &= ~(1ULL << bit);
450 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
453 static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
457 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
458 ctrl &= ~CTRL_INV_TO_MASK;
459 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
460 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
463 /* Function to enable the hardware */
464 static void iommu_enable(struct amd_iommu *iommu)
466 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
469 static void iommu_disable(struct amd_iommu *iommu)
471 if (!iommu->mmio_base)
474 /* Disable command buffer */
475 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
477 /* Disable event logging and event interrupts */
478 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
479 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
481 /* Disable IOMMU GA_LOG */
482 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
483 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
485 /* Disable IOMMU hardware itself */
486 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
490 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
491 * the system has one.
493 static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
495 if (!request_mem_region(address, end, "amd_iommu")) {
496 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
498 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
502 return (u8 __iomem *)ioremap(address, end);
505 static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
507 if (iommu->mmio_base)
508 iounmap(iommu->mmio_base);
509 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
512 static inline u32 get_ivhd_header_size(struct ivhd_header *h)
528 /****************************************************************************
530 * The functions below belong to the first pass of AMD IOMMU ACPI table
531 * parsing. In this pass we try to find out the highest device id this
532 * code has to handle. Upon this information the size of the shared data
533 * structures is determined later.
535 ****************************************************************************/
538 * This function calculates the length of a given IVHD entry
540 static inline int ivhd_entry_length(u8 *ivhd)
542 u32 type = ((struct ivhd_entry *)ivhd)->type;
545 return 0x04 << (*ivhd >> 6);
546 } else if (type == IVHD_DEV_ACPI_HID) {
547 /* For ACPI_HID, offset 21 is uid len */
548 return *((u8 *)ivhd + 21) + 22;
554 * After reading the highest device id from the IOMMU PCI capability header
555 * this function looks if there is a higher device id defined in the ACPI table
557 static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
559 u8 *p = (void *)h, *end = (void *)h;
560 struct ivhd_entry *dev;
562 u32 ivhd_size = get_ivhd_header_size(h);
565 pr_err("Unsupported IVHD type %#x\n", h->type);
573 dev = (struct ivhd_entry *)p;
576 /* Use maximum BDF value for DEV_ALL */
577 update_last_devid(0xffff);
579 case IVHD_DEV_SELECT:
580 case IVHD_DEV_RANGE_END:
582 case IVHD_DEV_EXT_SELECT:
583 /* all the above subfield types refer to device ids */
584 update_last_devid(dev->devid);
589 p += ivhd_entry_length(p);
597 static int __init check_ivrs_checksum(struct acpi_table_header *table)
600 u8 checksum = 0, *p = (u8 *)table;
602 for (i = 0; i < table->length; ++i)
605 /* ACPI table corrupt */
606 pr_err(FW_BUG "IVRS invalid checksum\n");
614 * Iterate over all IVHD entries in the ACPI table and find the highest device
615 * id which we need to handle. This is the first of three functions which parse
616 * the ACPI table. So we check the checksum here.
618 static int __init find_last_devid_acpi(struct acpi_table_header *table)
620 u8 *p = (u8 *)table, *end = (u8 *)table;
621 struct ivhd_header *h;
623 p += IVRS_HEADER_LENGTH;
625 end += table->length;
627 h = (struct ivhd_header *)p;
628 if (h->type == amd_iommu_target_ivhd_type) {
629 int ret = find_last_devid_from_ivhd(h);
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 device/alias/rlookup table and also
646 * basically initialize the hardware.
648 ****************************************************************************/
651 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
652 * write commands to that buffer later and the IOMMU will execute them
655 static int __init alloc_command_buffer(struct amd_iommu *iommu)
657 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
658 get_order(CMD_BUFFER_SIZE));
660 return iommu->cmd_buf ? 0 : -ENOMEM;
664 * This function restarts event logging in case the IOMMU experienced
665 * an event log buffer overflow.
667 void amd_iommu_restart_event_logging(struct amd_iommu *iommu)
669 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
670 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
674 * This function resets the command buffer if the IOMMU stopped fetching
677 void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
679 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
681 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
682 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
683 iommu->cmd_buf_head = 0;
684 iommu->cmd_buf_tail = 0;
686 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
690 * This function writes the command buffer address to the hardware and
693 static void iommu_enable_command_buffer(struct amd_iommu *iommu)
697 BUG_ON(iommu->cmd_buf == NULL);
699 entry = iommu_virt_to_phys(iommu->cmd_buf);
700 entry |= MMIO_CMD_SIZE_512;
702 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
703 &entry, sizeof(entry));
705 amd_iommu_reset_cmd_buffer(iommu);
709 * This function disables the command buffer
711 static void iommu_disable_command_buffer(struct amd_iommu *iommu)
713 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
716 static void __init free_command_buffer(struct amd_iommu *iommu)
718 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
721 static void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
722 gfp_t gfp, size_t size)
724 int order = get_order(size);
725 void *buf = (void *)__get_free_pages(gfp, order);
728 iommu_feature(iommu, FEATURE_SNP) &&
729 set_memory_4k((unsigned long)buf, (1 << order))) {
730 free_pages((unsigned long)buf, order);
737 /* allocates the memory where the IOMMU will log its events to */
738 static int __init alloc_event_buffer(struct amd_iommu *iommu)
740 iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
743 return iommu->evt_buf ? 0 : -ENOMEM;
746 static void iommu_enable_event_buffer(struct amd_iommu *iommu)
750 BUG_ON(iommu->evt_buf == NULL);
752 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
754 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
755 &entry, sizeof(entry));
757 /* set head and tail to zero manually */
758 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
759 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
761 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
765 * This function disables the event log buffer
767 static void iommu_disable_event_buffer(struct amd_iommu *iommu)
769 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
772 static void __init free_event_buffer(struct amd_iommu *iommu)
774 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
777 /* allocates the memory where the IOMMU will log its events to */
778 static int __init alloc_ppr_log(struct amd_iommu *iommu)
780 iommu->ppr_log = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
783 return iommu->ppr_log ? 0 : -ENOMEM;
786 static void iommu_enable_ppr_log(struct amd_iommu *iommu)
790 if (iommu->ppr_log == NULL)
793 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
795 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
796 &entry, sizeof(entry));
798 /* set head and tail to zero manually */
799 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
800 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
802 iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
803 iommu_feature_enable(iommu, CONTROL_PPR_EN);
806 static void __init free_ppr_log(struct amd_iommu *iommu)
808 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
811 static void free_ga_log(struct amd_iommu *iommu)
813 #ifdef CONFIG_IRQ_REMAP
814 free_pages((unsigned long)iommu->ga_log, get_order(GA_LOG_SIZE));
815 free_pages((unsigned long)iommu->ga_log_tail, get_order(8));
819 static int iommu_ga_log_enable(struct amd_iommu *iommu)
821 #ifdef CONFIG_IRQ_REMAP
828 /* Check if already running */
829 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
830 if (WARN_ON(status & (MMIO_STATUS_GALOG_RUN_MASK)))
833 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
834 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
835 &entry, sizeof(entry));
836 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
837 (BIT_ULL(52)-1)) & ~7ULL;
838 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
839 &entry, sizeof(entry));
840 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
841 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
844 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
845 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
847 for (i = 0; i < LOOP_TIMEOUT; ++i) {
848 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
849 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
854 if (WARN_ON(i >= LOOP_TIMEOUT))
856 #endif /* CONFIG_IRQ_REMAP */
860 static int iommu_init_ga_log(struct amd_iommu *iommu)
862 #ifdef CONFIG_IRQ_REMAP
863 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
866 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
867 get_order(GA_LOG_SIZE));
871 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
873 if (!iommu->ga_log_tail)
882 #endif /* CONFIG_IRQ_REMAP */
885 static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
887 iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO, 1);
889 return iommu->cmd_sem ? 0 : -ENOMEM;
892 static void __init free_cwwb_sem(struct amd_iommu *iommu)
895 free_page((unsigned long)iommu->cmd_sem);
898 static void iommu_enable_xt(struct amd_iommu *iommu)
900 #ifdef CONFIG_IRQ_REMAP
902 * XT mode (32-bit APIC destination ID) requires
903 * GA mode (128-bit IRTE support) as a prerequisite.
905 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
906 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
907 iommu_feature_enable(iommu, CONTROL_XT_EN);
908 #endif /* CONFIG_IRQ_REMAP */
911 static void iommu_enable_gt(struct amd_iommu *iommu)
913 if (!iommu_feature(iommu, FEATURE_GT))
916 iommu_feature_enable(iommu, CONTROL_GT_EN);
919 /* sets a specific bit in the device table entry. */
920 static void set_dev_entry_bit(u16 devid, u8 bit)
922 int i = (bit >> 6) & 0x03;
923 int _bit = bit & 0x3f;
925 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
928 static int get_dev_entry_bit(u16 devid, u8 bit)
930 int i = (bit >> 6) & 0x03;
931 int _bit = bit & 0x3f;
933 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
937 static bool copy_device_table(void)
939 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
940 struct dev_table_entry *old_devtb = NULL;
941 u32 lo, hi, devid, old_devtb_size;
942 phys_addr_t old_devtb_phys;
943 struct amd_iommu *iommu;
944 u16 dom_id, dte_v, irq_v;
948 if (!amd_iommu_pre_enabled)
951 pr_warn("Translation is already enabled - trying to copy translation structures\n");
952 for_each_iommu(iommu) {
953 /* All IOMMUs should use the same device table with the same size */
954 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
955 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
956 entry = (((u64) hi) << 32) + lo;
957 if (last_entry && last_entry != entry) {
958 pr_err("IOMMU:%d should use the same dev table as others!\n",
964 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
965 if (old_devtb_size != dev_table_size) {
966 pr_err("The device table size of IOMMU:%d is not expected!\n",
973 * When SME is enabled in the first kernel, the entry includes the
974 * memory encryption mask(sme_me_mask), we must remove the memory
975 * encryption mask to obtain the true physical address in kdump kernel.
977 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
979 if (old_devtb_phys >= 0x100000000ULL) {
980 pr_err("The address of old device table is above 4G, not trustworthy!\n");
983 old_devtb = (sme_active() && is_kdump_kernel())
984 ? (__force void *)ioremap_encrypted(old_devtb_phys,
986 : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
991 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
992 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
993 get_order(dev_table_size));
994 if (old_dev_tbl_cpy == NULL) {
995 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_IRQ_TABLE_LEN_MASK;
1022 if (irq_v && (int_ctl || int_tab_len)) {
1023 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
1024 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
1025 pr_err("Wrong old irq remapping flag: %#x\n", devid);
1029 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
1032 memunmap(old_devtb);
1037 void amd_iommu_apply_erratum_63(u16 devid)
1041 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
1042 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
1045 set_dev_entry_bit(devid, DEV_ENTRY_IW);
1048 /* Writes the specific IOMMU for a device into the rlookup table */
1049 static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
1051 amd_iommu_rlookup_table[devid] = iommu;
1055 * This function takes the device specific flags read from the ACPI
1056 * table and sets up the device table entry with that information
1058 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
1059 u16 devid, u32 flags, u32 ext_flags)
1061 if (flags & ACPI_DEVFLAG_INITPASS)
1062 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
1063 if (flags & ACPI_DEVFLAG_EXTINT)
1064 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
1065 if (flags & ACPI_DEVFLAG_NMI)
1066 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
1067 if (flags & ACPI_DEVFLAG_SYSMGT1)
1068 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
1069 if (flags & ACPI_DEVFLAG_SYSMGT2)
1070 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
1071 if (flags & ACPI_DEVFLAG_LINT0)
1072 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
1073 if (flags & ACPI_DEVFLAG_LINT1)
1074 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
1076 amd_iommu_apply_erratum_63(devid);
1078 set_iommu_for_device(iommu, devid);
1081 int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
1083 struct devid_map *entry;
1084 struct list_head *list;
1086 if (type == IVHD_SPECIAL_IOAPIC)
1088 else if (type == IVHD_SPECIAL_HPET)
1093 list_for_each_entry(entry, list, list) {
1094 if (!(entry->id == id && entry->cmd_line))
1097 pr_info("Command-line override present for %s id %d - ignoring\n",
1098 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1100 *devid = entry->devid;
1105 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1110 entry->devid = *devid;
1111 entry->cmd_line = cmd_line;
1113 list_add_tail(&entry->list, list);
1118 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1121 struct acpihid_map_entry *entry;
1122 struct list_head *list = &acpihid_map;
1124 list_for_each_entry(entry, list, list) {
1125 if (strcmp(entry->hid, hid) ||
1126 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1130 pr_info("Command-line override for hid:%s uid:%s\n",
1132 *devid = entry->devid;
1136 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1140 memcpy(entry->uid, uid, strlen(uid));
1141 memcpy(entry->hid, hid, strlen(hid));
1142 entry->devid = *devid;
1143 entry->cmd_line = cmd_line;
1144 entry->root_devid = (entry->devid & (~0x7));
1146 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1147 entry->cmd_line ? "cmd" : "ivrs",
1148 entry->hid, entry->uid, entry->root_devid);
1150 list_add_tail(&entry->list, list);
1154 static int __init add_early_maps(void)
1158 for (i = 0; i < early_ioapic_map_size; ++i) {
1159 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1160 early_ioapic_map[i].id,
1161 &early_ioapic_map[i].devid,
1162 early_ioapic_map[i].cmd_line);
1167 for (i = 0; i < early_hpet_map_size; ++i) {
1168 ret = add_special_device(IVHD_SPECIAL_HPET,
1169 early_hpet_map[i].id,
1170 &early_hpet_map[i].devid,
1171 early_hpet_map[i].cmd_line);
1176 for (i = 0; i < early_acpihid_map_size; ++i) {
1177 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1178 early_acpihid_map[i].uid,
1179 &early_acpihid_map[i].devid,
1180 early_acpihid_map[i].cmd_line);
1189 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1190 * initializes the hardware and our data structures with it.
1192 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1193 struct ivhd_header *h)
1196 u8 *end = p, flags = 0;
1197 u16 devid = 0, devid_start = 0, devid_to = 0;
1198 u32 dev_i, ext_flags = 0;
1200 struct ivhd_entry *e;
1205 ret = add_early_maps();
1209 amd_iommu_apply_ivrs_quirks();
1212 * First save the recommended feature enable bits from ACPI
1214 iommu->acpi_flags = h->flags;
1217 * Done. Now parse the device entries
1219 ivhd_size = get_ivhd_header_size(h);
1221 pr_err("Unsupported IVHD type %#x\n", h->type);
1231 e = (struct ivhd_entry *)p;
1235 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1237 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1238 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1240 case IVHD_DEV_SELECT:
1242 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1244 PCI_BUS_NUM(e->devid),
1250 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1252 case IVHD_DEV_SELECT_RANGE_START:
1254 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1255 "devid: %02x:%02x.%x flags: %02x\n",
1256 PCI_BUS_NUM(e->devid),
1261 devid_start = e->devid;
1266 case IVHD_DEV_ALIAS:
1268 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1269 "flags: %02x devid_to: %02x:%02x.%x\n",
1270 PCI_BUS_NUM(e->devid),
1274 PCI_BUS_NUM(e->ext >> 8),
1275 PCI_SLOT(e->ext >> 8),
1276 PCI_FUNC(e->ext >> 8));
1279 devid_to = e->ext >> 8;
1280 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1281 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1282 amd_iommu_alias_table[devid] = devid_to;
1284 case IVHD_DEV_ALIAS_RANGE:
1286 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1287 "devid: %02x:%02x.%x flags: %02x "
1288 "devid_to: %02x:%02x.%x\n",
1289 PCI_BUS_NUM(e->devid),
1293 PCI_BUS_NUM(e->ext >> 8),
1294 PCI_SLOT(e->ext >> 8),
1295 PCI_FUNC(e->ext >> 8));
1297 devid_start = e->devid;
1299 devid_to = e->ext >> 8;
1303 case IVHD_DEV_EXT_SELECT:
1305 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1306 "flags: %02x ext: %08x\n",
1307 PCI_BUS_NUM(e->devid),
1313 set_dev_entry_from_acpi(iommu, devid, e->flags,
1316 case IVHD_DEV_EXT_SELECT_RANGE:
1318 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1319 "%02x:%02x.%x flags: %02x ext: %08x\n",
1320 PCI_BUS_NUM(e->devid),
1325 devid_start = e->devid;
1330 case IVHD_DEV_RANGE_END:
1332 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1333 PCI_BUS_NUM(e->devid),
1335 PCI_FUNC(e->devid));
1338 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1340 amd_iommu_alias_table[dev_i] = devid_to;
1341 set_dev_entry_from_acpi(iommu,
1342 devid_to, flags, ext_flags);
1344 set_dev_entry_from_acpi(iommu, dev_i,
1348 case IVHD_DEV_SPECIAL: {
1354 handle = e->ext & 0xff;
1355 devid = (e->ext >> 8) & 0xffff;
1356 type = (e->ext >> 24) & 0xff;
1358 if (type == IVHD_SPECIAL_IOAPIC)
1360 else if (type == IVHD_SPECIAL_HPET)
1365 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1371 ret = add_special_device(type, handle, &devid, false);
1376 * add_special_device might update the devid in case a
1377 * command-line override is present. So call
1378 * set_dev_entry_from_acpi after add_special_device.
1380 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1384 case IVHD_DEV_ACPI_HID: {
1386 u8 hid[ACPIHID_HID_LEN];
1387 u8 uid[ACPIHID_UID_LEN];
1390 if (h->type != 0x40) {
1391 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1396 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1397 hid[ACPIHID_HID_LEN - 1] = '\0';
1400 pr_err(FW_BUG "Invalid HID.\n");
1406 case UID_NOT_PRESENT:
1409 pr_warn(FW_BUG "Invalid UID length.\n");
1412 case UID_IS_INTEGER:
1414 sprintf(uid, "%d", e->uid);
1417 case UID_IS_CHARACTER:
1419 memcpy(uid, &e->uid, e->uidl);
1420 uid[e->uidl] = '\0';
1428 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1436 ret = add_acpi_hid_device(hid, uid, &devid, false);
1441 * add_special_device might update the devid in case a
1442 * command-line override is present. So call
1443 * set_dev_entry_from_acpi after add_special_device.
1445 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1453 p += ivhd_entry_length(p);
1459 static void __init free_iommu_one(struct amd_iommu *iommu)
1461 free_cwwb_sem(iommu);
1462 free_command_buffer(iommu);
1463 free_event_buffer(iommu);
1464 free_ppr_log(iommu);
1466 iommu_unmap_mmio_space(iommu);
1469 static void __init free_iommu_all(void)
1471 struct amd_iommu *iommu, *next;
1473 for_each_iommu_safe(iommu, next) {
1474 list_del(&iommu->list);
1475 free_iommu_one(iommu);
1481 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1483 * BIOS should disable L2B micellaneous clock gating by setting
1484 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1486 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1490 if ((boot_cpu_data.x86 != 0x15) ||
1491 (boot_cpu_data.x86_model < 0x10) ||
1492 (boot_cpu_data.x86_model > 0x1f))
1495 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1496 pci_read_config_dword(iommu->dev, 0xf4, &value);
1501 /* Select NB indirect register 0x90 and enable writing */
1502 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1504 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1505 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
1507 /* Clear the enable writing bit */
1508 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1512 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1514 * BIOS should enable ATS write permission check by setting
1515 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1517 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1521 if ((boot_cpu_data.x86 != 0x15) ||
1522 (boot_cpu_data.x86_model < 0x30) ||
1523 (boot_cpu_data.x86_model > 0x3f))
1526 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1527 value = iommu_read_l2(iommu, 0x47);
1532 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1533 iommu_write_l2(iommu, 0x47, value | BIT(0));
1535 pci_info(iommu->dev, "Applying ATS write check workaround\n");
1539 * This function clues the initialization function for one IOMMU
1540 * together and also allocates the command buffer and programs the
1541 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1543 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1547 raw_spin_lock_init(&iommu->lock);
1548 iommu->cmd_sem_val = 0;
1550 /* Add IOMMU to internal data structures */
1551 list_add_tail(&iommu->list, &amd_iommu_list);
1552 iommu->index = amd_iommus_present++;
1554 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1555 WARN(1, "System has more IOMMUs than supported by this driver\n");
1559 /* Index is fine - add IOMMU to the array */
1560 amd_iommus[iommu->index] = iommu;
1563 * Copy data from ACPI table entry to the iommu struct
1565 iommu->devid = h->devid;
1566 iommu->cap_ptr = h->cap_ptr;
1567 iommu->pci_seg = h->pci_seg;
1568 iommu->mmio_phys = h->mmio_phys;
1572 /* Check if IVHD EFR contains proper max banks/counters */
1573 if ((h->efr_attr != 0) &&
1574 ((h->efr_attr & (0xF << 13)) != 0) &&
1575 ((h->efr_attr & (0x3F << 17)) != 0))
1576 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1578 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1581 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1582 * GAM also requires GA mode. Therefore, we need to
1583 * check cmpxchg16b support before enabling it.
1585 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1586 ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1587 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1591 if (h->efr_reg & (1 << 9))
1592 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1594 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1597 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1598 * XT, GAM also requires GA mode. Therefore, we need to
1599 * check cmpxchg16b support before enabling them.
1601 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1602 ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
1603 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1608 * Note: Since iommu_update_intcapxt() leverages
1609 * the IOMMU MMIO access to MSI capability block registers
1610 * for MSI address lo/hi/data, we need to check both
1611 * EFR[XtSup] and EFR[MsiCapMmioSup] for x2APIC support.
1613 if ((h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT)) &&
1614 (h->efr_reg & BIT(IOMMU_EFR_MSICAPMMIOSUP_SHIFT)))
1615 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1617 early_iommu_features_init(iommu, h);
1624 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1625 iommu->mmio_phys_end);
1626 if (!iommu->mmio_base)
1629 if (alloc_cwwb_sem(iommu))
1632 if (alloc_command_buffer(iommu))
1635 if (alloc_event_buffer(iommu))
1638 iommu->int_enabled = false;
1640 init_translation_status(iommu);
1641 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1642 iommu_disable(iommu);
1643 clear_translation_pre_enabled(iommu);
1644 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1647 if (amd_iommu_pre_enabled)
1648 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1650 ret = init_iommu_from_acpi(iommu, h);
1654 ret = amd_iommu_create_irq_domain(iommu);
1659 * Make sure IOMMU is not considered to translate itself. The IVRS
1660 * table tells us so, but this is a lie!
1662 amd_iommu_rlookup_table[iommu->devid] = NULL;
1668 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1669 * @ivrs: Pointer to the IVRS header
1671 * This function search through all IVDB of the maximum supported IVHD
1673 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1675 u8 *base = (u8 *)ivrs;
1676 struct ivhd_header *ivhd = (struct ivhd_header *)
1677 (base + IVRS_HEADER_LENGTH);
1678 u8 last_type = ivhd->type;
1679 u16 devid = ivhd->devid;
1681 while (((u8 *)ivhd - base < ivrs->length) &&
1682 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1683 u8 *p = (u8 *) ivhd;
1685 if (ivhd->devid == devid)
1686 last_type = ivhd->type;
1687 ivhd = (struct ivhd_header *)(p + ivhd->length);
1694 * Iterates over all IOMMU entries in the ACPI table, allocates the
1695 * IOMMU structure and initializes it with init_iommu_one()
1697 static int __init init_iommu_all(struct acpi_table_header *table)
1699 u8 *p = (u8 *)table, *end = (u8 *)table;
1700 struct ivhd_header *h;
1701 struct amd_iommu *iommu;
1704 end += table->length;
1705 p += IVRS_HEADER_LENGTH;
1708 h = (struct ivhd_header *)p;
1709 if (*p == amd_iommu_target_ivhd_type) {
1711 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1712 "seg: %d flags: %01x info %04x\n",
1713 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
1714 PCI_FUNC(h->devid), h->cap_ptr,
1715 h->pci_seg, h->flags, h->info);
1716 DUMP_printk(" mmio-addr: %016llx\n",
1719 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1723 ret = init_iommu_one(iommu, h);
1735 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1738 struct pci_dev *pdev = iommu->dev;
1740 if (!iommu_feature(iommu, FEATURE_PC))
1743 amd_iommu_pc_present = true;
1745 pci_info(pdev, "IOMMU performance counters supported\n");
1747 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1748 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1749 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1754 static ssize_t amd_iommu_show_cap(struct device *dev,
1755 struct device_attribute *attr,
1758 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1759 return sprintf(buf, "%x\n", iommu->cap);
1761 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1763 static ssize_t amd_iommu_show_features(struct device *dev,
1764 struct device_attribute *attr,
1767 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1768 return sprintf(buf, "%llx\n", iommu->features);
1770 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1772 static struct attribute *amd_iommu_attrs[] = {
1774 &dev_attr_features.attr,
1778 static struct attribute_group amd_iommu_group = {
1779 .name = "amd-iommu",
1780 .attrs = amd_iommu_attrs,
1783 static const struct attribute_group *amd_iommu_groups[] = {
1789 * Note: IVHD 0x11 and 0x40 also contains exact copy
1790 * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
1791 * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
1793 static void __init late_iommu_features_init(struct amd_iommu *iommu)
1797 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
1800 /* read extended feature bits */
1801 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1803 if (!iommu->features) {
1804 iommu->features = features;
1809 * Sanity check and warn if EFR values from
1810 * IVHD and MMIO conflict.
1812 if (features != iommu->features)
1813 pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx).\n",
1814 features, iommu->features);
1817 static int __init iommu_init_pci(struct amd_iommu *iommu)
1819 int cap_ptr = iommu->cap_ptr;
1822 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1823 iommu->devid & 0xff);
1827 /* Prevent binding other PCI device drivers to IOMMU devices */
1828 iommu->dev->match_driver = false;
1830 /* ACPI _PRT won't have an IRQ for IOMMU */
1831 iommu->dev->irq_managed = 1;
1833 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1836 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1837 amd_iommu_iotlb_sup = false;
1839 late_iommu_features_init(iommu);
1841 if (iommu_feature(iommu, FEATURE_GT)) {
1846 pasmax = iommu->features & FEATURE_PASID_MASK;
1847 pasmax >>= FEATURE_PASID_SHIFT;
1848 max_pasid = (1 << (pasmax + 1)) - 1;
1850 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1852 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
1854 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1855 glxval >>= FEATURE_GLXVAL_SHIFT;
1857 if (amd_iommu_max_glx_val == -1)
1858 amd_iommu_max_glx_val = glxval;
1860 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1863 if (iommu_feature(iommu, FEATURE_GT) &&
1864 iommu_feature(iommu, FEATURE_PPR)) {
1865 iommu->is_iommu_v2 = true;
1866 amd_iommu_v2_present = true;
1869 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1872 ret = iommu_init_ga_log(iommu);
1876 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1877 amd_iommu_np_cache = true;
1879 init_iommu_perf_ctr(iommu);
1881 if (is_rd890_iommu(iommu->dev)) {
1885 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1889 * Some rd890 systems may not be fully reconfigured by the
1890 * BIOS, so it's necessary for us to store this information so
1891 * it can be reprogrammed on resume
1893 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1894 &iommu->stored_addr_lo);
1895 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1896 &iommu->stored_addr_hi);
1898 /* Low bit locks writes to configuration space */
1899 iommu->stored_addr_lo &= ~1;
1901 for (i = 0; i < 6; i++)
1902 for (j = 0; j < 0x12; j++)
1903 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1905 for (i = 0; i < 0x83; i++)
1906 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1909 amd_iommu_erratum_746_workaround(iommu);
1910 amd_iommu_ats_write_check_workaround(iommu);
1912 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1913 amd_iommu_groups, "ivhd%d", iommu->index);
1914 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1915 iommu_device_register(&iommu->iommu);
1917 return pci_enable_device(iommu->dev);
1920 static void print_iommu_info(void)
1922 static const char * const feat_str[] = {
1923 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1924 "IA", "GA", "HE", "PC"
1926 struct amd_iommu *iommu;
1928 for_each_iommu(iommu) {
1929 struct pci_dev *pdev = iommu->dev;
1932 pci_info(pdev, "Found IOMMU cap 0x%hx\n", iommu->cap_ptr);
1934 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1935 pr_info("Extended features (%#llx):", iommu->features);
1937 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
1938 if (iommu_feature(iommu, (1ULL << i)))
1939 pr_cont(" %s", feat_str[i]);
1942 if (iommu->features & FEATURE_GAM_VAPIC)
1943 pr_cont(" GA_vAPIC");
1948 if (irq_remapping_enabled) {
1949 pr_info("Interrupt remapping enabled\n");
1950 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1951 pr_info("Virtual APIC enabled\n");
1952 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1953 pr_info("X2APIC enabled\n");
1957 static int __init amd_iommu_init_pci(void)
1959 struct amd_iommu *iommu;
1962 for_each_iommu(iommu) {
1963 ret = iommu_init_pci(iommu);
1967 /* Need to setup range after PCI init */
1968 iommu_set_cwwb_range(iommu);
1972 * Order is important here to make sure any unity map requirements are
1973 * fulfilled. The unity mappings are created and written to the device
1974 * table during the amd_iommu_init_api() call.
1976 * After that we call init_device_table_dma() to make sure any
1977 * uninitialized DTE will block DMA, and in the end we flush the caches
1978 * of all IOMMUs to make sure the changes to the device table are
1981 ret = amd_iommu_init_api();
1983 init_device_table_dma();
1985 for_each_iommu(iommu)
1986 iommu_flush_all_caches(iommu);
1994 /****************************************************************************
1996 * The following functions initialize the MSI interrupts for all IOMMUs
1997 * in the system. It's a bit challenging because there could be multiple
1998 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
2001 ****************************************************************************/
2003 static int iommu_setup_msi(struct amd_iommu *iommu)
2007 r = pci_enable_msi(iommu->dev);
2011 r = request_threaded_irq(iommu->dev->irq,
2012 amd_iommu_int_handler,
2013 amd_iommu_int_thread,
2018 pci_disable_msi(iommu->dev);
2022 iommu->int_enabled = true;
2027 #define XT_INT_DEST_MODE(x) (((x) & 0x1ULL) << 2)
2028 #define XT_INT_DEST_LO(x) (((x) & 0xFFFFFFULL) << 8)
2029 #define XT_INT_VEC(x) (((x) & 0xFFULL) << 32)
2030 #define XT_INT_DEST_HI(x) ((((x) >> 24) & 0xFFULL) << 56)
2033 * Setup the IntCapXT registers with interrupt routing information
2034 * based on the PCI MSI capability block registers, accessed via
2035 * MMIO MSI address low/hi and MSI data registers.
2037 static void iommu_update_intcapxt(struct amd_iommu *iommu)
2040 u32 addr_lo = readl(iommu->mmio_base + MMIO_MSI_ADDR_LO_OFFSET);
2041 u32 addr_hi = readl(iommu->mmio_base + MMIO_MSI_ADDR_HI_OFFSET);
2042 u32 data = readl(iommu->mmio_base + MMIO_MSI_DATA_OFFSET);
2043 bool dm = (addr_lo >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
2044 u32 dest = ((addr_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xFF);
2046 if (x2apic_enabled())
2047 dest |= MSI_ADDR_EXT_DEST_ID(addr_hi);
2049 val = XT_INT_VEC(data & 0xFF) |
2050 XT_INT_DEST_MODE(dm) |
2051 XT_INT_DEST_LO(dest) |
2052 XT_INT_DEST_HI(dest);
2055 * Current IOMMU implemtation uses the same IRQ for all
2056 * 3 IOMMU interrupts.
2058 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2059 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2060 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2063 static void _irq_notifier_notify(struct irq_affinity_notify *notify,
2064 const cpumask_t *mask)
2066 struct amd_iommu *iommu;
2068 for_each_iommu(iommu) {
2069 if (iommu->dev->irq == notify->irq) {
2070 iommu_update_intcapxt(iommu);
2076 static void _irq_notifier_release(struct kref *ref)
2080 static int iommu_init_intcapxt(struct amd_iommu *iommu)
2083 struct irq_affinity_notify *notify = &iommu->intcapxt_notify;
2086 * IntCapXT requires XTSup=1 and MsiCapMmioSup=1,
2087 * which can be inferred from amd_iommu_xt_mode.
2089 if (amd_iommu_xt_mode != IRQ_REMAP_X2APIC_MODE)
2093 * Also, we need to setup notifier to update the IntCapXT registers
2094 * whenever the irq affinity is changed from user-space.
2096 notify->irq = iommu->dev->irq;
2097 notify->notify = _irq_notifier_notify,
2098 notify->release = _irq_notifier_release,
2099 ret = irq_set_affinity_notifier(iommu->dev->irq, notify);
2101 pr_err("Failed to register irq affinity notifier (devid=%#x, irq %d)\n",
2102 iommu->devid, iommu->dev->irq);
2106 iommu_update_intcapxt(iommu);
2107 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2111 static int iommu_init_msi(struct amd_iommu *iommu)
2115 if (iommu->int_enabled)
2118 if (iommu->dev->msi_cap)
2119 ret = iommu_setup_msi(iommu);
2127 ret = iommu_init_intcapxt(iommu);
2131 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2133 if (iommu->ppr_log != NULL)
2134 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
2136 iommu_ga_log_enable(iommu);
2141 /****************************************************************************
2143 * The next functions belong to the third pass of parsing the ACPI
2144 * table. In this last pass the memory mapping requirements are
2145 * gathered (like exclusion and unity mapping ranges).
2147 ****************************************************************************/
2149 static void __init free_unity_maps(void)
2151 struct unity_map_entry *entry, *next;
2153 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
2154 list_del(&entry->list);
2159 /* called for unity map ACPI definition */
2160 static int __init init_unity_map_range(struct ivmd_header *m)
2162 struct unity_map_entry *e = NULL;
2165 e = kzalloc(sizeof(*e), GFP_KERNEL);
2173 case ACPI_IVMD_TYPE:
2174 s = "IVMD_TYPEi\t\t\t";
2175 e->devid_start = e->devid_end = m->devid;
2177 case ACPI_IVMD_TYPE_ALL:
2178 s = "IVMD_TYPE_ALL\t\t";
2180 e->devid_end = amd_iommu_last_bdf;
2182 case ACPI_IVMD_TYPE_RANGE:
2183 s = "IVMD_TYPE_RANGE\t\t";
2184 e->devid_start = m->devid;
2185 e->devid_end = m->aux;
2188 e->address_start = PAGE_ALIGN(m->range_start);
2189 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2190 e->prot = m->flags >> 1;
2193 * Treat per-device exclusion ranges as r/w unity-mapped regions
2194 * since some buggy BIOSes might lead to the overwritten exclusion
2195 * range (exclusion_start and exclusion_length members). This
2196 * happens when there are multiple exclusion ranges (IVMD entries)
2197 * defined in ACPI table.
2199 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2200 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
2202 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2203 " range_start: %016llx range_end: %016llx flags: %x\n", s,
2204 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2205 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
2206 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2207 e->address_start, e->address_end, m->flags);
2209 list_add_tail(&e->list, &amd_iommu_unity_map);
2214 /* iterates over all memory definitions we find in the ACPI table */
2215 static int __init init_memory_definitions(struct acpi_table_header *table)
2217 u8 *p = (u8 *)table, *end = (u8 *)table;
2218 struct ivmd_header *m;
2220 end += table->length;
2221 p += IVRS_HEADER_LENGTH;
2224 m = (struct ivmd_header *)p;
2225 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
2226 init_unity_map_range(m);
2235 * Init the device table to not allow DMA access for devices
2237 static void init_device_table_dma(void)
2241 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2242 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2243 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
2247 static void __init uninit_device_table_dma(void)
2251 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2252 amd_iommu_dev_table[devid].data[0] = 0ULL;
2253 amd_iommu_dev_table[devid].data[1] = 0ULL;
2257 static void init_device_table(void)
2261 if (!amd_iommu_irq_remap)
2264 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2265 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2268 static void iommu_init_flags(struct amd_iommu *iommu)
2270 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2271 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2272 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2274 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2275 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2276 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2278 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2279 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2280 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2282 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2283 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2284 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2287 * make IOMMU memory accesses cache coherent
2289 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2291 /* Set IOTLB invalidation timeout to 1s */
2292 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2295 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2298 u32 ioc_feature_control;
2299 struct pci_dev *pdev = iommu->root_pdev;
2301 /* RD890 BIOSes may not have completely reconfigured the iommu */
2302 if (!is_rd890_iommu(iommu->dev) || !pdev)
2306 * First, we need to ensure that the iommu is enabled. This is
2307 * controlled by a register in the northbridge
2310 /* Select Northbridge indirect register 0x75 and enable writing */
2311 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2312 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2314 /* Enable the iommu */
2315 if (!(ioc_feature_control & 0x1))
2316 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2318 /* Restore the iommu BAR */
2319 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2320 iommu->stored_addr_lo);
2321 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2322 iommu->stored_addr_hi);
2324 /* Restore the l1 indirect regs for each of the 6 l1s */
2325 for (i = 0; i < 6; i++)
2326 for (j = 0; j < 0x12; j++)
2327 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2329 /* Restore the l2 indirect regs */
2330 for (i = 0; i < 0x83; i++)
2331 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2333 /* Lock PCI setup registers */
2334 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2335 iommu->stored_addr_lo | 1);
2338 static void iommu_enable_ga(struct amd_iommu *iommu)
2340 #ifdef CONFIG_IRQ_REMAP
2341 switch (amd_iommu_guest_ir) {
2342 case AMD_IOMMU_GUEST_IR_VAPIC:
2343 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2345 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2346 iommu_feature_enable(iommu, CONTROL_GA_EN);
2347 iommu->irte_ops = &irte_128_ops;
2350 iommu->irte_ops = &irte_32_ops;
2356 static void early_enable_iommu(struct amd_iommu *iommu)
2358 iommu_disable(iommu);
2359 iommu_init_flags(iommu);
2360 iommu_set_device_table(iommu);
2361 iommu_enable_command_buffer(iommu);
2362 iommu_enable_event_buffer(iommu);
2363 iommu_set_exclusion_range(iommu);
2364 iommu_enable_ga(iommu);
2365 iommu_enable_xt(iommu);
2366 iommu_enable(iommu);
2367 iommu_flush_all_caches(iommu);
2371 * This function finally enables all IOMMUs found in the system after
2372 * they have been initialized.
2374 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2375 * the old content of device table entries. Not this case or copy failed,
2376 * just continue as normal kernel does.
2378 static void early_enable_iommus(void)
2380 struct amd_iommu *iommu;
2383 if (!copy_device_table()) {
2385 * If come here because of failure in copying device table from old
2386 * kernel with all IOMMUs enabled, print error message and try to
2387 * free allocated old_dev_tbl_cpy.
2389 if (amd_iommu_pre_enabled)
2390 pr_err("Failed to copy DEV table from previous kernel.\n");
2391 if (old_dev_tbl_cpy != NULL)
2392 free_pages((unsigned long)old_dev_tbl_cpy,
2393 get_order(dev_table_size));
2395 for_each_iommu(iommu) {
2396 clear_translation_pre_enabled(iommu);
2397 early_enable_iommu(iommu);
2400 pr_info("Copied DEV table from previous kernel.\n");
2401 free_pages((unsigned long)amd_iommu_dev_table,
2402 get_order(dev_table_size));
2403 amd_iommu_dev_table = old_dev_tbl_cpy;
2404 for_each_iommu(iommu) {
2405 iommu_disable_command_buffer(iommu);
2406 iommu_disable_event_buffer(iommu);
2407 iommu_enable_command_buffer(iommu);
2408 iommu_enable_event_buffer(iommu);
2409 iommu_enable_ga(iommu);
2410 iommu_enable_xt(iommu);
2411 iommu_set_device_table(iommu);
2412 iommu_flush_all_caches(iommu);
2416 #ifdef CONFIG_IRQ_REMAP
2418 * Note: We have already checked GASup from IVRS table.
2419 * Now, we need to make sure that GAMSup is set.
2421 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2422 !check_feature_on_all_iommus(FEATURE_GAM_VAPIC))
2423 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2425 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2426 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2430 static void enable_iommus_v2(void)
2432 struct amd_iommu *iommu;
2434 for_each_iommu(iommu) {
2435 iommu_enable_ppr_log(iommu);
2436 iommu_enable_gt(iommu);
2440 static void enable_iommus(void)
2442 early_enable_iommus();
2447 static void disable_iommus(void)
2449 struct amd_iommu *iommu;
2451 for_each_iommu(iommu)
2452 iommu_disable(iommu);
2454 #ifdef CONFIG_IRQ_REMAP
2455 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2456 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2461 * Suspend/Resume support
2462 * disable suspend until real resume implemented
2465 static void amd_iommu_resume(void)
2467 struct amd_iommu *iommu;
2469 for_each_iommu(iommu)
2470 iommu_apply_resume_quirks(iommu);
2472 /* re-load the hardware */
2475 amd_iommu_enable_interrupts();
2478 static int amd_iommu_suspend(void)
2480 /* disable IOMMUs to go out of the way for BIOS */
2486 static struct syscore_ops amd_iommu_syscore_ops = {
2487 .suspend = amd_iommu_suspend,
2488 .resume = amd_iommu_resume,
2491 static void __init free_iommu_resources(void)
2493 kmemleak_free(irq_lookup_table);
2494 free_pages((unsigned long)irq_lookup_table,
2495 get_order(rlookup_table_size));
2496 irq_lookup_table = NULL;
2498 kmem_cache_destroy(amd_iommu_irq_cache);
2499 amd_iommu_irq_cache = NULL;
2501 free_pages((unsigned long)amd_iommu_rlookup_table,
2502 get_order(rlookup_table_size));
2503 amd_iommu_rlookup_table = NULL;
2505 free_pages((unsigned long)amd_iommu_alias_table,
2506 get_order(alias_table_size));
2507 amd_iommu_alias_table = NULL;
2509 free_pages((unsigned long)amd_iommu_dev_table,
2510 get_order(dev_table_size));
2511 amd_iommu_dev_table = NULL;
2516 /* SB IOAPIC is always on this device in AMD systems */
2517 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2519 static bool __init check_ioapic_information(void)
2521 const char *fw_bug = FW_BUG;
2522 bool ret, has_sb_ioapic;
2525 has_sb_ioapic = false;
2529 * If we have map overrides on the kernel command line the
2530 * messages in this function might not describe firmware bugs
2531 * anymore - so be careful
2536 for (idx = 0; idx < nr_ioapics; idx++) {
2537 int devid, id = mpc_ioapic_id(idx);
2539 devid = get_ioapic_devid(id);
2541 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2544 } else if (devid == IOAPIC_SB_DEVID) {
2545 has_sb_ioapic = true;
2550 if (!has_sb_ioapic) {
2552 * We expect the SB IOAPIC to be listed in the IVRS
2553 * table. The system timer is connected to the SB IOAPIC
2554 * and if we don't have it in the list the system will
2555 * panic at boot time. This situation usually happens
2556 * when the BIOS is buggy and provides us the wrong
2557 * device id for the IOAPIC in the system.
2559 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2563 pr_err("Disabling interrupt remapping\n");
2568 static void __init free_dma_resources(void)
2570 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2571 get_order(MAX_DOMAIN_ID/8));
2572 amd_iommu_pd_alloc_bitmap = NULL;
2577 static void __init ivinfo_init(void *ivrs)
2579 amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
2583 * This is the hardware init function for AMD IOMMU in the system.
2584 * This function is called either from amd_iommu_init or from the interrupt
2585 * remapping setup code.
2587 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2590 * 1 pass) Discover the most comprehensive IVHD type to use.
2592 * 2 pass) Find the highest PCI device id the driver has to handle.
2593 * Upon this information the size of the data structures is
2594 * determined that needs to be allocated.
2596 * 3 pass) Initialize the data structures just allocated with the
2597 * information in the ACPI table about available AMD IOMMUs
2598 * in the system. It also maps the PCI devices in the
2599 * system to specific IOMMUs
2601 * 4 pass) After the basic data structures are allocated and
2602 * initialized we update them with information about memory
2603 * remapping requirements parsed out of the ACPI table in
2606 * After everything is set up the IOMMUs are enabled and the necessary
2607 * hotplug and suspend notifiers are registered.
2609 static int __init early_amd_iommu_init(void)
2611 struct acpi_table_header *ivrs_base;
2613 int i, remap_cache_sz, ret = 0;
2616 if (!amd_iommu_detected)
2619 status = acpi_get_table("IVRS", 0, &ivrs_base);
2620 if (status == AE_NOT_FOUND)
2622 else if (ACPI_FAILURE(status)) {
2623 const char *err = acpi_format_exception(status);
2624 pr_err("IVRS table error: %s\n", err);
2629 * Validate checksum here so we don't need to do it when
2630 * we actually parse the table
2632 ret = check_ivrs_checksum(ivrs_base);
2636 ivinfo_init(ivrs_base);
2638 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2639 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2642 * First parse ACPI tables to find the largest Bus/Dev/Func
2643 * we need to handle. Upon this information the shared data
2644 * structures for the IOMMUs in the system will be allocated
2646 ret = find_last_devid_acpi(ivrs_base);
2650 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2651 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2652 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
2654 /* Device table - directly used by all IOMMUs */
2656 amd_iommu_dev_table = (void *)__get_free_pages(
2657 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
2658 get_order(dev_table_size));
2659 if (amd_iommu_dev_table == NULL)
2663 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2664 * IOMMU see for that device
2666 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2667 get_order(alias_table_size));
2668 if (amd_iommu_alias_table == NULL)
2671 /* IOMMU rlookup table - find the IOMMU for a specific device */
2672 amd_iommu_rlookup_table = (void *)__get_free_pages(
2673 GFP_KERNEL | __GFP_ZERO,
2674 get_order(rlookup_table_size));
2675 if (amd_iommu_rlookup_table == NULL)
2678 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2679 GFP_KERNEL | __GFP_ZERO,
2680 get_order(MAX_DOMAIN_ID/8));
2681 if (amd_iommu_pd_alloc_bitmap == NULL)
2685 * let all alias entries point to itself
2687 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2688 amd_iommu_alias_table[i] = i;
2691 * never allocate domain 0 because its used as the non-allocated and
2692 * error value placeholder
2694 __set_bit(0, amd_iommu_pd_alloc_bitmap);
2697 * now the data structures are allocated and basically initialized
2698 * start the real acpi table scan
2700 ret = init_iommu_all(ivrs_base);
2704 /* Disable IOMMU if there's Stoney Ridge graphics */
2705 for (i = 0; i < 32; i++) {
2706 pci_id = read_pci_config(0, i, 0, 0);
2707 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
2708 pr_info("Disable IOMMU on Stoney Ridge\n");
2709 amd_iommu_disabled = true;
2714 /* Disable any previously enabled IOMMUs */
2715 if (!is_kdump_kernel() || amd_iommu_disabled)
2718 if (amd_iommu_irq_remap)
2719 amd_iommu_irq_remap = check_ioapic_information();
2721 if (amd_iommu_irq_remap) {
2723 * Interrupt remapping enabled, create kmem_cache for the
2727 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2728 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2730 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
2731 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
2733 IRQ_TABLE_ALIGNMENT,
2735 if (!amd_iommu_irq_cache)
2738 irq_lookup_table = (void *)__get_free_pages(
2739 GFP_KERNEL | __GFP_ZERO,
2740 get_order(rlookup_table_size));
2741 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2743 if (!irq_lookup_table)
2747 ret = init_memory_definitions(ivrs_base);
2751 /* init the device table */
2752 init_device_table();
2755 /* Don't leak any ACPI memory */
2756 acpi_put_table(ivrs_base);
2762 static int amd_iommu_enable_interrupts(void)
2764 struct amd_iommu *iommu;
2767 for_each_iommu(iommu) {
2768 ret = iommu_init_msi(iommu);
2777 static bool detect_ivrs(void)
2779 struct acpi_table_header *ivrs_base;
2782 status = acpi_get_table("IVRS", 0, &ivrs_base);
2783 if (status == AE_NOT_FOUND)
2785 else if (ACPI_FAILURE(status)) {
2786 const char *err = acpi_format_exception(status);
2787 pr_err("IVRS table error: %s\n", err);
2791 acpi_put_table(ivrs_base);
2793 /* Make sure ACS will be enabled during PCI probe */
2799 /****************************************************************************
2801 * AMD IOMMU Initialization State Machine
2803 ****************************************************************************/
2805 static int __init state_next(void)
2809 switch (init_state) {
2810 case IOMMU_START_STATE:
2811 if (!detect_ivrs()) {
2812 init_state = IOMMU_NOT_FOUND;
2815 init_state = IOMMU_IVRS_DETECTED;
2818 case IOMMU_IVRS_DETECTED:
2819 ret = early_amd_iommu_init();
2820 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2821 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2822 pr_info("AMD IOMMU disabled\n");
2823 init_state = IOMMU_CMDLINE_DISABLED;
2827 case IOMMU_ACPI_FINISHED:
2828 early_enable_iommus();
2829 x86_platform.iommu_shutdown = disable_iommus;
2830 init_state = IOMMU_ENABLED;
2833 register_syscore_ops(&amd_iommu_syscore_ops);
2834 ret = amd_iommu_init_pci();
2835 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2838 case IOMMU_PCI_INIT:
2839 ret = amd_iommu_enable_interrupts();
2840 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2842 case IOMMU_INTERRUPTS_EN:
2843 ret = amd_iommu_init_dma_ops();
2844 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2847 init_state = IOMMU_INITIALIZED;
2849 case IOMMU_INITIALIZED:
2852 case IOMMU_NOT_FOUND:
2853 case IOMMU_INIT_ERROR:
2854 case IOMMU_CMDLINE_DISABLED:
2855 /* Error states => do nothing */
2864 free_dma_resources();
2865 if (!irq_remapping_enabled) {
2867 free_iommu_resources();
2869 struct amd_iommu *iommu;
2871 uninit_device_table_dma();
2872 for_each_iommu(iommu)
2873 iommu_flush_all_caches(iommu);
2879 static int __init iommu_go_to_state(enum iommu_init_state state)
2883 while (init_state != state) {
2884 if (init_state == IOMMU_NOT_FOUND ||
2885 init_state == IOMMU_INIT_ERROR ||
2886 init_state == IOMMU_CMDLINE_DISABLED)
2894 #ifdef CONFIG_IRQ_REMAP
2895 int __init amd_iommu_prepare(void)
2899 amd_iommu_irq_remap = true;
2901 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2904 return amd_iommu_irq_remap ? 0 : -ENODEV;
2907 int __init amd_iommu_enable(void)
2911 ret = iommu_go_to_state(IOMMU_ENABLED);
2915 irq_remapping_enabled = 1;
2916 return amd_iommu_xt_mode;
2919 void amd_iommu_disable(void)
2921 amd_iommu_suspend();
2924 int amd_iommu_reenable(int mode)
2931 int __init amd_iommu_enable_faulting(void)
2933 /* We enable MSI later when PCI is initialized */
2939 * This is the core init function for AMD IOMMU hardware in the system.
2940 * This function is called from the generic x86 DMA layer initialization
2943 static int __init amd_iommu_init(void)
2945 struct amd_iommu *iommu;
2948 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2949 #ifdef CONFIG_GART_IOMMU
2950 if (ret && list_empty(&amd_iommu_list)) {
2952 * We failed to initialize the AMD IOMMU - try fallback
2953 * to GART if possible.
2959 for_each_iommu(iommu)
2960 amd_iommu_debugfs_setup(iommu);
2965 static bool amd_iommu_sme_check(void)
2967 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
2970 /* For Fam17h, a specific level of support is required */
2971 if (boot_cpu_data.microcode >= 0x08001205)
2974 if ((boot_cpu_data.microcode >= 0x08001126) &&
2975 (boot_cpu_data.microcode <= 0x080011ff))
2978 pr_notice("IOMMU not currently supported when SME is active\n");
2983 /****************************************************************************
2985 * Early detect code. This code runs at IOMMU detection time in the DMA
2986 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2989 ****************************************************************************/
2990 int __init amd_iommu_detect(void)
2994 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
2997 if (!amd_iommu_sme_check())
3000 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
3004 amd_iommu_detected = true;
3006 x86_init.iommu.iommu_init = amd_iommu_init;
3011 /****************************************************************************
3013 * Parsing functions for the AMD IOMMU specific kernel command line
3016 ****************************************************************************/
3018 static int __init parse_amd_iommu_dump(char *str)
3020 amd_iommu_dump = true;
3025 static int __init parse_amd_iommu_intr(char *str)
3027 for (; *str; ++str) {
3028 if (strncmp(str, "legacy", 6) == 0) {
3029 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
3032 if (strncmp(str, "vapic", 5) == 0) {
3033 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
3040 static int __init parse_amd_iommu_options(char *str)
3042 for (; *str; ++str) {
3043 if (strncmp(str, "fullflush", 9) == 0)
3044 amd_iommu_unmap_flush = true;
3045 if (strncmp(str, "off", 3) == 0)
3046 amd_iommu_disabled = true;
3047 if (strncmp(str, "force_isolation", 15) == 0)
3048 amd_iommu_force_isolation = true;
3054 static int __init parse_ivrs_ioapic(char *str)
3056 u32 seg = 0, bus, dev, fn;
3060 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3061 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3064 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3065 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3066 pr_warn("ivrs_ioapic%s option format deprecated; use ivrs_ioapic=%d@%04x:%02x:%02x.%d instead\n",
3067 str, id, seg, bus, dev, fn);
3071 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
3075 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
3076 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
3081 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3083 cmdline_maps = true;
3084 i = early_ioapic_map_size++;
3085 early_ioapic_map[i].id = id;
3086 early_ioapic_map[i].devid = devid;
3087 early_ioapic_map[i].cmd_line = true;
3092 static int __init parse_ivrs_hpet(char *str)
3094 u32 seg = 0, bus, dev, fn;
3098 if (sscanf(str, "=%d@%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3099 sscanf(str, "=%d@%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5)
3102 if (sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn) == 4 ||
3103 sscanf(str, "[%d]=%x:%x:%x.%x", &id, &seg, &bus, &dev, &fn) == 5) {
3104 pr_warn("ivrs_hpet%s option format deprecated; use ivrs_hpet=%d@%04x:%02x:%02x.%d instead\n",
3105 str, id, seg, bus, dev, fn);
3109 pr_err("Invalid command line: ivrs_hpet%s\n", str);
3113 if (early_hpet_map_size == EARLY_MAP_SIZE) {
3114 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3119 devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3121 cmdline_maps = true;
3122 i = early_hpet_map_size++;
3123 early_hpet_map[i].id = id;
3124 early_hpet_map[i].devid = devid;
3125 early_hpet_map[i].cmd_line = true;
3130 #define ACPIID_LEN (ACPIHID_UID_LEN + ACPIHID_HID_LEN)
3132 static int __init parse_ivrs_acpihid(char *str)
3134 u32 seg = 0, bus, dev, fn;
3135 char *hid, *uid, *p, *addr;
3136 char acpiid[ACPIID_LEN] = {0};
3139 addr = strchr(str, '@');
3141 addr = strchr(str, '=');
3147 if (strlen(addr) > ACPIID_LEN)
3150 if (sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid) == 4 ||
3151 sscanf(str, "[%x:%x:%x.%x]=%s", &seg, &bus, &dev, &fn, acpiid) == 5) {
3152 pr_warn("ivrs_acpihid%s option format deprecated; use ivrs_acpihid=%s@%04x:%02x:%02x.%d instead\n",
3153 str, acpiid, seg, bus, dev, fn);
3159 /* We have the '@', make it the terminator to get just the acpiid */
3162 if (strlen(str) > ACPIID_LEN + 1)
3165 if (sscanf(str, "=%s", acpiid) != 1)
3168 if (sscanf(addr, "%x:%x.%x", &bus, &dev, &fn) == 3 ||
3169 sscanf(addr, "%x:%x:%x.%x", &seg, &bus, &dev, &fn) == 4)
3173 pr_err("Invalid command line: ivrs_acpihid%s\n", str);
3178 hid = strsep(&p, ":");
3181 if (!hid || !(*hid) || !uid) {
3182 pr_err("Invalid command line: hid or uid\n");
3187 * Ignore leading zeroes after ':', so e.g., AMDI0095:00
3188 * will match AMDI0095:0 in the second strcmp in acpi_dev_hid_uid_match
3190 while (*uid == '0' && *(uid + 1))
3193 i = early_acpihid_map_size++;
3194 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3195 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3196 early_acpihid_map[i].devid = IVRS_GET_SBDF_ID(seg, bus, dev, fn);
3197 early_acpihid_map[i].cmd_line = true;
3202 __setup("amd_iommu_dump", parse_amd_iommu_dump);
3203 __setup("amd_iommu=", parse_amd_iommu_options);
3204 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
3205 __setup("ivrs_ioapic", parse_ivrs_ioapic);
3206 __setup("ivrs_hpet", parse_ivrs_hpet);
3207 __setup("ivrs_acpihid", parse_ivrs_acpihid);
3209 IOMMU_INIT_FINISH(amd_iommu_detect,
3210 gart_iommu_hole_init,
3214 bool amd_iommu_v2_supported(void)
3216 return amd_iommu_v2_present;
3218 EXPORT_SYMBOL(amd_iommu_v2_supported);
3220 struct amd_iommu *get_amd_iommu(unsigned int idx)
3223 struct amd_iommu *iommu;
3225 for_each_iommu(iommu)
3230 EXPORT_SYMBOL(get_amd_iommu);
3232 /****************************************************************************
3234 * IOMMU EFR Performance Counter support functionality. This code allows
3235 * access to the IOMMU PC functionality.
3237 ****************************************************************************/
3239 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3241 struct amd_iommu *iommu = get_amd_iommu(idx);
3244 return iommu->max_banks;
3248 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3250 bool amd_iommu_pc_supported(void)
3252 return amd_iommu_pc_present;
3254 EXPORT_SYMBOL(amd_iommu_pc_supported);
3256 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3258 struct amd_iommu *iommu = get_amd_iommu(idx);
3261 return iommu->max_counters;
3265 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3267 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3268 u8 fxn, u64 *value, bool is_write)
3273 /* Make sure the IOMMU PC resource is available */
3274 if (!amd_iommu_pc_present)
3277 /* Check for valid iommu and pc register indexing */
3278 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3281 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3283 /* Limit the offset to the hw defined mmio region aperture */
3284 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3285 (iommu->max_counters << 8) | 0x28);
3286 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3287 (offset > max_offset_lim))
3291 u64 val = *value & GENMASK_ULL(47, 0);
3293 writel((u32)val, iommu->mmio_base + offset);
3294 writel((val >> 32), iommu->mmio_base + offset + 4);
3296 *value = readl(iommu->mmio_base + offset + 4);
3298 *value |= readl(iommu->mmio_base + offset);
3299 *value &= GENMASK_ULL(47, 0);
3305 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3310 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3312 EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3314 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3319 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3321 EXPORT_SYMBOL(amd_iommu_pc_set_reg);