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