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