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