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