20 #include <linux/pci.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
28 #include <linux/export.h>
31 #include <asm/iommu.h>
34 #include <asm/iommu_table.h>
36 #include <asm/irq_remapping.h>
45 #define IVRS_HEADER_LENGTH 48
47 #define ACPI_IVHD_TYPE 0x10
48 #define ACPI_IVMD_TYPE_ALL 0x20
49 #define ACPI_IVMD_TYPE 0x21
50 #define ACPI_IVMD_TYPE_RANGE 0x22
52 #define IVHD_DEV_ALL 0x01
53 #define IVHD_DEV_SELECT 0x02
54 #define IVHD_DEV_SELECT_RANGE_START 0x03
55 #define IVHD_DEV_RANGE_END 0x04
56 #define IVHD_DEV_ALIAS 0x42
57 #define IVHD_DEV_ALIAS_RANGE 0x43
58 #define IVHD_DEV_EXT_SELECT 0x46
59 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
60 #define IVHD_DEV_SPECIAL 0x48
62 #define IVHD_SPECIAL_IOAPIC 1
63 #define IVHD_SPECIAL_HPET 2
65 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
66 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
67 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
68 #define IVHD_FLAG_ISOC_EN_MASK 0x08
70 #define IVMD_FLAG_EXCL_RANGE 0x08
71 #define IVMD_FLAG_UNITY_MAP 0x01
73 #define ACPI_DEVFLAG_INITPASS 0x01
74 #define ACPI_DEVFLAG_EXTINT 0x02
75 #define ACPI_DEVFLAG_NMI 0x04
76 #define ACPI_DEVFLAG_SYSMGT1 0x10
77 #define ACPI_DEVFLAG_SYSMGT2 0x20
78 #define ACPI_DEVFLAG_LINT0 0x40
79 #define ACPI_DEVFLAG_LINT1 0x80
80 #define ACPI_DEVFLAG_ATSDIS 0x10000000
134 static bool amd_iommu_detected;
199 static u32 dev_table_size;
200 static u32 alias_table_size;
201 static u32 rlookup_table_size;
218 static int amd_iommu_enable_interrupts(
void);
221 static inline void update_last_devid(
u16 devid)
223 if (devid > amd_iommu_last_bdf)
224 amd_iommu_last_bdf =
devid;
227 static inline unsigned long tbl_size(
int entry_size)
230 get_order(((
int)amd_iommu_last_bdf + 1) * entry_size);
241 pci_write_config_dword(iommu->
dev, 0xf8, (address | l1 << 16));
242 pci_read_config_dword(iommu->
dev, 0xfc, &val);
248 pci_write_config_dword(iommu->
dev, 0xf8, (address | l1 << 16 | 1 << 31));
249 pci_write_config_dword(iommu->
dev, 0xfc, val);
250 pci_write_config_dword(iommu->
dev, 0xf8, (address | l1 << 16));
257 pci_write_config_dword(iommu->
dev, 0xf0, address);
258 pci_read_config_dword(iommu->
dev, 0xf4, &val);
264 pci_write_config_dword(iommu->
dev, 0xf0, (address | 1 << 8));
265 pci_write_config_dword(iommu->
dev, 0xf4, val);
292 &entry,
sizeof(entry));
296 &entry,
sizeof(entry));
300 static void iommu_set_device_table(
struct amd_iommu *iommu)
307 entry |= (dev_table_size >> 12) - 1;
309 &entry,
sizeof(entry));
322 static void iommu_feature_disable(
struct amd_iommu *iommu,
u8 bit)
331 static void iommu_set_inv_tlb_timeout(
struct amd_iommu *iommu,
int timeout)
342 static void iommu_enable(
struct amd_iommu *iommu)
347 static void iommu_disable(
struct amd_iommu *iommu)
367 pr_err(
"AMD-Vi: Can not reserve memory region %llx for mmio\n",
369 pr_err(
"AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
395 static inline int ivhd_entry_length(
u8 *ivhd)
397 return 0x04 << (*ivhd >> 6);
420 u8 *
p = (
void *)h, *
end = (
void *)
h;
439 update_last_devid(dev->
devid);
444 p += ivhd_entry_length(p);
467 for (i = 0; i < table->
length; ++
i)
480 find_last_devid_from_ivhd(h);
537 static void iommu_enable_command_buffer(
struct amd_iommu *iommu)
547 &entry,
sizeof(entry));
573 static void iommu_enable_event_buffer(
struct amd_iommu *iommu)
582 &entry,
sizeof(entry));
608 static void iommu_enable_ppr_log(
struct amd_iommu *iommu)
618 &entry,
sizeof(entry));
636 static void iommu_enable_gt(
struct amd_iommu *iommu)
645 static void set_dev_entry_bit(
u16 devid,
u8 bit)
647 int i = (bit >> 6) & 0x03;
648 int _bit = bit & 0x3f;
653 static int get_dev_entry_bit(
u16 devid,
u8 bit)
655 int i = (bit >> 6) & 0x03;
656 int _bit = bit & 0x3f;
658 return (amd_iommu_dev_table[devid].
data[i] & (1
UL << _bit)) >> _bit;
676 amd_iommu_rlookup_table[
devid] = iommu;
703 set_iommu_for_device(iommu, devid);
706 static int add_special_device(
u8 type,
u8 id,
u16 devid)
763 u16 devid = 0, devid_start = 0, devid_to = 0;
764 u32 dev_i, ext_flags = 0;
785 DUMP_printk(
" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
786 " last device %02x:%02x.%x flags: %02x\n",
796 dev_i <= iommu->last_device; ++dev_i)
797 set_dev_entry_from_acpi(iommu, dev_i,
802 DUMP_printk(
" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
810 set_dev_entry_from_acpi(iommu, devid, e->
flags, 0);
815 "devid: %02x:%02x.%x flags: %02x\n",
821 devid_start = e->
devid;
828 DUMP_printk(
" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
829 "flags: %02x devid_to: %02x:%02x.%x\n",
839 devid_to = e->
ext >> 8;
840 set_dev_entry_from_acpi(iommu, devid , e->
flags, 0);
841 set_dev_entry_from_acpi(iommu, devid_to, e->
flags, 0);
842 amd_iommu_alias_table[
devid] = devid_to;
847 "devid: %02x:%02x.%x flags: %02x "
848 "devid_to: %02x:%02x.%x\n",
857 devid_start = e->
devid;
859 devid_to = e->
ext >> 8;
865 DUMP_printk(
" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
866 "flags: %02x ext: %08x\n",
873 set_dev_entry_from_acpi(iommu, devid, e->
flags,
879 "%02x:%02x.%x flags: %02x ext: %08x\n",
885 devid_start = e->
devid;
892 DUMP_printk(
" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
898 for (dev_i = devid_start; dev_i <=
devid; ++dev_i) {
900 amd_iommu_alias_table[dev_i] = devid_to;
901 set_dev_entry_from_acpi(iommu,
902 devid_to, flags, ext_flags);
904 set_dev_entry_from_acpi(iommu, dev_i,
914 handle = e->
ext & 0xff;
915 devid = (e->
ext >> 8) & 0xffff;
916 type = (e->
ext >> 24) & 0xff;
925 DUMP_printk(
" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
931 set_dev_entry_from_acpi(iommu, devid, e->
flags, 0);
932 ret = add_special_device(type, handle, devid);
941 p += ivhd_entry_length(p);
952 for (i = iommu->
first_device; i <= iommu->last_device; ++i)
953 set_iommu_for_device(iommu, i);
960 free_command_buffer(iommu);
961 free_event_buffer(iommu);
963 iommu_unmap_mmio_space(iommu);
966 static void __init free_iommu_all(
void)
972 free_iommu_one(iommu);
990 iommu->
index = amd_iommus_present++;
993 WARN(1,
"AMD-Vi: System has more IOMMUs than supported by this driver\n");
998 amd_iommus[iommu->
index] = iommu;
1011 iommu->
cmd_buf = alloc_command_buffer(iommu);
1015 iommu->
evt_buf = alloc_event_buffer(iommu);
1021 ret = init_iommu_from_acpi(iommu, h);
1029 amd_iommu_rlookup_table[iommu->
devid] =
NULL;
1031 init_iommu_devices(iommu);
1056 "seg: %d flags: %01x info %04x\n",
1067 ret = init_iommu_one(iommu, h);
1082 static int iommu_init_pci(
struct amd_iommu *iommu)
1088 iommu->
devid & 0xff);
1120 pasids = (1 << shift);
1140 iommu->
ppr_log = alloc_ppr_log(iommu);
1148 if (is_rd890_iommu(iommu->
dev)) {
1159 pci_read_config_dword(iommu->
dev, iommu->
cap_ptr + 4,
1161 pci_read_config_dword(iommu->
dev, iommu->
cap_ptr + 8,
1167 for (i = 0; i < 6; i++)
1168 for (j = 0; j < 0x12; j++)
1169 iommu->
stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1171 for (i = 0; i < 0x83; i++)
1172 iommu->
stored_l2[i] = iommu_read_l2(iommu, i);
1178 static void print_iommu_info(
void)
1180 static const char *
const feat_str[] = {
1181 "PreF",
"PPR",
"X2APIC",
"NX",
"GT",
"[5]",
1182 "IA",
"GA",
"HE",
"PC"
1189 pr_info(
"AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1193 pr_info(
"AMD-Vi: Extended features: ");
1195 if (iommu_feature(iommu, (1ULL << i)))
1202 pr_info(
"AMD-Vi: Interrupt remapping enabled\n");
1205 static int __init amd_iommu_init_pci(
void)
1211 ret = iommu_init_pci(iommu);
1232 static int iommu_setup_msi(
struct amd_iommu *iommu)
1236 r = pci_enable_msi(iommu->
dev);
1256 static int iommu_init_msi(
struct amd_iommu *iommu)
1264 ret = iommu_setup_msi(iommu);
1288 static void __init free_unity_maps(
void)
1305 set_device_exclusion_range(m->
devid, m);
1309 set_device_exclusion_range(i, m);
1312 for (i = m->
devid; i <= m->
aux; ++i)
1313 set_device_exclusion_range(i, m);
1337 s =
"IVMD_TYPEi\t\t\t";
1341 s =
"IVMD_TYPE_ALL\t\t";
1346 s =
"IVMD_TYPE_RANGE\t\t";
1355 DUMP_printk(
"%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1356 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1379 init_exclusion_range(m);
1381 init_unity_map_range(m);
1393 static void init_device_table_dma(
void)
1403 static void __init uninit_device_table_dma(
void)
1408 amd_iommu_dev_table[
devid].
data[0] = 0ULL;
1409 amd_iommu_dev_table[
devid].
data[1] = 0ULL;
1413 static void init_device_table(
void)
1417 if (!amd_iommu_irq_remap)
1424 static void iommu_init_flags(
struct amd_iommu *iommu)
1451 static void iommu_apply_resume_quirks(
struct amd_iommu *iommu)
1454 u32 ioc_feature_control;
1458 if (!is_rd890_iommu(iommu->
dev) || !pdev)
1467 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1468 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1471 if (!(ioc_feature_control & 0x1))
1472 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1475 pci_write_config_dword(iommu->
dev, iommu->
cap_ptr + 4,
1477 pci_write_config_dword(iommu->
dev, iommu->
cap_ptr + 8,
1481 for (i = 0; i < 6; i++)
1482 for (j = 0; j < 0x12; j++)
1483 iommu_write_l1(iommu, i, j, iommu->
stored_l1[i][j]);
1486 for (i = 0; i < 0x83; i++)
1487 iommu_write_l2(iommu, i, iommu->
stored_l2[i]);
1490 pci_write_config_dword(iommu->
dev, iommu->
cap_ptr + 4,
1498 static void early_enable_iommus(
void)
1503 iommu_disable(iommu);
1504 iommu_init_flags(iommu);
1505 iommu_set_device_table(iommu);
1506 iommu_enable_command_buffer(iommu);
1507 iommu_enable_event_buffer(iommu);
1508 iommu_set_exclusion_range(iommu);
1509 iommu_enable(iommu);
1514 static void enable_iommus_v2(
void)
1519 iommu_enable_ppr_log(iommu);
1520 iommu_enable_gt(iommu);
1524 static void enable_iommus(
void)
1526 early_enable_iommus();
1531 static void disable_iommus(
void)
1536 iommu_disable(iommu);
1544 static
void amd_iommu_resume(
void)
1549 iommu_apply_resume_quirks(iommu);
1554 amd_iommu_enable_interrupts();
1557 static
int amd_iommu_suspend(
void)
1565 static struct syscore_ops amd_iommu_syscore_ops = {
1566 .suspend = amd_iommu_suspend,
1567 .resume = amd_iommu_resume,
1570 static void __init free_on_init_error(
void)
1581 free_pages((
unsigned long)amd_iommu_rlookup_table,
1584 free_pages((
unsigned long)amd_iommu_alias_table,
1587 free_pages((
unsigned long)amd_iommu_dev_table,
1592 #ifdef CONFIG_GART_IOMMU
1603 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
1605 static bool __init check_ioapic_information(
void)
1607 bool ret, has_sb_ioapic;
1610 has_sb_ioapic =
false;
1616 devid = get_ioapic_devid(
id);
1618 pr_err(
FW_BUG "AMD-Vi: IOAPIC[%d] not in IVRS table\n",
id);
1621 has_sb_ioapic =
true;
1626 if (!has_sb_ioapic) {
1635 pr_err(
FW_BUG "AMD-Vi: No southbridge IOAPIC found in IVRS table\n");
1639 pr_err(
"AMD-Vi: Disabling interrupt remapping due to BIOS Bug(s)\n");
1644 static void __init free_dma_resources(
void)
1648 free_pages((
unsigned long)amd_iommu_pd_alloc_bitmap,
1679 static int __init early_amd_iommu_init(
void)
1682 acpi_size ivrs_size;
1686 if (!amd_iommu_detected)
1694 pr_err(
"AMD-Vi: IVRS table error: %s\n", err);
1703 ret = find_last_devid_acpi(ivrs_base);
1715 if (amd_iommu_dev_table ==
NULL)
1724 if (amd_iommu_alias_table ==
NULL)
1731 if (amd_iommu_rlookup_table ==
NULL)
1737 if (amd_iommu_pd_alloc_bitmap ==
NULL)
1744 amd_iommu_alias_table[i] = i;
1750 amd_iommu_pd_alloc_bitmap[0] = 1;
1758 ret = init_iommu_all(ivrs_base);
1762 if (amd_iommu_irq_remap)
1765 if (amd_iommu_irq_remap) {
1780 if (!irq_lookup_table)
1784 ret = init_memory_definitions(ivrs_base);
1789 init_device_table();
1799 static int amd_iommu_enable_interrupts(
void)
1805 ret = iommu_init_msi(iommu);
1814 static bool detect_ivrs(
void)
1817 acpi_size ivrs_size;
1825 pr_err(
"AMD-Vi: IVRS table error: %s\n", err);
1840 static int amd_iommu_init_dma(
void)
1845 init_device_table_dma();
1850 if (iommu_pass_through)
1871 static
int __init state_next(
void)
1875 switch (init_state) {
1877 if (!detect_ivrs()) {
1885 ret = early_amd_iommu_init();
1889 early_enable_iommus();
1895 ret = amd_iommu_init_pci();
1900 ret = amd_iommu_enable_interrupts();
1904 ret = amd_iommu_init_dma();
1930 while (init_state != state) {
1940 #ifdef CONFIG_IRQ_REMAP
1966 amd_iommu_suspend();
1988 static int __init amd_iommu_init(
void)
1994 free_dma_resources();
1997 free_on_init_error();
2001 uninit_device_table_dma();
2024 if (amd_iommu_disabled)
2031 amd_iommu_detected =
true;
2033 x86_init.iommu.iommu_init = amd_iommu_init;
2045 static int __init parse_amd_iommu_dump(
char *
str)
2047 amd_iommu_dump =
true;
2052 static int __init parse_amd_iommu_options(
char *
str)
2055 if (
strncmp(str,
"fullflush", 9) == 0)
2056 amd_iommu_unmap_flush =
true;
2057 if (
strncmp(str,
"off", 3) == 0)
2058 amd_iommu_disabled =
true;
2059 if (
strncmp(str,
"force_isolation", 15) == 0)
2066 __setup(
"amd_iommu_dump", parse_amd_iommu_dump);
2067 __setup(
"amd_iommu=", parse_amd_iommu_options);