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