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