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