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