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