Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 1 | /* |
Joerg Roedel | 5d0d715 | 2010-10-13 11:13:21 +0200 | [diff] [blame] | 2 | * Copyright (C) 2007-2010 Advanced Micro Devices, Inc. |
Joerg Roedel | 63ce3ae | 2015-02-04 16:12:55 +0100 | [diff] [blame] | 3 | * Author: Joerg Roedel <jroedel@suse.de> |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 4 | * Leo Duran <leo.duran@amd.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/pci.h> |
| 21 | #include <linux/acpi.h> |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 22 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Rafael J. Wysocki | f3c6ea1 | 2011-03-23 22:15:54 +0100 | [diff] [blame] | 24 | #include <linux/syscore_ops.h> |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/msi.h> |
Joerg Roedel | 403f81d | 2011-06-14 16:44:25 +0200 | [diff] [blame] | 27 | #include <linux/amd-iommu.h> |
Joerg Roedel | 400a28a | 2011-11-28 15:11:02 +0100 | [diff] [blame] | 28 | #include <linux/export.h> |
Alex Williamson | 066f2e9 | 2014-06-12 16:12:37 -0600 | [diff] [blame] | 29 | #include <linux/iommu.h> |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 30 | #include <asm/pci-direct.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 31 | #include <asm/iommu.h> |
Joerg Roedel | 1d9b16d | 2008-11-27 18:39:15 +0100 | [diff] [blame] | 32 | #include <asm/gart.h> |
FUJITA Tomonori | ea1b0d3 | 2009-11-10 19:46:15 +0900 | [diff] [blame] | 33 | #include <asm/x86_init.h> |
Konrad Rzeszutek Wilk | 22e6daf | 2010-08-26 13:58:03 -0400 | [diff] [blame] | 34 | #include <asm/iommu_table.h> |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 35 | #include <asm/io_apic.h> |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 36 | #include <asm/irq_remapping.h> |
Joerg Roedel | 403f81d | 2011-06-14 16:44:25 +0200 | [diff] [blame] | 37 | |
| 38 | #include "amd_iommu_proto.h" |
| 39 | #include "amd_iommu_types.h" |
Joerg Roedel | 05152a0 | 2012-06-15 16:53:51 +0200 | [diff] [blame] | 40 | #include "irq_remapping.h" |
Joerg Roedel | 403f81d | 2011-06-14 16:44:25 +0200 | [diff] [blame] | 41 | |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 42 | /* |
| 43 | * definitions for the ACPI scanning code |
| 44 | */ |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 45 | #define IVRS_HEADER_LENGTH 48 |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 46 | |
| 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 |
| 51 | |
| 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 |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 60 | #define IVHD_DEV_SPECIAL 0x48 |
| 61 | |
| 62 | #define IVHD_SPECIAL_IOAPIC 1 |
| 63 | #define IVHD_SPECIAL_HPET 2 |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 64 | |
Joerg Roedel | 6da7342 | 2009-05-04 11:44:38 +0200 | [diff] [blame] | 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 |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 69 | |
| 70 | #define IVMD_FLAG_EXCL_RANGE 0x08 |
| 71 | #define IVMD_FLAG_UNITY_MAP 0x01 |
| 72 | |
| 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 |
| 81 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 82 | /* |
| 83 | * ACPI table definitions |
| 84 | * |
| 85 | * These data structures are laid over the table to parse the important values |
| 86 | * out of it. |
| 87 | */ |
| 88 | |
| 89 | /* |
| 90 | * structure describing one IOMMU in the ACPI table. Typically followed by one |
| 91 | * or more ivhd_entrys. |
| 92 | */ |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 93 | struct ivhd_header { |
| 94 | u8 type; |
| 95 | u8 flags; |
| 96 | u16 length; |
| 97 | u16 devid; |
| 98 | u16 cap_ptr; |
| 99 | u64 mmio_phys; |
| 100 | u16 pci_seg; |
| 101 | u16 info; |
Suravee Suthikulpanit | 7d7d38a | 2016-04-01 09:05:57 -0400 | [diff] [blame] | 102 | u32 efr_attr; |
| 103 | |
| 104 | /* Following only valid on IVHD type 11h and 40h */ |
| 105 | u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */ |
| 106 | u64 res; |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 107 | } __attribute__((packed)); |
| 108 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 109 | /* |
| 110 | * A device entry describing which devices a specific IOMMU translates and |
| 111 | * which requestor ids they use. |
| 112 | */ |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 113 | struct ivhd_entry { |
| 114 | u8 type; |
| 115 | u16 devid; |
| 116 | u8 flags; |
| 117 | u32 ext; |
| 118 | } __attribute__((packed)); |
| 119 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 120 | /* |
| 121 | * An AMD IOMMU memory definition structure. It defines things like exclusion |
| 122 | * ranges for devices and regions that should be unity mapped. |
| 123 | */ |
Joerg Roedel | f6e2e6b | 2008-06-26 21:27:39 +0200 | [diff] [blame] | 124 | struct ivmd_header { |
| 125 | u8 type; |
| 126 | u8 flags; |
| 127 | u16 length; |
| 128 | u16 devid; |
| 129 | u16 aux; |
| 130 | u64 resv; |
| 131 | u64 range_start; |
| 132 | u64 range_length; |
| 133 | } __attribute__((packed)); |
| 134 | |
Joerg Roedel | fefda11 | 2009-05-20 12:21:42 +0200 | [diff] [blame] | 135 | bool amd_iommu_dump; |
Joerg Roedel | 05152a0 | 2012-06-15 16:53:51 +0200 | [diff] [blame] | 136 | bool amd_iommu_irq_remap __read_mostly; |
Joerg Roedel | fefda11 | 2009-05-20 12:21:42 +0200 | [diff] [blame] | 137 | |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 138 | static bool amd_iommu_detected; |
Joerg Roedel | a523572 | 2010-05-11 17:12:33 +0200 | [diff] [blame] | 139 | static bool __initdata amd_iommu_disabled; |
Joerg Roedel | c1cbebe | 2008-07-03 19:35:10 +0200 | [diff] [blame] | 140 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 141 | u16 amd_iommu_last_bdf; /* largest PCI device id we have |
| 142 | to handle */ |
Joerg Roedel | 2e22847 | 2008-07-11 17:14:31 +0200 | [diff] [blame] | 143 | LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 144 | we find in ACPI */ |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 145 | bool amd_iommu_unmap_flush; /* if true, flush on every unmap */ |
Joerg Roedel | 928abd2 | 2008-06-26 21:27:40 +0200 | [diff] [blame] | 146 | |
Joerg Roedel | 2e22847 | 2008-07-11 17:14:31 +0200 | [diff] [blame] | 147 | LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 148 | system */ |
| 149 | |
Joerg Roedel | bb52777 | 2009-11-20 14:31:51 +0100 | [diff] [blame] | 150 | /* Array to assign indices to IOMMUs*/ |
| 151 | struct amd_iommu *amd_iommus[MAX_IOMMUS]; |
| 152 | int amd_iommus_present; |
| 153 | |
Joerg Roedel | 318afd4 | 2009-11-23 18:32:38 +0100 | [diff] [blame] | 154 | /* IOMMUs have a non-present cache? */ |
| 155 | bool amd_iommu_np_cache __read_mostly; |
Joerg Roedel | 60f723b | 2011-04-05 12:50:24 +0200 | [diff] [blame] | 156 | bool amd_iommu_iotlb_sup __read_mostly = true; |
Joerg Roedel | 318afd4 | 2009-11-23 18:32:38 +0100 | [diff] [blame] | 157 | |
Suravee Suthikulpanit | a919a01 | 2014-03-05 18:54:18 -0600 | [diff] [blame] | 158 | u32 amd_iommu_max_pasid __read_mostly = ~0; |
Joerg Roedel | 62f71ab | 2011-11-10 14:41:57 +0100 | [diff] [blame] | 159 | |
Joerg Roedel | 400a28a | 2011-11-28 15:11:02 +0100 | [diff] [blame] | 160 | bool amd_iommu_v2_present __read_mostly; |
Joerg Roedel | 4160cd9 | 2015-08-13 11:31:48 +0200 | [diff] [blame] | 161 | static bool amd_iommu_pc_present __read_mostly; |
Joerg Roedel | 400a28a | 2011-11-28 15:11:02 +0100 | [diff] [blame] | 162 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 163 | bool amd_iommu_force_isolation __read_mostly; |
| 164 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 165 | /* |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 166 | * List of protection domains - used during resume |
| 167 | */ |
| 168 | LIST_HEAD(amd_iommu_pd_list); |
| 169 | spinlock_t amd_iommu_pd_lock; |
| 170 | |
| 171 | /* |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 172 | * Pointer to the device table which is shared by all AMD IOMMUs |
| 173 | * it is indexed by the PCI device id or the HT unit id and contains |
| 174 | * information about the domain the device belongs to as well as the |
| 175 | * page table root pointer. |
| 176 | */ |
Joerg Roedel | 928abd2 | 2008-06-26 21:27:40 +0200 | [diff] [blame] | 177 | struct dev_table_entry *amd_iommu_dev_table; |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * The alias table is a driver specific data structure which contains the |
| 181 | * mappings of the PCI device ids to the actual requestor ids on the IOMMU. |
| 182 | * More than one device can share the same requestor id. |
| 183 | */ |
Joerg Roedel | 928abd2 | 2008-06-26 21:27:40 +0200 | [diff] [blame] | 184 | u16 *amd_iommu_alias_table; |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * The rlookup table is used to find the IOMMU which is responsible |
| 188 | * for a specific device. It is also indexed by the PCI device id. |
| 189 | */ |
Joerg Roedel | 928abd2 | 2008-06-26 21:27:40 +0200 | [diff] [blame] | 190 | struct amd_iommu **amd_iommu_rlookup_table; |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 191 | |
| 192 | /* |
Joerg Roedel | 0ea2c42 | 2012-06-15 18:05:20 +0200 | [diff] [blame] | 193 | * This table is used to find the irq remapping table for a given device id |
| 194 | * quickly. |
| 195 | */ |
| 196 | struct irq_remap_table **irq_lookup_table; |
| 197 | |
| 198 | /* |
Frank Arnold | df805ab | 2012-08-27 19:21:04 +0200 | [diff] [blame] | 199 | * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 200 | * to know which ones are already in use. |
| 201 | */ |
Joerg Roedel | 928abd2 | 2008-06-26 21:27:40 +0200 | [diff] [blame] | 202 | unsigned long *amd_iommu_pd_alloc_bitmap; |
| 203 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 204 | static u32 dev_table_size; /* size of the device table */ |
| 205 | static u32 alias_table_size; /* size of the alias table */ |
| 206 | static u32 rlookup_table_size; /* size if the rlookup table */ |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 207 | |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 208 | enum iommu_init_state { |
| 209 | IOMMU_START_STATE, |
| 210 | IOMMU_IVRS_DETECTED, |
| 211 | IOMMU_ACPI_FINISHED, |
| 212 | IOMMU_ENABLED, |
| 213 | IOMMU_PCI_INIT, |
| 214 | IOMMU_INTERRUPTS_EN, |
| 215 | IOMMU_DMA_OPS, |
| 216 | IOMMU_INITIALIZED, |
| 217 | IOMMU_NOT_FOUND, |
| 218 | IOMMU_INIT_ERROR, |
| 219 | }; |
| 220 | |
Joerg Roedel | 235dacb | 2013-04-09 17:53:14 +0200 | [diff] [blame] | 221 | /* Early ioapic and hpet maps from kernel command line */ |
| 222 | #define EARLY_MAP_SIZE 4 |
| 223 | static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE]; |
| 224 | static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE]; |
| 225 | static int __initdata early_ioapic_map_size; |
| 226 | static int __initdata early_hpet_map_size; |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 227 | static bool __initdata cmdline_maps; |
Joerg Roedel | 235dacb | 2013-04-09 17:53:14 +0200 | [diff] [blame] | 228 | |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 229 | static enum iommu_init_state init_state = IOMMU_START_STATE; |
| 230 | |
Gerard Snitselaar | ae29514 | 2012-03-16 11:38:22 -0700 | [diff] [blame] | 231 | static int amd_iommu_enable_interrupts(void); |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 232 | static int __init iommu_go_to_state(enum iommu_init_state state); |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 233 | static void init_device_table_dma(void); |
Joerg Roedel | 3d9761e | 2012-03-15 16:39:21 +0100 | [diff] [blame] | 234 | |
Suravee Suthikulpanit | 38e45d0 | 2016-02-23 13:03:30 +0100 | [diff] [blame] | 235 | static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu, |
| 236 | u8 bank, u8 cntr, u8 fxn, |
| 237 | u64 *value, bool is_write); |
| 238 | |
Joerg Roedel | 208ec8c | 2008-07-11 17:14:24 +0200 | [diff] [blame] | 239 | static inline void update_last_devid(u16 devid) |
| 240 | { |
| 241 | if (devid > amd_iommu_last_bdf) |
| 242 | amd_iommu_last_bdf = devid; |
| 243 | } |
| 244 | |
Joerg Roedel | c571484 | 2008-07-11 17:14:25 +0200 | [diff] [blame] | 245 | static inline unsigned long tbl_size(int entry_size) |
| 246 | { |
| 247 | unsigned shift = PAGE_SHIFT + |
Neil Turton | 421f909 | 2009-05-14 14:00:35 +0100 | [diff] [blame] | 248 | get_order(((int)amd_iommu_last_bdf + 1) * entry_size); |
Joerg Roedel | c571484 | 2008-07-11 17:14:25 +0200 | [diff] [blame] | 249 | |
| 250 | return 1UL << shift; |
| 251 | } |
| 252 | |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 253 | /* Access to l1 and l2 indexed register spaces */ |
| 254 | |
| 255 | static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address) |
| 256 | { |
| 257 | u32 val; |
| 258 | |
| 259 | pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16)); |
| 260 | pci_read_config_dword(iommu->dev, 0xfc, &val); |
| 261 | return val; |
| 262 | } |
| 263 | |
| 264 | static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val) |
| 265 | { |
| 266 | pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31)); |
| 267 | pci_write_config_dword(iommu->dev, 0xfc, val); |
| 268 | pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16)); |
| 269 | } |
| 270 | |
| 271 | static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address) |
| 272 | { |
| 273 | u32 val; |
| 274 | |
| 275 | pci_write_config_dword(iommu->dev, 0xf0, address); |
| 276 | pci_read_config_dword(iommu->dev, 0xf4, &val); |
| 277 | return val; |
| 278 | } |
| 279 | |
| 280 | static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val) |
| 281 | { |
| 282 | pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8)); |
| 283 | pci_write_config_dword(iommu->dev, 0xf4, val); |
| 284 | } |
| 285 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 286 | /**************************************************************************** |
| 287 | * |
| 288 | * AMD IOMMU MMIO register space handling functions |
| 289 | * |
| 290 | * These functions are used to program the IOMMU device registers in |
| 291 | * MMIO space required for that driver. |
| 292 | * |
| 293 | ****************************************************************************/ |
| 294 | |
| 295 | /* |
| 296 | * This function set the exclusion range in the IOMMU. DMA accesses to the |
| 297 | * exclusion range are passed through untranslated |
| 298 | */ |
Joerg Roedel | 05f92db | 2009-05-12 09:52:46 +0200 | [diff] [blame] | 299 | static void iommu_set_exclusion_range(struct amd_iommu *iommu) |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 300 | { |
| 301 | u64 start = iommu->exclusion_start & PAGE_MASK; |
| 302 | u64 limit = (start + iommu->exclusion_length) & PAGE_MASK; |
| 303 | u64 entry; |
| 304 | |
| 305 | if (!iommu->exclusion_start) |
| 306 | return; |
| 307 | |
| 308 | entry = start | MMIO_EXCL_ENABLE_MASK; |
| 309 | memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET, |
| 310 | &entry, sizeof(entry)); |
| 311 | |
| 312 | entry = limit; |
| 313 | memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET, |
| 314 | &entry, sizeof(entry)); |
| 315 | } |
| 316 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 317 | /* Programs the physical address of the device table into the IOMMU hardware */ |
Jan Beulich | 6b7f000 | 2012-03-08 08:58:13 +0000 | [diff] [blame] | 318 | static void iommu_set_device_table(struct amd_iommu *iommu) |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 319 | { |
Andreas Herrmann | f609891 | 2008-10-16 16:27:36 +0200 | [diff] [blame] | 320 | u64 entry; |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 321 | |
| 322 | BUG_ON(iommu->mmio_base == NULL); |
| 323 | |
| 324 | entry = virt_to_phys(amd_iommu_dev_table); |
| 325 | entry |= (dev_table_size >> 12) - 1; |
| 326 | memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET, |
| 327 | &entry, sizeof(entry)); |
| 328 | } |
| 329 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 330 | /* Generic functions to enable/disable certain features of the IOMMU. */ |
Joerg Roedel | 05f92db | 2009-05-12 09:52:46 +0200 | [diff] [blame] | 331 | static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit) |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 332 | { |
| 333 | u32 ctrl; |
| 334 | |
| 335 | ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 336 | ctrl |= (1 << bit); |
| 337 | writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 338 | } |
| 339 | |
Joerg Roedel | ca020711 | 2009-10-28 18:02:26 +0100 | [diff] [blame] | 340 | static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit) |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 341 | { |
| 342 | u32 ctrl; |
| 343 | |
Joerg Roedel | 199d0d5 | 2008-09-17 16:45:59 +0200 | [diff] [blame] | 344 | ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 345 | ctrl &= ~(1 << bit); |
| 346 | writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 347 | } |
| 348 | |
Joerg Roedel | 1456e9d | 2011-12-22 14:51:53 +0100 | [diff] [blame] | 349 | static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout) |
| 350 | { |
| 351 | u32 ctrl; |
| 352 | |
| 353 | ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 354 | ctrl &= ~CTRL_INV_TO_MASK; |
| 355 | ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK; |
| 356 | writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET); |
| 357 | } |
| 358 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 359 | /* Function to enable the hardware */ |
Joerg Roedel | 05f92db | 2009-05-12 09:52:46 +0200 | [diff] [blame] | 360 | static void iommu_enable(struct amd_iommu *iommu) |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 361 | { |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 362 | iommu_feature_enable(iommu, CONTROL_IOMMU_EN); |
Joerg Roedel | b2026aa | 2008-06-26 21:27:44 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Joerg Roedel | 92ac432 | 2009-05-19 19:06:27 +0200 | [diff] [blame] | 365 | static void iommu_disable(struct amd_iommu *iommu) |
Joerg Roedel | 126c52b | 2008-09-09 16:47:35 +0200 | [diff] [blame] | 366 | { |
Chris Wright | a8c485b | 2009-06-15 15:53:45 +0200 | [diff] [blame] | 367 | /* Disable command buffer */ |
| 368 | iommu_feature_disable(iommu, CONTROL_CMDBUF_EN); |
| 369 | |
| 370 | /* Disable event logging and event interrupts */ |
| 371 | iommu_feature_disable(iommu, CONTROL_EVT_INT_EN); |
| 372 | iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN); |
| 373 | |
| 374 | /* Disable IOMMU hardware itself */ |
Joerg Roedel | 92ac432 | 2009-05-19 19:06:27 +0200 | [diff] [blame] | 375 | iommu_feature_disable(iommu, CONTROL_IOMMU_EN); |
Joerg Roedel | 126c52b | 2008-09-09 16:47:35 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 378 | /* |
| 379 | * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in |
| 380 | * the system has one. |
| 381 | */ |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 382 | static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end) |
Joerg Roedel | 6c56747 | 2008-06-26 21:27:43 +0200 | [diff] [blame] | 383 | { |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 384 | if (!request_mem_region(address, end, "amd_iommu")) { |
| 385 | pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n", |
| 386 | address, end); |
Joerg Roedel | e82752d | 2010-05-28 14:26:48 +0200 | [diff] [blame] | 387 | pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n"); |
Joerg Roedel | 6c56747 | 2008-06-26 21:27:43 +0200 | [diff] [blame] | 388 | return NULL; |
Joerg Roedel | e82752d | 2010-05-28 14:26:48 +0200 | [diff] [blame] | 389 | } |
Joerg Roedel | 6c56747 | 2008-06-26 21:27:43 +0200 | [diff] [blame] | 390 | |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 391 | return (u8 __iomem *)ioremap_nocache(address, end); |
Joerg Roedel | 6c56747 | 2008-06-26 21:27:43 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu) |
| 395 | { |
| 396 | if (iommu->mmio_base) |
| 397 | iounmap(iommu->mmio_base); |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 398 | release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end); |
Joerg Roedel | 6c56747 | 2008-06-26 21:27:43 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Suravee Suthikulpanit | ac7ccf6 | 2016-04-01 09:05:58 -0400 | [diff] [blame^] | 401 | static inline u32 get_ivhd_header_size(struct ivhd_header *h) |
| 402 | { |
| 403 | u32 size = 0; |
| 404 | |
| 405 | switch (h->type) { |
| 406 | case 0x10: |
| 407 | size = 24; |
| 408 | break; |
| 409 | case 0x11: |
| 410 | case 0x40: |
| 411 | size = 40; |
| 412 | break; |
| 413 | } |
| 414 | return size; |
| 415 | } |
| 416 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 417 | /**************************************************************************** |
| 418 | * |
| 419 | * The functions below belong to the first pass of AMD IOMMU ACPI table |
| 420 | * parsing. In this pass we try to find out the highest device id this |
| 421 | * code has to handle. Upon this information the size of the shared data |
| 422 | * structures is determined later. |
| 423 | * |
| 424 | ****************************************************************************/ |
| 425 | |
| 426 | /* |
Joerg Roedel | b514e55 | 2008-09-17 17:14:27 +0200 | [diff] [blame] | 427 | * This function calculates the length of a given IVHD entry |
| 428 | */ |
| 429 | static inline int ivhd_entry_length(u8 *ivhd) |
| 430 | { |
| 431 | return 0x04 << (*ivhd >> 6); |
| 432 | } |
| 433 | |
| 434 | /* |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 435 | * After reading the highest device id from the IOMMU PCI capability header |
| 436 | * this function looks if there is a higher device id defined in the ACPI table |
| 437 | */ |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 438 | static int __init find_last_devid_from_ivhd(struct ivhd_header *h) |
| 439 | { |
| 440 | u8 *p = (void *)h, *end = (void *)h; |
| 441 | struct ivhd_entry *dev; |
| 442 | |
Suravee Suthikulpanit | ac7ccf6 | 2016-04-01 09:05:58 -0400 | [diff] [blame^] | 443 | u32 ivhd_size = get_ivhd_header_size(h); |
| 444 | |
| 445 | if (!ivhd_size) { |
| 446 | pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type); |
| 447 | return -EINVAL; |
| 448 | } |
| 449 | |
| 450 | p += ivhd_size; |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 451 | end += h->length; |
| 452 | |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 453 | while (p < end) { |
| 454 | dev = (struct ivhd_entry *)p; |
| 455 | switch (dev->type) { |
Joerg Roedel | d125941 | 2015-10-20 17:33:43 +0200 | [diff] [blame] | 456 | case IVHD_DEV_ALL: |
| 457 | /* Use maximum BDF value for DEV_ALL */ |
| 458 | update_last_devid(0xffff); |
| 459 | break; |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 460 | case IVHD_DEV_SELECT: |
| 461 | case IVHD_DEV_RANGE_END: |
| 462 | case IVHD_DEV_ALIAS: |
| 463 | case IVHD_DEV_EXT_SELECT: |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 464 | /* all the above subfield types refer to device ids */ |
Joerg Roedel | 208ec8c | 2008-07-11 17:14:24 +0200 | [diff] [blame] | 465 | update_last_devid(dev->devid); |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 466 | break; |
| 467 | default: |
| 468 | break; |
| 469 | } |
Joerg Roedel | b514e55 | 2008-09-17 17:14:27 +0200 | [diff] [blame] | 470 | p += ivhd_entry_length(p); |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | WARN_ON(p != end); |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 478 | /* |
| 479 | * Iterate over all IVHD entries in the ACPI table and find the highest device |
| 480 | * id which we need to handle. This is the first of three functions which parse |
| 481 | * the ACPI table. So we check the checksum here. |
| 482 | */ |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 483 | static int __init find_last_devid_acpi(struct acpi_table_header *table) |
| 484 | { |
| 485 | int i; |
| 486 | u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table; |
| 487 | struct ivhd_header *h; |
| 488 | |
| 489 | /* |
| 490 | * Validate checksum here so we don't need to do it when |
| 491 | * we actually parse the table |
| 492 | */ |
| 493 | for (i = 0; i < table->length; ++i) |
| 494 | checksum += p[i]; |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 495 | if (checksum != 0) |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 496 | /* ACPI table corrupt */ |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 497 | return -ENODEV; |
Joerg Roedel | 3e8064b | 2008-06-26 21:27:41 +0200 | [diff] [blame] | 498 | |
| 499 | p += IVRS_HEADER_LENGTH; |
| 500 | |
| 501 | end += table->length; |
| 502 | while (p < end) { |
| 503 | h = (struct ivhd_header *)p; |
| 504 | switch (h->type) { |
| 505 | case ACPI_IVHD_TYPE: |
| 506 | find_last_devid_from_ivhd(h); |
| 507 | break; |
| 508 | default: |
| 509 | break; |
| 510 | } |
| 511 | p += h->length; |
| 512 | } |
| 513 | WARN_ON(p != end); |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 518 | /**************************************************************************** |
| 519 | * |
Frank Arnold | df805ab | 2012-08-27 19:21:04 +0200 | [diff] [blame] | 520 | * The following functions belong to the code path which parses the ACPI table |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 521 | * the second time. In this ACPI parsing iteration we allocate IOMMU specific |
| 522 | * data structures, initialize the device/alias/rlookup table and also |
| 523 | * basically initialize the hardware. |
| 524 | * |
| 525 | ****************************************************************************/ |
| 526 | |
| 527 | /* |
| 528 | * Allocates the command buffer. This buffer is per AMD IOMMU. We can |
| 529 | * write commands to that buffer later and the IOMMU will execute them |
| 530 | * asynchronously |
| 531 | */ |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 532 | static int __init alloc_command_buffer(struct amd_iommu *iommu) |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame] | 533 | { |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 534 | iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 535 | get_order(CMD_BUFFER_SIZE)); |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame] | 536 | |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 537 | return iommu->cmd_buf ? 0 : -ENOMEM; |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* |
Joerg Roedel | 93f1cc67 | 2009-09-03 14:50:20 +0200 | [diff] [blame] | 541 | * This function resets the command buffer if the IOMMU stopped fetching |
| 542 | * commands from it. |
| 543 | */ |
| 544 | void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu) |
| 545 | { |
| 546 | iommu_feature_disable(iommu, CONTROL_CMDBUF_EN); |
| 547 | |
| 548 | writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); |
| 549 | writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
| 550 | |
| 551 | iommu_feature_enable(iommu, CONTROL_CMDBUF_EN); |
| 552 | } |
| 553 | |
| 554 | /* |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 555 | * This function writes the command buffer address to the hardware and |
| 556 | * enables it. |
| 557 | */ |
| 558 | static void iommu_enable_command_buffer(struct amd_iommu *iommu) |
| 559 | { |
| 560 | u64 entry; |
| 561 | |
| 562 | BUG_ON(iommu->cmd_buf == NULL); |
| 563 | |
| 564 | entry = (u64)virt_to_phys(iommu->cmd_buf); |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame] | 565 | entry |= MMIO_CMD_SIZE_512; |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 566 | |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame] | 567 | memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET, |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 568 | &entry, sizeof(entry)); |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame] | 569 | |
Joerg Roedel | 93f1cc67 | 2009-09-03 14:50:20 +0200 | [diff] [blame] | 570 | amd_iommu_reset_cmd_buffer(iommu); |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static void __init free_command_buffer(struct amd_iommu *iommu) |
| 574 | { |
Joerg Roedel | deba4bc | 2015-10-20 17:33:41 +0200 | [diff] [blame] | 575 | free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE)); |
Joerg Roedel | b36ca91 | 2008-06-26 21:27:45 +0200 | [diff] [blame] | 576 | } |
| 577 | |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 578 | /* allocates the memory where the IOMMU will log its events to */ |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 579 | static int __init alloc_event_buffer(struct amd_iommu *iommu) |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 580 | { |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 581 | iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 582 | get_order(EVT_BUFFER_SIZE)); |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 583 | |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 584 | return iommu->evt_buf ? 0 : -ENOMEM; |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static void iommu_enable_event_buffer(struct amd_iommu *iommu) |
| 588 | { |
| 589 | u64 entry; |
| 590 | |
| 591 | BUG_ON(iommu->evt_buf == NULL); |
| 592 | |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 593 | entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK; |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 594 | |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 595 | memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET, |
| 596 | &entry, sizeof(entry)); |
| 597 | |
Joerg Roedel | 09067207 | 2009-06-15 16:06:48 +0200 | [diff] [blame] | 598 | /* set head and tail to zero manually */ |
| 599 | writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); |
| 600 | writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); |
| 601 | |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 602 | iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN); |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static void __init free_event_buffer(struct amd_iommu *iommu) |
| 606 | { |
| 607 | free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE)); |
| 608 | } |
| 609 | |
Joerg Roedel | 1a29ac0 | 2011-11-10 15:41:40 +0100 | [diff] [blame] | 610 | /* allocates the memory where the IOMMU will log its events to */ |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 611 | static int __init alloc_ppr_log(struct amd_iommu *iommu) |
Joerg Roedel | 1a29ac0 | 2011-11-10 15:41:40 +0100 | [diff] [blame] | 612 | { |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 613 | iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 614 | get_order(PPR_LOG_SIZE)); |
Joerg Roedel | 1a29ac0 | 2011-11-10 15:41:40 +0100 | [diff] [blame] | 615 | |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 616 | return iommu->ppr_log ? 0 : -ENOMEM; |
Joerg Roedel | 1a29ac0 | 2011-11-10 15:41:40 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static void iommu_enable_ppr_log(struct amd_iommu *iommu) |
| 620 | { |
| 621 | u64 entry; |
| 622 | |
| 623 | if (iommu->ppr_log == NULL) |
| 624 | return; |
| 625 | |
| 626 | entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512; |
| 627 | |
| 628 | memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET, |
| 629 | &entry, sizeof(entry)); |
| 630 | |
| 631 | /* set head and tail to zero manually */ |
| 632 | writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); |
| 633 | writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET); |
| 634 | |
| 635 | iommu_feature_enable(iommu, CONTROL_PPFLOG_EN); |
| 636 | iommu_feature_enable(iommu, CONTROL_PPR_EN); |
| 637 | } |
| 638 | |
| 639 | static void __init free_ppr_log(struct amd_iommu *iommu) |
| 640 | { |
| 641 | if (iommu->ppr_log == NULL) |
| 642 | return; |
| 643 | |
| 644 | free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE)); |
| 645 | } |
| 646 | |
Joerg Roedel | cbc33a9 | 2011-11-25 11:41:31 +0100 | [diff] [blame] | 647 | static void iommu_enable_gt(struct amd_iommu *iommu) |
| 648 | { |
| 649 | if (!iommu_feature(iommu, FEATURE_GT)) |
| 650 | return; |
| 651 | |
| 652 | iommu_feature_enable(iommu, CONTROL_GT_EN); |
| 653 | } |
| 654 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 655 | /* sets a specific bit in the device table entry. */ |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 656 | static void set_dev_entry_bit(u16 devid, u8 bit) |
| 657 | { |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 658 | int i = (bit >> 6) & 0x03; |
| 659 | int _bit = bit & 0x3f; |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 660 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 661 | amd_iommu_dev_table[devid].data[i] |= (1UL << _bit); |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 662 | } |
| 663 | |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 664 | static int get_dev_entry_bit(u16 devid, u8 bit) |
| 665 | { |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 666 | int i = (bit >> 6) & 0x03; |
| 667 | int _bit = bit & 0x3f; |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 668 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 669 | return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit; |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
| 673 | void amd_iommu_apply_erratum_63(u16 devid) |
| 674 | { |
| 675 | int sysmgt; |
| 676 | |
| 677 | sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) | |
| 678 | (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1); |
| 679 | |
| 680 | if (sysmgt == 0x01) |
| 681 | set_dev_entry_bit(devid, DEV_ENTRY_IW); |
| 682 | } |
| 683 | |
Joerg Roedel | 5ff4789 | 2008-07-14 20:11:18 +0200 | [diff] [blame] | 684 | /* Writes the specific IOMMU for a device into the rlookup table */ |
| 685 | static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid) |
| 686 | { |
| 687 | amd_iommu_rlookup_table[devid] = iommu; |
| 688 | } |
| 689 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 690 | /* |
| 691 | * This function takes the device specific flags read from the ACPI |
| 692 | * table and sets up the device table entry with that information |
| 693 | */ |
Joerg Roedel | 5ff4789 | 2008-07-14 20:11:18 +0200 | [diff] [blame] | 694 | static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu, |
| 695 | u16 devid, u32 flags, u32 ext_flags) |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 696 | { |
| 697 | if (flags & ACPI_DEVFLAG_INITPASS) |
| 698 | set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS); |
| 699 | if (flags & ACPI_DEVFLAG_EXTINT) |
| 700 | set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS); |
| 701 | if (flags & ACPI_DEVFLAG_NMI) |
| 702 | set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS); |
| 703 | if (flags & ACPI_DEVFLAG_SYSMGT1) |
| 704 | set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1); |
| 705 | if (flags & ACPI_DEVFLAG_SYSMGT2) |
| 706 | set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2); |
| 707 | if (flags & ACPI_DEVFLAG_LINT0) |
| 708 | set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS); |
| 709 | if (flags & ACPI_DEVFLAG_LINT1) |
| 710 | set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS); |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 711 | |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 712 | amd_iommu_apply_erratum_63(devid); |
| 713 | |
Joerg Roedel | 5ff4789 | 2008-07-14 20:11:18 +0200 | [diff] [blame] | 714 | set_iommu_for_device(iommu, devid); |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Joerg Roedel | c50e324 | 2014-09-09 15:59:37 +0200 | [diff] [blame] | 717 | static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line) |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 718 | { |
| 719 | struct devid_map *entry; |
| 720 | struct list_head *list; |
| 721 | |
Joerg Roedel | 31cff67 | 2013-04-09 16:53:58 +0200 | [diff] [blame] | 722 | if (type == IVHD_SPECIAL_IOAPIC) |
| 723 | list = &ioapic_map; |
| 724 | else if (type == IVHD_SPECIAL_HPET) |
| 725 | list = &hpet_map; |
| 726 | else |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 727 | return -EINVAL; |
| 728 | |
Joerg Roedel | 31cff67 | 2013-04-09 16:53:58 +0200 | [diff] [blame] | 729 | list_for_each_entry(entry, list, list) { |
| 730 | if (!(entry->id == id && entry->cmd_line)) |
| 731 | continue; |
| 732 | |
| 733 | pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n", |
| 734 | type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id); |
| 735 | |
Joerg Roedel | c50e324 | 2014-09-09 15:59:37 +0200 | [diff] [blame] | 736 | *devid = entry->devid; |
| 737 | |
Joerg Roedel | 31cff67 | 2013-04-09 16:53:58 +0200 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 741 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 742 | if (!entry) |
| 743 | return -ENOMEM; |
| 744 | |
Joerg Roedel | 31cff67 | 2013-04-09 16:53:58 +0200 | [diff] [blame] | 745 | entry->id = id; |
Joerg Roedel | c50e324 | 2014-09-09 15:59:37 +0200 | [diff] [blame] | 746 | entry->devid = *devid; |
Joerg Roedel | 31cff67 | 2013-04-09 16:53:58 +0200 | [diff] [blame] | 747 | entry->cmd_line = cmd_line; |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 748 | |
| 749 | list_add_tail(&entry->list, list); |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
Joerg Roedel | 235dacb | 2013-04-09 17:53:14 +0200 | [diff] [blame] | 754 | static int __init add_early_maps(void) |
| 755 | { |
| 756 | int i, ret; |
| 757 | |
| 758 | for (i = 0; i < early_ioapic_map_size; ++i) { |
| 759 | ret = add_special_device(IVHD_SPECIAL_IOAPIC, |
| 760 | early_ioapic_map[i].id, |
Joerg Roedel | c50e324 | 2014-09-09 15:59:37 +0200 | [diff] [blame] | 761 | &early_ioapic_map[i].devid, |
Joerg Roedel | 235dacb | 2013-04-09 17:53:14 +0200 | [diff] [blame] | 762 | early_ioapic_map[i].cmd_line); |
| 763 | if (ret) |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | for (i = 0; i < early_hpet_map_size; ++i) { |
| 768 | ret = add_special_device(IVHD_SPECIAL_HPET, |
| 769 | early_hpet_map[i].id, |
Joerg Roedel | c50e324 | 2014-09-09 15:59:37 +0200 | [diff] [blame] | 770 | &early_hpet_map[i].devid, |
Joerg Roedel | 235dacb | 2013-04-09 17:53:14 +0200 | [diff] [blame] | 771 | early_hpet_map[i].cmd_line); |
| 772 | if (ret) |
| 773 | return ret; |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | } |
| 778 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 779 | /* |
Frank Arnold | df805ab | 2012-08-27 19:21:04 +0200 | [diff] [blame] | 780 | * Reads the device exclusion range from ACPI and initializes the IOMMU with |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 781 | * it |
| 782 | */ |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 783 | static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m) |
| 784 | { |
| 785 | struct amd_iommu *iommu = amd_iommu_rlookup_table[devid]; |
| 786 | |
| 787 | if (!(m->flags & IVMD_FLAG_EXCL_RANGE)) |
| 788 | return; |
| 789 | |
| 790 | if (iommu) { |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 791 | /* |
| 792 | * We only can configure exclusion ranges per IOMMU, not |
| 793 | * per device. But we can enable the exclusion range per |
| 794 | * device. This is done here |
| 795 | */ |
Su Friendy | 2c16c9f | 2014-05-07 13:54:52 +0800 | [diff] [blame] | 796 | set_dev_entry_bit(devid, DEV_ENTRY_EX); |
Joerg Roedel | 3566b77 | 2008-06-26 21:27:46 +0200 | [diff] [blame] | 797 | iommu->exclusion_start = m->range_start; |
| 798 | iommu->exclusion_length = m->range_length; |
| 799 | } |
| 800 | } |
| 801 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 802 | /* |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 803 | * Takes a pointer to an AMD IOMMU entry in the ACPI table and |
| 804 | * initializes the hardware and our data structures with it. |
| 805 | */ |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 806 | static int __init init_iommu_from_acpi(struct amd_iommu *iommu, |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 807 | struct ivhd_header *h) |
| 808 | { |
| 809 | u8 *p = (u8 *)h; |
| 810 | u8 *end = p, flags = 0; |
Joerg Roedel | 0de66d5 | 2011-06-06 16:04:02 +0200 | [diff] [blame] | 811 | u16 devid = 0, devid_start = 0, devid_to = 0; |
| 812 | u32 dev_i, ext_flags = 0; |
Joerg Roedel | 58a3bee | 2008-07-11 17:14:30 +0200 | [diff] [blame] | 813 | bool alias = false; |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 814 | struct ivhd_entry *e; |
Suravee Suthikulpanit | ac7ccf6 | 2016-04-01 09:05:58 -0400 | [diff] [blame^] | 815 | u32 ivhd_size; |
Joerg Roedel | 235dacb | 2013-04-09 17:53:14 +0200 | [diff] [blame] | 816 | int ret; |
| 817 | |
| 818 | |
| 819 | ret = add_early_maps(); |
| 820 | if (ret) |
| 821 | return ret; |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 822 | |
| 823 | /* |
Joerg Roedel | e9bf519 | 2010-09-20 14:33:07 +0200 | [diff] [blame] | 824 | * First save the recommended feature enable bits from ACPI |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 825 | */ |
Joerg Roedel | e9bf519 | 2010-09-20 14:33:07 +0200 | [diff] [blame] | 826 | iommu->acpi_flags = h->flags; |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 827 | |
| 828 | /* |
| 829 | * Done. Now parse the device entries |
| 830 | */ |
Suravee Suthikulpanit | ac7ccf6 | 2016-04-01 09:05:58 -0400 | [diff] [blame^] | 831 | ivhd_size = get_ivhd_header_size(h); |
| 832 | if (!ivhd_size) { |
| 833 | pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type); |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | |
| 837 | p += ivhd_size; |
| 838 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 839 | end += h->length; |
| 840 | |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 841 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 842 | while (p < end) { |
| 843 | e = (struct ivhd_entry *)p; |
| 844 | switch (e->type) { |
| 845 | case IVHD_DEV_ALL: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 846 | |
Joerg Roedel | 226e889 | 2015-10-20 17:33:44 +0200 | [diff] [blame] | 847 | DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags); |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 848 | |
Joerg Roedel | 226e889 | 2015-10-20 17:33:44 +0200 | [diff] [blame] | 849 | for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i) |
| 850 | set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0); |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 851 | break; |
| 852 | case IVHD_DEV_SELECT: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 853 | |
| 854 | DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x " |
| 855 | "flags: %02x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 856 | PCI_BUS_NUM(e->devid), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 857 | PCI_SLOT(e->devid), |
| 858 | PCI_FUNC(e->devid), |
| 859 | e->flags); |
| 860 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 861 | devid = e->devid; |
Joerg Roedel | 5ff4789 | 2008-07-14 20:11:18 +0200 | [diff] [blame] | 862 | set_dev_entry_from_acpi(iommu, devid, e->flags, 0); |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 863 | break; |
| 864 | case IVHD_DEV_SELECT_RANGE_START: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 865 | |
| 866 | DUMP_printk(" DEV_SELECT_RANGE_START\t " |
| 867 | "devid: %02x:%02x.%x flags: %02x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 868 | PCI_BUS_NUM(e->devid), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 869 | PCI_SLOT(e->devid), |
| 870 | PCI_FUNC(e->devid), |
| 871 | e->flags); |
| 872 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 873 | devid_start = e->devid; |
| 874 | flags = e->flags; |
| 875 | ext_flags = 0; |
Joerg Roedel | 58a3bee | 2008-07-11 17:14:30 +0200 | [diff] [blame] | 876 | alias = false; |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 877 | break; |
| 878 | case IVHD_DEV_ALIAS: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 879 | |
| 880 | DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x " |
| 881 | "flags: %02x devid_to: %02x:%02x.%x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 882 | PCI_BUS_NUM(e->devid), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 883 | PCI_SLOT(e->devid), |
| 884 | PCI_FUNC(e->devid), |
| 885 | e->flags, |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 886 | PCI_BUS_NUM(e->ext >> 8), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 887 | PCI_SLOT(e->ext >> 8), |
| 888 | PCI_FUNC(e->ext >> 8)); |
| 889 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 890 | devid = e->devid; |
| 891 | devid_to = e->ext >> 8; |
Joerg Roedel | 7a6a3a0 | 2009-07-02 12:23:23 +0200 | [diff] [blame] | 892 | set_dev_entry_from_acpi(iommu, devid , e->flags, 0); |
Neil Turton | 7455aab | 2009-05-14 14:08:11 +0100 | [diff] [blame] | 893 | set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0); |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 894 | amd_iommu_alias_table[devid] = devid_to; |
| 895 | break; |
| 896 | case IVHD_DEV_ALIAS_RANGE: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 897 | |
| 898 | DUMP_printk(" DEV_ALIAS_RANGE\t\t " |
| 899 | "devid: %02x:%02x.%x flags: %02x " |
| 900 | "devid_to: %02x:%02x.%x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 901 | PCI_BUS_NUM(e->devid), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 902 | PCI_SLOT(e->devid), |
| 903 | PCI_FUNC(e->devid), |
| 904 | e->flags, |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 905 | PCI_BUS_NUM(e->ext >> 8), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 906 | PCI_SLOT(e->ext >> 8), |
| 907 | PCI_FUNC(e->ext >> 8)); |
| 908 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 909 | devid_start = e->devid; |
| 910 | flags = e->flags; |
| 911 | devid_to = e->ext >> 8; |
| 912 | ext_flags = 0; |
Joerg Roedel | 58a3bee | 2008-07-11 17:14:30 +0200 | [diff] [blame] | 913 | alias = true; |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 914 | break; |
| 915 | case IVHD_DEV_EXT_SELECT: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 916 | |
| 917 | DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x " |
| 918 | "flags: %02x ext: %08x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 919 | PCI_BUS_NUM(e->devid), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 920 | PCI_SLOT(e->devid), |
| 921 | PCI_FUNC(e->devid), |
| 922 | e->flags, e->ext); |
| 923 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 924 | devid = e->devid; |
Joerg Roedel | 5ff4789 | 2008-07-14 20:11:18 +0200 | [diff] [blame] | 925 | set_dev_entry_from_acpi(iommu, devid, e->flags, |
| 926 | e->ext); |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 927 | break; |
| 928 | case IVHD_DEV_EXT_SELECT_RANGE: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 929 | |
| 930 | DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: " |
| 931 | "%02x:%02x.%x flags: %02x ext: %08x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 932 | PCI_BUS_NUM(e->devid), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 933 | PCI_SLOT(e->devid), |
| 934 | PCI_FUNC(e->devid), |
| 935 | e->flags, e->ext); |
| 936 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 937 | devid_start = e->devid; |
| 938 | flags = e->flags; |
| 939 | ext_flags = e->ext; |
Joerg Roedel | 58a3bee | 2008-07-11 17:14:30 +0200 | [diff] [blame] | 940 | alias = false; |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 941 | break; |
| 942 | case IVHD_DEV_RANGE_END: |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 943 | |
| 944 | DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 945 | PCI_BUS_NUM(e->devid), |
Joerg Roedel | 42a698f | 2009-05-20 15:41:28 +0200 | [diff] [blame] | 946 | PCI_SLOT(e->devid), |
| 947 | PCI_FUNC(e->devid)); |
| 948 | |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 949 | devid = e->devid; |
| 950 | for (dev_i = devid_start; dev_i <= devid; ++dev_i) { |
Joerg Roedel | 7a6a3a0 | 2009-07-02 12:23:23 +0200 | [diff] [blame] | 951 | if (alias) { |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 952 | amd_iommu_alias_table[dev_i] = devid_to; |
Joerg Roedel | 7a6a3a0 | 2009-07-02 12:23:23 +0200 | [diff] [blame] | 953 | set_dev_entry_from_acpi(iommu, |
| 954 | devid_to, flags, ext_flags); |
| 955 | } |
| 956 | set_dev_entry_from_acpi(iommu, dev_i, |
| 957 | flags, ext_flags); |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 958 | } |
| 959 | break; |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 960 | case IVHD_DEV_SPECIAL: { |
| 961 | u8 handle, type; |
| 962 | const char *var; |
| 963 | u16 devid; |
| 964 | int ret; |
| 965 | |
| 966 | handle = e->ext & 0xff; |
| 967 | devid = (e->ext >> 8) & 0xffff; |
| 968 | type = (e->ext >> 24) & 0xff; |
| 969 | |
| 970 | if (type == IVHD_SPECIAL_IOAPIC) |
| 971 | var = "IOAPIC"; |
| 972 | else if (type == IVHD_SPECIAL_HPET) |
| 973 | var = "HPET"; |
| 974 | else |
| 975 | var = "UNKNOWN"; |
| 976 | |
| 977 | DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n", |
| 978 | var, (int)handle, |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 979 | PCI_BUS_NUM(devid), |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 980 | PCI_SLOT(devid), |
| 981 | PCI_FUNC(devid)); |
| 982 | |
Joerg Roedel | c50e324 | 2014-09-09 15:59:37 +0200 | [diff] [blame] | 983 | ret = add_special_device(type, handle, &devid, false); |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 984 | if (ret) |
| 985 | return ret; |
Joerg Roedel | c50e324 | 2014-09-09 15:59:37 +0200 | [diff] [blame] | 986 | |
| 987 | /* |
| 988 | * add_special_device might update the devid in case a |
| 989 | * command-line override is present. So call |
| 990 | * set_dev_entry_from_acpi after add_special_device. |
| 991 | */ |
| 992 | set_dev_entry_from_acpi(iommu, devid, e->flags, 0); |
| 993 | |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 994 | break; |
| 995 | } |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 996 | default: |
| 997 | break; |
| 998 | } |
| 999 | |
Joerg Roedel | b514e55 | 2008-09-17 17:14:27 +0200 | [diff] [blame] | 1000 | p += ivhd_entry_length(p); |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 1001 | } |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 1002 | |
| 1003 | return 0; |
Joerg Roedel | 5d0c8e4 | 2008-06-26 21:27:47 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1006 | static void __init free_iommu_one(struct amd_iommu *iommu) |
| 1007 | { |
| 1008 | free_command_buffer(iommu); |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 1009 | free_event_buffer(iommu); |
Joerg Roedel | 1a29ac0 | 2011-11-10 15:41:40 +0100 | [diff] [blame] | 1010 | free_ppr_log(iommu); |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1011 | iommu_unmap_mmio_space(iommu); |
| 1012 | } |
| 1013 | |
| 1014 | static void __init free_iommu_all(void) |
| 1015 | { |
| 1016 | struct amd_iommu *iommu, *next; |
| 1017 | |
Joerg Roedel | 3bd2217 | 2009-05-04 15:06:20 +0200 | [diff] [blame] | 1018 | for_each_iommu_safe(iommu, next) { |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1019 | list_del(&iommu->list); |
| 1020 | free_iommu_one(iommu); |
| 1021 | kfree(iommu); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1025 | /* |
Suravee Suthikulpanit | 318fe78 | 2013-01-24 13:17:53 -0600 | [diff] [blame] | 1026 | * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations) |
| 1027 | * Workaround: |
| 1028 | * BIOS should disable L2B micellaneous clock gating by setting |
| 1029 | * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b |
| 1030 | */ |
Nikola Pajkovsky | e2f1a3b | 2013-02-26 16:12:05 +0100 | [diff] [blame] | 1031 | static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu) |
Suravee Suthikulpanit | 318fe78 | 2013-01-24 13:17:53 -0600 | [diff] [blame] | 1032 | { |
| 1033 | u32 value; |
| 1034 | |
| 1035 | if ((boot_cpu_data.x86 != 0x15) || |
| 1036 | (boot_cpu_data.x86_model < 0x10) || |
| 1037 | (boot_cpu_data.x86_model > 0x1f)) |
| 1038 | return; |
| 1039 | |
| 1040 | pci_write_config_dword(iommu->dev, 0xf0, 0x90); |
| 1041 | pci_read_config_dword(iommu->dev, 0xf4, &value); |
| 1042 | |
| 1043 | if (value & BIT(2)) |
| 1044 | return; |
| 1045 | |
| 1046 | /* Select NB indirect register 0x90 and enable writing */ |
| 1047 | pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8)); |
| 1048 | |
| 1049 | pci_write_config_dword(iommu->dev, 0xf4, value | 0x4); |
| 1050 | pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n", |
| 1051 | dev_name(&iommu->dev->dev)); |
| 1052 | |
| 1053 | /* Clear the enable writing bit */ |
| 1054 | pci_write_config_dword(iommu->dev, 0xf0, 0x90); |
| 1055 | } |
| 1056 | |
| 1057 | /* |
Jay Cornwall | 358875f | 2016-02-10 15:48:01 -0600 | [diff] [blame] | 1058 | * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission) |
| 1059 | * Workaround: |
| 1060 | * BIOS should enable ATS write permission check by setting |
| 1061 | * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b |
| 1062 | */ |
| 1063 | static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu) |
| 1064 | { |
| 1065 | u32 value; |
| 1066 | |
| 1067 | if ((boot_cpu_data.x86 != 0x15) || |
| 1068 | (boot_cpu_data.x86_model < 0x30) || |
| 1069 | (boot_cpu_data.x86_model > 0x3f)) |
| 1070 | return; |
| 1071 | |
| 1072 | /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */ |
| 1073 | value = iommu_read_l2(iommu, 0x47); |
| 1074 | |
| 1075 | if (value & BIT(0)) |
| 1076 | return; |
| 1077 | |
| 1078 | /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */ |
| 1079 | iommu_write_l2(iommu, 0x47, value | BIT(0)); |
| 1080 | |
| 1081 | pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n", |
| 1082 | dev_name(&iommu->dev->dev)); |
| 1083 | } |
| 1084 | |
| 1085 | /* |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1086 | * This function clues the initialization function for one IOMMU |
| 1087 | * together and also allocates the command buffer and programs the |
| 1088 | * hardware. It does NOT enable the IOMMU. This is done afterwards. |
| 1089 | */ |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1090 | static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h) |
| 1091 | { |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 1092 | int ret; |
| 1093 | |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1094 | spin_lock_init(&iommu->lock); |
Joerg Roedel | bb52777 | 2009-11-20 14:31:51 +0100 | [diff] [blame] | 1095 | |
| 1096 | /* Add IOMMU to internal data structures */ |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1097 | list_add_tail(&iommu->list, &amd_iommu_list); |
Joerg Roedel | bb52777 | 2009-11-20 14:31:51 +0100 | [diff] [blame] | 1098 | iommu->index = amd_iommus_present++; |
| 1099 | |
| 1100 | if (unlikely(iommu->index >= MAX_IOMMUS)) { |
| 1101 | WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n"); |
| 1102 | return -ENOSYS; |
| 1103 | } |
| 1104 | |
| 1105 | /* Index is fine - add IOMMU to the array */ |
| 1106 | amd_iommus[iommu->index] = iommu; |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1107 | |
| 1108 | /* |
| 1109 | * Copy data from ACPI table entry to the iommu struct |
| 1110 | */ |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1111 | iommu->devid = h->devid; |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1112 | iommu->cap_ptr = h->cap_ptr; |
Joerg Roedel | ee893c2 | 2008-09-08 14:48:04 +0200 | [diff] [blame] | 1113 | iommu->pci_seg = h->pci_seg; |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1114 | iommu->mmio_phys = h->mmio_phys; |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 1115 | |
Suravee Suthikulpanit | 7d7d38a | 2016-04-01 09:05:57 -0400 | [diff] [blame] | 1116 | switch (h->type) { |
| 1117 | case 0x10: |
| 1118 | /* Check if IVHD EFR contains proper max banks/counters */ |
| 1119 | if ((h->efr_attr != 0) && |
| 1120 | ((h->efr_attr & (0xF << 13)) != 0) && |
| 1121 | ((h->efr_attr & (0x3F << 17)) != 0)) |
| 1122 | iommu->mmio_phys_end = MMIO_REG_END_OFFSET; |
| 1123 | else |
| 1124 | iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET; |
| 1125 | break; |
| 1126 | case 0x11: |
| 1127 | case 0x40: |
| 1128 | if (h->efr_reg & (1 << 9)) |
| 1129 | iommu->mmio_phys_end = MMIO_REG_END_OFFSET; |
| 1130 | else |
| 1131 | iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET; |
| 1132 | break; |
| 1133 | default: |
| 1134 | return -EINVAL; |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys, |
| 1138 | iommu->mmio_phys_end); |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1139 | if (!iommu->mmio_base) |
| 1140 | return -ENOMEM; |
| 1141 | |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 1142 | if (alloc_command_buffer(iommu)) |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1143 | return -ENOMEM; |
| 1144 | |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 1145 | if (alloc_event_buffer(iommu)) |
Joerg Roedel | 335503e | 2008-09-05 14:29:07 +0200 | [diff] [blame] | 1146 | return -ENOMEM; |
| 1147 | |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1148 | iommu->int_enabled = false; |
| 1149 | |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 1150 | ret = init_iommu_from_acpi(iommu, h); |
| 1151 | if (ret) |
| 1152 | return ret; |
Joerg Roedel | f6fec00 | 2012-06-21 16:51:25 +0200 | [diff] [blame] | 1153 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 1154 | ret = amd_iommu_create_irq_domain(iommu); |
| 1155 | if (ret) |
| 1156 | return ret; |
| 1157 | |
Joerg Roedel | f6fec00 | 2012-06-21 16:51:25 +0200 | [diff] [blame] | 1158 | /* |
| 1159 | * Make sure IOMMU is not considered to translate itself. The IVRS |
| 1160 | * table tells us so, but this is a lie! |
| 1161 | */ |
| 1162 | amd_iommu_rlookup_table[iommu->devid] = NULL; |
| 1163 | |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1164 | return 0; |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1167 | /* |
| 1168 | * Iterates over all IOMMU entries in the ACPI table, allocates the |
| 1169 | * IOMMU structure and initializes it with init_iommu_one() |
| 1170 | */ |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1171 | static int __init init_iommu_all(struct acpi_table_header *table) |
| 1172 | { |
| 1173 | u8 *p = (u8 *)table, *end = (u8 *)table; |
| 1174 | struct ivhd_header *h; |
| 1175 | struct amd_iommu *iommu; |
| 1176 | int ret; |
| 1177 | |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1178 | end += table->length; |
| 1179 | p += IVRS_HEADER_LENGTH; |
| 1180 | |
| 1181 | while (p < end) { |
| 1182 | h = (struct ivhd_header *)p; |
| 1183 | switch (*p) { |
| 1184 | case ACPI_IVHD_TYPE: |
Joerg Roedel | 9c72041 | 2009-05-20 13:53:57 +0200 | [diff] [blame] | 1185 | |
Joerg Roedel | ae908c2 | 2009-09-01 16:52:16 +0200 | [diff] [blame] | 1186 | DUMP_printk("device: %02x:%02x.%01x cap: %04x " |
Joerg Roedel | 9c72041 | 2009-05-20 13:53:57 +0200 | [diff] [blame] | 1187 | "seg: %d flags: %01x info %04x\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 1188 | PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid), |
Joerg Roedel | 9c72041 | 2009-05-20 13:53:57 +0200 | [diff] [blame] | 1189 | PCI_FUNC(h->devid), h->cap_ptr, |
| 1190 | h->pci_seg, h->flags, h->info); |
| 1191 | DUMP_printk(" mmio-addr: %016llx\n", |
| 1192 | h->mmio_phys); |
| 1193 | |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1194 | iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL); |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 1195 | if (iommu == NULL) |
| 1196 | return -ENOMEM; |
Joerg Roedel | 3551a70 | 2010-03-01 13:52:19 +0100 | [diff] [blame] | 1197 | |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1198 | ret = init_iommu_one(iommu, h); |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 1199 | if (ret) |
| 1200 | return ret; |
Joerg Roedel | e47d402 | 2008-06-26 21:27:48 +0200 | [diff] [blame] | 1201 | break; |
| 1202 | default: |
| 1203 | break; |
| 1204 | } |
| 1205 | p += h->length; |
| 1206 | |
| 1207 | } |
| 1208 | WARN_ON(p != end); |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 1213 | |
| 1214 | static void init_iommu_perf_ctr(struct amd_iommu *iommu) |
| 1215 | { |
| 1216 | u64 val = 0xabcd, val2 = 0; |
| 1217 | |
| 1218 | if (!iommu_feature(iommu, FEATURE_PC)) |
| 1219 | return; |
| 1220 | |
| 1221 | amd_iommu_pc_present = true; |
| 1222 | |
| 1223 | /* Check if the performance counters can be written to */ |
Suravee Suthikulpanit | 38e45d0 | 2016-02-23 13:03:30 +0100 | [diff] [blame] | 1224 | if ((0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val, true)) || |
| 1225 | (0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val2, false)) || |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 1226 | (val != val2)) { |
| 1227 | pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n"); |
| 1228 | amd_iommu_pc_present = false; |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | pr_info("AMD-Vi: IOMMU performance counters supported\n"); |
| 1233 | |
| 1234 | val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET); |
| 1235 | iommu->max_banks = (u8) ((val >> 12) & 0x3f); |
| 1236 | iommu->max_counters = (u8) ((val >> 7) & 0xf); |
| 1237 | } |
| 1238 | |
Alex Williamson | 066f2e9 | 2014-06-12 16:12:37 -0600 | [diff] [blame] | 1239 | static ssize_t amd_iommu_show_cap(struct device *dev, |
| 1240 | struct device_attribute *attr, |
| 1241 | char *buf) |
| 1242 | { |
| 1243 | struct amd_iommu *iommu = dev_get_drvdata(dev); |
| 1244 | return sprintf(buf, "%x\n", iommu->cap); |
| 1245 | } |
| 1246 | static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL); |
| 1247 | |
| 1248 | static ssize_t amd_iommu_show_features(struct device *dev, |
| 1249 | struct device_attribute *attr, |
| 1250 | char *buf) |
| 1251 | { |
| 1252 | struct amd_iommu *iommu = dev_get_drvdata(dev); |
| 1253 | return sprintf(buf, "%llx\n", iommu->features); |
| 1254 | } |
| 1255 | static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL); |
| 1256 | |
| 1257 | static struct attribute *amd_iommu_attrs[] = { |
| 1258 | &dev_attr_cap.attr, |
| 1259 | &dev_attr_features.attr, |
| 1260 | NULL, |
| 1261 | }; |
| 1262 | |
| 1263 | static struct attribute_group amd_iommu_group = { |
| 1264 | .name = "amd-iommu", |
| 1265 | .attrs = amd_iommu_attrs, |
| 1266 | }; |
| 1267 | |
| 1268 | static const struct attribute_group *amd_iommu_groups[] = { |
| 1269 | &amd_iommu_group, |
| 1270 | NULL, |
| 1271 | }; |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 1272 | |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1273 | static int iommu_init_pci(struct amd_iommu *iommu) |
| 1274 | { |
| 1275 | int cap_ptr = iommu->cap_ptr; |
| 1276 | u32 range, misc, low, high; |
| 1277 | |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 1278 | iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid), |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1279 | iommu->devid & 0xff); |
| 1280 | if (!iommu->dev) |
| 1281 | return -ENODEV; |
| 1282 | |
Jiang Liu | cbbc00b | 2015-10-09 22:07:31 +0800 | [diff] [blame] | 1283 | /* Prevent binding other PCI device drivers to IOMMU devices */ |
| 1284 | iommu->dev->match_driver = false; |
| 1285 | |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1286 | pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET, |
| 1287 | &iommu->cap); |
| 1288 | pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET, |
| 1289 | &range); |
| 1290 | pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET, |
| 1291 | &misc); |
| 1292 | |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1293 | if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB))) |
| 1294 | amd_iommu_iotlb_sup = false; |
| 1295 | |
| 1296 | /* read extended feature bits */ |
| 1297 | low = readl(iommu->mmio_base + MMIO_EXT_FEATURES); |
| 1298 | high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4); |
| 1299 | |
| 1300 | iommu->features = ((u64)high << 32) | low; |
| 1301 | |
| 1302 | if (iommu_feature(iommu, FEATURE_GT)) { |
| 1303 | int glxval; |
Suravee Suthikulpanit | a919a01 | 2014-03-05 18:54:18 -0600 | [diff] [blame] | 1304 | u32 max_pasid; |
| 1305 | u64 pasmax; |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1306 | |
Suravee Suthikulpanit | a919a01 | 2014-03-05 18:54:18 -0600 | [diff] [blame] | 1307 | pasmax = iommu->features & FEATURE_PASID_MASK; |
| 1308 | pasmax >>= FEATURE_PASID_SHIFT; |
| 1309 | max_pasid = (1 << (pasmax + 1)) - 1; |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1310 | |
Suravee Suthikulpanit | a919a01 | 2014-03-05 18:54:18 -0600 | [diff] [blame] | 1311 | amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid); |
| 1312 | |
| 1313 | BUG_ON(amd_iommu_max_pasid & ~PASID_MASK); |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1314 | |
| 1315 | glxval = iommu->features & FEATURE_GLXVAL_MASK; |
| 1316 | glxval >>= FEATURE_GLXVAL_SHIFT; |
| 1317 | |
| 1318 | if (amd_iommu_max_glx_val == -1) |
| 1319 | amd_iommu_max_glx_val = glxval; |
| 1320 | else |
| 1321 | amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval); |
| 1322 | } |
| 1323 | |
| 1324 | if (iommu_feature(iommu, FEATURE_GT) && |
| 1325 | iommu_feature(iommu, FEATURE_PPR)) { |
| 1326 | iommu->is_iommu_v2 = true; |
| 1327 | amd_iommu_v2_present = true; |
| 1328 | } |
| 1329 | |
Joerg Roedel | f2c2db5 | 2015-10-20 17:33:42 +0200 | [diff] [blame] | 1330 | if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu)) |
| 1331 | return -ENOMEM; |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1332 | |
| 1333 | if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) |
| 1334 | amd_iommu_np_cache = true; |
| 1335 | |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 1336 | init_iommu_perf_ctr(iommu); |
| 1337 | |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1338 | if (is_rd890_iommu(iommu->dev)) { |
| 1339 | int i, j; |
| 1340 | |
| 1341 | iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number, |
| 1342 | PCI_DEVFN(0, 0)); |
| 1343 | |
| 1344 | /* |
| 1345 | * Some rd890 systems may not be fully reconfigured by the |
| 1346 | * BIOS, so it's necessary for us to store this information so |
| 1347 | * it can be reprogrammed on resume |
| 1348 | */ |
| 1349 | pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4, |
| 1350 | &iommu->stored_addr_lo); |
| 1351 | pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8, |
| 1352 | &iommu->stored_addr_hi); |
| 1353 | |
| 1354 | /* Low bit locks writes to configuration space */ |
| 1355 | iommu->stored_addr_lo &= ~1; |
| 1356 | |
| 1357 | for (i = 0; i < 6; i++) |
| 1358 | for (j = 0; j < 0x12; j++) |
| 1359 | iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j); |
| 1360 | |
| 1361 | for (i = 0; i < 0x83; i++) |
| 1362 | iommu->stored_l2[i] = iommu_read_l2(iommu, i); |
| 1363 | } |
| 1364 | |
Suravee Suthikulpanit | 318fe78 | 2013-01-24 13:17:53 -0600 | [diff] [blame] | 1365 | amd_iommu_erratum_746_workaround(iommu); |
Jay Cornwall | 358875f | 2016-02-10 15:48:01 -0600 | [diff] [blame] | 1366 | amd_iommu_ats_write_check_workaround(iommu); |
Suravee Suthikulpanit | 318fe78 | 2013-01-24 13:17:53 -0600 | [diff] [blame] | 1367 | |
Alex Williamson | 066f2e9 | 2014-06-12 16:12:37 -0600 | [diff] [blame] | 1368 | iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu, |
| 1369 | amd_iommu_groups, "ivhd%d", |
| 1370 | iommu->index); |
| 1371 | |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1372 | return pci_enable_device(iommu->dev); |
| 1373 | } |
| 1374 | |
Joerg Roedel | 4d121c3 | 2012-06-14 12:21:55 +0200 | [diff] [blame] | 1375 | static void print_iommu_info(void) |
| 1376 | { |
| 1377 | static const char * const feat_str[] = { |
| 1378 | "PreF", "PPR", "X2APIC", "NX", "GT", "[5]", |
| 1379 | "IA", "GA", "HE", "PC" |
| 1380 | }; |
| 1381 | struct amd_iommu *iommu; |
| 1382 | |
| 1383 | for_each_iommu(iommu) { |
| 1384 | int i; |
| 1385 | |
| 1386 | pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n", |
| 1387 | dev_name(&iommu->dev->dev), iommu->cap_ptr); |
| 1388 | |
| 1389 | if (iommu->cap & (1 << IOMMU_CAP_EFR)) { |
| 1390 | pr_info("AMD-Vi: Extended features: "); |
Joerg Roedel | 2bd5ed0 | 2012-08-10 11:34:08 +0200 | [diff] [blame] | 1391 | for (i = 0; i < ARRAY_SIZE(feat_str); ++i) { |
Joerg Roedel | 4d121c3 | 2012-06-14 12:21:55 +0200 | [diff] [blame] | 1392 | if (iommu_feature(iommu, (1ULL << i))) |
| 1393 | pr_cont(" %s", feat_str[i]); |
| 1394 | } |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 1395 | pr_cont("\n"); |
Borislav Petkov | 500c25e | 2012-09-28 16:22:26 +0200 | [diff] [blame] | 1396 | } |
Joerg Roedel | 4d121c3 | 2012-06-14 12:21:55 +0200 | [diff] [blame] | 1397 | } |
Joerg Roedel | ebe60bb | 2012-07-02 18:36:03 +0200 | [diff] [blame] | 1398 | if (irq_remapping_enabled) |
| 1399 | pr_info("AMD-Vi: Interrupt remapping enabled\n"); |
Joerg Roedel | 4d121c3 | 2012-06-14 12:21:55 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 1402 | static int __init amd_iommu_init_pci(void) |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1403 | { |
| 1404 | struct amd_iommu *iommu; |
| 1405 | int ret = 0; |
| 1406 | |
| 1407 | for_each_iommu(iommu) { |
| 1408 | ret = iommu_init_pci(iommu); |
| 1409 | if (ret) |
| 1410 | break; |
| 1411 | } |
| 1412 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 1413 | init_device_table_dma(); |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1414 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 1415 | for_each_iommu(iommu) |
| 1416 | iommu_flush_all_caches(iommu); |
| 1417 | |
Joerg Roedel | 3a18404c | 2015-05-28 18:41:45 +0200 | [diff] [blame] | 1418 | ret = amd_iommu_init_api(); |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1419 | |
Joerg Roedel | 3a18404c | 2015-05-28 18:41:45 +0200 | [diff] [blame] | 1420 | if (!ret) |
| 1421 | print_iommu_info(); |
Joerg Roedel | 4d121c3 | 2012-06-14 12:21:55 +0200 | [diff] [blame] | 1422 | |
Joerg Roedel | 23c742d | 2012-06-12 11:47:34 +0200 | [diff] [blame] | 1423 | return ret; |
| 1424 | } |
| 1425 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1426 | /**************************************************************************** |
| 1427 | * |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1428 | * The following functions initialize the MSI interrupts for all IOMMUs |
Frank Arnold | df805ab | 2012-08-27 19:21:04 +0200 | [diff] [blame] | 1429 | * in the system. It's a bit challenging because there could be multiple |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1430 | * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per |
| 1431 | * pci_dev. |
| 1432 | * |
| 1433 | ****************************************************************************/ |
| 1434 | |
Joerg Roedel | 9f800de | 2009-11-23 12:45:25 +0100 | [diff] [blame] | 1435 | static int iommu_setup_msi(struct amd_iommu *iommu) |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1436 | { |
| 1437 | int r; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1438 | |
Joerg Roedel | 9ddd592 | 2012-03-15 16:29:47 +0100 | [diff] [blame] | 1439 | r = pci_enable_msi(iommu->dev); |
| 1440 | if (r) |
| 1441 | return r; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1442 | |
Joerg Roedel | 72fe00f | 2011-05-10 10:50:42 +0200 | [diff] [blame] | 1443 | r = request_threaded_irq(iommu->dev->irq, |
| 1444 | amd_iommu_int_handler, |
| 1445 | amd_iommu_int_thread, |
| 1446 | 0, "AMD-Vi", |
Suravee Suthikulpanit | 3f398bc | 2013-04-22 16:32:34 -0500 | [diff] [blame] | 1447 | iommu); |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1448 | |
| 1449 | if (r) { |
| 1450 | pci_disable_msi(iommu->dev); |
Joerg Roedel | 9ddd592 | 2012-03-15 16:29:47 +0100 | [diff] [blame] | 1451 | return r; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
Joerg Roedel | fab6afa | 2009-05-04 18:46:34 +0200 | [diff] [blame] | 1454 | iommu->int_enabled = true; |
Joerg Roedel | 1a29ac0 | 2011-11-10 15:41:40 +0100 | [diff] [blame] | 1455 | |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1456 | return 0; |
| 1457 | } |
| 1458 | |
Joerg Roedel | 05f92db | 2009-05-12 09:52:46 +0200 | [diff] [blame] | 1459 | static int iommu_init_msi(struct amd_iommu *iommu) |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1460 | { |
Joerg Roedel | 9ddd592 | 2012-03-15 16:29:47 +0100 | [diff] [blame] | 1461 | int ret; |
| 1462 | |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1463 | if (iommu->int_enabled) |
Joerg Roedel | 9ddd592 | 2012-03-15 16:29:47 +0100 | [diff] [blame] | 1464 | goto enable_faults; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1465 | |
Yijing Wang | 82fcfc6 | 2013-08-08 21:12:36 +0800 | [diff] [blame] | 1466 | if (iommu->dev->msi_cap) |
Joerg Roedel | 9ddd592 | 2012-03-15 16:29:47 +0100 | [diff] [blame] | 1467 | ret = iommu_setup_msi(iommu); |
| 1468 | else |
| 1469 | ret = -ENODEV; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1470 | |
Joerg Roedel | 9ddd592 | 2012-03-15 16:29:47 +0100 | [diff] [blame] | 1471 | if (ret) |
| 1472 | return ret; |
| 1473 | |
| 1474 | enable_faults: |
| 1475 | iommu_feature_enable(iommu, CONTROL_EVT_INT_EN); |
| 1476 | |
| 1477 | if (iommu->ppr_log != NULL) |
| 1478 | iommu_feature_enable(iommu, CONTROL_PPFINT_EN); |
| 1479 | |
| 1480 | return 0; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | /**************************************************************************** |
| 1484 | * |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1485 | * The next functions belong to the third pass of parsing the ACPI |
| 1486 | * table. In this last pass the memory mapping requirements are |
Frank Arnold | df805ab | 2012-08-27 19:21:04 +0200 | [diff] [blame] | 1487 | * gathered (like exclusion and unity mapping ranges). |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1488 | * |
| 1489 | ****************************************************************************/ |
| 1490 | |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1491 | static void __init free_unity_maps(void) |
| 1492 | { |
| 1493 | struct unity_map_entry *entry, *next; |
| 1494 | |
| 1495 | list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) { |
| 1496 | list_del(&entry->list); |
| 1497 | kfree(entry); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1501 | /* called when we find an exclusion range definition in ACPI */ |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1502 | static int __init init_exclusion_range(struct ivmd_header *m) |
| 1503 | { |
| 1504 | int i; |
| 1505 | |
| 1506 | switch (m->type) { |
| 1507 | case ACPI_IVMD_TYPE: |
| 1508 | set_device_exclusion_range(m->devid, m); |
| 1509 | break; |
| 1510 | case ACPI_IVMD_TYPE_ALL: |
Joerg Roedel | 3a61ec3 | 2008-07-25 13:07:50 +0200 | [diff] [blame] | 1511 | for (i = 0; i <= amd_iommu_last_bdf; ++i) |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1512 | set_device_exclusion_range(i, m); |
| 1513 | break; |
| 1514 | case ACPI_IVMD_TYPE_RANGE: |
| 1515 | for (i = m->devid; i <= m->aux; ++i) |
| 1516 | set_device_exclusion_range(i, m); |
| 1517 | break; |
| 1518 | default: |
| 1519 | break; |
| 1520 | } |
| 1521 | |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1525 | /* called for unity map ACPI definition */ |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1526 | static int __init init_unity_map_range(struct ivmd_header *m) |
| 1527 | { |
Joerg Roedel | 98f1ad2 | 2012-07-06 13:28:37 +0200 | [diff] [blame] | 1528 | struct unity_map_entry *e = NULL; |
Joerg Roedel | 02acc43 | 2009-05-20 16:24:21 +0200 | [diff] [blame] | 1529 | char *s; |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1530 | |
| 1531 | e = kzalloc(sizeof(*e), GFP_KERNEL); |
| 1532 | if (e == NULL) |
| 1533 | return -ENOMEM; |
| 1534 | |
| 1535 | switch (m->type) { |
| 1536 | default: |
Joerg Roedel | 0bc252f | 2009-05-22 12:48:05 +0200 | [diff] [blame] | 1537 | kfree(e); |
| 1538 | return 0; |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1539 | case ACPI_IVMD_TYPE: |
Joerg Roedel | 02acc43 | 2009-05-20 16:24:21 +0200 | [diff] [blame] | 1540 | s = "IVMD_TYPEi\t\t\t"; |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1541 | e->devid_start = e->devid_end = m->devid; |
| 1542 | break; |
| 1543 | case ACPI_IVMD_TYPE_ALL: |
Joerg Roedel | 02acc43 | 2009-05-20 16:24:21 +0200 | [diff] [blame] | 1544 | s = "IVMD_TYPE_ALL\t\t"; |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1545 | e->devid_start = 0; |
| 1546 | e->devid_end = amd_iommu_last_bdf; |
| 1547 | break; |
| 1548 | case ACPI_IVMD_TYPE_RANGE: |
Joerg Roedel | 02acc43 | 2009-05-20 16:24:21 +0200 | [diff] [blame] | 1549 | s = "IVMD_TYPE_RANGE\t\t"; |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1550 | e->devid_start = m->devid; |
| 1551 | e->devid_end = m->aux; |
| 1552 | break; |
| 1553 | } |
| 1554 | e->address_start = PAGE_ALIGN(m->range_start); |
| 1555 | e->address_end = e->address_start + PAGE_ALIGN(m->range_length); |
| 1556 | e->prot = m->flags >> 1; |
| 1557 | |
Joerg Roedel | 02acc43 | 2009-05-20 16:24:21 +0200 | [diff] [blame] | 1558 | DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x" |
| 1559 | " range_start: %016llx range_end: %016llx flags: %x\n", s, |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 1560 | PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start), |
| 1561 | PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end), |
Joerg Roedel | 02acc43 | 2009-05-20 16:24:21 +0200 | [diff] [blame] | 1562 | PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end), |
| 1563 | e->address_start, e->address_end, m->flags); |
| 1564 | |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1565 | list_add_tail(&e->list, &amd_iommu_unity_map); |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1570 | /* iterates over all memory definitions we find in the ACPI table */ |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1571 | static int __init init_memory_definitions(struct acpi_table_header *table) |
| 1572 | { |
| 1573 | u8 *p = (u8 *)table, *end = (u8 *)table; |
| 1574 | struct ivmd_header *m; |
| 1575 | |
Joerg Roedel | be2a022 | 2008-06-26 21:27:49 +0200 | [diff] [blame] | 1576 | end += table->length; |
| 1577 | p += IVRS_HEADER_LENGTH; |
| 1578 | |
| 1579 | while (p < end) { |
| 1580 | m = (struct ivmd_header *)p; |
| 1581 | if (m->flags & IVMD_FLAG_EXCL_RANGE) |
| 1582 | init_exclusion_range(m); |
| 1583 | else if (m->flags & IVMD_FLAG_UNITY_MAP) |
| 1584 | init_unity_map_range(m); |
| 1585 | |
| 1586 | p += m->length; |
| 1587 | } |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1592 | /* |
Joerg Roedel | 9f5f5fb | 2008-08-14 19:55:16 +0200 | [diff] [blame] | 1593 | * Init the device table to not allow DMA access for devices and |
| 1594 | * suppress all page faults |
| 1595 | */ |
Joerg Roedel | 33f28c5 | 2012-06-15 18:03:31 +0200 | [diff] [blame] | 1596 | static void init_device_table_dma(void) |
Joerg Roedel | 9f5f5fb | 2008-08-14 19:55:16 +0200 | [diff] [blame] | 1597 | { |
Joerg Roedel | 0de66d5 | 2011-06-06 16:04:02 +0200 | [diff] [blame] | 1598 | u32 devid; |
Joerg Roedel | 9f5f5fb | 2008-08-14 19:55:16 +0200 | [diff] [blame] | 1599 | |
| 1600 | for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) { |
| 1601 | set_dev_entry_bit(devid, DEV_ENTRY_VALID); |
| 1602 | set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION); |
Joerg Roedel | 9f5f5fb | 2008-08-14 19:55:16 +0200 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | |
Joerg Roedel | d04e0ba | 2012-07-02 16:02:20 +0200 | [diff] [blame] | 1606 | static void __init uninit_device_table_dma(void) |
| 1607 | { |
| 1608 | u32 devid; |
| 1609 | |
| 1610 | for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) { |
| 1611 | amd_iommu_dev_table[devid].data[0] = 0ULL; |
| 1612 | amd_iommu_dev_table[devid].data[1] = 0ULL; |
| 1613 | } |
| 1614 | } |
| 1615 | |
Joerg Roedel | 33f28c5 | 2012-06-15 18:03:31 +0200 | [diff] [blame] | 1616 | static void init_device_table(void) |
| 1617 | { |
| 1618 | u32 devid; |
| 1619 | |
| 1620 | if (!amd_iommu_irq_remap) |
| 1621 | return; |
| 1622 | |
| 1623 | for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) |
| 1624 | set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN); |
| 1625 | } |
| 1626 | |
Joerg Roedel | e9bf519 | 2010-09-20 14:33:07 +0200 | [diff] [blame] | 1627 | static void iommu_init_flags(struct amd_iommu *iommu) |
| 1628 | { |
| 1629 | iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ? |
| 1630 | iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) : |
| 1631 | iommu_feature_disable(iommu, CONTROL_HT_TUN_EN); |
| 1632 | |
| 1633 | iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ? |
| 1634 | iommu_feature_enable(iommu, CONTROL_PASSPW_EN) : |
| 1635 | iommu_feature_disable(iommu, CONTROL_PASSPW_EN); |
| 1636 | |
| 1637 | iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ? |
| 1638 | iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) : |
| 1639 | iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN); |
| 1640 | |
| 1641 | iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ? |
| 1642 | iommu_feature_enable(iommu, CONTROL_ISOC_EN) : |
| 1643 | iommu_feature_disable(iommu, CONTROL_ISOC_EN); |
| 1644 | |
| 1645 | /* |
| 1646 | * make IOMMU memory accesses cache coherent |
| 1647 | */ |
| 1648 | iommu_feature_enable(iommu, CONTROL_COHERENT_EN); |
Joerg Roedel | 1456e9d | 2011-12-22 14:51:53 +0100 | [diff] [blame] | 1649 | |
| 1650 | /* Set IOTLB invalidation timeout to 1s */ |
| 1651 | iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S); |
Joerg Roedel | e9bf519 | 2010-09-20 14:33:07 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 1654 | static void iommu_apply_resume_quirks(struct amd_iommu *iommu) |
Joerg Roedel | 4c894f4 | 2010-09-23 15:15:19 +0200 | [diff] [blame] | 1655 | { |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 1656 | int i, j; |
| 1657 | u32 ioc_feature_control; |
Joerg Roedel | c1bf94e | 2012-05-31 17:38:11 +0200 | [diff] [blame] | 1658 | struct pci_dev *pdev = iommu->root_pdev; |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 1659 | |
| 1660 | /* RD890 BIOSes may not have completely reconfigured the iommu */ |
Joerg Roedel | c1bf94e | 2012-05-31 17:38:11 +0200 | [diff] [blame] | 1661 | if (!is_rd890_iommu(iommu->dev) || !pdev) |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 1662 | return; |
| 1663 | |
| 1664 | /* |
| 1665 | * First, we need to ensure that the iommu is enabled. This is |
| 1666 | * controlled by a register in the northbridge |
| 1667 | */ |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 1668 | |
| 1669 | /* Select Northbridge indirect register 0x75 and enable writing */ |
| 1670 | pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7)); |
| 1671 | pci_read_config_dword(pdev, 0x64, &ioc_feature_control); |
| 1672 | |
| 1673 | /* Enable the iommu */ |
| 1674 | if (!(ioc_feature_control & 0x1)) |
| 1675 | pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1); |
| 1676 | |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 1677 | /* Restore the iommu BAR */ |
| 1678 | pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4, |
| 1679 | iommu->stored_addr_lo); |
| 1680 | pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8, |
| 1681 | iommu->stored_addr_hi); |
| 1682 | |
| 1683 | /* Restore the l1 indirect regs for each of the 6 l1s */ |
| 1684 | for (i = 0; i < 6; i++) |
| 1685 | for (j = 0; j < 0x12; j++) |
| 1686 | iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]); |
| 1687 | |
| 1688 | /* Restore the l2 indirect regs */ |
| 1689 | for (i = 0; i < 0x83; i++) |
| 1690 | iommu_write_l2(iommu, i, iommu->stored_l2[i]); |
| 1691 | |
| 1692 | /* Lock PCI setup registers */ |
| 1693 | pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4, |
| 1694 | iommu->stored_addr_lo | 1); |
Joerg Roedel | 4c894f4 | 2010-09-23 15:15:19 +0200 | [diff] [blame] | 1695 | } |
| 1696 | |
Joerg Roedel | 9f5f5fb | 2008-08-14 19:55:16 +0200 | [diff] [blame] | 1697 | /* |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1698 | * This function finally enables all IOMMUs found in the system after |
| 1699 | * they have been initialized |
| 1700 | */ |
Joerg Roedel | 11ee5ac | 2012-06-12 16:30:06 +0200 | [diff] [blame] | 1701 | static void early_enable_iommus(void) |
Joerg Roedel | 8736197 | 2008-06-26 21:28:07 +0200 | [diff] [blame] | 1702 | { |
| 1703 | struct amd_iommu *iommu; |
| 1704 | |
Joerg Roedel | 3bd2217 | 2009-05-04 15:06:20 +0200 | [diff] [blame] | 1705 | for_each_iommu(iommu) { |
Chris Wright | a8c485b | 2009-06-15 15:53:45 +0200 | [diff] [blame] | 1706 | iommu_disable(iommu); |
Joerg Roedel | e9bf519 | 2010-09-20 14:33:07 +0200 | [diff] [blame] | 1707 | iommu_init_flags(iommu); |
Joerg Roedel | 58492e1 | 2009-05-04 18:41:16 +0200 | [diff] [blame] | 1708 | iommu_set_device_table(iommu); |
| 1709 | iommu_enable_command_buffer(iommu); |
| 1710 | iommu_enable_event_buffer(iommu); |
Joerg Roedel | 8736197 | 2008-06-26 21:28:07 +0200 | [diff] [blame] | 1711 | iommu_set_exclusion_range(iommu); |
| 1712 | iommu_enable(iommu); |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1713 | iommu_flush_all_caches(iommu); |
Joerg Roedel | 8736197 | 2008-06-26 21:28:07 +0200 | [diff] [blame] | 1714 | } |
| 1715 | } |
| 1716 | |
Joerg Roedel | 11ee5ac | 2012-06-12 16:30:06 +0200 | [diff] [blame] | 1717 | static void enable_iommus_v2(void) |
| 1718 | { |
| 1719 | struct amd_iommu *iommu; |
| 1720 | |
| 1721 | for_each_iommu(iommu) { |
| 1722 | iommu_enable_ppr_log(iommu); |
| 1723 | iommu_enable_gt(iommu); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | static void enable_iommus(void) |
| 1728 | { |
| 1729 | early_enable_iommus(); |
| 1730 | |
| 1731 | enable_iommus_v2(); |
| 1732 | } |
| 1733 | |
Joerg Roedel | 92ac432 | 2009-05-19 19:06:27 +0200 | [diff] [blame] | 1734 | static void disable_iommus(void) |
| 1735 | { |
| 1736 | struct amd_iommu *iommu; |
| 1737 | |
| 1738 | for_each_iommu(iommu) |
| 1739 | iommu_disable(iommu); |
| 1740 | } |
| 1741 | |
Joerg Roedel | 7441e9c | 2008-06-30 20:18:02 +0200 | [diff] [blame] | 1742 | /* |
| 1743 | * Suspend/Resume support |
| 1744 | * disable suspend until real resume implemented |
| 1745 | */ |
| 1746 | |
Rafael J. Wysocki | f3c6ea1 | 2011-03-23 22:15:54 +0100 | [diff] [blame] | 1747 | static void amd_iommu_resume(void) |
Joerg Roedel | 7441e9c | 2008-06-30 20:18:02 +0200 | [diff] [blame] | 1748 | { |
Matthew Garrett | 5bcd757 | 2010-10-04 14:59:31 -0400 | [diff] [blame] | 1749 | struct amd_iommu *iommu; |
| 1750 | |
| 1751 | for_each_iommu(iommu) |
| 1752 | iommu_apply_resume_quirks(iommu); |
| 1753 | |
Joerg Roedel | 736501e | 2009-05-12 09:56:12 +0200 | [diff] [blame] | 1754 | /* re-load the hardware */ |
| 1755 | enable_iommus(); |
Joerg Roedel | 3d9761e | 2012-03-15 16:39:21 +0100 | [diff] [blame] | 1756 | |
| 1757 | amd_iommu_enable_interrupts(); |
Joerg Roedel | 7441e9c | 2008-06-30 20:18:02 +0200 | [diff] [blame] | 1758 | } |
| 1759 | |
Rafael J. Wysocki | f3c6ea1 | 2011-03-23 22:15:54 +0100 | [diff] [blame] | 1760 | static int amd_iommu_suspend(void) |
Joerg Roedel | 7441e9c | 2008-06-30 20:18:02 +0200 | [diff] [blame] | 1761 | { |
Joerg Roedel | 736501e | 2009-05-12 09:56:12 +0200 | [diff] [blame] | 1762 | /* disable IOMMUs to go out of the way for BIOS */ |
| 1763 | disable_iommus(); |
| 1764 | |
| 1765 | return 0; |
Joerg Roedel | 7441e9c | 2008-06-30 20:18:02 +0200 | [diff] [blame] | 1766 | } |
| 1767 | |
Rafael J. Wysocki | f3c6ea1 | 2011-03-23 22:15:54 +0100 | [diff] [blame] | 1768 | static struct syscore_ops amd_iommu_syscore_ops = { |
Joerg Roedel | 7441e9c | 2008-06-30 20:18:02 +0200 | [diff] [blame] | 1769 | .suspend = amd_iommu_suspend, |
| 1770 | .resume = amd_iommu_resume, |
| 1771 | }; |
| 1772 | |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1773 | static void __init free_on_init_error(void) |
| 1774 | { |
Joerg Roedel | 0ea2c42 | 2012-06-15 18:05:20 +0200 | [diff] [blame] | 1775 | free_pages((unsigned long)irq_lookup_table, |
| 1776 | get_order(rlookup_table_size)); |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1777 | |
Julia Lawall | a591989 | 2015-09-13 14:15:31 +0200 | [diff] [blame] | 1778 | kmem_cache_destroy(amd_iommu_irq_cache); |
| 1779 | amd_iommu_irq_cache = NULL; |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1780 | |
| 1781 | free_pages((unsigned long)amd_iommu_rlookup_table, |
| 1782 | get_order(rlookup_table_size)); |
| 1783 | |
| 1784 | free_pages((unsigned long)amd_iommu_alias_table, |
| 1785 | get_order(alias_table_size)); |
| 1786 | |
| 1787 | free_pages((unsigned long)amd_iommu_dev_table, |
| 1788 | get_order(dev_table_size)); |
| 1789 | |
| 1790 | free_iommu_all(); |
| 1791 | |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1792 | #ifdef CONFIG_GART_IOMMU |
| 1793 | /* |
| 1794 | * We failed to initialize the AMD IOMMU - try fallback to GART |
| 1795 | * if possible. |
| 1796 | */ |
| 1797 | gart_iommu_init(); |
| 1798 | |
| 1799 | #endif |
| 1800 | } |
| 1801 | |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1802 | /* SB IOAPIC is always on this device in AMD systems */ |
| 1803 | #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0)) |
| 1804 | |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 1805 | static bool __init check_ioapic_information(void) |
| 1806 | { |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 1807 | const char *fw_bug = FW_BUG; |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1808 | bool ret, has_sb_ioapic; |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 1809 | int idx; |
| 1810 | |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1811 | has_sb_ioapic = false; |
| 1812 | ret = false; |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 1813 | |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 1814 | /* |
| 1815 | * If we have map overrides on the kernel command line the |
| 1816 | * messages in this function might not describe firmware bugs |
| 1817 | * anymore - so be careful |
| 1818 | */ |
| 1819 | if (cmdline_maps) |
| 1820 | fw_bug = ""; |
| 1821 | |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1822 | for (idx = 0; idx < nr_ioapics; idx++) { |
| 1823 | int devid, id = mpc_ioapic_id(idx); |
| 1824 | |
| 1825 | devid = get_ioapic_devid(id); |
| 1826 | if (devid < 0) { |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 1827 | pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n", |
| 1828 | fw_bug, id); |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1829 | ret = false; |
| 1830 | } else if (devid == IOAPIC_SB_DEVID) { |
| 1831 | has_sb_ioapic = true; |
| 1832 | ret = true; |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1836 | if (!has_sb_ioapic) { |
| 1837 | /* |
| 1838 | * We expect the SB IOAPIC to be listed in the IVRS |
| 1839 | * table. The system timer is connected to the SB IOAPIC |
| 1840 | * and if we don't have it in the list the system will |
| 1841 | * panic at boot time. This situation usually happens |
| 1842 | * when the BIOS is buggy and provides us the wrong |
| 1843 | * device id for the IOAPIC in the system. |
| 1844 | */ |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 1845 | pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug); |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | if (!ret) |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 1849 | pr_err("AMD-Vi: Disabling interrupt remapping\n"); |
Joerg Roedel | c2ff5cf5 | 2012-10-16 14:52:51 +0200 | [diff] [blame] | 1850 | |
| 1851 | return ret; |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
Joerg Roedel | d04e0ba | 2012-07-02 16:02:20 +0200 | [diff] [blame] | 1854 | static void __init free_dma_resources(void) |
| 1855 | { |
Joerg Roedel | d04e0ba | 2012-07-02 16:02:20 +0200 | [diff] [blame] | 1856 | free_pages((unsigned long)amd_iommu_pd_alloc_bitmap, |
| 1857 | get_order(MAX_DOMAIN_ID/8)); |
| 1858 | |
| 1859 | free_unity_maps(); |
| 1860 | } |
| 1861 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1862 | /* |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1863 | * This is the hardware init function for AMD IOMMU in the system. |
| 1864 | * This function is called either from amd_iommu_init or from the interrupt |
| 1865 | * remapping setup code. |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1866 | * |
| 1867 | * This function basically parses the ACPI table for AMD IOMMU (IVRS) |
| 1868 | * three times: |
| 1869 | * |
| 1870 | * 1 pass) Find the highest PCI device id the driver has to handle. |
| 1871 | * Upon this information the size of the data structures is |
| 1872 | * determined that needs to be allocated. |
| 1873 | * |
| 1874 | * 2 pass) Initialize the data structures just allocated with the |
| 1875 | * information in the ACPI table about available AMD IOMMUs |
| 1876 | * in the system. It also maps the PCI devices in the |
| 1877 | * system to specific IOMMUs |
| 1878 | * |
| 1879 | * 3 pass) After the basic data structures are allocated and |
| 1880 | * initialized we update them with information about memory |
| 1881 | * remapping requirements parsed out of the ACPI table in |
| 1882 | * this last pass. |
| 1883 | * |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1884 | * After everything is set up the IOMMUs are enabled and the necessary |
| 1885 | * hotplug and suspend notifiers are registered. |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 1886 | */ |
Joerg Roedel | 643511b | 2012-06-12 12:09:35 +0200 | [diff] [blame] | 1887 | static int __init early_amd_iommu_init(void) |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1888 | { |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 1889 | struct acpi_table_header *ivrs_base; |
| 1890 | acpi_size ivrs_size; |
| 1891 | acpi_status status; |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1892 | int i, ret = 0; |
| 1893 | |
Joerg Roedel | 643511b | 2012-06-12 12:09:35 +0200 | [diff] [blame] | 1894 | if (!amd_iommu_detected) |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1895 | return -ENODEV; |
| 1896 | |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 1897 | status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size); |
| 1898 | if (status == AE_NOT_FOUND) |
| 1899 | return -ENODEV; |
| 1900 | else if (ACPI_FAILURE(status)) { |
| 1901 | const char *err = acpi_format_exception(status); |
| 1902 | pr_err("AMD-Vi: IVRS table error: %s\n", err); |
| 1903 | return -EINVAL; |
| 1904 | } |
| 1905 | |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1906 | /* |
| 1907 | * First parse ACPI tables to find the largest Bus/Dev/Func |
| 1908 | * we need to handle. Upon this information the shared data |
| 1909 | * structures for the IOMMUs in the system will be allocated |
| 1910 | */ |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 1911 | ret = find_last_devid_acpi(ivrs_base); |
| 1912 | if (ret) |
Joerg Roedel | 3551a70 | 2010-03-01 13:52:19 +0100 | [diff] [blame] | 1913 | goto out; |
| 1914 | |
Joerg Roedel | c571484 | 2008-07-11 17:14:25 +0200 | [diff] [blame] | 1915 | dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE); |
| 1916 | alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE); |
| 1917 | rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE); |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1918 | |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1919 | /* Device table - directly used by all IOMMUs */ |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 1920 | ret = -ENOMEM; |
Joerg Roedel | 5dc8bff | 2008-07-11 17:14:32 +0200 | [diff] [blame] | 1921 | amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1922 | get_order(dev_table_size)); |
| 1923 | if (amd_iommu_dev_table == NULL) |
| 1924 | goto out; |
| 1925 | |
| 1926 | /* |
| 1927 | * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the |
| 1928 | * IOMMU see for that device |
| 1929 | */ |
| 1930 | amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL, |
| 1931 | get_order(alias_table_size)); |
| 1932 | if (amd_iommu_alias_table == NULL) |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 1933 | goto out; |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1934 | |
| 1935 | /* IOMMU rlookup table - find the IOMMU for a specific device */ |
Joerg Roedel | 83fd5cc | 2008-12-16 19:17:11 +0100 | [diff] [blame] | 1936 | amd_iommu_rlookup_table = (void *)__get_free_pages( |
| 1937 | GFP_KERNEL | __GFP_ZERO, |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1938 | get_order(rlookup_table_size)); |
| 1939 | if (amd_iommu_rlookup_table == NULL) |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 1940 | goto out; |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1941 | |
Joerg Roedel | 5dc8bff | 2008-07-11 17:14:32 +0200 | [diff] [blame] | 1942 | amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages( |
| 1943 | GFP_KERNEL | __GFP_ZERO, |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1944 | get_order(MAX_DOMAIN_ID/8)); |
| 1945 | if (amd_iommu_pd_alloc_bitmap == NULL) |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 1946 | goto out; |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1947 | |
| 1948 | /* |
Joerg Roedel | 5dc8bff | 2008-07-11 17:14:32 +0200 | [diff] [blame] | 1949 | * let all alias entries point to itself |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1950 | */ |
Joerg Roedel | 3a61ec3 | 2008-07-25 13:07:50 +0200 | [diff] [blame] | 1951 | for (i = 0; i <= amd_iommu_last_bdf; ++i) |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1952 | amd_iommu_alias_table[i] = i; |
| 1953 | |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1954 | /* |
| 1955 | * never allocate domain 0 because its used as the non-allocated and |
| 1956 | * error value placeholder |
| 1957 | */ |
| 1958 | amd_iommu_pd_alloc_bitmap[0] = 1; |
| 1959 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 1960 | spin_lock_init(&amd_iommu_pd_lock); |
| 1961 | |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1962 | /* |
| 1963 | * now the data structures are allocated and basically initialized |
| 1964 | * start the real acpi table scan |
| 1965 | */ |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 1966 | ret = init_iommu_all(ivrs_base); |
| 1967 | if (ret) |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 1968 | goto out; |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 1969 | |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 1970 | if (amd_iommu_irq_remap) |
| 1971 | amd_iommu_irq_remap = check_ioapic_information(); |
| 1972 | |
Joerg Roedel | 05152a0 | 2012-06-15 16:53:51 +0200 | [diff] [blame] | 1973 | if (amd_iommu_irq_remap) { |
| 1974 | /* |
| 1975 | * Interrupt remapping enabled, create kmem_cache for the |
| 1976 | * remapping tables. |
| 1977 | */ |
Wei Yongjun | 83ed9c1 | 2013-04-23 10:47:44 +0800 | [diff] [blame] | 1978 | ret = -ENOMEM; |
Joerg Roedel | 05152a0 | 2012-06-15 16:53:51 +0200 | [diff] [blame] | 1979 | amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache", |
| 1980 | MAX_IRQS_PER_TABLE * sizeof(u32), |
| 1981 | IRQ_TABLE_ALIGNMENT, |
| 1982 | 0, NULL); |
| 1983 | if (!amd_iommu_irq_cache) |
| 1984 | goto out; |
Joerg Roedel | 0ea2c42 | 2012-06-15 18:05:20 +0200 | [diff] [blame] | 1985 | |
| 1986 | irq_lookup_table = (void *)__get_free_pages( |
| 1987 | GFP_KERNEL | __GFP_ZERO, |
| 1988 | get_order(rlookup_table_size)); |
| 1989 | if (!irq_lookup_table) |
| 1990 | goto out; |
Joerg Roedel | 05152a0 | 2012-06-15 16:53:51 +0200 | [diff] [blame] | 1991 | } |
| 1992 | |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 1993 | ret = init_memory_definitions(ivrs_base); |
| 1994 | if (ret) |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 1995 | goto out; |
Joerg Roedel | 3551a70 | 2010-03-01 13:52:19 +0100 | [diff] [blame] | 1996 | |
Joerg Roedel | eb1eb7a | 2012-07-05 11:58:02 +0200 | [diff] [blame] | 1997 | /* init the device table */ |
| 1998 | init_device_table(); |
| 1999 | |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 2000 | out: |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 2001 | /* Don't leak any ACPI memory */ |
| 2002 | early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size); |
| 2003 | ivrs_base = NULL; |
| 2004 | |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 2005 | return ret; |
Joerg Roedel | 643511b | 2012-06-12 12:09:35 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Gerard Snitselaar | ae29514 | 2012-03-16 11:38:22 -0700 | [diff] [blame] | 2008 | static int amd_iommu_enable_interrupts(void) |
Joerg Roedel | 3d9761e | 2012-03-15 16:39:21 +0100 | [diff] [blame] | 2009 | { |
| 2010 | struct amd_iommu *iommu; |
| 2011 | int ret = 0; |
| 2012 | |
| 2013 | for_each_iommu(iommu) { |
| 2014 | ret = iommu_init_msi(iommu); |
| 2015 | if (ret) |
| 2016 | goto out; |
| 2017 | } |
| 2018 | |
| 2019 | out: |
| 2020 | return ret; |
| 2021 | } |
| 2022 | |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 2023 | static bool detect_ivrs(void) |
| 2024 | { |
| 2025 | struct acpi_table_header *ivrs_base; |
| 2026 | acpi_size ivrs_size; |
| 2027 | acpi_status status; |
| 2028 | |
| 2029 | status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size); |
| 2030 | if (status == AE_NOT_FOUND) |
| 2031 | return false; |
| 2032 | else if (ACPI_FAILURE(status)) { |
| 2033 | const char *err = acpi_format_exception(status); |
| 2034 | pr_err("AMD-Vi: IVRS table error: %s\n", err); |
| 2035 | return false; |
| 2036 | } |
| 2037 | |
| 2038 | early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size); |
| 2039 | |
Joerg Roedel | 1adb7d3 | 2012-08-06 14:18:42 +0200 | [diff] [blame] | 2040 | /* Make sure ACS will be enabled during PCI probe */ |
| 2041 | pci_request_acs(); |
| 2042 | |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 2043 | return true; |
| 2044 | } |
| 2045 | |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2046 | /**************************************************************************** |
| 2047 | * |
| 2048 | * AMD IOMMU Initialization State Machine |
| 2049 | * |
| 2050 | ****************************************************************************/ |
| 2051 | |
| 2052 | static int __init state_next(void) |
| 2053 | { |
| 2054 | int ret = 0; |
| 2055 | |
| 2056 | switch (init_state) { |
| 2057 | case IOMMU_START_STATE: |
| 2058 | if (!detect_ivrs()) { |
| 2059 | init_state = IOMMU_NOT_FOUND; |
| 2060 | ret = -ENODEV; |
| 2061 | } else { |
| 2062 | init_state = IOMMU_IVRS_DETECTED; |
| 2063 | } |
| 2064 | break; |
| 2065 | case IOMMU_IVRS_DETECTED: |
| 2066 | ret = early_amd_iommu_init(); |
| 2067 | init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED; |
| 2068 | break; |
| 2069 | case IOMMU_ACPI_FINISHED: |
| 2070 | early_enable_iommus(); |
| 2071 | register_syscore_ops(&amd_iommu_syscore_ops); |
| 2072 | x86_platform.iommu_shutdown = disable_iommus; |
| 2073 | init_state = IOMMU_ENABLED; |
| 2074 | break; |
| 2075 | case IOMMU_ENABLED: |
| 2076 | ret = amd_iommu_init_pci(); |
| 2077 | init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT; |
| 2078 | enable_iommus_v2(); |
| 2079 | break; |
| 2080 | case IOMMU_PCI_INIT: |
| 2081 | ret = amd_iommu_enable_interrupts(); |
| 2082 | init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN; |
| 2083 | break; |
| 2084 | case IOMMU_INTERRUPTS_EN: |
Joerg Roedel | 1e6a7b0 | 2015-07-28 16:58:48 +0200 | [diff] [blame] | 2085 | ret = amd_iommu_init_dma_ops(); |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2086 | init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS; |
| 2087 | break; |
| 2088 | case IOMMU_DMA_OPS: |
| 2089 | init_state = IOMMU_INITIALIZED; |
| 2090 | break; |
| 2091 | case IOMMU_INITIALIZED: |
| 2092 | /* Nothing to do */ |
| 2093 | break; |
| 2094 | case IOMMU_NOT_FOUND: |
| 2095 | case IOMMU_INIT_ERROR: |
| 2096 | /* Error states => do nothing */ |
| 2097 | ret = -EINVAL; |
| 2098 | break; |
| 2099 | default: |
| 2100 | /* Unknown state */ |
| 2101 | BUG(); |
| 2102 | } |
| 2103 | |
| 2104 | return ret; |
| 2105 | } |
| 2106 | |
| 2107 | static int __init iommu_go_to_state(enum iommu_init_state state) |
| 2108 | { |
| 2109 | int ret = 0; |
| 2110 | |
| 2111 | while (init_state != state) { |
| 2112 | ret = state_next(); |
| 2113 | if (init_state == IOMMU_NOT_FOUND || |
| 2114 | init_state == IOMMU_INIT_ERROR) |
| 2115 | break; |
| 2116 | } |
| 2117 | |
| 2118 | return ret; |
| 2119 | } |
| 2120 | |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 2121 | #ifdef CONFIG_IRQ_REMAP |
| 2122 | int __init amd_iommu_prepare(void) |
| 2123 | { |
Thomas Gleixner | 3f4cb7c | 2015-01-23 14:32:46 +0100 | [diff] [blame] | 2124 | int ret; |
| 2125 | |
Jiang Liu | 7fa1c84 | 2015-01-07 15:31:42 +0800 | [diff] [blame] | 2126 | amd_iommu_irq_remap = true; |
Joerg Roedel | 84d0779 | 2015-01-07 15:31:39 +0800 | [diff] [blame] | 2127 | |
Thomas Gleixner | 3f4cb7c | 2015-01-23 14:32:46 +0100 | [diff] [blame] | 2128 | ret = iommu_go_to_state(IOMMU_ACPI_FINISHED); |
| 2129 | if (ret) |
| 2130 | return ret; |
| 2131 | return amd_iommu_irq_remap ? 0 : -ENODEV; |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 2132 | } |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2133 | |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 2134 | int __init amd_iommu_enable(void) |
| 2135 | { |
| 2136 | int ret; |
| 2137 | |
| 2138 | ret = iommu_go_to_state(IOMMU_ENABLED); |
| 2139 | if (ret) |
| 2140 | return ret; |
| 2141 | |
| 2142 | irq_remapping_enabled = 1; |
| 2143 | |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | void amd_iommu_disable(void) |
| 2148 | { |
| 2149 | amd_iommu_suspend(); |
| 2150 | } |
| 2151 | |
| 2152 | int amd_iommu_reenable(int mode) |
| 2153 | { |
| 2154 | amd_iommu_resume(); |
| 2155 | |
| 2156 | return 0; |
| 2157 | } |
| 2158 | |
| 2159 | int __init amd_iommu_enable_faulting(void) |
| 2160 | { |
| 2161 | /* We enable MSI later when PCI is initialized */ |
| 2162 | return 0; |
| 2163 | } |
| 2164 | #endif |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2165 | |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 2166 | /* |
| 2167 | * This is the core init function for AMD IOMMU hardware in the system. |
| 2168 | * This function is called from the generic x86 DMA layer initialization |
| 2169 | * code. |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 2170 | */ |
| 2171 | static int __init amd_iommu_init(void) |
| 2172 | { |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2173 | int ret; |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 2174 | |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2175 | ret = iommu_go_to_state(IOMMU_INITIALIZED); |
| 2176 | if (ret) { |
Joerg Roedel | d04e0ba | 2012-07-02 16:02:20 +0200 | [diff] [blame] | 2177 | free_dma_resources(); |
| 2178 | if (!irq_remapping_enabled) { |
| 2179 | disable_iommus(); |
| 2180 | free_on_init_error(); |
| 2181 | } else { |
| 2182 | struct amd_iommu *iommu; |
| 2183 | |
| 2184 | uninit_device_table_dma(); |
| 2185 | for_each_iommu(iommu) |
| 2186 | iommu_flush_all_caches(iommu); |
| 2187 | } |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2188 | } |
Joerg Roedel | 8704a1b | 2012-03-01 15:57:53 +0100 | [diff] [blame] | 2189 | |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 2190 | return ret; |
Joerg Roedel | fe74c9c | 2008-06-26 21:27:50 +0200 | [diff] [blame] | 2191 | } |
| 2192 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 2193 | /**************************************************************************** |
| 2194 | * |
| 2195 | * Early detect code. This code runs at IOMMU detection time in the DMA |
| 2196 | * layer. It just looks if there is an IVRS ACPI table to detect AMD |
| 2197 | * IOMMUs |
| 2198 | * |
| 2199 | ****************************************************************************/ |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 2200 | int __init amd_iommu_detect(void) |
Joerg Roedel | ae7877d | 2008-06-26 21:27:51 +0200 | [diff] [blame] | 2201 | { |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2202 | int ret; |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 2203 | |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 2204 | if (no_iommu || (iommu_detected && !gart_iommu_aperture)) |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 2205 | return -ENODEV; |
Joerg Roedel | ae7877d | 2008-06-26 21:27:51 +0200 | [diff] [blame] | 2206 | |
Joerg Roedel | a523572 | 2010-05-11 17:12:33 +0200 | [diff] [blame] | 2207 | if (amd_iommu_disabled) |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 2208 | return -ENODEV; |
Joerg Roedel | a523572 | 2010-05-11 17:12:33 +0200 | [diff] [blame] | 2209 | |
Joerg Roedel | 2c0ae17 | 2012-06-12 15:59:30 +0200 | [diff] [blame] | 2210 | ret = iommu_go_to_state(IOMMU_IVRS_DETECTED); |
| 2211 | if (ret) |
| 2212 | return ret; |
Linus Torvalds | 11bd04f | 2009-12-11 12:18:16 -0800 | [diff] [blame] | 2213 | |
Joerg Roedel | 02f3b3f | 2012-06-11 17:45:25 +0200 | [diff] [blame] | 2214 | amd_iommu_detected = true; |
| 2215 | iommu_detected = 1; |
| 2216 | x86_init.iommu.iommu_init = amd_iommu_init; |
| 2217 | |
Jérôme Glisse | 4781bc4 | 2015-08-31 18:13:03 -0400 | [diff] [blame] | 2218 | return 1; |
Joerg Roedel | ae7877d | 2008-06-26 21:27:51 +0200 | [diff] [blame] | 2219 | } |
| 2220 | |
Joerg Roedel | b65233a | 2008-07-11 17:14:21 +0200 | [diff] [blame] | 2221 | /**************************************************************************** |
| 2222 | * |
| 2223 | * Parsing functions for the AMD IOMMU specific kernel command line |
| 2224 | * options. |
| 2225 | * |
| 2226 | ****************************************************************************/ |
| 2227 | |
Joerg Roedel | fefda11 | 2009-05-20 12:21:42 +0200 | [diff] [blame] | 2228 | static int __init parse_amd_iommu_dump(char *str) |
| 2229 | { |
| 2230 | amd_iommu_dump = true; |
| 2231 | |
| 2232 | return 1; |
| 2233 | } |
| 2234 | |
Joerg Roedel | 918ad6c | 2008-06-26 21:27:52 +0200 | [diff] [blame] | 2235 | static int __init parse_amd_iommu_options(char *str) |
| 2236 | { |
| 2237 | for (; *str; ++str) { |
Joerg Roedel | 695b567 | 2008-11-17 15:16:43 +0100 | [diff] [blame] | 2238 | if (strncmp(str, "fullflush", 9) == 0) |
FUJITA Tomonori | afa9fdc | 2008-09-20 01:23:30 +0900 | [diff] [blame] | 2239 | amd_iommu_unmap_flush = true; |
Joerg Roedel | a523572 | 2010-05-11 17:12:33 +0200 | [diff] [blame] | 2240 | if (strncmp(str, "off", 3) == 0) |
| 2241 | amd_iommu_disabled = true; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2242 | if (strncmp(str, "force_isolation", 15) == 0) |
| 2243 | amd_iommu_force_isolation = true; |
Joerg Roedel | 918ad6c | 2008-06-26 21:27:52 +0200 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | return 1; |
| 2247 | } |
| 2248 | |
Joerg Roedel | 440e8998 | 2013-04-09 16:35:28 +0200 | [diff] [blame] | 2249 | static int __init parse_ivrs_ioapic(char *str) |
| 2250 | { |
| 2251 | unsigned int bus, dev, fn; |
| 2252 | int ret, id, i; |
| 2253 | u16 devid; |
| 2254 | |
| 2255 | ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn); |
| 2256 | |
| 2257 | if (ret != 4) { |
| 2258 | pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str); |
| 2259 | return 1; |
| 2260 | } |
| 2261 | |
| 2262 | if (early_ioapic_map_size == EARLY_MAP_SIZE) { |
| 2263 | pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n", |
| 2264 | str); |
| 2265 | return 1; |
| 2266 | } |
| 2267 | |
| 2268 | devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7); |
| 2269 | |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 2270 | cmdline_maps = true; |
Joerg Roedel | 440e8998 | 2013-04-09 16:35:28 +0200 | [diff] [blame] | 2271 | i = early_ioapic_map_size++; |
| 2272 | early_ioapic_map[i].id = id; |
| 2273 | early_ioapic_map[i].devid = devid; |
| 2274 | early_ioapic_map[i].cmd_line = true; |
| 2275 | |
| 2276 | return 1; |
| 2277 | } |
| 2278 | |
| 2279 | static int __init parse_ivrs_hpet(char *str) |
| 2280 | { |
| 2281 | unsigned int bus, dev, fn; |
| 2282 | int ret, id, i; |
| 2283 | u16 devid; |
| 2284 | |
| 2285 | ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn); |
| 2286 | |
| 2287 | if (ret != 4) { |
| 2288 | pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str); |
| 2289 | return 1; |
| 2290 | } |
| 2291 | |
| 2292 | if (early_hpet_map_size == EARLY_MAP_SIZE) { |
| 2293 | pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n", |
| 2294 | str); |
| 2295 | return 1; |
| 2296 | } |
| 2297 | |
| 2298 | devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7); |
| 2299 | |
Joerg Roedel | dfbb6d4 | 2013-04-09 19:06:18 +0200 | [diff] [blame] | 2300 | cmdline_maps = true; |
Joerg Roedel | 440e8998 | 2013-04-09 16:35:28 +0200 | [diff] [blame] | 2301 | i = early_hpet_map_size++; |
| 2302 | early_hpet_map[i].id = id; |
| 2303 | early_hpet_map[i].devid = devid; |
| 2304 | early_hpet_map[i].cmd_line = true; |
| 2305 | |
| 2306 | return 1; |
| 2307 | } |
| 2308 | |
| 2309 | __setup("amd_iommu_dump", parse_amd_iommu_dump); |
| 2310 | __setup("amd_iommu=", parse_amd_iommu_options); |
| 2311 | __setup("ivrs_ioapic", parse_ivrs_ioapic); |
| 2312 | __setup("ivrs_hpet", parse_ivrs_hpet); |
Konrad Rzeszutek Wilk | 22e6daf | 2010-08-26 13:58:03 -0400 | [diff] [blame] | 2313 | |
| 2314 | IOMMU_INIT_FINISH(amd_iommu_detect, |
| 2315 | gart_iommu_hole_init, |
Joerg Roedel | 98f1ad2 | 2012-07-06 13:28:37 +0200 | [diff] [blame] | 2316 | NULL, |
| 2317 | NULL); |
Joerg Roedel | 400a28a | 2011-11-28 15:11:02 +0100 | [diff] [blame] | 2318 | |
| 2319 | bool amd_iommu_v2_supported(void) |
| 2320 | { |
| 2321 | return amd_iommu_v2_present; |
| 2322 | } |
| 2323 | EXPORT_SYMBOL(amd_iommu_v2_supported); |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 2324 | |
| 2325 | /**************************************************************************** |
| 2326 | * |
| 2327 | * IOMMU EFR Performance Counter support functionality. This code allows |
| 2328 | * access to the IOMMU PC functionality. |
| 2329 | * |
| 2330 | ****************************************************************************/ |
| 2331 | |
| 2332 | u8 amd_iommu_pc_get_max_banks(u16 devid) |
| 2333 | { |
| 2334 | struct amd_iommu *iommu; |
| 2335 | u8 ret = 0; |
| 2336 | |
| 2337 | /* locate the iommu governing the devid */ |
| 2338 | iommu = amd_iommu_rlookup_table[devid]; |
| 2339 | if (iommu) |
| 2340 | ret = iommu->max_banks; |
| 2341 | |
| 2342 | return ret; |
| 2343 | } |
| 2344 | EXPORT_SYMBOL(amd_iommu_pc_get_max_banks); |
| 2345 | |
| 2346 | bool amd_iommu_pc_supported(void) |
| 2347 | { |
| 2348 | return amd_iommu_pc_present; |
| 2349 | } |
| 2350 | EXPORT_SYMBOL(amd_iommu_pc_supported); |
| 2351 | |
| 2352 | u8 amd_iommu_pc_get_max_counters(u16 devid) |
| 2353 | { |
| 2354 | struct amd_iommu *iommu; |
| 2355 | u8 ret = 0; |
| 2356 | |
| 2357 | /* locate the iommu governing the devid */ |
| 2358 | iommu = amd_iommu_rlookup_table[devid]; |
| 2359 | if (iommu) |
| 2360 | ret = iommu->max_counters; |
| 2361 | |
| 2362 | return ret; |
| 2363 | } |
| 2364 | EXPORT_SYMBOL(amd_iommu_pc_get_max_counters); |
| 2365 | |
Suravee Suthikulpanit | 38e45d0 | 2016-02-23 13:03:30 +0100 | [diff] [blame] | 2366 | static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu, |
| 2367 | u8 bank, u8 cntr, u8 fxn, |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 2368 | u64 *value, bool is_write) |
| 2369 | { |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 2370 | u32 offset; |
| 2371 | u32 max_offset_lim; |
| 2372 | |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 2373 | /* Check for valid iommu and pc register indexing */ |
Suravee Suthikulpanit | 38e45d0 | 2016-02-23 13:03:30 +0100 | [diff] [blame] | 2374 | if (WARN_ON((fxn > 0x28) || (fxn & 7))) |
Steven L Kinney | 30861dd | 2013-06-05 16:11:48 -0500 | [diff] [blame] | 2375 | return -ENODEV; |
| 2376 | |
| 2377 | offset = (u32)(((0x40|bank) << 12) | (cntr << 8) | fxn); |
| 2378 | |
| 2379 | /* Limit the offset to the hw defined mmio region aperture */ |
| 2380 | max_offset_lim = (u32)(((0x40|iommu->max_banks) << 12) | |
| 2381 | (iommu->max_counters << 8) | 0x28); |
| 2382 | if ((offset < MMIO_CNTR_REG_OFFSET) || |
| 2383 | (offset > max_offset_lim)) |
| 2384 | return -EINVAL; |
| 2385 | |
| 2386 | if (is_write) { |
| 2387 | writel((u32)*value, iommu->mmio_base + offset); |
| 2388 | writel((*value >> 32), iommu->mmio_base + offset + 4); |
| 2389 | } else { |
| 2390 | *value = readl(iommu->mmio_base + offset + 4); |
| 2391 | *value <<= 32; |
| 2392 | *value = readl(iommu->mmio_base + offset); |
| 2393 | } |
| 2394 | |
| 2395 | return 0; |
| 2396 | } |
| 2397 | EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val); |
Suravee Suthikulpanit | 38e45d0 | 2016-02-23 13:03:30 +0100 | [diff] [blame] | 2398 | |
| 2399 | int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn, |
| 2400 | u64 *value, bool is_write) |
| 2401 | { |
| 2402 | struct amd_iommu *iommu = amd_iommu_rlookup_table[devid]; |
| 2403 | |
| 2404 | /* Make sure the IOMMU PC resource is available */ |
| 2405 | if (!amd_iommu_pc_present || iommu == NULL) |
| 2406 | return -ENODEV; |
| 2407 | |
| 2408 | return iommu_pc_get_set_reg_val(iommu, bank, cntr, fxn, |
| 2409 | value, is_write); |
| 2410 | } |