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/iommu.h>
22 #include <linux/kmemleak.h>
23 #include <linux/mem_encrypt.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>
34 #include <linux/crash_dump.h>
35 #include "amd_iommu.h"
36 #include "amd_iommu_proto.h"
37 #include "amd_iommu_types.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 100000
89 * ACPI table definitions
91 * These data structures are laid over the table to parse the important values
95 extern const struct iommu_ops amd_iommu_ops
;
98 * structure describing one IOMMU in the ACPI table. Typically followed by one
99 * or more ivhd_entrys.
112 /* Following only valid on IVHD type 11h and 40h */
113 u64 efr_reg
; /* Exact copy of MMIO_EXT_FEATURES */
115 } __attribute__((packed
));
118 * A device entry describing which devices a specific IOMMU translates and
119 * which requestor ids they use.
131 } __attribute__((packed
));
134 * An AMD IOMMU memory definition structure. It defines things like exclusion
135 * ranges for devices and regions that should be unity mapped.
146 } __attribute__((packed
));
149 bool amd_iommu_irq_remap __read_mostly
;
151 int amd_iommu_guest_ir
= AMD_IOMMU_GUEST_IR_VAPIC
;
152 static int amd_iommu_xt_mode
= IRQ_REMAP_XAPIC_MODE
;
154 static bool amd_iommu_detected
;
155 static bool __initdata amd_iommu_disabled
;
156 static int amd_iommu_target_ivhd_type
;
158 u16 amd_iommu_last_bdf
; /* largest PCI device id we have
160 LIST_HEAD(amd_iommu_unity_map
); /* a list of required unity mappings
162 bool amd_iommu_unmap_flush
; /* if true, flush on every unmap */
164 LIST_HEAD(amd_iommu_list
); /* list of all AMD IOMMUs in the
167 /* Array to assign indices to IOMMUs*/
168 struct amd_iommu
*amd_iommus
[MAX_IOMMUS
];
170 /* Number of IOMMUs present in the system */
171 static int amd_iommus_present
;
173 /* IOMMUs have a non-present cache? */
174 bool amd_iommu_np_cache __read_mostly
;
175 bool amd_iommu_iotlb_sup __read_mostly
= true;
177 u32 amd_iommu_max_pasid __read_mostly
= ~0;
179 bool amd_iommu_v2_present __read_mostly
;
180 static bool amd_iommu_pc_present __read_mostly
;
182 bool amd_iommu_force_isolation __read_mostly
;
185 * Pointer to the device table which is shared by all AMD IOMMUs
186 * it is indexed by the PCI device id or the HT unit id and contains
187 * information about the domain the device belongs to as well as the
188 * page table root pointer.
190 struct dev_table_entry
*amd_iommu_dev_table
;
192 * Pointer to a device table which the content of old device table
193 * will be copied to. It's only be used in kdump kernel.
195 static struct dev_table_entry
*old_dev_tbl_cpy
;
198 * The alias table is a driver specific data structure which contains the
199 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
200 * More than one device can share the same requestor id.
202 u16
*amd_iommu_alias_table
;
205 * The rlookup table is used to find the IOMMU which is responsible
206 * for a specific device. It is also indexed by the PCI device id.
208 struct amd_iommu
**amd_iommu_rlookup_table
;
209 EXPORT_SYMBOL(amd_iommu_rlookup_table
);
212 * This table is used to find the irq remapping table for a given device id
215 struct irq_remap_table
**irq_lookup_table
;
218 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
219 * to know which ones are already in use.
221 unsigned long *amd_iommu_pd_alloc_bitmap
;
223 static u32 dev_table_size
; /* size of the device table */
224 static u32 alias_table_size
; /* size of the alias table */
225 static u32 rlookup_table_size
; /* size if the rlookup table */
227 enum iommu_init_state
{
238 IOMMU_CMDLINE_DISABLED
,
241 /* Early ioapic and hpet maps from kernel command line */
242 #define EARLY_MAP_SIZE 4
243 static struct devid_map __initdata early_ioapic_map
[EARLY_MAP_SIZE
];
244 static struct devid_map __initdata early_hpet_map
[EARLY_MAP_SIZE
];
245 static struct acpihid_map_entry __initdata early_acpihid_map
[EARLY_MAP_SIZE
];
247 static int __initdata early_ioapic_map_size
;
248 static int __initdata early_hpet_map_size
;
249 static int __initdata early_acpihid_map_size
;
251 static bool __initdata cmdline_maps
;
253 static enum iommu_init_state init_state
= IOMMU_START_STATE
;
255 static int amd_iommu_enable_interrupts(void);
256 static int __init
iommu_go_to_state(enum iommu_init_state state
);
257 static void init_device_table_dma(void);
259 static bool amd_iommu_pre_enabled
= true;
261 bool translation_pre_enabled(struct amd_iommu
*iommu
)
263 return (iommu
->flags
& AMD_IOMMU_FLAG_TRANS_PRE_ENABLED
);
265 EXPORT_SYMBOL(translation_pre_enabled
);
267 static void clear_translation_pre_enabled(struct amd_iommu
*iommu
)
269 iommu
->flags
&= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED
;
272 static void init_translation_status(struct amd_iommu
*iommu
)
276 ctrl
= readq(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
277 if (ctrl
& (1<<CONTROL_IOMMU_EN
))
278 iommu
->flags
|= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED
;
281 static inline void update_last_devid(u16 devid
)
283 if (devid
> amd_iommu_last_bdf
)
284 amd_iommu_last_bdf
= devid
;
287 static inline unsigned long tbl_size(int entry_size
)
289 unsigned shift
= PAGE_SHIFT
+
290 get_order(((int)amd_iommu_last_bdf
+ 1) * entry_size
);
295 int amd_iommu_get_num_iommus(void)
297 return amd_iommus_present
;
300 /* Access to l1 and l2 indexed register spaces */
302 static u32
iommu_read_l1(struct amd_iommu
*iommu
, u16 l1
, u8 address
)
306 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16));
307 pci_read_config_dword(iommu
->dev
, 0xfc, &val
);
311 static void iommu_write_l1(struct amd_iommu
*iommu
, u16 l1
, u8 address
, u32 val
)
313 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16 | 1 << 31));
314 pci_write_config_dword(iommu
->dev
, 0xfc, val
);
315 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16));
318 static u32
iommu_read_l2(struct amd_iommu
*iommu
, u8 address
)
322 pci_write_config_dword(iommu
->dev
, 0xf0, address
);
323 pci_read_config_dword(iommu
->dev
, 0xf4, &val
);
327 static void iommu_write_l2(struct amd_iommu
*iommu
, u8 address
, u32 val
)
329 pci_write_config_dword(iommu
->dev
, 0xf0, (address
| 1 << 8));
330 pci_write_config_dword(iommu
->dev
, 0xf4, val
);
333 /****************************************************************************
335 * AMD IOMMU MMIO register space handling functions
337 * These functions are used to program the IOMMU device registers in
338 * MMIO space required for that driver.
340 ****************************************************************************/
343 * This function set the exclusion range in the IOMMU. DMA accesses to the
344 * exclusion range are passed through untranslated
346 static void iommu_set_exclusion_range(struct amd_iommu
*iommu
)
348 u64 start
= iommu
->exclusion_start
& PAGE_MASK
;
349 u64 limit
= (start
+ iommu
->exclusion_length
- 1) & PAGE_MASK
;
352 if (!iommu
->exclusion_start
)
355 entry
= start
| MMIO_EXCL_ENABLE_MASK
;
356 memcpy_toio(iommu
->mmio_base
+ MMIO_EXCL_BASE_OFFSET
,
357 &entry
, sizeof(entry
));
360 memcpy_toio(iommu
->mmio_base
+ MMIO_EXCL_LIMIT_OFFSET
,
361 &entry
, sizeof(entry
));
364 /* Programs the physical address of the device table into the IOMMU hardware */
365 static void iommu_set_device_table(struct amd_iommu
*iommu
)
369 BUG_ON(iommu
->mmio_base
== NULL
);
371 entry
= iommu_virt_to_phys(amd_iommu_dev_table
);
372 entry
|= (dev_table_size
>> 12) - 1;
373 memcpy_toio(iommu
->mmio_base
+ MMIO_DEV_TABLE_OFFSET
,
374 &entry
, sizeof(entry
));
377 /* Generic functions to enable/disable certain features of the IOMMU. */
378 static void iommu_feature_enable(struct amd_iommu
*iommu
, u8 bit
)
382 ctrl
= readq(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
383 ctrl
|= (1ULL << bit
);
384 writeq(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
387 static void iommu_feature_disable(struct amd_iommu
*iommu
, u8 bit
)
391 ctrl
= readq(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
392 ctrl
&= ~(1ULL << bit
);
393 writeq(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
396 static void iommu_set_inv_tlb_timeout(struct amd_iommu
*iommu
, int timeout
)
400 ctrl
= readq(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
401 ctrl
&= ~CTRL_INV_TO_MASK
;
402 ctrl
|= (timeout
<< CONTROL_INV_TIMEOUT
) & CTRL_INV_TO_MASK
;
403 writeq(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
406 /* Function to enable the hardware */
407 static void iommu_enable(struct amd_iommu
*iommu
)
409 iommu_feature_enable(iommu
, CONTROL_IOMMU_EN
);
412 static void iommu_disable(struct amd_iommu
*iommu
)
414 if (!iommu
->mmio_base
)
417 /* Disable command buffer */
418 iommu_feature_disable(iommu
, CONTROL_CMDBUF_EN
);
420 /* Disable event logging and event interrupts */
421 iommu_feature_disable(iommu
, CONTROL_EVT_INT_EN
);
422 iommu_feature_disable(iommu
, CONTROL_EVT_LOG_EN
);
424 /* Disable IOMMU GA_LOG */
425 iommu_feature_disable(iommu
, CONTROL_GALOG_EN
);
426 iommu_feature_disable(iommu
, CONTROL_GAINT_EN
);
428 /* Disable IOMMU hardware itself */
429 iommu_feature_disable(iommu
, CONTROL_IOMMU_EN
);
433 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
434 * the system has one.
436 static u8 __iomem
* __init
iommu_map_mmio_space(u64 address
, u64 end
)
438 if (!request_mem_region(address
, end
, "amd_iommu")) {
439 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
441 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
445 return (u8 __iomem
*)ioremap(address
, end
);
448 static void __init
iommu_unmap_mmio_space(struct amd_iommu
*iommu
)
450 if (iommu
->mmio_base
)
451 iounmap(iommu
->mmio_base
);
452 release_mem_region(iommu
->mmio_phys
, iommu
->mmio_phys_end
);
455 static inline u32
get_ivhd_header_size(struct ivhd_header
*h
)
471 /****************************************************************************
473 * The functions below belong to the first pass of AMD IOMMU ACPI table
474 * parsing. In this pass we try to find out the highest device id this
475 * code has to handle. Upon this information the size of the shared data
476 * structures is determined later.
478 ****************************************************************************/
481 * This function calculates the length of a given IVHD entry
483 static inline int ivhd_entry_length(u8
*ivhd
)
485 u32 type
= ((struct ivhd_entry
*)ivhd
)->type
;
488 return 0x04 << (*ivhd
>> 6);
489 } else if (type
== IVHD_DEV_ACPI_HID
) {
490 /* For ACPI_HID, offset 21 is uid len */
491 return *((u8
*)ivhd
+ 21) + 22;
497 * After reading the highest device id from the IOMMU PCI capability header
498 * this function looks if there is a higher device id defined in the ACPI table
500 static int __init
find_last_devid_from_ivhd(struct ivhd_header
*h
)
502 u8
*p
= (void *)h
, *end
= (void *)h
;
503 struct ivhd_entry
*dev
;
505 u32 ivhd_size
= get_ivhd_header_size(h
);
508 pr_err("Unsupported IVHD type %#x\n", h
->type
);
516 dev
= (struct ivhd_entry
*)p
;
519 /* Use maximum BDF value for DEV_ALL */
520 update_last_devid(0xffff);
522 case IVHD_DEV_SELECT
:
523 case IVHD_DEV_RANGE_END
:
525 case IVHD_DEV_EXT_SELECT
:
526 /* all the above subfield types refer to device ids */
527 update_last_devid(dev
->devid
);
532 p
+= ivhd_entry_length(p
);
540 static int __init
check_ivrs_checksum(struct acpi_table_header
*table
)
543 u8 checksum
= 0, *p
= (u8
*)table
;
545 for (i
= 0; i
< table
->length
; ++i
)
548 /* ACPI table corrupt */
549 pr_err(FW_BUG
"IVRS invalid checksum\n");
557 * Iterate over all IVHD entries in the ACPI table and find the highest device
558 * id which we need to handle. This is the first of three functions which parse
559 * the ACPI table. So we check the checksum here.
561 static int __init
find_last_devid_acpi(struct acpi_table_header
*table
)
563 u8
*p
= (u8
*)table
, *end
= (u8
*)table
;
564 struct ivhd_header
*h
;
566 p
+= IVRS_HEADER_LENGTH
;
568 end
+= table
->length
;
570 h
= (struct ivhd_header
*)p
;
571 if (h
->type
== amd_iommu_target_ivhd_type
) {
572 int ret
= find_last_devid_from_ivhd(h
);
584 /****************************************************************************
586 * The following functions belong to the code path which parses the ACPI table
587 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
588 * data structures, initialize the device/alias/rlookup table and also
589 * basically initialize the hardware.
591 ****************************************************************************/
594 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
595 * write commands to that buffer later and the IOMMU will execute them
598 static int __init
alloc_command_buffer(struct amd_iommu
*iommu
)
600 iommu
->cmd_buf
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
601 get_order(CMD_BUFFER_SIZE
));
603 return iommu
->cmd_buf
? 0 : -ENOMEM
;
607 * This function resets the command buffer if the IOMMU stopped fetching
610 void amd_iommu_reset_cmd_buffer(struct amd_iommu
*iommu
)
612 iommu_feature_disable(iommu
, CONTROL_CMDBUF_EN
);
614 writel(0x00, iommu
->mmio_base
+ MMIO_CMD_HEAD_OFFSET
);
615 writel(0x00, iommu
->mmio_base
+ MMIO_CMD_TAIL_OFFSET
);
616 iommu
->cmd_buf_head
= 0;
617 iommu
->cmd_buf_tail
= 0;
619 iommu_feature_enable(iommu
, CONTROL_CMDBUF_EN
);
623 * This function writes the command buffer address to the hardware and
626 static void iommu_enable_command_buffer(struct amd_iommu
*iommu
)
630 BUG_ON(iommu
->cmd_buf
== NULL
);
632 entry
= iommu_virt_to_phys(iommu
->cmd_buf
);
633 entry
|= MMIO_CMD_SIZE_512
;
635 memcpy_toio(iommu
->mmio_base
+ MMIO_CMD_BUF_OFFSET
,
636 &entry
, sizeof(entry
));
638 amd_iommu_reset_cmd_buffer(iommu
);
642 * This function disables the command buffer
644 static void iommu_disable_command_buffer(struct amd_iommu
*iommu
)
646 iommu_feature_disable(iommu
, CONTROL_CMDBUF_EN
);
649 static void __init
free_command_buffer(struct amd_iommu
*iommu
)
651 free_pages((unsigned long)iommu
->cmd_buf
, get_order(CMD_BUFFER_SIZE
));
654 /* allocates the memory where the IOMMU will log its events to */
655 static int __init
alloc_event_buffer(struct amd_iommu
*iommu
)
657 iommu
->evt_buf
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
658 get_order(EVT_BUFFER_SIZE
));
660 return iommu
->evt_buf
? 0 : -ENOMEM
;
663 static void iommu_enable_event_buffer(struct amd_iommu
*iommu
)
667 BUG_ON(iommu
->evt_buf
== NULL
);
669 entry
= iommu_virt_to_phys(iommu
->evt_buf
) | EVT_LEN_MASK
;
671 memcpy_toio(iommu
->mmio_base
+ MMIO_EVT_BUF_OFFSET
,
672 &entry
, sizeof(entry
));
674 /* set head and tail to zero manually */
675 writel(0x00, iommu
->mmio_base
+ MMIO_EVT_HEAD_OFFSET
);
676 writel(0x00, iommu
->mmio_base
+ MMIO_EVT_TAIL_OFFSET
);
678 iommu_feature_enable(iommu
, CONTROL_EVT_LOG_EN
);
682 * This function disables the event log buffer
684 static void iommu_disable_event_buffer(struct amd_iommu
*iommu
)
686 iommu_feature_disable(iommu
, CONTROL_EVT_LOG_EN
);
689 static void __init
free_event_buffer(struct amd_iommu
*iommu
)
691 free_pages((unsigned long)iommu
->evt_buf
, get_order(EVT_BUFFER_SIZE
));
694 /* allocates the memory where the IOMMU will log its events to */
695 static int __init
alloc_ppr_log(struct amd_iommu
*iommu
)
697 iommu
->ppr_log
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
698 get_order(PPR_LOG_SIZE
));
700 return iommu
->ppr_log
? 0 : -ENOMEM
;
703 static void iommu_enable_ppr_log(struct amd_iommu
*iommu
)
707 if (iommu
->ppr_log
== NULL
)
710 entry
= iommu_virt_to_phys(iommu
->ppr_log
) | PPR_LOG_SIZE_512
;
712 memcpy_toio(iommu
->mmio_base
+ MMIO_PPR_LOG_OFFSET
,
713 &entry
, sizeof(entry
));
715 /* set head and tail to zero manually */
716 writel(0x00, iommu
->mmio_base
+ MMIO_PPR_HEAD_OFFSET
);
717 writel(0x00, iommu
->mmio_base
+ MMIO_PPR_TAIL_OFFSET
);
719 iommu_feature_enable(iommu
, CONTROL_PPRLOG_EN
);
720 iommu_feature_enable(iommu
, CONTROL_PPR_EN
);
723 static void __init
free_ppr_log(struct amd_iommu
*iommu
)
725 if (iommu
->ppr_log
== NULL
)
728 free_pages((unsigned long)iommu
->ppr_log
, get_order(PPR_LOG_SIZE
));
731 static void free_ga_log(struct amd_iommu
*iommu
)
733 #ifdef CONFIG_IRQ_REMAP
735 free_pages((unsigned long)iommu
->ga_log
,
736 get_order(GA_LOG_SIZE
));
737 if (iommu
->ga_log_tail
)
738 free_pages((unsigned long)iommu
->ga_log_tail
,
743 static int iommu_ga_log_enable(struct amd_iommu
*iommu
)
745 #ifdef CONFIG_IRQ_REMAP
751 status
= readl(iommu
->mmio_base
+ MMIO_STATUS_OFFSET
);
753 /* Check if already running */
754 if (status
& (MMIO_STATUS_GALOG_RUN_MASK
))
757 iommu_feature_enable(iommu
, CONTROL_GAINT_EN
);
758 iommu_feature_enable(iommu
, CONTROL_GALOG_EN
);
760 for (i
= 0; i
< LOOP_TIMEOUT
; ++i
) {
761 status
= readl(iommu
->mmio_base
+ MMIO_STATUS_OFFSET
);
762 if (status
& (MMIO_STATUS_GALOG_RUN_MASK
))
766 if (i
>= LOOP_TIMEOUT
)
768 #endif /* CONFIG_IRQ_REMAP */
772 #ifdef CONFIG_IRQ_REMAP
773 static int iommu_init_ga_log(struct amd_iommu
*iommu
)
777 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
))
780 iommu
->ga_log
= (u8
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
781 get_order(GA_LOG_SIZE
));
785 iommu
->ga_log_tail
= (u8
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
787 if (!iommu
->ga_log_tail
)
790 entry
= iommu_virt_to_phys(iommu
->ga_log
) | GA_LOG_SIZE_512
;
791 memcpy_toio(iommu
->mmio_base
+ MMIO_GA_LOG_BASE_OFFSET
,
792 &entry
, sizeof(entry
));
793 entry
= (iommu_virt_to_phys(iommu
->ga_log_tail
) &
794 (BIT_ULL(52)-1)) & ~7ULL;
795 memcpy_toio(iommu
->mmio_base
+ MMIO_GA_LOG_TAIL_OFFSET
,
796 &entry
, sizeof(entry
));
797 writel(0x00, iommu
->mmio_base
+ MMIO_GA_HEAD_OFFSET
);
798 writel(0x00, iommu
->mmio_base
+ MMIO_GA_TAIL_OFFSET
);
805 #endif /* CONFIG_IRQ_REMAP */
807 static int iommu_init_ga(struct amd_iommu
*iommu
)
811 #ifdef CONFIG_IRQ_REMAP
812 /* Note: We have already checked GASup from IVRS table.
813 * Now, we need to make sure that GAMSup is set.
815 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
) &&
816 !iommu_feature(iommu
, FEATURE_GAM_VAPIC
))
817 amd_iommu_guest_ir
= AMD_IOMMU_GUEST_IR_LEGACY_GA
;
819 ret
= iommu_init_ga_log(iommu
);
820 #endif /* CONFIG_IRQ_REMAP */
825 static void iommu_enable_xt(struct amd_iommu
*iommu
)
827 #ifdef CONFIG_IRQ_REMAP
829 * XT mode (32-bit APIC destination ID) requires
830 * GA mode (128-bit IRTE support) as a prerequisite.
832 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir
) &&
833 amd_iommu_xt_mode
== IRQ_REMAP_X2APIC_MODE
)
834 iommu_feature_enable(iommu
, CONTROL_XT_EN
);
835 #endif /* CONFIG_IRQ_REMAP */
838 static void iommu_enable_gt(struct amd_iommu
*iommu
)
840 if (!iommu_feature(iommu
, FEATURE_GT
))
843 iommu_feature_enable(iommu
, CONTROL_GT_EN
);
846 /* sets a specific bit in the device table entry. */
847 static void set_dev_entry_bit(u16 devid
, u8 bit
)
849 int i
= (bit
>> 6) & 0x03;
850 int _bit
= bit
& 0x3f;
852 amd_iommu_dev_table
[devid
].data
[i
] |= (1UL << _bit
);
855 static int get_dev_entry_bit(u16 devid
, u8 bit
)
857 int i
= (bit
>> 6) & 0x03;
858 int _bit
= bit
& 0x3f;
860 return (amd_iommu_dev_table
[devid
].data
[i
] & (1UL << _bit
)) >> _bit
;
864 static bool copy_device_table(void)
866 u64 int_ctl
, int_tab_len
, entry
= 0, last_entry
= 0;
867 struct dev_table_entry
*old_devtb
= NULL
;
868 u32 lo
, hi
, devid
, old_devtb_size
;
869 phys_addr_t old_devtb_phys
;
870 struct amd_iommu
*iommu
;
871 u16 dom_id
, dte_v
, irq_v
;
875 if (!amd_iommu_pre_enabled
)
878 pr_warn("Translation is already enabled - trying to copy translation structures\n");
879 for_each_iommu(iommu
) {
880 /* All IOMMUs should use the same device table with the same size */
881 lo
= readl(iommu
->mmio_base
+ MMIO_DEV_TABLE_OFFSET
);
882 hi
= readl(iommu
->mmio_base
+ MMIO_DEV_TABLE_OFFSET
+ 4);
883 entry
= (((u64
) hi
) << 32) + lo
;
884 if (last_entry
&& last_entry
!= entry
) {
885 pr_err("IOMMU:%d should use the same dev table as others!\n",
891 old_devtb_size
= ((entry
& ~PAGE_MASK
) + 1) << 12;
892 if (old_devtb_size
!= dev_table_size
) {
893 pr_err("The device table size of IOMMU:%d is not expected!\n",
900 * When SME is enabled in the first kernel, the entry includes the
901 * memory encryption mask(sme_me_mask), we must remove the memory
902 * encryption mask to obtain the true physical address in kdump kernel.
904 old_devtb_phys
= __sme_clr(entry
) & PAGE_MASK
;
906 if (old_devtb_phys
>= 0x100000000ULL
) {
907 pr_err("The address of old device table is above 4G, not trustworthy!\n");
910 old_devtb
= (sme_active() && is_kdump_kernel())
911 ? (__force
void *)ioremap_encrypted(old_devtb_phys
,
913 : memremap(old_devtb_phys
, dev_table_size
, MEMREMAP_WB
);
918 gfp_flag
= GFP_KERNEL
| __GFP_ZERO
| GFP_DMA32
;
919 old_dev_tbl_cpy
= (void *)__get_free_pages(gfp_flag
,
920 get_order(dev_table_size
));
921 if (old_dev_tbl_cpy
== NULL
) {
922 pr_err("Failed to allocate memory for copying old device table!\n");
926 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
) {
927 old_dev_tbl_cpy
[devid
] = old_devtb
[devid
];
928 dom_id
= old_devtb
[devid
].data
[1] & DEV_DOMID_MASK
;
929 dte_v
= old_devtb
[devid
].data
[0] & DTE_FLAG_V
;
931 if (dte_v
&& dom_id
) {
932 old_dev_tbl_cpy
[devid
].data
[0] = old_devtb
[devid
].data
[0];
933 old_dev_tbl_cpy
[devid
].data
[1] = old_devtb
[devid
].data
[1];
934 __set_bit(dom_id
, amd_iommu_pd_alloc_bitmap
);
935 /* If gcr3 table existed, mask it out */
936 if (old_devtb
[devid
].data
[0] & DTE_FLAG_GV
) {
937 tmp
= DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B
;
938 tmp
|= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C
;
939 old_dev_tbl_cpy
[devid
].data
[1] &= ~tmp
;
940 tmp
= DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A
;
942 old_dev_tbl_cpy
[devid
].data
[0] &= ~tmp
;
946 irq_v
= old_devtb
[devid
].data
[2] & DTE_IRQ_REMAP_ENABLE
;
947 int_ctl
= old_devtb
[devid
].data
[2] & DTE_IRQ_REMAP_INTCTL_MASK
;
948 int_tab_len
= old_devtb
[devid
].data
[2] & DTE_IRQ_TABLE_LEN_MASK
;
949 if (irq_v
&& (int_ctl
|| int_tab_len
)) {
950 if ((int_ctl
!= DTE_IRQ_REMAP_INTCTL
) ||
951 (int_tab_len
!= DTE_IRQ_TABLE_LEN
)) {
952 pr_err("Wrong old irq remapping flag: %#x\n", devid
);
956 old_dev_tbl_cpy
[devid
].data
[2] = old_devtb
[devid
].data
[2];
964 void amd_iommu_apply_erratum_63(u16 devid
)
968 sysmgt
= get_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT1
) |
969 (get_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT2
) << 1);
972 set_dev_entry_bit(devid
, DEV_ENTRY_IW
);
975 /* Writes the specific IOMMU for a device into the rlookup table */
976 static void __init
set_iommu_for_device(struct amd_iommu
*iommu
, u16 devid
)
978 amd_iommu_rlookup_table
[devid
] = iommu
;
982 * This function takes the device specific flags read from the ACPI
983 * table and sets up the device table entry with that information
985 static void __init
set_dev_entry_from_acpi(struct amd_iommu
*iommu
,
986 u16 devid
, u32 flags
, u32 ext_flags
)
988 if (flags
& ACPI_DEVFLAG_INITPASS
)
989 set_dev_entry_bit(devid
, DEV_ENTRY_INIT_PASS
);
990 if (flags
& ACPI_DEVFLAG_EXTINT
)
991 set_dev_entry_bit(devid
, DEV_ENTRY_EINT_PASS
);
992 if (flags
& ACPI_DEVFLAG_NMI
)
993 set_dev_entry_bit(devid
, DEV_ENTRY_NMI_PASS
);
994 if (flags
& ACPI_DEVFLAG_SYSMGT1
)
995 set_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT1
);
996 if (flags
& ACPI_DEVFLAG_SYSMGT2
)
997 set_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT2
);
998 if (flags
& ACPI_DEVFLAG_LINT0
)
999 set_dev_entry_bit(devid
, DEV_ENTRY_LINT0_PASS
);
1000 if (flags
& ACPI_DEVFLAG_LINT1
)
1001 set_dev_entry_bit(devid
, DEV_ENTRY_LINT1_PASS
);
1003 amd_iommu_apply_erratum_63(devid
);
1005 set_iommu_for_device(iommu
, devid
);
1008 int __init
add_special_device(u8 type
, u8 id
, u16
*devid
, bool cmd_line
)
1010 struct devid_map
*entry
;
1011 struct list_head
*list
;
1013 if (type
== IVHD_SPECIAL_IOAPIC
)
1015 else if (type
== IVHD_SPECIAL_HPET
)
1020 list_for_each_entry(entry
, list
, list
) {
1021 if (!(entry
->id
== id
&& entry
->cmd_line
))
1024 pr_info("Command-line override present for %s id %d - ignoring\n",
1025 type
== IVHD_SPECIAL_IOAPIC
? "IOAPIC" : "HPET", id
);
1027 *devid
= entry
->devid
;
1032 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
1037 entry
->devid
= *devid
;
1038 entry
->cmd_line
= cmd_line
;
1040 list_add_tail(&entry
->list
, list
);
1045 static int __init
add_acpi_hid_device(u8
*hid
, u8
*uid
, u16
*devid
,
1048 struct acpihid_map_entry
*entry
;
1049 struct list_head
*list
= &acpihid_map
;
1051 list_for_each_entry(entry
, list
, list
) {
1052 if (strcmp(entry
->hid
, hid
) ||
1053 (*uid
&& *entry
->uid
&& strcmp(entry
->uid
, uid
)) ||
1057 pr_info("Command-line override for hid:%s uid:%s\n",
1059 *devid
= entry
->devid
;
1063 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
1067 memcpy(entry
->uid
, uid
, strlen(uid
));
1068 memcpy(entry
->hid
, hid
, strlen(hid
));
1069 entry
->devid
= *devid
;
1070 entry
->cmd_line
= cmd_line
;
1071 entry
->root_devid
= (entry
->devid
& (~0x7));
1073 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1074 entry
->cmd_line
? "cmd" : "ivrs",
1075 entry
->hid
, entry
->uid
, entry
->root_devid
);
1077 list_add_tail(&entry
->list
, list
);
1081 static int __init
add_early_maps(void)
1085 for (i
= 0; i
< early_ioapic_map_size
; ++i
) {
1086 ret
= add_special_device(IVHD_SPECIAL_IOAPIC
,
1087 early_ioapic_map
[i
].id
,
1088 &early_ioapic_map
[i
].devid
,
1089 early_ioapic_map
[i
].cmd_line
);
1094 for (i
= 0; i
< early_hpet_map_size
; ++i
) {
1095 ret
= add_special_device(IVHD_SPECIAL_HPET
,
1096 early_hpet_map
[i
].id
,
1097 &early_hpet_map
[i
].devid
,
1098 early_hpet_map
[i
].cmd_line
);
1103 for (i
= 0; i
< early_acpihid_map_size
; ++i
) {
1104 ret
= add_acpi_hid_device(early_acpihid_map
[i
].hid
,
1105 early_acpihid_map
[i
].uid
,
1106 &early_acpihid_map
[i
].devid
,
1107 early_acpihid_map
[i
].cmd_line
);
1116 * Reads the device exclusion range from ACPI and initializes the IOMMU with
1119 static void __init
set_device_exclusion_range(u16 devid
, struct ivmd_header
*m
)
1121 if (!(m
->flags
& IVMD_FLAG_EXCL_RANGE
))
1125 * Treat per-device exclusion ranges as r/w unity-mapped regions
1126 * since some buggy BIOSes might lead to the overwritten exclusion
1127 * range (exclusion_start and exclusion_length members). This
1128 * happens when there are multiple exclusion ranges (IVMD entries)
1129 * defined in ACPI table.
1131 m
->flags
= (IVMD_FLAG_IW
| IVMD_FLAG_IR
| IVMD_FLAG_UNITY_MAP
);
1135 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1136 * initializes the hardware and our data structures with it.
1138 static int __init
init_iommu_from_acpi(struct amd_iommu
*iommu
,
1139 struct ivhd_header
*h
)
1142 u8
*end
= p
, flags
= 0;
1143 u16 devid
= 0, devid_start
= 0, devid_to
= 0;
1144 u32 dev_i
, ext_flags
= 0;
1146 struct ivhd_entry
*e
;
1151 ret
= add_early_maps();
1155 amd_iommu_apply_ivrs_quirks();
1158 * First save the recommended feature enable bits from ACPI
1160 iommu
->acpi_flags
= h
->flags
;
1163 * Done. Now parse the device entries
1165 ivhd_size
= get_ivhd_header_size(h
);
1167 pr_err("Unsupported IVHD type %#x\n", h
->type
);
1177 e
= (struct ivhd_entry
*)p
;
1181 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e
->flags
);
1183 for (dev_i
= 0; dev_i
<= amd_iommu_last_bdf
; ++dev_i
)
1184 set_dev_entry_from_acpi(iommu
, dev_i
, e
->flags
, 0);
1186 case IVHD_DEV_SELECT
:
1188 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1190 PCI_BUS_NUM(e
->devid
),
1196 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
1198 case IVHD_DEV_SELECT_RANGE_START
:
1200 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1201 "devid: %02x:%02x.%x flags: %02x\n",
1202 PCI_BUS_NUM(e
->devid
),
1207 devid_start
= e
->devid
;
1212 case IVHD_DEV_ALIAS
:
1214 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1215 "flags: %02x devid_to: %02x:%02x.%x\n",
1216 PCI_BUS_NUM(e
->devid
),
1220 PCI_BUS_NUM(e
->ext
>> 8),
1221 PCI_SLOT(e
->ext
>> 8),
1222 PCI_FUNC(e
->ext
>> 8));
1225 devid_to
= e
->ext
>> 8;
1226 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
1227 set_dev_entry_from_acpi(iommu
, devid_to
, e
->flags
, 0);
1228 amd_iommu_alias_table
[devid
] = devid_to
;
1230 case IVHD_DEV_ALIAS_RANGE
:
1232 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1233 "devid: %02x:%02x.%x flags: %02x "
1234 "devid_to: %02x:%02x.%x\n",
1235 PCI_BUS_NUM(e
->devid
),
1239 PCI_BUS_NUM(e
->ext
>> 8),
1240 PCI_SLOT(e
->ext
>> 8),
1241 PCI_FUNC(e
->ext
>> 8));
1243 devid_start
= e
->devid
;
1245 devid_to
= e
->ext
>> 8;
1249 case IVHD_DEV_EXT_SELECT
:
1251 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1252 "flags: %02x ext: %08x\n",
1253 PCI_BUS_NUM(e
->devid
),
1259 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
,
1262 case IVHD_DEV_EXT_SELECT_RANGE
:
1264 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1265 "%02x:%02x.%x flags: %02x ext: %08x\n",
1266 PCI_BUS_NUM(e
->devid
),
1271 devid_start
= e
->devid
;
1276 case IVHD_DEV_RANGE_END
:
1278 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1279 PCI_BUS_NUM(e
->devid
),
1281 PCI_FUNC(e
->devid
));
1284 for (dev_i
= devid_start
; dev_i
<= devid
; ++dev_i
) {
1286 amd_iommu_alias_table
[dev_i
] = devid_to
;
1287 set_dev_entry_from_acpi(iommu
,
1288 devid_to
, flags
, ext_flags
);
1290 set_dev_entry_from_acpi(iommu
, dev_i
,
1294 case IVHD_DEV_SPECIAL
: {
1300 handle
= e
->ext
& 0xff;
1301 devid
= (e
->ext
>> 8) & 0xffff;
1302 type
= (e
->ext
>> 24) & 0xff;
1304 if (type
== IVHD_SPECIAL_IOAPIC
)
1306 else if (type
== IVHD_SPECIAL_HPET
)
1311 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1317 ret
= add_special_device(type
, handle
, &devid
, false);
1322 * add_special_device might update the devid in case a
1323 * command-line override is present. So call
1324 * set_dev_entry_from_acpi after add_special_device.
1326 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
1330 case IVHD_DEV_ACPI_HID
: {
1332 u8 hid
[ACPIHID_HID_LEN
];
1333 u8 uid
[ACPIHID_UID_LEN
];
1336 if (h
->type
!= 0x40) {
1337 pr_err(FW_BUG
"Invalid IVHD device type %#x\n",
1342 memcpy(hid
, (u8
*)(&e
->ext
), ACPIHID_HID_LEN
- 1);
1343 hid
[ACPIHID_HID_LEN
- 1] = '\0';
1346 pr_err(FW_BUG
"Invalid HID.\n");
1352 case UID_NOT_PRESENT
:
1355 pr_warn(FW_BUG
"Invalid UID length.\n");
1358 case UID_IS_INTEGER
:
1360 sprintf(uid
, "%d", e
->uid
);
1363 case UID_IS_CHARACTER
:
1365 memcpy(uid
, &e
->uid
, e
->uidl
);
1366 uid
[e
->uidl
] = '\0';
1374 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1382 ret
= add_acpi_hid_device(hid
, uid
, &devid
, false);
1387 * add_special_device might update the devid in case a
1388 * command-line override is present. So call
1389 * set_dev_entry_from_acpi after add_special_device.
1391 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
1399 p
+= ivhd_entry_length(p
);
1405 static void __init
free_iommu_one(struct amd_iommu
*iommu
)
1407 free_command_buffer(iommu
);
1408 free_event_buffer(iommu
);
1409 free_ppr_log(iommu
);
1411 iommu_unmap_mmio_space(iommu
);
1414 static void __init
free_iommu_all(void)
1416 struct amd_iommu
*iommu
, *next
;
1418 for_each_iommu_safe(iommu
, next
) {
1419 list_del(&iommu
->list
);
1420 free_iommu_one(iommu
);
1426 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1428 * BIOS should disable L2B micellaneous clock gating by setting
1429 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1431 static void amd_iommu_erratum_746_workaround(struct amd_iommu
*iommu
)
1435 if ((boot_cpu_data
.x86
!= 0x15) ||
1436 (boot_cpu_data
.x86_model
< 0x10) ||
1437 (boot_cpu_data
.x86_model
> 0x1f))
1440 pci_write_config_dword(iommu
->dev
, 0xf0, 0x90);
1441 pci_read_config_dword(iommu
->dev
, 0xf4, &value
);
1446 /* Select NB indirect register 0x90 and enable writing */
1447 pci_write_config_dword(iommu
->dev
, 0xf0, 0x90 | (1 << 8));
1449 pci_write_config_dword(iommu
->dev
, 0xf4, value
| 0x4);
1450 pci_info(iommu
->dev
, "Applying erratum 746 workaround\n");
1452 /* Clear the enable writing bit */
1453 pci_write_config_dword(iommu
->dev
, 0xf0, 0x90);
1457 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1459 * BIOS should enable ATS write permission check by setting
1460 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1462 static void amd_iommu_ats_write_check_workaround(struct amd_iommu
*iommu
)
1466 if ((boot_cpu_data
.x86
!= 0x15) ||
1467 (boot_cpu_data
.x86_model
< 0x30) ||
1468 (boot_cpu_data
.x86_model
> 0x3f))
1471 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1472 value
= iommu_read_l2(iommu
, 0x47);
1477 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1478 iommu_write_l2(iommu
, 0x47, value
| BIT(0));
1480 pci_info(iommu
->dev
, "Applying ATS write check workaround\n");
1484 * This function clues the initialization function for one IOMMU
1485 * together and also allocates the command buffer and programs the
1486 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1488 static int __init
init_iommu_one(struct amd_iommu
*iommu
, struct ivhd_header
*h
)
1492 raw_spin_lock_init(&iommu
->lock
);
1494 /* Add IOMMU to internal data structures */
1495 list_add_tail(&iommu
->list
, &amd_iommu_list
);
1496 iommu
->index
= amd_iommus_present
++;
1498 if (unlikely(iommu
->index
>= MAX_IOMMUS
)) {
1499 WARN(1, "System has more IOMMUs than supported by this driver\n");
1503 /* Index is fine - add IOMMU to the array */
1504 amd_iommus
[iommu
->index
] = iommu
;
1507 * Copy data from ACPI table entry to the iommu struct
1509 iommu
->devid
= h
->devid
;
1510 iommu
->cap_ptr
= h
->cap_ptr
;
1511 iommu
->pci_seg
= h
->pci_seg
;
1512 iommu
->mmio_phys
= h
->mmio_phys
;
1516 /* Check if IVHD EFR contains proper max banks/counters */
1517 if ((h
->efr_attr
!= 0) &&
1518 ((h
->efr_attr
& (0xF << 13)) != 0) &&
1519 ((h
->efr_attr
& (0x3F << 17)) != 0))
1520 iommu
->mmio_phys_end
= MMIO_REG_END_OFFSET
;
1522 iommu
->mmio_phys_end
= MMIO_CNTR_CONF_OFFSET
;
1523 if (((h
->efr_attr
& (0x1 << IOMMU_FEAT_GASUP_SHIFT
)) == 0))
1524 amd_iommu_guest_ir
= AMD_IOMMU_GUEST_IR_LEGACY
;
1528 if (h
->efr_reg
& (1 << 9))
1529 iommu
->mmio_phys_end
= MMIO_REG_END_OFFSET
;
1531 iommu
->mmio_phys_end
= MMIO_CNTR_CONF_OFFSET
;
1532 if (((h
->efr_reg
& (0x1 << IOMMU_EFR_GASUP_SHIFT
)) == 0))
1533 amd_iommu_guest_ir
= AMD_IOMMU_GUEST_IR_LEGACY
;
1535 * Note: Since iommu_update_intcapxt() leverages
1536 * the IOMMU MMIO access to MSI capability block registers
1537 * for MSI address lo/hi/data, we need to check both
1538 * EFR[XtSup] and EFR[MsiCapMmioSup] for x2APIC support.
1540 if ((h
->efr_reg
& BIT(IOMMU_EFR_XTSUP_SHIFT
)) &&
1541 (h
->efr_reg
& BIT(IOMMU_EFR_MSICAPMMIOSUP_SHIFT
)))
1542 amd_iommu_xt_mode
= IRQ_REMAP_X2APIC_MODE
;
1548 iommu
->mmio_base
= iommu_map_mmio_space(iommu
->mmio_phys
,
1549 iommu
->mmio_phys_end
);
1550 if (!iommu
->mmio_base
)
1553 if (alloc_command_buffer(iommu
))
1556 if (alloc_event_buffer(iommu
))
1559 iommu
->int_enabled
= false;
1561 init_translation_status(iommu
);
1562 if (translation_pre_enabled(iommu
) && !is_kdump_kernel()) {
1563 iommu_disable(iommu
);
1564 clear_translation_pre_enabled(iommu
);
1565 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1568 if (amd_iommu_pre_enabled
)
1569 amd_iommu_pre_enabled
= translation_pre_enabled(iommu
);
1571 ret
= init_iommu_from_acpi(iommu
, h
);
1575 ret
= amd_iommu_create_irq_domain(iommu
);
1580 * Make sure IOMMU is not considered to translate itself. The IVRS
1581 * table tells us so, but this is a lie!
1583 amd_iommu_rlookup_table
[iommu
->devid
] = NULL
;
1589 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1590 * @ivrs Pointer to the IVRS header
1592 * This function search through all IVDB of the maximum supported IVHD
1594 static u8
get_highest_supported_ivhd_type(struct acpi_table_header
*ivrs
)
1596 u8
*base
= (u8
*)ivrs
;
1597 struct ivhd_header
*ivhd
= (struct ivhd_header
*)
1598 (base
+ IVRS_HEADER_LENGTH
);
1599 u8 last_type
= ivhd
->type
;
1600 u16 devid
= ivhd
->devid
;
1602 while (((u8
*)ivhd
- base
< ivrs
->length
) &&
1603 (ivhd
->type
<= ACPI_IVHD_TYPE_MAX_SUPPORTED
)) {
1604 u8
*p
= (u8
*) ivhd
;
1606 if (ivhd
->devid
== devid
)
1607 last_type
= ivhd
->type
;
1608 ivhd
= (struct ivhd_header
*)(p
+ ivhd
->length
);
1615 * Iterates over all IOMMU entries in the ACPI table, allocates the
1616 * IOMMU structure and initializes it with init_iommu_one()
1618 static int __init
init_iommu_all(struct acpi_table_header
*table
)
1620 u8
*p
= (u8
*)table
, *end
= (u8
*)table
;
1621 struct ivhd_header
*h
;
1622 struct amd_iommu
*iommu
;
1625 end
+= table
->length
;
1626 p
+= IVRS_HEADER_LENGTH
;
1629 h
= (struct ivhd_header
*)p
;
1630 if (*p
== amd_iommu_target_ivhd_type
) {
1632 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1633 "seg: %d flags: %01x info %04x\n",
1634 PCI_BUS_NUM(h
->devid
), PCI_SLOT(h
->devid
),
1635 PCI_FUNC(h
->devid
), h
->cap_ptr
,
1636 h
->pci_seg
, h
->flags
, h
->info
);
1637 DUMP_printk(" mmio-addr: %016llx\n",
1640 iommu
= kzalloc(sizeof(struct amd_iommu
), GFP_KERNEL
);
1644 ret
= init_iommu_one(iommu
, h
);
1656 static int iommu_pc_get_set_reg(struct amd_iommu
*iommu
, u8 bank
, u8 cntr
,
1657 u8 fxn
, u64
*value
, bool is_write
);
1659 static void init_iommu_perf_ctr(struct amd_iommu
*iommu
)
1661 struct pci_dev
*pdev
= iommu
->dev
;
1662 u64 val
= 0xabcd, val2
= 0, save_reg
= 0;
1664 if (!iommu_feature(iommu
, FEATURE_PC
))
1667 amd_iommu_pc_present
= true;
1669 /* save the value to restore, if writable */
1670 if (iommu_pc_get_set_reg(iommu
, 0, 0, 0, &save_reg
, false))
1673 /* Check if the performance counters can be written to */
1674 if ((iommu_pc_get_set_reg(iommu
, 0, 0, 0, &val
, true)) ||
1675 (iommu_pc_get_set_reg(iommu
, 0, 0, 0, &val2
, false)) ||
1680 if (iommu_pc_get_set_reg(iommu
, 0, 0, 0, &save_reg
, true))
1683 pci_info(pdev
, "IOMMU performance counters supported\n");
1685 val
= readl(iommu
->mmio_base
+ MMIO_CNTR_CONF_OFFSET
);
1686 iommu
->max_banks
= (u8
) ((val
>> 12) & 0x3f);
1687 iommu
->max_counters
= (u8
) ((val
>> 7) & 0xf);
1692 pci_err(pdev
, "Unable to read/write to IOMMU perf counter.\n");
1693 amd_iommu_pc_present
= false;
1697 static ssize_t
amd_iommu_show_cap(struct device
*dev
,
1698 struct device_attribute
*attr
,
1701 struct amd_iommu
*iommu
= dev_to_amd_iommu(dev
);
1702 return sprintf(buf
, "%x\n", iommu
->cap
);
1704 static DEVICE_ATTR(cap
, S_IRUGO
, amd_iommu_show_cap
, NULL
);
1706 static ssize_t
amd_iommu_show_features(struct device
*dev
,
1707 struct device_attribute
*attr
,
1710 struct amd_iommu
*iommu
= dev_to_amd_iommu(dev
);
1711 return sprintf(buf
, "%llx\n", iommu
->features
);
1713 static DEVICE_ATTR(features
, S_IRUGO
, amd_iommu_show_features
, NULL
);
1715 static struct attribute
*amd_iommu_attrs
[] = {
1717 &dev_attr_features
.attr
,
1721 static struct attribute_group amd_iommu_group
= {
1722 .name
= "amd-iommu",
1723 .attrs
= amd_iommu_attrs
,
1726 static const struct attribute_group
*amd_iommu_groups
[] = {
1731 static int __init
iommu_init_pci(struct amd_iommu
*iommu
)
1733 int cap_ptr
= iommu
->cap_ptr
;
1736 iommu
->dev
= pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu
->devid
),
1737 iommu
->devid
& 0xff);
1741 /* Prevent binding other PCI device drivers to IOMMU devices */
1742 iommu
->dev
->match_driver
= false;
1744 pci_read_config_dword(iommu
->dev
, cap_ptr
+ MMIO_CAP_HDR_OFFSET
,
1747 if (!(iommu
->cap
& (1 << IOMMU_CAP_IOTLB
)))
1748 amd_iommu_iotlb_sup
= false;
1750 /* read extended feature bits */
1751 iommu
->features
= readq(iommu
->mmio_base
+ MMIO_EXT_FEATURES
);
1753 if (iommu_feature(iommu
, FEATURE_GT
)) {
1758 pasmax
= iommu
->features
& FEATURE_PASID_MASK
;
1759 pasmax
>>= FEATURE_PASID_SHIFT
;
1760 max_pasid
= (1 << (pasmax
+ 1)) - 1;
1762 amd_iommu_max_pasid
= min(amd_iommu_max_pasid
, max_pasid
);
1764 BUG_ON(amd_iommu_max_pasid
& ~PASID_MASK
);
1766 glxval
= iommu
->features
& FEATURE_GLXVAL_MASK
;
1767 glxval
>>= FEATURE_GLXVAL_SHIFT
;
1769 if (amd_iommu_max_glx_val
== -1)
1770 amd_iommu_max_glx_val
= glxval
;
1772 amd_iommu_max_glx_val
= min(amd_iommu_max_glx_val
, glxval
);
1775 if (iommu_feature(iommu
, FEATURE_GT
) &&
1776 iommu_feature(iommu
, FEATURE_PPR
)) {
1777 iommu
->is_iommu_v2
= true;
1778 amd_iommu_v2_present
= true;
1781 if (iommu_feature(iommu
, FEATURE_PPR
) && alloc_ppr_log(iommu
))
1784 ret
= iommu_init_ga(iommu
);
1788 if (iommu
->cap
& (1UL << IOMMU_CAP_NPCACHE
))
1789 amd_iommu_np_cache
= true;
1791 init_iommu_perf_ctr(iommu
);
1793 if (is_rd890_iommu(iommu
->dev
)) {
1797 pci_get_domain_bus_and_slot(0, iommu
->dev
->bus
->number
,
1801 * Some rd890 systems may not be fully reconfigured by the
1802 * BIOS, so it's necessary for us to store this information so
1803 * it can be reprogrammed on resume
1805 pci_read_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
1806 &iommu
->stored_addr_lo
);
1807 pci_read_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 8,
1808 &iommu
->stored_addr_hi
);
1810 /* Low bit locks writes to configuration space */
1811 iommu
->stored_addr_lo
&= ~1;
1813 for (i
= 0; i
< 6; i
++)
1814 for (j
= 0; j
< 0x12; j
++)
1815 iommu
->stored_l1
[i
][j
] = iommu_read_l1(iommu
, i
, j
);
1817 for (i
= 0; i
< 0x83; i
++)
1818 iommu
->stored_l2
[i
] = iommu_read_l2(iommu
, i
);
1821 amd_iommu_erratum_746_workaround(iommu
);
1822 amd_iommu_ats_write_check_workaround(iommu
);
1824 iommu_device_sysfs_add(&iommu
->iommu
, &iommu
->dev
->dev
,
1825 amd_iommu_groups
, "ivhd%d", iommu
->index
);
1826 iommu_device_set_ops(&iommu
->iommu
, &amd_iommu_ops
);
1827 iommu_device_register(&iommu
->iommu
);
1829 return pci_enable_device(iommu
->dev
);
1832 static void print_iommu_info(void)
1834 static const char * const feat_str
[] = {
1835 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1836 "IA", "GA", "HE", "PC"
1838 struct amd_iommu
*iommu
;
1840 for_each_iommu(iommu
) {
1841 struct pci_dev
*pdev
= iommu
->dev
;
1844 pci_info(pdev
, "Found IOMMU cap 0x%hx\n", iommu
->cap_ptr
);
1846 if (iommu
->cap
& (1 << IOMMU_CAP_EFR
)) {
1847 pci_info(pdev
, "Extended features (%#llx):\n",
1849 for (i
= 0; i
< ARRAY_SIZE(feat_str
); ++i
) {
1850 if (iommu_feature(iommu
, (1ULL << i
)))
1851 pr_cont(" %s", feat_str
[i
]);
1854 if (iommu
->features
& FEATURE_GAM_VAPIC
)
1855 pr_cont(" GA_vAPIC");
1860 if (irq_remapping_enabled
) {
1861 pr_info("Interrupt remapping enabled\n");
1862 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
))
1863 pr_info("Virtual APIC enabled\n");
1864 if (amd_iommu_xt_mode
== IRQ_REMAP_X2APIC_MODE
)
1865 pr_info("X2APIC enabled\n");
1869 static int __init
amd_iommu_init_pci(void)
1871 struct amd_iommu
*iommu
;
1874 for_each_iommu(iommu
) {
1875 ret
= iommu_init_pci(iommu
);
1881 * Order is important here to make sure any unity map requirements are
1882 * fulfilled. The unity mappings are created and written to the device
1883 * table during the amd_iommu_init_api() call.
1885 * After that we call init_device_table_dma() to make sure any
1886 * uninitialized DTE will block DMA, and in the end we flush the caches
1887 * of all IOMMUs to make sure the changes to the device table are
1890 ret
= amd_iommu_init_api();
1892 init_device_table_dma();
1894 for_each_iommu(iommu
)
1895 iommu_flush_all_caches(iommu
);
1903 /****************************************************************************
1905 * The following functions initialize the MSI interrupts for all IOMMUs
1906 * in the system. It's a bit challenging because there could be multiple
1907 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1910 ****************************************************************************/
1912 static int iommu_setup_msi(struct amd_iommu
*iommu
)
1916 r
= pci_enable_msi(iommu
->dev
);
1920 r
= request_threaded_irq(iommu
->dev
->irq
,
1921 amd_iommu_int_handler
,
1922 amd_iommu_int_thread
,
1927 pci_disable_msi(iommu
->dev
);
1931 iommu
->int_enabled
= true;
1936 #define XT_INT_DEST_MODE(x) (((x) & 0x1ULL) << 2)
1937 #define XT_INT_DEST_LO(x) (((x) & 0xFFFFFFULL) << 8)
1938 #define XT_INT_VEC(x) (((x) & 0xFFULL) << 32)
1939 #define XT_INT_DEST_HI(x) ((((x) >> 24) & 0xFFULL) << 56)
1942 * Setup the IntCapXT registers with interrupt routing information
1943 * based on the PCI MSI capability block registers, accessed via
1944 * MMIO MSI address low/hi and MSI data registers.
1946 static void iommu_update_intcapxt(struct amd_iommu
*iommu
)
1949 u32 addr_lo
= readl(iommu
->mmio_base
+ MMIO_MSI_ADDR_LO_OFFSET
);
1950 u32 addr_hi
= readl(iommu
->mmio_base
+ MMIO_MSI_ADDR_HI_OFFSET
);
1951 u32 data
= readl(iommu
->mmio_base
+ MMIO_MSI_DATA_OFFSET
);
1952 bool dm
= (addr_lo
>> MSI_ADDR_DEST_MODE_SHIFT
) & 0x1;
1953 u32 dest
= ((addr_lo
>> MSI_ADDR_DEST_ID_SHIFT
) & 0xFF);
1955 if (x2apic_enabled())
1956 dest
|= MSI_ADDR_EXT_DEST_ID(addr_hi
);
1958 val
= XT_INT_VEC(data
& 0xFF) |
1959 XT_INT_DEST_MODE(dm
) |
1960 XT_INT_DEST_LO(dest
) |
1961 XT_INT_DEST_HI(dest
);
1964 * Current IOMMU implemtation uses the same IRQ for all
1965 * 3 IOMMU interrupts.
1967 writeq(val
, iommu
->mmio_base
+ MMIO_INTCAPXT_EVT_OFFSET
);
1968 writeq(val
, iommu
->mmio_base
+ MMIO_INTCAPXT_PPR_OFFSET
);
1969 writeq(val
, iommu
->mmio_base
+ MMIO_INTCAPXT_GALOG_OFFSET
);
1972 static void _irq_notifier_notify(struct irq_affinity_notify
*notify
,
1973 const cpumask_t
*mask
)
1975 struct amd_iommu
*iommu
;
1977 for_each_iommu(iommu
) {
1978 if (iommu
->dev
->irq
== notify
->irq
) {
1979 iommu_update_intcapxt(iommu
);
1985 static void _irq_notifier_release(struct kref
*ref
)
1989 static int iommu_init_intcapxt(struct amd_iommu
*iommu
)
1992 struct irq_affinity_notify
*notify
= &iommu
->intcapxt_notify
;
1995 * IntCapXT requires XTSup=1 and MsiCapMmioSup=1,
1996 * which can be inferred from amd_iommu_xt_mode.
1998 if (amd_iommu_xt_mode
!= IRQ_REMAP_X2APIC_MODE
)
2002 * Also, we need to setup notifier to update the IntCapXT registers
2003 * whenever the irq affinity is changed from user-space.
2005 notify
->irq
= iommu
->dev
->irq
;
2006 notify
->notify
= _irq_notifier_notify
,
2007 notify
->release
= _irq_notifier_release
,
2008 ret
= irq_set_affinity_notifier(iommu
->dev
->irq
, notify
);
2010 pr_err("Failed to register irq affinity notifier (devid=%#x, irq %d)\n",
2011 iommu
->devid
, iommu
->dev
->irq
);
2015 iommu_update_intcapxt(iommu
);
2016 iommu_feature_enable(iommu
, CONTROL_INTCAPXT_EN
);
2020 static int iommu_init_msi(struct amd_iommu
*iommu
)
2024 if (iommu
->int_enabled
)
2027 if (iommu
->dev
->msi_cap
)
2028 ret
= iommu_setup_msi(iommu
);
2036 ret
= iommu_init_intcapxt(iommu
);
2040 iommu_feature_enable(iommu
, CONTROL_EVT_INT_EN
);
2042 if (iommu
->ppr_log
!= NULL
)
2043 iommu_feature_enable(iommu
, CONTROL_PPRINT_EN
);
2045 iommu_ga_log_enable(iommu
);
2050 /****************************************************************************
2052 * The next functions belong to the third pass of parsing the ACPI
2053 * table. In this last pass the memory mapping requirements are
2054 * gathered (like exclusion and unity mapping ranges).
2056 ****************************************************************************/
2058 static void __init
free_unity_maps(void)
2060 struct unity_map_entry
*entry
, *next
;
2062 list_for_each_entry_safe(entry
, next
, &amd_iommu_unity_map
, list
) {
2063 list_del(&entry
->list
);
2068 /* called when we find an exclusion range definition in ACPI */
2069 static int __init
init_exclusion_range(struct ivmd_header
*m
)
2074 case ACPI_IVMD_TYPE
:
2075 set_device_exclusion_range(m
->devid
, m
);
2077 case ACPI_IVMD_TYPE_ALL
:
2078 for (i
= 0; i
<= amd_iommu_last_bdf
; ++i
)
2079 set_device_exclusion_range(i
, m
);
2081 case ACPI_IVMD_TYPE_RANGE
:
2082 for (i
= m
->devid
; i
<= m
->aux
; ++i
)
2083 set_device_exclusion_range(i
, m
);
2092 /* called for unity map ACPI definition */
2093 static int __init
init_unity_map_range(struct ivmd_header
*m
)
2095 struct unity_map_entry
*e
= NULL
;
2098 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
2102 if (m
->flags
& IVMD_FLAG_EXCL_RANGE
)
2103 init_exclusion_range(m
);
2109 case ACPI_IVMD_TYPE
:
2110 s
= "IVMD_TYPEi\t\t\t";
2111 e
->devid_start
= e
->devid_end
= m
->devid
;
2113 case ACPI_IVMD_TYPE_ALL
:
2114 s
= "IVMD_TYPE_ALL\t\t";
2116 e
->devid_end
= amd_iommu_last_bdf
;
2118 case ACPI_IVMD_TYPE_RANGE
:
2119 s
= "IVMD_TYPE_RANGE\t\t";
2120 e
->devid_start
= m
->devid
;
2121 e
->devid_end
= m
->aux
;
2124 e
->address_start
= PAGE_ALIGN(m
->range_start
);
2125 e
->address_end
= e
->address_start
+ PAGE_ALIGN(m
->range_length
);
2126 e
->prot
= m
->flags
>> 1;
2128 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2129 " range_start: %016llx range_end: %016llx flags: %x\n", s
,
2130 PCI_BUS_NUM(e
->devid_start
), PCI_SLOT(e
->devid_start
),
2131 PCI_FUNC(e
->devid_start
), PCI_BUS_NUM(e
->devid_end
),
2132 PCI_SLOT(e
->devid_end
), PCI_FUNC(e
->devid_end
),
2133 e
->address_start
, e
->address_end
, m
->flags
);
2135 list_add_tail(&e
->list
, &amd_iommu_unity_map
);
2140 /* iterates over all memory definitions we find in the ACPI table */
2141 static int __init
init_memory_definitions(struct acpi_table_header
*table
)
2143 u8
*p
= (u8
*)table
, *end
= (u8
*)table
;
2144 struct ivmd_header
*m
;
2146 end
+= table
->length
;
2147 p
+= IVRS_HEADER_LENGTH
;
2150 m
= (struct ivmd_header
*)p
;
2151 if (m
->flags
& (IVMD_FLAG_UNITY_MAP
| IVMD_FLAG_EXCL_RANGE
))
2152 init_unity_map_range(m
);
2161 * Init the device table to not allow DMA access for devices
2163 static void init_device_table_dma(void)
2167 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
) {
2168 set_dev_entry_bit(devid
, DEV_ENTRY_VALID
);
2169 set_dev_entry_bit(devid
, DEV_ENTRY_TRANSLATION
);
2173 static void __init
uninit_device_table_dma(void)
2177 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
) {
2178 amd_iommu_dev_table
[devid
].data
[0] = 0ULL;
2179 amd_iommu_dev_table
[devid
].data
[1] = 0ULL;
2183 static void init_device_table(void)
2187 if (!amd_iommu_irq_remap
)
2190 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
)
2191 set_dev_entry_bit(devid
, DEV_ENTRY_IRQ_TBL_EN
);
2194 static void iommu_init_flags(struct amd_iommu
*iommu
)
2196 iommu
->acpi_flags
& IVHD_FLAG_HT_TUN_EN_MASK
?
2197 iommu_feature_enable(iommu
, CONTROL_HT_TUN_EN
) :
2198 iommu_feature_disable(iommu
, CONTROL_HT_TUN_EN
);
2200 iommu
->acpi_flags
& IVHD_FLAG_PASSPW_EN_MASK
?
2201 iommu_feature_enable(iommu
, CONTROL_PASSPW_EN
) :
2202 iommu_feature_disable(iommu
, CONTROL_PASSPW_EN
);
2204 iommu
->acpi_flags
& IVHD_FLAG_RESPASSPW_EN_MASK
?
2205 iommu_feature_enable(iommu
, CONTROL_RESPASSPW_EN
) :
2206 iommu_feature_disable(iommu
, CONTROL_RESPASSPW_EN
);
2208 iommu
->acpi_flags
& IVHD_FLAG_ISOC_EN_MASK
?
2209 iommu_feature_enable(iommu
, CONTROL_ISOC_EN
) :
2210 iommu_feature_disable(iommu
, CONTROL_ISOC_EN
);
2213 * make IOMMU memory accesses cache coherent
2215 iommu_feature_enable(iommu
, CONTROL_COHERENT_EN
);
2217 /* Set IOTLB invalidation timeout to 1s */
2218 iommu_set_inv_tlb_timeout(iommu
, CTRL_INV_TO_1S
);
2221 static void iommu_apply_resume_quirks(struct amd_iommu
*iommu
)
2224 u32 ioc_feature_control
;
2225 struct pci_dev
*pdev
= iommu
->root_pdev
;
2227 /* RD890 BIOSes may not have completely reconfigured the iommu */
2228 if (!is_rd890_iommu(iommu
->dev
) || !pdev
)
2232 * First, we need to ensure that the iommu is enabled. This is
2233 * controlled by a register in the northbridge
2236 /* Select Northbridge indirect register 0x75 and enable writing */
2237 pci_write_config_dword(pdev
, 0x60, 0x75 | (1 << 7));
2238 pci_read_config_dword(pdev
, 0x64, &ioc_feature_control
);
2240 /* Enable the iommu */
2241 if (!(ioc_feature_control
& 0x1))
2242 pci_write_config_dword(pdev
, 0x64, ioc_feature_control
| 1);
2244 /* Restore the iommu BAR */
2245 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
2246 iommu
->stored_addr_lo
);
2247 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 8,
2248 iommu
->stored_addr_hi
);
2250 /* Restore the l1 indirect regs for each of the 6 l1s */
2251 for (i
= 0; i
< 6; i
++)
2252 for (j
= 0; j
< 0x12; j
++)
2253 iommu_write_l1(iommu
, i
, j
, iommu
->stored_l1
[i
][j
]);
2255 /* Restore the l2 indirect regs */
2256 for (i
= 0; i
< 0x83; i
++)
2257 iommu_write_l2(iommu
, i
, iommu
->stored_l2
[i
]);
2259 /* Lock PCI setup registers */
2260 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
2261 iommu
->stored_addr_lo
| 1);
2264 static void iommu_enable_ga(struct amd_iommu
*iommu
)
2266 #ifdef CONFIG_IRQ_REMAP
2267 switch (amd_iommu_guest_ir
) {
2268 case AMD_IOMMU_GUEST_IR_VAPIC
:
2269 iommu_feature_enable(iommu
, CONTROL_GAM_EN
);
2271 case AMD_IOMMU_GUEST_IR_LEGACY_GA
:
2272 iommu_feature_enable(iommu
, CONTROL_GA_EN
);
2273 iommu
->irte_ops
= &irte_128_ops
;
2276 iommu
->irte_ops
= &irte_32_ops
;
2282 static void early_enable_iommu(struct amd_iommu
*iommu
)
2284 iommu_disable(iommu
);
2285 iommu_init_flags(iommu
);
2286 iommu_set_device_table(iommu
);
2287 iommu_enable_command_buffer(iommu
);
2288 iommu_enable_event_buffer(iommu
);
2289 iommu_set_exclusion_range(iommu
);
2290 iommu_enable_ga(iommu
);
2291 iommu_enable_xt(iommu
);
2292 iommu_enable(iommu
);
2293 iommu_flush_all_caches(iommu
);
2297 * This function finally enables all IOMMUs found in the system after
2298 * they have been initialized.
2300 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2301 * the old content of device table entries. Not this case or copy failed,
2302 * just continue as normal kernel does.
2304 static void early_enable_iommus(void)
2306 struct amd_iommu
*iommu
;
2309 if (!copy_device_table()) {
2311 * If come here because of failure in copying device table from old
2312 * kernel with all IOMMUs enabled, print error message and try to
2313 * free allocated old_dev_tbl_cpy.
2315 if (amd_iommu_pre_enabled
)
2316 pr_err("Failed to copy DEV table from previous kernel.\n");
2317 if (old_dev_tbl_cpy
!= NULL
)
2318 free_pages((unsigned long)old_dev_tbl_cpy
,
2319 get_order(dev_table_size
));
2321 for_each_iommu(iommu
) {
2322 clear_translation_pre_enabled(iommu
);
2323 early_enable_iommu(iommu
);
2326 pr_info("Copied DEV table from previous kernel.\n");
2327 free_pages((unsigned long)amd_iommu_dev_table
,
2328 get_order(dev_table_size
));
2329 amd_iommu_dev_table
= old_dev_tbl_cpy
;
2330 for_each_iommu(iommu
) {
2331 iommu_disable_command_buffer(iommu
);
2332 iommu_disable_event_buffer(iommu
);
2333 iommu_enable_command_buffer(iommu
);
2334 iommu_enable_event_buffer(iommu
);
2335 iommu_enable_ga(iommu
);
2336 iommu_enable_xt(iommu
);
2337 iommu_set_device_table(iommu
);
2338 iommu_flush_all_caches(iommu
);
2342 #ifdef CONFIG_IRQ_REMAP
2343 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
))
2344 amd_iommu_irq_ops
.capability
|= (1 << IRQ_POSTING_CAP
);
2348 static void enable_iommus_v2(void)
2350 struct amd_iommu
*iommu
;
2352 for_each_iommu(iommu
) {
2353 iommu_enable_ppr_log(iommu
);
2354 iommu_enable_gt(iommu
);
2358 static void enable_iommus(void)
2360 early_enable_iommus();
2365 static void disable_iommus(void)
2367 struct amd_iommu
*iommu
;
2369 for_each_iommu(iommu
)
2370 iommu_disable(iommu
);
2372 #ifdef CONFIG_IRQ_REMAP
2373 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir
))
2374 amd_iommu_irq_ops
.capability
&= ~(1 << IRQ_POSTING_CAP
);
2379 * Suspend/Resume support
2380 * disable suspend until real resume implemented
2383 static void amd_iommu_resume(void)
2385 struct amd_iommu
*iommu
;
2387 for_each_iommu(iommu
)
2388 iommu_apply_resume_quirks(iommu
);
2390 /* re-load the hardware */
2393 amd_iommu_enable_interrupts();
2396 static int amd_iommu_suspend(void)
2398 /* disable IOMMUs to go out of the way for BIOS */
2404 static struct syscore_ops amd_iommu_syscore_ops
= {
2405 .suspend
= amd_iommu_suspend
,
2406 .resume
= amd_iommu_resume
,
2409 static void __init
free_iommu_resources(void)
2411 kmemleak_free(irq_lookup_table
);
2412 free_pages((unsigned long)irq_lookup_table
,
2413 get_order(rlookup_table_size
));
2414 irq_lookup_table
= NULL
;
2416 kmem_cache_destroy(amd_iommu_irq_cache
);
2417 amd_iommu_irq_cache
= NULL
;
2419 free_pages((unsigned long)amd_iommu_rlookup_table
,
2420 get_order(rlookup_table_size
));
2421 amd_iommu_rlookup_table
= NULL
;
2423 free_pages((unsigned long)amd_iommu_alias_table
,
2424 get_order(alias_table_size
));
2425 amd_iommu_alias_table
= NULL
;
2427 free_pages((unsigned long)amd_iommu_dev_table
,
2428 get_order(dev_table_size
));
2429 amd_iommu_dev_table
= NULL
;
2434 /* SB IOAPIC is always on this device in AMD systems */
2435 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2437 static bool __init
check_ioapic_information(void)
2439 const char *fw_bug
= FW_BUG
;
2440 bool ret
, has_sb_ioapic
;
2443 has_sb_ioapic
= false;
2447 * If we have map overrides on the kernel command line the
2448 * messages in this function might not describe firmware bugs
2449 * anymore - so be careful
2454 for (idx
= 0; idx
< nr_ioapics
; idx
++) {
2455 int devid
, id
= mpc_ioapic_id(idx
);
2457 devid
= get_ioapic_devid(id
);
2459 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2462 } else if (devid
== IOAPIC_SB_DEVID
) {
2463 has_sb_ioapic
= true;
2468 if (!has_sb_ioapic
) {
2470 * We expect the SB IOAPIC to be listed in the IVRS
2471 * table. The system timer is connected to the SB IOAPIC
2472 * and if we don't have it in the list the system will
2473 * panic at boot time. This situation usually happens
2474 * when the BIOS is buggy and provides us the wrong
2475 * device id for the IOAPIC in the system.
2477 pr_err("%s: No southbridge IOAPIC found\n", fw_bug
);
2481 pr_err("Disabling interrupt remapping\n");
2486 static void __init
free_dma_resources(void)
2488 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap
,
2489 get_order(MAX_DOMAIN_ID
/8));
2490 amd_iommu_pd_alloc_bitmap
= NULL
;
2496 * This is the hardware init function for AMD IOMMU in the system.
2497 * This function is called either from amd_iommu_init or from the interrupt
2498 * remapping setup code.
2500 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2503 * 1 pass) Discover the most comprehensive IVHD type to use.
2505 * 2 pass) Find the highest PCI device id the driver has to handle.
2506 * Upon this information the size of the data structures is
2507 * determined that needs to be allocated.
2509 * 3 pass) Initialize the data structures just allocated with the
2510 * information in the ACPI table about available AMD IOMMUs
2511 * in the system. It also maps the PCI devices in the
2512 * system to specific IOMMUs
2514 * 4 pass) After the basic data structures are allocated and
2515 * initialized we update them with information about memory
2516 * remapping requirements parsed out of the ACPI table in
2519 * After everything is set up the IOMMUs are enabled and the necessary
2520 * hotplug and suspend notifiers are registered.
2522 static int __init
early_amd_iommu_init(void)
2524 struct acpi_table_header
*ivrs_base
;
2526 int i
, remap_cache_sz
, ret
= 0;
2529 if (!amd_iommu_detected
)
2532 status
= acpi_get_table("IVRS", 0, &ivrs_base
);
2533 if (status
== AE_NOT_FOUND
)
2535 else if (ACPI_FAILURE(status
)) {
2536 const char *err
= acpi_format_exception(status
);
2537 pr_err("IVRS table error: %s\n", err
);
2542 * Validate checksum here so we don't need to do it when
2543 * we actually parse the table
2545 ret
= check_ivrs_checksum(ivrs_base
);
2549 amd_iommu_target_ivhd_type
= get_highest_supported_ivhd_type(ivrs_base
);
2550 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type
);
2553 * First parse ACPI tables to find the largest Bus/Dev/Func
2554 * we need to handle. Upon this information the shared data
2555 * structures for the IOMMUs in the system will be allocated
2557 ret
= find_last_devid_acpi(ivrs_base
);
2561 dev_table_size
= tbl_size(DEV_TABLE_ENTRY_SIZE
);
2562 alias_table_size
= tbl_size(ALIAS_TABLE_ENTRY_SIZE
);
2563 rlookup_table_size
= tbl_size(RLOOKUP_TABLE_ENTRY_SIZE
);
2565 /* Device table - directly used by all IOMMUs */
2567 amd_iommu_dev_table
= (void *)__get_free_pages(
2568 GFP_KERNEL
| __GFP_ZERO
| GFP_DMA32
,
2569 get_order(dev_table_size
));
2570 if (amd_iommu_dev_table
== NULL
)
2574 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2575 * IOMMU see for that device
2577 amd_iommu_alias_table
= (void *)__get_free_pages(GFP_KERNEL
,
2578 get_order(alias_table_size
));
2579 if (amd_iommu_alias_table
== NULL
)
2582 /* IOMMU rlookup table - find the IOMMU for a specific device */
2583 amd_iommu_rlookup_table
= (void *)__get_free_pages(
2584 GFP_KERNEL
| __GFP_ZERO
,
2585 get_order(rlookup_table_size
));
2586 if (amd_iommu_rlookup_table
== NULL
)
2589 amd_iommu_pd_alloc_bitmap
= (void *)__get_free_pages(
2590 GFP_KERNEL
| __GFP_ZERO
,
2591 get_order(MAX_DOMAIN_ID
/8));
2592 if (amd_iommu_pd_alloc_bitmap
== NULL
)
2596 * let all alias entries point to itself
2598 for (i
= 0; i
<= amd_iommu_last_bdf
; ++i
)
2599 amd_iommu_alias_table
[i
] = i
;
2602 * never allocate domain 0 because its used as the non-allocated and
2603 * error value placeholder
2605 __set_bit(0, amd_iommu_pd_alloc_bitmap
);
2608 * now the data structures are allocated and basically initialized
2609 * start the real acpi table scan
2611 ret
= init_iommu_all(ivrs_base
);
2615 /* Disable IOMMU if there's Stoney Ridge graphics */
2616 for (i
= 0; i
< 32; i
++) {
2617 pci_id
= read_pci_config(0, i
, 0, 0);
2618 if ((pci_id
& 0xffff) == 0x1002 && (pci_id
>> 16) == 0x98e4) {
2619 pr_info("Disable IOMMU on Stoney Ridge\n");
2620 amd_iommu_disabled
= true;
2625 /* Disable any previously enabled IOMMUs */
2626 if (!is_kdump_kernel() || amd_iommu_disabled
)
2629 if (amd_iommu_irq_remap
)
2630 amd_iommu_irq_remap
= check_ioapic_information();
2632 if (amd_iommu_irq_remap
) {
2634 * Interrupt remapping enabled, create kmem_cache for the
2638 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir
))
2639 remap_cache_sz
= MAX_IRQS_PER_TABLE
* sizeof(u32
);
2641 remap_cache_sz
= MAX_IRQS_PER_TABLE
* (sizeof(u64
) * 2);
2642 amd_iommu_irq_cache
= kmem_cache_create("irq_remap_cache",
2644 IRQ_TABLE_ALIGNMENT
,
2646 if (!amd_iommu_irq_cache
)
2649 irq_lookup_table
= (void *)__get_free_pages(
2650 GFP_KERNEL
| __GFP_ZERO
,
2651 get_order(rlookup_table_size
));
2652 kmemleak_alloc(irq_lookup_table
, rlookup_table_size
,
2654 if (!irq_lookup_table
)
2658 ret
= init_memory_definitions(ivrs_base
);
2662 /* init the device table */
2663 init_device_table();
2666 /* Don't leak any ACPI memory */
2667 acpi_put_table(ivrs_base
);
2673 static int amd_iommu_enable_interrupts(void)
2675 struct amd_iommu
*iommu
;
2678 for_each_iommu(iommu
) {
2679 ret
= iommu_init_msi(iommu
);
2688 static bool detect_ivrs(void)
2690 struct acpi_table_header
*ivrs_base
;
2693 status
= acpi_get_table("IVRS", 0, &ivrs_base
);
2694 if (status
== AE_NOT_FOUND
)
2696 else if (ACPI_FAILURE(status
)) {
2697 const char *err
= acpi_format_exception(status
);
2698 pr_err("IVRS table error: %s\n", err
);
2702 acpi_put_table(ivrs_base
);
2704 /* Make sure ACS will be enabled during PCI probe */
2710 /****************************************************************************
2712 * AMD IOMMU Initialization State Machine
2714 ****************************************************************************/
2716 static int __init
state_next(void)
2720 switch (init_state
) {
2721 case IOMMU_START_STATE
:
2722 if (!detect_ivrs()) {
2723 init_state
= IOMMU_NOT_FOUND
;
2726 init_state
= IOMMU_IVRS_DETECTED
;
2729 case IOMMU_IVRS_DETECTED
:
2730 ret
= early_amd_iommu_init();
2731 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_ACPI_FINISHED
;
2732 if (init_state
== IOMMU_ACPI_FINISHED
&& amd_iommu_disabled
) {
2733 pr_info("AMD IOMMU disabled\n");
2734 init_state
= IOMMU_CMDLINE_DISABLED
;
2738 case IOMMU_ACPI_FINISHED
:
2739 early_enable_iommus();
2740 x86_platform
.iommu_shutdown
= disable_iommus
;
2741 init_state
= IOMMU_ENABLED
;
2744 register_syscore_ops(&amd_iommu_syscore_ops
);
2745 ret
= amd_iommu_init_pci();
2746 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_PCI_INIT
;
2749 case IOMMU_PCI_INIT
:
2750 ret
= amd_iommu_enable_interrupts();
2751 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_INTERRUPTS_EN
;
2753 case IOMMU_INTERRUPTS_EN
:
2754 ret
= amd_iommu_init_dma_ops();
2755 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_DMA_OPS
;
2758 init_state
= IOMMU_INITIALIZED
;
2760 case IOMMU_INITIALIZED
:
2763 case IOMMU_NOT_FOUND
:
2764 case IOMMU_INIT_ERROR
:
2765 case IOMMU_CMDLINE_DISABLED
:
2766 /* Error states => do nothing */
2775 free_dma_resources();
2776 if (!irq_remapping_enabled
) {
2778 free_iommu_resources();
2780 struct amd_iommu
*iommu
;
2782 uninit_device_table_dma();
2783 for_each_iommu(iommu
)
2784 iommu_flush_all_caches(iommu
);
2790 static int __init
iommu_go_to_state(enum iommu_init_state state
)
2794 while (init_state
!= state
) {
2795 if (init_state
== IOMMU_NOT_FOUND
||
2796 init_state
== IOMMU_INIT_ERROR
||
2797 init_state
== IOMMU_CMDLINE_DISABLED
)
2805 #ifdef CONFIG_IRQ_REMAP
2806 int __init
amd_iommu_prepare(void)
2810 amd_iommu_irq_remap
= true;
2812 ret
= iommu_go_to_state(IOMMU_ACPI_FINISHED
);
2815 return amd_iommu_irq_remap
? 0 : -ENODEV
;
2818 int __init
amd_iommu_enable(void)
2822 ret
= iommu_go_to_state(IOMMU_ENABLED
);
2826 irq_remapping_enabled
= 1;
2827 return amd_iommu_xt_mode
;
2830 void amd_iommu_disable(void)
2832 amd_iommu_suspend();
2835 int amd_iommu_reenable(int mode
)
2842 int __init
amd_iommu_enable_faulting(void)
2844 /* We enable MSI later when PCI is initialized */
2850 * This is the core init function for AMD IOMMU hardware in the system.
2851 * This function is called from the generic x86 DMA layer initialization
2854 static int __init
amd_iommu_init(void)
2856 struct amd_iommu
*iommu
;
2859 ret
= iommu_go_to_state(IOMMU_INITIALIZED
);
2860 #ifdef CONFIG_GART_IOMMU
2861 if (ret
&& list_empty(&amd_iommu_list
)) {
2863 * We failed to initialize the AMD IOMMU - try fallback
2864 * to GART if possible.
2870 for_each_iommu(iommu
)
2871 amd_iommu_debugfs_setup(iommu
);
2876 static bool amd_iommu_sme_check(void)
2878 if (!sme_active() || (boot_cpu_data
.x86
!= 0x17))
2881 /* For Fam17h, a specific level of support is required */
2882 if (boot_cpu_data
.microcode
>= 0x08001205)
2885 if ((boot_cpu_data
.microcode
>= 0x08001126) &&
2886 (boot_cpu_data
.microcode
<= 0x080011ff))
2889 pr_notice("IOMMU not currently supported when SME is active\n");
2894 /****************************************************************************
2896 * Early detect code. This code runs at IOMMU detection time in the DMA
2897 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2900 ****************************************************************************/
2901 int __init
amd_iommu_detect(void)
2905 if (no_iommu
|| (iommu_detected
&& !gart_iommu_aperture
))
2908 if (!amd_iommu_sme_check())
2911 ret
= iommu_go_to_state(IOMMU_IVRS_DETECTED
);
2915 amd_iommu_detected
= true;
2917 x86_init
.iommu
.iommu_init
= amd_iommu_init
;
2922 /****************************************************************************
2924 * Parsing functions for the AMD IOMMU specific kernel command line
2927 ****************************************************************************/
2929 static int __init
parse_amd_iommu_dump(char *str
)
2931 amd_iommu_dump
= true;
2936 static int __init
parse_amd_iommu_intr(char *str
)
2938 for (; *str
; ++str
) {
2939 if (strncmp(str
, "legacy", 6) == 0) {
2940 amd_iommu_guest_ir
= AMD_IOMMU_GUEST_IR_LEGACY_GA
;
2943 if (strncmp(str
, "vapic", 5) == 0) {
2944 amd_iommu_guest_ir
= AMD_IOMMU_GUEST_IR_VAPIC
;
2951 static int __init
parse_amd_iommu_options(char *str
)
2953 for (; *str
; ++str
) {
2954 if (strncmp(str
, "fullflush", 9) == 0)
2955 amd_iommu_unmap_flush
= true;
2956 if (strncmp(str
, "off", 3) == 0)
2957 amd_iommu_disabled
= true;
2958 if (strncmp(str
, "force_isolation", 15) == 0)
2959 amd_iommu_force_isolation
= true;
2965 static int __init
parse_ivrs_ioapic(char *str
)
2967 unsigned int bus
, dev
, fn
;
2971 ret
= sscanf(str
, "[%d]=%x:%x.%x", &id
, &bus
, &dev
, &fn
);
2974 pr_err("Invalid command line: ivrs_ioapic%s\n", str
);
2978 if (early_ioapic_map_size
== EARLY_MAP_SIZE
) {
2979 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2984 devid
= ((bus
& 0xff) << 8) | ((dev
& 0x1f) << 3) | (fn
& 0x7);
2986 cmdline_maps
= true;
2987 i
= early_ioapic_map_size
++;
2988 early_ioapic_map
[i
].id
= id
;
2989 early_ioapic_map
[i
].devid
= devid
;
2990 early_ioapic_map
[i
].cmd_line
= true;
2995 static int __init
parse_ivrs_hpet(char *str
)
2997 unsigned int bus
, dev
, fn
;
3001 ret
= sscanf(str
, "[%d]=%x:%x.%x", &id
, &bus
, &dev
, &fn
);
3004 pr_err("Invalid command line: ivrs_hpet%s\n", str
);
3008 if (early_hpet_map_size
== EARLY_MAP_SIZE
) {
3009 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3014 devid
= ((bus
& 0xff) << 8) | ((dev
& 0x1f) << 3) | (fn
& 0x7);
3016 cmdline_maps
= true;
3017 i
= early_hpet_map_size
++;
3018 early_hpet_map
[i
].id
= id
;
3019 early_hpet_map
[i
].devid
= devid
;
3020 early_hpet_map
[i
].cmd_line
= true;
3025 static int __init
parse_ivrs_acpihid(char *str
)
3028 char *hid
, *uid
, *p
;
3029 char acpiid
[ACPIHID_UID_LEN
+ ACPIHID_HID_LEN
] = {0};
3032 ret
= sscanf(str
, "[%x:%x.%x]=%s", &bus
, &dev
, &fn
, acpiid
);
3034 pr_err("Invalid command line: ivrs_acpihid(%s)\n", str
);
3039 hid
= strsep(&p
, ":");
3042 if (!hid
|| !(*hid
) || !uid
) {
3043 pr_err("Invalid command line: hid or uid\n");
3047 i
= early_acpihid_map_size
++;
3048 memcpy(early_acpihid_map
[i
].hid
, hid
, strlen(hid
));
3049 memcpy(early_acpihid_map
[i
].uid
, uid
, strlen(uid
));
3050 early_acpihid_map
[i
].devid
=
3051 ((bus
& 0xff) << 8) | ((dev
& 0x1f) << 3) | (fn
& 0x7);
3052 early_acpihid_map
[i
].cmd_line
= true;
3057 __setup("amd_iommu_dump", parse_amd_iommu_dump
);
3058 __setup("amd_iommu=", parse_amd_iommu_options
);
3059 __setup("amd_iommu_intr=", parse_amd_iommu_intr
);
3060 __setup("ivrs_ioapic", parse_ivrs_ioapic
);
3061 __setup("ivrs_hpet", parse_ivrs_hpet
);
3062 __setup("ivrs_acpihid", parse_ivrs_acpihid
);
3064 IOMMU_INIT_FINISH(amd_iommu_detect
,
3065 gart_iommu_hole_init
,
3069 bool amd_iommu_v2_supported(void)
3071 return amd_iommu_v2_present
;
3073 EXPORT_SYMBOL(amd_iommu_v2_supported
);
3075 struct amd_iommu
*get_amd_iommu(unsigned int idx
)
3078 struct amd_iommu
*iommu
;
3080 for_each_iommu(iommu
)
3085 EXPORT_SYMBOL(get_amd_iommu
);
3087 /****************************************************************************
3089 * IOMMU EFR Performance Counter support functionality. This code allows
3090 * access to the IOMMU PC functionality.
3092 ****************************************************************************/
3094 u8
amd_iommu_pc_get_max_banks(unsigned int idx
)
3096 struct amd_iommu
*iommu
= get_amd_iommu(idx
);
3099 return iommu
->max_banks
;
3103 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks
);
3105 bool amd_iommu_pc_supported(void)
3107 return amd_iommu_pc_present
;
3109 EXPORT_SYMBOL(amd_iommu_pc_supported
);
3111 u8
amd_iommu_pc_get_max_counters(unsigned int idx
)
3113 struct amd_iommu
*iommu
= get_amd_iommu(idx
);
3116 return iommu
->max_counters
;
3120 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters
);
3122 static int iommu_pc_get_set_reg(struct amd_iommu
*iommu
, u8 bank
, u8 cntr
,
3123 u8 fxn
, u64
*value
, bool is_write
)
3128 /* Make sure the IOMMU PC resource is available */
3129 if (!amd_iommu_pc_present
)
3132 /* Check for valid iommu and pc register indexing */
3133 if (WARN_ON(!iommu
|| (fxn
> 0x28) || (fxn
& 7)))
3136 offset
= (u32
)(((0x40 | bank
) << 12) | (cntr
<< 8) | fxn
);
3138 /* Limit the offset to the hw defined mmio region aperture */
3139 max_offset_lim
= (u32
)(((0x40 | iommu
->max_banks
) << 12) |
3140 (iommu
->max_counters
<< 8) | 0x28);
3141 if ((offset
< MMIO_CNTR_REG_OFFSET
) ||
3142 (offset
> max_offset_lim
))
3146 u64 val
= *value
& GENMASK_ULL(47, 0);
3148 writel((u32
)val
, iommu
->mmio_base
+ offset
);
3149 writel((val
>> 32), iommu
->mmio_base
+ offset
+ 4);
3151 *value
= readl(iommu
->mmio_base
+ offset
+ 4);
3153 *value
|= readl(iommu
->mmio_base
+ offset
);
3154 *value
&= GENMASK_ULL(47, 0);
3160 int amd_iommu_pc_get_reg(struct amd_iommu
*iommu
, u8 bank
, u8 cntr
, u8 fxn
, u64
*value
)
3165 return iommu_pc_get_set_reg(iommu
, bank
, cntr
, fxn
, value
, false);
3167 EXPORT_SYMBOL(amd_iommu_pc_get_reg
);
3169 int amd_iommu_pc_set_reg(struct amd_iommu
*iommu
, u8 bank
, u8 cntr
, u8 fxn
, u64
*value
)
3174 return iommu_pc_get_set_reg(iommu
, bank
, cntr
, fxn
, value
, true);
3176 EXPORT_SYMBOL(amd_iommu_pc_set_reg
);