Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 1 | /* |
Joerg Roedel | 5d0d715 | 2010-10-13 11:13:21 +0200 | [diff] [blame] | 2 | * Copyright (C) 2007-2010 Advanced Micro Devices, Inc. |
Joerg Roedel | 63ce3ae | 2015-02-04 16:12:55 +0100 | [diff] [blame] | 3 | * Author: Joerg Roedel <jroedel@suse.de> |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 4 | * Leo Duran <leo.duran@amd.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 20 | #include <linux/ratelimit.h> |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 21 | #include <linux/pci.h> |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 22 | #include <linux/acpi.h> |
Wan Zongshun | 9a4d3bf | 2016-04-01 09:06:05 -0400 | [diff] [blame] | 23 | #include <linux/amba/bus.h> |
Wan Zongshun | 0076cd3 | 2016-05-10 09:21:01 -0400 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 25 | #include <linux/pci-ats.h> |
Akinobu Mita | a66022c | 2009-12-15 16:48:28 -0800 | [diff] [blame] | 26 | #include <linux/bitmap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Joerg Roedel | 7f26508 | 2008-12-12 13:50:21 +0100 | [diff] [blame] | 28 | #include <linux/debugfs.h> |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 29 | #include <linux/scatterlist.h> |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 30 | #include <linux/dma-mapping.h> |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 31 | #include <linux/iommu-helper.h> |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 32 | #include <linux/iommu.h> |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 33 | #include <linux/delay.h> |
Joerg Roedel | 403f81d | 2011-06-14 16:44:25 +0200 | [diff] [blame] | 34 | #include <linux/amd-iommu.h> |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 35 | #include <linux/notifier.h> |
| 36 | #include <linux/export.h> |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 37 | #include <linux/irq.h> |
| 38 | #include <linux/msi.h> |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 39 | #include <linux/dma-contiguous.h> |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 40 | #include <linux/irqdomain.h> |
Joerg Roedel | 5f6bed5 | 2015-12-22 13:34:22 +0100 | [diff] [blame] | 41 | #include <linux/percpu.h> |
Joerg Roedel | 307d585 | 2016-07-05 11:54:04 +0200 | [diff] [blame] | 42 | #include <linux/iova.h> |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 43 | #include <asm/irq_remapping.h> |
| 44 | #include <asm/io_apic.h> |
| 45 | #include <asm/apic.h> |
| 46 | #include <asm/hw_irq.h> |
Joerg Roedel | 17f5b56 | 2011-07-06 17:14:44 +0200 | [diff] [blame] | 47 | #include <asm/msidef.h> |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 48 | #include <asm/proto.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 49 | #include <asm/iommu.h> |
Joerg Roedel | 1d9b16d | 2008-11-27 18:39:15 +0100 | [diff] [blame] | 50 | #include <asm/gart.h> |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 51 | #include <asm/dma.h> |
Joerg Roedel | 403f81d | 2011-06-14 16:44:25 +0200 | [diff] [blame] | 52 | |
| 53 | #include "amd_iommu_proto.h" |
| 54 | #include "amd_iommu_types.h" |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 55 | #include "irq_remapping.h" |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 56 | |
| 57 | #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) |
| 58 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 59 | #define LOOP_TIMEOUT 100000 |
Joerg Roedel | 136f78a | 2008-07-11 17:14:27 +0200 | [diff] [blame] | 60 | |
Joerg Roedel | 307d585 | 2016-07-05 11:54:04 +0200 | [diff] [blame] | 61 | /* IO virtual address start page frame number */ |
| 62 | #define IOVA_START_PFN (1) |
| 63 | #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) |
| 64 | #define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32)) |
| 65 | |
Joerg Roedel | 81cd07b | 2016-07-07 18:01:10 +0200 | [diff] [blame] | 66 | /* Reserved IOVA ranges */ |
| 67 | #define MSI_RANGE_START (0xfee00000) |
| 68 | #define MSI_RANGE_END (0xfeefffff) |
| 69 | #define HT_RANGE_START (0xfd00000000ULL) |
| 70 | #define HT_RANGE_END (0xffffffffffULL) |
| 71 | |
Ohad Ben-Cohen | aa3de9c | 2011-11-10 11:32:29 +0200 | [diff] [blame] | 72 | /* |
| 73 | * This bitmap is used to advertise the page sizes our hardware support |
| 74 | * to the IOMMU core, which will then use this information to split |
| 75 | * physically contiguous memory regions it is mapping into page sizes |
| 76 | * that we support. |
| 77 | * |
Joerg Roedel | 954e3dd | 2012-12-02 15:35:37 +0100 | [diff] [blame] | 78 | * 512GB Pages are not supported due to a hardware bug |
Ohad Ben-Cohen | aa3de9c | 2011-11-10 11:32:29 +0200 | [diff] [blame] | 79 | */ |
Joerg Roedel | 954e3dd | 2012-12-02 15:35:37 +0100 | [diff] [blame] | 80 | #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38)) |
Ohad Ben-Cohen | aa3de9c | 2011-11-10 11:32:29 +0200 | [diff] [blame] | 81 | |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 82 | static DEFINE_RWLOCK(amd_iommu_devtable_lock); |
| 83 | |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 84 | /* List of all available dev_data structures */ |
| 85 | static LIST_HEAD(dev_data_list); |
| 86 | static DEFINE_SPINLOCK(dev_data_list_lock); |
| 87 | |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 88 | LIST_HEAD(ioapic_map); |
| 89 | LIST_HEAD(hpet_map); |
Wan Zongshun | 2a0cb4e | 2016-04-01 09:06:00 -0400 | [diff] [blame] | 90 | LIST_HEAD(acpihid_map); |
Joerg Roedel | 6efed63 | 2012-06-14 15:52:58 +0200 | [diff] [blame] | 91 | |
Joerg Roedel | c5b5da9 | 2016-07-06 11:55:37 +0200 | [diff] [blame^] | 92 | #define FLUSH_QUEUE_SIZE 256 |
| 93 | |
| 94 | struct flush_queue_entry { |
| 95 | unsigned long iova_pfn; |
| 96 | unsigned long pages; |
| 97 | struct dma_ops_domain *dma_dom; |
| 98 | }; |
| 99 | |
| 100 | struct flush_queue { |
| 101 | spinlock_t lock; |
| 102 | unsigned next; |
| 103 | struct flush_queue_entry *entries; |
| 104 | }; |
| 105 | |
| 106 | DEFINE_PER_CPU(struct flush_queue, flush_queue); |
| 107 | |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 108 | /* |
| 109 | * Domain for untranslated devices - only allocated |
| 110 | * if iommu=pt passed on kernel cmd line. |
| 111 | */ |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 112 | static const struct iommu_ops amd_iommu_ops; |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 113 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 114 | static ATOMIC_NOTIFIER_HEAD(ppr_notifier); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 115 | int amd_iommu_max_glx_val = -1; |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 116 | |
Joerg Roedel | ac1534a | 2012-06-21 14:52:40 +0200 | [diff] [blame] | 117 | static struct dma_map_ops amd_iommu_dma_ops; |
| 118 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 119 | /* |
Joerg Roedel | 50917e2 | 2014-08-05 16:38:38 +0200 | [diff] [blame] | 120 | * This struct contains device specific data for the IOMMU |
| 121 | */ |
| 122 | struct iommu_dev_data { |
| 123 | struct list_head list; /* For domain->dev_list */ |
| 124 | struct list_head dev_data_list; /* For global dev_data_list */ |
Joerg Roedel | 50917e2 | 2014-08-05 16:38:38 +0200 | [diff] [blame] | 125 | struct protection_domain *domain; /* Domain the device is bound to */ |
Joerg Roedel | 50917e2 | 2014-08-05 16:38:38 +0200 | [diff] [blame] | 126 | u16 devid; /* PCI Device ID */ |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 127 | u16 alias; /* Alias Device ID */ |
Joerg Roedel | 50917e2 | 2014-08-05 16:38:38 +0200 | [diff] [blame] | 128 | bool iommu_v2; /* Device can make use of IOMMUv2 */ |
Joerg Roedel | 1e6a7b0 | 2015-07-28 16:58:48 +0200 | [diff] [blame] | 129 | bool passthrough; /* Device is identity mapped */ |
Joerg Roedel | 50917e2 | 2014-08-05 16:38:38 +0200 | [diff] [blame] | 130 | struct { |
| 131 | bool enabled; |
| 132 | int qdep; |
| 133 | } ats; /* ATS state */ |
| 134 | bool pri_tlp; /* PASID TLB required for |
| 135 | PPR completions */ |
| 136 | u32 errata; /* Bitmap for errata to apply */ |
| 137 | }; |
| 138 | |
| 139 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 140 | * general struct to manage commands send to an IOMMU |
| 141 | */ |
Joerg Roedel | d644953 | 2008-07-11 17:14:28 +0200 | [diff] [blame] | 142 | struct iommu_cmd { |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 143 | u32 data[4]; |
| 144 | }; |
| 145 | |
Joerg Roedel | 05152a0 | 2012-06-15 16:53:51 +0200 | [diff] [blame] | 146 | struct kmem_cache *amd_iommu_irq_cache; |
| 147 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 148 | static void update_domain(struct protection_domain *domain); |
Joerg Roedel | 7a5a566 | 2015-06-30 08:56:11 +0200 | [diff] [blame] | 149 | static int protection_domain_init(struct protection_domain *domain); |
Joerg Roedel | b6809ee | 2016-02-26 16:48:59 +0100 | [diff] [blame] | 150 | static void detach_device(struct device *dev); |
Chris Wright | c1eee67 | 2009-05-21 00:56:58 -0700 | [diff] [blame] | 151 | |
Joerg Roedel | 007b74b | 2015-12-21 12:53:54 +0100 | [diff] [blame] | 152 | /* |
Joerg Roedel | 007b74b | 2015-12-21 12:53:54 +0100 | [diff] [blame] | 153 | * Data container for a dma_ops specific protection domain |
| 154 | */ |
| 155 | struct dma_ops_domain { |
| 156 | /* generic protection domain information */ |
| 157 | struct protection_domain domain; |
| 158 | |
Joerg Roedel | 307d585 | 2016-07-05 11:54:04 +0200 | [diff] [blame] | 159 | /* IOVA RB-Tree */ |
| 160 | struct iova_domain iovad; |
Joerg Roedel | 007b74b | 2015-12-21 12:53:54 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
Joerg Roedel | 81cd07b | 2016-07-07 18:01:10 +0200 | [diff] [blame] | 163 | static struct iova_domain reserved_iova_ranges; |
| 164 | static struct lock_class_key reserved_rbtree_key; |
| 165 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 166 | /**************************************************************************** |
| 167 | * |
| 168 | * Helper functions |
| 169 | * |
| 170 | ****************************************************************************/ |
| 171 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 172 | static inline int match_hid_uid(struct device *dev, |
| 173 | struct acpihid_map_entry *entry) |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 174 | { |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 175 | const char *hid, *uid; |
| 176 | |
| 177 | hid = acpi_device_hid(ACPI_COMPANION(dev)); |
| 178 | uid = acpi_device_uid(ACPI_COMPANION(dev)); |
| 179 | |
| 180 | if (!hid || !(*hid)) |
| 181 | return -ENODEV; |
| 182 | |
| 183 | if (!uid || !(*uid)) |
| 184 | return strcmp(hid, entry->hid); |
| 185 | |
| 186 | if (!(*entry->uid)) |
| 187 | return strcmp(hid, entry->hid); |
| 188 | |
| 189 | return (strcmp(hid, entry->hid) || strcmp(uid, entry->uid)); |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 192 | static inline u16 get_pci_device_id(struct device *dev) |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 193 | { |
| 194 | struct pci_dev *pdev = to_pci_dev(dev); |
| 195 | |
| 196 | return PCI_DEVID(pdev->bus->number, pdev->devfn); |
| 197 | } |
| 198 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 199 | static inline int get_acpihid_device_id(struct device *dev, |
| 200 | struct acpihid_map_entry **entry) |
| 201 | { |
| 202 | struct acpihid_map_entry *p; |
| 203 | |
| 204 | list_for_each_entry(p, &acpihid_map, list) { |
| 205 | if (!match_hid_uid(dev, p)) { |
| 206 | if (entry) |
| 207 | *entry = p; |
| 208 | return p->devid; |
| 209 | } |
| 210 | } |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | static inline int get_device_id(struct device *dev) |
| 215 | { |
| 216 | int devid; |
| 217 | |
| 218 | if (dev_is_pci(dev)) |
| 219 | devid = get_pci_device_id(dev); |
| 220 | else |
| 221 | devid = get_acpihid_device_id(dev, NULL); |
| 222 | |
| 223 | return devid; |
| 224 | } |
| 225 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 226 | static struct protection_domain *to_pdomain(struct iommu_domain *dom) |
| 227 | { |
| 228 | return container_of(dom, struct protection_domain, domain); |
| 229 | } |
| 230 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 231 | static struct iommu_dev_data *alloc_dev_data(u16 devid) |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 232 | { |
| 233 | struct iommu_dev_data *dev_data; |
| 234 | unsigned long flags; |
| 235 | |
| 236 | dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL); |
| 237 | if (!dev_data) |
| 238 | return NULL; |
| 239 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 240 | dev_data->devid = devid; |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 241 | |
| 242 | spin_lock_irqsave(&dev_data_list_lock, flags); |
| 243 | list_add_tail(&dev_data->dev_data_list, &dev_data_list); |
| 244 | spin_unlock_irqrestore(&dev_data_list_lock, flags); |
| 245 | |
| 246 | return dev_data; |
| 247 | } |
| 248 | |
Joerg Roedel | 3b03bb7 | 2011-06-09 18:53:25 +0200 | [diff] [blame] | 249 | static struct iommu_dev_data *search_dev_data(u16 devid) |
| 250 | { |
| 251 | struct iommu_dev_data *dev_data; |
| 252 | unsigned long flags; |
| 253 | |
| 254 | spin_lock_irqsave(&dev_data_list_lock, flags); |
| 255 | list_for_each_entry(dev_data, &dev_data_list, dev_data_list) { |
| 256 | if (dev_data->devid == devid) |
| 257 | goto out_unlock; |
| 258 | } |
| 259 | |
| 260 | dev_data = NULL; |
| 261 | |
| 262 | out_unlock: |
| 263 | spin_unlock_irqrestore(&dev_data_list_lock, flags); |
| 264 | |
| 265 | return dev_data; |
| 266 | } |
| 267 | |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 268 | static int __last_alias(struct pci_dev *pdev, u16 alias, void *data) |
| 269 | { |
| 270 | *(u16 *)data = alias; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static u16 get_alias(struct device *dev) |
| 275 | { |
| 276 | struct pci_dev *pdev = to_pci_dev(dev); |
| 277 | u16 devid, ivrs_alias, pci_alias; |
| 278 | |
Joerg Roedel | 6c0b43d | 2016-05-09 19:39:17 +0200 | [diff] [blame] | 279 | /* The callers make sure that get_device_id() does not fail here */ |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 280 | devid = get_device_id(dev); |
| 281 | ivrs_alias = amd_iommu_alias_table[devid]; |
| 282 | pci_for_each_dma_alias(pdev, __last_alias, &pci_alias); |
| 283 | |
| 284 | if (ivrs_alias == pci_alias) |
| 285 | return ivrs_alias; |
| 286 | |
| 287 | /* |
| 288 | * DMA alias showdown |
| 289 | * |
| 290 | * The IVRS is fairly reliable in telling us about aliases, but it |
| 291 | * can't know about every screwy device. If we don't have an IVRS |
| 292 | * reported alias, use the PCI reported alias. In that case we may |
| 293 | * still need to initialize the rlookup and dev_table entries if the |
| 294 | * alias is to a non-existent device. |
| 295 | */ |
| 296 | if (ivrs_alias == devid) { |
| 297 | if (!amd_iommu_rlookup_table[pci_alias]) { |
| 298 | amd_iommu_rlookup_table[pci_alias] = |
| 299 | amd_iommu_rlookup_table[devid]; |
| 300 | memcpy(amd_iommu_dev_table[pci_alias].data, |
| 301 | amd_iommu_dev_table[devid].data, |
| 302 | sizeof(amd_iommu_dev_table[pci_alias].data)); |
| 303 | } |
| 304 | |
| 305 | return pci_alias; |
| 306 | } |
| 307 | |
| 308 | pr_info("AMD-Vi: Using IVRS reported alias %02x:%02x.%d " |
| 309 | "for device %s[%04x:%04x], kernel reported alias " |
| 310 | "%02x:%02x.%d\n", PCI_BUS_NUM(ivrs_alias), PCI_SLOT(ivrs_alias), |
| 311 | PCI_FUNC(ivrs_alias), dev_name(dev), pdev->vendor, pdev->device, |
| 312 | PCI_BUS_NUM(pci_alias), PCI_SLOT(pci_alias), |
| 313 | PCI_FUNC(pci_alias)); |
| 314 | |
| 315 | /* |
| 316 | * If we don't have a PCI DMA alias and the IVRS alias is on the same |
| 317 | * bus, then the IVRS table may know about a quirk that we don't. |
| 318 | */ |
| 319 | if (pci_alias == devid && |
| 320 | PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) { |
Linus Torvalds | 7afd16f | 2016-05-19 13:10:54 -0700 | [diff] [blame] | 321 | pci_add_dma_alias(pdev, ivrs_alias & 0xff); |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 322 | pr_info("AMD-Vi: Added PCI DMA alias %02x.%d for %s\n", |
| 323 | PCI_SLOT(ivrs_alias), PCI_FUNC(ivrs_alias), |
| 324 | dev_name(dev)); |
| 325 | } |
| 326 | |
| 327 | return ivrs_alias; |
| 328 | } |
| 329 | |
Joerg Roedel | 3b03bb7 | 2011-06-09 18:53:25 +0200 | [diff] [blame] | 330 | static struct iommu_dev_data *find_dev_data(u16 devid) |
| 331 | { |
| 332 | struct iommu_dev_data *dev_data; |
| 333 | |
| 334 | dev_data = search_dev_data(devid); |
| 335 | |
| 336 | if (dev_data == NULL) |
| 337 | dev_data = alloc_dev_data(devid); |
| 338 | |
| 339 | return dev_data; |
| 340 | } |
| 341 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 342 | static struct iommu_dev_data *get_dev_data(struct device *dev) |
| 343 | { |
| 344 | return dev->archdata.iommu; |
| 345 | } |
| 346 | |
Wan Zongshun | b097d11 | 2016-04-01 09:06:04 -0400 | [diff] [blame] | 347 | /* |
| 348 | * Find or create an IOMMU group for a acpihid device. |
| 349 | */ |
| 350 | static struct iommu_group *acpihid_device_group(struct device *dev) |
| 351 | { |
| 352 | struct acpihid_map_entry *p, *entry = NULL; |
Dan Carpenter | 2d8e1f0 | 2016-04-11 10:14:46 +0300 | [diff] [blame] | 353 | int devid; |
Wan Zongshun | b097d11 | 2016-04-01 09:06:04 -0400 | [diff] [blame] | 354 | |
| 355 | devid = get_acpihid_device_id(dev, &entry); |
| 356 | if (devid < 0) |
| 357 | return ERR_PTR(devid); |
| 358 | |
| 359 | list_for_each_entry(p, &acpihid_map, list) { |
| 360 | if ((devid == p->devid) && p->group) |
| 361 | entry->group = p->group; |
| 362 | } |
| 363 | |
| 364 | if (!entry->group) |
| 365 | entry->group = generic_device_group(dev); |
| 366 | |
| 367 | return entry->group; |
| 368 | } |
| 369 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 370 | static bool pci_iommuv2_capable(struct pci_dev *pdev) |
| 371 | { |
| 372 | static const int caps[] = { |
| 373 | PCI_EXT_CAP_ID_ATS, |
Joerg Roedel | 46277b7 | 2011-12-07 14:34:02 +0100 | [diff] [blame] | 374 | PCI_EXT_CAP_ID_PRI, |
| 375 | PCI_EXT_CAP_ID_PASID, |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 376 | }; |
| 377 | int i, pos; |
| 378 | |
| 379 | for (i = 0; i < 3; ++i) { |
| 380 | pos = pci_find_ext_capability(pdev, caps[i]); |
| 381 | if (pos == 0) |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 388 | static bool pdev_pri_erratum(struct pci_dev *pdev, u32 erratum) |
| 389 | { |
| 390 | struct iommu_dev_data *dev_data; |
| 391 | |
| 392 | dev_data = get_dev_data(&pdev->dev); |
| 393 | |
| 394 | return dev_data->errata & (1 << erratum) ? true : false; |
| 395 | } |
| 396 | |
Joerg Roedel | 71c7098 | 2009-11-24 16:43:06 +0100 | [diff] [blame] | 397 | /* |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 398 | * This function checks if the driver got a valid device from the caller to |
| 399 | * avoid dereferencing invalid pointers. |
| 400 | */ |
| 401 | static bool check_device(struct device *dev) |
| 402 | { |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 403 | int devid; |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 404 | |
| 405 | if (!dev || !dev->dma_mask) |
| 406 | return false; |
| 407 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 408 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 409 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 410 | return false; |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 411 | |
| 412 | /* Out of our scope? */ |
| 413 | if (devid > amd_iommu_last_bdf) |
| 414 | return false; |
| 415 | |
| 416 | if (amd_iommu_rlookup_table[devid] == NULL) |
| 417 | return false; |
| 418 | |
| 419 | return true; |
| 420 | } |
| 421 | |
Alex Williamson | 25b11ce | 2014-09-19 10:03:13 -0600 | [diff] [blame] | 422 | static void init_iommu_group(struct device *dev) |
Alex Williamson | 2851db2 | 2012-10-08 22:49:41 -0600 | [diff] [blame] | 423 | { |
Alex Williamson | 2851db2 | 2012-10-08 22:49:41 -0600 | [diff] [blame] | 424 | struct iommu_group *group; |
Alex Williamson | 2851db2 | 2012-10-08 22:49:41 -0600 | [diff] [blame] | 425 | |
Alex Williamson | 65d5352 | 2014-07-03 09:51:30 -0600 | [diff] [blame] | 426 | group = iommu_group_get_for_dev(dev); |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 427 | if (IS_ERR(group)) |
| 428 | return; |
| 429 | |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 430 | iommu_group_put(group); |
Alex Williamson | eb9c952 | 2012-10-08 22:49:35 -0600 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static int iommu_init_device(struct device *dev) |
| 434 | { |
Alex Williamson | eb9c952 | 2012-10-08 22:49:35 -0600 | [diff] [blame] | 435 | struct iommu_dev_data *dev_data; |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 436 | int devid; |
Alex Williamson | eb9c952 | 2012-10-08 22:49:35 -0600 | [diff] [blame] | 437 | |
| 438 | if (dev->archdata.iommu) |
| 439 | return 0; |
| 440 | |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 441 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 442 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 443 | return devid; |
| 444 | |
| 445 | dev_data = find_dev_data(devid); |
Alex Williamson | eb9c952 | 2012-10-08 22:49:35 -0600 | [diff] [blame] | 446 | if (!dev_data) |
| 447 | return -ENOMEM; |
| 448 | |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 449 | dev_data->alias = get_alias(dev); |
| 450 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 451 | if (dev_is_pci(dev) && pci_iommuv2_capable(to_pci_dev(dev))) { |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 452 | struct amd_iommu *iommu; |
| 453 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 454 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 455 | dev_data->iommu_v2 = iommu->is_iommu_v2; |
| 456 | } |
| 457 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 458 | dev->archdata.iommu = dev_data; |
| 459 | |
Alex Williamson | 066f2e9 | 2014-06-12 16:12:37 -0600 | [diff] [blame] | 460 | iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev, |
| 461 | dev); |
| 462 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
Joerg Roedel | 2601887 | 2011-06-06 16:50:14 +0200 | [diff] [blame] | 466 | static void iommu_ignore_device(struct device *dev) |
| 467 | { |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 468 | u16 alias; |
| 469 | int devid; |
Joerg Roedel | 2601887 | 2011-06-06 16:50:14 +0200 | [diff] [blame] | 470 | |
| 471 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 472 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 473 | return; |
| 474 | |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 475 | alias = get_alias(dev); |
Joerg Roedel | 2601887 | 2011-06-06 16:50:14 +0200 | [diff] [blame] | 476 | |
| 477 | memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry)); |
| 478 | memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry)); |
| 479 | |
| 480 | amd_iommu_rlookup_table[devid] = NULL; |
| 481 | amd_iommu_rlookup_table[alias] = NULL; |
| 482 | } |
| 483 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 484 | static void iommu_uninit_device(struct device *dev) |
| 485 | { |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 486 | int devid; |
| 487 | struct iommu_dev_data *dev_data; |
Alex Williamson | c193109 | 2014-07-03 09:51:24 -0600 | [diff] [blame] | 488 | |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 489 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 490 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 491 | return; |
| 492 | |
| 493 | dev_data = search_dev_data(devid); |
Alex Williamson | c193109 | 2014-07-03 09:51:24 -0600 | [diff] [blame] | 494 | if (!dev_data) |
| 495 | return; |
| 496 | |
Joerg Roedel | b6809ee | 2016-02-26 16:48:59 +0100 | [diff] [blame] | 497 | if (dev_data->domain) |
| 498 | detach_device(dev); |
| 499 | |
Alex Williamson | 066f2e9 | 2014-06-12 16:12:37 -0600 | [diff] [blame] | 500 | iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev, |
| 501 | dev); |
| 502 | |
Alex Williamson | 9dcd613 | 2012-05-30 14:19:07 -0600 | [diff] [blame] | 503 | iommu_group_remove_device(dev); |
| 504 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 505 | /* Remove dma-ops */ |
| 506 | dev->archdata.dma_ops = NULL; |
| 507 | |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 508 | /* |
Alex Williamson | c193109 | 2014-07-03 09:51:24 -0600 | [diff] [blame] | 509 | * We keep dev_data around for unplugged devices and reuse it when the |
| 510 | * device is re-plugged - not doing so would introduce a ton of races. |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 511 | */ |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 512 | } |
Joerg Roedel | b7cc955 | 2009-12-10 11:03:39 +0100 | [diff] [blame] | 513 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 514 | /**************************************************************************** |
| 515 | * |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 516 | * Interrupt handling functions |
| 517 | * |
| 518 | ****************************************************************************/ |
| 519 | |
Joerg Roedel | e3e5987 | 2009-09-03 14:02:10 +0200 | [diff] [blame] | 520 | static void dump_dte_entry(u16 devid) |
| 521 | { |
| 522 | int i; |
| 523 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 524 | for (i = 0; i < 4; ++i) |
| 525 | pr_err("AMD-Vi: DTE[%d]: %016llx\n", i, |
Joerg Roedel | e3e5987 | 2009-09-03 14:02:10 +0200 | [diff] [blame] | 526 | amd_iommu_dev_table[devid].data[i]); |
| 527 | } |
| 528 | |
Joerg Roedel | 945b4ac | 2009-09-03 14:25:02 +0200 | [diff] [blame] | 529 | static void dump_command(unsigned long phys_addr) |
| 530 | { |
| 531 | struct iommu_cmd *cmd = phys_to_virt(phys_addr); |
| 532 | int i; |
| 533 | |
| 534 | for (i = 0; i < 4; ++i) |
| 535 | pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]); |
| 536 | } |
| 537 | |
Joerg Roedel | a345b23 | 2009-09-03 15:01:43 +0200 | [diff] [blame] | 538 | static void iommu_print_event(struct amd_iommu *iommu, void *__evt) |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 539 | { |
Joerg Roedel | 3d06fca | 2012-04-12 14:12:00 +0200 | [diff] [blame] | 540 | int type, devid, domid, flags; |
| 541 | volatile u32 *event = __evt; |
| 542 | int count = 0; |
| 543 | u64 address; |
| 544 | |
| 545 | retry: |
| 546 | type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; |
| 547 | devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; |
| 548 | domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK; |
| 549 | flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; |
| 550 | address = (u64)(((u64)event[3]) << 32) | event[2]; |
| 551 | |
| 552 | if (type == 0) { |
| 553 | /* Did we hit the erratum? */ |
| 554 | if (++count == LOOP_TIMEOUT) { |
| 555 | pr_err("AMD-Vi: No event written to event log\n"); |
| 556 | return; |
| 557 | } |
| 558 | udelay(1); |
| 559 | goto retry; |
| 560 | } |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 561 | |
Joerg Roedel | 4c6f40d | 2009-09-01 16:43:58 +0200 | [diff] [blame] | 562 | printk(KERN_ERR "AMD-Vi: Event logged ["); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 563 | |
| 564 | switch (type) { |
| 565 | case EVENT_TYPE_ILL_DEV: |
| 566 | printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x " |
| 567 | "address=0x%016llx flags=0x%04x]\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 568 | PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 569 | address, flags); |
Joerg Roedel | e3e5987 | 2009-09-03 14:02:10 +0200 | [diff] [blame] | 570 | dump_dte_entry(devid); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 571 | break; |
| 572 | case EVENT_TYPE_IO_FAULT: |
| 573 | printk("IO_PAGE_FAULT device=%02x:%02x.%x " |
| 574 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 575 | PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 576 | domid, address, flags); |
| 577 | break; |
| 578 | case EVENT_TYPE_DEV_TAB_ERR: |
| 579 | printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x " |
| 580 | "address=0x%016llx flags=0x%04x]\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 581 | PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 582 | address, flags); |
| 583 | break; |
| 584 | case EVENT_TYPE_PAGE_TAB_ERR: |
| 585 | printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x " |
| 586 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 587 | PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 588 | domid, address, flags); |
| 589 | break; |
| 590 | case EVENT_TYPE_ILL_CMD: |
| 591 | printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); |
Joerg Roedel | 945b4ac | 2009-09-03 14:25:02 +0200 | [diff] [blame] | 592 | dump_command(address); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 593 | break; |
| 594 | case EVENT_TYPE_CMD_HARD_ERR: |
| 595 | printk("COMMAND_HARDWARE_ERROR address=0x%016llx " |
| 596 | "flags=0x%04x]\n", address, flags); |
| 597 | break; |
| 598 | case EVENT_TYPE_IOTLB_INV_TO: |
| 599 | printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x " |
| 600 | "address=0x%016llx]\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 601 | PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 602 | address); |
| 603 | break; |
| 604 | case EVENT_TYPE_INV_DEV_REQ: |
| 605 | printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x " |
| 606 | "address=0x%016llx flags=0x%04x]\n", |
Shuah Khan | c5081cd | 2013-02-27 17:07:19 -0700 | [diff] [blame] | 607 | PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 608 | address, flags); |
| 609 | break; |
| 610 | default: |
| 611 | printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type); |
| 612 | } |
Joerg Roedel | 3d06fca | 2012-04-12 14:12:00 +0200 | [diff] [blame] | 613 | |
| 614 | memset(__evt, 0, 4 * sizeof(u32)); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void iommu_poll_events(struct amd_iommu *iommu) |
| 618 | { |
| 619 | u32 head, tail; |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 620 | |
| 621 | head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); |
| 622 | tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); |
| 623 | |
| 624 | while (head != tail) { |
Joerg Roedel | a345b23 | 2009-09-03 15:01:43 +0200 | [diff] [blame] | 625 | iommu_print_event(iommu, iommu->evt_buf + head); |
Joerg Roedel | deba4bc | 2015-10-20 17:33:41 +0200 | [diff] [blame] | 626 | head = (head + EVENT_ENTRY_SIZE) % EVT_BUFFER_SIZE; |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Joerg Roedel | eee5353 | 2012-06-01 15:20:23 +0200 | [diff] [blame] | 632 | static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u64 *raw) |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 633 | { |
| 634 | struct amd_iommu_fault fault; |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 635 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 636 | if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) { |
| 637 | pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n"); |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | fault.address = raw[1]; |
| 642 | fault.pasid = PPR_PASID(raw[0]); |
| 643 | fault.device_id = PPR_DEVID(raw[0]); |
| 644 | fault.tag = PPR_TAG(raw[0]); |
| 645 | fault.flags = PPR_FLAGS(raw[0]); |
| 646 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 647 | atomic_notifier_call_chain(&ppr_notifier, 0, &fault); |
| 648 | } |
| 649 | |
| 650 | static void iommu_poll_ppr_log(struct amd_iommu *iommu) |
| 651 | { |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 652 | u32 head, tail; |
| 653 | |
| 654 | if (iommu->ppr_log == NULL) |
| 655 | return; |
| 656 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 657 | head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); |
| 658 | tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET); |
| 659 | |
| 660 | while (head != tail) { |
Joerg Roedel | eee5353 | 2012-06-01 15:20:23 +0200 | [diff] [blame] | 661 | volatile u64 *raw; |
| 662 | u64 entry[2]; |
| 663 | int i; |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 664 | |
Joerg Roedel | eee5353 | 2012-06-01 15:20:23 +0200 | [diff] [blame] | 665 | raw = (u64 *)(iommu->ppr_log + head); |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 666 | |
Joerg Roedel | eee5353 | 2012-06-01 15:20:23 +0200 | [diff] [blame] | 667 | /* |
| 668 | * Hardware bug: Interrupt may arrive before the entry is |
| 669 | * written to memory. If this happens we need to wait for the |
| 670 | * entry to arrive. |
| 671 | */ |
| 672 | for (i = 0; i < LOOP_TIMEOUT; ++i) { |
| 673 | if (PPR_REQ_TYPE(raw[0]) != 0) |
| 674 | break; |
| 675 | udelay(1); |
| 676 | } |
| 677 | |
| 678 | /* Avoid memcpy function-call overhead */ |
| 679 | entry[0] = raw[0]; |
| 680 | entry[1] = raw[1]; |
| 681 | |
| 682 | /* |
| 683 | * To detect the hardware bug we need to clear the entry |
| 684 | * back to zero. |
| 685 | */ |
| 686 | raw[0] = raw[1] = 0UL; |
| 687 | |
| 688 | /* Update head pointer of hardware ring-buffer */ |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 689 | head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE; |
| 690 | writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); |
Joerg Roedel | eee5353 | 2012-06-01 15:20:23 +0200 | [diff] [blame] | 691 | |
Joerg Roedel | eee5353 | 2012-06-01 15:20:23 +0200 | [diff] [blame] | 692 | /* Handle PPR entry */ |
| 693 | iommu_handle_ppr_entry(iommu, entry); |
| 694 | |
Joerg Roedel | eee5353 | 2012-06-01 15:20:23 +0200 | [diff] [blame] | 695 | /* Refresh ring-buffer information */ |
| 696 | head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 697 | tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET); |
| 698 | } |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Joerg Roedel | 72fe00f | 2011-05-10 10:50:42 +0200 | [diff] [blame] | 701 | irqreturn_t amd_iommu_int_thread(int irq, void *data) |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 702 | { |
Suravee Suthikulpanit | 3f398bc | 2013-04-22 16:32:34 -0500 | [diff] [blame] | 703 | struct amd_iommu *iommu = (struct amd_iommu *) data; |
| 704 | u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 705 | |
Suravee Suthikulpanit | 3f398bc | 2013-04-22 16:32:34 -0500 | [diff] [blame] | 706 | while (status & (MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK)) { |
| 707 | /* Enable EVT and PPR interrupts again */ |
| 708 | writel((MMIO_STATUS_EVT_INT_MASK | MMIO_STATUS_PPR_INT_MASK), |
| 709 | iommu->mmio_base + MMIO_STATUS_OFFSET); |
| 710 | |
| 711 | if (status & MMIO_STATUS_EVT_INT_MASK) { |
| 712 | pr_devel("AMD-Vi: Processing IOMMU Event Log\n"); |
| 713 | iommu_poll_events(iommu); |
| 714 | } |
| 715 | |
| 716 | if (status & MMIO_STATUS_PPR_INT_MASK) { |
| 717 | pr_devel("AMD-Vi: Processing IOMMU PPR Log\n"); |
| 718 | iommu_poll_ppr_log(iommu); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Hardware bug: ERBT1312 |
| 723 | * When re-enabling interrupt (by writing 1 |
| 724 | * to clear the bit), the hardware might also try to set |
| 725 | * the interrupt bit in the event status register. |
| 726 | * In this scenario, the bit will be set, and disable |
| 727 | * subsequent interrupts. |
| 728 | * |
| 729 | * Workaround: The IOMMU driver should read back the |
| 730 | * status register and check if the interrupt bits are cleared. |
| 731 | * If not, driver will need to go through the interrupt handler |
| 732 | * again and re-clear the bits |
| 733 | */ |
| 734 | status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 735 | } |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 736 | return IRQ_HANDLED; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Joerg Roedel | 72fe00f | 2011-05-10 10:50:42 +0200 | [diff] [blame] | 739 | irqreturn_t amd_iommu_int_handler(int irq, void *data) |
| 740 | { |
| 741 | return IRQ_WAKE_THREAD; |
| 742 | } |
| 743 | |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 744 | /**************************************************************************** |
| 745 | * |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 746 | * IOMMU command queuing functions |
| 747 | * |
| 748 | ****************************************************************************/ |
| 749 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 750 | static int wait_on_sem(volatile u64 *sem) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 751 | { |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 752 | int i = 0; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 753 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 754 | while (*sem == 0 && i < LOOP_TIMEOUT) { |
| 755 | udelay(1); |
| 756 | i += 1; |
| 757 | } |
| 758 | |
| 759 | if (i == LOOP_TIMEOUT) { |
| 760 | pr_alert("AMD-Vi: Completion-Wait loop timed out\n"); |
| 761 | return -EIO; |
| 762 | } |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 767 | static void copy_cmd_to_buffer(struct amd_iommu *iommu, |
| 768 | struct iommu_cmd *cmd, |
| 769 | u32 tail) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 770 | { |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 771 | u8 *target; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 772 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 773 | target = iommu->cmd_buf + tail; |
Joerg Roedel | deba4bc | 2015-10-20 17:33:41 +0200 | [diff] [blame] | 774 | tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 775 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 776 | /* Copy command to buffer */ |
| 777 | memcpy(target, cmd, sizeof(*cmd)); |
| 778 | |
| 779 | /* Tell the IOMMU about it */ |
| 780 | writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
| 781 | } |
| 782 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 783 | static void build_completion_wait(struct iommu_cmd *cmd, u64 address) |
Joerg Roedel | ded4673 | 2011-04-06 10:53:48 +0200 | [diff] [blame] | 784 | { |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 785 | WARN_ON(address & 0x7ULL); |
| 786 | |
Joerg Roedel | ded4673 | 2011-04-06 10:53:48 +0200 | [diff] [blame] | 787 | memset(cmd, 0, sizeof(*cmd)); |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 788 | cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK; |
| 789 | cmd->data[1] = upper_32_bits(__pa(address)); |
| 790 | cmd->data[2] = 1; |
Joerg Roedel | ded4673 | 2011-04-06 10:53:48 +0200 | [diff] [blame] | 791 | CMD_SET_TYPE(cmd, CMD_COMPL_WAIT); |
| 792 | } |
| 793 | |
Joerg Roedel | 94fe79e | 2011-04-06 11:07:21 +0200 | [diff] [blame] | 794 | static void build_inv_dte(struct iommu_cmd *cmd, u16 devid) |
| 795 | { |
| 796 | memset(cmd, 0, sizeof(*cmd)); |
| 797 | cmd->data[0] = devid; |
| 798 | CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY); |
| 799 | } |
| 800 | |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 801 | static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, |
| 802 | size_t size, u16 domid, int pde) |
| 803 | { |
| 804 | u64 pages; |
Quentin Lambert | ae0cbbb | 2015-02-04 11:40:07 +0100 | [diff] [blame] | 805 | bool s; |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 806 | |
| 807 | pages = iommu_num_pages(address, size, PAGE_SIZE); |
Quentin Lambert | ae0cbbb | 2015-02-04 11:40:07 +0100 | [diff] [blame] | 808 | s = false; |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 809 | |
| 810 | if (pages > 1) { |
| 811 | /* |
| 812 | * If we have to flush more than one page, flush all |
| 813 | * TLB entries for this domain |
| 814 | */ |
| 815 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; |
Quentin Lambert | ae0cbbb | 2015-02-04 11:40:07 +0100 | [diff] [blame] | 816 | s = true; |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | address &= PAGE_MASK; |
| 820 | |
| 821 | memset(cmd, 0, sizeof(*cmd)); |
| 822 | cmd->data[1] |= domid; |
| 823 | cmd->data[2] = lower_32_bits(address); |
| 824 | cmd->data[3] = upper_32_bits(address); |
| 825 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); |
| 826 | if (s) /* size bit - we flush more than one 4kb page */ |
| 827 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; |
Frank Arnold | df805ab | 2012-08-27 19:21:04 +0200 | [diff] [blame] | 828 | if (pde) /* PDE bit - we want to flush everything, not only the PTEs */ |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 829 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; |
| 830 | } |
| 831 | |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 832 | static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, |
| 833 | u64 address, size_t size) |
| 834 | { |
| 835 | u64 pages; |
Quentin Lambert | ae0cbbb | 2015-02-04 11:40:07 +0100 | [diff] [blame] | 836 | bool s; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 837 | |
| 838 | pages = iommu_num_pages(address, size, PAGE_SIZE); |
Quentin Lambert | ae0cbbb | 2015-02-04 11:40:07 +0100 | [diff] [blame] | 839 | s = false; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 840 | |
| 841 | if (pages > 1) { |
| 842 | /* |
| 843 | * If we have to flush more than one page, flush all |
| 844 | * TLB entries for this domain |
| 845 | */ |
| 846 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; |
Quentin Lambert | ae0cbbb | 2015-02-04 11:40:07 +0100 | [diff] [blame] | 847 | s = true; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | address &= PAGE_MASK; |
| 851 | |
| 852 | memset(cmd, 0, sizeof(*cmd)); |
| 853 | cmd->data[0] = devid; |
| 854 | cmd->data[0] |= (qdep & 0xff) << 24; |
| 855 | cmd->data[1] = devid; |
| 856 | cmd->data[2] = lower_32_bits(address); |
| 857 | cmd->data[3] = upper_32_bits(address); |
| 858 | CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); |
| 859 | if (s) |
| 860 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; |
| 861 | } |
| 862 | |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 863 | static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, int pasid, |
| 864 | u64 address, bool size) |
| 865 | { |
| 866 | memset(cmd, 0, sizeof(*cmd)); |
| 867 | |
| 868 | address &= ~(0xfffULL); |
| 869 | |
Suravee Suthikulpanit | a919a01 | 2014-03-05 18:54:18 -0600 | [diff] [blame] | 870 | cmd->data[0] = pasid; |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 871 | cmd->data[1] = domid; |
| 872 | cmd->data[2] = lower_32_bits(address); |
| 873 | cmd->data[3] = upper_32_bits(address); |
| 874 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; |
| 875 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; |
| 876 | if (size) |
| 877 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; |
| 878 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); |
| 879 | } |
| 880 | |
| 881 | static void build_inv_iotlb_pasid(struct iommu_cmd *cmd, u16 devid, int pasid, |
| 882 | int qdep, u64 address, bool size) |
| 883 | { |
| 884 | memset(cmd, 0, sizeof(*cmd)); |
| 885 | |
| 886 | address &= ~(0xfffULL); |
| 887 | |
| 888 | cmd->data[0] = devid; |
Jay Cornwall | e8d2d82 | 2014-02-26 15:49:31 -0600 | [diff] [blame] | 889 | cmd->data[0] |= ((pasid >> 8) & 0xff) << 16; |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 890 | cmd->data[0] |= (qdep & 0xff) << 24; |
| 891 | cmd->data[1] = devid; |
Jay Cornwall | e8d2d82 | 2014-02-26 15:49:31 -0600 | [diff] [blame] | 892 | cmd->data[1] |= (pasid & 0xff) << 16; |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 893 | cmd->data[2] = lower_32_bits(address); |
| 894 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; |
| 895 | cmd->data[3] = upper_32_bits(address); |
| 896 | if (size) |
| 897 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; |
| 898 | CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); |
| 899 | } |
| 900 | |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 901 | static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, int pasid, |
| 902 | int status, int tag, bool gn) |
| 903 | { |
| 904 | memset(cmd, 0, sizeof(*cmd)); |
| 905 | |
| 906 | cmd->data[0] = devid; |
| 907 | if (gn) { |
Suravee Suthikulpanit | a919a01 | 2014-03-05 18:54:18 -0600 | [diff] [blame] | 908 | cmd->data[1] = pasid; |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 909 | cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK; |
| 910 | } |
| 911 | cmd->data[3] = tag & 0x1ff; |
| 912 | cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT; |
| 913 | |
| 914 | CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR); |
| 915 | } |
| 916 | |
Joerg Roedel | 58fc7f1 | 2011-04-11 11:13:24 +0200 | [diff] [blame] | 917 | static void build_inv_all(struct iommu_cmd *cmd) |
| 918 | { |
| 919 | memset(cmd, 0, sizeof(*cmd)); |
| 920 | CMD_SET_TYPE(cmd, CMD_INV_ALL); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Joerg Roedel | 7ef2798 | 2012-06-21 16:46:04 +0200 | [diff] [blame] | 923 | static void build_inv_irt(struct iommu_cmd *cmd, u16 devid) |
| 924 | { |
| 925 | memset(cmd, 0, sizeof(*cmd)); |
| 926 | cmd->data[0] = devid; |
| 927 | CMD_SET_TYPE(cmd, CMD_INV_IRT); |
| 928 | } |
| 929 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 930 | /* |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 931 | * Writes the command to the IOMMUs command buffer and informs the |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 932 | * hardware about the new command. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 933 | */ |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 934 | static int iommu_queue_command_sync(struct amd_iommu *iommu, |
| 935 | struct iommu_cmd *cmd, |
| 936 | bool sync) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 937 | { |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 938 | u32 left, tail, head, next_tail; |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 939 | unsigned long flags; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 940 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 941 | again: |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 942 | spin_lock_irqsave(&iommu->lock, flags); |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 943 | |
| 944 | head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); |
| 945 | tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
Joerg Roedel | deba4bc | 2015-10-20 17:33:41 +0200 | [diff] [blame] | 946 | next_tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE; |
| 947 | left = (head - next_tail) % CMD_BUFFER_SIZE; |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 948 | |
| 949 | if (left <= 2) { |
| 950 | struct iommu_cmd sync_cmd; |
| 951 | volatile u64 sem = 0; |
| 952 | int ret; |
| 953 | |
| 954 | build_completion_wait(&sync_cmd, (u64)&sem); |
| 955 | copy_cmd_to_buffer(iommu, &sync_cmd, tail); |
| 956 | |
| 957 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 958 | |
| 959 | if ((ret = wait_on_sem(&sem)) != 0) |
| 960 | return ret; |
| 961 | |
| 962 | goto again; |
Joerg Roedel | 136f78a | 2008-07-11 17:14:27 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 965 | copy_cmd_to_buffer(iommu, cmd, tail); |
Joerg Roedel | 519c31b | 2008-08-14 19:55:15 +0200 | [diff] [blame] | 966 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 967 | /* We need to sync now to make sure all commands are processed */ |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 968 | iommu->need_sync = sync; |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 969 | |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 970 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 971 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 972 | return 0; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 973 | } |
| 974 | |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 975 | static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) |
| 976 | { |
| 977 | return iommu_queue_command_sync(iommu, cmd, true); |
| 978 | } |
| 979 | |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 980 | /* |
| 981 | * This function queues a completion wait command into the command |
| 982 | * buffer of an IOMMU |
| 983 | */ |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 984 | static int iommu_completion_wait(struct amd_iommu *iommu) |
| 985 | { |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 986 | struct iommu_cmd cmd; |
| 987 | volatile u64 sem = 0; |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 988 | int ret; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 989 | |
| 990 | if (!iommu->need_sync) |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 991 | return 0; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 992 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 993 | build_completion_wait(&cmd, (u64)&sem); |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 994 | |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 995 | ret = iommu_queue_command_sync(iommu, &cmd, false); |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 996 | if (ret) |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 997 | return ret; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 998 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 999 | return wait_on_sem(&sem); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Joerg Roedel | d8c1308 | 2011-04-06 18:51:26 +0200 | [diff] [blame] | 1002 | static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1003 | { |
| 1004 | struct iommu_cmd cmd; |
| 1005 | |
Joerg Roedel | d8c1308 | 2011-04-06 18:51:26 +0200 | [diff] [blame] | 1006 | build_inv_dte(&cmd, devid); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1007 | |
Joerg Roedel | d8c1308 | 2011-04-06 18:51:26 +0200 | [diff] [blame] | 1008 | return iommu_queue_command(iommu, &cmd); |
| 1009 | } |
| 1010 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1011 | static void iommu_flush_dte_all(struct amd_iommu *iommu) |
| 1012 | { |
| 1013 | u32 devid; |
| 1014 | |
| 1015 | for (devid = 0; devid <= 0xffff; ++devid) |
| 1016 | iommu_flush_dte(iommu, devid); |
| 1017 | |
| 1018 | iommu_completion_wait(iommu); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1022 | * This function uses heavy locking and may disable irqs for some time. But |
| 1023 | * this is no issue because it is only called during resume. |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1024 | */ |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1025 | static void iommu_flush_tlb_all(struct amd_iommu *iommu) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1026 | { |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1027 | u32 dom_id; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1028 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1029 | for (dom_id = 0; dom_id <= 0xffff; ++dom_id) { |
| 1030 | struct iommu_cmd cmd; |
| 1031 | build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, |
| 1032 | dom_id, 1); |
| 1033 | iommu_queue_command(iommu, &cmd); |
| 1034 | } |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1035 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1036 | iommu_completion_wait(iommu); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Joerg Roedel | 58fc7f1 | 2011-04-11 11:13:24 +0200 | [diff] [blame] | 1039 | static void iommu_flush_all(struct amd_iommu *iommu) |
| 1040 | { |
| 1041 | struct iommu_cmd cmd; |
| 1042 | |
| 1043 | build_inv_all(&cmd); |
| 1044 | |
| 1045 | iommu_queue_command(iommu, &cmd); |
| 1046 | iommu_completion_wait(iommu); |
| 1047 | } |
| 1048 | |
Joerg Roedel | 7ef2798 | 2012-06-21 16:46:04 +0200 | [diff] [blame] | 1049 | static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid) |
| 1050 | { |
| 1051 | struct iommu_cmd cmd; |
| 1052 | |
| 1053 | build_inv_irt(&cmd, devid); |
| 1054 | |
| 1055 | iommu_queue_command(iommu, &cmd); |
| 1056 | } |
| 1057 | |
| 1058 | static void iommu_flush_irt_all(struct amd_iommu *iommu) |
| 1059 | { |
| 1060 | u32 devid; |
| 1061 | |
| 1062 | for (devid = 0; devid <= MAX_DEV_TABLE_ENTRIES; devid++) |
| 1063 | iommu_flush_irt(iommu, devid); |
| 1064 | |
| 1065 | iommu_completion_wait(iommu); |
| 1066 | } |
| 1067 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1068 | void iommu_flush_all_caches(struct amd_iommu *iommu) |
| 1069 | { |
Joerg Roedel | 58fc7f1 | 2011-04-11 11:13:24 +0200 | [diff] [blame] | 1070 | if (iommu_feature(iommu, FEATURE_IA)) { |
| 1071 | iommu_flush_all(iommu); |
| 1072 | } else { |
| 1073 | iommu_flush_dte_all(iommu); |
Joerg Roedel | 7ef2798 | 2012-06-21 16:46:04 +0200 | [diff] [blame] | 1074 | iommu_flush_irt_all(iommu); |
Joerg Roedel | 58fc7f1 | 2011-04-11 11:13:24 +0200 | [diff] [blame] | 1075 | iommu_flush_tlb_all(iommu); |
| 1076 | } |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1079 | /* |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1080 | * Command send function for flushing on-device TLB |
| 1081 | */ |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1082 | static int device_flush_iotlb(struct iommu_dev_data *dev_data, |
| 1083 | u64 address, size_t size) |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1084 | { |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1085 | struct amd_iommu *iommu; |
| 1086 | struct iommu_cmd cmd; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1087 | int qdep; |
| 1088 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1089 | qdep = dev_data->ats.qdep; |
| 1090 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1091 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1092 | build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1093 | |
| 1094 | return iommu_queue_command(iommu, &cmd); |
| 1095 | } |
| 1096 | |
| 1097 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1098 | * Command send function for invalidating a device table entry |
| 1099 | */ |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1100 | static int device_flush_dte(struct iommu_dev_data *dev_data) |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 1101 | { |
| 1102 | struct amd_iommu *iommu; |
Joerg Roedel | e25bfb5 | 2015-10-20 17:33:38 +0200 | [diff] [blame] | 1103 | u16 alias; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1104 | int ret; |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 1105 | |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1106 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 1107 | alias = dev_data->alias; |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 1108 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 1109 | ret = iommu_flush_dte(iommu, dev_data->devid); |
Joerg Roedel | e25bfb5 | 2015-10-20 17:33:38 +0200 | [diff] [blame] | 1110 | if (!ret && alias != dev_data->devid) |
| 1111 | ret = iommu_flush_dte(iommu, alias); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1112 | if (ret) |
| 1113 | return ret; |
| 1114 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1115 | if (dev_data->ats.enabled) |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1116 | ret = device_flush_iotlb(dev_data, 0, ~0UL); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1117 | |
| 1118 | return ret; |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1121 | /* |
| 1122 | * TLB invalidation function which is called from the mapping functions. |
| 1123 | * It invalidates a single PTE if the range to flush is within a single |
| 1124 | * page. Otherwise it flushes the whole TLB of the IOMMU. |
| 1125 | */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1126 | static void __domain_flush_pages(struct protection_domain *domain, |
| 1127 | u64 address, size_t size, int pde) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1128 | { |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1129 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 1130 | struct iommu_cmd cmd; |
| 1131 | int ret = 0, i; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1132 | |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 1133 | build_inv_iommu_pages(&cmd, address, size, domain->id, pde); |
Joerg Roedel | 999ba41 | 2008-07-03 19:35:08 +0200 | [diff] [blame] | 1134 | |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 1135 | for (i = 0; i < amd_iommus_present; ++i) { |
| 1136 | if (!domain->dev_iommu[i]) |
| 1137 | continue; |
| 1138 | |
| 1139 | /* |
| 1140 | * Devices of this domain are behind this IOMMU |
| 1141 | * We need a TLB flush |
| 1142 | */ |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 1143 | ret |= iommu_queue_command(amd_iommus[i], &cmd); |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 1144 | } |
| 1145 | |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1146 | list_for_each_entry(dev_data, &domain->dev_list, list) { |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1147 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1148 | if (!dev_data->ats.enabled) |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1149 | continue; |
| 1150 | |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1151 | ret |= device_flush_iotlb(dev_data, address, size); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 1154 | WARN_ON(ret); |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 1155 | } |
| 1156 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1157 | static void domain_flush_pages(struct protection_domain *domain, |
| 1158 | u64 address, size_t size) |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 1159 | { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1160 | __domain_flush_pages(domain, address, size, 0); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 1161 | } |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 1162 | |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 1163 | /* Flush the whole IO/TLB for a given protection domain */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1164 | static void domain_flush_tlb(struct protection_domain *domain) |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 1165 | { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1166 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0); |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
Chris Wright | 42a49f9 | 2009-06-15 15:42:00 +0200 | [diff] [blame] | 1169 | /* Flush the whole IO/TLB for a given protection domain - including PDE */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1170 | static void domain_flush_tlb_pde(struct protection_domain *domain) |
Chris Wright | 42a49f9 | 2009-06-15 15:42:00 +0200 | [diff] [blame] | 1171 | { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1172 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1); |
| 1173 | } |
| 1174 | |
| 1175 | static void domain_flush_complete(struct protection_domain *domain) |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 1176 | { |
| 1177 | int i; |
| 1178 | |
| 1179 | for (i = 0; i < amd_iommus_present; ++i) { |
| 1180 | if (!domain->dev_iommu[i]) |
| 1181 | continue; |
| 1182 | |
| 1183 | /* |
| 1184 | * Devices of this domain are behind this IOMMU |
| 1185 | * We need to wait for completion of all commands. |
| 1186 | */ |
| 1187 | iommu_completion_wait(amd_iommus[i]); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 1191 | |
Joerg Roedel | 43f4960 | 2008-12-02 21:01:12 +0100 | [diff] [blame] | 1192 | /* |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 1193 | * This function flushes the DTEs for all devices in domain |
Joerg Roedel | 43f4960 | 2008-12-02 21:01:12 +0100 | [diff] [blame] | 1194 | */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1195 | static void domain_flush_devices(struct protection_domain *domain) |
Joerg Roedel | bfd1be1 | 2009-05-05 15:33:57 +0200 | [diff] [blame] | 1196 | { |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 1197 | struct iommu_dev_data *dev_data; |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 1198 | |
| 1199 | list_for_each_entry(dev_data, &domain->dev_list, list) |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1200 | device_flush_dte(dev_data); |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1203 | /**************************************************************************** |
| 1204 | * |
| 1205 | * The functions below are used the create the page table mappings for |
| 1206 | * unity mapped regions. |
| 1207 | * |
| 1208 | ****************************************************************************/ |
| 1209 | |
| 1210 | /* |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1211 | * This function is used to add another level to an IO page table. Adding |
| 1212 | * another level increases the size of the address space by 9 bits to a size up |
| 1213 | * to 64 bits. |
| 1214 | */ |
| 1215 | static bool increase_address_space(struct protection_domain *domain, |
| 1216 | gfp_t gfp) |
| 1217 | { |
| 1218 | u64 *pte; |
| 1219 | |
| 1220 | if (domain->mode == PAGE_MODE_6_LEVEL) |
| 1221 | /* address space already 64 bit large */ |
| 1222 | return false; |
| 1223 | |
| 1224 | pte = (void *)get_zeroed_page(gfp); |
| 1225 | if (!pte) |
| 1226 | return false; |
| 1227 | |
| 1228 | *pte = PM_LEVEL_PDE(domain->mode, |
| 1229 | virt_to_phys(domain->pt_root)); |
| 1230 | domain->pt_root = pte; |
| 1231 | domain->mode += 1; |
| 1232 | domain->updated = true; |
| 1233 | |
| 1234 | return true; |
| 1235 | } |
| 1236 | |
| 1237 | static u64 *alloc_pte(struct protection_domain *domain, |
| 1238 | unsigned long address, |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1239 | unsigned long page_size, |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1240 | u64 **pte_page, |
| 1241 | gfp_t gfp) |
| 1242 | { |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1243 | int level, end_lvl; |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1244 | u64 *pte, *page; |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1245 | |
| 1246 | BUG_ON(!is_power_of_2(page_size)); |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1247 | |
| 1248 | while (address > PM_LEVEL_SIZE(domain->mode)) |
| 1249 | increase_address_space(domain, gfp); |
| 1250 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1251 | level = domain->mode - 1; |
| 1252 | pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)]; |
| 1253 | address = PAGE_SIZE_ALIGN(address, page_size); |
| 1254 | end_lvl = PAGE_SIZE_LEVEL(page_size); |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1255 | |
| 1256 | while (level > end_lvl) { |
Joerg Roedel | 7bfa5bd | 2015-12-21 19:07:50 +0100 | [diff] [blame] | 1257 | u64 __pte, __npte; |
| 1258 | |
| 1259 | __pte = *pte; |
| 1260 | |
| 1261 | if (!IOMMU_PTE_PRESENT(__pte)) { |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1262 | page = (u64 *)get_zeroed_page(gfp); |
| 1263 | if (!page) |
| 1264 | return NULL; |
Joerg Roedel | 7bfa5bd | 2015-12-21 19:07:50 +0100 | [diff] [blame] | 1265 | |
| 1266 | __npte = PM_LEVEL_PDE(level, virt_to_phys(page)); |
| 1267 | |
| 1268 | if (cmpxchg64(pte, __pte, __npte)) { |
| 1269 | free_page((unsigned long)page); |
| 1270 | continue; |
| 1271 | } |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1272 | } |
| 1273 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1274 | /* No level skipping support yet */ |
| 1275 | if (PM_PTE_LEVEL(*pte) != level) |
| 1276 | return NULL; |
| 1277 | |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1278 | level -= 1; |
| 1279 | |
| 1280 | pte = IOMMU_PTE_PAGE(*pte); |
| 1281 | |
| 1282 | if (pte_page && level == end_lvl) |
| 1283 | *pte_page = pte; |
| 1284 | |
| 1285 | pte = &pte[PM_LEVEL_INDEX(level, address)]; |
| 1286 | } |
| 1287 | |
| 1288 | return pte; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * This function checks if there is a PTE for a given dma address. If |
| 1293 | * there is one, it returns the pointer to it. |
| 1294 | */ |
Joerg Roedel | 3039ca1 | 2015-04-01 14:58:48 +0200 | [diff] [blame] | 1295 | static u64 *fetch_pte(struct protection_domain *domain, |
| 1296 | unsigned long address, |
| 1297 | unsigned long *page_size) |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1298 | { |
| 1299 | int level; |
| 1300 | u64 *pte; |
| 1301 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1302 | if (address > PM_LEVEL_SIZE(domain->mode)) |
| 1303 | return NULL; |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1304 | |
Joerg Roedel | 3039ca1 | 2015-04-01 14:58:48 +0200 | [diff] [blame] | 1305 | level = domain->mode - 1; |
| 1306 | pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)]; |
| 1307 | *page_size = PTE_LEVEL_PAGE_SIZE(level); |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1308 | |
| 1309 | while (level > 0) { |
| 1310 | |
| 1311 | /* Not Present */ |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1312 | if (!IOMMU_PTE_PRESENT(*pte)) |
| 1313 | return NULL; |
| 1314 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1315 | /* Large PTE */ |
Joerg Roedel | 3039ca1 | 2015-04-01 14:58:48 +0200 | [diff] [blame] | 1316 | if (PM_PTE_LEVEL(*pte) == 7 || |
| 1317 | PM_PTE_LEVEL(*pte) == 0) |
| 1318 | break; |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1319 | |
| 1320 | /* No level skipping support yet */ |
| 1321 | if (PM_PTE_LEVEL(*pte) != level) |
| 1322 | return NULL; |
| 1323 | |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1324 | level -= 1; |
| 1325 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1326 | /* Walk to the next level */ |
Joerg Roedel | 3039ca1 | 2015-04-01 14:58:48 +0200 | [diff] [blame] | 1327 | pte = IOMMU_PTE_PAGE(*pte); |
| 1328 | pte = &pte[PM_LEVEL_INDEX(level, address)]; |
| 1329 | *page_size = PTE_LEVEL_PAGE_SIZE(level); |
| 1330 | } |
| 1331 | |
| 1332 | if (PM_PTE_LEVEL(*pte) == 0x07) { |
| 1333 | unsigned long pte_mask; |
| 1334 | |
| 1335 | /* |
| 1336 | * If we have a series of large PTEs, make |
| 1337 | * sure to return a pointer to the first one. |
| 1338 | */ |
| 1339 | *page_size = pte_mask = PTE_PAGE_SIZE(*pte); |
| 1340 | pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1); |
| 1341 | pte = (u64 *)(((unsigned long)pte) & pte_mask); |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | return pte; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1348 | * Generic mapping functions. It maps a physical address into a DMA |
| 1349 | * address space. It allocates the page table pages if necessary. |
| 1350 | * In the future it can be extended to a generic mapping function |
| 1351 | * supporting all features of AMD IOMMU page tables like level skipping |
| 1352 | * and full 64 bit address spaces. |
| 1353 | */ |
Joerg Roedel | 38e817f | 2008-12-02 17:27:52 +0100 | [diff] [blame] | 1354 | static int iommu_map_page(struct protection_domain *dom, |
| 1355 | unsigned long bus_addr, |
| 1356 | unsigned long phys_addr, |
Joerg Roedel | b911b89 | 2016-07-05 14:29:11 +0200 | [diff] [blame] | 1357 | unsigned long page_size, |
Joerg Roedel | abdc5eb | 2009-09-03 11:33:51 +0200 | [diff] [blame] | 1358 | int prot, |
Joerg Roedel | b911b89 | 2016-07-05 14:29:11 +0200 | [diff] [blame] | 1359 | gfp_t gfp) |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1360 | { |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 1361 | u64 __pte, *pte; |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1362 | int i, count; |
Joerg Roedel | abdc5eb | 2009-09-03 11:33:51 +0200 | [diff] [blame] | 1363 | |
Joerg Roedel | d4b0366 | 2015-04-01 14:58:52 +0200 | [diff] [blame] | 1364 | BUG_ON(!IS_ALIGNED(bus_addr, page_size)); |
| 1365 | BUG_ON(!IS_ALIGNED(phys_addr, page_size)); |
| 1366 | |
Joerg Roedel | bad1cac | 2009-09-02 16:52:23 +0200 | [diff] [blame] | 1367 | if (!(prot & IOMMU_PROT_MASK)) |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1368 | return -EINVAL; |
| 1369 | |
Joerg Roedel | d4b0366 | 2015-04-01 14:58:52 +0200 | [diff] [blame] | 1370 | count = PAGE_SIZE_PTE_COUNT(page_size); |
Joerg Roedel | b911b89 | 2016-07-05 14:29:11 +0200 | [diff] [blame] | 1371 | pte = alloc_pte(dom, bus_addr, page_size, NULL, gfp); |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1372 | |
Maurizio Lombardi | 63eaa75 | 2014-09-11 12:28:03 +0200 | [diff] [blame] | 1373 | if (!pte) |
| 1374 | return -ENOMEM; |
| 1375 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1376 | for (i = 0; i < count; ++i) |
| 1377 | if (IOMMU_PTE_PRESENT(pte[i])) |
| 1378 | return -EBUSY; |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1379 | |
Joerg Roedel | d4b0366 | 2015-04-01 14:58:52 +0200 | [diff] [blame] | 1380 | if (count > 1) { |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1381 | __pte = PAGE_SIZE_PTE(phys_addr, page_size); |
| 1382 | __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC; |
| 1383 | } else |
| 1384 | __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC; |
| 1385 | |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1386 | if (prot & IOMMU_PROT_IR) |
| 1387 | __pte |= IOMMU_PTE_IR; |
| 1388 | if (prot & IOMMU_PROT_IW) |
| 1389 | __pte |= IOMMU_PTE_IW; |
| 1390 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1391 | for (i = 0; i < count; ++i) |
| 1392 | pte[i] = __pte; |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1393 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 1394 | update_domain(dom); |
| 1395 | |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1396 | return 0; |
| 1397 | } |
| 1398 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1399 | static unsigned long iommu_unmap_page(struct protection_domain *dom, |
| 1400 | unsigned long bus_addr, |
| 1401 | unsigned long page_size) |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1402 | { |
Joerg Roedel | 71b390e | 2015-04-01 14:58:49 +0200 | [diff] [blame] | 1403 | unsigned long long unmapped; |
| 1404 | unsigned long unmap_size; |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1405 | u64 *pte; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1406 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1407 | BUG_ON(!is_power_of_2(page_size)); |
| 1408 | |
| 1409 | unmapped = 0; |
| 1410 | |
| 1411 | while (unmapped < page_size) { |
| 1412 | |
Joerg Roedel | 71b390e | 2015-04-01 14:58:49 +0200 | [diff] [blame] | 1413 | pte = fetch_pte(dom, bus_addr, &unmap_size); |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1414 | |
Joerg Roedel | 71b390e | 2015-04-01 14:58:49 +0200 | [diff] [blame] | 1415 | if (pte) { |
| 1416 | int i, count; |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1417 | |
Joerg Roedel | 71b390e | 2015-04-01 14:58:49 +0200 | [diff] [blame] | 1418 | count = PAGE_SIZE_PTE_COUNT(unmap_size); |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1419 | for (i = 0; i < count; i++) |
| 1420 | pte[i] = 0ULL; |
| 1421 | } |
| 1422 | |
| 1423 | bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size; |
| 1424 | unmapped += unmap_size; |
| 1425 | } |
| 1426 | |
Alex Williamson | 60d0ca3 | 2013-06-21 14:33:19 -0600 | [diff] [blame] | 1427 | BUG_ON(unmapped && !is_power_of_2(unmapped)); |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1428 | |
| 1429 | return unmapped; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1430 | } |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1431 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1432 | /**************************************************************************** |
| 1433 | * |
| 1434 | * The next functions belong to the address allocator for the dma_ops |
Joerg Roedel | 2d4c515 | 2016-07-05 16:21:32 +0200 | [diff] [blame] | 1435 | * interface functions. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1436 | * |
| 1437 | ****************************************************************************/ |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1438 | |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1439 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1440 | static unsigned long dma_ops_alloc_iova(struct device *dev, |
| 1441 | struct dma_ops_domain *dma_dom, |
| 1442 | unsigned int pages, u64 dma_mask) |
Joerg Roedel | a0f5144 | 2015-12-21 16:20:09 +0100 | [diff] [blame] | 1443 | { |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1444 | unsigned long pfn = 0; |
Joerg Roedel | a0f5144 | 2015-12-21 16:20:09 +0100 | [diff] [blame] | 1445 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1446 | pages = __roundup_pow_of_two(pages); |
Joerg Roedel | a0f5144 | 2015-12-21 16:20:09 +0100 | [diff] [blame] | 1447 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1448 | if (dma_mask > DMA_BIT_MASK(32)) |
| 1449 | pfn = alloc_iova_fast(&dma_dom->iovad, pages, |
| 1450 | IOVA_PFN(DMA_BIT_MASK(32))); |
Joerg Roedel | 7b5e25b | 2015-12-22 13:38:12 +0100 | [diff] [blame] | 1451 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1452 | if (!pfn) |
| 1453 | pfn = alloc_iova_fast(&dma_dom->iovad, pages, IOVA_PFN(dma_mask)); |
Joerg Roedel | 60e6a7c | 2015-12-21 16:53:17 +0100 | [diff] [blame] | 1454 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1455 | return (pfn << PAGE_SHIFT); |
Joerg Roedel | a0f5144 | 2015-12-21 16:20:09 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1458 | static void dma_ops_free_iova(struct dma_ops_domain *dma_dom, |
| 1459 | unsigned long address, |
| 1460 | unsigned int pages) |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1461 | { |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1462 | pages = __roundup_pow_of_two(pages); |
| 1463 | address >>= PAGE_SHIFT; |
Joerg Roedel | 5f6bed5 | 2015-12-22 13:34:22 +0100 | [diff] [blame] | 1464 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 1465 | free_iova_fast(&dma_dom->iovad, address, pages); |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1468 | /**************************************************************************** |
| 1469 | * |
| 1470 | * The next functions belong to the domain allocation. A domain is |
| 1471 | * allocated for every IOMMU as the default domain. If device isolation |
| 1472 | * is enabled, every device get its own domain. The most important thing |
| 1473 | * about domains is the page table mapping the DMA address space they |
| 1474 | * contain. |
| 1475 | * |
| 1476 | ****************************************************************************/ |
| 1477 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 1478 | /* |
| 1479 | * This function adds a protection domain to the global protection domain list |
| 1480 | */ |
| 1481 | static void add_domain_to_list(struct protection_domain *domain) |
| 1482 | { |
| 1483 | unsigned long flags; |
| 1484 | |
| 1485 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); |
| 1486 | list_add(&domain->list, &amd_iommu_pd_list); |
| 1487 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * This function removes a protection domain to the global |
| 1492 | * protection domain list |
| 1493 | */ |
| 1494 | static void del_domain_from_list(struct protection_domain *domain) |
| 1495 | { |
| 1496 | unsigned long flags; |
| 1497 | |
| 1498 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); |
| 1499 | list_del(&domain->list); |
| 1500 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); |
| 1501 | } |
| 1502 | |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1503 | static u16 domain_id_alloc(void) |
| 1504 | { |
| 1505 | unsigned long flags; |
| 1506 | int id; |
| 1507 | |
| 1508 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 1509 | id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID); |
| 1510 | BUG_ON(id == 0); |
| 1511 | if (id > 0 && id < MAX_DOMAIN_ID) |
| 1512 | __set_bit(id, amd_iommu_pd_alloc_bitmap); |
| 1513 | else |
| 1514 | id = 0; |
| 1515 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 1516 | |
| 1517 | return id; |
| 1518 | } |
| 1519 | |
Joerg Roedel | a2acfb7 | 2008-12-02 18:28:53 +0100 | [diff] [blame] | 1520 | static void domain_id_free(int id) |
| 1521 | { |
| 1522 | unsigned long flags; |
| 1523 | |
| 1524 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 1525 | if (id > 0 && id < MAX_DOMAIN_ID) |
| 1526 | __clear_bit(id, amd_iommu_pd_alloc_bitmap); |
| 1527 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 1528 | } |
Joerg Roedel | a2acfb7 | 2008-12-02 18:28:53 +0100 | [diff] [blame] | 1529 | |
Joerg Roedel | 5c34c40 | 2013-06-20 20:22:58 +0200 | [diff] [blame] | 1530 | #define DEFINE_FREE_PT_FN(LVL, FN) \ |
| 1531 | static void free_pt_##LVL (unsigned long __pt) \ |
| 1532 | { \ |
| 1533 | unsigned long p; \ |
| 1534 | u64 *pt; \ |
| 1535 | int i; \ |
| 1536 | \ |
| 1537 | pt = (u64 *)__pt; \ |
| 1538 | \ |
| 1539 | for (i = 0; i < 512; ++i) { \ |
Joerg Roedel | 0b3fff5 | 2015-06-18 10:48:34 +0200 | [diff] [blame] | 1540 | /* PTE present? */ \ |
Joerg Roedel | 5c34c40 | 2013-06-20 20:22:58 +0200 | [diff] [blame] | 1541 | if (!IOMMU_PTE_PRESENT(pt[i])) \ |
| 1542 | continue; \ |
| 1543 | \ |
Joerg Roedel | 0b3fff5 | 2015-06-18 10:48:34 +0200 | [diff] [blame] | 1544 | /* Large PTE? */ \ |
| 1545 | if (PM_PTE_LEVEL(pt[i]) == 0 || \ |
| 1546 | PM_PTE_LEVEL(pt[i]) == 7) \ |
| 1547 | continue; \ |
| 1548 | \ |
Joerg Roedel | 5c34c40 | 2013-06-20 20:22:58 +0200 | [diff] [blame] | 1549 | p = (unsigned long)IOMMU_PTE_PAGE(pt[i]); \ |
| 1550 | FN(p); \ |
| 1551 | } \ |
| 1552 | free_page((unsigned long)pt); \ |
| 1553 | } |
| 1554 | |
| 1555 | DEFINE_FREE_PT_FN(l2, free_page) |
| 1556 | DEFINE_FREE_PT_FN(l3, free_pt_l2) |
| 1557 | DEFINE_FREE_PT_FN(l4, free_pt_l3) |
| 1558 | DEFINE_FREE_PT_FN(l5, free_pt_l4) |
| 1559 | DEFINE_FREE_PT_FN(l6, free_pt_l5) |
| 1560 | |
Joerg Roedel | 86db2e5 | 2008-12-02 18:20:21 +0100 | [diff] [blame] | 1561 | static void free_pagetable(struct protection_domain *domain) |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1562 | { |
Joerg Roedel | 5c34c40 | 2013-06-20 20:22:58 +0200 | [diff] [blame] | 1563 | unsigned long root = (unsigned long)domain->pt_root; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1564 | |
Joerg Roedel | 5c34c40 | 2013-06-20 20:22:58 +0200 | [diff] [blame] | 1565 | switch (domain->mode) { |
| 1566 | case PAGE_MODE_NONE: |
| 1567 | break; |
| 1568 | case PAGE_MODE_1_LEVEL: |
| 1569 | free_page(root); |
| 1570 | break; |
| 1571 | case PAGE_MODE_2_LEVEL: |
| 1572 | free_pt_l2(root); |
| 1573 | break; |
| 1574 | case PAGE_MODE_3_LEVEL: |
| 1575 | free_pt_l3(root); |
| 1576 | break; |
| 1577 | case PAGE_MODE_4_LEVEL: |
| 1578 | free_pt_l4(root); |
| 1579 | break; |
| 1580 | case PAGE_MODE_5_LEVEL: |
| 1581 | free_pt_l5(root); |
| 1582 | break; |
| 1583 | case PAGE_MODE_6_LEVEL: |
| 1584 | free_pt_l6(root); |
| 1585 | break; |
| 1586 | default: |
| 1587 | BUG(); |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1588 | } |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
Joerg Roedel | b16137b | 2011-11-21 16:50:23 +0100 | [diff] [blame] | 1591 | static void free_gcr3_tbl_level1(u64 *tbl) |
| 1592 | { |
| 1593 | u64 *ptr; |
| 1594 | int i; |
| 1595 | |
| 1596 | for (i = 0; i < 512; ++i) { |
| 1597 | if (!(tbl[i] & GCR3_VALID)) |
| 1598 | continue; |
| 1599 | |
| 1600 | ptr = __va(tbl[i] & PAGE_MASK); |
| 1601 | |
| 1602 | free_page((unsigned long)ptr); |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | static void free_gcr3_tbl_level2(u64 *tbl) |
| 1607 | { |
| 1608 | u64 *ptr; |
| 1609 | int i; |
| 1610 | |
| 1611 | for (i = 0; i < 512; ++i) { |
| 1612 | if (!(tbl[i] & GCR3_VALID)) |
| 1613 | continue; |
| 1614 | |
| 1615 | ptr = __va(tbl[i] & PAGE_MASK); |
| 1616 | |
| 1617 | free_gcr3_tbl_level1(ptr); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1621 | static void free_gcr3_table(struct protection_domain *domain) |
| 1622 | { |
Joerg Roedel | b16137b | 2011-11-21 16:50:23 +0100 | [diff] [blame] | 1623 | if (domain->glx == 2) |
| 1624 | free_gcr3_tbl_level2(domain->gcr3_tbl); |
| 1625 | else if (domain->glx == 1) |
| 1626 | free_gcr3_tbl_level1(domain->gcr3_tbl); |
Joerg Roedel | 23d3a98 | 2015-08-13 11:15:13 +0200 | [diff] [blame] | 1627 | else |
| 1628 | BUG_ON(domain->glx != 0); |
Joerg Roedel | b16137b | 2011-11-21 16:50:23 +0100 | [diff] [blame] | 1629 | |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1630 | free_page((unsigned long)domain->gcr3_tbl); |
| 1631 | } |
| 1632 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1633 | /* |
| 1634 | * Free a domain, only used if something went wrong in the |
| 1635 | * allocation path and we need to free an already allocated page table |
| 1636 | */ |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1637 | static void dma_ops_domain_free(struct dma_ops_domain *dom) |
| 1638 | { |
| 1639 | if (!dom) |
| 1640 | return; |
| 1641 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 1642 | del_domain_from_list(&dom->domain); |
| 1643 | |
Joerg Roedel | 2d4c515 | 2016-07-05 16:21:32 +0200 | [diff] [blame] | 1644 | put_iova_domain(&dom->iovad); |
| 1645 | |
Joerg Roedel | 86db2e5 | 2008-12-02 18:20:21 +0100 | [diff] [blame] | 1646 | free_pagetable(&dom->domain); |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1647 | |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1648 | kfree(dom); |
| 1649 | } |
| 1650 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1651 | /* |
| 1652 | * Allocates a new protection domain usable for the dma_ops functions. |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1653 | * It also initializes the page table and the address allocator data |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1654 | * structures required for the dma_ops interface |
| 1655 | */ |
Joerg Roedel | 87a64d5 | 2009-11-24 17:26:43 +0100 | [diff] [blame] | 1656 | static struct dma_ops_domain *dma_ops_domain_alloc(void) |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1657 | { |
| 1658 | struct dma_ops_domain *dma_dom; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1659 | |
| 1660 | dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL); |
| 1661 | if (!dma_dom) |
| 1662 | return NULL; |
| 1663 | |
Joerg Roedel | 7a5a566 | 2015-06-30 08:56:11 +0200 | [diff] [blame] | 1664 | if (protection_domain_init(&dma_dom->domain)) |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1665 | goto free_dma_dom; |
Joerg Roedel | 7a5a566 | 2015-06-30 08:56:11 +0200 | [diff] [blame] | 1666 | |
Joerg Roedel | 8f7a017 | 2009-09-02 16:55:24 +0200 | [diff] [blame] | 1667 | dma_dom->domain.mode = PAGE_MODE_2_LEVEL; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1668 | dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL); |
Joerg Roedel | 9fdb19d | 2008-12-02 17:46:25 +0100 | [diff] [blame] | 1669 | dma_dom->domain.flags = PD_DMA_OPS_MASK; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1670 | dma_dom->domain.priv = dma_dom; |
| 1671 | if (!dma_dom->domain.pt_root) |
| 1672 | goto free_dma_dom; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1673 | |
Joerg Roedel | 307d585 | 2016-07-05 11:54:04 +0200 | [diff] [blame] | 1674 | init_iova_domain(&dma_dom->iovad, PAGE_SIZE, |
| 1675 | IOVA_START_PFN, DMA_32BIT_PFN); |
| 1676 | |
Joerg Roedel | 81cd07b | 2016-07-07 18:01:10 +0200 | [diff] [blame] | 1677 | /* Initialize reserved ranges */ |
| 1678 | copy_reserved_iova(&reserved_iova_ranges, &dma_dom->iovad); |
| 1679 | |
Joerg Roedel | 2d4c515 | 2016-07-05 16:21:32 +0200 | [diff] [blame] | 1680 | add_domain_to_list(&dma_dom->domain); |
| 1681 | |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1682 | return dma_dom; |
| 1683 | |
| 1684 | free_dma_dom: |
| 1685 | dma_ops_domain_free(dma_dom); |
| 1686 | |
| 1687 | return NULL; |
| 1688 | } |
| 1689 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1690 | /* |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 1691 | * little helper function to check whether a given protection domain is a |
| 1692 | * dma_ops domain |
| 1693 | */ |
| 1694 | static bool dma_ops_domain(struct protection_domain *domain) |
| 1695 | { |
| 1696 | return domain->flags & PD_DMA_OPS_MASK; |
| 1697 | } |
| 1698 | |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1699 | static void set_dte_entry(u16 devid, struct protection_domain *domain, bool ats) |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 1700 | { |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 1701 | u64 pte_root = 0; |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 1702 | u64 flags = 0; |
Joerg Roedel | 863c74e | 2008-12-02 17:56:36 +0100 | [diff] [blame] | 1703 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 1704 | if (domain->mode != PAGE_MODE_NONE) |
| 1705 | pte_root = virt_to_phys(domain->pt_root); |
| 1706 | |
Joerg Roedel | 38ddf41 | 2008-09-11 10:38:32 +0200 | [diff] [blame] | 1707 | pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) |
| 1708 | << DEV_ENTRY_MODE_SHIFT; |
| 1709 | pte_root |= IOMMU_PTE_IR | IOMMU_PTE_IW | IOMMU_PTE_P | IOMMU_PTE_TV; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 1710 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 1711 | flags = amd_iommu_dev_table[devid].data[1]; |
| 1712 | |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1713 | if (ats) |
| 1714 | flags |= DTE_FLAG_IOTLB; |
| 1715 | |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1716 | if (domain->flags & PD_IOMMUV2_MASK) { |
| 1717 | u64 gcr3 = __pa(domain->gcr3_tbl); |
| 1718 | u64 glx = domain->glx; |
| 1719 | u64 tmp; |
| 1720 | |
| 1721 | pte_root |= DTE_FLAG_GV; |
| 1722 | pte_root |= (glx & DTE_GLX_MASK) << DTE_GLX_SHIFT; |
| 1723 | |
| 1724 | /* First mask out possible old values for GCR3 table */ |
| 1725 | tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B; |
| 1726 | flags &= ~tmp; |
| 1727 | |
| 1728 | tmp = DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C; |
| 1729 | flags &= ~tmp; |
| 1730 | |
| 1731 | /* Encode GCR3 table into DTE */ |
| 1732 | tmp = DTE_GCR3_VAL_A(gcr3) << DTE_GCR3_SHIFT_A; |
| 1733 | pte_root |= tmp; |
| 1734 | |
| 1735 | tmp = DTE_GCR3_VAL_B(gcr3) << DTE_GCR3_SHIFT_B; |
| 1736 | flags |= tmp; |
| 1737 | |
| 1738 | tmp = DTE_GCR3_VAL_C(gcr3) << DTE_GCR3_SHIFT_C; |
| 1739 | flags |= tmp; |
| 1740 | } |
| 1741 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 1742 | flags &= ~(0xffffUL); |
| 1743 | flags |= domain->id; |
| 1744 | |
| 1745 | amd_iommu_dev_table[devid].data[1] = flags; |
| 1746 | amd_iommu_dev_table[devid].data[0] = pte_root; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 1747 | } |
| 1748 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1749 | static void clear_dte_entry(u16 devid) |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1750 | { |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1751 | /* remove entry from the device table seen by the hardware */ |
Joerg Roedel | cbf3ccd | 2015-10-20 14:59:36 +0200 | [diff] [blame] | 1752 | amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV; |
| 1753 | amd_iommu_dev_table[devid].data[1] &= DTE_FLAG_MASK; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1754 | |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 1755 | amd_iommu_apply_erratum_63(devid); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1756 | } |
| 1757 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1758 | static void do_attach(struct iommu_dev_data *dev_data, |
| 1759 | struct protection_domain *domain) |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1760 | { |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1761 | struct amd_iommu *iommu; |
Joerg Roedel | e25bfb5 | 2015-10-20 17:33:38 +0200 | [diff] [blame] | 1762 | u16 alias; |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1763 | bool ats; |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1764 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1765 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 1766 | alias = dev_data->alias; |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1767 | ats = dev_data->ats.enabled; |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1768 | |
| 1769 | /* Update data structures */ |
| 1770 | dev_data->domain = domain; |
| 1771 | list_add(&dev_data->list, &domain->dev_list); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1772 | |
| 1773 | /* Do reference counting */ |
| 1774 | domain->dev_iommu[iommu->index] += 1; |
| 1775 | domain->dev_cnt += 1; |
| 1776 | |
Joerg Roedel | e25bfb5 | 2015-10-20 17:33:38 +0200 | [diff] [blame] | 1777 | /* Update device table */ |
| 1778 | set_dte_entry(dev_data->devid, domain, ats); |
| 1779 | if (alias != dev_data->devid) |
Baoquan He | 9b1a12d | 2016-01-20 22:01:19 +0800 | [diff] [blame] | 1780 | set_dte_entry(alias, domain, ats); |
Joerg Roedel | e25bfb5 | 2015-10-20 17:33:38 +0200 | [diff] [blame] | 1781 | |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1782 | device_flush_dte(dev_data); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1783 | } |
| 1784 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1785 | static void do_detach(struct iommu_dev_data *dev_data) |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1786 | { |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1787 | struct amd_iommu *iommu; |
Joerg Roedel | e25bfb5 | 2015-10-20 17:33:38 +0200 | [diff] [blame] | 1788 | u16 alias; |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1789 | |
Joerg Roedel | 5adad99 | 2015-10-09 16:23:33 +0200 | [diff] [blame] | 1790 | /* |
| 1791 | * First check if the device is still attached. It might already |
| 1792 | * be detached from its domain because the generic |
| 1793 | * iommu_detach_group code detached it and we try again here in |
| 1794 | * our alias handling. |
| 1795 | */ |
| 1796 | if (!dev_data->domain) |
| 1797 | return; |
| 1798 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1799 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | e315604 | 2016-04-08 15:12:24 +0200 | [diff] [blame] | 1800 | alias = dev_data->alias; |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 1801 | |
Joerg Roedel | c459611 | 2009-11-20 14:57:32 +0100 | [diff] [blame] | 1802 | /* decrease reference counters */ |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1803 | dev_data->domain->dev_iommu[iommu->index] -= 1; |
| 1804 | dev_data->domain->dev_cnt -= 1; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1805 | |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1806 | /* Update data structures */ |
| 1807 | dev_data->domain = NULL; |
| 1808 | list_del(&dev_data->list); |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 1809 | clear_dte_entry(dev_data->devid); |
Joerg Roedel | e25bfb5 | 2015-10-20 17:33:38 +0200 | [diff] [blame] | 1810 | if (alias != dev_data->devid) |
| 1811 | clear_dte_entry(alias); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1812 | |
| 1813 | /* Flush the DTE entry */ |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1814 | device_flush_dte(dev_data); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | /* |
| 1818 | * If a device is not yet associated with a domain, this function does |
| 1819 | * assigns it visible for the hardware |
| 1820 | */ |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1821 | static int __attach_device(struct iommu_dev_data *dev_data, |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1822 | struct protection_domain *domain) |
| 1823 | { |
Julia Lawall | 84fe6c1 | 2010-05-27 12:31:51 +0200 | [diff] [blame] | 1824 | int ret; |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 1825 | |
Joerg Roedel | 272e4f9 | 2015-10-20 17:33:37 +0200 | [diff] [blame] | 1826 | /* |
| 1827 | * Must be called with IRQs disabled. Warn here to detect early |
| 1828 | * when its not. |
| 1829 | */ |
| 1830 | WARN_ON(!irqs_disabled()); |
| 1831 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1832 | /* lock domain */ |
| 1833 | spin_lock(&domain->lock); |
| 1834 | |
Joerg Roedel | 397111a | 2014-08-05 17:31:51 +0200 | [diff] [blame] | 1835 | ret = -EBUSY; |
Joerg Roedel | 150952f | 2015-10-20 17:33:35 +0200 | [diff] [blame] | 1836 | if (dev_data->domain != NULL) |
Joerg Roedel | 397111a | 2014-08-05 17:31:51 +0200 | [diff] [blame] | 1837 | goto out_unlock; |
Joerg Roedel | 2410005 | 2009-11-25 15:59:57 +0100 | [diff] [blame] | 1838 | |
Joerg Roedel | 397111a | 2014-08-05 17:31:51 +0200 | [diff] [blame] | 1839 | /* Attach alias group root */ |
Joerg Roedel | 150952f | 2015-10-20 17:33:35 +0200 | [diff] [blame] | 1840 | do_attach(dev_data, domain); |
Joerg Roedel | 2410005 | 2009-11-25 15:59:57 +0100 | [diff] [blame] | 1841 | |
Julia Lawall | 84fe6c1 | 2010-05-27 12:31:51 +0200 | [diff] [blame] | 1842 | ret = 0; |
| 1843 | |
| 1844 | out_unlock: |
| 1845 | |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1846 | /* ready */ |
| 1847 | spin_unlock(&domain->lock); |
Joerg Roedel | 21129f7 | 2009-09-01 11:59:42 +0200 | [diff] [blame] | 1848 | |
Julia Lawall | 84fe6c1 | 2010-05-27 12:31:51 +0200 | [diff] [blame] | 1849 | return ret; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1852 | |
| 1853 | static void pdev_iommuv2_disable(struct pci_dev *pdev) |
| 1854 | { |
| 1855 | pci_disable_ats(pdev); |
| 1856 | pci_disable_pri(pdev); |
| 1857 | pci_disable_pasid(pdev); |
| 1858 | } |
| 1859 | |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 1860 | /* FIXME: Change generic reset-function to do the same */ |
| 1861 | static int pri_reset_while_enabled(struct pci_dev *pdev) |
| 1862 | { |
| 1863 | u16 control; |
| 1864 | int pos; |
| 1865 | |
Joerg Roedel | 46277b7 | 2011-12-07 14:34:02 +0100 | [diff] [blame] | 1866 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 1867 | if (!pos) |
| 1868 | return -EINVAL; |
| 1869 | |
Joerg Roedel | 46277b7 | 2011-12-07 14:34:02 +0100 | [diff] [blame] | 1870 | pci_read_config_word(pdev, pos + PCI_PRI_CTRL, &control); |
| 1871 | control |= PCI_PRI_CTRL_RESET; |
| 1872 | pci_write_config_word(pdev, pos + PCI_PRI_CTRL, control); |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 1873 | |
| 1874 | return 0; |
| 1875 | } |
| 1876 | |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1877 | static int pdev_iommuv2_enable(struct pci_dev *pdev) |
| 1878 | { |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 1879 | bool reset_enable; |
| 1880 | int reqs, ret; |
| 1881 | |
| 1882 | /* FIXME: Hardcode number of outstanding requests for now */ |
| 1883 | reqs = 32; |
| 1884 | if (pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_LIMIT_REQ_ONE)) |
| 1885 | reqs = 1; |
| 1886 | reset_enable = pdev_pri_erratum(pdev, AMD_PRI_DEV_ERRATUM_ENABLE_RESET); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1887 | |
| 1888 | /* Only allow access to user-accessible pages */ |
| 1889 | ret = pci_enable_pasid(pdev, 0); |
| 1890 | if (ret) |
| 1891 | goto out_err; |
| 1892 | |
| 1893 | /* First reset the PRI state of the device */ |
| 1894 | ret = pci_reset_pri(pdev); |
| 1895 | if (ret) |
| 1896 | goto out_err; |
| 1897 | |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 1898 | /* Enable PRI */ |
| 1899 | ret = pci_enable_pri(pdev, reqs); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1900 | if (ret) |
| 1901 | goto out_err; |
| 1902 | |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 1903 | if (reset_enable) { |
| 1904 | ret = pri_reset_while_enabled(pdev); |
| 1905 | if (ret) |
| 1906 | goto out_err; |
| 1907 | } |
| 1908 | |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1909 | ret = pci_enable_ats(pdev, PAGE_SHIFT); |
| 1910 | if (ret) |
| 1911 | goto out_err; |
| 1912 | |
| 1913 | return 0; |
| 1914 | |
| 1915 | out_err: |
| 1916 | pci_disable_pri(pdev); |
| 1917 | pci_disable_pasid(pdev); |
| 1918 | |
| 1919 | return ret; |
| 1920 | } |
| 1921 | |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 1922 | /* FIXME: Move this to PCI code */ |
Joerg Roedel | a3b9312 | 2012-04-12 12:49:26 +0200 | [diff] [blame] | 1923 | #define PCI_PRI_TLP_OFF (1 << 15) |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 1924 | |
Joerg Roedel | 98f1ad2 | 2012-07-06 13:28:37 +0200 | [diff] [blame] | 1925 | static bool pci_pri_tlp_required(struct pci_dev *pdev) |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 1926 | { |
Joerg Roedel | a3b9312 | 2012-04-12 12:49:26 +0200 | [diff] [blame] | 1927 | u16 status; |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 1928 | int pos; |
| 1929 | |
Joerg Roedel | 46277b7 | 2011-12-07 14:34:02 +0100 | [diff] [blame] | 1930 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 1931 | if (!pos) |
| 1932 | return false; |
| 1933 | |
Joerg Roedel | a3b9312 | 2012-04-12 12:49:26 +0200 | [diff] [blame] | 1934 | pci_read_config_word(pdev, pos + PCI_PRI_STATUS, &status); |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 1935 | |
Joerg Roedel | a3b9312 | 2012-04-12 12:49:26 +0200 | [diff] [blame] | 1936 | return (status & PCI_PRI_TLP_OFF) ? true : false; |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 1937 | } |
| 1938 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1939 | /* |
Frank Arnold | df805ab | 2012-08-27 19:21:04 +0200 | [diff] [blame] | 1940 | * If a device is not yet associated with a domain, this function |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1941 | * assigns it visible for the hardware |
| 1942 | */ |
| 1943 | static int attach_device(struct device *dev, |
| 1944 | struct protection_domain *domain) |
| 1945 | { |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 1946 | struct pci_dev *pdev; |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1947 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1948 | unsigned long flags; |
| 1949 | int ret; |
| 1950 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1951 | dev_data = get_dev_data(dev); |
| 1952 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 1953 | if (!dev_is_pci(dev)) |
| 1954 | goto skip_ats_check; |
| 1955 | |
| 1956 | pdev = to_pci_dev(dev); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1957 | if (domain->flags & PD_IOMMUV2_MASK) { |
Joerg Roedel | 02ca202 | 2015-07-28 16:58:49 +0200 | [diff] [blame] | 1958 | if (!dev_data->passthrough) |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1959 | return -EINVAL; |
| 1960 | |
Joerg Roedel | 02ca202 | 2015-07-28 16:58:49 +0200 | [diff] [blame] | 1961 | if (dev_data->iommu_v2) { |
| 1962 | if (pdev_iommuv2_enable(pdev) != 0) |
| 1963 | return -EINVAL; |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1964 | |
Joerg Roedel | 02ca202 | 2015-07-28 16:58:49 +0200 | [diff] [blame] | 1965 | dev_data->ats.enabled = true; |
| 1966 | dev_data->ats.qdep = pci_ats_queue_depth(pdev); |
| 1967 | dev_data->pri_tlp = pci_pri_tlp_required(pdev); |
| 1968 | } |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 1969 | } else if (amd_iommu_iotlb_sup && |
| 1970 | pci_enable_ats(pdev, PAGE_SHIFT) == 0) { |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1971 | dev_data->ats.enabled = true; |
| 1972 | dev_data->ats.qdep = pci_ats_queue_depth(pdev); |
| 1973 | } |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1974 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 1975 | skip_ats_check: |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1976 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1977 | ret = __attach_device(dev_data, domain); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1978 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 1979 | |
| 1980 | /* |
| 1981 | * We might boot into a crash-kernel here. The crashed kernel |
| 1982 | * left the caches in the IOMMU dirty. So we have to flush |
| 1983 | * here to evict all dirty stuff. |
| 1984 | */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1985 | domain_flush_tlb_pde(domain); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1986 | |
| 1987 | return ret; |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * Removes a device from a protection domain (unlocked) |
| 1992 | */ |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1993 | static void __detach_device(struct iommu_dev_data *dev_data) |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1994 | { |
Joerg Roedel | 2ca7627 | 2010-01-22 16:45:31 +0100 | [diff] [blame] | 1995 | struct protection_domain *domain; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1996 | |
Joerg Roedel | 272e4f9 | 2015-10-20 17:33:37 +0200 | [diff] [blame] | 1997 | /* |
| 1998 | * Must be called with IRQs disabled. Warn here to detect early |
| 1999 | * when its not. |
| 2000 | */ |
| 2001 | WARN_ON(!irqs_disabled()); |
| 2002 | |
Joerg Roedel | f34c73f | 2015-10-20 17:33:34 +0200 | [diff] [blame] | 2003 | if (WARN_ON(!dev_data->domain)) |
| 2004 | return; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2005 | |
Joerg Roedel | 2ca7627 | 2010-01-22 16:45:31 +0100 | [diff] [blame] | 2006 | domain = dev_data->domain; |
| 2007 | |
Joerg Roedel | f1dd0a8 | 2015-10-20 17:33:36 +0200 | [diff] [blame] | 2008 | spin_lock(&domain->lock); |
Joerg Roedel | 2410005 | 2009-11-25 15:59:57 +0100 | [diff] [blame] | 2009 | |
Joerg Roedel | 150952f | 2015-10-20 17:33:35 +0200 | [diff] [blame] | 2010 | do_detach(dev_data); |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 2011 | |
Joerg Roedel | f1dd0a8 | 2015-10-20 17:33:36 +0200 | [diff] [blame] | 2012 | spin_unlock(&domain->lock); |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | /* |
| 2016 | * Removes a device from a protection domain (with devtable_lock held) |
| 2017 | */ |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2018 | static void detach_device(struct device *dev) |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 2019 | { |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 2020 | struct protection_domain *domain; |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 2021 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 2022 | unsigned long flags; |
| 2023 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 2024 | dev_data = get_dev_data(dev); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 2025 | domain = dev_data->domain; |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 2026 | |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 2027 | /* lock device table */ |
| 2028 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 2029 | __detach_device(dev_data); |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 2030 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 2031 | |
Wan Zongshun | 2bf9a0a | 2016-04-01 09:06:03 -0400 | [diff] [blame] | 2032 | if (!dev_is_pci(dev)) |
| 2033 | return; |
| 2034 | |
Joerg Roedel | 02ca202 | 2015-07-28 16:58:49 +0200 | [diff] [blame] | 2035 | if (domain->flags & PD_IOMMUV2_MASK && dev_data->iommu_v2) |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 2036 | pdev_iommuv2_disable(to_pci_dev(dev)); |
| 2037 | else if (dev_data->ats.enabled) |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 2038 | pci_disable_ats(to_pci_dev(dev)); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 2039 | |
| 2040 | dev_data->ats.enabled = false; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 2041 | } |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2042 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2043 | static int amd_iommu_add_device(struct device *dev) |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2044 | { |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 2045 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 07ee869 | 2015-05-28 18:41:42 +0200 | [diff] [blame] | 2046 | struct iommu_domain *domain; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2047 | struct amd_iommu *iommu; |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2048 | int ret, devid; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2049 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2050 | if (!check_device(dev) || get_dev_data(dev)) |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2051 | return 0; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2052 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2053 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 2054 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2055 | return devid; |
| 2056 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2057 | iommu = amd_iommu_rlookup_table[devid]; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2058 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2059 | ret = iommu_init_device(dev); |
Joerg Roedel | 4d58b8a | 2015-06-11 09:21:39 +0200 | [diff] [blame] | 2060 | if (ret) { |
| 2061 | if (ret != -ENOTSUPP) |
| 2062 | pr_err("Failed to initialize device %s - trying to proceed anyway\n", |
| 2063 | dev_name(dev)); |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2064 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2065 | iommu_ignore_device(dev); |
Joerg Roedel | 343e9ca | 2015-05-28 18:41:43 +0200 | [diff] [blame] | 2066 | dev->archdata.dma_ops = &nommu_dma_ops; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2067 | goto out; |
| 2068 | } |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2069 | init_iommu_group(dev); |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2070 | |
Joerg Roedel | 07ee869 | 2015-05-28 18:41:42 +0200 | [diff] [blame] | 2071 | dev_data = get_dev_data(dev); |
Joerg Roedel | 4d58b8a | 2015-06-11 09:21:39 +0200 | [diff] [blame] | 2072 | |
| 2073 | BUG_ON(!dev_data); |
| 2074 | |
Joerg Roedel | 1e6a7b0 | 2015-07-28 16:58:48 +0200 | [diff] [blame] | 2075 | if (iommu_pass_through || dev_data->iommu_v2) |
Joerg Roedel | 07ee869 | 2015-05-28 18:41:42 +0200 | [diff] [blame] | 2076 | iommu_request_dm_for_dev(dev); |
| 2077 | |
| 2078 | /* Domains are initialized for this device - have a look what we ended up with */ |
| 2079 | domain = iommu_get_domain_for_dev(dev); |
Joerg Roedel | 3230232 | 2015-07-28 16:58:50 +0200 | [diff] [blame] | 2080 | if (domain->type == IOMMU_DOMAIN_IDENTITY) |
Joerg Roedel | 07ee869 | 2015-05-28 18:41:42 +0200 | [diff] [blame] | 2081 | dev_data->passthrough = true; |
Joerg Roedel | 3230232 | 2015-07-28 16:58:50 +0200 | [diff] [blame] | 2082 | else |
Joerg Roedel | 07ee869 | 2015-05-28 18:41:42 +0200 | [diff] [blame] | 2083 | dev->archdata.dma_ops = &amd_iommu_dma_ops; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2084 | |
| 2085 | out: |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2086 | iommu_completion_wait(iommu); |
| 2087 | |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 2088 | return 0; |
| 2089 | } |
| 2090 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2091 | static void amd_iommu_remove_device(struct device *dev) |
Joerg Roedel | 8638c49 | 2009-12-10 11:12:25 +0100 | [diff] [blame] | 2092 | { |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2093 | struct amd_iommu *iommu; |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2094 | int devid; |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2095 | |
| 2096 | if (!check_device(dev)) |
| 2097 | return; |
| 2098 | |
| 2099 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 2100 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2101 | return; |
| 2102 | |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2103 | iommu = amd_iommu_rlookup_table[devid]; |
| 2104 | |
| 2105 | iommu_uninit_device(dev); |
| 2106 | iommu_completion_wait(iommu); |
Joerg Roedel | 8638c49 | 2009-12-10 11:12:25 +0100 | [diff] [blame] | 2107 | } |
| 2108 | |
Wan Zongshun | b097d11 | 2016-04-01 09:06:04 -0400 | [diff] [blame] | 2109 | static struct iommu_group *amd_iommu_device_group(struct device *dev) |
| 2110 | { |
| 2111 | if (dev_is_pci(dev)) |
| 2112 | return pci_device_group(dev); |
| 2113 | |
| 2114 | return acpihid_device_group(dev); |
| 2115 | } |
| 2116 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2117 | /***************************************************************************** |
| 2118 | * |
| 2119 | * The next functions belong to the dma_ops mapping/unmapping code. |
| 2120 | * |
| 2121 | *****************************************************************************/ |
| 2122 | |
| 2123 | /* |
| 2124 | * In the dma_ops path we only have the struct device. This function |
| 2125 | * finds the corresponding IOMMU, the protection domain and the |
| 2126 | * requestor id for a given device. |
| 2127 | * If the device is not yet associated with a domain this is also done |
| 2128 | * in this function. |
| 2129 | */ |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2130 | static struct protection_domain *get_domain(struct device *dev) |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2131 | { |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2132 | struct protection_domain *domain; |
Joerg Roedel | 063071d | 2015-05-28 18:41:38 +0200 | [diff] [blame] | 2133 | struct iommu_domain *io_domain; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2134 | |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2135 | if (!check_device(dev)) |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2136 | return ERR_PTR(-EINVAL); |
Joerg Roedel | dbcc112 | 2008-09-04 15:04:26 +0200 | [diff] [blame] | 2137 | |
Joerg Roedel | 063071d | 2015-05-28 18:41:38 +0200 | [diff] [blame] | 2138 | io_domain = iommu_get_domain_for_dev(dev); |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 2139 | if (!io_domain) |
| 2140 | return NULL; |
Joerg Roedel | 063071d | 2015-05-28 18:41:38 +0200 | [diff] [blame] | 2141 | |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 2142 | domain = to_pdomain(io_domain); |
| 2143 | if (!dma_ops_domain(domain)) |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2144 | return ERR_PTR(-EBUSY); |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2145 | |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 2146 | return domain; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2147 | } |
| 2148 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2149 | static void update_device_table(struct protection_domain *domain) |
| 2150 | { |
Joerg Roedel | 492667d | 2009-11-27 13:25:47 +0100 | [diff] [blame] | 2151 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2152 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 2153 | list_for_each_entry(dev_data, &domain->dev_list, list) |
| 2154 | set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled); |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | static void update_domain(struct protection_domain *domain) |
| 2158 | { |
| 2159 | if (!domain->updated) |
| 2160 | return; |
| 2161 | |
| 2162 | update_device_table(domain); |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2163 | |
| 2164 | domain_flush_devices(domain); |
| 2165 | domain_flush_tlb_pde(domain); |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2166 | |
| 2167 | domain->updated = false; |
| 2168 | } |
| 2169 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2170 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2171 | * This function contains common code for mapping of a physically |
Joerg Roedel | 24f8116 | 2008-12-08 14:25:39 +0100 | [diff] [blame] | 2172 | * contiguous memory region into DMA address space. It is used by all |
| 2173 | * mapping functions provided with this IOMMU driver. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2174 | * Must be called with the domain lock held. |
| 2175 | */ |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2176 | static dma_addr_t __map_single(struct device *dev, |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2177 | struct dma_ops_domain *dma_dom, |
| 2178 | phys_addr_t paddr, |
| 2179 | size_t size, |
Joerg Roedel | 518d9b4 | 2016-07-05 14:39:47 +0200 | [diff] [blame] | 2180 | int direction, |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2181 | u64 dma_mask) |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2182 | { |
| 2183 | dma_addr_t offset = paddr & ~PAGE_MASK; |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2184 | dma_addr_t address, start, ret; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2185 | unsigned int pages; |
Joerg Roedel | 518d9b4 | 2016-07-05 14:39:47 +0200 | [diff] [blame] | 2186 | int prot = 0; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2187 | int i; |
| 2188 | |
Joerg Roedel | e3c449f | 2008-10-15 22:02:11 -0700 | [diff] [blame] | 2189 | pages = iommu_num_pages(paddr, size, PAGE_SIZE); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2190 | paddr &= PAGE_MASK; |
| 2191 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 2192 | address = dma_ops_alloc_iova(dev, dma_dom, pages, dma_mask); |
Joerg Roedel | 266a3bd | 2015-12-21 18:54:24 +0100 | [diff] [blame] | 2193 | if (address == DMA_ERROR_CODE) |
| 2194 | goto out; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2195 | |
Joerg Roedel | 518d9b4 | 2016-07-05 14:39:47 +0200 | [diff] [blame] | 2196 | if (direction == DMA_TO_DEVICE) |
| 2197 | prot = IOMMU_PROT_IR; |
| 2198 | else if (direction == DMA_FROM_DEVICE) |
| 2199 | prot = IOMMU_PROT_IW; |
| 2200 | else if (direction == DMA_BIDIRECTIONAL) |
| 2201 | prot = IOMMU_PROT_IW | IOMMU_PROT_IR; |
| 2202 | |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2203 | start = address; |
| 2204 | for (i = 0; i < pages; ++i) { |
Joerg Roedel | 518d9b4 | 2016-07-05 14:39:47 +0200 | [diff] [blame] | 2205 | ret = iommu_map_page(&dma_dom->domain, start, paddr, |
| 2206 | PAGE_SIZE, prot, GFP_ATOMIC); |
| 2207 | if (ret) |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2208 | goto out_unmap; |
| 2209 | |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2210 | paddr += PAGE_SIZE; |
| 2211 | start += PAGE_SIZE; |
| 2212 | } |
| 2213 | address += offset; |
| 2214 | |
Joerg Roedel | ab7032b | 2015-12-21 18:47:11 +0100 | [diff] [blame] | 2215 | if (unlikely(amd_iommu_np_cache)) { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2216 | domain_flush_pages(&dma_dom->domain, address, size); |
Joerg Roedel | ab7032b | 2015-12-21 18:47:11 +0100 | [diff] [blame] | 2217 | domain_flush_complete(&dma_dom->domain); |
| 2218 | } |
Joerg Roedel | 270cab24 | 2008-09-04 15:49:46 +0200 | [diff] [blame] | 2219 | |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2220 | out: |
| 2221 | return address; |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2222 | |
| 2223 | out_unmap: |
| 2224 | |
| 2225 | for (--i; i >= 0; --i) { |
| 2226 | start -= PAGE_SIZE; |
Joerg Roedel | 518d9b4 | 2016-07-05 14:39:47 +0200 | [diff] [blame] | 2227 | iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE); |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 2230 | domain_flush_tlb(&dma_dom->domain); |
| 2231 | domain_flush_complete(&dma_dom->domain); |
| 2232 | |
| 2233 | dma_ops_free_iova(dma_dom, address, pages); |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2234 | |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2235 | return DMA_ERROR_CODE; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2238 | /* |
| 2239 | * Does the reverse of the __map_single function. Must be called with |
| 2240 | * the domain lock held too |
| 2241 | */ |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2242 | static void __unmap_single(struct dma_ops_domain *dma_dom, |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2243 | dma_addr_t dma_addr, |
| 2244 | size_t size, |
| 2245 | int dir) |
| 2246 | { |
Joerg Roedel | 04e0463 | 2010-09-23 16:12:48 +0200 | [diff] [blame] | 2247 | dma_addr_t flush_addr; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2248 | dma_addr_t i, start; |
| 2249 | unsigned int pages; |
| 2250 | |
Joerg Roedel | 04e0463 | 2010-09-23 16:12:48 +0200 | [diff] [blame] | 2251 | flush_addr = dma_addr; |
Joerg Roedel | e3c449f | 2008-10-15 22:02:11 -0700 | [diff] [blame] | 2252 | pages = iommu_num_pages(dma_addr, size, PAGE_SIZE); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2253 | dma_addr &= PAGE_MASK; |
| 2254 | start = dma_addr; |
| 2255 | |
| 2256 | for (i = 0; i < pages; ++i) { |
Joerg Roedel | 518d9b4 | 2016-07-05 14:39:47 +0200 | [diff] [blame] | 2257 | iommu_unmap_page(&dma_dom->domain, start, PAGE_SIZE); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2258 | start += PAGE_SIZE; |
| 2259 | } |
| 2260 | |
Joerg Roedel | 256e462 | 2016-07-05 14:23:01 +0200 | [diff] [blame] | 2261 | domain_flush_tlb(&dma_dom->domain); |
| 2262 | domain_flush_complete(&dma_dom->domain); |
| 2263 | |
| 2264 | dma_ops_free_iova(dma_dom, dma_addr, pages); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2267 | /* |
| 2268 | * The exported map_single function for dma_ops. |
| 2269 | */ |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 2270 | static dma_addr_t map_page(struct device *dev, struct page *page, |
| 2271 | unsigned long offset, size_t size, |
| 2272 | enum dma_data_direction dir, |
| 2273 | struct dma_attrs *attrs) |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2274 | { |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 2275 | phys_addr_t paddr = page_to_phys(page) + offset; |
Joerg Roedel | 92d420e | 2015-12-21 19:31:33 +0100 | [diff] [blame] | 2276 | struct protection_domain *domain; |
| 2277 | u64 dma_mask; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2278 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2279 | domain = get_domain(dev); |
| 2280 | if (PTR_ERR(domain) == -EINVAL) |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2281 | return (dma_addr_t)paddr; |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2282 | else if (IS_ERR(domain)) |
| 2283 | return DMA_ERROR_CODE; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2284 | |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2285 | dma_mask = *dev->dma_mask; |
| 2286 | |
Joerg Roedel | bda350d | 2016-07-05 16:28:02 +0200 | [diff] [blame] | 2287 | return __map_single(dev, domain->priv, paddr, size, dir, dma_mask); |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2288 | } |
| 2289 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2290 | /* |
| 2291 | * The exported unmap_single function for dma_ops. |
| 2292 | */ |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 2293 | static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size, |
| 2294 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2295 | { |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2296 | struct protection_domain *domain; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2297 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2298 | domain = get_domain(dev); |
| 2299 | if (IS_ERR(domain)) |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2300 | return; |
| 2301 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2302 | __unmap_single(domain->priv, dma_addr, size, dir); |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2303 | } |
| 2304 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2305 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2306 | * The exported map_sg function for dma_ops (handles scatter-gather |
| 2307 | * lists). |
| 2308 | */ |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2309 | static int map_sg(struct device *dev, struct scatterlist *sglist, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 2310 | int nelems, enum dma_data_direction dir, |
| 2311 | struct dma_attrs *attrs) |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2312 | { |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2313 | struct protection_domain *domain; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2314 | int i; |
| 2315 | struct scatterlist *s; |
| 2316 | phys_addr_t paddr; |
| 2317 | int mapped_elems = 0; |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2318 | u64 dma_mask; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2319 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2320 | domain = get_domain(dev); |
Joerg Roedel | a0e191b | 2013-04-09 15:04:36 +0200 | [diff] [blame] | 2321 | if (IS_ERR(domain)) |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2322 | return 0; |
Joerg Roedel | dbcc112 | 2008-09-04 15:04:26 +0200 | [diff] [blame] | 2323 | |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2324 | dma_mask = *dev->dma_mask; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2325 | |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2326 | for_each_sg(sglist, s, nelems, i) { |
| 2327 | paddr = sg_phys(s); |
| 2328 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2329 | s->dma_address = __map_single(dev, domain->priv, |
Joerg Roedel | bda350d | 2016-07-05 16:28:02 +0200 | [diff] [blame] | 2330 | paddr, s->length, dir, dma_mask); |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2331 | |
| 2332 | if (s->dma_address) { |
| 2333 | s->dma_length = s->length; |
| 2334 | mapped_elems++; |
| 2335 | } else |
| 2336 | goto unmap; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2337 | } |
| 2338 | |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2339 | return mapped_elems; |
Joerg Roedel | 92d420e | 2015-12-21 19:31:33 +0100 | [diff] [blame] | 2340 | |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2341 | unmap: |
| 2342 | for_each_sg(sglist, s, mapped_elems, i) { |
| 2343 | if (s->dma_address) |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2344 | __unmap_single(domain->priv, s->dma_address, |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2345 | s->dma_length, dir); |
| 2346 | s->dma_address = s->dma_length = 0; |
| 2347 | } |
| 2348 | |
Joerg Roedel | 92d420e | 2015-12-21 19:31:33 +0100 | [diff] [blame] | 2349 | return 0; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2350 | } |
| 2351 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2352 | /* |
| 2353 | * The exported map_sg function for dma_ops (handles scatter-gather |
| 2354 | * lists). |
| 2355 | */ |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2356 | static void unmap_sg(struct device *dev, struct scatterlist *sglist, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 2357 | int nelems, enum dma_data_direction dir, |
| 2358 | struct dma_attrs *attrs) |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2359 | { |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2360 | struct protection_domain *domain; |
| 2361 | struct scatterlist *s; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2362 | int i; |
| 2363 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2364 | domain = get_domain(dev); |
| 2365 | if (IS_ERR(domain)) |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2366 | return; |
| 2367 | |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2368 | for_each_sg(sglist, s, nelems, i) { |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2369 | __unmap_single(domain->priv, s->dma_address, |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2370 | s->dma_length, dir); |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2371 | s->dma_address = s->dma_length = 0; |
| 2372 | } |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2373 | } |
| 2374 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2375 | /* |
| 2376 | * The exported alloc_coherent function for dma_ops. |
| 2377 | */ |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2378 | static void *alloc_coherent(struct device *dev, size_t size, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 2379 | dma_addr_t *dma_addr, gfp_t flag, |
| 2380 | struct dma_attrs *attrs) |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2381 | { |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2382 | u64 dma_mask = dev->coherent_dma_mask; |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2383 | struct protection_domain *domain; |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2384 | struct page *page; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2385 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2386 | domain = get_domain(dev); |
| 2387 | if (PTR_ERR(domain) == -EINVAL) { |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2388 | page = alloc_pages(flag, get_order(size)); |
| 2389 | *dma_addr = page_to_phys(page); |
| 2390 | return page_address(page); |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2391 | } else if (IS_ERR(domain)) |
| 2392 | return NULL; |
Joerg Roedel | dbcc112 | 2008-09-04 15:04:26 +0200 | [diff] [blame] | 2393 | |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2394 | size = PAGE_ALIGN(size); |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2395 | dma_mask = dev->coherent_dma_mask; |
| 2396 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); |
Joerg Roedel | 2d0ec7a | 2015-06-01 17:30:57 +0200 | [diff] [blame] | 2397 | flag |= __GFP_ZERO; |
FUJITA Tomonori | 13d9fea | 2008-09-10 20:19:40 +0900 | [diff] [blame] | 2398 | |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2399 | page = alloc_pages(flag | __GFP_NOWARN, get_order(size)); |
| 2400 | if (!page) { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 2401 | if (!gfpflags_allow_blocking(flag)) |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2402 | return NULL; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2403 | |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2404 | page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT, |
| 2405 | get_order(size)); |
| 2406 | if (!page) |
| 2407 | return NULL; |
| 2408 | } |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2409 | |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2410 | if (!dma_mask) |
| 2411 | dma_mask = *dev->dma_mask; |
| 2412 | |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2413 | *dma_addr = __map_single(dev, domain->priv, page_to_phys(page), |
Joerg Roedel | bda350d | 2016-07-05 16:28:02 +0200 | [diff] [blame] | 2414 | size, DMA_BIDIRECTIONAL, dma_mask); |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2415 | |
Joerg Roedel | 92d420e | 2015-12-21 19:31:33 +0100 | [diff] [blame] | 2416 | if (*dma_addr == DMA_ERROR_CODE) |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2417 | goto out_free; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2418 | |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2419 | return page_address(page); |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2420 | |
| 2421 | out_free: |
| 2422 | |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2423 | if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) |
| 2424 | __free_pages(page, get_order(size)); |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2425 | |
| 2426 | return NULL; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2429 | /* |
| 2430 | * The exported free_coherent function for dma_ops. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2431 | */ |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2432 | static void free_coherent(struct device *dev, size_t size, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 2433 | void *virt_addr, dma_addr_t dma_addr, |
| 2434 | struct dma_attrs *attrs) |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2435 | { |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2436 | struct protection_domain *domain; |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2437 | struct page *page; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2438 | |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2439 | page = virt_to_page(virt_addr); |
| 2440 | size = PAGE_ALIGN(size); |
| 2441 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2442 | domain = get_domain(dev); |
| 2443 | if (IS_ERR(domain)) |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2444 | goto free_mem; |
| 2445 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2446 | __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL); |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2447 | |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2448 | free_mem: |
Joerg Roedel | 3b839a5 | 2015-04-01 14:58:47 +0200 | [diff] [blame] | 2449 | if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) |
| 2450 | __free_pages(page, get_order(size)); |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2451 | } |
| 2452 | |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2453 | /* |
Joerg Roedel | b39ba6a | 2008-09-09 18:40:46 +0200 | [diff] [blame] | 2454 | * This function is called by the DMA layer to find out if we can handle a |
| 2455 | * particular device. It is part of the dma_ops. |
| 2456 | */ |
| 2457 | static int amd_iommu_dma_supported(struct device *dev, u64 mask) |
| 2458 | { |
Joerg Roedel | 420aef8 | 2009-11-23 16:14:57 +0100 | [diff] [blame] | 2459 | return check_device(dev); |
Joerg Roedel | b39ba6a | 2008-09-09 18:40:46 +0200 | [diff] [blame] | 2460 | } |
| 2461 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 2462 | static struct dma_map_ops amd_iommu_dma_ops = { |
Joerg Roedel | a639a8e | 2015-12-22 16:06:49 +0100 | [diff] [blame] | 2463 | .alloc = alloc_coherent, |
| 2464 | .free = free_coherent, |
| 2465 | .map_page = map_page, |
| 2466 | .unmap_page = unmap_page, |
| 2467 | .map_sg = map_sg, |
| 2468 | .unmap_sg = unmap_sg, |
| 2469 | .dma_supported = amd_iommu_dma_supported, |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2470 | }; |
| 2471 | |
Joerg Roedel | 81cd07b | 2016-07-07 18:01:10 +0200 | [diff] [blame] | 2472 | static int init_reserved_iova_ranges(void) |
| 2473 | { |
| 2474 | struct pci_dev *pdev = NULL; |
| 2475 | struct iova *val; |
| 2476 | |
| 2477 | init_iova_domain(&reserved_iova_ranges, PAGE_SIZE, |
| 2478 | IOVA_START_PFN, DMA_32BIT_PFN); |
| 2479 | |
| 2480 | lockdep_set_class(&reserved_iova_ranges.iova_rbtree_lock, |
| 2481 | &reserved_rbtree_key); |
| 2482 | |
| 2483 | /* MSI memory range */ |
| 2484 | val = reserve_iova(&reserved_iova_ranges, |
| 2485 | IOVA_PFN(MSI_RANGE_START), IOVA_PFN(MSI_RANGE_END)); |
| 2486 | if (!val) { |
| 2487 | pr_err("Reserving MSI range failed\n"); |
| 2488 | return -ENOMEM; |
| 2489 | } |
| 2490 | |
| 2491 | /* HT memory range */ |
| 2492 | val = reserve_iova(&reserved_iova_ranges, |
| 2493 | IOVA_PFN(HT_RANGE_START), IOVA_PFN(HT_RANGE_END)); |
| 2494 | if (!val) { |
| 2495 | pr_err("Reserving HT range failed\n"); |
| 2496 | return -ENOMEM; |
| 2497 | } |
| 2498 | |
| 2499 | /* |
| 2500 | * Memory used for PCI resources |
| 2501 | * FIXME: Check whether we can reserve the PCI-hole completly |
| 2502 | */ |
| 2503 | for_each_pci_dev(pdev) { |
| 2504 | int i; |
| 2505 | |
| 2506 | for (i = 0; i < PCI_NUM_RESOURCES; ++i) { |
| 2507 | struct resource *r = &pdev->resource[i]; |
| 2508 | |
| 2509 | if (!(r->flags & IORESOURCE_MEM)) |
| 2510 | continue; |
| 2511 | |
| 2512 | val = reserve_iova(&reserved_iova_ranges, |
| 2513 | IOVA_PFN(r->start), |
| 2514 | IOVA_PFN(r->end)); |
| 2515 | if (!val) { |
| 2516 | pr_err("Reserve pci-resource range failed\n"); |
| 2517 | return -ENOMEM; |
| 2518 | } |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
Joerg Roedel | 3a18404c | 2015-05-28 18:41:45 +0200 | [diff] [blame] | 2525 | int __init amd_iommu_init_api(void) |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 2526 | { |
Joerg Roedel | c5b5da9 | 2016-07-06 11:55:37 +0200 | [diff] [blame^] | 2527 | int ret, cpu, err = 0; |
Joerg Roedel | 307d585 | 2016-07-05 11:54:04 +0200 | [diff] [blame] | 2528 | |
| 2529 | ret = iova_cache_get(); |
| 2530 | if (ret) |
| 2531 | return ret; |
Wan Zongshun | 9a4d3bf | 2016-04-01 09:06:05 -0400 | [diff] [blame] | 2532 | |
Joerg Roedel | 81cd07b | 2016-07-07 18:01:10 +0200 | [diff] [blame] | 2533 | ret = init_reserved_iova_ranges(); |
| 2534 | if (ret) |
| 2535 | return ret; |
| 2536 | |
Joerg Roedel | c5b5da9 | 2016-07-06 11:55:37 +0200 | [diff] [blame^] | 2537 | for_each_possible_cpu(cpu) { |
| 2538 | struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu); |
| 2539 | |
| 2540 | queue->entries = kzalloc(FLUSH_QUEUE_SIZE * |
| 2541 | sizeof(*queue->entries), |
| 2542 | GFP_KERNEL); |
| 2543 | if (!queue->entries) |
| 2544 | goto out_put_iova; |
| 2545 | |
| 2546 | spin_lock_init(&queue->lock); |
| 2547 | } |
| 2548 | |
Wan Zongshun | 9a4d3bf | 2016-04-01 09:06:05 -0400 | [diff] [blame] | 2549 | err = bus_set_iommu(&pci_bus_type, &amd_iommu_ops); |
| 2550 | if (err) |
| 2551 | return err; |
| 2552 | #ifdef CONFIG_ARM_AMBA |
| 2553 | err = bus_set_iommu(&amba_bustype, &amd_iommu_ops); |
| 2554 | if (err) |
| 2555 | return err; |
| 2556 | #endif |
Wan Zongshun | 0076cd3 | 2016-05-10 09:21:01 -0400 | [diff] [blame] | 2557 | err = bus_set_iommu(&platform_bus_type, &amd_iommu_ops); |
| 2558 | if (err) |
| 2559 | return err; |
Wan Zongshun | 9a4d3bf | 2016-04-01 09:06:05 -0400 | [diff] [blame] | 2560 | return 0; |
Joerg Roedel | c5b5da9 | 2016-07-06 11:55:37 +0200 | [diff] [blame^] | 2561 | |
| 2562 | out_put_iova: |
| 2563 | for_each_possible_cpu(cpu) { |
| 2564 | struct flush_queue *queue = per_cpu_ptr(&flush_queue, cpu); |
| 2565 | |
| 2566 | kfree(queue->entries); |
| 2567 | } |
| 2568 | |
| 2569 | return -ENOMEM; |
Joerg Roedel | f532509 | 2010-01-22 17:44:35 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2572 | int __init amd_iommu_init_dma_ops(void) |
| 2573 | { |
Joerg Roedel | 3230232 | 2015-07-28 16:58:50 +0200 | [diff] [blame] | 2574 | swiotlb = iommu_pass_through ? 1 : 0; |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2575 | iommu_detected = 1; |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2576 | |
Joerg Roedel | 5271782 | 2015-07-28 16:58:51 +0200 | [diff] [blame] | 2577 | /* |
| 2578 | * In case we don't initialize SWIOTLB (actually the common case |
| 2579 | * when AMD IOMMU is enabled), make sure there are global |
| 2580 | * dma_ops set as a fall-back for devices not handled by this |
| 2581 | * driver (for example non-PCI devices). |
| 2582 | */ |
| 2583 | if (!swiotlb) |
| 2584 | dma_ops = &nommu_dma_ops; |
| 2585 | |
Joerg Roedel | 62410ee | 2012-06-12 16:42:43 +0200 | [diff] [blame] | 2586 | if (amd_iommu_unmap_flush) |
| 2587 | pr_info("AMD-Vi: IO/TLB flush on unmap enabled\n"); |
| 2588 | else |
| 2589 | pr_info("AMD-Vi: Lazy IO/TLB flushing enabled\n"); |
| 2590 | |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2591 | return 0; |
Joerg Roedel | c5b5da9 | 2016-07-06 11:55:37 +0200 | [diff] [blame^] | 2592 | |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2593 | } |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2594 | |
| 2595 | /***************************************************************************** |
| 2596 | * |
| 2597 | * The following functions belong to the exported interface of AMD IOMMU |
| 2598 | * |
| 2599 | * This interface allows access to lower level functions of the IOMMU |
| 2600 | * like protection domain handling and assignement of devices to domains |
| 2601 | * which is not possible with the dma_ops interface. |
| 2602 | * |
| 2603 | *****************************************************************************/ |
| 2604 | |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2605 | static void cleanup_domain(struct protection_domain *domain) |
| 2606 | { |
Joerg Roedel | 9b29d3c | 2014-08-05 17:50:15 +0200 | [diff] [blame] | 2607 | struct iommu_dev_data *entry; |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2608 | unsigned long flags; |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2609 | |
| 2610 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 2611 | |
Joerg Roedel | 9b29d3c | 2014-08-05 17:50:15 +0200 | [diff] [blame] | 2612 | while (!list_empty(&domain->dev_list)) { |
| 2613 | entry = list_first_entry(&domain->dev_list, |
| 2614 | struct iommu_dev_data, list); |
| 2615 | __detach_device(entry); |
Joerg Roedel | 492667d | 2009-11-27 13:25:47 +0100 | [diff] [blame] | 2616 | } |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2617 | |
| 2618 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 2619 | } |
| 2620 | |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2621 | static void protection_domain_free(struct protection_domain *domain) |
| 2622 | { |
| 2623 | if (!domain) |
| 2624 | return; |
| 2625 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 2626 | del_domain_from_list(domain); |
| 2627 | |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2628 | if (domain->id) |
| 2629 | domain_id_free(domain->id); |
| 2630 | |
| 2631 | kfree(domain); |
| 2632 | } |
| 2633 | |
Joerg Roedel | 7a5a566 | 2015-06-30 08:56:11 +0200 | [diff] [blame] | 2634 | static int protection_domain_init(struct protection_domain *domain) |
| 2635 | { |
| 2636 | spin_lock_init(&domain->lock); |
| 2637 | mutex_init(&domain->api_lock); |
| 2638 | domain->id = domain_id_alloc(); |
| 2639 | if (!domain->id) |
| 2640 | return -ENOMEM; |
| 2641 | INIT_LIST_HEAD(&domain->dev_list); |
| 2642 | |
| 2643 | return 0; |
| 2644 | } |
| 2645 | |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2646 | static struct protection_domain *protection_domain_alloc(void) |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2647 | { |
| 2648 | struct protection_domain *domain; |
| 2649 | |
| 2650 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 2651 | if (!domain) |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2652 | return NULL; |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2653 | |
Joerg Roedel | 7a5a566 | 2015-06-30 08:56:11 +0200 | [diff] [blame] | 2654 | if (protection_domain_init(domain)) |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2655 | goto out_err; |
| 2656 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 2657 | add_domain_to_list(domain); |
| 2658 | |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2659 | return domain; |
| 2660 | |
| 2661 | out_err: |
| 2662 | kfree(domain); |
| 2663 | |
| 2664 | return NULL; |
| 2665 | } |
| 2666 | |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2667 | static struct iommu_domain *amd_iommu_domain_alloc(unsigned type) |
| 2668 | { |
| 2669 | struct protection_domain *pdomain; |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 2670 | struct dma_ops_domain *dma_domain; |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2671 | |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 2672 | switch (type) { |
| 2673 | case IOMMU_DOMAIN_UNMANAGED: |
| 2674 | pdomain = protection_domain_alloc(); |
| 2675 | if (!pdomain) |
| 2676 | return NULL; |
| 2677 | |
| 2678 | pdomain->mode = PAGE_MODE_3_LEVEL; |
| 2679 | pdomain->pt_root = (void *)get_zeroed_page(GFP_KERNEL); |
| 2680 | if (!pdomain->pt_root) { |
| 2681 | protection_domain_free(pdomain); |
| 2682 | return NULL; |
| 2683 | } |
| 2684 | |
| 2685 | pdomain->domain.geometry.aperture_start = 0; |
| 2686 | pdomain->domain.geometry.aperture_end = ~0ULL; |
| 2687 | pdomain->domain.geometry.force_aperture = true; |
| 2688 | |
| 2689 | break; |
| 2690 | case IOMMU_DOMAIN_DMA: |
| 2691 | dma_domain = dma_ops_domain_alloc(); |
| 2692 | if (!dma_domain) { |
| 2693 | pr_err("AMD-Vi: Failed to allocate\n"); |
| 2694 | return NULL; |
| 2695 | } |
| 2696 | pdomain = &dma_domain->domain; |
| 2697 | break; |
Joerg Roedel | 07f643a | 2015-05-28 18:41:41 +0200 | [diff] [blame] | 2698 | case IOMMU_DOMAIN_IDENTITY: |
| 2699 | pdomain = protection_domain_alloc(); |
| 2700 | if (!pdomain) |
| 2701 | return NULL; |
| 2702 | |
| 2703 | pdomain->mode = PAGE_MODE_NONE; |
| 2704 | break; |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 2705 | default: |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2706 | return NULL; |
Joerg Roedel | 0bb6e24 | 2015-05-28 18:41:40 +0200 | [diff] [blame] | 2707 | } |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2708 | |
| 2709 | return &pdomain->domain; |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | static void amd_iommu_domain_free(struct iommu_domain *dom) |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2713 | { |
| 2714 | struct protection_domain *domain; |
| 2715 | |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2716 | if (!dom) |
Joerg Roedel | 98383fc | 2008-12-02 18:34:12 +0100 | [diff] [blame] | 2717 | return; |
| 2718 | |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2719 | domain = to_pdomain(dom); |
| 2720 | |
Joerg Roedel | 98383fc | 2008-12-02 18:34:12 +0100 | [diff] [blame] | 2721 | if (domain->dev_cnt > 0) |
| 2722 | cleanup_domain(domain); |
| 2723 | |
| 2724 | BUG_ON(domain->dev_cnt != 0); |
| 2725 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 2726 | if (domain->mode != PAGE_MODE_NONE) |
| 2727 | free_pagetable(domain); |
Joerg Roedel | 98383fc | 2008-12-02 18:34:12 +0100 | [diff] [blame] | 2728 | |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 2729 | if (domain->flags & PD_IOMMUV2_MASK) |
| 2730 | free_gcr3_table(domain); |
| 2731 | |
Joerg Roedel | 8b408fe | 2010-03-08 14:20:07 +0100 | [diff] [blame] | 2732 | protection_domain_free(domain); |
Joerg Roedel | 98383fc | 2008-12-02 18:34:12 +0100 | [diff] [blame] | 2733 | } |
| 2734 | |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2735 | static void amd_iommu_detach_device(struct iommu_domain *dom, |
| 2736 | struct device *dev) |
| 2737 | { |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2738 | struct iommu_dev_data *dev_data = dev->archdata.iommu; |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2739 | struct amd_iommu *iommu; |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2740 | int devid; |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2741 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2742 | if (!check_device(dev)) |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2743 | return; |
| 2744 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2745 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 2746 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2747 | return; |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2748 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2749 | if (dev_data->domain != NULL) |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2750 | detach_device(dev); |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2751 | |
| 2752 | iommu = amd_iommu_rlookup_table[devid]; |
| 2753 | if (!iommu) |
| 2754 | return; |
| 2755 | |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2756 | iommu_completion_wait(iommu); |
| 2757 | } |
| 2758 | |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2759 | static int amd_iommu_attach_device(struct iommu_domain *dom, |
| 2760 | struct device *dev) |
| 2761 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2762 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2763 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2764 | struct amd_iommu *iommu; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2765 | int ret; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2766 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2767 | if (!check_device(dev)) |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2768 | return -EINVAL; |
| 2769 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2770 | dev_data = dev->archdata.iommu; |
| 2771 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 2772 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2773 | if (!iommu) |
| 2774 | return -EINVAL; |
| 2775 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2776 | if (dev_data->domain) |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2777 | detach_device(dev); |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2778 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2779 | ret = attach_device(dev, domain); |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2780 | |
| 2781 | iommu_completion_wait(iommu); |
| 2782 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2783 | return ret; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2784 | } |
| 2785 | |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2786 | static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova, |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 2787 | phys_addr_t paddr, size_t page_size, int iommu_prot) |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2788 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2789 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2790 | int prot = 0; |
| 2791 | int ret; |
| 2792 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 2793 | if (domain->mode == PAGE_MODE_NONE) |
| 2794 | return -EINVAL; |
| 2795 | |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2796 | if (iommu_prot & IOMMU_READ) |
| 2797 | prot |= IOMMU_PROT_IR; |
| 2798 | if (iommu_prot & IOMMU_WRITE) |
| 2799 | prot |= IOMMU_PROT_IW; |
| 2800 | |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2801 | mutex_lock(&domain->api_lock); |
Joerg Roedel | b911b89 | 2016-07-05 14:29:11 +0200 | [diff] [blame] | 2802 | ret = iommu_map_page(domain, iova, paddr, page_size, prot, GFP_KERNEL); |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2803 | mutex_unlock(&domain->api_lock); |
| 2804 | |
Joerg Roedel | 795e74f | 2010-05-11 17:40:57 +0200 | [diff] [blame] | 2805 | return ret; |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2806 | } |
| 2807 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 2808 | static size_t amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova, |
| 2809 | size_t page_size) |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2810 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2811 | struct protection_domain *domain = to_pdomain(dom); |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 2812 | size_t unmap_size; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2813 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 2814 | if (domain->mode == PAGE_MODE_NONE) |
| 2815 | return -EINVAL; |
| 2816 | |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2817 | mutex_lock(&domain->api_lock); |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2818 | unmap_size = iommu_unmap_page(domain, iova, page_size); |
Joerg Roedel | 795e74f | 2010-05-11 17:40:57 +0200 | [diff] [blame] | 2819 | mutex_unlock(&domain->api_lock); |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2820 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2821 | domain_flush_tlb_pde(domain); |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2822 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 2823 | return unmap_size; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2824 | } |
| 2825 | |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2826 | static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom, |
Varun Sethi | bb5547ac | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 2827 | dma_addr_t iova) |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2828 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2829 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | 3039ca1 | 2015-04-01 14:58:48 +0200 | [diff] [blame] | 2830 | unsigned long offset_mask, pte_pgsize; |
Joerg Roedel | f03152b | 2010-01-21 16:15:24 +0100 | [diff] [blame] | 2831 | u64 *pte, __pte; |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2832 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 2833 | if (domain->mode == PAGE_MODE_NONE) |
| 2834 | return iova; |
| 2835 | |
Joerg Roedel | 3039ca1 | 2015-04-01 14:58:48 +0200 | [diff] [blame] | 2836 | pte = fetch_pte(domain, iova, &pte_pgsize); |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2837 | |
Joerg Roedel | a6d41a4 | 2009-09-02 17:08:55 +0200 | [diff] [blame] | 2838 | if (!pte || !IOMMU_PTE_PRESENT(*pte)) |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2839 | return 0; |
| 2840 | |
Joerg Roedel | b24b1b6 | 2015-04-01 14:58:51 +0200 | [diff] [blame] | 2841 | offset_mask = pte_pgsize - 1; |
| 2842 | __pte = *pte & PM_ADDR_MASK; |
Joerg Roedel | f03152b | 2010-01-21 16:15:24 +0100 | [diff] [blame] | 2843 | |
Joerg Roedel | b24b1b6 | 2015-04-01 14:58:51 +0200 | [diff] [blame] | 2844 | return (__pte & ~offset_mask) | (iova & offset_mask); |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2845 | } |
| 2846 | |
Joerg Roedel | ab63648 | 2014-09-05 10:48:21 +0200 | [diff] [blame] | 2847 | static bool amd_iommu_capable(enum iommu_cap cap) |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 2848 | { |
Joerg Roedel | 80a506b | 2010-07-27 17:14:24 +0200 | [diff] [blame] | 2849 | switch (cap) { |
| 2850 | case IOMMU_CAP_CACHE_COHERENCY: |
Joerg Roedel | ab63648 | 2014-09-05 10:48:21 +0200 | [diff] [blame] | 2851 | return true; |
Joerg Roedel | bdddadc | 2012-07-02 18:38:13 +0200 | [diff] [blame] | 2852 | case IOMMU_CAP_INTR_REMAP: |
Joerg Roedel | ab63648 | 2014-09-05 10:48:21 +0200 | [diff] [blame] | 2853 | return (irq_remapping_enabled == 1); |
Will Deacon | cfdeec2 | 2014-10-27 11:24:48 +0000 | [diff] [blame] | 2854 | case IOMMU_CAP_NOEXEC: |
| 2855 | return false; |
Joerg Roedel | 80a506b | 2010-07-27 17:14:24 +0200 | [diff] [blame] | 2856 | } |
| 2857 | |
Joerg Roedel | ab63648 | 2014-09-05 10:48:21 +0200 | [diff] [blame] | 2858 | return false; |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 2859 | } |
| 2860 | |
Joerg Roedel | 35cf248 | 2015-05-28 18:41:37 +0200 | [diff] [blame] | 2861 | static void amd_iommu_get_dm_regions(struct device *dev, |
| 2862 | struct list_head *head) |
| 2863 | { |
| 2864 | struct unity_map_entry *entry; |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2865 | int devid; |
Joerg Roedel | 35cf248 | 2015-05-28 18:41:37 +0200 | [diff] [blame] | 2866 | |
| 2867 | devid = get_device_id(dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 2868 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 2869 | return; |
Joerg Roedel | 35cf248 | 2015-05-28 18:41:37 +0200 | [diff] [blame] | 2870 | |
| 2871 | list_for_each_entry(entry, &amd_iommu_unity_map, list) { |
| 2872 | struct iommu_dm_region *region; |
| 2873 | |
| 2874 | if (devid < entry->devid_start || devid > entry->devid_end) |
| 2875 | continue; |
| 2876 | |
| 2877 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 2878 | if (!region) { |
| 2879 | pr_err("Out of memory allocating dm-regions for %s\n", |
| 2880 | dev_name(dev)); |
| 2881 | return; |
| 2882 | } |
| 2883 | |
| 2884 | region->start = entry->address_start; |
| 2885 | region->length = entry->address_end - entry->address_start; |
| 2886 | if (entry->prot & IOMMU_PROT_IR) |
| 2887 | region->prot |= IOMMU_READ; |
| 2888 | if (entry->prot & IOMMU_PROT_IW) |
| 2889 | region->prot |= IOMMU_WRITE; |
| 2890 | |
| 2891 | list_add_tail(®ion->list, head); |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | static void amd_iommu_put_dm_regions(struct device *dev, |
| 2896 | struct list_head *head) |
| 2897 | { |
| 2898 | struct iommu_dm_region *entry, *next; |
| 2899 | |
| 2900 | list_for_each_entry_safe(entry, next, head, list) |
| 2901 | kfree(entry); |
| 2902 | } |
| 2903 | |
Joerg Roedel | 8d54d6c | 2016-07-05 13:32:20 +0200 | [diff] [blame] | 2904 | static void amd_iommu_apply_dm_region(struct device *dev, |
| 2905 | struct iommu_domain *domain, |
| 2906 | struct iommu_dm_region *region) |
| 2907 | { |
| 2908 | struct protection_domain *pdomain = to_pdomain(domain); |
| 2909 | struct dma_ops_domain *dma_dom = pdomain->priv; |
| 2910 | unsigned long start, end; |
| 2911 | |
| 2912 | start = IOVA_PFN(region->start); |
| 2913 | end = IOVA_PFN(region->start + region->length); |
| 2914 | |
| 2915 | WARN_ON_ONCE(reserve_iova(&dma_dom->iovad, start, end) == NULL); |
| 2916 | } |
| 2917 | |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 2918 | static const struct iommu_ops amd_iommu_ops = { |
Joerg Roedel | ab63648 | 2014-09-05 10:48:21 +0200 | [diff] [blame] | 2919 | .capable = amd_iommu_capable, |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2920 | .domain_alloc = amd_iommu_domain_alloc, |
| 2921 | .domain_free = amd_iommu_domain_free, |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 2922 | .attach_dev = amd_iommu_attach_device, |
| 2923 | .detach_dev = amd_iommu_detach_device, |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2924 | .map = amd_iommu_map, |
| 2925 | .unmap = amd_iommu_unmap, |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 2926 | .map_sg = default_iommu_map_sg, |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 2927 | .iova_to_phys = amd_iommu_iova_to_phys, |
Joerg Roedel | aafd8ba | 2015-05-28 18:41:39 +0200 | [diff] [blame] | 2928 | .add_device = amd_iommu_add_device, |
| 2929 | .remove_device = amd_iommu_remove_device, |
Wan Zongshun | b097d11 | 2016-04-01 09:06:04 -0400 | [diff] [blame] | 2930 | .device_group = amd_iommu_device_group, |
Joerg Roedel | 35cf248 | 2015-05-28 18:41:37 +0200 | [diff] [blame] | 2931 | .get_dm_regions = amd_iommu_get_dm_regions, |
| 2932 | .put_dm_regions = amd_iommu_put_dm_regions, |
Joerg Roedel | 8d54d6c | 2016-07-05 13:32:20 +0200 | [diff] [blame] | 2933 | .apply_dm_region = amd_iommu_apply_dm_region, |
Ohad Ben-Cohen | aa3de9c | 2011-11-10 11:32:29 +0200 | [diff] [blame] | 2934 | .pgsize_bitmap = AMD_IOMMU_PGSIZES, |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 2935 | }; |
| 2936 | |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2937 | /***************************************************************************** |
| 2938 | * |
| 2939 | * The next functions do a basic initialization of IOMMU for pass through |
| 2940 | * mode |
| 2941 | * |
| 2942 | * In passthrough mode the IOMMU is initialized and enabled but not used for |
| 2943 | * DMA-API translation. |
| 2944 | * |
| 2945 | *****************************************************************************/ |
| 2946 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 2947 | /* IOMMUv2 specific functions */ |
| 2948 | int amd_iommu_register_ppr_notifier(struct notifier_block *nb) |
| 2949 | { |
| 2950 | return atomic_notifier_chain_register(&ppr_notifier, nb); |
| 2951 | } |
| 2952 | EXPORT_SYMBOL(amd_iommu_register_ppr_notifier); |
| 2953 | |
| 2954 | int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb) |
| 2955 | { |
| 2956 | return atomic_notifier_chain_unregister(&ppr_notifier, nb); |
| 2957 | } |
| 2958 | EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier); |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 2959 | |
| 2960 | void amd_iommu_domain_direct_map(struct iommu_domain *dom) |
| 2961 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2962 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame] | 2963 | unsigned long flags; |
| 2964 | |
| 2965 | spin_lock_irqsave(&domain->lock, flags); |
| 2966 | |
| 2967 | /* Update data structure */ |
| 2968 | domain->mode = PAGE_MODE_NONE; |
| 2969 | domain->updated = true; |
| 2970 | |
| 2971 | /* Make changes visible to IOMMUs */ |
| 2972 | update_domain(domain); |
| 2973 | |
| 2974 | /* Page-table is not visible to IOMMU anymore, so free it */ |
| 2975 | free_pagetable(domain); |
| 2976 | |
| 2977 | spin_unlock_irqrestore(&domain->lock, flags); |
| 2978 | } |
| 2979 | EXPORT_SYMBOL(amd_iommu_domain_direct_map); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 2980 | |
| 2981 | int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids) |
| 2982 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 2983 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | 52815b7 | 2011-11-17 17:24:28 +0100 | [diff] [blame] | 2984 | unsigned long flags; |
| 2985 | int levels, ret; |
| 2986 | |
| 2987 | if (pasids <= 0 || pasids > (PASID_MASK + 1)) |
| 2988 | return -EINVAL; |
| 2989 | |
| 2990 | /* Number of GCR3 table levels required */ |
| 2991 | for (levels = 0; (pasids - 1) & ~0x1ff; pasids >>= 9) |
| 2992 | levels += 1; |
| 2993 | |
| 2994 | if (levels > amd_iommu_max_glx_val) |
| 2995 | return -EINVAL; |
| 2996 | |
| 2997 | spin_lock_irqsave(&domain->lock, flags); |
| 2998 | |
| 2999 | /* |
| 3000 | * Save us all sanity checks whether devices already in the |
| 3001 | * domain support IOMMUv2. Just force that the domain has no |
| 3002 | * devices attached when it is switched into IOMMUv2 mode. |
| 3003 | */ |
| 3004 | ret = -EBUSY; |
| 3005 | if (domain->dev_cnt > 0 || domain->flags & PD_IOMMUV2_MASK) |
| 3006 | goto out; |
| 3007 | |
| 3008 | ret = -ENOMEM; |
| 3009 | domain->gcr3_tbl = (void *)get_zeroed_page(GFP_ATOMIC); |
| 3010 | if (domain->gcr3_tbl == NULL) |
| 3011 | goto out; |
| 3012 | |
| 3013 | domain->glx = levels; |
| 3014 | domain->flags |= PD_IOMMUV2_MASK; |
| 3015 | domain->updated = true; |
| 3016 | |
| 3017 | update_domain(domain); |
| 3018 | |
| 3019 | ret = 0; |
| 3020 | |
| 3021 | out: |
| 3022 | spin_unlock_irqrestore(&domain->lock, flags); |
| 3023 | |
| 3024 | return ret; |
| 3025 | } |
| 3026 | EXPORT_SYMBOL(amd_iommu_domain_enable_v2); |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 3027 | |
| 3028 | static int __flush_pasid(struct protection_domain *domain, int pasid, |
| 3029 | u64 address, bool size) |
| 3030 | { |
| 3031 | struct iommu_dev_data *dev_data; |
| 3032 | struct iommu_cmd cmd; |
| 3033 | int i, ret; |
| 3034 | |
| 3035 | if (!(domain->flags & PD_IOMMUV2_MASK)) |
| 3036 | return -EINVAL; |
| 3037 | |
| 3038 | build_inv_iommu_pasid(&cmd, domain->id, pasid, address, size); |
| 3039 | |
| 3040 | /* |
| 3041 | * IOMMU TLB needs to be flushed before Device TLB to |
| 3042 | * prevent device TLB refill from IOMMU TLB |
| 3043 | */ |
| 3044 | for (i = 0; i < amd_iommus_present; ++i) { |
| 3045 | if (domain->dev_iommu[i] == 0) |
| 3046 | continue; |
| 3047 | |
| 3048 | ret = iommu_queue_command(amd_iommus[i], &cmd); |
| 3049 | if (ret != 0) |
| 3050 | goto out; |
| 3051 | } |
| 3052 | |
| 3053 | /* Wait until IOMMU TLB flushes are complete */ |
| 3054 | domain_flush_complete(domain); |
| 3055 | |
| 3056 | /* Now flush device TLBs */ |
| 3057 | list_for_each_entry(dev_data, &domain->dev_list, list) { |
| 3058 | struct amd_iommu *iommu; |
| 3059 | int qdep; |
| 3060 | |
Joerg Roedel | 1c1cc45 | 2015-07-30 11:24:45 +0200 | [diff] [blame] | 3061 | /* |
| 3062 | There might be non-IOMMUv2 capable devices in an IOMMUv2 |
| 3063 | * domain. |
| 3064 | */ |
| 3065 | if (!dev_data->ats.enabled) |
| 3066 | continue; |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 3067 | |
| 3068 | qdep = dev_data->ats.qdep; |
| 3069 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
| 3070 | |
| 3071 | build_inv_iotlb_pasid(&cmd, dev_data->devid, pasid, |
| 3072 | qdep, address, size); |
| 3073 | |
| 3074 | ret = iommu_queue_command(iommu, &cmd); |
| 3075 | if (ret != 0) |
| 3076 | goto out; |
| 3077 | } |
| 3078 | |
| 3079 | /* Wait until all device TLBs are flushed */ |
| 3080 | domain_flush_complete(domain); |
| 3081 | |
| 3082 | ret = 0; |
| 3083 | |
| 3084 | out: |
| 3085 | |
| 3086 | return ret; |
| 3087 | } |
| 3088 | |
| 3089 | static int __amd_iommu_flush_page(struct protection_domain *domain, int pasid, |
| 3090 | u64 address) |
| 3091 | { |
| 3092 | return __flush_pasid(domain, pasid, address, false); |
| 3093 | } |
| 3094 | |
| 3095 | int amd_iommu_flush_page(struct iommu_domain *dom, int pasid, |
| 3096 | u64 address) |
| 3097 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3098 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 3099 | unsigned long flags; |
| 3100 | int ret; |
| 3101 | |
| 3102 | spin_lock_irqsave(&domain->lock, flags); |
| 3103 | ret = __amd_iommu_flush_page(domain, pasid, address); |
| 3104 | spin_unlock_irqrestore(&domain->lock, flags); |
| 3105 | |
| 3106 | return ret; |
| 3107 | } |
| 3108 | EXPORT_SYMBOL(amd_iommu_flush_page); |
| 3109 | |
| 3110 | static int __amd_iommu_flush_tlb(struct protection_domain *domain, int pasid) |
| 3111 | { |
| 3112 | return __flush_pasid(domain, pasid, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, |
| 3113 | true); |
| 3114 | } |
| 3115 | |
| 3116 | int amd_iommu_flush_tlb(struct iommu_domain *dom, int pasid) |
| 3117 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3118 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | 22e266c | 2011-11-21 15:59:08 +0100 | [diff] [blame] | 3119 | unsigned long flags; |
| 3120 | int ret; |
| 3121 | |
| 3122 | spin_lock_irqsave(&domain->lock, flags); |
| 3123 | ret = __amd_iommu_flush_tlb(domain, pasid); |
| 3124 | spin_unlock_irqrestore(&domain->lock, flags); |
| 3125 | |
| 3126 | return ret; |
| 3127 | } |
| 3128 | EXPORT_SYMBOL(amd_iommu_flush_tlb); |
| 3129 | |
Joerg Roedel | b16137b | 2011-11-21 16:50:23 +0100 | [diff] [blame] | 3130 | static u64 *__get_gcr3_pte(u64 *root, int level, int pasid, bool alloc) |
| 3131 | { |
| 3132 | int index; |
| 3133 | u64 *pte; |
| 3134 | |
| 3135 | while (true) { |
| 3136 | |
| 3137 | index = (pasid >> (9 * level)) & 0x1ff; |
| 3138 | pte = &root[index]; |
| 3139 | |
| 3140 | if (level == 0) |
| 3141 | break; |
| 3142 | |
| 3143 | if (!(*pte & GCR3_VALID)) { |
| 3144 | if (!alloc) |
| 3145 | return NULL; |
| 3146 | |
| 3147 | root = (void *)get_zeroed_page(GFP_ATOMIC); |
| 3148 | if (root == NULL) |
| 3149 | return NULL; |
| 3150 | |
| 3151 | *pte = __pa(root) | GCR3_VALID; |
| 3152 | } |
| 3153 | |
| 3154 | root = __va(*pte & PAGE_MASK); |
| 3155 | |
| 3156 | level -= 1; |
| 3157 | } |
| 3158 | |
| 3159 | return pte; |
| 3160 | } |
| 3161 | |
| 3162 | static int __set_gcr3(struct protection_domain *domain, int pasid, |
| 3163 | unsigned long cr3) |
| 3164 | { |
| 3165 | u64 *pte; |
| 3166 | |
| 3167 | if (domain->mode != PAGE_MODE_NONE) |
| 3168 | return -EINVAL; |
| 3169 | |
| 3170 | pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, true); |
| 3171 | if (pte == NULL) |
| 3172 | return -ENOMEM; |
| 3173 | |
| 3174 | *pte = (cr3 & PAGE_MASK) | GCR3_VALID; |
| 3175 | |
| 3176 | return __amd_iommu_flush_tlb(domain, pasid); |
| 3177 | } |
| 3178 | |
| 3179 | static int __clear_gcr3(struct protection_domain *domain, int pasid) |
| 3180 | { |
| 3181 | u64 *pte; |
| 3182 | |
| 3183 | if (domain->mode != PAGE_MODE_NONE) |
| 3184 | return -EINVAL; |
| 3185 | |
| 3186 | pte = __get_gcr3_pte(domain->gcr3_tbl, domain->glx, pasid, false); |
| 3187 | if (pte == NULL) |
| 3188 | return 0; |
| 3189 | |
| 3190 | *pte = 0; |
| 3191 | |
| 3192 | return __amd_iommu_flush_tlb(domain, pasid); |
| 3193 | } |
| 3194 | |
| 3195 | int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, int pasid, |
| 3196 | unsigned long cr3) |
| 3197 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3198 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | b16137b | 2011-11-21 16:50:23 +0100 | [diff] [blame] | 3199 | unsigned long flags; |
| 3200 | int ret; |
| 3201 | |
| 3202 | spin_lock_irqsave(&domain->lock, flags); |
| 3203 | ret = __set_gcr3(domain, pasid, cr3); |
| 3204 | spin_unlock_irqrestore(&domain->lock, flags); |
| 3205 | |
| 3206 | return ret; |
| 3207 | } |
| 3208 | EXPORT_SYMBOL(amd_iommu_domain_set_gcr3); |
| 3209 | |
| 3210 | int amd_iommu_domain_clear_gcr3(struct iommu_domain *dom, int pasid) |
| 3211 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3212 | struct protection_domain *domain = to_pdomain(dom); |
Joerg Roedel | b16137b | 2011-11-21 16:50:23 +0100 | [diff] [blame] | 3213 | unsigned long flags; |
| 3214 | int ret; |
| 3215 | |
| 3216 | spin_lock_irqsave(&domain->lock, flags); |
| 3217 | ret = __clear_gcr3(domain, pasid); |
| 3218 | spin_unlock_irqrestore(&domain->lock, flags); |
| 3219 | |
| 3220 | return ret; |
| 3221 | } |
| 3222 | EXPORT_SYMBOL(amd_iommu_domain_clear_gcr3); |
Joerg Roedel | c99afa2 | 2011-11-21 18:19:25 +0100 | [diff] [blame] | 3223 | |
| 3224 | int amd_iommu_complete_ppr(struct pci_dev *pdev, int pasid, |
| 3225 | int status, int tag) |
| 3226 | { |
| 3227 | struct iommu_dev_data *dev_data; |
| 3228 | struct amd_iommu *iommu; |
| 3229 | struct iommu_cmd cmd; |
| 3230 | |
| 3231 | dev_data = get_dev_data(&pdev->dev); |
| 3232 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
| 3233 | |
| 3234 | build_complete_ppr(&cmd, dev_data->devid, pasid, status, |
| 3235 | tag, dev_data->pri_tlp); |
| 3236 | |
| 3237 | return iommu_queue_command(iommu, &cmd); |
| 3238 | } |
| 3239 | EXPORT_SYMBOL(amd_iommu_complete_ppr); |
Joerg Roedel | f3572db | 2011-11-23 12:36:25 +0100 | [diff] [blame] | 3240 | |
| 3241 | struct iommu_domain *amd_iommu_get_v2_domain(struct pci_dev *pdev) |
| 3242 | { |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3243 | struct protection_domain *pdomain; |
Joerg Roedel | f3572db | 2011-11-23 12:36:25 +0100 | [diff] [blame] | 3244 | |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3245 | pdomain = get_domain(&pdev->dev); |
| 3246 | if (IS_ERR(pdomain)) |
Joerg Roedel | f3572db | 2011-11-23 12:36:25 +0100 | [diff] [blame] | 3247 | return NULL; |
| 3248 | |
| 3249 | /* Only return IOMMUv2 domains */ |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3250 | if (!(pdomain->flags & PD_IOMMUV2_MASK)) |
Joerg Roedel | f3572db | 2011-11-23 12:36:25 +0100 | [diff] [blame] | 3251 | return NULL; |
| 3252 | |
Joerg Roedel | 3f4b87b | 2015-03-26 13:43:07 +0100 | [diff] [blame] | 3253 | return &pdomain->domain; |
Joerg Roedel | f3572db | 2011-11-23 12:36:25 +0100 | [diff] [blame] | 3254 | } |
| 3255 | EXPORT_SYMBOL(amd_iommu_get_v2_domain); |
Joerg Roedel | 6a113dd | 2011-12-01 12:04:58 +0100 | [diff] [blame] | 3256 | |
| 3257 | void amd_iommu_enable_device_erratum(struct pci_dev *pdev, u32 erratum) |
| 3258 | { |
| 3259 | struct iommu_dev_data *dev_data; |
| 3260 | |
| 3261 | if (!amd_iommu_v2_supported()) |
| 3262 | return; |
| 3263 | |
| 3264 | dev_data = get_dev_data(&pdev->dev); |
| 3265 | dev_data->errata |= (1 << erratum); |
| 3266 | } |
| 3267 | EXPORT_SYMBOL(amd_iommu_enable_device_erratum); |
Joerg Roedel | 52efdb8 | 2011-12-07 12:01:36 +0100 | [diff] [blame] | 3268 | |
| 3269 | int amd_iommu_device_info(struct pci_dev *pdev, |
| 3270 | struct amd_iommu_device_info *info) |
| 3271 | { |
| 3272 | int max_pasids; |
| 3273 | int pos; |
| 3274 | |
| 3275 | if (pdev == NULL || info == NULL) |
| 3276 | return -EINVAL; |
| 3277 | |
| 3278 | if (!amd_iommu_v2_supported()) |
| 3279 | return -EINVAL; |
| 3280 | |
| 3281 | memset(info, 0, sizeof(*info)); |
| 3282 | |
| 3283 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS); |
| 3284 | if (pos) |
| 3285 | info->flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP; |
| 3286 | |
| 3287 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI); |
| 3288 | if (pos) |
| 3289 | info->flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP; |
| 3290 | |
| 3291 | pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PASID); |
| 3292 | if (pos) { |
| 3293 | int features; |
| 3294 | |
| 3295 | max_pasids = 1 << (9 * (amd_iommu_max_glx_val + 1)); |
| 3296 | max_pasids = min(max_pasids, (1 << 20)); |
| 3297 | |
| 3298 | info->flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP; |
| 3299 | info->max_pasids = min(pci_max_pasids(pdev), max_pasids); |
| 3300 | |
| 3301 | features = pci_pasid_features(pdev); |
| 3302 | if (features & PCI_PASID_CAP_EXEC) |
| 3303 | info->flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP; |
| 3304 | if (features & PCI_PASID_CAP_PRIV) |
| 3305 | info->flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP; |
| 3306 | } |
| 3307 | |
| 3308 | return 0; |
| 3309 | } |
| 3310 | EXPORT_SYMBOL(amd_iommu_device_info); |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3311 | |
| 3312 | #ifdef CONFIG_IRQ_REMAP |
| 3313 | |
| 3314 | /***************************************************************************** |
| 3315 | * |
| 3316 | * Interrupt Remapping Implementation |
| 3317 | * |
| 3318 | *****************************************************************************/ |
| 3319 | |
| 3320 | union irte { |
| 3321 | u32 val; |
| 3322 | struct { |
| 3323 | u32 valid : 1, |
| 3324 | no_fault : 1, |
| 3325 | int_type : 3, |
| 3326 | rq_eoi : 1, |
| 3327 | dm : 1, |
| 3328 | rsvd_1 : 1, |
| 3329 | destination : 8, |
| 3330 | vector : 8, |
| 3331 | rsvd_2 : 8; |
| 3332 | } fields; |
| 3333 | }; |
| 3334 | |
Jiang Liu | 9c72496 | 2015-04-14 10:29:52 +0800 | [diff] [blame] | 3335 | struct irq_2_irte { |
| 3336 | u16 devid; /* Device ID for IRTE table */ |
| 3337 | u16 index; /* Index into IRTE table*/ |
| 3338 | }; |
| 3339 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3340 | struct amd_ir_data { |
| 3341 | struct irq_2_irte irq_2_irte; |
| 3342 | union irte irte_entry; |
| 3343 | union { |
| 3344 | struct msi_msg msi_entry; |
| 3345 | }; |
| 3346 | }; |
| 3347 | |
| 3348 | static struct irq_chip amd_ir_chip; |
| 3349 | |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3350 | #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6) |
| 3351 | #define DTE_IRQ_REMAP_INTCTL (2ULL << 60) |
| 3352 | #define DTE_IRQ_TABLE_LEN (8ULL << 1) |
| 3353 | #define DTE_IRQ_REMAP_ENABLE 1ULL |
| 3354 | |
| 3355 | static void set_dte_irq_entry(u16 devid, struct irq_remap_table *table) |
| 3356 | { |
| 3357 | u64 dte; |
| 3358 | |
| 3359 | dte = amd_iommu_dev_table[devid].data[2]; |
| 3360 | dte &= ~DTE_IRQ_PHYS_ADDR_MASK; |
| 3361 | dte |= virt_to_phys(table->table); |
| 3362 | dte |= DTE_IRQ_REMAP_INTCTL; |
| 3363 | dte |= DTE_IRQ_TABLE_LEN; |
| 3364 | dte |= DTE_IRQ_REMAP_ENABLE; |
| 3365 | |
| 3366 | amd_iommu_dev_table[devid].data[2] = dte; |
| 3367 | } |
| 3368 | |
| 3369 | #define IRTE_ALLOCATED (~1U) |
| 3370 | |
| 3371 | static struct irq_remap_table *get_irq_table(u16 devid, bool ioapic) |
| 3372 | { |
| 3373 | struct irq_remap_table *table = NULL; |
| 3374 | struct amd_iommu *iommu; |
| 3375 | unsigned long flags; |
| 3376 | u16 alias; |
| 3377 | |
| 3378 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 3379 | |
| 3380 | iommu = amd_iommu_rlookup_table[devid]; |
| 3381 | if (!iommu) |
| 3382 | goto out_unlock; |
| 3383 | |
| 3384 | table = irq_lookup_table[devid]; |
| 3385 | if (table) |
| 3386 | goto out; |
| 3387 | |
| 3388 | alias = amd_iommu_alias_table[devid]; |
| 3389 | table = irq_lookup_table[alias]; |
| 3390 | if (table) { |
| 3391 | irq_lookup_table[devid] = table; |
| 3392 | set_dte_irq_entry(devid, table); |
| 3393 | iommu_flush_dte(iommu, devid); |
| 3394 | goto out; |
| 3395 | } |
| 3396 | |
| 3397 | /* Nothing there yet, allocate new irq remapping table */ |
| 3398 | table = kzalloc(sizeof(*table), GFP_ATOMIC); |
| 3399 | if (!table) |
| 3400 | goto out; |
| 3401 | |
Joerg Roedel | 197887f | 2013-04-09 21:14:08 +0200 | [diff] [blame] | 3402 | /* Initialize table spin-lock */ |
| 3403 | spin_lock_init(&table->lock); |
| 3404 | |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3405 | if (ioapic) |
| 3406 | /* Keep the first 32 indexes free for IOAPIC interrupts */ |
| 3407 | table->min_index = 32; |
| 3408 | |
| 3409 | table->table = kmem_cache_alloc(amd_iommu_irq_cache, GFP_ATOMIC); |
| 3410 | if (!table->table) { |
| 3411 | kfree(table); |
Dan Carpenter | 821f0f6 | 2012-10-02 11:34:40 +0300 | [diff] [blame] | 3412 | table = NULL; |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3413 | goto out; |
| 3414 | } |
| 3415 | |
| 3416 | memset(table->table, 0, MAX_IRQS_PER_TABLE * sizeof(u32)); |
| 3417 | |
| 3418 | if (ioapic) { |
| 3419 | int i; |
| 3420 | |
| 3421 | for (i = 0; i < 32; ++i) |
| 3422 | table->table[i] = IRTE_ALLOCATED; |
| 3423 | } |
| 3424 | |
| 3425 | irq_lookup_table[devid] = table; |
| 3426 | set_dte_irq_entry(devid, table); |
| 3427 | iommu_flush_dte(iommu, devid); |
| 3428 | if (devid != alias) { |
| 3429 | irq_lookup_table[alias] = table; |
Alex Williamson | e028a9e | 2014-04-22 10:08:40 -0600 | [diff] [blame] | 3430 | set_dte_irq_entry(alias, table); |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3431 | iommu_flush_dte(iommu, alias); |
| 3432 | } |
| 3433 | |
| 3434 | out: |
| 3435 | iommu_completion_wait(iommu); |
| 3436 | |
| 3437 | out_unlock: |
| 3438 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 3439 | |
| 3440 | return table; |
| 3441 | } |
| 3442 | |
Jiang Liu | 3c3d4f9 | 2015-04-13 14:11:38 +0800 | [diff] [blame] | 3443 | static int alloc_irq_index(u16 devid, int count) |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3444 | { |
| 3445 | struct irq_remap_table *table; |
| 3446 | unsigned long flags; |
| 3447 | int index, c; |
| 3448 | |
| 3449 | table = get_irq_table(devid, false); |
| 3450 | if (!table) |
| 3451 | return -ENODEV; |
| 3452 | |
| 3453 | spin_lock_irqsave(&table->lock, flags); |
| 3454 | |
| 3455 | /* Scan table for free entries */ |
| 3456 | for (c = 0, index = table->min_index; |
| 3457 | index < MAX_IRQS_PER_TABLE; |
| 3458 | ++index) { |
| 3459 | if (table->table[index] == 0) |
| 3460 | c += 1; |
| 3461 | else |
| 3462 | c = 0; |
| 3463 | |
| 3464 | if (c == count) { |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3465 | for (; c != 0; --c) |
| 3466 | table->table[index - c + 1] = IRTE_ALLOCATED; |
| 3467 | |
| 3468 | index -= count - 1; |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3469 | goto out; |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | index = -ENOSPC; |
| 3474 | |
| 3475 | out: |
| 3476 | spin_unlock_irqrestore(&table->lock, flags); |
| 3477 | |
| 3478 | return index; |
| 3479 | } |
| 3480 | |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3481 | static int modify_irte(u16 devid, int index, union irte irte) |
| 3482 | { |
| 3483 | struct irq_remap_table *table; |
| 3484 | struct amd_iommu *iommu; |
| 3485 | unsigned long flags; |
| 3486 | |
| 3487 | iommu = amd_iommu_rlookup_table[devid]; |
| 3488 | if (iommu == NULL) |
| 3489 | return -EINVAL; |
| 3490 | |
| 3491 | table = get_irq_table(devid, false); |
| 3492 | if (!table) |
| 3493 | return -ENOMEM; |
| 3494 | |
| 3495 | spin_lock_irqsave(&table->lock, flags); |
| 3496 | table->table[index] = irte.val; |
| 3497 | spin_unlock_irqrestore(&table->lock, flags); |
| 3498 | |
| 3499 | iommu_flush_irt(iommu, devid); |
| 3500 | iommu_completion_wait(iommu); |
| 3501 | |
| 3502 | return 0; |
| 3503 | } |
| 3504 | |
| 3505 | static void free_irte(u16 devid, int index) |
| 3506 | { |
| 3507 | struct irq_remap_table *table; |
| 3508 | struct amd_iommu *iommu; |
| 3509 | unsigned long flags; |
| 3510 | |
| 3511 | iommu = amd_iommu_rlookup_table[devid]; |
| 3512 | if (iommu == NULL) |
| 3513 | return; |
| 3514 | |
| 3515 | table = get_irq_table(devid, false); |
| 3516 | if (!table) |
| 3517 | return; |
| 3518 | |
| 3519 | spin_lock_irqsave(&table->lock, flags); |
| 3520 | table->table[index] = 0; |
| 3521 | spin_unlock_irqrestore(&table->lock, flags); |
| 3522 | |
| 3523 | iommu_flush_irt(iommu, devid); |
| 3524 | iommu_completion_wait(iommu); |
| 3525 | } |
| 3526 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3527 | static int get_devid(struct irq_alloc_info *info) |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3528 | { |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3529 | int devid = -1; |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3530 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3531 | switch (info->type) { |
| 3532 | case X86_IRQ_ALLOC_TYPE_IOAPIC: |
| 3533 | devid = get_ioapic_devid(info->ioapic_id); |
| 3534 | break; |
| 3535 | case X86_IRQ_ALLOC_TYPE_HPET: |
| 3536 | devid = get_hpet_devid(info->hpet_id); |
| 3537 | break; |
| 3538 | case X86_IRQ_ALLOC_TYPE_MSI: |
| 3539 | case X86_IRQ_ALLOC_TYPE_MSIX: |
| 3540 | devid = get_device_id(&info->msi_dev->dev); |
| 3541 | break; |
| 3542 | default: |
| 3543 | BUG_ON(1); |
| 3544 | break; |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3545 | } |
| 3546 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3547 | return devid; |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3548 | } |
| 3549 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3550 | static struct irq_domain *get_ir_irq_domain(struct irq_alloc_info *info) |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3551 | { |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3552 | struct amd_iommu *iommu; |
| 3553 | int devid; |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3554 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3555 | if (!info) |
| 3556 | return NULL; |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3557 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3558 | devid = get_devid(info); |
| 3559 | if (devid >= 0) { |
| 3560 | iommu = amd_iommu_rlookup_table[devid]; |
| 3561 | if (iommu) |
| 3562 | return iommu->ir_domain; |
| 3563 | } |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3564 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3565 | return NULL; |
Joerg Roedel | 5527de7 | 2012-06-26 11:17:32 +0200 | [diff] [blame] | 3566 | } |
| 3567 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3568 | static struct irq_domain *get_irq_domain(struct irq_alloc_info *info) |
Joerg Roedel | 0b4d48c | 2012-06-26 14:54:17 +0200 | [diff] [blame] | 3569 | { |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3570 | struct amd_iommu *iommu; |
| 3571 | int devid; |
Joerg Roedel | 0b4d48c | 2012-06-26 14:54:17 +0200 | [diff] [blame] | 3572 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3573 | if (!info) |
| 3574 | return NULL; |
Joerg Roedel | 0b4d48c | 2012-06-26 14:54:17 +0200 | [diff] [blame] | 3575 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3576 | switch (info->type) { |
| 3577 | case X86_IRQ_ALLOC_TYPE_MSI: |
| 3578 | case X86_IRQ_ALLOC_TYPE_MSIX: |
| 3579 | devid = get_device_id(&info->msi_dev->dev); |
Joerg Roedel | 9ee35e4 | 2016-04-21 18:21:31 +0200 | [diff] [blame] | 3580 | if (devid < 0) |
Wan Zongshun | 7aba6cb | 2016-04-01 09:06:02 -0400 | [diff] [blame] | 3581 | return NULL; |
| 3582 | |
Dan Carpenter | 1fb260b | 2016-01-07 12:36:06 +0300 | [diff] [blame] | 3583 | iommu = amd_iommu_rlookup_table[devid]; |
| 3584 | if (iommu) |
| 3585 | return iommu->msi_domain; |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3586 | break; |
| 3587 | default: |
| 3588 | break; |
| 3589 | } |
Joerg Roedel | 0b4d48c | 2012-06-26 14:54:17 +0200 | [diff] [blame] | 3590 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3591 | return NULL; |
Joerg Roedel | d976195 | 2012-06-26 16:00:08 +0200 | [diff] [blame] | 3592 | } |
| 3593 | |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 3594 | struct irq_remap_ops amd_iommu_irq_ops = { |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 3595 | .prepare = amd_iommu_prepare, |
| 3596 | .enable = amd_iommu_enable, |
| 3597 | .disable = amd_iommu_disable, |
| 3598 | .reenable = amd_iommu_reenable, |
| 3599 | .enable_faulting = amd_iommu_enable_faulting, |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3600 | .get_ir_irq_domain = get_ir_irq_domain, |
| 3601 | .get_irq_domain = get_irq_domain, |
Joerg Roedel | 6b474b8 | 2012-06-26 16:46:04 +0200 | [diff] [blame] | 3602 | }; |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3603 | |
| 3604 | static void irq_remapping_prepare_irte(struct amd_ir_data *data, |
| 3605 | struct irq_cfg *irq_cfg, |
| 3606 | struct irq_alloc_info *info, |
| 3607 | int devid, int index, int sub_handle) |
| 3608 | { |
| 3609 | struct irq_2_irte *irte_info = &data->irq_2_irte; |
| 3610 | struct msi_msg *msg = &data->msi_entry; |
| 3611 | union irte *irte = &data->irte_entry; |
| 3612 | struct IO_APIC_route_entry *entry; |
| 3613 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3614 | data->irq_2_irte.devid = devid; |
| 3615 | data->irq_2_irte.index = index + sub_handle; |
| 3616 | |
| 3617 | /* Setup IRTE for IOMMU */ |
| 3618 | irte->val = 0; |
| 3619 | irte->fields.vector = irq_cfg->vector; |
| 3620 | irte->fields.int_type = apic->irq_delivery_mode; |
| 3621 | irte->fields.destination = irq_cfg->dest_apicid; |
| 3622 | irte->fields.dm = apic->irq_dest_mode; |
| 3623 | irte->fields.valid = 1; |
| 3624 | |
| 3625 | switch (info->type) { |
| 3626 | case X86_IRQ_ALLOC_TYPE_IOAPIC: |
| 3627 | /* Setup IOAPIC entry */ |
| 3628 | entry = info->ioapic_entry; |
| 3629 | info->ioapic_entry = NULL; |
| 3630 | memset(entry, 0, sizeof(*entry)); |
| 3631 | entry->vector = index; |
| 3632 | entry->mask = 0; |
| 3633 | entry->trigger = info->ioapic_trigger; |
| 3634 | entry->polarity = info->ioapic_polarity; |
| 3635 | /* Mask level triggered irqs. */ |
| 3636 | if (info->ioapic_trigger) |
| 3637 | entry->mask = 1; |
| 3638 | break; |
| 3639 | |
| 3640 | case X86_IRQ_ALLOC_TYPE_HPET: |
| 3641 | case X86_IRQ_ALLOC_TYPE_MSI: |
| 3642 | case X86_IRQ_ALLOC_TYPE_MSIX: |
| 3643 | msg->address_hi = MSI_ADDR_BASE_HI; |
| 3644 | msg->address_lo = MSI_ADDR_BASE_LO; |
| 3645 | msg->data = irte_info->index; |
| 3646 | break; |
| 3647 | |
| 3648 | default: |
| 3649 | BUG_ON(1); |
| 3650 | break; |
| 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq, |
| 3655 | unsigned int nr_irqs, void *arg) |
| 3656 | { |
| 3657 | struct irq_alloc_info *info = arg; |
| 3658 | struct irq_data *irq_data; |
| 3659 | struct amd_ir_data *data; |
| 3660 | struct irq_cfg *cfg; |
| 3661 | int i, ret, devid; |
| 3662 | int index = -1; |
| 3663 | |
| 3664 | if (!info) |
| 3665 | return -EINVAL; |
| 3666 | if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_MSI && |
| 3667 | info->type != X86_IRQ_ALLOC_TYPE_MSIX) |
| 3668 | return -EINVAL; |
| 3669 | |
| 3670 | /* |
| 3671 | * With IRQ remapping enabled, don't need contiguous CPU vectors |
| 3672 | * to support multiple MSI interrupts. |
| 3673 | */ |
| 3674 | if (info->type == X86_IRQ_ALLOC_TYPE_MSI) |
| 3675 | info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS; |
| 3676 | |
| 3677 | devid = get_devid(info); |
| 3678 | if (devid < 0) |
| 3679 | return -EINVAL; |
| 3680 | |
| 3681 | ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg); |
| 3682 | if (ret < 0) |
| 3683 | return ret; |
| 3684 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3685 | if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) { |
| 3686 | if (get_irq_table(devid, true)) |
| 3687 | index = info->ioapic_pin; |
| 3688 | else |
| 3689 | ret = -ENOMEM; |
| 3690 | } else { |
Jiang Liu | 3c3d4f9 | 2015-04-13 14:11:38 +0800 | [diff] [blame] | 3691 | index = alloc_irq_index(devid, nr_irqs); |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3692 | } |
| 3693 | if (index < 0) { |
| 3694 | pr_warn("Failed to allocate IRTE\n"); |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3695 | goto out_free_parent; |
| 3696 | } |
| 3697 | |
| 3698 | for (i = 0; i < nr_irqs; i++) { |
| 3699 | irq_data = irq_domain_get_irq_data(domain, virq + i); |
| 3700 | cfg = irqd_cfg(irq_data); |
| 3701 | if (!irq_data || !cfg) { |
| 3702 | ret = -EINVAL; |
| 3703 | goto out_free_data; |
| 3704 | } |
| 3705 | |
Joerg Roedel | a130e69 | 2015-08-13 11:07:25 +0200 | [diff] [blame] | 3706 | ret = -ENOMEM; |
| 3707 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 3708 | if (!data) |
| 3709 | goto out_free_data; |
| 3710 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3711 | irq_data->hwirq = (devid << 16) + i; |
| 3712 | irq_data->chip_data = data; |
| 3713 | irq_data->chip = &amd_ir_chip; |
| 3714 | irq_remapping_prepare_irte(data, cfg, info, devid, index, i); |
| 3715 | irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT); |
| 3716 | } |
Joerg Roedel | a130e69 | 2015-08-13 11:07:25 +0200 | [diff] [blame] | 3717 | |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3718 | return 0; |
| 3719 | |
| 3720 | out_free_data: |
| 3721 | for (i--; i >= 0; i--) { |
| 3722 | irq_data = irq_domain_get_irq_data(domain, virq + i); |
| 3723 | if (irq_data) |
| 3724 | kfree(irq_data->chip_data); |
| 3725 | } |
| 3726 | for (i = 0; i < nr_irqs; i++) |
| 3727 | free_irte(devid, index + i); |
| 3728 | out_free_parent: |
| 3729 | irq_domain_free_irqs_common(domain, virq, nr_irqs); |
| 3730 | return ret; |
| 3731 | } |
| 3732 | |
| 3733 | static void irq_remapping_free(struct irq_domain *domain, unsigned int virq, |
| 3734 | unsigned int nr_irqs) |
| 3735 | { |
| 3736 | struct irq_2_irte *irte_info; |
| 3737 | struct irq_data *irq_data; |
| 3738 | struct amd_ir_data *data; |
| 3739 | int i; |
| 3740 | |
| 3741 | for (i = 0; i < nr_irqs; i++) { |
| 3742 | irq_data = irq_domain_get_irq_data(domain, virq + i); |
| 3743 | if (irq_data && irq_data->chip_data) { |
| 3744 | data = irq_data->chip_data; |
| 3745 | irte_info = &data->irq_2_irte; |
| 3746 | free_irte(irte_info->devid, irte_info->index); |
| 3747 | kfree(data); |
| 3748 | } |
| 3749 | } |
| 3750 | irq_domain_free_irqs_common(domain, virq, nr_irqs); |
| 3751 | } |
| 3752 | |
| 3753 | static void irq_remapping_activate(struct irq_domain *domain, |
| 3754 | struct irq_data *irq_data) |
| 3755 | { |
| 3756 | struct amd_ir_data *data = irq_data->chip_data; |
| 3757 | struct irq_2_irte *irte_info = &data->irq_2_irte; |
| 3758 | |
| 3759 | modify_irte(irte_info->devid, irte_info->index, data->irte_entry); |
| 3760 | } |
| 3761 | |
| 3762 | static void irq_remapping_deactivate(struct irq_domain *domain, |
| 3763 | struct irq_data *irq_data) |
| 3764 | { |
| 3765 | struct amd_ir_data *data = irq_data->chip_data; |
| 3766 | struct irq_2_irte *irte_info = &data->irq_2_irte; |
| 3767 | union irte entry; |
| 3768 | |
| 3769 | entry.val = 0; |
| 3770 | modify_irte(irte_info->devid, irte_info->index, data->irte_entry); |
| 3771 | } |
| 3772 | |
| 3773 | static struct irq_domain_ops amd_ir_domain_ops = { |
| 3774 | .alloc = irq_remapping_alloc, |
| 3775 | .free = irq_remapping_free, |
| 3776 | .activate = irq_remapping_activate, |
| 3777 | .deactivate = irq_remapping_deactivate, |
| 3778 | }; |
| 3779 | |
| 3780 | static int amd_ir_set_affinity(struct irq_data *data, |
| 3781 | const struct cpumask *mask, bool force) |
| 3782 | { |
| 3783 | struct amd_ir_data *ir_data = data->chip_data; |
| 3784 | struct irq_2_irte *irte_info = &ir_data->irq_2_irte; |
| 3785 | struct irq_cfg *cfg = irqd_cfg(data); |
| 3786 | struct irq_data *parent = data->parent_data; |
| 3787 | int ret; |
| 3788 | |
| 3789 | ret = parent->chip->irq_set_affinity(parent, mask, force); |
| 3790 | if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE) |
| 3791 | return ret; |
| 3792 | |
| 3793 | /* |
| 3794 | * Atomically updates the IRTE with the new destination, vector |
| 3795 | * and flushes the interrupt entry cache. |
| 3796 | */ |
| 3797 | ir_data->irte_entry.fields.vector = cfg->vector; |
| 3798 | ir_data->irte_entry.fields.destination = cfg->dest_apicid; |
| 3799 | modify_irte(irte_info->devid, irte_info->index, ir_data->irte_entry); |
| 3800 | |
| 3801 | /* |
| 3802 | * After this point, all the interrupts will start arriving |
| 3803 | * at the new destination. So, time to cleanup the previous |
| 3804 | * vector allocation. |
| 3805 | */ |
Jiang Liu | c6c2002 | 2015-04-14 10:30:02 +0800 | [diff] [blame] | 3806 | send_cleanup_vector(cfg); |
Jiang Liu | 7c71d30 | 2015-04-13 14:11:33 +0800 | [diff] [blame] | 3807 | |
| 3808 | return IRQ_SET_MASK_OK_DONE; |
| 3809 | } |
| 3810 | |
| 3811 | static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg) |
| 3812 | { |
| 3813 | struct amd_ir_data *ir_data = irq_data->chip_data; |
| 3814 | |
| 3815 | *msg = ir_data->msi_entry; |
| 3816 | } |
| 3817 | |
| 3818 | static struct irq_chip amd_ir_chip = { |
| 3819 | .irq_ack = ir_ack_apic_edge, |
| 3820 | .irq_set_affinity = amd_ir_set_affinity, |
| 3821 | .irq_compose_msi_msg = ir_compose_msi_msg, |
| 3822 | }; |
| 3823 | |
| 3824 | int amd_iommu_create_irq_domain(struct amd_iommu *iommu) |
| 3825 | { |
| 3826 | iommu->ir_domain = irq_domain_add_tree(NULL, &amd_ir_domain_ops, iommu); |
| 3827 | if (!iommu->ir_domain) |
| 3828 | return -ENOMEM; |
| 3829 | |
| 3830 | iommu->ir_domain->parent = arch_get_ir_parent_domain(); |
| 3831 | iommu->msi_domain = arch_create_msi_irq_domain(iommu->ir_domain); |
| 3832 | |
| 3833 | return 0; |
| 3834 | } |
Joerg Roedel | 2b32450 | 2012-06-21 16:29:10 +0200 | [diff] [blame] | 3835 | #endif |