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