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