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 | 17f5b56 | 2011-07-06 17:14:44 +0200 | [diff] [blame] | 34 | #include <asm/msidef.h> |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 35 | #include <asm/proto.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 36 | #include <asm/iommu.h> |
Joerg Roedel | 1d9b16d | 2008-11-27 18:39:15 +0100 | [diff] [blame] | 37 | #include <asm/gart.h> |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 38 | #include <asm/dma.h> |
Joerg Roedel | 403f81d | 2011-06-14 16:44:25 +0200 | [diff] [blame] | 39 | |
| 40 | #include "amd_iommu_proto.h" |
| 41 | #include "amd_iommu_types.h" |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 42 | |
| 43 | #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) |
| 44 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 45 | #define LOOP_TIMEOUT 100000 |
Joerg Roedel | 136f78a | 2008-07-11 17:14:27 +0200 | [diff] [blame] | 46 | |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 47 | static DEFINE_RWLOCK(amd_iommu_devtable_lock); |
| 48 | |
Joerg Roedel | bd60b73 | 2008-09-11 10:24:48 +0200 | [diff] [blame] | 49 | /* A list of preallocated protection domains */ |
| 50 | static LIST_HEAD(iommu_pd_list); |
| 51 | static DEFINE_SPINLOCK(iommu_pd_list_lock); |
| 52 | |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 53 | /* List of all available dev_data structures */ |
| 54 | static LIST_HEAD(dev_data_list); |
| 55 | static DEFINE_SPINLOCK(dev_data_list_lock); |
| 56 | |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 57 | /* |
| 58 | * Domain for untranslated devices - only allocated |
| 59 | * if iommu=pt passed on kernel cmd line. |
| 60 | */ |
| 61 | static struct protection_domain *pt_domain; |
| 62 | |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 63 | static struct iommu_ops amd_iommu_ops; |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 64 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 65 | static ATOMIC_NOTIFIER_HEAD(ppr_notifier); |
| 66 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 67 | /* |
| 68 | * general struct to manage commands send to an IOMMU |
| 69 | */ |
Joerg Roedel | d644953 | 2008-07-11 17:14:28 +0200 | [diff] [blame] | 70 | struct iommu_cmd { |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 71 | u32 data[4]; |
| 72 | }; |
| 73 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 74 | static void update_domain(struct protection_domain *domain); |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 75 | static int __init alloc_passthrough_domain(void); |
Chris Wright | c1eee67 | 2009-05-21 00:56:58 -0700 | [diff] [blame] | 76 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 77 | /**************************************************************************** |
| 78 | * |
| 79 | * Helper functions |
| 80 | * |
| 81 | ****************************************************************************/ |
| 82 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 83 | static struct iommu_dev_data *alloc_dev_data(u16 devid) |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 84 | { |
| 85 | struct iommu_dev_data *dev_data; |
| 86 | unsigned long flags; |
| 87 | |
| 88 | dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL); |
| 89 | if (!dev_data) |
| 90 | return NULL; |
| 91 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 92 | dev_data->devid = devid; |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 93 | atomic_set(&dev_data->bind, 0); |
| 94 | |
| 95 | spin_lock_irqsave(&dev_data_list_lock, flags); |
| 96 | list_add_tail(&dev_data->dev_data_list, &dev_data_list); |
| 97 | spin_unlock_irqrestore(&dev_data_list_lock, flags); |
| 98 | |
| 99 | return dev_data; |
| 100 | } |
| 101 | |
| 102 | static void free_dev_data(struct iommu_dev_data *dev_data) |
| 103 | { |
| 104 | unsigned long flags; |
| 105 | |
| 106 | spin_lock_irqsave(&dev_data_list_lock, flags); |
| 107 | list_del(&dev_data->dev_data_list); |
| 108 | spin_unlock_irqrestore(&dev_data_list_lock, flags); |
| 109 | |
| 110 | kfree(dev_data); |
| 111 | } |
| 112 | |
Joerg Roedel | 3b03bb7 | 2011-06-09 18:53:25 +0200 | [diff] [blame] | 113 | static struct iommu_dev_data *search_dev_data(u16 devid) |
| 114 | { |
| 115 | struct iommu_dev_data *dev_data; |
| 116 | unsigned long flags; |
| 117 | |
| 118 | spin_lock_irqsave(&dev_data_list_lock, flags); |
| 119 | list_for_each_entry(dev_data, &dev_data_list, dev_data_list) { |
| 120 | if (dev_data->devid == devid) |
| 121 | goto out_unlock; |
| 122 | } |
| 123 | |
| 124 | dev_data = NULL; |
| 125 | |
| 126 | out_unlock: |
| 127 | spin_unlock_irqrestore(&dev_data_list_lock, flags); |
| 128 | |
| 129 | return dev_data; |
| 130 | } |
| 131 | |
| 132 | static struct iommu_dev_data *find_dev_data(u16 devid) |
| 133 | { |
| 134 | struct iommu_dev_data *dev_data; |
| 135 | |
| 136 | dev_data = search_dev_data(devid); |
| 137 | |
| 138 | if (dev_data == NULL) |
| 139 | dev_data = alloc_dev_data(devid); |
| 140 | |
| 141 | return dev_data; |
| 142 | } |
| 143 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 144 | static inline u16 get_device_id(struct device *dev) |
| 145 | { |
| 146 | struct pci_dev *pdev = to_pci_dev(dev); |
| 147 | |
| 148 | return calc_devid(pdev->bus->number, pdev->devfn); |
| 149 | } |
| 150 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 151 | static struct iommu_dev_data *get_dev_data(struct device *dev) |
| 152 | { |
| 153 | return dev->archdata.iommu; |
| 154 | } |
| 155 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 156 | static bool pci_iommuv2_capable(struct pci_dev *pdev) |
| 157 | { |
| 158 | static const int caps[] = { |
| 159 | PCI_EXT_CAP_ID_ATS, |
| 160 | PCI_PRI_CAP, |
| 161 | PCI_PASID_CAP, |
| 162 | }; |
| 163 | int i, pos; |
| 164 | |
| 165 | for (i = 0; i < 3; ++i) { |
| 166 | pos = pci_find_ext_capability(pdev, caps[i]); |
| 167 | if (pos == 0) |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
Joerg Roedel | 71c7098 | 2009-11-24 16:43:06 +0100 | [diff] [blame] | 174 | /* |
| 175 | * In this function the list of preallocated protection domains is traversed to |
| 176 | * find the domain for a specific device |
| 177 | */ |
| 178 | static struct dma_ops_domain *find_protection_domain(u16 devid) |
| 179 | { |
| 180 | struct dma_ops_domain *entry, *ret = NULL; |
| 181 | unsigned long flags; |
| 182 | u16 alias = amd_iommu_alias_table[devid]; |
| 183 | |
| 184 | if (list_empty(&iommu_pd_list)) |
| 185 | return NULL; |
| 186 | |
| 187 | spin_lock_irqsave(&iommu_pd_list_lock, flags); |
| 188 | |
| 189 | list_for_each_entry(entry, &iommu_pd_list, list) { |
| 190 | if (entry->target_dev == devid || |
| 191 | entry->target_dev == alias) { |
| 192 | ret = entry; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | spin_unlock_irqrestore(&iommu_pd_list_lock, flags); |
| 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 202 | /* |
| 203 | * This function checks if the driver got a valid device from the caller to |
| 204 | * avoid dereferencing invalid pointers. |
| 205 | */ |
| 206 | static bool check_device(struct device *dev) |
| 207 | { |
| 208 | u16 devid; |
| 209 | |
| 210 | if (!dev || !dev->dma_mask) |
| 211 | return false; |
| 212 | |
| 213 | /* No device or no PCI device */ |
Julia Lawall | 339d326 | 2010-02-06 09:42:39 +0100 | [diff] [blame] | 214 | if (dev->bus != &pci_bus_type) |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 215 | return false; |
| 216 | |
| 217 | devid = get_device_id(dev); |
| 218 | |
| 219 | /* Out of our scope? */ |
| 220 | if (devid > amd_iommu_last_bdf) |
| 221 | return false; |
| 222 | |
| 223 | if (amd_iommu_rlookup_table[devid] == NULL) |
| 224 | return false; |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 229 | static int iommu_init_device(struct device *dev) |
| 230 | { |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 231 | struct pci_dev *pdev = to_pci_dev(dev); |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 232 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 233 | u16 alias; |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 234 | |
| 235 | if (dev->archdata.iommu) |
| 236 | return 0; |
| 237 | |
Joerg Roedel | 3b03bb7 | 2011-06-09 18:53:25 +0200 | [diff] [blame] | 238 | dev_data = find_dev_data(get_device_id(dev)); |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 239 | if (!dev_data) |
| 240 | return -ENOMEM; |
| 241 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 242 | alias = amd_iommu_alias_table[dev_data->devid]; |
Joerg Roedel | 2b02b09 | 2011-06-09 17:48:39 +0200 | [diff] [blame] | 243 | if (alias != dev_data->devid) { |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 244 | struct iommu_dev_data *alias_data; |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 245 | |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 246 | alias_data = find_dev_data(alias); |
| 247 | if (alias_data == NULL) { |
| 248 | pr_err("AMD-Vi: Warning: Unhandled device %s\n", |
| 249 | dev_name(dev)); |
Joerg Roedel | 2b02b09 | 2011-06-09 17:48:39 +0200 | [diff] [blame] | 250 | free_dev_data(dev_data); |
| 251 | return -ENOTSUPP; |
| 252 | } |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 253 | dev_data->alias_data = alias_data; |
Joerg Roedel | 2601887 | 2011-06-06 16:50:14 +0200 | [diff] [blame] | 254 | } |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 255 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 256 | if (pci_iommuv2_capable(pdev)) { |
| 257 | struct amd_iommu *iommu; |
| 258 | |
| 259 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
| 260 | dev_data->iommu_v2 = iommu->is_iommu_v2; |
| 261 | } |
| 262 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 263 | dev->archdata.iommu = dev_data; |
| 264 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Joerg Roedel | 2601887 | 2011-06-06 16:50:14 +0200 | [diff] [blame] | 268 | static void iommu_ignore_device(struct device *dev) |
| 269 | { |
| 270 | u16 devid, alias; |
| 271 | |
| 272 | devid = get_device_id(dev); |
| 273 | alias = amd_iommu_alias_table[devid]; |
| 274 | |
| 275 | memset(&amd_iommu_dev_table[devid], 0, sizeof(struct dev_table_entry)); |
| 276 | memset(&amd_iommu_dev_table[alias], 0, sizeof(struct dev_table_entry)); |
| 277 | |
| 278 | amd_iommu_rlookup_table[devid] = NULL; |
| 279 | amd_iommu_rlookup_table[alias] = NULL; |
| 280 | } |
| 281 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 282 | static void iommu_uninit_device(struct device *dev) |
| 283 | { |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 284 | /* |
| 285 | * Nothing to do here - we keep dev_data around for unplugged devices |
| 286 | * and reuse it when the device is re-plugged - not doing so would |
| 287 | * introduce a ton of races. |
| 288 | */ |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 289 | } |
Joerg Roedel | b7cc955 | 2009-12-10 11:03:39 +0100 | [diff] [blame] | 290 | |
| 291 | void __init amd_iommu_uninit_devices(void) |
| 292 | { |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 293 | struct iommu_dev_data *dev_data, *n; |
Joerg Roedel | b7cc955 | 2009-12-10 11:03:39 +0100 | [diff] [blame] | 294 | struct pci_dev *pdev = NULL; |
| 295 | |
| 296 | for_each_pci_dev(pdev) { |
| 297 | |
| 298 | if (!check_device(&pdev->dev)) |
| 299 | continue; |
| 300 | |
| 301 | iommu_uninit_device(&pdev->dev); |
| 302 | } |
Joerg Roedel | 8fa5f80 | 2011-06-09 12:24:45 +0200 | [diff] [blame] | 303 | |
| 304 | /* Free all of our dev_data structures */ |
| 305 | list_for_each_entry_safe(dev_data, n, &dev_data_list, dev_data_list) |
| 306 | free_dev_data(dev_data); |
Joerg Roedel | b7cc955 | 2009-12-10 11:03:39 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | int __init amd_iommu_init_devices(void) |
| 310 | { |
| 311 | struct pci_dev *pdev = NULL; |
| 312 | int ret = 0; |
| 313 | |
| 314 | for_each_pci_dev(pdev) { |
| 315 | |
| 316 | if (!check_device(&pdev->dev)) |
| 317 | continue; |
| 318 | |
| 319 | ret = iommu_init_device(&pdev->dev); |
Joerg Roedel | 2601887 | 2011-06-06 16:50:14 +0200 | [diff] [blame] | 320 | if (ret == -ENOTSUPP) |
| 321 | iommu_ignore_device(&pdev->dev); |
| 322 | else if (ret) |
Joerg Roedel | b7cc955 | 2009-12-10 11:03:39 +0100 | [diff] [blame] | 323 | goto out_free; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | |
| 328 | out_free: |
| 329 | |
| 330 | amd_iommu_uninit_devices(); |
| 331 | |
| 332 | return ret; |
| 333 | } |
Joerg Roedel | 7f26508 | 2008-12-12 13:50:21 +0100 | [diff] [blame] | 334 | #ifdef CONFIG_AMD_IOMMU_STATS |
| 335 | |
| 336 | /* |
| 337 | * Initialization code for statistics collection |
| 338 | */ |
| 339 | |
Joerg Roedel | da49f6d | 2008-12-12 14:59:58 +0100 | [diff] [blame] | 340 | DECLARE_STATS_COUNTER(compl_wait); |
Joerg Roedel | 0f2a86f | 2008-12-12 15:05:16 +0100 | [diff] [blame] | 341 | DECLARE_STATS_COUNTER(cnt_map_single); |
Joerg Roedel | 146a691 | 2008-12-12 15:07:12 +0100 | [diff] [blame] | 342 | DECLARE_STATS_COUNTER(cnt_unmap_single); |
Joerg Roedel | d03f067a | 2008-12-12 15:09:48 +0100 | [diff] [blame] | 343 | DECLARE_STATS_COUNTER(cnt_map_sg); |
Joerg Roedel | 55877a6 | 2008-12-12 15:12:14 +0100 | [diff] [blame] | 344 | DECLARE_STATS_COUNTER(cnt_unmap_sg); |
Joerg Roedel | c8f0fb3 | 2008-12-12 15:14:21 +0100 | [diff] [blame] | 345 | DECLARE_STATS_COUNTER(cnt_alloc_coherent); |
Joerg Roedel | 5d31ee7 | 2008-12-12 15:16:38 +0100 | [diff] [blame] | 346 | DECLARE_STATS_COUNTER(cnt_free_coherent); |
Joerg Roedel | c185897 | 2008-12-12 15:42:39 +0100 | [diff] [blame] | 347 | DECLARE_STATS_COUNTER(cross_page); |
Joerg Roedel | f57d98a | 2008-12-12 15:46:29 +0100 | [diff] [blame] | 348 | DECLARE_STATS_COUNTER(domain_flush_single); |
Joerg Roedel | 18811f5 | 2008-12-12 15:48:28 +0100 | [diff] [blame] | 349 | DECLARE_STATS_COUNTER(domain_flush_all); |
Joerg Roedel | 5774f7c | 2008-12-12 15:57:30 +0100 | [diff] [blame] | 350 | DECLARE_STATS_COUNTER(alloced_io_mem); |
Joerg Roedel | 8ecaf8f | 2008-12-12 16:13:04 +0100 | [diff] [blame] | 351 | DECLARE_STATS_COUNTER(total_map_requests); |
Joerg Roedel | da49f6d | 2008-12-12 14:59:58 +0100 | [diff] [blame] | 352 | |
Joerg Roedel | 7f26508 | 2008-12-12 13:50:21 +0100 | [diff] [blame] | 353 | static struct dentry *stats_dir; |
Joerg Roedel | 7f26508 | 2008-12-12 13:50:21 +0100 | [diff] [blame] | 354 | static struct dentry *de_fflush; |
| 355 | |
| 356 | static void amd_iommu_stats_add(struct __iommu_counter *cnt) |
| 357 | { |
| 358 | if (stats_dir == NULL) |
| 359 | return; |
| 360 | |
| 361 | cnt->dent = debugfs_create_u64(cnt->name, 0444, stats_dir, |
| 362 | &cnt->value); |
| 363 | } |
| 364 | |
| 365 | static void amd_iommu_stats_init(void) |
| 366 | { |
| 367 | stats_dir = debugfs_create_dir("amd-iommu", NULL); |
| 368 | if (stats_dir == NULL) |
| 369 | return; |
| 370 | |
Joerg Roedel | 7f26508 | 2008-12-12 13:50:21 +0100 | [diff] [blame] | 371 | de_fflush = debugfs_create_bool("fullflush", 0444, stats_dir, |
| 372 | (u32 *)&amd_iommu_unmap_flush); |
Joerg Roedel | da49f6d | 2008-12-12 14:59:58 +0100 | [diff] [blame] | 373 | |
| 374 | amd_iommu_stats_add(&compl_wait); |
Joerg Roedel | 0f2a86f | 2008-12-12 15:05:16 +0100 | [diff] [blame] | 375 | amd_iommu_stats_add(&cnt_map_single); |
Joerg Roedel | 146a691 | 2008-12-12 15:07:12 +0100 | [diff] [blame] | 376 | amd_iommu_stats_add(&cnt_unmap_single); |
Joerg Roedel | d03f067a | 2008-12-12 15:09:48 +0100 | [diff] [blame] | 377 | amd_iommu_stats_add(&cnt_map_sg); |
Joerg Roedel | 55877a6 | 2008-12-12 15:12:14 +0100 | [diff] [blame] | 378 | amd_iommu_stats_add(&cnt_unmap_sg); |
Joerg Roedel | c8f0fb3 | 2008-12-12 15:14:21 +0100 | [diff] [blame] | 379 | amd_iommu_stats_add(&cnt_alloc_coherent); |
Joerg Roedel | 5d31ee7 | 2008-12-12 15:16:38 +0100 | [diff] [blame] | 380 | amd_iommu_stats_add(&cnt_free_coherent); |
Joerg Roedel | c185897 | 2008-12-12 15:42:39 +0100 | [diff] [blame] | 381 | amd_iommu_stats_add(&cross_page); |
Joerg Roedel | f57d98a | 2008-12-12 15:46:29 +0100 | [diff] [blame] | 382 | amd_iommu_stats_add(&domain_flush_single); |
Joerg Roedel | 18811f5 | 2008-12-12 15:48:28 +0100 | [diff] [blame] | 383 | amd_iommu_stats_add(&domain_flush_all); |
Joerg Roedel | 5774f7c | 2008-12-12 15:57:30 +0100 | [diff] [blame] | 384 | amd_iommu_stats_add(&alloced_io_mem); |
Joerg Roedel | 8ecaf8f | 2008-12-12 16:13:04 +0100 | [diff] [blame] | 385 | amd_iommu_stats_add(&total_map_requests); |
Joerg Roedel | 7f26508 | 2008-12-12 13:50:21 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | #endif |
| 389 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 390 | /**************************************************************************** |
| 391 | * |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 392 | * Interrupt handling functions |
| 393 | * |
| 394 | ****************************************************************************/ |
| 395 | |
Joerg Roedel | e3e5987 | 2009-09-03 14:02:10 +0200 | [diff] [blame] | 396 | static void dump_dte_entry(u16 devid) |
| 397 | { |
| 398 | int i; |
| 399 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 400 | for (i = 0; i < 4; ++i) |
| 401 | pr_err("AMD-Vi: DTE[%d]: %016llx\n", i, |
Joerg Roedel | e3e5987 | 2009-09-03 14:02:10 +0200 | [diff] [blame] | 402 | amd_iommu_dev_table[devid].data[i]); |
| 403 | } |
| 404 | |
Joerg Roedel | 945b4ac | 2009-09-03 14:25:02 +0200 | [diff] [blame] | 405 | static void dump_command(unsigned long phys_addr) |
| 406 | { |
| 407 | struct iommu_cmd *cmd = phys_to_virt(phys_addr); |
| 408 | int i; |
| 409 | |
| 410 | for (i = 0; i < 4; ++i) |
| 411 | pr_err("AMD-Vi: CMD[%d]: %08x\n", i, cmd->data[i]); |
| 412 | } |
| 413 | |
Joerg Roedel | a345b23 | 2009-09-03 15:01:43 +0200 | [diff] [blame] | 414 | static void iommu_print_event(struct amd_iommu *iommu, void *__evt) |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 415 | { |
| 416 | u32 *event = __evt; |
| 417 | int type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; |
| 418 | int devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; |
| 419 | int domid = (event[1] >> EVENT_DOMID_SHIFT) & EVENT_DOMID_MASK; |
| 420 | int flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; |
| 421 | u64 address = (u64)(((u64)event[3]) << 32) | event[2]; |
| 422 | |
Joerg Roedel | 4c6f40d | 2009-09-01 16:43:58 +0200 | [diff] [blame] | 423 | printk(KERN_ERR "AMD-Vi: Event logged ["); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 424 | |
| 425 | switch (type) { |
| 426 | case EVENT_TYPE_ILL_DEV: |
| 427 | printk("ILLEGAL_DEV_TABLE_ENTRY device=%02x:%02x.%x " |
| 428 | "address=0x%016llx flags=0x%04x]\n", |
| 429 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
| 430 | address, flags); |
Joerg Roedel | e3e5987 | 2009-09-03 14:02:10 +0200 | [diff] [blame] | 431 | dump_dte_entry(devid); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 432 | break; |
| 433 | case EVENT_TYPE_IO_FAULT: |
| 434 | printk("IO_PAGE_FAULT device=%02x:%02x.%x " |
| 435 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", |
| 436 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
| 437 | domid, address, flags); |
| 438 | break; |
| 439 | case EVENT_TYPE_DEV_TAB_ERR: |
| 440 | printk("DEV_TAB_HARDWARE_ERROR device=%02x:%02x.%x " |
| 441 | "address=0x%016llx flags=0x%04x]\n", |
| 442 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
| 443 | address, flags); |
| 444 | break; |
| 445 | case EVENT_TYPE_PAGE_TAB_ERR: |
| 446 | printk("PAGE_TAB_HARDWARE_ERROR device=%02x:%02x.%x " |
| 447 | "domain=0x%04x address=0x%016llx flags=0x%04x]\n", |
| 448 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
| 449 | domid, address, flags); |
| 450 | break; |
| 451 | case EVENT_TYPE_ILL_CMD: |
| 452 | printk("ILLEGAL_COMMAND_ERROR address=0x%016llx]\n", address); |
Joerg Roedel | 945b4ac | 2009-09-03 14:25:02 +0200 | [diff] [blame] | 453 | dump_command(address); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 454 | break; |
| 455 | case EVENT_TYPE_CMD_HARD_ERR: |
| 456 | printk("COMMAND_HARDWARE_ERROR address=0x%016llx " |
| 457 | "flags=0x%04x]\n", address, flags); |
| 458 | break; |
| 459 | case EVENT_TYPE_IOTLB_INV_TO: |
| 460 | printk("IOTLB_INV_TIMEOUT device=%02x:%02x.%x " |
| 461 | "address=0x%016llx]\n", |
| 462 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
| 463 | address); |
| 464 | break; |
| 465 | case EVENT_TYPE_INV_DEV_REQ: |
| 466 | printk("INVALID_DEVICE_REQUEST device=%02x:%02x.%x " |
| 467 | "address=0x%016llx flags=0x%04x]\n", |
| 468 | PCI_BUS(devid), PCI_SLOT(devid), PCI_FUNC(devid), |
| 469 | address, flags); |
| 470 | break; |
| 471 | default: |
| 472 | printk(KERN_ERR "UNKNOWN type=0x%02x]\n", type); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | static void iommu_poll_events(struct amd_iommu *iommu) |
| 477 | { |
| 478 | u32 head, tail; |
| 479 | unsigned long flags; |
| 480 | |
| 481 | spin_lock_irqsave(&iommu->lock, flags); |
| 482 | |
| 483 | head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); |
| 484 | tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); |
| 485 | |
| 486 | while (head != tail) { |
Joerg Roedel | a345b23 | 2009-09-03 15:01:43 +0200 | [diff] [blame] | 487 | iommu_print_event(iommu, iommu->evt_buf + head); |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 488 | head = (head + EVENT_ENTRY_SIZE) % iommu->evt_buf_size; |
| 489 | } |
| 490 | |
| 491 | writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); |
| 492 | |
| 493 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 494 | } |
| 495 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 496 | static void iommu_handle_ppr_entry(struct amd_iommu *iommu, u32 head) |
| 497 | { |
| 498 | struct amd_iommu_fault fault; |
| 499 | volatile u64 *raw; |
| 500 | int i; |
| 501 | |
| 502 | raw = (u64 *)(iommu->ppr_log + head); |
| 503 | |
| 504 | /* |
| 505 | * Hardware bug: Interrupt may arrive before the entry is written to |
| 506 | * memory. If this happens we need to wait for the entry to arrive. |
| 507 | */ |
| 508 | for (i = 0; i < LOOP_TIMEOUT; ++i) { |
| 509 | if (PPR_REQ_TYPE(raw[0]) != 0) |
| 510 | break; |
| 511 | udelay(1); |
| 512 | } |
| 513 | |
| 514 | if (PPR_REQ_TYPE(raw[0]) != PPR_REQ_FAULT) { |
| 515 | pr_err_ratelimited("AMD-Vi: Unknown PPR request received\n"); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | fault.address = raw[1]; |
| 520 | fault.pasid = PPR_PASID(raw[0]); |
| 521 | fault.device_id = PPR_DEVID(raw[0]); |
| 522 | fault.tag = PPR_TAG(raw[0]); |
| 523 | fault.flags = PPR_FLAGS(raw[0]); |
| 524 | |
| 525 | /* |
| 526 | * To detect the hardware bug we need to clear the entry |
| 527 | * to back to zero. |
| 528 | */ |
| 529 | raw[0] = raw[1] = 0; |
| 530 | |
| 531 | atomic_notifier_call_chain(&ppr_notifier, 0, &fault); |
| 532 | } |
| 533 | |
| 534 | static void iommu_poll_ppr_log(struct amd_iommu *iommu) |
| 535 | { |
| 536 | unsigned long flags; |
| 537 | u32 head, tail; |
| 538 | |
| 539 | if (iommu->ppr_log == NULL) |
| 540 | return; |
| 541 | |
| 542 | spin_lock_irqsave(&iommu->lock, flags); |
| 543 | |
| 544 | head = readl(iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); |
| 545 | tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET); |
| 546 | |
| 547 | while (head != tail) { |
| 548 | |
| 549 | /* Handle PPR entry */ |
| 550 | iommu_handle_ppr_entry(iommu, head); |
| 551 | |
| 552 | /* Update and refresh ring-buffer state*/ |
| 553 | head = (head + PPR_ENTRY_SIZE) % PPR_LOG_SIZE; |
| 554 | writel(head, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET); |
| 555 | tail = readl(iommu->mmio_base + MMIO_PPR_TAIL_OFFSET); |
| 556 | } |
| 557 | |
| 558 | /* enable ppr interrupts again */ |
| 559 | writel(MMIO_STATUS_PPR_INT_MASK, iommu->mmio_base + MMIO_STATUS_OFFSET); |
| 560 | |
| 561 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 562 | } |
| 563 | |
Joerg Roedel | 72fe00f | 2011-05-10 10:50:42 +0200 | [diff] [blame] | 564 | irqreturn_t amd_iommu_int_thread(int irq, void *data) |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 565 | { |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 566 | struct amd_iommu *iommu; |
| 567 | |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 568 | for_each_iommu(iommu) { |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 569 | iommu_poll_events(iommu); |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 570 | iommu_poll_ppr_log(iommu); |
| 571 | } |
Joerg Roedel | 90008ee | 2008-09-09 16:41:05 +0200 | [diff] [blame] | 572 | |
| 573 | return IRQ_HANDLED; |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Joerg Roedel | 72fe00f | 2011-05-10 10:50:42 +0200 | [diff] [blame] | 576 | irqreturn_t amd_iommu_int_handler(int irq, void *data) |
| 577 | { |
| 578 | return IRQ_WAKE_THREAD; |
| 579 | } |
| 580 | |
Joerg Roedel | a80dc3e | 2008-09-11 16:51:41 +0200 | [diff] [blame] | 581 | /**************************************************************************** |
| 582 | * |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 583 | * IOMMU command queuing functions |
| 584 | * |
| 585 | ****************************************************************************/ |
| 586 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 587 | static int wait_on_sem(volatile u64 *sem) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 588 | { |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 589 | int i = 0; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 590 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 591 | while (*sem == 0 && i < LOOP_TIMEOUT) { |
| 592 | udelay(1); |
| 593 | i += 1; |
| 594 | } |
| 595 | |
| 596 | if (i == LOOP_TIMEOUT) { |
| 597 | pr_alert("AMD-Vi: Completion-Wait loop timed out\n"); |
| 598 | return -EIO; |
| 599 | } |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 604 | static void copy_cmd_to_buffer(struct amd_iommu *iommu, |
| 605 | struct iommu_cmd *cmd, |
| 606 | u32 tail) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 607 | { |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 608 | u8 *target; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 609 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 610 | target = iommu->cmd_buf + tail; |
| 611 | tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 612 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 613 | /* Copy command to buffer */ |
| 614 | memcpy(target, cmd, sizeof(*cmd)); |
| 615 | |
| 616 | /* Tell the IOMMU about it */ |
| 617 | writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
| 618 | } |
| 619 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 620 | static void build_completion_wait(struct iommu_cmd *cmd, u64 address) |
Joerg Roedel | ded4673 | 2011-04-06 10:53:48 +0200 | [diff] [blame] | 621 | { |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 622 | WARN_ON(address & 0x7ULL); |
| 623 | |
Joerg Roedel | ded4673 | 2011-04-06 10:53:48 +0200 | [diff] [blame] | 624 | memset(cmd, 0, sizeof(*cmd)); |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 625 | cmd->data[0] = lower_32_bits(__pa(address)) | CMD_COMPL_WAIT_STORE_MASK; |
| 626 | cmd->data[1] = upper_32_bits(__pa(address)); |
| 627 | cmd->data[2] = 1; |
Joerg Roedel | ded4673 | 2011-04-06 10:53:48 +0200 | [diff] [blame] | 628 | CMD_SET_TYPE(cmd, CMD_COMPL_WAIT); |
| 629 | } |
| 630 | |
Joerg Roedel | 94fe79e | 2011-04-06 11:07:21 +0200 | [diff] [blame] | 631 | static void build_inv_dte(struct iommu_cmd *cmd, u16 devid) |
| 632 | { |
| 633 | memset(cmd, 0, sizeof(*cmd)); |
| 634 | cmd->data[0] = devid; |
| 635 | CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY); |
| 636 | } |
| 637 | |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 638 | static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, |
| 639 | size_t size, u16 domid, int pde) |
| 640 | { |
| 641 | u64 pages; |
| 642 | int s; |
| 643 | |
| 644 | pages = iommu_num_pages(address, size, PAGE_SIZE); |
| 645 | s = 0; |
| 646 | |
| 647 | if (pages > 1) { |
| 648 | /* |
| 649 | * If we have to flush more than one page, flush all |
| 650 | * TLB entries for this domain |
| 651 | */ |
| 652 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; |
| 653 | s = 1; |
| 654 | } |
| 655 | |
| 656 | address &= PAGE_MASK; |
| 657 | |
| 658 | memset(cmd, 0, sizeof(*cmd)); |
| 659 | cmd->data[1] |= domid; |
| 660 | cmd->data[2] = lower_32_bits(address); |
| 661 | cmd->data[3] = upper_32_bits(address); |
| 662 | CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); |
| 663 | if (s) /* size bit - we flush more than one 4kb page */ |
| 664 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; |
| 665 | if (pde) /* PDE bit - we wan't flush everything not only the PTEs */ |
| 666 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_PDE_MASK; |
| 667 | } |
| 668 | |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 669 | static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, |
| 670 | u64 address, size_t size) |
| 671 | { |
| 672 | u64 pages; |
| 673 | int s; |
| 674 | |
| 675 | pages = iommu_num_pages(address, size, PAGE_SIZE); |
| 676 | s = 0; |
| 677 | |
| 678 | if (pages > 1) { |
| 679 | /* |
| 680 | * If we have to flush more than one page, flush all |
| 681 | * TLB entries for this domain |
| 682 | */ |
| 683 | address = CMD_INV_IOMMU_ALL_PAGES_ADDRESS; |
| 684 | s = 1; |
| 685 | } |
| 686 | |
| 687 | address &= PAGE_MASK; |
| 688 | |
| 689 | memset(cmd, 0, sizeof(*cmd)); |
| 690 | cmd->data[0] = devid; |
| 691 | cmd->data[0] |= (qdep & 0xff) << 24; |
| 692 | cmd->data[1] = devid; |
| 693 | cmd->data[2] = lower_32_bits(address); |
| 694 | cmd->data[3] = upper_32_bits(address); |
| 695 | CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); |
| 696 | if (s) |
| 697 | cmd->data[2] |= CMD_INV_IOMMU_PAGES_SIZE_MASK; |
| 698 | } |
| 699 | |
Joerg Roedel | 58fc7f1 | 2011-04-11 11:13:24 +0200 | [diff] [blame] | 700 | static void build_inv_all(struct iommu_cmd *cmd) |
| 701 | { |
| 702 | memset(cmd, 0, sizeof(*cmd)); |
| 703 | CMD_SET_TYPE(cmd, CMD_INV_ALL); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 706 | /* |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 707 | * Writes the command to the IOMMUs command buffer and informs the |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 708 | * hardware about the new command. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 709 | */ |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 710 | static int iommu_queue_command_sync(struct amd_iommu *iommu, |
| 711 | struct iommu_cmd *cmd, |
| 712 | bool sync) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 713 | { |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 714 | u32 left, tail, head, next_tail; |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 715 | unsigned long flags; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 716 | |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 717 | WARN_ON(iommu->cmd_buf_size & CMD_BUFFER_UNINITIALIZED); |
Joerg Roedel | da49f6d | 2008-12-12 14:59:58 +0100 | [diff] [blame] | 718 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 719 | again: |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 720 | spin_lock_irqsave(&iommu->lock, flags); |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 721 | |
| 722 | head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); |
| 723 | tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); |
| 724 | next_tail = (tail + sizeof(*cmd)) % iommu->cmd_buf_size; |
| 725 | left = (head - next_tail) % iommu->cmd_buf_size; |
| 726 | |
| 727 | if (left <= 2) { |
| 728 | struct iommu_cmd sync_cmd; |
| 729 | volatile u64 sem = 0; |
| 730 | int ret; |
| 731 | |
| 732 | build_completion_wait(&sync_cmd, (u64)&sem); |
| 733 | copy_cmd_to_buffer(iommu, &sync_cmd, tail); |
| 734 | |
| 735 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 736 | |
| 737 | if ((ret = wait_on_sem(&sem)) != 0) |
| 738 | return ret; |
| 739 | |
| 740 | goto again; |
Joerg Roedel | 136f78a | 2008-07-11 17:14:27 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 743 | copy_cmd_to_buffer(iommu, cmd, tail); |
Joerg Roedel | 519c31b | 2008-08-14 19:55:15 +0200 | [diff] [blame] | 744 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 745 | /* We need to sync now to make sure all commands are processed */ |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 746 | iommu->need_sync = sync; |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 747 | |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 748 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 749 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 750 | return 0; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 753 | static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) |
| 754 | { |
| 755 | return iommu_queue_command_sync(iommu, cmd, true); |
| 756 | } |
| 757 | |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 758 | /* |
| 759 | * This function queues a completion wait command into the command |
| 760 | * buffer of an IOMMU |
| 761 | */ |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 762 | static int iommu_completion_wait(struct amd_iommu *iommu) |
| 763 | { |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 764 | struct iommu_cmd cmd; |
| 765 | volatile u64 sem = 0; |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 766 | int ret; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 767 | |
| 768 | if (!iommu->need_sync) |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 769 | return 0; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 770 | |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 771 | build_completion_wait(&cmd, (u64)&sem); |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 772 | |
Joerg Roedel | f1ca151 | 2011-09-02 14:10:32 +0200 | [diff] [blame] | 773 | ret = iommu_queue_command_sync(iommu, &cmd, false); |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 774 | if (ret) |
Joerg Roedel | 815b33f | 2011-04-06 17:26:49 +0200 | [diff] [blame] | 775 | return ret; |
Joerg Roedel | 8d20196 | 2008-12-02 20:34:41 +0100 | [diff] [blame] | 776 | |
Joerg Roedel | ac0ea6e | 2011-04-06 18:38:20 +0200 | [diff] [blame] | 777 | return wait_on_sem(&sem); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Joerg Roedel | d8c1308 | 2011-04-06 18:51:26 +0200 | [diff] [blame] | 780 | static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 781 | { |
| 782 | struct iommu_cmd cmd; |
| 783 | |
Joerg Roedel | d8c1308 | 2011-04-06 18:51:26 +0200 | [diff] [blame] | 784 | build_inv_dte(&cmd, devid); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 785 | |
Joerg Roedel | d8c1308 | 2011-04-06 18:51:26 +0200 | [diff] [blame] | 786 | return iommu_queue_command(iommu, &cmd); |
| 787 | } |
| 788 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 789 | static void iommu_flush_dte_all(struct amd_iommu *iommu) |
| 790 | { |
| 791 | u32 devid; |
| 792 | |
| 793 | for (devid = 0; devid <= 0xffff; ++devid) |
| 794 | iommu_flush_dte(iommu, devid); |
| 795 | |
| 796 | iommu_completion_wait(iommu); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 800 | * This function uses heavy locking and may disable irqs for some time. But |
| 801 | * this is no issue because it is only called during resume. |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 802 | */ |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 803 | static void iommu_flush_tlb_all(struct amd_iommu *iommu) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 804 | { |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 805 | u32 dom_id; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 806 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 807 | for (dom_id = 0; dom_id <= 0xffff; ++dom_id) { |
| 808 | struct iommu_cmd cmd; |
| 809 | build_inv_iommu_pages(&cmd, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, |
| 810 | dom_id, 1); |
| 811 | iommu_queue_command(iommu, &cmd); |
| 812 | } |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 813 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 814 | iommu_completion_wait(iommu); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 815 | } |
| 816 | |
Joerg Roedel | 58fc7f1 | 2011-04-11 11:13:24 +0200 | [diff] [blame] | 817 | static void iommu_flush_all(struct amd_iommu *iommu) |
| 818 | { |
| 819 | struct iommu_cmd cmd; |
| 820 | |
| 821 | build_inv_all(&cmd); |
| 822 | |
| 823 | iommu_queue_command(iommu, &cmd); |
| 824 | iommu_completion_wait(iommu); |
| 825 | } |
| 826 | |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 827 | void iommu_flush_all_caches(struct amd_iommu *iommu) |
| 828 | { |
Joerg Roedel | 58fc7f1 | 2011-04-11 11:13:24 +0200 | [diff] [blame] | 829 | if (iommu_feature(iommu, FEATURE_IA)) { |
| 830 | iommu_flush_all(iommu); |
| 831 | } else { |
| 832 | iommu_flush_dte_all(iommu); |
| 833 | iommu_flush_tlb_all(iommu); |
| 834 | } |
Joerg Roedel | 7d0c5cc | 2011-04-07 08:16:10 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 837 | /* |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 838 | * Command send function for flushing on-device TLB |
| 839 | */ |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 840 | static int device_flush_iotlb(struct iommu_dev_data *dev_data, |
| 841 | u64 address, size_t size) |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 842 | { |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 843 | struct amd_iommu *iommu; |
| 844 | struct iommu_cmd cmd; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 845 | int qdep; |
| 846 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 847 | qdep = dev_data->ats.qdep; |
| 848 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 849 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 850 | build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, size); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 851 | |
| 852 | return iommu_queue_command(iommu, &cmd); |
| 853 | } |
| 854 | |
| 855 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 856 | * Command send function for invalidating a device table entry |
| 857 | */ |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 858 | static int device_flush_dte(struct iommu_dev_data *dev_data) |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 859 | { |
| 860 | struct amd_iommu *iommu; |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 861 | int ret; |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 862 | |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 863 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 864 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 865 | ret = iommu_flush_dte(iommu, dev_data->devid); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 866 | if (ret) |
| 867 | return ret; |
| 868 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 869 | if (dev_data->ats.enabled) |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 870 | ret = device_flush_iotlb(dev_data, 0, ~0UL); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 871 | |
| 872 | return ret; |
Joerg Roedel | 3fa4365 | 2009-11-26 15:04:38 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 875 | /* |
| 876 | * TLB invalidation function which is called from the mapping functions. |
| 877 | * It invalidates a single PTE if the range to flush is within a single |
| 878 | * page. Otherwise it flushes the whole TLB of the IOMMU. |
| 879 | */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 880 | static void __domain_flush_pages(struct protection_domain *domain, |
| 881 | u64 address, size_t size, int pde) |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 882 | { |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 883 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 884 | struct iommu_cmd cmd; |
| 885 | int ret = 0, i; |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 886 | |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 887 | build_inv_iommu_pages(&cmd, address, size, domain->id, pde); |
Joerg Roedel | 999ba41 | 2008-07-03 19:35:08 +0200 | [diff] [blame] | 888 | |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 889 | for (i = 0; i < amd_iommus_present; ++i) { |
| 890 | if (!domain->dev_iommu[i]) |
| 891 | continue; |
| 892 | |
| 893 | /* |
| 894 | * Devices of this domain are behind this IOMMU |
| 895 | * We need a TLB flush |
| 896 | */ |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 897 | ret |= iommu_queue_command(amd_iommus[i], &cmd); |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 900 | list_for_each_entry(dev_data, &domain->dev_list, list) { |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 901 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 902 | if (!dev_data->ats.enabled) |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 903 | continue; |
| 904 | |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 905 | ret |= device_flush_iotlb(dev_data, address, size); |
Joerg Roedel | cb41ed8 | 2011-04-05 11:00:53 +0200 | [diff] [blame] | 906 | } |
| 907 | |
Joerg Roedel | 11b6402 | 2011-04-06 11:49:28 +0200 | [diff] [blame] | 908 | WARN_ON(ret); |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 911 | static void domain_flush_pages(struct protection_domain *domain, |
| 912 | u64 address, size_t size) |
Joerg Roedel | 6de8ad9 | 2009-11-23 18:30:32 +0100 | [diff] [blame] | 913 | { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 914 | __domain_flush_pages(domain, address, size, 0); |
Joerg Roedel | a19ae1e | 2008-06-26 21:27:55 +0200 | [diff] [blame] | 915 | } |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 916 | |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 917 | /* Flush the whole IO/TLB for a given protection domain */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 918 | static void domain_flush_tlb(struct protection_domain *domain) |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 919 | { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 920 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 0); |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 921 | } |
| 922 | |
Chris Wright | 42a49f9 | 2009-06-15 15:42:00 +0200 | [diff] [blame] | 923 | /* Flush the whole IO/TLB for a given protection domain - including PDE */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 924 | static void domain_flush_tlb_pde(struct protection_domain *domain) |
Chris Wright | 42a49f9 | 2009-06-15 15:42:00 +0200 | [diff] [blame] | 925 | { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 926 | __domain_flush_pages(domain, 0, CMD_INV_IOMMU_ALL_PAGES_ADDRESS, 1); |
| 927 | } |
| 928 | |
| 929 | static void domain_flush_complete(struct protection_domain *domain) |
Joerg Roedel | b6c0271 | 2008-06-26 21:27:53 +0200 | [diff] [blame] | 930 | { |
| 931 | int i; |
| 932 | |
| 933 | for (i = 0; i < amd_iommus_present; ++i) { |
| 934 | if (!domain->dev_iommu[i]) |
| 935 | continue; |
| 936 | |
| 937 | /* |
| 938 | * Devices of this domain are behind this IOMMU |
| 939 | * We need to wait for completion of all commands. |
| 940 | */ |
| 941 | iommu_completion_wait(amd_iommus[i]); |
| 942 | } |
| 943 | } |
| 944 | |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 945 | |
Joerg Roedel | 43f4960 | 2008-12-02 21:01:12 +0100 | [diff] [blame] | 946 | /* |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 947 | * This function flushes the DTEs for all devices in domain |
Joerg Roedel | 43f4960 | 2008-12-02 21:01:12 +0100 | [diff] [blame] | 948 | */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 949 | static void domain_flush_devices(struct protection_domain *domain) |
Joerg Roedel | bfd1be1 | 2009-05-05 15:33:57 +0200 | [diff] [blame] | 950 | { |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 951 | struct iommu_dev_data *dev_data; |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 952 | |
| 953 | list_for_each_entry(dev_data, &domain->dev_list, list) |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 954 | device_flush_dte(dev_data); |
Joerg Roedel | b00d3bc | 2009-11-26 15:35:33 +0100 | [diff] [blame] | 955 | } |
| 956 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 957 | /**************************************************************************** |
| 958 | * |
| 959 | * The functions below are used the create the page table mappings for |
| 960 | * unity mapped regions. |
| 961 | * |
| 962 | ****************************************************************************/ |
| 963 | |
| 964 | /* |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 965 | * This function is used to add another level to an IO page table. Adding |
| 966 | * another level increases the size of the address space by 9 bits to a size up |
| 967 | * to 64 bits. |
| 968 | */ |
| 969 | static bool increase_address_space(struct protection_domain *domain, |
| 970 | gfp_t gfp) |
| 971 | { |
| 972 | u64 *pte; |
| 973 | |
| 974 | if (domain->mode == PAGE_MODE_6_LEVEL) |
| 975 | /* address space already 64 bit large */ |
| 976 | return false; |
| 977 | |
| 978 | pte = (void *)get_zeroed_page(gfp); |
| 979 | if (!pte) |
| 980 | return false; |
| 981 | |
| 982 | *pte = PM_LEVEL_PDE(domain->mode, |
| 983 | virt_to_phys(domain->pt_root)); |
| 984 | domain->pt_root = pte; |
| 985 | domain->mode += 1; |
| 986 | domain->updated = true; |
| 987 | |
| 988 | return true; |
| 989 | } |
| 990 | |
| 991 | static u64 *alloc_pte(struct protection_domain *domain, |
| 992 | unsigned long address, |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 993 | unsigned long page_size, |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 994 | u64 **pte_page, |
| 995 | gfp_t gfp) |
| 996 | { |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 997 | int level, end_lvl; |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 998 | u64 *pte, *page; |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 999 | |
| 1000 | BUG_ON(!is_power_of_2(page_size)); |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1001 | |
| 1002 | while (address > PM_LEVEL_SIZE(domain->mode)) |
| 1003 | increase_address_space(domain, gfp); |
| 1004 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1005 | level = domain->mode - 1; |
| 1006 | pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)]; |
| 1007 | address = PAGE_SIZE_ALIGN(address, page_size); |
| 1008 | end_lvl = PAGE_SIZE_LEVEL(page_size); |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1009 | |
| 1010 | while (level > end_lvl) { |
| 1011 | if (!IOMMU_PTE_PRESENT(*pte)) { |
| 1012 | page = (u64 *)get_zeroed_page(gfp); |
| 1013 | if (!page) |
| 1014 | return NULL; |
| 1015 | *pte = PM_LEVEL_PDE(level, virt_to_phys(page)); |
| 1016 | } |
| 1017 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1018 | /* No level skipping support yet */ |
| 1019 | if (PM_PTE_LEVEL(*pte) != level) |
| 1020 | return NULL; |
| 1021 | |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1022 | level -= 1; |
| 1023 | |
| 1024 | pte = IOMMU_PTE_PAGE(*pte); |
| 1025 | |
| 1026 | if (pte_page && level == end_lvl) |
| 1027 | *pte_page = pte; |
| 1028 | |
| 1029 | pte = &pte[PM_LEVEL_INDEX(level, address)]; |
| 1030 | } |
| 1031 | |
| 1032 | return pte; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * This function checks if there is a PTE for a given dma address. If |
| 1037 | * there is one, it returns the pointer to it. |
| 1038 | */ |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1039 | static u64 *fetch_pte(struct protection_domain *domain, unsigned long address) |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1040 | { |
| 1041 | int level; |
| 1042 | u64 *pte; |
| 1043 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1044 | if (address > PM_LEVEL_SIZE(domain->mode)) |
| 1045 | return NULL; |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1046 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1047 | level = domain->mode - 1; |
| 1048 | pte = &domain->pt_root[PM_LEVEL_INDEX(level, address)]; |
| 1049 | |
| 1050 | while (level > 0) { |
| 1051 | |
| 1052 | /* Not Present */ |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1053 | if (!IOMMU_PTE_PRESENT(*pte)) |
| 1054 | return NULL; |
| 1055 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1056 | /* Large PTE */ |
| 1057 | if (PM_PTE_LEVEL(*pte) == 0x07) { |
| 1058 | unsigned long pte_mask, __pte; |
| 1059 | |
| 1060 | /* |
| 1061 | * If we have a series of large PTEs, make |
| 1062 | * sure to return a pointer to the first one. |
| 1063 | */ |
| 1064 | pte_mask = PTE_PAGE_SIZE(*pte); |
| 1065 | pte_mask = ~((PAGE_SIZE_PTE_COUNT(pte_mask) << 3) - 1); |
| 1066 | __pte = ((unsigned long)pte) & pte_mask; |
| 1067 | |
| 1068 | return (u64 *)__pte; |
| 1069 | } |
| 1070 | |
| 1071 | /* No level skipping support yet */ |
| 1072 | if (PM_PTE_LEVEL(*pte) != level) |
| 1073 | return NULL; |
| 1074 | |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1075 | level -= 1; |
| 1076 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1077 | /* Walk to the next level */ |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1078 | pte = IOMMU_PTE_PAGE(*pte); |
| 1079 | pte = &pte[PM_LEVEL_INDEX(level, address)]; |
Joerg Roedel | 308973d | 2009-11-24 17:43:32 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | return pte; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1086 | * Generic mapping functions. It maps a physical address into a DMA |
| 1087 | * address space. It allocates the page table pages if necessary. |
| 1088 | * In the future it can be extended to a generic mapping function |
| 1089 | * supporting all features of AMD IOMMU page tables like level skipping |
| 1090 | * and full 64 bit address spaces. |
| 1091 | */ |
Joerg Roedel | 38e817f | 2008-12-02 17:27:52 +0100 | [diff] [blame] | 1092 | static int iommu_map_page(struct protection_domain *dom, |
| 1093 | unsigned long bus_addr, |
| 1094 | unsigned long phys_addr, |
Joerg Roedel | abdc5eb | 2009-09-03 11:33:51 +0200 | [diff] [blame] | 1095 | int prot, |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1096 | unsigned long page_size) |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1097 | { |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 1098 | u64 __pte, *pte; |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1099 | int i, count; |
Joerg Roedel | abdc5eb | 2009-09-03 11:33:51 +0200 | [diff] [blame] | 1100 | |
Joerg Roedel | bad1cac | 2009-09-02 16:52:23 +0200 | [diff] [blame] | 1101 | if (!(prot & IOMMU_PROT_MASK)) |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1102 | return -EINVAL; |
| 1103 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1104 | bus_addr = PAGE_ALIGN(bus_addr); |
| 1105 | phys_addr = PAGE_ALIGN(phys_addr); |
| 1106 | count = PAGE_SIZE_PTE_COUNT(page_size); |
| 1107 | pte = alloc_pte(dom, bus_addr, page_size, NULL, GFP_KERNEL); |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1108 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1109 | for (i = 0; i < count; ++i) |
| 1110 | if (IOMMU_PTE_PRESENT(pte[i])) |
| 1111 | return -EBUSY; |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1112 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1113 | if (page_size > PAGE_SIZE) { |
| 1114 | __pte = PAGE_SIZE_PTE(phys_addr, page_size); |
| 1115 | __pte |= PM_LEVEL_ENC(7) | IOMMU_PTE_P | IOMMU_PTE_FC; |
| 1116 | } else |
| 1117 | __pte = phys_addr | IOMMU_PTE_P | IOMMU_PTE_FC; |
| 1118 | |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1119 | if (prot & IOMMU_PROT_IR) |
| 1120 | __pte |= IOMMU_PTE_IR; |
| 1121 | if (prot & IOMMU_PROT_IW) |
| 1122 | __pte |= IOMMU_PTE_IW; |
| 1123 | |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1124 | for (i = 0; i < count; ++i) |
| 1125 | pte[i] = __pte; |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1126 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 1127 | update_domain(dom); |
| 1128 | |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1129 | return 0; |
| 1130 | } |
| 1131 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1132 | static unsigned long iommu_unmap_page(struct protection_domain *dom, |
| 1133 | unsigned long bus_addr, |
| 1134 | unsigned long page_size) |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1135 | { |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1136 | unsigned long long unmap_size, unmapped; |
| 1137 | u64 *pte; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1138 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1139 | BUG_ON(!is_power_of_2(page_size)); |
| 1140 | |
| 1141 | unmapped = 0; |
| 1142 | |
| 1143 | while (unmapped < page_size) { |
| 1144 | |
| 1145 | pte = fetch_pte(dom, bus_addr); |
| 1146 | |
| 1147 | if (!pte) { |
| 1148 | /* |
| 1149 | * No PTE for this address |
| 1150 | * move forward in 4kb steps |
| 1151 | */ |
| 1152 | unmap_size = PAGE_SIZE; |
| 1153 | } else if (PM_PTE_LEVEL(*pte) == 0) { |
| 1154 | /* 4kb PTE found for this address */ |
| 1155 | unmap_size = PAGE_SIZE; |
| 1156 | *pte = 0ULL; |
| 1157 | } else { |
| 1158 | int count, i; |
| 1159 | |
| 1160 | /* Large PTE found which maps this address */ |
| 1161 | unmap_size = PTE_PAGE_SIZE(*pte); |
| 1162 | count = PAGE_SIZE_PTE_COUNT(unmap_size); |
| 1163 | for (i = 0; i < count; i++) |
| 1164 | pte[i] = 0ULL; |
| 1165 | } |
| 1166 | |
| 1167 | bus_addr = (bus_addr & ~(unmap_size - 1)) + unmap_size; |
| 1168 | unmapped += unmap_size; |
| 1169 | } |
| 1170 | |
| 1171 | BUG_ON(!is_power_of_2(unmapped)); |
| 1172 | |
| 1173 | return unmapped; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1174 | } |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 1175 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1176 | /* |
| 1177 | * This function checks if a specific unity mapping entry is needed for |
| 1178 | * this specific IOMMU. |
| 1179 | */ |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1180 | static int iommu_for_unity_map(struct amd_iommu *iommu, |
| 1181 | struct unity_map_entry *entry) |
| 1182 | { |
| 1183 | u16 bdf, i; |
| 1184 | |
| 1185 | for (i = entry->devid_start; i <= entry->devid_end; ++i) { |
| 1186 | bdf = amd_iommu_alias_table[i]; |
| 1187 | if (amd_iommu_rlookup_table[bdf] == iommu) |
| 1188 | return 1; |
| 1189 | } |
| 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1194 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1195 | * This function actually applies the mapping to the page table of the |
| 1196 | * dma_ops domain. |
| 1197 | */ |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1198 | static int dma_ops_unity_map(struct dma_ops_domain *dma_dom, |
| 1199 | struct unity_map_entry *e) |
| 1200 | { |
| 1201 | u64 addr; |
| 1202 | int ret; |
| 1203 | |
| 1204 | for (addr = e->address_start; addr < e->address_end; |
| 1205 | addr += PAGE_SIZE) { |
Joerg Roedel | abdc5eb | 2009-09-03 11:33:51 +0200 | [diff] [blame] | 1206 | ret = iommu_map_page(&dma_dom->domain, addr, addr, e->prot, |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1207 | PAGE_SIZE); |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1208 | if (ret) |
| 1209 | return ret; |
| 1210 | /* |
| 1211 | * if unity mapping is in aperture range mark the page |
| 1212 | * as allocated in the aperture |
| 1213 | */ |
| 1214 | if (addr < dma_dom->aperture_size) |
Joerg Roedel | c323956 | 2009-05-12 10:56:44 +0200 | [diff] [blame] | 1215 | __set_bit(addr >> PAGE_SHIFT, |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1216 | dma_dom->aperture[0]->bitmap); |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1222 | /* |
Joerg Roedel | 171e7b3 | 2009-11-24 17:47:56 +0100 | [diff] [blame] | 1223 | * Init the unity mappings for a specific IOMMU in the system |
| 1224 | * |
| 1225 | * Basically iterates over all unity mapping entries and applies them to |
| 1226 | * the default domain DMA of that IOMMU if necessary. |
| 1227 | */ |
| 1228 | static int iommu_init_unity_mappings(struct amd_iommu *iommu) |
| 1229 | { |
| 1230 | struct unity_map_entry *entry; |
| 1231 | int ret; |
| 1232 | |
| 1233 | list_for_each_entry(entry, &amd_iommu_unity_map, list) { |
| 1234 | if (!iommu_for_unity_map(iommu, entry)) |
| 1235 | continue; |
| 1236 | ret = dma_ops_unity_map(iommu->default_dom, entry); |
| 1237 | if (ret) |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1245 | * Inits the unity mappings required for a specific device |
| 1246 | */ |
Joerg Roedel | bd0e521 | 2008-06-26 21:27:56 +0200 | [diff] [blame] | 1247 | static int init_unity_mappings_for_device(struct dma_ops_domain *dma_dom, |
| 1248 | u16 devid) |
| 1249 | { |
| 1250 | struct unity_map_entry *e; |
| 1251 | int ret; |
| 1252 | |
| 1253 | list_for_each_entry(e, &amd_iommu_unity_map, list) { |
| 1254 | if (!(devid >= e->devid_start && devid <= e->devid_end)) |
| 1255 | continue; |
| 1256 | ret = dma_ops_unity_map(dma_dom, e); |
| 1257 | if (ret) |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
| 1261 | return 0; |
| 1262 | } |
| 1263 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1264 | /**************************************************************************** |
| 1265 | * |
| 1266 | * The next functions belong to the address allocator for the dma_ops |
| 1267 | * interface functions. They work like the allocators in the other IOMMU |
| 1268 | * drivers. Its basically a bitmap which marks the allocated pages in |
| 1269 | * the aperture. Maybe it could be enhanced in the future to a more |
| 1270 | * efficient allocator. |
| 1271 | * |
| 1272 | ****************************************************************************/ |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1273 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1274 | /* |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1275 | * The address allocator core functions. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1276 | * |
| 1277 | * called with domain->lock held |
| 1278 | */ |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1279 | |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1280 | /* |
Joerg Roedel | 171e7b3 | 2009-11-24 17:47:56 +0100 | [diff] [blame] | 1281 | * Used to reserve address ranges in the aperture (e.g. for exclusion |
| 1282 | * ranges. |
| 1283 | */ |
| 1284 | static void dma_ops_reserve_addresses(struct dma_ops_domain *dom, |
| 1285 | unsigned long start_page, |
| 1286 | unsigned int pages) |
| 1287 | { |
| 1288 | unsigned int i, last_page = dom->aperture_size >> PAGE_SHIFT; |
| 1289 | |
| 1290 | if (start_page + pages > last_page) |
| 1291 | pages = last_page - start_page; |
| 1292 | |
| 1293 | for (i = start_page; i < start_page + pages; ++i) { |
| 1294 | int index = i / APERTURE_RANGE_PAGES; |
| 1295 | int page = i % APERTURE_RANGE_PAGES; |
| 1296 | __set_bit(page, dom->aperture[index]->bitmap); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | /* |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1301 | * This function is used to add a new aperture range to an existing |
| 1302 | * aperture in case of dma_ops domain allocation or address allocation |
| 1303 | * failure. |
| 1304 | */ |
Joerg Roedel | 576175c | 2009-11-23 19:08:46 +0100 | [diff] [blame] | 1305 | static int alloc_new_range(struct dma_ops_domain *dma_dom, |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1306 | bool populate, gfp_t gfp) |
| 1307 | { |
| 1308 | int index = dma_dom->aperture_size >> APERTURE_RANGE_SHIFT; |
Joerg Roedel | 576175c | 2009-11-23 19:08:46 +0100 | [diff] [blame] | 1309 | struct amd_iommu *iommu; |
Joerg Roedel | 17f5b56 | 2011-07-06 17:14:44 +0200 | [diff] [blame] | 1310 | unsigned long i, old_size; |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1311 | |
Joerg Roedel | f5e9705 | 2009-05-22 12:31:53 +0200 | [diff] [blame] | 1312 | #ifdef CONFIG_IOMMU_STRESS |
| 1313 | populate = false; |
| 1314 | #endif |
| 1315 | |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1316 | if (index >= APERTURE_MAX_RANGES) |
| 1317 | return -ENOMEM; |
| 1318 | |
| 1319 | dma_dom->aperture[index] = kzalloc(sizeof(struct aperture_range), gfp); |
| 1320 | if (!dma_dom->aperture[index]) |
| 1321 | return -ENOMEM; |
| 1322 | |
| 1323 | dma_dom->aperture[index]->bitmap = (void *)get_zeroed_page(gfp); |
| 1324 | if (!dma_dom->aperture[index]->bitmap) |
| 1325 | goto out_free; |
| 1326 | |
| 1327 | dma_dom->aperture[index]->offset = dma_dom->aperture_size; |
| 1328 | |
| 1329 | if (populate) { |
| 1330 | unsigned long address = dma_dom->aperture_size; |
| 1331 | int i, num_ptes = APERTURE_RANGE_PAGES / 512; |
| 1332 | u64 *pte, *pte_page; |
| 1333 | |
| 1334 | for (i = 0; i < num_ptes; ++i) { |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 1335 | pte = alloc_pte(&dma_dom->domain, address, PAGE_SIZE, |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1336 | &pte_page, gfp); |
| 1337 | if (!pte) |
| 1338 | goto out_free; |
| 1339 | |
| 1340 | dma_dom->aperture[index]->pte_pages[i] = pte_page; |
| 1341 | |
| 1342 | address += APERTURE_RANGE_SIZE / 64; |
| 1343 | } |
| 1344 | } |
| 1345 | |
Joerg Roedel | 17f5b56 | 2011-07-06 17:14:44 +0200 | [diff] [blame] | 1346 | old_size = dma_dom->aperture_size; |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1347 | dma_dom->aperture_size += APERTURE_RANGE_SIZE; |
| 1348 | |
Joerg Roedel | 17f5b56 | 2011-07-06 17:14:44 +0200 | [diff] [blame] | 1349 | /* Reserve address range used for MSI messages */ |
| 1350 | if (old_size < MSI_ADDR_BASE_LO && |
| 1351 | dma_dom->aperture_size > MSI_ADDR_BASE_LO) { |
| 1352 | unsigned long spage; |
| 1353 | int pages; |
| 1354 | |
| 1355 | pages = iommu_num_pages(MSI_ADDR_BASE_LO, 0x10000, PAGE_SIZE); |
| 1356 | spage = MSI_ADDR_BASE_LO >> PAGE_SHIFT; |
| 1357 | |
| 1358 | dma_ops_reserve_addresses(dma_dom, spage, pages); |
| 1359 | } |
| 1360 | |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1361 | /* Initialize the exclusion range if necessary */ |
Joerg Roedel | 576175c | 2009-11-23 19:08:46 +0100 | [diff] [blame] | 1362 | for_each_iommu(iommu) { |
| 1363 | if (iommu->exclusion_start && |
| 1364 | iommu->exclusion_start >= dma_dom->aperture[index]->offset |
| 1365 | && iommu->exclusion_start < dma_dom->aperture_size) { |
| 1366 | unsigned long startpage; |
| 1367 | int pages = iommu_num_pages(iommu->exclusion_start, |
| 1368 | iommu->exclusion_length, |
| 1369 | PAGE_SIZE); |
| 1370 | startpage = iommu->exclusion_start >> PAGE_SHIFT; |
| 1371 | dma_ops_reserve_addresses(dma_dom, startpage, pages); |
| 1372 | } |
Joerg Roedel | 00cd122 | 2009-05-19 09:52:40 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /* |
| 1376 | * Check for areas already mapped as present in the new aperture |
| 1377 | * range and mark those pages as reserved in the allocator. Such |
| 1378 | * mappings may already exist as a result of requested unity |
| 1379 | * mappings for devices. |
| 1380 | */ |
| 1381 | for (i = dma_dom->aperture[index]->offset; |
| 1382 | i < dma_dom->aperture_size; |
| 1383 | i += PAGE_SIZE) { |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 1384 | u64 *pte = fetch_pte(&dma_dom->domain, i); |
Joerg Roedel | 00cd122 | 2009-05-19 09:52:40 +0200 | [diff] [blame] | 1385 | if (!pte || !IOMMU_PTE_PRESENT(*pte)) |
| 1386 | continue; |
| 1387 | |
Joerg Roedel | fcd0861 | 2011-10-11 17:41:32 +0200 | [diff] [blame] | 1388 | dma_ops_reserve_addresses(dma_dom, i >> PAGE_SHIFT, 1); |
Joerg Roedel | 00cd122 | 2009-05-19 09:52:40 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 1391 | update_domain(&dma_dom->domain); |
| 1392 | |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1393 | return 0; |
| 1394 | |
| 1395 | out_free: |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 1396 | update_domain(&dma_dom->domain); |
| 1397 | |
Joerg Roedel | 9cabe89 | 2009-05-18 16:38:55 +0200 | [diff] [blame] | 1398 | free_page((unsigned long)dma_dom->aperture[index]->bitmap); |
| 1399 | |
| 1400 | kfree(dma_dom->aperture[index]); |
| 1401 | dma_dom->aperture[index] = NULL; |
| 1402 | |
| 1403 | return -ENOMEM; |
| 1404 | } |
| 1405 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1406 | static unsigned long dma_ops_area_alloc(struct device *dev, |
| 1407 | struct dma_ops_domain *dom, |
| 1408 | unsigned int pages, |
| 1409 | unsigned long align_mask, |
| 1410 | u64 dma_mask, |
| 1411 | unsigned long start) |
| 1412 | { |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1413 | unsigned long next_bit = dom->next_address % APERTURE_RANGE_SIZE; |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1414 | int max_index = dom->aperture_size >> APERTURE_RANGE_SHIFT; |
| 1415 | int i = start >> APERTURE_RANGE_SHIFT; |
| 1416 | unsigned long boundary_size; |
| 1417 | unsigned long address = -1; |
| 1418 | unsigned long limit; |
| 1419 | |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1420 | next_bit >>= PAGE_SHIFT; |
| 1421 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1422 | boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, |
| 1423 | PAGE_SIZE) >> PAGE_SHIFT; |
| 1424 | |
| 1425 | for (;i < max_index; ++i) { |
| 1426 | unsigned long offset = dom->aperture[i]->offset >> PAGE_SHIFT; |
| 1427 | |
| 1428 | if (dom->aperture[i]->offset >= dma_mask) |
| 1429 | break; |
| 1430 | |
| 1431 | limit = iommu_device_max_index(APERTURE_RANGE_PAGES, offset, |
| 1432 | dma_mask >> PAGE_SHIFT); |
| 1433 | |
| 1434 | address = iommu_area_alloc(dom->aperture[i]->bitmap, |
| 1435 | limit, next_bit, pages, 0, |
| 1436 | boundary_size, align_mask); |
| 1437 | if (address != -1) { |
| 1438 | address = dom->aperture[i]->offset + |
| 1439 | (address << PAGE_SHIFT); |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1440 | dom->next_address = address + (pages << PAGE_SHIFT); |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | |
| 1444 | next_bit = 0; |
| 1445 | } |
| 1446 | |
| 1447 | return address; |
| 1448 | } |
| 1449 | |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1450 | static unsigned long dma_ops_alloc_addresses(struct device *dev, |
| 1451 | struct dma_ops_domain *dom, |
Joerg Roedel | 6d4f343 | 2008-09-04 19:18:02 +0200 | [diff] [blame] | 1452 | unsigned int pages, |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 1453 | unsigned long align_mask, |
| 1454 | u64 dma_mask) |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1455 | { |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1456 | unsigned long address; |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1457 | |
Joerg Roedel | fe16f08 | 2009-05-22 12:27:53 +0200 | [diff] [blame] | 1458 | #ifdef CONFIG_IOMMU_STRESS |
| 1459 | dom->next_address = 0; |
| 1460 | dom->need_flush = true; |
| 1461 | #endif |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1462 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1463 | address = dma_ops_area_alloc(dev, dom, pages, align_mask, |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1464 | dma_mask, dom->next_address); |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1465 | |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 1466 | if (address == -1) { |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1467 | dom->next_address = 0; |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1468 | address = dma_ops_area_alloc(dev, dom, pages, align_mask, |
| 1469 | dma_mask, 0); |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 1470 | dom->need_flush = true; |
| 1471 | } |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1472 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1473 | if (unlikely(address == -1)) |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 1474 | address = DMA_ERROR_CODE; |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1475 | |
| 1476 | WARN_ON((address + (PAGE_SIZE*pages)) > dom->aperture_size); |
| 1477 | |
| 1478 | return address; |
| 1479 | } |
| 1480 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1481 | /* |
| 1482 | * The address free function. |
| 1483 | * |
| 1484 | * called with domain->lock held |
| 1485 | */ |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1486 | static void dma_ops_free_addresses(struct dma_ops_domain *dom, |
| 1487 | unsigned long address, |
| 1488 | unsigned int pages) |
| 1489 | { |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1490 | unsigned i = address >> APERTURE_RANGE_SHIFT; |
| 1491 | struct aperture_range *range = dom->aperture[i]; |
Joerg Roedel | 80be308 | 2008-11-06 14:59:05 +0100 | [diff] [blame] | 1492 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1493 | BUG_ON(i >= APERTURE_MAX_RANGES || range == NULL); |
| 1494 | |
Joerg Roedel | 47bccd6 | 2009-05-22 12:40:54 +0200 | [diff] [blame] | 1495 | #ifdef CONFIG_IOMMU_STRESS |
| 1496 | if (i < 4) |
| 1497 | return; |
| 1498 | #endif |
| 1499 | |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1500 | if (address >= dom->next_address) |
Joerg Roedel | 80be308 | 2008-11-06 14:59:05 +0100 | [diff] [blame] | 1501 | dom->need_flush = true; |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1502 | |
| 1503 | address = (address % APERTURE_RANGE_SIZE) >> PAGE_SHIFT; |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1504 | |
Akinobu Mita | a66022c | 2009-12-15 16:48:28 -0800 | [diff] [blame] | 1505 | bitmap_clear(range->bitmap, address, pages); |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1506 | |
Joerg Roedel | d308644 | 2008-06-26 21:27:57 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1509 | /**************************************************************************** |
| 1510 | * |
| 1511 | * The next functions belong to the domain allocation. A domain is |
| 1512 | * allocated for every IOMMU as the default domain. If device isolation |
| 1513 | * is enabled, every device get its own domain. The most important thing |
| 1514 | * about domains is the page table mapping the DMA address space they |
| 1515 | * contain. |
| 1516 | * |
| 1517 | ****************************************************************************/ |
| 1518 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 1519 | /* |
| 1520 | * This function adds a protection domain to the global protection domain list |
| 1521 | */ |
| 1522 | static void add_domain_to_list(struct protection_domain *domain) |
| 1523 | { |
| 1524 | unsigned long flags; |
| 1525 | |
| 1526 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); |
| 1527 | list_add(&domain->list, &amd_iommu_pd_list); |
| 1528 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * This function removes a protection domain to the global |
| 1533 | * protection domain list |
| 1534 | */ |
| 1535 | static void del_domain_from_list(struct protection_domain *domain) |
| 1536 | { |
| 1537 | unsigned long flags; |
| 1538 | |
| 1539 | spin_lock_irqsave(&amd_iommu_pd_lock, flags); |
| 1540 | list_del(&domain->list); |
| 1541 | spin_unlock_irqrestore(&amd_iommu_pd_lock, flags); |
| 1542 | } |
| 1543 | |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1544 | static u16 domain_id_alloc(void) |
| 1545 | { |
| 1546 | unsigned long flags; |
| 1547 | int id; |
| 1548 | |
| 1549 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 1550 | id = find_first_zero_bit(amd_iommu_pd_alloc_bitmap, MAX_DOMAIN_ID); |
| 1551 | BUG_ON(id == 0); |
| 1552 | if (id > 0 && id < MAX_DOMAIN_ID) |
| 1553 | __set_bit(id, amd_iommu_pd_alloc_bitmap); |
| 1554 | else |
| 1555 | id = 0; |
| 1556 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 1557 | |
| 1558 | return id; |
| 1559 | } |
| 1560 | |
Joerg Roedel | a2acfb7 | 2008-12-02 18:28:53 +0100 | [diff] [blame] | 1561 | static void domain_id_free(int id) |
| 1562 | { |
| 1563 | unsigned long flags; |
| 1564 | |
| 1565 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 1566 | if (id > 0 && id < MAX_DOMAIN_ID) |
| 1567 | __clear_bit(id, amd_iommu_pd_alloc_bitmap); |
| 1568 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 1569 | } |
Joerg Roedel | a2acfb7 | 2008-12-02 18:28:53 +0100 | [diff] [blame] | 1570 | |
Joerg Roedel | 86db2e5 | 2008-12-02 18:20:21 +0100 | [diff] [blame] | 1571 | static void free_pagetable(struct protection_domain *domain) |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1572 | { |
| 1573 | int i, j; |
| 1574 | u64 *p1, *p2, *p3; |
| 1575 | |
Joerg Roedel | 86db2e5 | 2008-12-02 18:20:21 +0100 | [diff] [blame] | 1576 | p1 = domain->pt_root; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1577 | |
| 1578 | if (!p1) |
| 1579 | return; |
| 1580 | |
| 1581 | for (i = 0; i < 512; ++i) { |
| 1582 | if (!IOMMU_PTE_PRESENT(p1[i])) |
| 1583 | continue; |
| 1584 | |
| 1585 | p2 = IOMMU_PTE_PAGE(p1[i]); |
Joerg Roedel | 3cc3d84 | 2008-12-04 16:44:31 +0100 | [diff] [blame] | 1586 | for (j = 0; j < 512; ++j) { |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1587 | if (!IOMMU_PTE_PRESENT(p2[j])) |
| 1588 | continue; |
| 1589 | p3 = IOMMU_PTE_PAGE(p2[j]); |
| 1590 | free_page((unsigned long)p3); |
| 1591 | } |
| 1592 | |
| 1593 | free_page((unsigned long)p2); |
| 1594 | } |
| 1595 | |
| 1596 | free_page((unsigned long)p1); |
Joerg Roedel | 86db2e5 | 2008-12-02 18:20:21 +0100 | [diff] [blame] | 1597 | |
| 1598 | domain->pt_root = NULL; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1601 | /* |
| 1602 | * Free a domain, only used if something went wrong in the |
| 1603 | * allocation path and we need to free an already allocated page table |
| 1604 | */ |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1605 | static void dma_ops_domain_free(struct dma_ops_domain *dom) |
| 1606 | { |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1607 | int i; |
| 1608 | |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1609 | if (!dom) |
| 1610 | return; |
| 1611 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 1612 | del_domain_from_list(&dom->domain); |
| 1613 | |
Joerg Roedel | 86db2e5 | 2008-12-02 18:20:21 +0100 | [diff] [blame] | 1614 | free_pagetable(&dom->domain); |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1615 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1616 | for (i = 0; i < APERTURE_MAX_RANGES; ++i) { |
| 1617 | if (!dom->aperture[i]) |
| 1618 | continue; |
| 1619 | free_page((unsigned long)dom->aperture[i]->bitmap); |
| 1620 | kfree(dom->aperture[i]); |
| 1621 | } |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1622 | |
| 1623 | kfree(dom); |
| 1624 | } |
| 1625 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1626 | /* |
| 1627 | * 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] | 1628 | * It also initializes the page table and the address allocator data |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1629 | * structures required for the dma_ops interface |
| 1630 | */ |
Joerg Roedel | 87a64d5 | 2009-11-24 17:26:43 +0100 | [diff] [blame] | 1631 | static struct dma_ops_domain *dma_ops_domain_alloc(void) |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1632 | { |
| 1633 | struct dma_ops_domain *dma_dom; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1634 | |
| 1635 | dma_dom = kzalloc(sizeof(struct dma_ops_domain), GFP_KERNEL); |
| 1636 | if (!dma_dom) |
| 1637 | return NULL; |
| 1638 | |
| 1639 | spin_lock_init(&dma_dom->domain.lock); |
| 1640 | |
| 1641 | dma_dom->domain.id = domain_id_alloc(); |
| 1642 | if (dma_dom->domain.id == 0) |
| 1643 | goto free_dma_dom; |
Joerg Roedel | 7c392cb | 2009-11-26 11:13:32 +0100 | [diff] [blame] | 1644 | INIT_LIST_HEAD(&dma_dom->domain.dev_list); |
Joerg Roedel | 8f7a017 | 2009-09-02 16:55:24 +0200 | [diff] [blame] | 1645 | dma_dom->domain.mode = PAGE_MODE_2_LEVEL; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1646 | dma_dom->domain.pt_root = (void *)get_zeroed_page(GFP_KERNEL); |
Joerg Roedel | 9fdb19d | 2008-12-02 17:46:25 +0100 | [diff] [blame] | 1647 | dma_dom->domain.flags = PD_DMA_OPS_MASK; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1648 | dma_dom->domain.priv = dma_dom; |
| 1649 | if (!dma_dom->domain.pt_root) |
| 1650 | goto free_dma_dom; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1651 | |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 1652 | dma_dom->need_flush = false; |
Joerg Roedel | bd60b73 | 2008-09-11 10:24:48 +0200 | [diff] [blame] | 1653 | dma_dom->target_dev = 0xffff; |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 1654 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 1655 | add_domain_to_list(&dma_dom->domain); |
| 1656 | |
Joerg Roedel | 576175c | 2009-11-23 19:08:46 +0100 | [diff] [blame] | 1657 | if (alloc_new_range(dma_dom, true, GFP_KERNEL)) |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1658 | goto free_dma_dom; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1659 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1660 | /* |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1661 | * mark the first page as allocated so we never return 0 as |
| 1662 | * a valid dma-address. So we can use 0 as error value |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1663 | */ |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 1664 | dma_dom->aperture[0]->bitmap[0] = 1; |
Joerg Roedel | 803b8cb | 2009-05-18 15:32:48 +0200 | [diff] [blame] | 1665 | dma_dom->next_address = 0; |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1666 | |
Joerg Roedel | ec487d1 | 2008-06-26 21:27:58 +0200 | [diff] [blame] | 1667 | |
| 1668 | return dma_dom; |
| 1669 | |
| 1670 | free_dma_dom: |
| 1671 | dma_ops_domain_free(dma_dom); |
| 1672 | |
| 1673 | return NULL; |
| 1674 | } |
| 1675 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1676 | /* |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 1677 | * little helper function to check whether a given protection domain is a |
| 1678 | * dma_ops domain |
| 1679 | */ |
| 1680 | static bool dma_ops_domain(struct protection_domain *domain) |
| 1681 | { |
| 1682 | return domain->flags & PD_DMA_OPS_MASK; |
| 1683 | } |
| 1684 | |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1685 | 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] | 1686 | { |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame^] | 1687 | u64 pte_root = 0; |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 1688 | u64 flags = 0; |
Joerg Roedel | 863c74e | 2008-12-02 17:56:36 +0100 | [diff] [blame] | 1689 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame^] | 1690 | if (domain->mode != PAGE_MODE_NONE) |
| 1691 | pte_root = virt_to_phys(domain->pt_root); |
| 1692 | |
Joerg Roedel | 38ddf41 | 2008-09-11 10:38:32 +0200 | [diff] [blame] | 1693 | pte_root |= (domain->mode & DEV_ENTRY_MODE_MASK) |
| 1694 | << DEV_ENTRY_MODE_SHIFT; |
| 1695 | 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] | 1696 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 1697 | flags = amd_iommu_dev_table[devid].data[1]; |
| 1698 | |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1699 | if (ats) |
| 1700 | flags |= DTE_FLAG_IOTLB; |
| 1701 | |
Joerg Roedel | ee6c286 | 2011-11-09 12:06:03 +0100 | [diff] [blame] | 1702 | flags &= ~(0xffffUL); |
| 1703 | flags |= domain->id; |
| 1704 | |
| 1705 | amd_iommu_dev_table[devid].data[1] = flags; |
| 1706 | amd_iommu_dev_table[devid].data[0] = pte_root; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 1707 | } |
| 1708 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1709 | static void clear_dte_entry(u16 devid) |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1710 | { |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1711 | /* remove entry from the device table seen by the hardware */ |
| 1712 | amd_iommu_dev_table[devid].data[0] = IOMMU_PTE_P | IOMMU_PTE_TV; |
| 1713 | amd_iommu_dev_table[devid].data[1] = 0; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1714 | |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 1715 | amd_iommu_apply_erratum_63(devid); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1716 | } |
| 1717 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1718 | static void do_attach(struct iommu_dev_data *dev_data, |
| 1719 | struct protection_domain *domain) |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1720 | { |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1721 | struct amd_iommu *iommu; |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1722 | bool ats; |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1723 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1724 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
| 1725 | ats = dev_data->ats.enabled; |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1726 | |
| 1727 | /* Update data structures */ |
| 1728 | dev_data->domain = domain; |
| 1729 | list_add(&dev_data->list, &domain->dev_list); |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 1730 | set_dte_entry(dev_data->devid, domain, ats); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1731 | |
| 1732 | /* Do reference counting */ |
| 1733 | domain->dev_iommu[iommu->index] += 1; |
| 1734 | domain->dev_cnt += 1; |
| 1735 | |
| 1736 | /* Flush the DTE entry */ |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1737 | device_flush_dte(dev_data); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1738 | } |
| 1739 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1740 | static void do_detach(struct iommu_dev_data *dev_data) |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1741 | { |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1742 | struct amd_iommu *iommu; |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1743 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1744 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | c5cca14 | 2009-10-09 18:31:20 +0200 | [diff] [blame] | 1745 | |
Joerg Roedel | c459611 | 2009-11-20 14:57:32 +0100 | [diff] [blame] | 1746 | /* decrease reference counters */ |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1747 | dev_data->domain->dev_iommu[iommu->index] -= 1; |
| 1748 | dev_data->domain->dev_cnt -= 1; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1749 | |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1750 | /* Update data structures */ |
| 1751 | dev_data->domain = NULL; |
| 1752 | list_del(&dev_data->list); |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 1753 | clear_dte_entry(dev_data->devid); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1754 | |
| 1755 | /* Flush the DTE entry */ |
Joerg Roedel | 6c54204 | 2011-06-09 17:07:31 +0200 | [diff] [blame] | 1756 | device_flush_dte(dev_data); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | /* |
| 1760 | * If a device is not yet associated with a domain, this function does |
| 1761 | * assigns it visible for the hardware |
| 1762 | */ |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1763 | static int __attach_device(struct iommu_dev_data *dev_data, |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1764 | struct protection_domain *domain) |
| 1765 | { |
Julia Lawall | 84fe6c1 | 2010-05-27 12:31:51 +0200 | [diff] [blame] | 1766 | int ret; |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 1767 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1768 | /* lock domain */ |
| 1769 | spin_lock(&domain->lock); |
| 1770 | |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 1771 | if (dev_data->alias_data != NULL) { |
| 1772 | struct iommu_dev_data *alias_data = dev_data->alias_data; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1773 | |
Joerg Roedel | 2b02b09 | 2011-06-09 17:48:39 +0200 | [diff] [blame] | 1774 | /* Some sanity checks */ |
| 1775 | ret = -EBUSY; |
| 1776 | if (alias_data->domain != NULL && |
| 1777 | alias_data->domain != domain) |
| 1778 | goto out_unlock; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1779 | |
Joerg Roedel | 2b02b09 | 2011-06-09 17:48:39 +0200 | [diff] [blame] | 1780 | if (dev_data->domain != NULL && |
| 1781 | dev_data->domain != domain) |
| 1782 | goto out_unlock; |
| 1783 | |
| 1784 | /* Do real assignment */ |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1785 | if (alias_data->domain == NULL) |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1786 | do_attach(alias_data, domain); |
Joerg Roedel | 2410005 | 2009-11-25 15:59:57 +0100 | [diff] [blame] | 1787 | |
| 1788 | atomic_inc(&alias_data->bind); |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 1789 | } |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1790 | |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1791 | if (dev_data->domain == NULL) |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1792 | do_attach(dev_data, domain); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1793 | |
Joerg Roedel | 2410005 | 2009-11-25 15:59:57 +0100 | [diff] [blame] | 1794 | atomic_inc(&dev_data->bind); |
| 1795 | |
Julia Lawall | 84fe6c1 | 2010-05-27 12:31:51 +0200 | [diff] [blame] | 1796 | ret = 0; |
| 1797 | |
| 1798 | out_unlock: |
| 1799 | |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1800 | /* ready */ |
| 1801 | spin_unlock(&domain->lock); |
Joerg Roedel | 21129f7 | 2009-09-01 11:59:42 +0200 | [diff] [blame] | 1802 | |
Julia Lawall | 84fe6c1 | 2010-05-27 12:31:51 +0200 | [diff] [blame] | 1803 | return ret; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * If a device is not yet associated with a domain, this function does |
| 1808 | * assigns it visible for the hardware |
| 1809 | */ |
| 1810 | static int attach_device(struct device *dev, |
| 1811 | struct protection_domain *domain) |
| 1812 | { |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1813 | struct pci_dev *pdev = to_pci_dev(dev); |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1814 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1815 | unsigned long flags; |
| 1816 | int ret; |
| 1817 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1818 | dev_data = get_dev_data(dev); |
| 1819 | |
| 1820 | if (amd_iommu_iotlb_sup && pci_enable_ats(pdev, PAGE_SHIFT) == 0) { |
| 1821 | dev_data->ats.enabled = true; |
| 1822 | dev_data->ats.qdep = pci_ats_queue_depth(pdev); |
| 1823 | } |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1824 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1825 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1826 | ret = __attach_device(dev_data, domain); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1827 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 1828 | |
| 1829 | /* |
| 1830 | * We might boot into a crash-kernel here. The crashed kernel |
| 1831 | * left the caches in the IOMMU dirty. So we have to flush |
| 1832 | * here to evict all dirty stuff. |
| 1833 | */ |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 1834 | domain_flush_tlb_pde(domain); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1835 | |
| 1836 | return ret; |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * Removes a device from a protection domain (unlocked) |
| 1841 | */ |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1842 | static void __detach_device(struct iommu_dev_data *dev_data) |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1843 | { |
Joerg Roedel | 2ca7627 | 2010-01-22 16:45:31 +0100 | [diff] [blame] | 1844 | struct protection_domain *domain; |
Joerg Roedel | 7c392cb | 2009-11-26 11:13:32 +0100 | [diff] [blame] | 1845 | unsigned long flags; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1846 | |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1847 | BUG_ON(!dev_data->domain); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1848 | |
Joerg Roedel | 2ca7627 | 2010-01-22 16:45:31 +0100 | [diff] [blame] | 1849 | domain = dev_data->domain; |
| 1850 | |
| 1851 | spin_lock_irqsave(&domain->lock, flags); |
Joerg Roedel | 2410005 | 2009-11-25 15:59:57 +0100 | [diff] [blame] | 1852 | |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 1853 | if (dev_data->alias_data != NULL) { |
| 1854 | struct iommu_dev_data *alias_data = dev_data->alias_data; |
| 1855 | |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1856 | if (atomic_dec_and_test(&alias_data->bind)) |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1857 | do_detach(alias_data); |
Joerg Roedel | 2410005 | 2009-11-25 15:59:57 +0100 | [diff] [blame] | 1858 | } |
| 1859 | |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1860 | if (atomic_dec_and_test(&dev_data->bind)) |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1861 | do_detach(dev_data); |
Joerg Roedel | 7f760dd | 2009-11-26 14:49:59 +0100 | [diff] [blame] | 1862 | |
Joerg Roedel | 2ca7627 | 2010-01-22 16:45:31 +0100 | [diff] [blame] | 1863 | spin_unlock_irqrestore(&domain->lock, flags); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1864 | |
Joerg Roedel | 21129f7 | 2009-09-01 11:59:42 +0200 | [diff] [blame] | 1865 | /* |
| 1866 | * 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] | 1867 | * passthrough domain if it is detached from any other domain. |
| 1868 | * Make sure we can deassign from the pt_domain itself. |
Joerg Roedel | 21129f7 | 2009-09-01 11:59:42 +0200 | [diff] [blame] | 1869 | */ |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 1870 | if (dev_data->passthrough && |
Joerg Roedel | d3ad937 | 2010-01-22 17:55:27 +0100 | [diff] [blame] | 1871 | (dev_data->domain == NULL && domain != pt_domain)) |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1872 | __attach_device(dev_data, pt_domain); |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | /* |
| 1876 | * Removes a device from a protection domain (with devtable_lock held) |
| 1877 | */ |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1878 | static void detach_device(struct device *dev) |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1879 | { |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1880 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1881 | unsigned long flags; |
| 1882 | |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1883 | dev_data = get_dev_data(dev); |
| 1884 | |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1885 | /* lock device table */ |
| 1886 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 1887 | __detach_device(dev_data); |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1888 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
Joerg Roedel | fd7b553 | 2011-04-05 15:31:08 +0200 | [diff] [blame] | 1889 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 1890 | if (dev_data->ats.enabled) { |
| 1891 | pci_disable_ats(to_pci_dev(dev)); |
| 1892 | dev_data->ats.enabled = false; |
| 1893 | } |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1894 | } |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1895 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1896 | /* |
| 1897 | * Find out the protection domain structure for a given PCI device. This |
| 1898 | * will give us the pointer to the page table root for example. |
| 1899 | */ |
| 1900 | static struct protection_domain *domain_for_device(struct device *dev) |
| 1901 | { |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 1902 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 2b02b09 | 2011-06-09 17:48:39 +0200 | [diff] [blame] | 1903 | struct protection_domain *dom = NULL; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1904 | unsigned long flags; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1905 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 1906 | dev_data = get_dev_data(dev); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1907 | |
Joerg Roedel | 2b02b09 | 2011-06-09 17:48:39 +0200 | [diff] [blame] | 1908 | if (dev_data->domain) |
| 1909 | return dev_data->domain; |
| 1910 | |
Joerg Roedel | 71f7758 | 2011-06-09 19:03:15 +0200 | [diff] [blame] | 1911 | if (dev_data->alias_data != NULL) { |
| 1912 | struct iommu_dev_data *alias_data = dev_data->alias_data; |
Joerg Roedel | 2b02b09 | 2011-06-09 17:48:39 +0200 | [diff] [blame] | 1913 | |
| 1914 | read_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 1915 | if (alias_data->domain != NULL) { |
| 1916 | __attach_device(dev_data, alias_data->domain); |
| 1917 | dom = alias_data->domain; |
| 1918 | } |
| 1919 | read_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1920 | } |
| 1921 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1922 | return dom; |
| 1923 | } |
| 1924 | |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1925 | static int device_change_notifier(struct notifier_block *nb, |
| 1926 | unsigned long action, void *data) |
| 1927 | { |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1928 | struct dma_ops_domain *dma_domain; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 1929 | struct protection_domain *domain; |
| 1930 | struct iommu_dev_data *dev_data; |
| 1931 | struct device *dev = data; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1932 | struct amd_iommu *iommu; |
Joerg Roedel | 1ac4cbb | 2008-12-10 19:33:26 +0100 | [diff] [blame] | 1933 | unsigned long flags; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 1934 | u16 devid; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1935 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 1936 | if (!check_device(dev)) |
| 1937 | return 0; |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1938 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 1939 | devid = get_device_id(dev); |
| 1940 | iommu = amd_iommu_rlookup_table[devid]; |
| 1941 | dev_data = get_dev_data(dev); |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1942 | |
| 1943 | switch (action) { |
Chris Wright | c1eee67 | 2009-05-21 00:56:58 -0700 | [diff] [blame] | 1944 | case BUS_NOTIFY_UNBOUND_DRIVER: |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 1945 | |
| 1946 | domain = domain_for_device(dev); |
| 1947 | |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1948 | if (!domain) |
| 1949 | goto out; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 1950 | if (dev_data->passthrough) |
Joerg Roedel | a1ca331 | 2009-09-01 12:22:22 +0200 | [diff] [blame] | 1951 | break; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 1952 | detach_device(dev); |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1953 | break; |
Joerg Roedel | 1ac4cbb | 2008-12-10 19:33:26 +0100 | [diff] [blame] | 1954 | case BUS_NOTIFY_ADD_DEVICE: |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 1955 | |
| 1956 | iommu_init_device(dev); |
| 1957 | |
| 1958 | domain = domain_for_device(dev); |
| 1959 | |
Joerg Roedel | 1ac4cbb | 2008-12-10 19:33:26 +0100 | [diff] [blame] | 1960 | /* allocate a protection domain if a device is added */ |
| 1961 | dma_domain = find_protection_domain(devid); |
| 1962 | if (dma_domain) |
| 1963 | goto out; |
Joerg Roedel | 87a64d5 | 2009-11-24 17:26:43 +0100 | [diff] [blame] | 1964 | dma_domain = dma_ops_domain_alloc(); |
Joerg Roedel | 1ac4cbb | 2008-12-10 19:33:26 +0100 | [diff] [blame] | 1965 | if (!dma_domain) |
| 1966 | goto out; |
| 1967 | dma_domain->target_dev = devid; |
| 1968 | |
| 1969 | spin_lock_irqsave(&iommu_pd_list_lock, flags); |
| 1970 | list_add_tail(&dma_domain->list, &iommu_pd_list); |
| 1971 | spin_unlock_irqrestore(&iommu_pd_list_lock, flags); |
| 1972 | |
| 1973 | break; |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 1974 | case BUS_NOTIFY_DEL_DEVICE: |
| 1975 | |
| 1976 | iommu_uninit_device(dev); |
| 1977 | |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1978 | default: |
| 1979 | goto out; |
| 1980 | } |
| 1981 | |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1982 | iommu_completion_wait(iommu); |
| 1983 | |
| 1984 | out: |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Jaswinder Singh Rajput | b25ae67 | 2009-07-01 19:53:14 +0530 | [diff] [blame] | 1988 | static struct notifier_block device_nb = { |
Joerg Roedel | e275a2a | 2008-12-10 18:27:25 +0100 | [diff] [blame] | 1989 | .notifier_call = device_change_notifier, |
| 1990 | }; |
Joerg Roedel | 355bf55 | 2008-12-08 12:02:41 +0100 | [diff] [blame] | 1991 | |
Joerg Roedel | 8638c49 | 2009-12-10 11:12:25 +0100 | [diff] [blame] | 1992 | void amd_iommu_init_notifier(void) |
| 1993 | { |
| 1994 | bus_register_notifier(&pci_bus_type, &device_nb); |
| 1995 | } |
| 1996 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 1997 | /***************************************************************************** |
| 1998 | * |
| 1999 | * The next functions belong to the dma_ops mapping/unmapping code. |
| 2000 | * |
| 2001 | *****************************************************************************/ |
| 2002 | |
| 2003 | /* |
| 2004 | * In the dma_ops path we only have the struct device. This function |
| 2005 | * finds the corresponding IOMMU, the protection domain and the |
| 2006 | * requestor id for a given device. |
| 2007 | * If the device is not yet associated with a domain this is also done |
| 2008 | * in this function. |
| 2009 | */ |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2010 | static struct protection_domain *get_domain(struct device *dev) |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2011 | { |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2012 | struct protection_domain *domain; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2013 | struct dma_ops_domain *dma_dom; |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2014 | u16 devid = get_device_id(dev); |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2015 | |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2016 | if (!check_device(dev)) |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2017 | return ERR_PTR(-EINVAL); |
Joerg Roedel | dbcc112 | 2008-09-04 15:04:26 +0200 | [diff] [blame] | 2018 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2019 | domain = domain_for_device(dev); |
| 2020 | if (domain != NULL && !dma_ops_domain(domain)) |
| 2021 | return ERR_PTR(-EBUSY); |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2022 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2023 | if (domain != NULL) |
| 2024 | return domain; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2025 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2026 | /* Device not bount yet - bind it */ |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2027 | dma_dom = find_protection_domain(devid); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2028 | if (!dma_dom) |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2029 | dma_dom = amd_iommu_rlookup_table[devid]->default_dom; |
| 2030 | attach_device(dev, &dma_dom->domain); |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2031 | DUMP_printk("Using protection domain %d for device %s\n", |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2032 | dma_dom->domain.id, dev_name(dev)); |
Joerg Roedel | f91ba19 | 2008-11-25 12:56:12 +0100 | [diff] [blame] | 2033 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2034 | return &dma_dom->domain; |
Joerg Roedel | b20ac0d | 2008-06-26 21:27:59 +0200 | [diff] [blame] | 2035 | } |
| 2036 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2037 | static void update_device_table(struct protection_domain *domain) |
| 2038 | { |
Joerg Roedel | 492667d | 2009-11-27 13:25:47 +0100 | [diff] [blame] | 2039 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2040 | |
Joerg Roedel | ea61cdd | 2011-06-09 12:56:30 +0200 | [diff] [blame] | 2041 | list_for_each_entry(dev_data, &domain->dev_list, list) |
| 2042 | set_dte_entry(dev_data->devid, domain, dev_data->ats.enabled); |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | static void update_domain(struct protection_domain *domain) |
| 2046 | { |
| 2047 | if (!domain->updated) |
| 2048 | return; |
| 2049 | |
| 2050 | update_device_table(domain); |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2051 | |
| 2052 | domain_flush_devices(domain); |
| 2053 | domain_flush_tlb_pde(domain); |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2054 | |
| 2055 | domain->updated = false; |
| 2056 | } |
| 2057 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2058 | /* |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 2059 | * This function fetches the PTE for a given address in the aperture |
| 2060 | */ |
| 2061 | static u64* dma_ops_get_pte(struct dma_ops_domain *dom, |
| 2062 | unsigned long address) |
| 2063 | { |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 2064 | struct aperture_range *aperture; |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 2065 | u64 *pte, *pte_page; |
| 2066 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 2067 | aperture = dom->aperture[APERTURE_RANGE_INDEX(address)]; |
| 2068 | if (!aperture) |
| 2069 | return NULL; |
| 2070 | |
| 2071 | pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)]; |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 2072 | if (!pte) { |
Joerg Roedel | cbb9d72 | 2010-01-15 14:41:15 +0100 | [diff] [blame] | 2073 | pte = alloc_pte(&dom->domain, address, PAGE_SIZE, &pte_page, |
Joerg Roedel | abdc5eb | 2009-09-03 11:33:51 +0200 | [diff] [blame] | 2074 | GFP_ATOMIC); |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 2075 | aperture->pte_pages[APERTURE_PAGE_INDEX(address)] = pte_page; |
| 2076 | } else |
Joerg Roedel | 8c8c143 | 2009-09-02 17:30:00 +0200 | [diff] [blame] | 2077 | pte += PM_LEVEL_INDEX(0, address); |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 2078 | |
Joerg Roedel | 04bfdd8 | 2009-09-02 16:00:23 +0200 | [diff] [blame] | 2079 | update_domain(&dom->domain); |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 2080 | |
| 2081 | return pte; |
| 2082 | } |
| 2083 | |
| 2084 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2085 | * This is the generic map function. It maps one 4kb page at paddr to |
| 2086 | * the given address in the DMA address space for the domain. |
| 2087 | */ |
Joerg Roedel | 680525e | 2009-11-23 18:44:42 +0100 | [diff] [blame] | 2088 | 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] | 2089 | unsigned long address, |
| 2090 | phys_addr_t paddr, |
| 2091 | int direction) |
| 2092 | { |
| 2093 | u64 *pte, __pte; |
| 2094 | |
| 2095 | WARN_ON(address > dom->aperture_size); |
| 2096 | |
| 2097 | paddr &= PAGE_MASK; |
| 2098 | |
Joerg Roedel | 8bda309 | 2009-05-12 12:02:46 +0200 | [diff] [blame] | 2099 | pte = dma_ops_get_pte(dom, address); |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2100 | if (!pte) |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2101 | return DMA_ERROR_CODE; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2102 | |
| 2103 | __pte = paddr | IOMMU_PTE_P | IOMMU_PTE_FC; |
| 2104 | |
| 2105 | if (direction == DMA_TO_DEVICE) |
| 2106 | __pte |= IOMMU_PTE_IR; |
| 2107 | else if (direction == DMA_FROM_DEVICE) |
| 2108 | __pte |= IOMMU_PTE_IW; |
| 2109 | else if (direction == DMA_BIDIRECTIONAL) |
| 2110 | __pte |= IOMMU_PTE_IR | IOMMU_PTE_IW; |
| 2111 | |
| 2112 | WARN_ON(*pte); |
| 2113 | |
| 2114 | *pte = __pte; |
| 2115 | |
| 2116 | return (dma_addr_t)address; |
| 2117 | } |
| 2118 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2119 | /* |
| 2120 | * The generic unmapping function for on page in the DMA address space. |
| 2121 | */ |
Joerg Roedel | 680525e | 2009-11-23 18:44:42 +0100 | [diff] [blame] | 2122 | static void dma_ops_domain_unmap(struct dma_ops_domain *dom, |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2123 | unsigned long address) |
| 2124 | { |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 2125 | struct aperture_range *aperture; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2126 | u64 *pte; |
| 2127 | |
| 2128 | if (address >= dom->aperture_size) |
| 2129 | return; |
| 2130 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 2131 | aperture = dom->aperture[APERTURE_RANGE_INDEX(address)]; |
| 2132 | if (!aperture) |
| 2133 | return; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2134 | |
Joerg Roedel | 384de72 | 2009-05-15 12:30:05 +0200 | [diff] [blame] | 2135 | pte = aperture->pte_pages[APERTURE_PAGE_INDEX(address)]; |
| 2136 | if (!pte) |
| 2137 | return; |
| 2138 | |
Joerg Roedel | 8c8c143 | 2009-09-02 17:30:00 +0200 | [diff] [blame] | 2139 | pte += PM_LEVEL_INDEX(0, address); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2140 | |
| 2141 | WARN_ON(!*pte); |
| 2142 | |
| 2143 | *pte = 0ULL; |
| 2144 | } |
| 2145 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2146 | /* |
| 2147 | * This function contains common code for mapping of a physically |
Joerg Roedel | 24f8116 | 2008-12-08 14:25:39 +0100 | [diff] [blame] | 2148 | * contiguous memory region into DMA address space. It is used by all |
| 2149 | * mapping functions provided with this IOMMU driver. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2150 | * Must be called with the domain lock held. |
| 2151 | */ |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2152 | static dma_addr_t __map_single(struct device *dev, |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2153 | struct dma_ops_domain *dma_dom, |
| 2154 | phys_addr_t paddr, |
| 2155 | size_t size, |
Joerg Roedel | 6d4f343 | 2008-09-04 19:18:02 +0200 | [diff] [blame] | 2156 | int dir, |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2157 | bool align, |
| 2158 | u64 dma_mask) |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2159 | { |
| 2160 | dma_addr_t offset = paddr & ~PAGE_MASK; |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2161 | dma_addr_t address, start, ret; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2162 | unsigned int pages; |
Joerg Roedel | 6d4f343 | 2008-09-04 19:18:02 +0200 | [diff] [blame] | 2163 | unsigned long align_mask = 0; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2164 | int i; |
| 2165 | |
Joerg Roedel | e3c449f | 2008-10-15 22:02:11 -0700 | [diff] [blame] | 2166 | pages = iommu_num_pages(paddr, size, PAGE_SIZE); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2167 | paddr &= PAGE_MASK; |
| 2168 | |
Joerg Roedel | 8ecaf8f | 2008-12-12 16:13:04 +0100 | [diff] [blame] | 2169 | INC_STATS_COUNTER(total_map_requests); |
| 2170 | |
Joerg Roedel | c185897 | 2008-12-12 15:42:39 +0100 | [diff] [blame] | 2171 | if (pages > 1) |
| 2172 | INC_STATS_COUNTER(cross_page); |
| 2173 | |
Joerg Roedel | 6d4f343 | 2008-09-04 19:18:02 +0200 | [diff] [blame] | 2174 | if (align) |
| 2175 | align_mask = (1UL << get_order(size)) - 1; |
| 2176 | |
Joerg Roedel | 11b8388 | 2009-05-19 10:23:15 +0200 | [diff] [blame] | 2177 | retry: |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2178 | address = dma_ops_alloc_addresses(dev, dma_dom, pages, align_mask, |
| 2179 | dma_mask); |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2180 | if (unlikely(address == DMA_ERROR_CODE)) { |
Joerg Roedel | 11b8388 | 2009-05-19 10:23:15 +0200 | [diff] [blame] | 2181 | /* |
| 2182 | * setting next_address here will let the address |
| 2183 | * allocator only scan the new allocated range in the |
| 2184 | * first run. This is a small optimization. |
| 2185 | */ |
| 2186 | dma_dom->next_address = dma_dom->aperture_size; |
| 2187 | |
Joerg Roedel | 576175c | 2009-11-23 19:08:46 +0100 | [diff] [blame] | 2188 | if (alloc_new_range(dma_dom, false, GFP_ATOMIC)) |
Joerg Roedel | 11b8388 | 2009-05-19 10:23:15 +0200 | [diff] [blame] | 2189 | goto out; |
| 2190 | |
| 2191 | /* |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2192 | * aperture was successfully enlarged by 128 MB, try |
Joerg Roedel | 11b8388 | 2009-05-19 10:23:15 +0200 | [diff] [blame] | 2193 | * allocation again |
| 2194 | */ |
| 2195 | goto retry; |
| 2196 | } |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2197 | |
| 2198 | start = address; |
| 2199 | for (i = 0; i < pages; ++i) { |
Joerg Roedel | 680525e | 2009-11-23 18:44:42 +0100 | [diff] [blame] | 2200 | ret = dma_ops_domain_map(dma_dom, start, paddr, dir); |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2201 | if (ret == DMA_ERROR_CODE) |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2202 | goto out_unmap; |
| 2203 | |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2204 | paddr += PAGE_SIZE; |
| 2205 | start += PAGE_SIZE; |
| 2206 | } |
| 2207 | address += offset; |
| 2208 | |
Joerg Roedel | 5774f7c | 2008-12-12 15:57:30 +0100 | [diff] [blame] | 2209 | ADD_STATS_COUNTER(alloced_io_mem, size); |
| 2210 | |
FUJITA Tomonori | afa9fdc | 2008-09-20 01:23:30 +0900 | [diff] [blame] | 2211 | if (unlikely(dma_dom->need_flush && !amd_iommu_unmap_flush)) { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2212 | domain_flush_tlb(&dma_dom->domain); |
Joerg Roedel | 1c65577 | 2008-09-04 18:40:05 +0200 | [diff] [blame] | 2213 | dma_dom->need_flush = false; |
Joerg Roedel | 318afd4 | 2009-11-23 18:32:38 +0100 | [diff] [blame] | 2214 | } else if (unlikely(amd_iommu_np_cache)) |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2215 | domain_flush_pages(&dma_dom->domain, address, size); |
Joerg Roedel | 270cab24 | 2008-09-04 15:49:46 +0200 | [diff] [blame] | 2216 | |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2217 | out: |
| 2218 | return address; |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2219 | |
| 2220 | out_unmap: |
| 2221 | |
| 2222 | for (--i; i >= 0; --i) { |
| 2223 | start -= PAGE_SIZE; |
Joerg Roedel | 680525e | 2009-11-23 18:44:42 +0100 | [diff] [blame] | 2224 | dma_ops_domain_unmap(dma_dom, start); |
Joerg Roedel | 53812c1 | 2009-05-12 12:17:38 +0200 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | dma_ops_free_addresses(dma_dom, address, pages); |
| 2228 | |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2229 | return DMA_ERROR_CODE; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2230 | } |
| 2231 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2232 | /* |
| 2233 | * Does the reverse of the __map_single function. Must be called with |
| 2234 | * the domain lock held too |
| 2235 | */ |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2236 | static void __unmap_single(struct dma_ops_domain *dma_dom, |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2237 | dma_addr_t dma_addr, |
| 2238 | size_t size, |
| 2239 | int dir) |
| 2240 | { |
Joerg Roedel | 04e0463 | 2010-09-23 16:12:48 +0200 | [diff] [blame] | 2241 | dma_addr_t flush_addr; |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2242 | dma_addr_t i, start; |
| 2243 | unsigned int pages; |
| 2244 | |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2245 | if ((dma_addr == DMA_ERROR_CODE) || |
Joerg Roedel | b8d9905 | 2008-12-08 14:40:26 +0100 | [diff] [blame] | 2246 | (dma_addr + size > dma_dom->aperture_size)) |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2247 | return; |
| 2248 | |
Joerg Roedel | 04e0463 | 2010-09-23 16:12:48 +0200 | [diff] [blame] | 2249 | flush_addr = dma_addr; |
Joerg Roedel | e3c449f | 2008-10-15 22:02:11 -0700 | [diff] [blame] | 2250 | pages = iommu_num_pages(dma_addr, size, PAGE_SIZE); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2251 | dma_addr &= PAGE_MASK; |
| 2252 | start = dma_addr; |
| 2253 | |
| 2254 | for (i = 0; i < pages; ++i) { |
Joerg Roedel | 680525e | 2009-11-23 18:44:42 +0100 | [diff] [blame] | 2255 | dma_ops_domain_unmap(dma_dom, start); |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2256 | start += PAGE_SIZE; |
| 2257 | } |
| 2258 | |
Joerg Roedel | 5774f7c | 2008-12-12 15:57:30 +0100 | [diff] [blame] | 2259 | SUB_STATS_COUNTER(alloced_io_mem, size); |
| 2260 | |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2261 | dma_ops_free_addresses(dma_dom, dma_addr, pages); |
Joerg Roedel | 270cab24 | 2008-09-04 15:49:46 +0200 | [diff] [blame] | 2262 | |
Joerg Roedel | 80be308 | 2008-11-06 14:59:05 +0100 | [diff] [blame] | 2263 | if (amd_iommu_unmap_flush || dma_dom->need_flush) { |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2264 | domain_flush_pages(&dma_dom->domain, flush_addr, size); |
Joerg Roedel | 80be308 | 2008-11-06 14:59:05 +0100 | [diff] [blame] | 2265 | dma_dom->need_flush = false; |
| 2266 | } |
Joerg Roedel | cb76c32 | 2008-06-26 21:28:00 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2269 | /* |
| 2270 | * The exported map_single function for dma_ops. |
| 2271 | */ |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 2272 | static dma_addr_t map_page(struct device *dev, struct page *page, |
| 2273 | unsigned long offset, size_t size, |
| 2274 | enum dma_data_direction dir, |
| 2275 | struct dma_attrs *attrs) |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2276 | { |
| 2277 | unsigned long flags; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2278 | struct protection_domain *domain; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2279 | dma_addr_t addr; |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2280 | u64 dma_mask; |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 2281 | phys_addr_t paddr = page_to_phys(page) + offset; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2282 | |
Joerg Roedel | 0f2a86f | 2008-12-12 15:05:16 +0100 | [diff] [blame] | 2283 | INC_STATS_COUNTER(cnt_map_single); |
| 2284 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2285 | domain = get_domain(dev); |
| 2286 | if (PTR_ERR(domain) == -EINVAL) |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2287 | return (dma_addr_t)paddr; |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2288 | else if (IS_ERR(domain)) |
| 2289 | return DMA_ERROR_CODE; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2290 | |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2291 | dma_mask = *dev->dma_mask; |
| 2292 | |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2293 | spin_lock_irqsave(&domain->lock, flags); |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2294 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2295 | addr = __map_single(dev, domain->priv, paddr, size, dir, false, |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2296 | dma_mask); |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2297 | if (addr == DMA_ERROR_CODE) |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2298 | goto out; |
| 2299 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2300 | domain_flush_complete(domain); |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2301 | |
| 2302 | out: |
| 2303 | spin_unlock_irqrestore(&domain->lock, flags); |
| 2304 | |
| 2305 | return addr; |
| 2306 | } |
| 2307 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2308 | /* |
| 2309 | * The exported unmap_single function for dma_ops. |
| 2310 | */ |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 2311 | static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size, |
| 2312 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2313 | { |
| 2314 | unsigned long flags; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2315 | struct protection_domain *domain; |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2316 | |
Joerg Roedel | 146a691 | 2008-12-12 15:07:12 +0100 | [diff] [blame] | 2317 | INC_STATS_COUNTER(cnt_unmap_single); |
| 2318 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2319 | domain = get_domain(dev); |
| 2320 | if (IS_ERR(domain)) |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2321 | return; |
| 2322 | |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2323 | spin_lock_irqsave(&domain->lock, flags); |
| 2324 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2325 | __unmap_single(domain->priv, dma_addr, size, dir); |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2326 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2327 | domain_flush_complete(domain); |
Joerg Roedel | 4da70b9 | 2008-06-26 21:28:01 +0200 | [diff] [blame] | 2328 | |
| 2329 | spin_unlock_irqrestore(&domain->lock, flags); |
| 2330 | } |
| 2331 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2332 | /* |
| 2333 | * This is a special map_sg function which is used if we should map a |
| 2334 | * device which is not handled by an AMD IOMMU in the system. |
| 2335 | */ |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2336 | static int map_sg_no_iommu(struct device *dev, struct scatterlist *sglist, |
| 2337 | int nelems, int dir) |
| 2338 | { |
| 2339 | struct scatterlist *s; |
| 2340 | int i; |
| 2341 | |
| 2342 | for_each_sg(sglist, s, nelems, i) { |
| 2343 | s->dma_address = (dma_addr_t)sg_phys(s); |
| 2344 | s->dma_length = s->length; |
| 2345 | } |
| 2346 | |
| 2347 | return nelems; |
| 2348 | } |
| 2349 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2350 | /* |
| 2351 | * The exported map_sg function for dma_ops (handles scatter-gather |
| 2352 | * lists). |
| 2353 | */ |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2354 | static int map_sg(struct device *dev, struct scatterlist *sglist, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 2355 | int nelems, enum dma_data_direction dir, |
| 2356 | struct dma_attrs *attrs) |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2357 | { |
| 2358 | unsigned long flags; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2359 | struct protection_domain *domain; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2360 | int i; |
| 2361 | struct scatterlist *s; |
| 2362 | phys_addr_t paddr; |
| 2363 | int mapped_elems = 0; |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2364 | u64 dma_mask; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2365 | |
Joerg Roedel | d03f067a | 2008-12-12 15:09:48 +0100 | [diff] [blame] | 2366 | INC_STATS_COUNTER(cnt_map_sg); |
| 2367 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2368 | domain = get_domain(dev); |
| 2369 | if (PTR_ERR(domain) == -EINVAL) |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2370 | return map_sg_no_iommu(dev, sglist, nelems, dir); |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2371 | else if (IS_ERR(domain)) |
| 2372 | return 0; |
Joerg Roedel | dbcc112 | 2008-09-04 15:04:26 +0200 | [diff] [blame] | 2373 | |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2374 | dma_mask = *dev->dma_mask; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2375 | |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2376 | spin_lock_irqsave(&domain->lock, flags); |
| 2377 | |
| 2378 | for_each_sg(sglist, s, nelems, i) { |
| 2379 | paddr = sg_phys(s); |
| 2380 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2381 | s->dma_address = __map_single(dev, domain->priv, |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2382 | paddr, s->length, dir, false, |
| 2383 | dma_mask); |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2384 | |
| 2385 | if (s->dma_address) { |
| 2386 | s->dma_length = s->length; |
| 2387 | mapped_elems++; |
| 2388 | } else |
| 2389 | goto unmap; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2390 | } |
| 2391 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2392 | domain_flush_complete(domain); |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2393 | |
| 2394 | out: |
| 2395 | spin_unlock_irqrestore(&domain->lock, flags); |
| 2396 | |
| 2397 | return mapped_elems; |
| 2398 | unmap: |
| 2399 | for_each_sg(sglist, s, mapped_elems, i) { |
| 2400 | if (s->dma_address) |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2401 | __unmap_single(domain->priv, s->dma_address, |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2402 | s->dma_length, dir); |
| 2403 | s->dma_address = s->dma_length = 0; |
| 2404 | } |
| 2405 | |
| 2406 | mapped_elems = 0; |
| 2407 | |
| 2408 | goto out; |
| 2409 | } |
| 2410 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2411 | /* |
| 2412 | * The exported map_sg function for dma_ops (handles scatter-gather |
| 2413 | * lists). |
| 2414 | */ |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2415 | static void unmap_sg(struct device *dev, struct scatterlist *sglist, |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 2416 | int nelems, enum dma_data_direction dir, |
| 2417 | struct dma_attrs *attrs) |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2418 | { |
| 2419 | unsigned long flags; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2420 | struct protection_domain *domain; |
| 2421 | struct scatterlist *s; |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2422 | int i; |
| 2423 | |
Joerg Roedel | 55877a6 | 2008-12-12 15:12:14 +0100 | [diff] [blame] | 2424 | INC_STATS_COUNTER(cnt_unmap_sg); |
| 2425 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2426 | domain = get_domain(dev); |
| 2427 | if (IS_ERR(domain)) |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2428 | return; |
| 2429 | |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2430 | spin_lock_irqsave(&domain->lock, flags); |
| 2431 | |
| 2432 | for_each_sg(sglist, s, nelems, i) { |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2433 | __unmap_single(domain->priv, s->dma_address, |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2434 | s->dma_length, dir); |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2435 | s->dma_address = s->dma_length = 0; |
| 2436 | } |
| 2437 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2438 | domain_flush_complete(domain); |
Joerg Roedel | 65b050a | 2008-06-26 21:28:02 +0200 | [diff] [blame] | 2439 | |
| 2440 | spin_unlock_irqrestore(&domain->lock, flags); |
| 2441 | } |
| 2442 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2443 | /* |
| 2444 | * The exported alloc_coherent function for dma_ops. |
| 2445 | */ |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2446 | static void *alloc_coherent(struct device *dev, size_t size, |
| 2447 | dma_addr_t *dma_addr, gfp_t flag) |
| 2448 | { |
| 2449 | unsigned long flags; |
| 2450 | void *virt_addr; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2451 | struct protection_domain *domain; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2452 | phys_addr_t paddr; |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2453 | u64 dma_mask = dev->coherent_dma_mask; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2454 | |
Joerg Roedel | c8f0fb3 | 2008-12-12 15:14:21 +0100 | [diff] [blame] | 2455 | INC_STATS_COUNTER(cnt_alloc_coherent); |
| 2456 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2457 | domain = get_domain(dev); |
| 2458 | if (PTR_ERR(domain) == -EINVAL) { |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2459 | virt_addr = (void *)__get_free_pages(flag, get_order(size)); |
| 2460 | *dma_addr = __pa(virt_addr); |
| 2461 | return virt_addr; |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2462 | } else if (IS_ERR(domain)) |
| 2463 | return NULL; |
Joerg Roedel | dbcc112 | 2008-09-04 15:04:26 +0200 | [diff] [blame] | 2464 | |
Joerg Roedel | f99c0f1 | 2009-11-23 16:52:56 +0100 | [diff] [blame] | 2465 | dma_mask = dev->coherent_dma_mask; |
| 2466 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); |
| 2467 | flag |= __GFP_ZERO; |
FUJITA Tomonori | 13d9fea | 2008-09-10 20:19:40 +0900 | [diff] [blame] | 2468 | |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2469 | virt_addr = (void *)__get_free_pages(flag, get_order(size)); |
| 2470 | if (!virt_addr) |
Jaswinder Singh Rajput | b25ae67 | 2009-07-01 19:53:14 +0530 | [diff] [blame] | 2471 | return NULL; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2472 | |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2473 | paddr = virt_to_phys(virt_addr); |
| 2474 | |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2475 | if (!dma_mask) |
| 2476 | dma_mask = *dev->dma_mask; |
| 2477 | |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2478 | spin_lock_irqsave(&domain->lock, flags); |
| 2479 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2480 | *dma_addr = __map_single(dev, domain->priv, paddr, |
Joerg Roedel | 832a90c | 2008-09-18 15:54:23 +0200 | [diff] [blame] | 2481 | size, DMA_BIDIRECTIONAL, true, dma_mask); |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2482 | |
FUJITA Tomonori | 8fd524b | 2009-11-15 21:19:53 +0900 | [diff] [blame] | 2483 | if (*dma_addr == DMA_ERROR_CODE) { |
Jiri Slaby | 367d04c | 2009-05-28 09:54:48 +0200 | [diff] [blame] | 2484 | spin_unlock_irqrestore(&domain->lock, flags); |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2485 | goto out_free; |
Jiri Slaby | 367d04c | 2009-05-28 09:54:48 +0200 | [diff] [blame] | 2486 | } |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2487 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2488 | domain_flush_complete(domain); |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2489 | |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2490 | spin_unlock_irqrestore(&domain->lock, flags); |
| 2491 | |
| 2492 | return virt_addr; |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2493 | |
| 2494 | out_free: |
| 2495 | |
| 2496 | free_pages((unsigned long)virt_addr, get_order(size)); |
| 2497 | |
| 2498 | return NULL; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2499 | } |
| 2500 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2501 | /* |
| 2502 | * The exported free_coherent function for dma_ops. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2503 | */ |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2504 | static void free_coherent(struct device *dev, size_t size, |
| 2505 | void *virt_addr, dma_addr_t dma_addr) |
| 2506 | { |
| 2507 | unsigned long flags; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2508 | struct protection_domain *domain; |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2509 | |
Joerg Roedel | 5d31ee7 | 2008-12-12 15:16:38 +0100 | [diff] [blame] | 2510 | INC_STATS_COUNTER(cnt_free_coherent); |
| 2511 | |
Joerg Roedel | 94f6d19 | 2009-11-24 16:40:02 +0100 | [diff] [blame] | 2512 | domain = get_domain(dev); |
| 2513 | if (IS_ERR(domain)) |
Joerg Roedel | 5b28df6 | 2008-12-02 17:49:42 +0100 | [diff] [blame] | 2514 | goto free_mem; |
| 2515 | |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2516 | spin_lock_irqsave(&domain->lock, flags); |
| 2517 | |
Joerg Roedel | cd8c82e | 2009-11-23 19:33:56 +0100 | [diff] [blame] | 2518 | __unmap_single(domain->priv, dma_addr, size, DMA_BIDIRECTIONAL); |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2519 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2520 | domain_flush_complete(domain); |
Joerg Roedel | 5d8b53c | 2008-06-26 21:28:03 +0200 | [diff] [blame] | 2521 | |
| 2522 | spin_unlock_irqrestore(&domain->lock, flags); |
| 2523 | |
| 2524 | free_mem: |
| 2525 | free_pages((unsigned long)virt_addr, get_order(size)); |
| 2526 | } |
| 2527 | |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2528 | /* |
Joerg Roedel | b39ba6a | 2008-09-09 18:40:46 +0200 | [diff] [blame] | 2529 | * This function is called by the DMA layer to find out if we can handle a |
| 2530 | * particular device. It is part of the dma_ops. |
| 2531 | */ |
| 2532 | static int amd_iommu_dma_supported(struct device *dev, u64 mask) |
| 2533 | { |
Joerg Roedel | 420aef8 | 2009-11-23 16:14:57 +0100 | [diff] [blame] | 2534 | return check_device(dev); |
Joerg Roedel | b39ba6a | 2008-09-09 18:40:46 +0200 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | /* |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2538 | * The function for pre-allocating protection domains. |
| 2539 | * |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2540 | * If the driver core informs the DMA layer if a driver grabs a device |
| 2541 | * we don't need to preallocate the protection domains anymore. |
| 2542 | * For now we have to. |
| 2543 | */ |
Jaswinder Singh Rajput | 0e93dd8 | 2008-12-29 21:45:22 +0530 | [diff] [blame] | 2544 | static void prealloc_protection_domains(void) |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2545 | { |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2546 | struct iommu_dev_data *dev_data; |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2547 | struct dma_ops_domain *dma_dom; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2548 | struct pci_dev *dev = NULL; |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2549 | u16 devid; |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2550 | |
Chris Wright | d18c69d | 2010-04-02 18:27:55 -0700 | [diff] [blame] | 2551 | for_each_pci_dev(dev) { |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2552 | |
| 2553 | /* Do we handle this device? */ |
| 2554 | if (!check_device(&dev->dev)) |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2555 | continue; |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2556 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2557 | dev_data = get_dev_data(&dev->dev); |
| 2558 | if (!amd_iommu_force_isolation && dev_data->iommu_v2) { |
| 2559 | /* Make sure passthrough domain is allocated */ |
| 2560 | alloc_passthrough_domain(); |
| 2561 | dev_data->passthrough = true; |
| 2562 | attach_device(&dev->dev, pt_domain); |
| 2563 | pr_info("AMD-Vi: Using passthough domain for device %s\n", |
| 2564 | dev_name(&dev->dev)); |
| 2565 | } |
| 2566 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2567 | /* Is there already any domain for it? */ |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2568 | if (domain_for_device(&dev->dev)) |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2569 | continue; |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2570 | |
| 2571 | devid = get_device_id(&dev->dev); |
| 2572 | |
Joerg Roedel | 87a64d5 | 2009-11-24 17:26:43 +0100 | [diff] [blame] | 2573 | dma_dom = dma_ops_domain_alloc(); |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2574 | if (!dma_dom) |
| 2575 | continue; |
| 2576 | init_unity_mappings_for_device(dma_dom, devid); |
Joerg Roedel | bd60b73 | 2008-09-11 10:24:48 +0200 | [diff] [blame] | 2577 | dma_dom->target_dev = devid; |
| 2578 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2579 | attach_device(&dev->dev, &dma_dom->domain); |
Joerg Roedel | be83129 | 2009-11-23 12:50:00 +0100 | [diff] [blame] | 2580 | |
Joerg Roedel | bd60b73 | 2008-09-11 10:24:48 +0200 | [diff] [blame] | 2581 | list_add_tail(&dma_dom->list, &iommu_pd_list); |
Joerg Roedel | c432f3d | 2008-06-26 21:28:04 +0200 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 2585 | static struct dma_map_ops amd_iommu_dma_ops = { |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2586 | .alloc_coherent = alloc_coherent, |
| 2587 | .free_coherent = free_coherent, |
FUJITA Tomonori | 5149136 | 2009-01-05 23:47:25 +0900 | [diff] [blame] | 2588 | .map_page = map_page, |
| 2589 | .unmap_page = unmap_page, |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2590 | .map_sg = map_sg, |
| 2591 | .unmap_sg = unmap_sg, |
Joerg Roedel | b39ba6a | 2008-09-09 18:40:46 +0200 | [diff] [blame] | 2592 | .dma_supported = amd_iommu_dma_supported, |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2593 | }; |
| 2594 | |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 2595 | static unsigned device_dma_ops_init(void) |
| 2596 | { |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2597 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 2598 | struct pci_dev *pdev = NULL; |
| 2599 | unsigned unhandled = 0; |
| 2600 | |
| 2601 | for_each_pci_dev(pdev) { |
| 2602 | if (!check_device(&pdev->dev)) { |
| 2603 | unhandled += 1; |
| 2604 | continue; |
| 2605 | } |
| 2606 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2607 | dev_data = get_dev_data(&pdev->dev); |
| 2608 | |
| 2609 | if (!dev_data->passthrough) |
| 2610 | pdev->dev.archdata.dma_ops = &amd_iommu_dma_ops; |
| 2611 | else |
| 2612 | pdev->dev.archdata.dma_ops = &nommu_dma_ops; |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | return unhandled; |
| 2616 | } |
| 2617 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2618 | /* |
| 2619 | * The function which clues the AMD IOMMU driver into dma_ops. |
| 2620 | */ |
Joerg Roedel | f532509 | 2010-01-22 17:44:35 +0100 | [diff] [blame] | 2621 | |
| 2622 | void __init amd_iommu_init_api(void) |
| 2623 | { |
Joerg Roedel | 2cc21c4 | 2011-09-06 17:56:07 +0200 | [diff] [blame] | 2624 | bus_set_iommu(&pci_bus_type, &amd_iommu_ops); |
Joerg Roedel | f532509 | 2010-01-22 17:44:35 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2627 | int __init amd_iommu_init_dma_ops(void) |
| 2628 | { |
| 2629 | struct amd_iommu *iommu; |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 2630 | int ret, unhandled; |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2631 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2632 | /* |
| 2633 | * first allocate a default protection domain for every IOMMU we |
| 2634 | * found in the system. Devices not assigned to any other |
| 2635 | * protection domain will be assigned to the default one. |
| 2636 | */ |
Joerg Roedel | 3bd2217 | 2009-05-04 15:06:20 +0200 | [diff] [blame] | 2637 | for_each_iommu(iommu) { |
Joerg Roedel | 87a64d5 | 2009-11-24 17:26:43 +0100 | [diff] [blame] | 2638 | iommu->default_dom = dma_ops_domain_alloc(); |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2639 | if (iommu->default_dom == NULL) |
| 2640 | return -ENOMEM; |
Joerg Roedel | e2dc14a | 2008-12-10 18:48:59 +0100 | [diff] [blame] | 2641 | iommu->default_dom->domain.flags |= PD_DEFAULT_MASK; |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2642 | ret = iommu_init_unity_mappings(iommu); |
| 2643 | if (ret) |
| 2644 | goto free_domains; |
| 2645 | } |
| 2646 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2647 | /* |
Joerg Roedel | 8793abe | 2009-11-27 11:40:33 +0100 | [diff] [blame] | 2648 | * Pre-allocate the protection domains for each device. |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2649 | */ |
Joerg Roedel | 8793abe | 2009-11-27 11:40:33 +0100 | [diff] [blame] | 2650 | prealloc_protection_domains(); |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2651 | |
| 2652 | iommu_detected = 1; |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 2653 | swiotlb = 0; |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2654 | |
Joerg Roedel | 431b2a2 | 2008-07-11 17:14:22 +0200 | [diff] [blame] | 2655 | /* Make the driver finally visible to the drivers */ |
Joerg Roedel | 27c2127 | 2011-05-30 15:56:24 +0200 | [diff] [blame] | 2656 | unhandled = device_dma_ops_init(); |
| 2657 | if (unhandled && max_pfn > MAX_DMA32_PFN) { |
| 2658 | /* There are unhandled devices - initialize swiotlb for them */ |
| 2659 | swiotlb = 1; |
| 2660 | } |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2661 | |
Joerg Roedel | 7f26508 | 2008-12-12 13:50:21 +0100 | [diff] [blame] | 2662 | amd_iommu_stats_init(); |
| 2663 | |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2664 | return 0; |
| 2665 | |
| 2666 | free_domains: |
| 2667 | |
Joerg Roedel | 3bd2217 | 2009-05-04 15:06:20 +0200 | [diff] [blame] | 2668 | for_each_iommu(iommu) { |
Joerg Roedel | 6631ee9 | 2008-06-26 21:28:05 +0200 | [diff] [blame] | 2669 | if (iommu->default_dom) |
| 2670 | dma_ops_domain_free(iommu->default_dom); |
| 2671 | } |
| 2672 | |
| 2673 | return ret; |
| 2674 | } |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2675 | |
| 2676 | /***************************************************************************** |
| 2677 | * |
| 2678 | * The following functions belong to the exported interface of AMD IOMMU |
| 2679 | * |
| 2680 | * This interface allows access to lower level functions of the IOMMU |
| 2681 | * like protection domain handling and assignement of devices to domains |
| 2682 | * which is not possible with the dma_ops interface. |
| 2683 | * |
| 2684 | *****************************************************************************/ |
| 2685 | |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2686 | static void cleanup_domain(struct protection_domain *domain) |
| 2687 | { |
Joerg Roedel | 492667d | 2009-11-27 13:25:47 +0100 | [diff] [blame] | 2688 | struct iommu_dev_data *dev_data, *next; |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2689 | unsigned long flags; |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2690 | |
| 2691 | write_lock_irqsave(&amd_iommu_devtable_lock, flags); |
| 2692 | |
Joerg Roedel | 492667d | 2009-11-27 13:25:47 +0100 | [diff] [blame] | 2693 | list_for_each_entry_safe(dev_data, next, &domain->dev_list, list) { |
Joerg Roedel | ec9e79e | 2011-06-09 17:25:50 +0200 | [diff] [blame] | 2694 | __detach_device(dev_data); |
Joerg Roedel | 492667d | 2009-11-27 13:25:47 +0100 | [diff] [blame] | 2695 | atomic_set(&dev_data->bind, 0); |
| 2696 | } |
Joerg Roedel | 6d98cd8 | 2008-12-08 12:05:55 +0100 | [diff] [blame] | 2697 | |
| 2698 | write_unlock_irqrestore(&amd_iommu_devtable_lock, flags); |
| 2699 | } |
| 2700 | |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2701 | static void protection_domain_free(struct protection_domain *domain) |
| 2702 | { |
| 2703 | if (!domain) |
| 2704 | return; |
| 2705 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 2706 | del_domain_from_list(domain); |
| 2707 | |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2708 | if (domain->id) |
| 2709 | domain_id_free(domain->id); |
| 2710 | |
| 2711 | kfree(domain); |
| 2712 | } |
| 2713 | |
| 2714 | static struct protection_domain *protection_domain_alloc(void) |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2715 | { |
| 2716 | struct protection_domain *domain; |
| 2717 | |
| 2718 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 2719 | if (!domain) |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2720 | return NULL; |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2721 | |
| 2722 | spin_lock_init(&domain->lock); |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2723 | mutex_init(&domain->api_lock); |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2724 | domain->id = domain_id_alloc(); |
| 2725 | if (!domain->id) |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2726 | goto out_err; |
Joerg Roedel | 7c392cb | 2009-11-26 11:13:32 +0100 | [diff] [blame] | 2727 | INIT_LIST_HEAD(&domain->dev_list); |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2728 | |
Joerg Roedel | aeb26f5 | 2009-11-20 16:44:01 +0100 | [diff] [blame] | 2729 | add_domain_to_list(domain); |
| 2730 | |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2731 | return domain; |
| 2732 | |
| 2733 | out_err: |
| 2734 | kfree(domain); |
| 2735 | |
| 2736 | return NULL; |
| 2737 | } |
| 2738 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2739 | static int __init alloc_passthrough_domain(void) |
| 2740 | { |
| 2741 | if (pt_domain != NULL) |
| 2742 | return 0; |
| 2743 | |
| 2744 | /* allocate passthrough domain */ |
| 2745 | pt_domain = protection_domain_alloc(); |
| 2746 | if (!pt_domain) |
| 2747 | return -ENOMEM; |
| 2748 | |
| 2749 | pt_domain->mode = PAGE_MODE_NONE; |
| 2750 | |
| 2751 | return 0; |
| 2752 | } |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2753 | static int amd_iommu_domain_init(struct iommu_domain *dom) |
| 2754 | { |
| 2755 | struct protection_domain *domain; |
| 2756 | |
| 2757 | domain = protection_domain_alloc(); |
| 2758 | if (!domain) |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2759 | goto out_free; |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2760 | |
| 2761 | domain->mode = PAGE_MODE_3_LEVEL; |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2762 | domain->pt_root = (void *)get_zeroed_page(GFP_KERNEL); |
| 2763 | if (!domain->pt_root) |
| 2764 | goto out_free; |
| 2765 | |
| 2766 | dom->priv = domain; |
| 2767 | |
| 2768 | return 0; |
| 2769 | |
| 2770 | out_free: |
Joerg Roedel | 2650815 | 2009-08-26 16:52:40 +0200 | [diff] [blame] | 2771 | protection_domain_free(domain); |
Joerg Roedel | c156e34 | 2008-12-02 18:13:27 +0100 | [diff] [blame] | 2772 | |
| 2773 | return -ENOMEM; |
| 2774 | } |
| 2775 | |
Joerg Roedel | 98383fc | 2008-12-02 18:34:12 +0100 | [diff] [blame] | 2776 | static void amd_iommu_domain_destroy(struct iommu_domain *dom) |
| 2777 | { |
| 2778 | struct protection_domain *domain = dom->priv; |
| 2779 | |
| 2780 | if (!domain) |
| 2781 | return; |
| 2782 | |
| 2783 | if (domain->dev_cnt > 0) |
| 2784 | cleanup_domain(domain); |
| 2785 | |
| 2786 | BUG_ON(domain->dev_cnt != 0); |
| 2787 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame^] | 2788 | if (domain->mode != PAGE_MODE_NONE) |
| 2789 | free_pagetable(domain); |
Joerg Roedel | 98383fc | 2008-12-02 18:34:12 +0100 | [diff] [blame] | 2790 | |
Joerg Roedel | 8b408fe | 2010-03-08 14:20:07 +0100 | [diff] [blame] | 2791 | protection_domain_free(domain); |
Joerg Roedel | 98383fc | 2008-12-02 18:34:12 +0100 | [diff] [blame] | 2792 | |
| 2793 | dom->priv = NULL; |
| 2794 | } |
| 2795 | |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2796 | static void amd_iommu_detach_device(struct iommu_domain *dom, |
| 2797 | struct device *dev) |
| 2798 | { |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2799 | struct iommu_dev_data *dev_data = dev->archdata.iommu; |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2800 | struct amd_iommu *iommu; |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2801 | u16 devid; |
| 2802 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2803 | if (!check_device(dev)) |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2804 | return; |
| 2805 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2806 | devid = get_device_id(dev); |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2807 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2808 | if (dev_data->domain != NULL) |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2809 | detach_device(dev); |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2810 | |
| 2811 | iommu = amd_iommu_rlookup_table[devid]; |
| 2812 | if (!iommu) |
| 2813 | return; |
| 2814 | |
Joerg Roedel | 684f288 | 2008-12-08 12:07:44 +0100 | [diff] [blame] | 2815 | iommu_completion_wait(iommu); |
| 2816 | } |
| 2817 | |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2818 | static int amd_iommu_attach_device(struct iommu_domain *dom, |
| 2819 | struct device *dev) |
| 2820 | { |
| 2821 | struct protection_domain *domain = dom->priv; |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2822 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2823 | struct amd_iommu *iommu; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2824 | int ret; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2825 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2826 | if (!check_device(dev)) |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2827 | return -EINVAL; |
| 2828 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2829 | dev_data = dev->archdata.iommu; |
| 2830 | |
Joerg Roedel | f62dda6 | 2011-06-09 12:55:35 +0200 | [diff] [blame] | 2831 | iommu = amd_iommu_rlookup_table[dev_data->devid]; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2832 | if (!iommu) |
| 2833 | return -EINVAL; |
| 2834 | |
Joerg Roedel | 657cbb6 | 2009-11-23 15:26:46 +0100 | [diff] [blame] | 2835 | if (dev_data->domain) |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2836 | detach_device(dev); |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2837 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2838 | ret = attach_device(dev, domain); |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2839 | |
| 2840 | iommu_completion_wait(iommu); |
| 2841 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2842 | return ret; |
Joerg Roedel | 0110606 | 2008-12-02 19:34:11 +0100 | [diff] [blame] | 2843 | } |
| 2844 | |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2845 | static int amd_iommu_map(struct iommu_domain *dom, unsigned long iova, |
| 2846 | phys_addr_t paddr, int gfp_order, int iommu_prot) |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2847 | { |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2848 | unsigned long page_size = 0x1000UL << gfp_order; |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2849 | struct protection_domain *domain = dom->priv; |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2850 | int prot = 0; |
| 2851 | int ret; |
| 2852 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame^] | 2853 | if (domain->mode == PAGE_MODE_NONE) |
| 2854 | return -EINVAL; |
| 2855 | |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2856 | if (iommu_prot & IOMMU_READ) |
| 2857 | prot |= IOMMU_PROT_IR; |
| 2858 | if (iommu_prot & IOMMU_WRITE) |
| 2859 | prot |= IOMMU_PROT_IW; |
| 2860 | |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2861 | mutex_lock(&domain->api_lock); |
Joerg Roedel | 795e74f | 2010-05-11 17:40:57 +0200 | [diff] [blame] | 2862 | ret = iommu_map_page(domain, iova, paddr, prot, page_size); |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2863 | mutex_unlock(&domain->api_lock); |
| 2864 | |
Joerg Roedel | 795e74f | 2010-05-11 17:40:57 +0200 | [diff] [blame] | 2865 | return ret; |
Joerg Roedel | c6229ca | 2008-12-02 19:48:43 +0100 | [diff] [blame] | 2866 | } |
| 2867 | |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2868 | static int amd_iommu_unmap(struct iommu_domain *dom, unsigned long iova, |
| 2869 | int gfp_order) |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2870 | { |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2871 | struct protection_domain *domain = dom->priv; |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2872 | unsigned long page_size, unmap_size; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2873 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame^] | 2874 | if (domain->mode == PAGE_MODE_NONE) |
| 2875 | return -EINVAL; |
| 2876 | |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2877 | page_size = 0x1000UL << gfp_order; |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2878 | |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2879 | mutex_lock(&domain->api_lock); |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2880 | unmap_size = iommu_unmap_page(domain, iova, page_size); |
Joerg Roedel | 795e74f | 2010-05-11 17:40:57 +0200 | [diff] [blame] | 2881 | mutex_unlock(&domain->api_lock); |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2882 | |
Joerg Roedel | 17b124b | 2011-04-06 18:01:35 +0200 | [diff] [blame] | 2883 | domain_flush_tlb_pde(domain); |
Joerg Roedel | 5d214fe | 2010-02-08 14:44:49 +0100 | [diff] [blame] | 2884 | |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2885 | return get_order(unmap_size); |
Joerg Roedel | eb74ff6 | 2008-12-02 19:59:10 +0100 | [diff] [blame] | 2886 | } |
| 2887 | |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2888 | static phys_addr_t amd_iommu_iova_to_phys(struct iommu_domain *dom, |
| 2889 | unsigned long iova) |
| 2890 | { |
| 2891 | struct protection_domain *domain = dom->priv; |
Joerg Roedel | f03152b | 2010-01-21 16:15:24 +0100 | [diff] [blame] | 2892 | unsigned long offset_mask; |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2893 | phys_addr_t paddr; |
Joerg Roedel | f03152b | 2010-01-21 16:15:24 +0100 | [diff] [blame] | 2894 | u64 *pte, __pte; |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2895 | |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame^] | 2896 | if (domain->mode == PAGE_MODE_NONE) |
| 2897 | return iova; |
| 2898 | |
Joerg Roedel | 24cd772 | 2010-01-19 17:27:39 +0100 | [diff] [blame] | 2899 | pte = fetch_pte(domain, iova); |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2900 | |
Joerg Roedel | a6d41a4 | 2009-09-02 17:08:55 +0200 | [diff] [blame] | 2901 | if (!pte || !IOMMU_PTE_PRESENT(*pte)) |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2902 | return 0; |
| 2903 | |
Joerg Roedel | f03152b | 2010-01-21 16:15:24 +0100 | [diff] [blame] | 2904 | if (PM_PTE_LEVEL(*pte) == 0) |
| 2905 | offset_mask = PAGE_SIZE - 1; |
| 2906 | else |
| 2907 | offset_mask = PTE_PAGE_SIZE(*pte) - 1; |
| 2908 | |
| 2909 | __pte = *pte & PM_ADDR_MASK; |
| 2910 | paddr = (__pte & ~offset_mask) | (iova & offset_mask); |
Joerg Roedel | 645c4c8 | 2008-12-02 20:05:50 +0100 | [diff] [blame] | 2911 | |
| 2912 | return paddr; |
| 2913 | } |
| 2914 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 2915 | static int amd_iommu_domain_has_cap(struct iommu_domain *domain, |
| 2916 | unsigned long cap) |
| 2917 | { |
Joerg Roedel | 80a506b | 2010-07-27 17:14:24 +0200 | [diff] [blame] | 2918 | switch (cap) { |
| 2919 | case IOMMU_CAP_CACHE_COHERENCY: |
| 2920 | return 1; |
| 2921 | } |
| 2922 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 2923 | return 0; |
| 2924 | } |
| 2925 | |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 2926 | static struct iommu_ops amd_iommu_ops = { |
| 2927 | .domain_init = amd_iommu_domain_init, |
| 2928 | .domain_destroy = amd_iommu_domain_destroy, |
| 2929 | .attach_dev = amd_iommu_attach_device, |
| 2930 | .detach_dev = amd_iommu_detach_device, |
Joerg Roedel | 468e236 | 2010-01-21 16:37:36 +0100 | [diff] [blame] | 2931 | .map = amd_iommu_map, |
| 2932 | .unmap = amd_iommu_unmap, |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 2933 | .iova_to_phys = amd_iommu_iova_to_phys, |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 2934 | .domain_has_cap = amd_iommu_domain_has_cap, |
Joerg Roedel | 26961ef | 2008-12-03 17:00:17 +0100 | [diff] [blame] | 2935 | }; |
| 2936 | |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2937 | /***************************************************************************** |
| 2938 | * |
| 2939 | * The next functions do a basic initialization of IOMMU for pass through |
| 2940 | * mode |
| 2941 | * |
| 2942 | * In passthrough mode the IOMMU is initialized and enabled but not used for |
| 2943 | * DMA-API translation. |
| 2944 | * |
| 2945 | *****************************************************************************/ |
| 2946 | |
| 2947 | int __init amd_iommu_init_passthrough(void) |
| 2948 | { |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2949 | struct iommu_dev_data *dev_data; |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2950 | struct pci_dev *dev = NULL; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2951 | struct amd_iommu *iommu; |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2952 | u16 devid; |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2953 | int ret; |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2954 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2955 | ret = alloc_passthrough_domain(); |
| 2956 | if (ret) |
| 2957 | return ret; |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2958 | |
Kulikov Vasiliy | 6c54aab | 2010-07-03 12:03:51 -0400 | [diff] [blame] | 2959 | for_each_pci_dev(dev) { |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2960 | if (!check_device(&dev->dev)) |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2961 | continue; |
| 2962 | |
Joerg Roedel | 5abcdba | 2011-12-01 15:49:45 +0100 | [diff] [blame] | 2963 | dev_data = get_dev_data(&dev->dev); |
| 2964 | dev_data->passthrough = true; |
| 2965 | |
Joerg Roedel | 98fc5a6 | 2009-11-24 17:19:23 +0100 | [diff] [blame] | 2966 | devid = get_device_id(&dev->dev); |
| 2967 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2968 | iommu = amd_iommu_rlookup_table[devid]; |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2969 | if (!iommu) |
| 2970 | continue; |
| 2971 | |
Joerg Roedel | 15898bb | 2009-11-24 15:39:42 +0100 | [diff] [blame] | 2972 | attach_device(&dev->dev, pt_domain); |
Joerg Roedel | 0feae53 | 2009-08-26 15:26:30 +0200 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | pr_info("AMD-Vi: Initialized for Passthrough Mode\n"); |
| 2976 | |
| 2977 | return 0; |
| 2978 | } |
Joerg Roedel | 72e1dcc | 2011-11-10 19:13:51 +0100 | [diff] [blame] | 2979 | |
| 2980 | /* IOMMUv2 specific functions */ |
| 2981 | int amd_iommu_register_ppr_notifier(struct notifier_block *nb) |
| 2982 | { |
| 2983 | return atomic_notifier_chain_register(&ppr_notifier, nb); |
| 2984 | } |
| 2985 | EXPORT_SYMBOL(amd_iommu_register_ppr_notifier); |
| 2986 | |
| 2987 | int amd_iommu_unregister_ppr_notifier(struct notifier_block *nb) |
| 2988 | { |
| 2989 | return atomic_notifier_chain_unregister(&ppr_notifier, nb); |
| 2990 | } |
| 2991 | EXPORT_SYMBOL(amd_iommu_unregister_ppr_notifier); |
Joerg Roedel | 132bd68 | 2011-11-17 14:18:46 +0100 | [diff] [blame^] | 2992 | |
| 2993 | void amd_iommu_domain_direct_map(struct iommu_domain *dom) |
| 2994 | { |
| 2995 | struct protection_domain *domain = dom->priv; |
| 2996 | unsigned long flags; |
| 2997 | |
| 2998 | spin_lock_irqsave(&domain->lock, flags); |
| 2999 | |
| 3000 | /* Update data structure */ |
| 3001 | domain->mode = PAGE_MODE_NONE; |
| 3002 | domain->updated = true; |
| 3003 | |
| 3004 | /* Make changes visible to IOMMUs */ |
| 3005 | update_domain(domain); |
| 3006 | |
| 3007 | /* Page-table is not visible to IOMMU anymore, so free it */ |
| 3008 | free_pagetable(domain); |
| 3009 | |
| 3010 | spin_unlock_irqrestore(&domain->lock, flags); |
| 3011 | } |
| 3012 | EXPORT_SYMBOL(amd_iommu_domain_direct_map); |