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