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