Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006, Intel Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
mark gross | 98bcef5 | 2008-02-23 15:23:35 -0800 | [diff] [blame] | 17 | * Copyright (C) 2006-2008 Intel Corporation |
| 18 | * Author: Ashok Raj <ashok.raj@intel.com> |
| 19 | * Author: Shaohua Li <shaohua.li@intel.com> |
| 20 | * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 21 | * Author: Fenghua Yu <fenghua.yu@intel.com> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/bitmap.h> |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 26 | #include <linux/debugfs.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/irq.h> |
| 29 | #include <linux/interrupt.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/pci.h> |
| 32 | #include <linux/dmar.h> |
| 33 | #include <linux/dma-mapping.h> |
| 34 | #include <linux/mempool.h> |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 35 | #include <linux/timer.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 36 | #include <linux/iova.h> |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 37 | #include <linux/iommu.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 38 | #include <linux/intel-iommu.h> |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 39 | #include <linux/sysdev.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 40 | #include <asm/cacheflush.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 41 | #include <asm/iommu.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 42 | #include "pci.h" |
| 43 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 44 | #define ROOT_SIZE VTD_PAGE_SIZE |
| 45 | #define CONTEXT_SIZE VTD_PAGE_SIZE |
| 46 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 47 | #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) |
| 48 | #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA) |
| 49 | |
| 50 | #define IOAPIC_RANGE_START (0xfee00000) |
| 51 | #define IOAPIC_RANGE_END (0xfeefffff) |
| 52 | #define IOVA_START_ADDR (0x1000) |
| 53 | |
| 54 | #define DEFAULT_DOMAIN_ADDRESS_WIDTH 48 |
| 55 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 56 | #define MAX_AGAW_WIDTH 64 |
| 57 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 58 | #define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1) |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 59 | #define DOMAIN_MAX_PFN(gaw) ((((u64)1) << (gaw-VTD_PAGE_SHIFT)) - 1) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 60 | |
Mark McLoughlin | f27be03 | 2008-11-20 15:49:43 +0000 | [diff] [blame] | 61 | #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 62 | #define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32)) |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 63 | #define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64)) |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 64 | |
David Woodhouse | fd18de5 | 2009-05-10 23:57:41 +0100 | [diff] [blame] | 65 | |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 66 | /* VT-d pages must always be _smaller_ than MM pages. Otherwise things |
| 67 | are never going to work. */ |
| 68 | static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn) |
| 69 | { |
| 70 | return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 71 | } |
| 72 | |
| 73 | static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn) |
| 74 | { |
| 75 | return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 76 | } |
| 77 | static inline unsigned long page_to_dma_pfn(struct page *pg) |
| 78 | { |
| 79 | return mm_to_dma_pfn(page_to_pfn(pg)); |
| 80 | } |
| 81 | static inline unsigned long virt_to_dma_pfn(void *p) |
| 82 | { |
| 83 | return page_to_dma_pfn(virt_to_page(p)); |
| 84 | } |
| 85 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 86 | /* global iommu list, set NULL for ignored DMAR units */ |
| 87 | static struct intel_iommu **g_iommus; |
| 88 | |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 89 | static int rwbf_quirk; |
| 90 | |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 91 | /* |
| 92 | * 0: Present |
| 93 | * 1-11: Reserved |
| 94 | * 12-63: Context Ptr (12 - (haw-1)) |
| 95 | * 64-127: Reserved |
| 96 | */ |
| 97 | struct root_entry { |
| 98 | u64 val; |
| 99 | u64 rsvd1; |
| 100 | }; |
| 101 | #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry)) |
| 102 | static inline bool root_present(struct root_entry *root) |
| 103 | { |
| 104 | return (root->val & 1); |
| 105 | } |
| 106 | static inline void set_root_present(struct root_entry *root) |
| 107 | { |
| 108 | root->val |= 1; |
| 109 | } |
| 110 | static inline void set_root_value(struct root_entry *root, unsigned long value) |
| 111 | { |
| 112 | root->val |= value & VTD_PAGE_MASK; |
| 113 | } |
| 114 | |
| 115 | static inline struct context_entry * |
| 116 | get_context_addr_from_root(struct root_entry *root) |
| 117 | { |
| 118 | return (struct context_entry *) |
| 119 | (root_present(root)?phys_to_virt( |
| 120 | root->val & VTD_PAGE_MASK) : |
| 121 | NULL); |
| 122 | } |
| 123 | |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 124 | /* |
| 125 | * low 64 bits: |
| 126 | * 0: present |
| 127 | * 1: fault processing disable |
| 128 | * 2-3: translation type |
| 129 | * 12-63: address space root |
| 130 | * high 64 bits: |
| 131 | * 0-2: address width |
| 132 | * 3-6: aval |
| 133 | * 8-23: domain id |
| 134 | */ |
| 135 | struct context_entry { |
| 136 | u64 lo; |
| 137 | u64 hi; |
| 138 | }; |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 139 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 140 | static inline bool context_present(struct context_entry *context) |
| 141 | { |
| 142 | return (context->lo & 1); |
| 143 | } |
| 144 | static inline void context_set_present(struct context_entry *context) |
| 145 | { |
| 146 | context->lo |= 1; |
| 147 | } |
| 148 | |
| 149 | static inline void context_set_fault_enable(struct context_entry *context) |
| 150 | { |
| 151 | context->lo &= (((u64)-1) << 2) | 1; |
| 152 | } |
| 153 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 154 | static inline void context_set_translation_type(struct context_entry *context, |
| 155 | unsigned long value) |
| 156 | { |
| 157 | context->lo &= (((u64)-1) << 4) | 3; |
| 158 | context->lo |= (value & 3) << 2; |
| 159 | } |
| 160 | |
| 161 | static inline void context_set_address_root(struct context_entry *context, |
| 162 | unsigned long value) |
| 163 | { |
| 164 | context->lo |= value & VTD_PAGE_MASK; |
| 165 | } |
| 166 | |
| 167 | static inline void context_set_address_width(struct context_entry *context, |
| 168 | unsigned long value) |
| 169 | { |
| 170 | context->hi |= value & 7; |
| 171 | } |
| 172 | |
| 173 | static inline void context_set_domain_id(struct context_entry *context, |
| 174 | unsigned long value) |
| 175 | { |
| 176 | context->hi |= (value & ((1 << 16) - 1)) << 8; |
| 177 | } |
| 178 | |
| 179 | static inline void context_clear_entry(struct context_entry *context) |
| 180 | { |
| 181 | context->lo = 0; |
| 182 | context->hi = 0; |
| 183 | } |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 184 | |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 185 | /* |
| 186 | * 0: readable |
| 187 | * 1: writable |
| 188 | * 2-6: reserved |
| 189 | * 7: super page |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 190 | * 8-10: available |
| 191 | * 11: snoop behavior |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 192 | * 12-63: Host physcial address |
| 193 | */ |
| 194 | struct dma_pte { |
| 195 | u64 val; |
| 196 | }; |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 197 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 198 | static inline void dma_clear_pte(struct dma_pte *pte) |
| 199 | { |
| 200 | pte->val = 0; |
| 201 | } |
| 202 | |
| 203 | static inline void dma_set_pte_readable(struct dma_pte *pte) |
| 204 | { |
| 205 | pte->val |= DMA_PTE_READ; |
| 206 | } |
| 207 | |
| 208 | static inline void dma_set_pte_writable(struct dma_pte *pte) |
| 209 | { |
| 210 | pte->val |= DMA_PTE_WRITE; |
| 211 | } |
| 212 | |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 213 | static inline void dma_set_pte_snp(struct dma_pte *pte) |
| 214 | { |
| 215 | pte->val |= DMA_PTE_SNP; |
| 216 | } |
| 217 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 218 | static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot) |
| 219 | { |
| 220 | pte->val = (pte->val & ~3) | (prot & 3); |
| 221 | } |
| 222 | |
| 223 | static inline u64 dma_pte_addr(struct dma_pte *pte) |
| 224 | { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 225 | #ifdef CONFIG_64BIT |
| 226 | return pte->val & VTD_PAGE_MASK; |
| 227 | #else |
| 228 | /* Must have a full atomic 64-bit read */ |
| 229 | return __cmpxchg64(pte, 0ULL, 0ULL) & VTD_PAGE_MASK; |
| 230 | #endif |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 231 | } |
| 232 | |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 233 | static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn) |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 234 | { |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 235 | pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT; |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static inline bool dma_pte_present(struct dma_pte *pte) |
| 239 | { |
| 240 | return (pte->val & 3) != 0; |
| 241 | } |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 242 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 243 | /* |
| 244 | * This domain is a statically identity mapping domain. |
| 245 | * 1. This domain creats a static 1:1 mapping to all usable memory. |
| 246 | * 2. It maps to each iommu if successful. |
| 247 | * 3. Each iommu mapps to this domain if successful. |
| 248 | */ |
| 249 | struct dmar_domain *si_domain; |
| 250 | |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 251 | /* devices under the same p2p bridge are owned in one domain */ |
Mike Day | cdc7b83 | 2008-12-12 17:16:30 +0100 | [diff] [blame] | 252 | #define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0) |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 253 | |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 254 | /* domain represents a virtual machine, more than one devices |
| 255 | * across iommus may be owned in one domain, e.g. kvm guest. |
| 256 | */ |
| 257 | #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1) |
| 258 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 259 | /* si_domain contains mulitple devices */ |
| 260 | #define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2) |
| 261 | |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 262 | struct dmar_domain { |
| 263 | int id; /* domain id */ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 264 | unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/ |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 265 | |
| 266 | struct list_head devices; /* all devices' list */ |
| 267 | struct iova_domain iovad; /* iova's that belong to this domain */ |
| 268 | |
| 269 | struct dma_pte *pgd; /* virtual address */ |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 270 | int gaw; /* max guest address width */ |
| 271 | |
| 272 | /* adjusted guest address width, 0 is level 2 30-bit */ |
| 273 | int agaw; |
| 274 | |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 275 | int flags; /* flags to find out type of domain */ |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 276 | |
| 277 | int iommu_coherency;/* indicate coherency of iommu access */ |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 278 | int iommu_snooping; /* indicate snooping control feature*/ |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 279 | int iommu_count; /* reference count of iommu */ |
| 280 | spinlock_t iommu_lock; /* protect iommu set in domain */ |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 281 | u64 max_addr; /* maximum mapped address */ |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
Mark McLoughlin | a647dacb | 2008-11-20 15:49:48 +0000 | [diff] [blame] | 284 | /* PCI domain-device relationship */ |
| 285 | struct device_domain_info { |
| 286 | struct list_head link; /* link to domain siblings */ |
| 287 | struct list_head global; /* link to global list */ |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 288 | int segment; /* PCI domain */ |
| 289 | u8 bus; /* PCI bus number */ |
Mark McLoughlin | a647dacb | 2008-11-20 15:49:48 +0000 | [diff] [blame] | 290 | u8 devfn; /* PCI devfn number */ |
| 291 | struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */ |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 292 | struct intel_iommu *iommu; /* IOMMU used by this device */ |
Mark McLoughlin | a647dacb | 2008-11-20 15:49:48 +0000 | [diff] [blame] | 293 | struct dmar_domain *domain; /* pointer to domain */ |
| 294 | }; |
| 295 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 296 | static void flush_unmaps_timeout(unsigned long data); |
| 297 | |
| 298 | DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0); |
| 299 | |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 300 | #define HIGH_WATER_MARK 250 |
| 301 | struct deferred_flush_tables { |
| 302 | int next; |
| 303 | struct iova *iova[HIGH_WATER_MARK]; |
| 304 | struct dmar_domain *domain[HIGH_WATER_MARK]; |
| 305 | }; |
| 306 | |
| 307 | static struct deferred_flush_tables *deferred_flush; |
| 308 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 309 | /* bitmap for indexing intel_iommus */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 310 | static int g_num_of_iommus; |
| 311 | |
| 312 | static DEFINE_SPINLOCK(async_umap_flush_lock); |
| 313 | static LIST_HEAD(unmaps_to_do); |
| 314 | |
| 315 | static int timer_on; |
| 316 | static long list_size; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 317 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 318 | static void domain_remove_dev_info(struct dmar_domain *domain); |
| 319 | |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 320 | #ifdef CONFIG_DMAR_DEFAULT_ON |
| 321 | int dmar_disabled = 0; |
| 322 | #else |
| 323 | int dmar_disabled = 1; |
| 324 | #endif /*CONFIG_DMAR_DEFAULT_ON*/ |
| 325 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 326 | static int __initdata dmar_map_gfx = 1; |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 327 | static int dmar_forcedac; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 328 | static int intel_iommu_strict; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 329 | |
| 330 | #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1)) |
| 331 | static DEFINE_SPINLOCK(device_domain_lock); |
| 332 | static LIST_HEAD(device_domain_list); |
| 333 | |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 334 | static struct iommu_ops intel_iommu_ops; |
| 335 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 336 | static int __init intel_iommu_setup(char *str) |
| 337 | { |
| 338 | if (!str) |
| 339 | return -EINVAL; |
| 340 | while (*str) { |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 341 | if (!strncmp(str, "on", 2)) { |
| 342 | dmar_disabled = 0; |
| 343 | printk(KERN_INFO "Intel-IOMMU: enabled\n"); |
| 344 | } else if (!strncmp(str, "off", 3)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 345 | dmar_disabled = 1; |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 346 | printk(KERN_INFO "Intel-IOMMU: disabled\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 347 | } else if (!strncmp(str, "igfx_off", 8)) { |
| 348 | dmar_map_gfx = 0; |
| 349 | printk(KERN_INFO |
| 350 | "Intel-IOMMU: disable GFX device mapping\n"); |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 351 | } else if (!strncmp(str, "forcedac", 8)) { |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 352 | printk(KERN_INFO |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 353 | "Intel-IOMMU: Forcing DAC for PCI devices\n"); |
| 354 | dmar_forcedac = 1; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 355 | } else if (!strncmp(str, "strict", 6)) { |
| 356 | printk(KERN_INFO |
| 357 | "Intel-IOMMU: disable batched IOTLB flush\n"); |
| 358 | intel_iommu_strict = 1; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | str += strcspn(str, ","); |
| 362 | while (*str == ',') |
| 363 | str++; |
| 364 | } |
| 365 | return 0; |
| 366 | } |
| 367 | __setup("intel_iommu=", intel_iommu_setup); |
| 368 | |
| 369 | static struct kmem_cache *iommu_domain_cache; |
| 370 | static struct kmem_cache *iommu_devinfo_cache; |
| 371 | static struct kmem_cache *iommu_iova_cache; |
| 372 | |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 373 | static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep) |
| 374 | { |
| 375 | unsigned int flags; |
| 376 | void *vaddr; |
| 377 | |
| 378 | /* trying to avoid low memory issues */ |
| 379 | flags = current->flags & PF_MEMALLOC; |
| 380 | current->flags |= PF_MEMALLOC; |
| 381 | vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC); |
| 382 | current->flags &= (~PF_MEMALLOC | flags); |
| 383 | return vaddr; |
| 384 | } |
| 385 | |
| 386 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 387 | static inline void *alloc_pgtable_page(void) |
| 388 | { |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 389 | unsigned int flags; |
| 390 | void *vaddr; |
| 391 | |
| 392 | /* trying to avoid low memory issues */ |
| 393 | flags = current->flags & PF_MEMALLOC; |
| 394 | current->flags |= PF_MEMALLOC; |
| 395 | vaddr = (void *)get_zeroed_page(GFP_ATOMIC); |
| 396 | current->flags &= (~PF_MEMALLOC | flags); |
| 397 | return vaddr; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static inline void free_pgtable_page(void *vaddr) |
| 401 | { |
| 402 | free_page((unsigned long)vaddr); |
| 403 | } |
| 404 | |
| 405 | static inline void *alloc_domain_mem(void) |
| 406 | { |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 407 | return iommu_kmem_cache_alloc(iommu_domain_cache); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 410 | static void free_domain_mem(void *vaddr) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 411 | { |
| 412 | kmem_cache_free(iommu_domain_cache, vaddr); |
| 413 | } |
| 414 | |
| 415 | static inline void * alloc_devinfo_mem(void) |
| 416 | { |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 417 | return iommu_kmem_cache_alloc(iommu_devinfo_cache); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static inline void free_devinfo_mem(void *vaddr) |
| 421 | { |
| 422 | kmem_cache_free(iommu_devinfo_cache, vaddr); |
| 423 | } |
| 424 | |
| 425 | struct iova *alloc_iova_mem(void) |
| 426 | { |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 427 | return iommu_kmem_cache_alloc(iommu_iova_cache); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void free_iova_mem(struct iova *iova) |
| 431 | { |
| 432 | kmem_cache_free(iommu_iova_cache, iova); |
| 433 | } |
| 434 | |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 435 | |
| 436 | static inline int width_to_agaw(int width); |
| 437 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 438 | static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw) |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 439 | { |
| 440 | unsigned long sagaw; |
| 441 | int agaw = -1; |
| 442 | |
| 443 | sagaw = cap_sagaw(iommu->cap); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 444 | for (agaw = width_to_agaw(max_gaw); |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 445 | agaw >= 0; agaw--) { |
| 446 | if (test_bit(agaw, &sagaw)) |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | return agaw; |
| 451 | } |
| 452 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 453 | /* |
| 454 | * Calculate max SAGAW for each iommu. |
| 455 | */ |
| 456 | int iommu_calculate_max_sagaw(struct intel_iommu *iommu) |
| 457 | { |
| 458 | return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH); |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * calculate agaw for each iommu. |
| 463 | * "SAGAW" may be different across iommus, use a default agaw, and |
| 464 | * get a supported less agaw for iommus that don't support the default agaw. |
| 465 | */ |
| 466 | int iommu_calculate_agaw(struct intel_iommu *iommu) |
| 467 | { |
| 468 | return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 469 | } |
| 470 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 471 | /* This functionin only returns single iommu in a domain */ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 472 | static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain) |
| 473 | { |
| 474 | int iommu_id; |
| 475 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 476 | /* si_domain and vm domain should not get here. */ |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 477 | BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 478 | BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY); |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 479 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 480 | iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus); |
| 481 | if (iommu_id < 0 || iommu_id >= g_num_of_iommus) |
| 482 | return NULL; |
| 483 | |
| 484 | return g_iommus[iommu_id]; |
| 485 | } |
| 486 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 487 | static void domain_update_iommu_coherency(struct dmar_domain *domain) |
| 488 | { |
| 489 | int i; |
| 490 | |
| 491 | domain->iommu_coherency = 1; |
| 492 | |
| 493 | i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus); |
| 494 | for (; i < g_num_of_iommus; ) { |
| 495 | if (!ecap_coherent(g_iommus[i]->ecap)) { |
| 496 | domain->iommu_coherency = 0; |
| 497 | break; |
| 498 | } |
| 499 | i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1); |
| 500 | } |
| 501 | } |
| 502 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 503 | static void domain_update_iommu_snooping(struct dmar_domain *domain) |
| 504 | { |
| 505 | int i; |
| 506 | |
| 507 | domain->iommu_snooping = 1; |
| 508 | |
| 509 | i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus); |
| 510 | for (; i < g_num_of_iommus; ) { |
| 511 | if (!ecap_sc_support(g_iommus[i]->ecap)) { |
| 512 | domain->iommu_snooping = 0; |
| 513 | break; |
| 514 | } |
| 515 | i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /* Some capabilities may be different across iommus */ |
| 520 | static void domain_update_iommu_cap(struct dmar_domain *domain) |
| 521 | { |
| 522 | domain_update_iommu_coherency(domain); |
| 523 | domain_update_iommu_snooping(domain); |
| 524 | } |
| 525 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 526 | static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn) |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 527 | { |
| 528 | struct dmar_drhd_unit *drhd = NULL; |
| 529 | int i; |
| 530 | |
| 531 | for_each_drhd_unit(drhd) { |
| 532 | if (drhd->ignored) |
| 533 | continue; |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 534 | if (segment != drhd->segment) |
| 535 | continue; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 536 | |
David Woodhouse | 924b623 | 2009-04-04 00:39:25 +0100 | [diff] [blame] | 537 | for (i = 0; i < drhd->devices_cnt; i++) { |
Dirk Hohndel | 288e487 | 2009-01-11 15:33:51 +0000 | [diff] [blame] | 538 | if (drhd->devices[i] && |
| 539 | drhd->devices[i]->bus->number == bus && |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 540 | drhd->devices[i]->devfn == devfn) |
| 541 | return drhd->iommu; |
David Woodhouse | 4958c5d | 2009-04-06 13:30:01 -0700 | [diff] [blame] | 542 | if (drhd->devices[i] && |
| 543 | drhd->devices[i]->subordinate && |
David Woodhouse | 924b623 | 2009-04-04 00:39:25 +0100 | [diff] [blame] | 544 | drhd->devices[i]->subordinate->number <= bus && |
| 545 | drhd->devices[i]->subordinate->subordinate >= bus) |
| 546 | return drhd->iommu; |
| 547 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 548 | |
| 549 | if (drhd->include_all) |
| 550 | return drhd->iommu; |
| 551 | } |
| 552 | |
| 553 | return NULL; |
| 554 | } |
| 555 | |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 556 | static void domain_flush_cache(struct dmar_domain *domain, |
| 557 | void *addr, int size) |
| 558 | { |
| 559 | if (!domain->iommu_coherency) |
| 560 | clflush_cache_range(addr, size); |
| 561 | } |
| 562 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 563 | /* Gets context entry for a given bus and devfn */ |
| 564 | static struct context_entry * device_to_context_entry(struct intel_iommu *iommu, |
| 565 | u8 bus, u8 devfn) |
| 566 | { |
| 567 | struct root_entry *root; |
| 568 | struct context_entry *context; |
| 569 | unsigned long phy_addr; |
| 570 | unsigned long flags; |
| 571 | |
| 572 | spin_lock_irqsave(&iommu->lock, flags); |
| 573 | root = &iommu->root_entry[bus]; |
| 574 | context = get_context_addr_from_root(root); |
| 575 | if (!context) { |
| 576 | context = (struct context_entry *)alloc_pgtable_page(); |
| 577 | if (!context) { |
| 578 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 579 | return NULL; |
| 580 | } |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 581 | __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 582 | phy_addr = virt_to_phys((void *)context); |
| 583 | set_root_value(root, phy_addr); |
| 584 | set_root_present(root); |
| 585 | __iommu_flush_cache(iommu, root, sizeof(*root)); |
| 586 | } |
| 587 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 588 | return &context[devfn]; |
| 589 | } |
| 590 | |
| 591 | static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn) |
| 592 | { |
| 593 | struct root_entry *root; |
| 594 | struct context_entry *context; |
| 595 | int ret; |
| 596 | unsigned long flags; |
| 597 | |
| 598 | spin_lock_irqsave(&iommu->lock, flags); |
| 599 | root = &iommu->root_entry[bus]; |
| 600 | context = get_context_addr_from_root(root); |
| 601 | if (!context) { |
| 602 | ret = 0; |
| 603 | goto out; |
| 604 | } |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 605 | ret = context_present(&context[devfn]); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 606 | out: |
| 607 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn) |
| 612 | { |
| 613 | struct root_entry *root; |
| 614 | struct context_entry *context; |
| 615 | unsigned long flags; |
| 616 | |
| 617 | spin_lock_irqsave(&iommu->lock, flags); |
| 618 | root = &iommu->root_entry[bus]; |
| 619 | context = get_context_addr_from_root(root); |
| 620 | if (context) { |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 621 | context_clear_entry(&context[devfn]); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 622 | __iommu_flush_cache(iommu, &context[devfn], \ |
| 623 | sizeof(*context)); |
| 624 | } |
| 625 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 626 | } |
| 627 | |
| 628 | static void free_context_table(struct intel_iommu *iommu) |
| 629 | { |
| 630 | struct root_entry *root; |
| 631 | int i; |
| 632 | unsigned long flags; |
| 633 | struct context_entry *context; |
| 634 | |
| 635 | spin_lock_irqsave(&iommu->lock, flags); |
| 636 | if (!iommu->root_entry) { |
| 637 | goto out; |
| 638 | } |
| 639 | for (i = 0; i < ROOT_ENTRY_NR; i++) { |
| 640 | root = &iommu->root_entry[i]; |
| 641 | context = get_context_addr_from_root(root); |
| 642 | if (context) |
| 643 | free_pgtable_page(context); |
| 644 | } |
| 645 | free_pgtable_page(iommu->root_entry); |
| 646 | iommu->root_entry = NULL; |
| 647 | out: |
| 648 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 649 | } |
| 650 | |
| 651 | /* page table handling */ |
| 652 | #define LEVEL_STRIDE (9) |
| 653 | #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1) |
| 654 | |
| 655 | static inline int agaw_to_level(int agaw) |
| 656 | { |
| 657 | return agaw + 2; |
| 658 | } |
| 659 | |
| 660 | static inline int agaw_to_width(int agaw) |
| 661 | { |
| 662 | return 30 + agaw * LEVEL_STRIDE; |
| 663 | |
| 664 | } |
| 665 | |
| 666 | static inline int width_to_agaw(int width) |
| 667 | { |
| 668 | return (width - 30) / LEVEL_STRIDE; |
| 669 | } |
| 670 | |
| 671 | static inline unsigned int level_to_offset_bits(int level) |
| 672 | { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 673 | return (level - 1) * LEVEL_STRIDE; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 674 | } |
| 675 | |
David Woodhouse | 77dfa56 | 2009-06-27 16:40:08 +0100 | [diff] [blame] | 676 | static inline int pfn_level_offset(unsigned long pfn, int level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 677 | { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 678 | return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 679 | } |
| 680 | |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 681 | static inline unsigned long level_mask(int level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 682 | { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 683 | return -1UL << level_to_offset_bits(level); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 684 | } |
| 685 | |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 686 | static inline unsigned long level_size(int level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 687 | { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 688 | return 1UL << level_to_offset_bits(level); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 689 | } |
| 690 | |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 691 | static inline unsigned long align_to_level(unsigned long pfn, int level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 692 | { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 693 | return (pfn + level_size(level) - 1) & level_mask(level); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 694 | } |
| 695 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 696 | static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, |
| 697 | unsigned long pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 698 | { |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 699 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 700 | struct dma_pte *parent, *pte = NULL; |
| 701 | int level = agaw_to_level(domain->agaw); |
| 702 | int offset; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 703 | |
| 704 | BUG_ON(!domain->pgd); |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 705 | BUG_ON(addr_width < BITS_PER_LONG && pfn >> addr_width); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 706 | parent = domain->pgd; |
| 707 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 708 | while (level > 0) { |
| 709 | void *tmp_page; |
| 710 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 711 | offset = pfn_level_offset(pfn, level); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 712 | pte = &parent[offset]; |
| 713 | if (level == 1) |
| 714 | break; |
| 715 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 716 | if (!dma_pte_present(pte)) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 717 | uint64_t pteval; |
| 718 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 719 | tmp_page = alloc_pgtable_page(); |
| 720 | |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame^] | 721 | if (!tmp_page) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 722 | return NULL; |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame^] | 723 | |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 724 | domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE); |
| 725 | pteval = (virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE; |
| 726 | if (cmpxchg64(&pte->val, 0ULL, pteval)) { |
| 727 | /* Someone else set it while we were thinking; use theirs. */ |
| 728 | free_pgtable_page(tmp_page); |
| 729 | } else { |
| 730 | dma_pte_addr(pte); |
| 731 | domain_flush_cache(domain, pte, sizeof(*pte)); |
| 732 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 733 | } |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 734 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 735 | level--; |
| 736 | } |
| 737 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 738 | return pte; |
| 739 | } |
| 740 | |
| 741 | /* return address's pte at specific level */ |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 742 | static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain, |
| 743 | unsigned long pfn, |
| 744 | int level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 745 | { |
| 746 | struct dma_pte *parent, *pte = NULL; |
| 747 | int total = agaw_to_level(domain->agaw); |
| 748 | int offset; |
| 749 | |
| 750 | parent = domain->pgd; |
| 751 | while (level <= total) { |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 752 | offset = pfn_level_offset(pfn, total); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 753 | pte = &parent[offset]; |
| 754 | if (level == total) |
| 755 | return pte; |
| 756 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 757 | if (!dma_pte_present(pte)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 758 | break; |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 759 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 760 | total--; |
| 761 | } |
| 762 | return NULL; |
| 763 | } |
| 764 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 765 | /* clear last level pte, a tlb flush should be followed */ |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 766 | static void dma_pte_clear_range(struct dmar_domain *domain, |
| 767 | unsigned long start_pfn, |
| 768 | unsigned long last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 769 | { |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 770 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 771 | struct dma_pte *first_pte, *pte; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 772 | |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 773 | BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width); |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 774 | BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width); |
David Woodhouse | 66eae84 | 2009-06-27 19:00:32 +0100 | [diff] [blame] | 775 | |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 776 | /* we don't need lock here; nobody else touches the iova range */ |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 777 | while (start_pfn <= last_pfn) { |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 778 | first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1); |
| 779 | if (!pte) { |
| 780 | start_pfn = align_to_level(start_pfn + 1, 2); |
| 781 | continue; |
| 782 | } |
| 783 | while (start_pfn <= last_pfn && |
| 784 | (unsigned long)pte >> VTD_PAGE_SHIFT == |
| 785 | (unsigned long)first_pte >> VTD_PAGE_SHIFT) { |
| 786 | dma_clear_pte(pte); |
| 787 | start_pfn++; |
| 788 | pte++; |
| 789 | } |
| 790 | domain_flush_cache(domain, first_pte, |
| 791 | (void *)pte - (void *)first_pte); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | /* free page table pages. last level pte should already be cleared */ |
| 796 | static void dma_pte_free_pagetable(struct dmar_domain *domain, |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 797 | unsigned long start_pfn, |
| 798 | unsigned long last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 799 | { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 800 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 801 | struct dma_pte *first_pte, *pte; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 802 | int total = agaw_to_level(domain->agaw); |
| 803 | int level; |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 804 | unsigned long tmp; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 805 | |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 806 | BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width); |
| 807 | BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 808 | |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 809 | /* We don't need lock here; nobody else touches the iova range */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 810 | level = 2; |
| 811 | while (level <= total) { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 812 | tmp = align_to_level(start_pfn, level); |
| 813 | |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 814 | /* If we can't even clear one PTE at this level, we're done */ |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 815 | if (tmp + level_size(level) - 1 > last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 816 | return; |
| 817 | |
David Woodhouse | 3d7b0e4 | 2009-06-30 03:38:09 +0100 | [diff] [blame] | 818 | while (tmp + level_size(level) - 1 <= last_pfn) { |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 819 | first_pte = pte = dma_pfn_level_pte(domain, tmp, level); |
| 820 | if (!pte) { |
| 821 | tmp = align_to_level(tmp + 1, level + 1); |
| 822 | continue; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 823 | } |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 824 | while (tmp + level_size(level) - 1 <= last_pfn && |
| 825 | (unsigned long)pte >> VTD_PAGE_SHIFT == |
| 826 | (unsigned long)first_pte >> VTD_PAGE_SHIFT) { |
| 827 | free_pgtable_page(phys_to_virt(dma_pte_addr(pte))); |
| 828 | dma_clear_pte(pte); |
| 829 | pte++; |
| 830 | tmp += level_size(level); |
| 831 | } |
| 832 | domain_flush_cache(domain, first_pte, |
| 833 | (void *)pte - (void *)first_pte); |
| 834 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 835 | } |
| 836 | level++; |
| 837 | } |
| 838 | /* free pgd */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 839 | if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 840 | free_pgtable_page(domain->pgd); |
| 841 | domain->pgd = NULL; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | /* iommu handling */ |
| 846 | static int iommu_alloc_root_entry(struct intel_iommu *iommu) |
| 847 | { |
| 848 | struct root_entry *root; |
| 849 | unsigned long flags; |
| 850 | |
| 851 | root = (struct root_entry *)alloc_pgtable_page(); |
| 852 | if (!root) |
| 853 | return -ENOMEM; |
| 854 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 855 | __iommu_flush_cache(iommu, root, ROOT_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 856 | |
| 857 | spin_lock_irqsave(&iommu->lock, flags); |
| 858 | iommu->root_entry = root; |
| 859 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 864 | static void iommu_set_root_entry(struct intel_iommu *iommu) |
| 865 | { |
| 866 | void *addr; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 867 | u32 sts; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 868 | unsigned long flag; |
| 869 | |
| 870 | addr = iommu->root_entry; |
| 871 | |
| 872 | spin_lock_irqsave(&iommu->register_lock, flag); |
| 873 | dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr)); |
| 874 | |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 875 | writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 876 | |
| 877 | /* Make sure hardware complete it */ |
| 878 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 879 | readl, (sts & DMA_GSTS_RTPS), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 880 | |
| 881 | spin_unlock_irqrestore(&iommu->register_lock, flag); |
| 882 | } |
| 883 | |
| 884 | static void iommu_flush_write_buffer(struct intel_iommu *iommu) |
| 885 | { |
| 886 | u32 val; |
| 887 | unsigned long flag; |
| 888 | |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 889 | if (!rwbf_quirk && !cap_rwbf(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 890 | return; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 891 | |
| 892 | spin_lock_irqsave(&iommu->register_lock, flag); |
David Woodhouse | 462b60f | 2009-05-10 20:18:18 +0100 | [diff] [blame] | 893 | writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 894 | |
| 895 | /* Make sure hardware complete it */ |
| 896 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 897 | readl, (!(val & DMA_GSTS_WBFS)), val); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 898 | |
| 899 | spin_unlock_irqrestore(&iommu->register_lock, flag); |
| 900 | } |
| 901 | |
| 902 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 903 | static void __iommu_flush_context(struct intel_iommu *iommu, |
| 904 | u16 did, u16 source_id, u8 function_mask, |
| 905 | u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 906 | { |
| 907 | u64 val = 0; |
| 908 | unsigned long flag; |
| 909 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 910 | switch (type) { |
| 911 | case DMA_CCMD_GLOBAL_INVL: |
| 912 | val = DMA_CCMD_GLOBAL_INVL; |
| 913 | break; |
| 914 | case DMA_CCMD_DOMAIN_INVL: |
| 915 | val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did); |
| 916 | break; |
| 917 | case DMA_CCMD_DEVICE_INVL: |
| 918 | val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did) |
| 919 | | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask); |
| 920 | break; |
| 921 | default: |
| 922 | BUG(); |
| 923 | } |
| 924 | val |= DMA_CCMD_ICC; |
| 925 | |
| 926 | spin_lock_irqsave(&iommu->register_lock, flag); |
| 927 | dmar_writeq(iommu->reg + DMAR_CCMD_REG, val); |
| 928 | |
| 929 | /* Make sure hardware complete it */ |
| 930 | IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG, |
| 931 | dmar_readq, (!(val & DMA_CCMD_ICC)), val); |
| 932 | |
| 933 | spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 936 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 937 | static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, |
| 938 | u64 addr, unsigned int size_order, u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 939 | { |
| 940 | int tlb_offset = ecap_iotlb_offset(iommu->ecap); |
| 941 | u64 val = 0, val_iva = 0; |
| 942 | unsigned long flag; |
| 943 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 944 | switch (type) { |
| 945 | case DMA_TLB_GLOBAL_FLUSH: |
| 946 | /* global flush doesn't need set IVA_REG */ |
| 947 | val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT; |
| 948 | break; |
| 949 | case DMA_TLB_DSI_FLUSH: |
| 950 | val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
| 951 | break; |
| 952 | case DMA_TLB_PSI_FLUSH: |
| 953 | val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
| 954 | /* Note: always flush non-leaf currently */ |
| 955 | val_iva = size_order | addr; |
| 956 | break; |
| 957 | default: |
| 958 | BUG(); |
| 959 | } |
| 960 | /* Note: set drain read/write */ |
| 961 | #if 0 |
| 962 | /* |
| 963 | * This is probably to be super secure.. Looks like we can |
| 964 | * ignore it without any impact. |
| 965 | */ |
| 966 | if (cap_read_drain(iommu->cap)) |
| 967 | val |= DMA_TLB_READ_DRAIN; |
| 968 | #endif |
| 969 | if (cap_write_drain(iommu->cap)) |
| 970 | val |= DMA_TLB_WRITE_DRAIN; |
| 971 | |
| 972 | spin_lock_irqsave(&iommu->register_lock, flag); |
| 973 | /* Note: Only uses first TLB reg currently */ |
| 974 | if (val_iva) |
| 975 | dmar_writeq(iommu->reg + tlb_offset, val_iva); |
| 976 | dmar_writeq(iommu->reg + tlb_offset + 8, val); |
| 977 | |
| 978 | /* Make sure hardware complete it */ |
| 979 | IOMMU_WAIT_OP(iommu, tlb_offset + 8, |
| 980 | dmar_readq, (!(val & DMA_TLB_IVT)), val); |
| 981 | |
| 982 | spin_unlock_irqrestore(&iommu->register_lock, flag); |
| 983 | |
| 984 | /* check IOTLB invalidation granularity */ |
| 985 | if (DMA_TLB_IAIG(val) == 0) |
| 986 | printk(KERN_ERR"IOMMU: flush IOTLB failed\n"); |
| 987 | if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type)) |
| 988 | pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 989 | (unsigned long long)DMA_TLB_IIRG(type), |
| 990 | (unsigned long long)DMA_TLB_IAIG(val)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 993 | static struct device_domain_info *iommu_support_dev_iotlb( |
| 994 | struct dmar_domain *domain, int segment, u8 bus, u8 devfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 995 | { |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 996 | int found = 0; |
| 997 | unsigned long flags; |
| 998 | struct device_domain_info *info; |
| 999 | struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn); |
| 1000 | |
| 1001 | if (!ecap_dev_iotlb_support(iommu->ecap)) |
| 1002 | return NULL; |
| 1003 | |
| 1004 | if (!iommu->qi) |
| 1005 | return NULL; |
| 1006 | |
| 1007 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1008 | list_for_each_entry(info, &domain->devices, link) |
| 1009 | if (info->bus == bus && info->devfn == devfn) { |
| 1010 | found = 1; |
| 1011 | break; |
| 1012 | } |
| 1013 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1014 | |
| 1015 | if (!found || !info->dev) |
| 1016 | return NULL; |
| 1017 | |
| 1018 | if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS)) |
| 1019 | return NULL; |
| 1020 | |
| 1021 | if (!dmar_find_matched_atsr_unit(info->dev)) |
| 1022 | return NULL; |
| 1023 | |
| 1024 | info->iommu = iommu; |
| 1025 | |
| 1026 | return info; |
| 1027 | } |
| 1028 | |
| 1029 | static void iommu_enable_dev_iotlb(struct device_domain_info *info) |
| 1030 | { |
| 1031 | if (!info) |
| 1032 | return; |
| 1033 | |
| 1034 | pci_enable_ats(info->dev, VTD_PAGE_SHIFT); |
| 1035 | } |
| 1036 | |
| 1037 | static void iommu_disable_dev_iotlb(struct device_domain_info *info) |
| 1038 | { |
| 1039 | if (!info->dev || !pci_ats_enabled(info->dev)) |
| 1040 | return; |
| 1041 | |
| 1042 | pci_disable_ats(info->dev); |
| 1043 | } |
| 1044 | |
| 1045 | static void iommu_flush_dev_iotlb(struct dmar_domain *domain, |
| 1046 | u64 addr, unsigned mask) |
| 1047 | { |
| 1048 | u16 sid, qdep; |
| 1049 | unsigned long flags; |
| 1050 | struct device_domain_info *info; |
| 1051 | |
| 1052 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1053 | list_for_each_entry(info, &domain->devices, link) { |
| 1054 | if (!info->dev || !pci_ats_enabled(info->dev)) |
| 1055 | continue; |
| 1056 | |
| 1057 | sid = info->bus << 8 | info->devfn; |
| 1058 | qdep = pci_ats_queue_depth(info->dev); |
| 1059 | qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask); |
| 1060 | } |
| 1061 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1062 | } |
| 1063 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1064 | static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did, |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 1065 | unsigned long pfn, unsigned int pages) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1066 | { |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1067 | unsigned int mask = ilog2(__roundup_pow_of_two(pages)); |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 1068 | uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1069 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1070 | BUG_ON(pages == 0); |
| 1071 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1072 | /* |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1073 | * Fallback to domain selective flush if no PSI support or the size is |
| 1074 | * too big. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1075 | * PSI requires page size to be 2 ^ x, and the base address is naturally |
| 1076 | * aligned to the size |
| 1077 | */ |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1078 | if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap)) |
| 1079 | iommu->flush.flush_iotlb(iommu, did, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1080 | DMA_TLB_DSI_FLUSH); |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1081 | else |
| 1082 | iommu->flush.flush_iotlb(iommu, did, addr, mask, |
| 1083 | DMA_TLB_PSI_FLUSH); |
Yu Zhao | bf92df3 | 2009-06-29 11:31:45 +0800 | [diff] [blame] | 1084 | |
| 1085 | /* |
| 1086 | * In caching mode, domain ID 0 is reserved for non-present to present |
| 1087 | * mapping flush. Device IOTLB doesn't need to be flushed in this case. |
| 1088 | */ |
| 1089 | if (!cap_caching_mode(iommu->cap) || did) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1090 | iommu_flush_dev_iotlb(iommu->domains[did], addr, mask); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1093 | static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu) |
| 1094 | { |
| 1095 | u32 pmen; |
| 1096 | unsigned long flags; |
| 1097 | |
| 1098 | spin_lock_irqsave(&iommu->register_lock, flags); |
| 1099 | pmen = readl(iommu->reg + DMAR_PMEN_REG); |
| 1100 | pmen &= ~DMA_PMEN_EPM; |
| 1101 | writel(pmen, iommu->reg + DMAR_PMEN_REG); |
| 1102 | |
| 1103 | /* wait for the protected region status bit to clear */ |
| 1104 | IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG, |
| 1105 | readl, !(pmen & DMA_PMEN_PRS), pmen); |
| 1106 | |
| 1107 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 1108 | } |
| 1109 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1110 | static int iommu_enable_translation(struct intel_iommu *iommu) |
| 1111 | { |
| 1112 | u32 sts; |
| 1113 | unsigned long flags; |
| 1114 | |
| 1115 | spin_lock_irqsave(&iommu->register_lock, flags); |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1116 | iommu->gcmd |= DMA_GCMD_TE; |
| 1117 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1118 | |
| 1119 | /* Make sure hardware complete it */ |
| 1120 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1121 | readl, (sts & DMA_GSTS_TES), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1122 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1123 | spin_unlock_irqrestore(&iommu->register_lock, flags); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static int iommu_disable_translation(struct intel_iommu *iommu) |
| 1128 | { |
| 1129 | u32 sts; |
| 1130 | unsigned long flag; |
| 1131 | |
| 1132 | spin_lock_irqsave(&iommu->register_lock, flag); |
| 1133 | iommu->gcmd &= ~DMA_GCMD_TE; |
| 1134 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 1135 | |
| 1136 | /* Make sure hardware complete it */ |
| 1137 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1138 | readl, (!(sts & DMA_GSTS_TES)), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1139 | |
| 1140 | spin_unlock_irqrestore(&iommu->register_lock, flag); |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 1144 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1145 | static int iommu_init_domains(struct intel_iommu *iommu) |
| 1146 | { |
| 1147 | unsigned long ndomains; |
| 1148 | unsigned long nlongs; |
| 1149 | |
| 1150 | ndomains = cap_ndoms(iommu->cap); |
| 1151 | pr_debug("Number of Domains supportd <%ld>\n", ndomains); |
| 1152 | nlongs = BITS_TO_LONGS(ndomains); |
| 1153 | |
| 1154 | /* TBD: there might be 64K domains, |
| 1155 | * consider other allocation for future chip |
| 1156 | */ |
| 1157 | iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL); |
| 1158 | if (!iommu->domain_ids) { |
| 1159 | printk(KERN_ERR "Allocating domain id array failed\n"); |
| 1160 | return -ENOMEM; |
| 1161 | } |
| 1162 | iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *), |
| 1163 | GFP_KERNEL); |
| 1164 | if (!iommu->domains) { |
| 1165 | printk(KERN_ERR "Allocating domain array failed\n"); |
| 1166 | kfree(iommu->domain_ids); |
| 1167 | return -ENOMEM; |
| 1168 | } |
| 1169 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 1170 | spin_lock_init(&iommu->lock); |
| 1171 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1172 | /* |
| 1173 | * if Caching mode is set, then invalid translations are tagged |
| 1174 | * with domainid 0. Hence we need to pre-allocate it. |
| 1175 | */ |
| 1176 | if (cap_caching_mode(iommu->cap)) |
| 1177 | set_bit(0, iommu->domain_ids); |
| 1178 | return 0; |
| 1179 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1180 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1181 | |
| 1182 | static void domain_exit(struct dmar_domain *domain); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 1183 | static void vm_domain_exit(struct dmar_domain *domain); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 1184 | |
| 1185 | void free_dmar_iommu(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1186 | { |
| 1187 | struct dmar_domain *domain; |
| 1188 | int i; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1189 | unsigned long flags; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1190 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1191 | i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap)); |
| 1192 | for (; i < cap_ndoms(iommu->cap); ) { |
| 1193 | domain = iommu->domains[i]; |
| 1194 | clear_bit(i, iommu->domain_ids); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1195 | |
| 1196 | spin_lock_irqsave(&domain->iommu_lock, flags); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 1197 | if (--domain->iommu_count == 0) { |
| 1198 | if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) |
| 1199 | vm_domain_exit(domain); |
| 1200 | else |
| 1201 | domain_exit(domain); |
| 1202 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1203 | spin_unlock_irqrestore(&domain->iommu_lock, flags); |
| 1204 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1205 | i = find_next_bit(iommu->domain_ids, |
| 1206 | cap_ndoms(iommu->cap), i+1); |
| 1207 | } |
| 1208 | |
| 1209 | if (iommu->gcmd & DMA_GCMD_TE) |
| 1210 | iommu_disable_translation(iommu); |
| 1211 | |
| 1212 | if (iommu->irq) { |
| 1213 | set_irq_data(iommu->irq, NULL); |
| 1214 | /* This will mask the irq */ |
| 1215 | free_irq(iommu->irq, iommu); |
| 1216 | destroy_irq(iommu->irq); |
| 1217 | } |
| 1218 | |
| 1219 | kfree(iommu->domains); |
| 1220 | kfree(iommu->domain_ids); |
| 1221 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 1222 | g_iommus[iommu->seq_id] = NULL; |
| 1223 | |
| 1224 | /* if all iommus are freed, free g_iommus */ |
| 1225 | for (i = 0; i < g_num_of_iommus; i++) { |
| 1226 | if (g_iommus[i]) |
| 1227 | break; |
| 1228 | } |
| 1229 | |
| 1230 | if (i == g_num_of_iommus) |
| 1231 | kfree(g_iommus); |
| 1232 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1233 | /* free context mapping */ |
| 1234 | free_context_table(iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1237 | static struct dmar_domain *alloc_domain(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1238 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1239 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1240 | |
| 1241 | domain = alloc_domain_mem(); |
| 1242 | if (!domain) |
| 1243 | return NULL; |
| 1244 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1245 | memset(&domain->iommu_bmp, 0, sizeof(unsigned long)); |
Weidong Han | d71a2f3 | 2008-12-07 21:13:41 +0800 | [diff] [blame] | 1246 | domain->flags = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1247 | |
| 1248 | return domain; |
| 1249 | } |
| 1250 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1251 | static int iommu_attach_domain(struct dmar_domain *domain, |
| 1252 | struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1253 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1254 | int num; |
| 1255 | unsigned long ndomains; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1256 | unsigned long flags; |
| 1257 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1258 | ndomains = cap_ndoms(iommu->cap); |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1259 | |
| 1260 | spin_lock_irqsave(&iommu->lock, flags); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1261 | |
| 1262 | num = find_first_zero_bit(iommu->domain_ids, ndomains); |
| 1263 | if (num >= ndomains) { |
| 1264 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1265 | printk(KERN_ERR "IOMMU: no free domain ids\n"); |
| 1266 | return -ENOMEM; |
| 1267 | } |
| 1268 | |
| 1269 | domain->id = num; |
| 1270 | set_bit(num, iommu->domain_ids); |
| 1271 | set_bit(iommu->seq_id, &domain->iommu_bmp); |
| 1272 | iommu->domains[num] = domain; |
| 1273 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1274 | |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | static void iommu_detach_domain(struct dmar_domain *domain, |
| 1279 | struct intel_iommu *iommu) |
| 1280 | { |
| 1281 | unsigned long flags; |
| 1282 | int num, ndomains; |
| 1283 | int found = 0; |
| 1284 | |
| 1285 | spin_lock_irqsave(&iommu->lock, flags); |
| 1286 | ndomains = cap_ndoms(iommu->cap); |
| 1287 | num = find_first_bit(iommu->domain_ids, ndomains); |
| 1288 | for (; num < ndomains; ) { |
| 1289 | if (iommu->domains[num] == domain) { |
| 1290 | found = 1; |
| 1291 | break; |
| 1292 | } |
| 1293 | num = find_next_bit(iommu->domain_ids, |
| 1294 | cap_ndoms(iommu->cap), num+1); |
| 1295 | } |
| 1296 | |
| 1297 | if (found) { |
| 1298 | clear_bit(num, iommu->domain_ids); |
| 1299 | clear_bit(iommu->seq_id, &domain->iommu_bmp); |
| 1300 | iommu->domains[num] = NULL; |
| 1301 | } |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1302 | spin_unlock_irqrestore(&iommu->lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static struct iova_domain reserved_iova_list; |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1306 | static struct lock_class_key reserved_alloc_key; |
| 1307 | static struct lock_class_key reserved_rbtree_key; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1308 | |
| 1309 | static void dmar_init_reserved_ranges(void) |
| 1310 | { |
| 1311 | struct pci_dev *pdev = NULL; |
| 1312 | struct iova *iova; |
| 1313 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1314 | |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 1315 | init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1316 | |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1317 | lockdep_set_class(&reserved_iova_list.iova_alloc_lock, |
| 1318 | &reserved_alloc_key); |
| 1319 | lockdep_set_class(&reserved_iova_list.iova_rbtree_lock, |
| 1320 | &reserved_rbtree_key); |
| 1321 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1322 | /* IOAPIC ranges shouldn't be accessed by DMA */ |
| 1323 | iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START), |
| 1324 | IOVA_PFN(IOAPIC_RANGE_END)); |
| 1325 | if (!iova) |
| 1326 | printk(KERN_ERR "Reserve IOAPIC range failed\n"); |
| 1327 | |
| 1328 | /* Reserve all PCI MMIO to avoid peer-to-peer access */ |
| 1329 | for_each_pci_dev(pdev) { |
| 1330 | struct resource *r; |
| 1331 | |
| 1332 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 1333 | r = &pdev->resource[i]; |
| 1334 | if (!r->flags || !(r->flags & IORESOURCE_MEM)) |
| 1335 | continue; |
David Woodhouse | 1a4a455 | 2009-06-28 16:00:42 +0100 | [diff] [blame] | 1336 | iova = reserve_iova(&reserved_iova_list, |
| 1337 | IOVA_PFN(r->start), |
| 1338 | IOVA_PFN(r->end)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1339 | if (!iova) |
| 1340 | printk(KERN_ERR "Reserve iova failed\n"); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | } |
| 1345 | |
| 1346 | static void domain_reserve_special_ranges(struct dmar_domain *domain) |
| 1347 | { |
| 1348 | copy_reserved_iova(&reserved_iova_list, &domain->iovad); |
| 1349 | } |
| 1350 | |
| 1351 | static inline int guestwidth_to_adjustwidth(int gaw) |
| 1352 | { |
| 1353 | int agaw; |
| 1354 | int r = (gaw - 12) % 9; |
| 1355 | |
| 1356 | if (r == 0) |
| 1357 | agaw = gaw; |
| 1358 | else |
| 1359 | agaw = gaw + 9 - r; |
| 1360 | if (agaw > 64) |
| 1361 | agaw = 64; |
| 1362 | return agaw; |
| 1363 | } |
| 1364 | |
| 1365 | static int domain_init(struct dmar_domain *domain, int guest_width) |
| 1366 | { |
| 1367 | struct intel_iommu *iommu; |
| 1368 | int adjust_width, agaw; |
| 1369 | unsigned long sagaw; |
| 1370 | |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 1371 | init_iova_domain(&domain->iovad, DMA_32BIT_PFN); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1372 | spin_lock_init(&domain->iommu_lock); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1373 | |
| 1374 | domain_reserve_special_ranges(domain); |
| 1375 | |
| 1376 | /* calculate AGAW */ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1377 | iommu = domain_get_iommu(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1378 | if (guest_width > cap_mgaw(iommu->cap)) |
| 1379 | guest_width = cap_mgaw(iommu->cap); |
| 1380 | domain->gaw = guest_width; |
| 1381 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 1382 | agaw = width_to_agaw(adjust_width); |
| 1383 | sagaw = cap_sagaw(iommu->cap); |
| 1384 | if (!test_bit(agaw, &sagaw)) { |
| 1385 | /* hardware doesn't support it, choose a bigger one */ |
| 1386 | pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw); |
| 1387 | agaw = find_next_bit(&sagaw, 5, agaw); |
| 1388 | if (agaw >= 5) |
| 1389 | return -ENODEV; |
| 1390 | } |
| 1391 | domain->agaw = agaw; |
| 1392 | INIT_LIST_HEAD(&domain->devices); |
| 1393 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 1394 | if (ecap_coherent(iommu->ecap)) |
| 1395 | domain->iommu_coherency = 1; |
| 1396 | else |
| 1397 | domain->iommu_coherency = 0; |
| 1398 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 1399 | if (ecap_sc_support(iommu->ecap)) |
| 1400 | domain->iommu_snooping = 1; |
| 1401 | else |
| 1402 | domain->iommu_snooping = 0; |
| 1403 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1404 | domain->iommu_count = 1; |
| 1405 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1406 | /* always allocate the top pgd */ |
| 1407 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(); |
| 1408 | if (!domain->pgd) |
| 1409 | return -ENOMEM; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1410 | __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1411 | return 0; |
| 1412 | } |
| 1413 | |
| 1414 | static void domain_exit(struct dmar_domain *domain) |
| 1415 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1416 | struct dmar_drhd_unit *drhd; |
| 1417 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1418 | |
| 1419 | /* Domain 0 is reserved, so dont process it */ |
| 1420 | if (!domain) |
| 1421 | return; |
| 1422 | |
| 1423 | domain_remove_dev_info(domain); |
| 1424 | /* destroy iovas */ |
| 1425 | put_iova_domain(&domain->iovad); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1426 | |
| 1427 | /* clear ptes */ |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 1428 | dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1429 | |
| 1430 | /* free page tables */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 1431 | dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1432 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1433 | for_each_active_iommu(iommu, drhd) |
| 1434 | if (test_bit(iommu->seq_id, &domain->iommu_bmp)) |
| 1435 | iommu_detach_domain(domain, iommu); |
| 1436 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1437 | free_domain_mem(domain); |
| 1438 | } |
| 1439 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1440 | static int domain_context_mapping_one(struct dmar_domain *domain, int segment, |
| 1441 | u8 bus, u8 devfn, int translation) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1442 | { |
| 1443 | struct context_entry *context; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1444 | unsigned long flags; |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1445 | struct intel_iommu *iommu; |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1446 | struct dma_pte *pgd; |
| 1447 | unsigned long num; |
| 1448 | unsigned long ndomains; |
| 1449 | int id; |
| 1450 | int agaw; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1451 | struct device_domain_info *info = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1452 | |
| 1453 | pr_debug("Set context mapping for %02x:%02x.%d\n", |
| 1454 | bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1455 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1456 | BUG_ON(!domain->pgd); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1457 | BUG_ON(translation != CONTEXT_TT_PASS_THROUGH && |
| 1458 | translation != CONTEXT_TT_MULTI_LEVEL); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1459 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1460 | iommu = device_to_iommu(segment, bus, devfn); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1461 | if (!iommu) |
| 1462 | return -ENODEV; |
| 1463 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1464 | context = device_to_context_entry(iommu, bus, devfn); |
| 1465 | if (!context) |
| 1466 | return -ENOMEM; |
| 1467 | spin_lock_irqsave(&iommu->lock, flags); |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 1468 | if (context_present(context)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1469 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1473 | id = domain->id; |
| 1474 | pgd = domain->pgd; |
| 1475 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1476 | if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE || |
| 1477 | domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) { |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1478 | int found = 0; |
| 1479 | |
| 1480 | /* find an available domain id for this device in iommu */ |
| 1481 | ndomains = cap_ndoms(iommu->cap); |
| 1482 | num = find_first_bit(iommu->domain_ids, ndomains); |
| 1483 | for (; num < ndomains; ) { |
| 1484 | if (iommu->domains[num] == domain) { |
| 1485 | id = num; |
| 1486 | found = 1; |
| 1487 | break; |
| 1488 | } |
| 1489 | num = find_next_bit(iommu->domain_ids, |
| 1490 | cap_ndoms(iommu->cap), num+1); |
| 1491 | } |
| 1492 | |
| 1493 | if (found == 0) { |
| 1494 | num = find_first_zero_bit(iommu->domain_ids, ndomains); |
| 1495 | if (num >= ndomains) { |
| 1496 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1497 | printk(KERN_ERR "IOMMU: no free domain ids\n"); |
| 1498 | return -EFAULT; |
| 1499 | } |
| 1500 | |
| 1501 | set_bit(num, iommu->domain_ids); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1502 | set_bit(iommu->seq_id, &domain->iommu_bmp); |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1503 | iommu->domains[num] = domain; |
| 1504 | id = num; |
| 1505 | } |
| 1506 | |
| 1507 | /* Skip top levels of page tables for |
| 1508 | * iommu which has less agaw than default. |
| 1509 | */ |
| 1510 | for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) { |
| 1511 | pgd = phys_to_virt(dma_pte_addr(pgd)); |
| 1512 | if (!dma_pte_present(pgd)) { |
| 1513 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1514 | return -ENOMEM; |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | context_set_domain_id(context, id); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1520 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1521 | if (translation != CONTEXT_TT_PASS_THROUGH) { |
| 1522 | info = iommu_support_dev_iotlb(domain, segment, bus, devfn); |
| 1523 | translation = info ? CONTEXT_TT_DEV_IOTLB : |
| 1524 | CONTEXT_TT_MULTI_LEVEL; |
| 1525 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1526 | /* |
| 1527 | * In pass through mode, AW must be programmed to indicate the largest |
| 1528 | * AGAW value supported by hardware. And ASR is ignored by hardware. |
| 1529 | */ |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1530 | if (unlikely(translation == CONTEXT_TT_PASS_THROUGH)) |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1531 | context_set_address_width(context, iommu->msagaw); |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1532 | else { |
| 1533 | context_set_address_root(context, virt_to_phys(pgd)); |
| 1534 | context_set_address_width(context, iommu->agaw); |
| 1535 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1536 | |
| 1537 | context_set_translation_type(context, translation); |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 1538 | context_set_fault_enable(context); |
| 1539 | context_set_present(context); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1540 | domain_flush_cache(domain, context, sizeof(*context)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1541 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1542 | /* |
| 1543 | * It's a non-present to present mapping. If hardware doesn't cache |
| 1544 | * non-present entry we only need to flush the write-buffer. If the |
| 1545 | * _does_ cache non-present entries, then it does so in the special |
| 1546 | * domain #0, which we have to flush: |
| 1547 | */ |
| 1548 | if (cap_caching_mode(iommu->cap)) { |
| 1549 | iommu->flush.flush_context(iommu, 0, |
| 1550 | (((u16)bus) << 8) | devfn, |
| 1551 | DMA_CCMD_MASK_NOBIT, |
| 1552 | DMA_CCMD_DEVICE_INVL); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1553 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1554 | } else { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1555 | iommu_flush_write_buffer(iommu); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1556 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1557 | iommu_enable_dev_iotlb(info); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1558 | spin_unlock_irqrestore(&iommu->lock, flags); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1559 | |
| 1560 | spin_lock_irqsave(&domain->iommu_lock, flags); |
| 1561 | if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) { |
| 1562 | domain->iommu_count++; |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 1563 | domain_update_iommu_cap(domain); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1564 | } |
| 1565 | spin_unlock_irqrestore(&domain->iommu_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | static int |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1570 | domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev, |
| 1571 | int translation) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1572 | { |
| 1573 | int ret; |
| 1574 | struct pci_dev *tmp, *parent; |
| 1575 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1576 | ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus), |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1577 | pdev->bus->number, pdev->devfn, |
| 1578 | translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1579 | if (ret) |
| 1580 | return ret; |
| 1581 | |
| 1582 | /* dependent device mapping */ |
| 1583 | tmp = pci_find_upstream_pcie_bridge(pdev); |
| 1584 | if (!tmp) |
| 1585 | return 0; |
| 1586 | /* Secondary interface's bus number and devfn 0 */ |
| 1587 | parent = pdev->bus->self; |
| 1588 | while (parent != tmp) { |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1589 | ret = domain_context_mapping_one(domain, |
| 1590 | pci_domain_nr(parent->bus), |
| 1591 | parent->bus->number, |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1592 | parent->devfn, translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1593 | if (ret) |
| 1594 | return ret; |
| 1595 | parent = parent->bus->self; |
| 1596 | } |
| 1597 | if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */ |
| 1598 | return domain_context_mapping_one(domain, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1599 | pci_domain_nr(tmp->subordinate), |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1600 | tmp->subordinate->number, 0, |
| 1601 | translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1602 | else /* this is a legacy PCI bridge */ |
| 1603 | return domain_context_mapping_one(domain, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1604 | pci_domain_nr(tmp->bus), |
| 1605 | tmp->bus->number, |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1606 | tmp->devfn, |
| 1607 | translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1610 | static int domain_context_mapped(struct pci_dev *pdev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1611 | { |
| 1612 | int ret; |
| 1613 | struct pci_dev *tmp, *parent; |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1614 | struct intel_iommu *iommu; |
| 1615 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1616 | iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number, |
| 1617 | pdev->devfn); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1618 | if (!iommu) |
| 1619 | return -ENODEV; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1620 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1621 | ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1622 | if (!ret) |
| 1623 | return ret; |
| 1624 | /* dependent device mapping */ |
| 1625 | tmp = pci_find_upstream_pcie_bridge(pdev); |
| 1626 | if (!tmp) |
| 1627 | return ret; |
| 1628 | /* Secondary interface's bus number and devfn 0 */ |
| 1629 | parent = pdev->bus->self; |
| 1630 | while (parent != tmp) { |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1631 | ret = device_context_mapped(iommu, parent->bus->number, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1632 | parent->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1633 | if (!ret) |
| 1634 | return ret; |
| 1635 | parent = parent->bus->self; |
| 1636 | } |
| 1637 | if (tmp->is_pcie) |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1638 | return device_context_mapped(iommu, tmp->subordinate->number, |
| 1639 | 0); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1640 | else |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1641 | return device_context_mapped(iommu, tmp->bus->number, |
| 1642 | tmp->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1645 | static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 1646 | struct scatterlist *sg, unsigned long phys_pfn, |
| 1647 | unsigned long nr_pages, int prot) |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1648 | { |
| 1649 | struct dma_pte *first_pte = NULL, *pte = NULL; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1650 | phys_addr_t uninitialized_var(pteval); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1651 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1652 | unsigned long sg_res; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1653 | |
| 1654 | BUG_ON(addr_width < BITS_PER_LONG && (iov_pfn + nr_pages - 1) >> addr_width); |
| 1655 | |
| 1656 | if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) |
| 1657 | return -EINVAL; |
| 1658 | |
| 1659 | prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP; |
| 1660 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1661 | if (sg) |
| 1662 | sg_res = 0; |
| 1663 | else { |
| 1664 | sg_res = nr_pages + 1; |
| 1665 | pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot; |
| 1666 | } |
| 1667 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1668 | while (nr_pages--) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 1669 | uint64_t tmp; |
| 1670 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1671 | if (!sg_res) { |
| 1672 | sg_res = (sg->offset + sg->length + VTD_PAGE_SIZE - 1) >> VTD_PAGE_SHIFT; |
| 1673 | sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset; |
| 1674 | sg->dma_length = sg->length; |
| 1675 | pteval = page_to_phys(sg_page(sg)) | prot; |
| 1676 | } |
| 1677 | if (!pte) { |
| 1678 | first_pte = pte = pfn_to_dma_pte(domain, iov_pfn); |
| 1679 | if (!pte) |
| 1680 | return -ENOMEM; |
| 1681 | } |
| 1682 | /* We don't need lock here, nobody else |
| 1683 | * touches the iova range |
| 1684 | */ |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 1685 | tmp = cmpxchg64(&pte->val, 0ULL, pteval); |
| 1686 | if (tmp) { |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 1687 | static int dumps = 5; |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 1688 | printk(KERN_CRIT "ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n", |
| 1689 | iov_pfn, tmp, (unsigned long long)pteval); |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 1690 | if (dumps) { |
| 1691 | dumps--; |
| 1692 | debug_dma_dump_mappings(NULL); |
| 1693 | } |
| 1694 | WARN_ON(1); |
| 1695 | } |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1696 | pte++; |
| 1697 | if (!nr_pages || |
| 1698 | (unsigned long)pte >> VTD_PAGE_SHIFT != |
| 1699 | (unsigned long)first_pte >> VTD_PAGE_SHIFT) { |
| 1700 | domain_flush_cache(domain, first_pte, |
| 1701 | (void *)pte - (void *)first_pte); |
| 1702 | pte = NULL; |
| 1703 | } |
| 1704 | iov_pfn++; |
| 1705 | pteval += VTD_PAGE_SIZE; |
| 1706 | sg_res--; |
| 1707 | if (!sg_res) |
| 1708 | sg = sg_next(sg); |
| 1709 | } |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1713 | static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 1714 | struct scatterlist *sg, unsigned long nr_pages, |
| 1715 | int prot) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1716 | { |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1717 | return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot); |
| 1718 | } |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1719 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1720 | static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 1721 | unsigned long phys_pfn, unsigned long nr_pages, |
| 1722 | int prot) |
| 1723 | { |
| 1724 | return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1727 | static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1728 | { |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1729 | if (!iommu) |
| 1730 | return; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1731 | |
| 1732 | clear_context_table(iommu, bus, devfn); |
| 1733 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1734 | DMA_CCMD_GLOBAL_INVL); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1735 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | static void domain_remove_dev_info(struct dmar_domain *domain) |
| 1739 | { |
| 1740 | struct device_domain_info *info; |
| 1741 | unsigned long flags; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1742 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1743 | |
| 1744 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1745 | while (!list_empty(&domain->devices)) { |
| 1746 | info = list_entry(domain->devices.next, |
| 1747 | struct device_domain_info, link); |
| 1748 | list_del(&info->link); |
| 1749 | list_del(&info->global); |
| 1750 | if (info->dev) |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1751 | info->dev->dev.archdata.iommu = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1752 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1753 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1754 | iommu_disable_dev_iotlb(info); |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1755 | iommu = device_to_iommu(info->segment, info->bus, info->devfn); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1756 | iommu_detach_dev(iommu, info->bus, info->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1757 | free_devinfo_mem(info); |
| 1758 | |
| 1759 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1760 | } |
| 1761 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | * find_domain |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1766 | * Note: we use struct pci_dev->dev.archdata.iommu stores the info |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1767 | */ |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 1768 | static struct dmar_domain * |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1769 | find_domain(struct pci_dev *pdev) |
| 1770 | { |
| 1771 | struct device_domain_info *info; |
| 1772 | |
| 1773 | /* No lock here, assumes no domain exit in normal case */ |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1774 | info = pdev->dev.archdata.iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1775 | if (info) |
| 1776 | return info->domain; |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1780 | /* domain is initialized */ |
| 1781 | static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw) |
| 1782 | { |
| 1783 | struct dmar_domain *domain, *found = NULL; |
| 1784 | struct intel_iommu *iommu; |
| 1785 | struct dmar_drhd_unit *drhd; |
| 1786 | struct device_domain_info *info, *tmp; |
| 1787 | struct pci_dev *dev_tmp; |
| 1788 | unsigned long flags; |
| 1789 | int bus = 0, devfn = 0; |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1790 | int segment; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1791 | int ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1792 | |
| 1793 | domain = find_domain(pdev); |
| 1794 | if (domain) |
| 1795 | return domain; |
| 1796 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1797 | segment = pci_domain_nr(pdev->bus); |
| 1798 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1799 | dev_tmp = pci_find_upstream_pcie_bridge(pdev); |
| 1800 | if (dev_tmp) { |
| 1801 | if (dev_tmp->is_pcie) { |
| 1802 | bus = dev_tmp->subordinate->number; |
| 1803 | devfn = 0; |
| 1804 | } else { |
| 1805 | bus = dev_tmp->bus->number; |
| 1806 | devfn = dev_tmp->devfn; |
| 1807 | } |
| 1808 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1809 | list_for_each_entry(info, &device_domain_list, global) { |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1810 | if (info->segment == segment && |
| 1811 | info->bus == bus && info->devfn == devfn) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1812 | found = info->domain; |
| 1813 | break; |
| 1814 | } |
| 1815 | } |
| 1816 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1817 | /* pcie-pci bridge already has a domain, uses it */ |
| 1818 | if (found) { |
| 1819 | domain = found; |
| 1820 | goto found_domain; |
| 1821 | } |
| 1822 | } |
| 1823 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1824 | domain = alloc_domain(); |
| 1825 | if (!domain) |
| 1826 | goto error; |
| 1827 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1828 | /* Allocate new domain for the device */ |
| 1829 | drhd = dmar_find_matched_drhd_unit(pdev); |
| 1830 | if (!drhd) { |
| 1831 | printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n", |
| 1832 | pci_name(pdev)); |
| 1833 | return NULL; |
| 1834 | } |
| 1835 | iommu = drhd->iommu; |
| 1836 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1837 | ret = iommu_attach_domain(domain, iommu); |
| 1838 | if (ret) { |
| 1839 | domain_exit(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1840 | goto error; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1841 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1842 | |
| 1843 | if (domain_init(domain, gaw)) { |
| 1844 | domain_exit(domain); |
| 1845 | goto error; |
| 1846 | } |
| 1847 | |
| 1848 | /* register pcie-to-pci device */ |
| 1849 | if (dev_tmp) { |
| 1850 | info = alloc_devinfo_mem(); |
| 1851 | if (!info) { |
| 1852 | domain_exit(domain); |
| 1853 | goto error; |
| 1854 | } |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1855 | info->segment = segment; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1856 | info->bus = bus; |
| 1857 | info->devfn = devfn; |
| 1858 | info->dev = NULL; |
| 1859 | info->domain = domain; |
| 1860 | /* This domain is shared by devices under p2p bridge */ |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 1861 | domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1862 | |
| 1863 | /* pcie-to-pci bridge already has a domain, uses it */ |
| 1864 | found = NULL; |
| 1865 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1866 | list_for_each_entry(tmp, &device_domain_list, global) { |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1867 | if (tmp->segment == segment && |
| 1868 | tmp->bus == bus && tmp->devfn == devfn) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1869 | found = tmp->domain; |
| 1870 | break; |
| 1871 | } |
| 1872 | } |
| 1873 | if (found) { |
| 1874 | free_devinfo_mem(info); |
| 1875 | domain_exit(domain); |
| 1876 | domain = found; |
| 1877 | } else { |
| 1878 | list_add(&info->link, &domain->devices); |
| 1879 | list_add(&info->global, &device_domain_list); |
| 1880 | } |
| 1881 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1882 | } |
| 1883 | |
| 1884 | found_domain: |
| 1885 | info = alloc_devinfo_mem(); |
| 1886 | if (!info) |
| 1887 | goto error; |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1888 | info->segment = segment; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1889 | info->bus = pdev->bus->number; |
| 1890 | info->devfn = pdev->devfn; |
| 1891 | info->dev = pdev; |
| 1892 | info->domain = domain; |
| 1893 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1894 | /* somebody is fast */ |
| 1895 | found = find_domain(pdev); |
| 1896 | if (found != NULL) { |
| 1897 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1898 | if (found != domain) { |
| 1899 | domain_exit(domain); |
| 1900 | domain = found; |
| 1901 | } |
| 1902 | free_devinfo_mem(info); |
| 1903 | return domain; |
| 1904 | } |
| 1905 | list_add(&info->link, &domain->devices); |
| 1906 | list_add(&info->global, &device_domain_list); |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1907 | pdev->dev.archdata.iommu = info; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1908 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1909 | return domain; |
| 1910 | error: |
| 1911 | /* recheck it here, maybe others set it */ |
| 1912 | return find_domain(pdev); |
| 1913 | } |
| 1914 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1915 | static int iommu_identity_mapping; |
| 1916 | |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 1917 | static int iommu_domain_identity_map(struct dmar_domain *domain, |
| 1918 | unsigned long long start, |
| 1919 | unsigned long long end) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1920 | { |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 1921 | unsigned long first_vpfn = start >> VTD_PAGE_SHIFT; |
| 1922 | unsigned long last_vpfn = end >> VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1923 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 1924 | if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn), |
| 1925 | dma_to_mm_pfn(last_vpfn))) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1926 | printk(KERN_ERR "IOMMU: reserve iova failed\n"); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 1927 | return -ENOMEM; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1928 | } |
| 1929 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 1930 | pr_debug("Mapping reserved region %llx-%llx for domain %d\n", |
| 1931 | start, end, domain->id); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1932 | /* |
| 1933 | * RMRR range might have overlap with physical memory range, |
| 1934 | * clear it first |
| 1935 | */ |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 1936 | dma_pte_clear_range(domain, first_vpfn, last_vpfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1937 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 1938 | return domain_pfn_mapping(domain, first_vpfn, first_vpfn, |
| 1939 | last_vpfn - first_vpfn + 1, |
David Woodhouse | 61df744 | 2009-06-28 11:55:58 +0100 | [diff] [blame] | 1940 | DMA_PTE_READ|DMA_PTE_WRITE); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | static int iommu_prepare_identity_map(struct pci_dev *pdev, |
| 1944 | unsigned long long start, |
| 1945 | unsigned long long end) |
| 1946 | { |
| 1947 | struct dmar_domain *domain; |
| 1948 | int ret; |
| 1949 | |
| 1950 | printk(KERN_INFO |
| 1951 | "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n", |
| 1952 | pci_name(pdev), start, end); |
| 1953 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 1954 | domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 1955 | if (!domain) |
| 1956 | return -ENOMEM; |
| 1957 | |
| 1958 | ret = iommu_domain_identity_map(domain, start, end); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1959 | if (ret) |
| 1960 | goto error; |
| 1961 | |
| 1962 | /* context entry init */ |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1963 | ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 1964 | if (ret) |
| 1965 | goto error; |
| 1966 | |
| 1967 | return 0; |
| 1968 | |
| 1969 | error: |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1970 | domain_exit(domain); |
| 1971 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr, |
| 1975 | struct pci_dev *pdev) |
| 1976 | { |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1977 | if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1978 | return 0; |
| 1979 | return iommu_prepare_identity_map(pdev, rmrr->base_address, |
| 1980 | rmrr->end_address + 1); |
| 1981 | } |
| 1982 | |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 1983 | #ifdef CONFIG_DMAR_FLOPPY_WA |
| 1984 | static inline void iommu_prepare_isa(void) |
| 1985 | { |
| 1986 | struct pci_dev *pdev; |
| 1987 | int ret; |
| 1988 | |
| 1989 | pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL); |
| 1990 | if (!pdev) |
| 1991 | return; |
| 1992 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 1993 | printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n"); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 1994 | ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024); |
| 1995 | |
| 1996 | if (ret) |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 1997 | printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; " |
| 1998 | "floppy might not work\n"); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 1999 | |
| 2000 | } |
| 2001 | #else |
| 2002 | static inline void iommu_prepare_isa(void) |
| 2003 | { |
| 2004 | return; |
| 2005 | } |
| 2006 | #endif /* !CONFIG_DMAR_FLPY_WA */ |
| 2007 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2008 | /* Initialize each context entry as pass through.*/ |
| 2009 | static int __init init_context_pass_through(void) |
| 2010 | { |
| 2011 | struct pci_dev *pdev = NULL; |
| 2012 | struct dmar_domain *domain; |
| 2013 | int ret; |
| 2014 | |
| 2015 | for_each_pci_dev(pdev) { |
| 2016 | domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 2017 | ret = domain_context_mapping(domain, pdev, |
| 2018 | CONTEXT_TT_PASS_THROUGH); |
| 2019 | if (ret) |
| 2020 | return ret; |
| 2021 | } |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2025 | static int md_domain_init(struct dmar_domain *domain, int guest_width); |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2026 | |
| 2027 | static int __init si_domain_work_fn(unsigned long start_pfn, |
| 2028 | unsigned long end_pfn, void *datax) |
| 2029 | { |
| 2030 | int *ret = datax; |
| 2031 | |
| 2032 | *ret = iommu_domain_identity_map(si_domain, |
| 2033 | (uint64_t)start_pfn << PAGE_SHIFT, |
| 2034 | (uint64_t)end_pfn << PAGE_SHIFT); |
| 2035 | return *ret; |
| 2036 | |
| 2037 | } |
| 2038 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2039 | static int si_domain_init(void) |
| 2040 | { |
| 2041 | struct dmar_drhd_unit *drhd; |
| 2042 | struct intel_iommu *iommu; |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2043 | int nid, ret = 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2044 | |
| 2045 | si_domain = alloc_domain(); |
| 2046 | if (!si_domain) |
| 2047 | return -EFAULT; |
| 2048 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2049 | pr_debug("Identity mapping domain is domain %d\n", si_domain->id); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2050 | |
| 2051 | for_each_active_iommu(iommu, drhd) { |
| 2052 | ret = iommu_attach_domain(si_domain, iommu); |
| 2053 | if (ret) { |
| 2054 | domain_exit(si_domain); |
| 2055 | return -EFAULT; |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
| 2060 | domain_exit(si_domain); |
| 2061 | return -EFAULT; |
| 2062 | } |
| 2063 | |
| 2064 | si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY; |
| 2065 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2066 | for_each_online_node(nid) { |
| 2067 | work_with_active_regions(nid, si_domain_work_fn, &ret); |
| 2068 | if (ret) |
| 2069 | return ret; |
| 2070 | } |
| 2071 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | static void domain_remove_one_dev_info(struct dmar_domain *domain, |
| 2076 | struct pci_dev *pdev); |
| 2077 | static int identity_mapping(struct pci_dev *pdev) |
| 2078 | { |
| 2079 | struct device_domain_info *info; |
| 2080 | |
| 2081 | if (likely(!iommu_identity_mapping)) |
| 2082 | return 0; |
| 2083 | |
| 2084 | |
| 2085 | list_for_each_entry(info, &si_domain->devices, link) |
| 2086 | if (info->dev == pdev) |
| 2087 | return 1; |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | static int domain_add_dev_info(struct dmar_domain *domain, |
| 2092 | struct pci_dev *pdev) |
| 2093 | { |
| 2094 | struct device_domain_info *info; |
| 2095 | unsigned long flags; |
| 2096 | |
| 2097 | info = alloc_devinfo_mem(); |
| 2098 | if (!info) |
| 2099 | return -ENOMEM; |
| 2100 | |
| 2101 | info->segment = pci_domain_nr(pdev->bus); |
| 2102 | info->bus = pdev->bus->number; |
| 2103 | info->devfn = pdev->devfn; |
| 2104 | info->dev = pdev; |
| 2105 | info->domain = domain; |
| 2106 | |
| 2107 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2108 | list_add(&info->link, &domain->devices); |
| 2109 | list_add(&info->global, &device_domain_list); |
| 2110 | pdev->dev.archdata.iommu = info; |
| 2111 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2112 | |
| 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | static int iommu_prepare_static_identity_mapping(void) |
| 2117 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2118 | struct pci_dev *pdev = NULL; |
| 2119 | int ret; |
| 2120 | |
| 2121 | ret = si_domain_init(); |
| 2122 | if (ret) |
| 2123 | return -EFAULT; |
| 2124 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2125 | for_each_pci_dev(pdev) { |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2126 | printk(KERN_INFO "IOMMU: identity mapping for device %s\n", |
| 2127 | pci_name(pdev)); |
| 2128 | |
| 2129 | ret = domain_context_mapping(si_domain, pdev, |
| 2130 | CONTEXT_TT_MULTI_LEVEL); |
| 2131 | if (ret) |
| 2132 | return ret; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2133 | ret = domain_add_dev_info(si_domain, pdev); |
| 2134 | if (ret) |
| 2135 | return ret; |
| 2136 | } |
| 2137 | |
| 2138 | return 0; |
| 2139 | } |
| 2140 | |
| 2141 | int __init init_dmars(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2142 | { |
| 2143 | struct dmar_drhd_unit *drhd; |
| 2144 | struct dmar_rmrr_unit *rmrr; |
| 2145 | struct pci_dev *pdev; |
| 2146 | struct intel_iommu *iommu; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 2147 | int i, ret; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2148 | int pass_through = 1; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2149 | |
| 2150 | /* |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2151 | * In case pass through can not be enabled, iommu tries to use identity |
| 2152 | * mapping. |
| 2153 | */ |
| 2154 | if (iommu_pass_through) |
| 2155 | iommu_identity_mapping = 1; |
| 2156 | |
| 2157 | /* |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2158 | * for each drhd |
| 2159 | * allocate root |
| 2160 | * initialize and program root entry to not present |
| 2161 | * endfor |
| 2162 | */ |
| 2163 | for_each_drhd_unit(drhd) { |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2164 | g_num_of_iommus++; |
| 2165 | /* |
| 2166 | * lock not needed as this is only incremented in the single |
| 2167 | * threaded kernel __init code path all other access are read |
| 2168 | * only |
| 2169 | */ |
| 2170 | } |
| 2171 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 2172 | g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *), |
| 2173 | GFP_KERNEL); |
| 2174 | if (!g_iommus) { |
| 2175 | printk(KERN_ERR "Allocating global iommu array failed\n"); |
| 2176 | ret = -ENOMEM; |
| 2177 | goto error; |
| 2178 | } |
| 2179 | |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2180 | deferred_flush = kzalloc(g_num_of_iommus * |
| 2181 | sizeof(struct deferred_flush_tables), GFP_KERNEL); |
| 2182 | if (!deferred_flush) { |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 2183 | kfree(g_iommus); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2184 | ret = -ENOMEM; |
| 2185 | goto error; |
| 2186 | } |
| 2187 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2188 | for_each_drhd_unit(drhd) { |
| 2189 | if (drhd->ignored) |
| 2190 | continue; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 2191 | |
| 2192 | iommu = drhd->iommu; |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 2193 | g_iommus[iommu->seq_id] = iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2194 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 2195 | ret = iommu_init_domains(iommu); |
| 2196 | if (ret) |
| 2197 | goto error; |
| 2198 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2199 | /* |
| 2200 | * TBD: |
| 2201 | * we could share the same root & context tables |
| 2202 | * amoung all IOMMU's. Need to Split it later. |
| 2203 | */ |
| 2204 | ret = iommu_alloc_root_entry(iommu); |
| 2205 | if (ret) { |
| 2206 | printk(KERN_ERR "IOMMU: allocate root entry failed\n"); |
| 2207 | goto error; |
| 2208 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2209 | if (!ecap_pass_through(iommu->ecap)) |
| 2210 | pass_through = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2211 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2212 | if (iommu_pass_through) |
| 2213 | if (!pass_through) { |
| 2214 | printk(KERN_INFO |
| 2215 | "Pass Through is not supported by hardware.\n"); |
| 2216 | iommu_pass_through = 0; |
| 2217 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2218 | |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 2219 | /* |
| 2220 | * Start from the sane iommu hardware state. |
| 2221 | */ |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2222 | for_each_drhd_unit(drhd) { |
| 2223 | if (drhd->ignored) |
| 2224 | continue; |
| 2225 | |
| 2226 | iommu = drhd->iommu; |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 2227 | |
| 2228 | /* |
| 2229 | * If the queued invalidation is already initialized by us |
| 2230 | * (for example, while enabling interrupt-remapping) then |
| 2231 | * we got the things already rolling from a sane state. |
| 2232 | */ |
| 2233 | if (iommu->qi) |
| 2234 | continue; |
| 2235 | |
| 2236 | /* |
| 2237 | * Clear any previous faults. |
| 2238 | */ |
| 2239 | dmar_fault(-1, iommu); |
| 2240 | /* |
| 2241 | * Disable queued invalidation if supported and already enabled |
| 2242 | * before OS handover. |
| 2243 | */ |
| 2244 | dmar_disable_qi(iommu); |
| 2245 | } |
| 2246 | |
| 2247 | for_each_drhd_unit(drhd) { |
| 2248 | if (drhd->ignored) |
| 2249 | continue; |
| 2250 | |
| 2251 | iommu = drhd->iommu; |
| 2252 | |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2253 | if (dmar_enable_qi(iommu)) { |
| 2254 | /* |
| 2255 | * Queued Invalidate not enabled, use Register Based |
| 2256 | * Invalidate |
| 2257 | */ |
| 2258 | iommu->flush.flush_context = __iommu_flush_context; |
| 2259 | iommu->flush.flush_iotlb = __iommu_flush_iotlb; |
| 2260 | printk(KERN_INFO "IOMMU 0x%Lx: using Register based " |
FUJITA Tomonori | b4e0f9e | 2008-11-19 13:53:42 +0900 | [diff] [blame] | 2261 | "invalidation\n", |
| 2262 | (unsigned long long)drhd->reg_base_addr); |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2263 | } else { |
| 2264 | iommu->flush.flush_context = qi_flush_context; |
| 2265 | iommu->flush.flush_iotlb = qi_flush_iotlb; |
| 2266 | printk(KERN_INFO "IOMMU 0x%Lx: using Queued " |
FUJITA Tomonori | b4e0f9e | 2008-11-19 13:53:42 +0900 | [diff] [blame] | 2267 | "invalidation\n", |
| 2268 | (unsigned long long)drhd->reg_base_addr); |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2272 | /* |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2273 | * If pass through is set and enabled, context entries of all pci |
| 2274 | * devices are intialized by pass through translation type. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2275 | */ |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2276 | if (iommu_pass_through) { |
| 2277 | ret = init_context_pass_through(); |
| 2278 | if (ret) { |
| 2279 | printk(KERN_ERR "IOMMU: Pass through init failed.\n"); |
| 2280 | iommu_pass_through = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2281 | } |
| 2282 | } |
| 2283 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2284 | /* |
| 2285 | * If pass through is not set or not enabled, setup context entries for |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2286 | * identity mappings for rmrr, gfx, and isa and may fall back to static |
| 2287 | * identity mapping if iommu_identity_mapping is set. |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2288 | */ |
| 2289 | if (!iommu_pass_through) { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2290 | if (iommu_identity_mapping) |
| 2291 | iommu_prepare_static_identity_mapping(); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2292 | /* |
| 2293 | * For each rmrr |
| 2294 | * for each dev attached to rmrr |
| 2295 | * do |
| 2296 | * locate drhd for dev, alloc domain for dev |
| 2297 | * allocate free domain |
| 2298 | * allocate page table entries for rmrr |
| 2299 | * if context not allocated for bus |
| 2300 | * allocate and init context |
| 2301 | * set present in root table for this bus |
| 2302 | * init context with domain, translation etc |
| 2303 | * endfor |
| 2304 | * endfor |
| 2305 | */ |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2306 | printk(KERN_INFO "IOMMU: Setting RMRR:\n"); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2307 | for_each_rmrr_units(rmrr) { |
| 2308 | for (i = 0; i < rmrr->devices_cnt; i++) { |
| 2309 | pdev = rmrr->devices[i]; |
| 2310 | /* |
| 2311 | * some BIOS lists non-exist devices in DMAR |
| 2312 | * table. |
| 2313 | */ |
| 2314 | if (!pdev) |
| 2315 | continue; |
| 2316 | ret = iommu_prepare_rmrr_dev(rmrr, pdev); |
| 2317 | if (ret) |
| 2318 | printk(KERN_ERR |
| 2319 | "IOMMU: mapping reserved region failed\n"); |
| 2320 | } |
| 2321 | } |
Keshavamurthy, Anil S | e820482 | 2007-10-21 16:41:55 -0700 | [diff] [blame] | 2322 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2323 | iommu_prepare_isa(); |
| 2324 | } |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2325 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2326 | /* |
| 2327 | * for each drhd |
| 2328 | * enable fault log |
| 2329 | * global invalidate context cache |
| 2330 | * global invalidate iotlb |
| 2331 | * enable translation |
| 2332 | */ |
| 2333 | for_each_drhd_unit(drhd) { |
| 2334 | if (drhd->ignored) |
| 2335 | continue; |
| 2336 | iommu = drhd->iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2337 | |
| 2338 | iommu_flush_write_buffer(iommu); |
| 2339 | |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 2340 | ret = dmar_set_interrupt(iommu); |
| 2341 | if (ret) |
| 2342 | goto error; |
| 2343 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2344 | iommu_set_root_entry(iommu); |
| 2345 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2346 | iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2347 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 2348 | iommu_disable_protect_mem_regions(iommu); |
| 2349 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2350 | ret = iommu_enable_translation(iommu); |
| 2351 | if (ret) |
| 2352 | goto error; |
| 2353 | } |
| 2354 | |
| 2355 | return 0; |
| 2356 | error: |
| 2357 | for_each_drhd_unit(drhd) { |
| 2358 | if (drhd->ignored) |
| 2359 | continue; |
| 2360 | iommu = drhd->iommu; |
| 2361 | free_iommu(iommu); |
| 2362 | } |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 2363 | kfree(g_iommus); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2364 | return ret; |
| 2365 | } |
| 2366 | |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 2367 | static inline unsigned long aligned_nrpages(unsigned long host_addr, |
| 2368 | size_t size) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2369 | { |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 2370 | host_addr &= ~PAGE_MASK; |
| 2371 | host_addr += size + PAGE_SIZE - 1; |
| 2372 | |
| 2373 | return host_addr >> VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2376 | static struct iova *intel_alloc_iova(struct device *dev, |
| 2377 | struct dmar_domain *domain, |
| 2378 | unsigned long nrpages, uint64_t dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2379 | { |
| 2380 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2381 | struct iova *iova = NULL; |
| 2382 | |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2383 | /* Restrict dma_mask to the width that the iommu can handle */ |
| 2384 | dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask); |
| 2385 | |
| 2386 | if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2387 | /* |
| 2388 | * First try to allocate an io virtual address in |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 2389 | * DMA_BIT_MASK(32) and if that fails then try allocating |
Joe Perches | 3609801 | 2007-12-17 11:40:11 -0800 | [diff] [blame] | 2390 | * from higher range |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2391 | */ |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2392 | iova = alloc_iova(&domain->iovad, nrpages, |
| 2393 | IOVA_PFN(DMA_BIT_MASK(32)), 1); |
| 2394 | if (iova) |
| 2395 | return iova; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2396 | } |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2397 | iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1); |
| 2398 | if (unlikely(!iova)) { |
| 2399 | printk(KERN_ERR "Allocating %ld-page iova for %s failed", |
| 2400 | nrpages, pci_name(pdev)); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2401 | return NULL; |
| 2402 | } |
| 2403 | |
| 2404 | return iova; |
| 2405 | } |
| 2406 | |
| 2407 | static struct dmar_domain * |
| 2408 | get_valid_domain_for_dev(struct pci_dev *pdev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2409 | { |
| 2410 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2411 | int ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2412 | |
| 2413 | domain = get_domain_for_dev(pdev, |
| 2414 | DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 2415 | if (!domain) { |
| 2416 | printk(KERN_ERR |
| 2417 | "Allocating domain for %s failed", pci_name(pdev)); |
Al Viro | 4fe05bb | 2007-10-29 04:51:16 +0000 | [diff] [blame] | 2418 | return NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2419 | } |
| 2420 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2421 | /* make sure context mapping is ok */ |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2422 | if (unlikely(!domain_context_mapped(pdev))) { |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2423 | ret = domain_context_mapping(domain, pdev, |
| 2424 | CONTEXT_TT_MULTI_LEVEL); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2425 | if (ret) { |
| 2426 | printk(KERN_ERR |
| 2427 | "Domain context map for %s failed", |
| 2428 | pci_name(pdev)); |
Al Viro | 4fe05bb | 2007-10-29 04:51:16 +0000 | [diff] [blame] | 2429 | return NULL; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2430 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2433 | return domain; |
| 2434 | } |
| 2435 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2436 | static int iommu_dummy(struct pci_dev *pdev) |
| 2437 | { |
| 2438 | return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO; |
| 2439 | } |
| 2440 | |
| 2441 | /* Check if the pdev needs to go through non-identity map and unmap process.*/ |
| 2442 | static int iommu_no_mapping(struct pci_dev *pdev) |
| 2443 | { |
| 2444 | int found; |
| 2445 | |
| 2446 | if (!iommu_identity_mapping) |
| 2447 | return iommu_dummy(pdev); |
| 2448 | |
| 2449 | found = identity_mapping(pdev); |
| 2450 | if (found) { |
| 2451 | if (pdev->dma_mask > DMA_BIT_MASK(32)) |
| 2452 | return 1; |
| 2453 | else { |
| 2454 | /* |
| 2455 | * 32 bit DMA is removed from si_domain and fall back |
| 2456 | * to non-identity mapping. |
| 2457 | */ |
| 2458 | domain_remove_one_dev_info(si_domain, pdev); |
| 2459 | printk(KERN_INFO "32bit %s uses non-identity mapping\n", |
| 2460 | pci_name(pdev)); |
| 2461 | return 0; |
| 2462 | } |
| 2463 | } else { |
| 2464 | /* |
| 2465 | * In case of a detached 64 bit DMA device from vm, the device |
| 2466 | * is put into si_domain for identity mapping. |
| 2467 | */ |
| 2468 | if (pdev->dma_mask > DMA_BIT_MASK(32)) { |
| 2469 | int ret; |
| 2470 | ret = domain_add_dev_info(si_domain, pdev); |
| 2471 | if (!ret) { |
| 2472 | printk(KERN_INFO "64bit %s uses identity mapping\n", |
| 2473 | pci_name(pdev)); |
| 2474 | return 1; |
| 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | return iommu_dummy(pdev); |
| 2480 | } |
| 2481 | |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2482 | static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr, |
| 2483 | size_t size, int dir, u64 dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2484 | { |
| 2485 | struct pci_dev *pdev = to_pci_dev(hwdev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2486 | struct dmar_domain *domain; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2487 | phys_addr_t start_paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2488 | struct iova *iova; |
| 2489 | int prot = 0; |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2490 | int ret; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2491 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2492 | |
| 2493 | BUG_ON(dir == DMA_NONE); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2494 | |
| 2495 | if (iommu_no_mapping(pdev)) |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2496 | return paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2497 | |
| 2498 | domain = get_valid_domain_for_dev(pdev); |
| 2499 | if (!domain) |
| 2500 | return 0; |
| 2501 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2502 | iommu = domain_get_iommu(domain); |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 2503 | size = aligned_nrpages(paddr, size); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2504 | |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2505 | iova = intel_alloc_iova(hwdev, domain, size, pdev->dma_mask); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2506 | if (!iova) |
| 2507 | goto error; |
| 2508 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2509 | /* |
| 2510 | * Check if DMAR supports zero-length reads on write only |
| 2511 | * mappings.. |
| 2512 | */ |
| 2513 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2514 | !cap_zlr(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2515 | prot |= DMA_PTE_READ; |
| 2516 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 2517 | prot |= DMA_PTE_WRITE; |
| 2518 | /* |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2519 | * paddr - (paddr + size) might be partial page, we should map the whole |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2520 | * page. Note: if two part of one page are separately mapped, we |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2521 | * might have two guest_addr mapping to the same host paddr, but this |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2522 | * is not a big problem |
| 2523 | */ |
David Woodhouse | 0ab36de | 2009-06-28 14:01:43 +0100 | [diff] [blame] | 2524 | ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo), |
| 2525 | paddr >> VTD_PAGE_SHIFT, size, prot); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2526 | if (ret) |
| 2527 | goto error; |
| 2528 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2529 | /* it's a non-present to present mapping. Only flush if caching mode */ |
| 2530 | if (cap_caching_mode(iommu->cap)) |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 2531 | iommu_flush_iotlb_psi(iommu, 0, mm_to_dma_pfn(iova->pfn_lo), size); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2532 | else |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2533 | iommu_flush_write_buffer(iommu); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2534 | |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 2535 | start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT; |
| 2536 | start_paddr += paddr & ~PAGE_MASK; |
| 2537 | return start_paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2538 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2539 | error: |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2540 | if (iova) |
| 2541 | __free_iova(&domain->iovad, iova); |
David Woodhouse | 4cf2e75 | 2009-02-11 17:23:43 +0000 | [diff] [blame] | 2542 | printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2543 | pci_name(pdev), size, (unsigned long long)paddr, dir); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2544 | return 0; |
| 2545 | } |
| 2546 | |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2547 | static dma_addr_t intel_map_page(struct device *dev, struct page *page, |
| 2548 | unsigned long offset, size_t size, |
| 2549 | enum dma_data_direction dir, |
| 2550 | struct dma_attrs *attrs) |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2551 | { |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2552 | return __intel_map_single(dev, page_to_phys(page) + offset, size, |
| 2553 | dir, to_pci_dev(dev)->dma_mask); |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2554 | } |
| 2555 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2556 | static void flush_unmaps(void) |
| 2557 | { |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2558 | int i, j; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2559 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2560 | timer_on = 0; |
| 2561 | |
| 2562 | /* just flush them all */ |
| 2563 | for (i = 0; i < g_num_of_iommus; i++) { |
Weidong Han | a2bb845 | 2008-12-08 11:24:12 +0800 | [diff] [blame] | 2564 | struct intel_iommu *iommu = g_iommus[i]; |
| 2565 | if (!iommu) |
| 2566 | continue; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 2567 | |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 2568 | if (!deferred_flush[i].next) |
| 2569 | continue; |
| 2570 | |
| 2571 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2572 | DMA_TLB_GLOBAL_FLUSH); |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 2573 | for (j = 0; j < deferred_flush[i].next; j++) { |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2574 | unsigned long mask; |
| 2575 | struct iova *iova = deferred_flush[i].iova[j]; |
| 2576 | |
| 2577 | mask = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT; |
| 2578 | mask = ilog2(mask >> VTD_PAGE_SHIFT); |
| 2579 | iommu_flush_dev_iotlb(deferred_flush[i].domain[j], |
| 2580 | iova->pfn_lo << PAGE_SHIFT, mask); |
| 2581 | __free_iova(&deferred_flush[i].domain[j]->iovad, iova); |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2582 | } |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 2583 | deferred_flush[i].next = 0; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2584 | } |
| 2585 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2586 | list_size = 0; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | static void flush_unmaps_timeout(unsigned long data) |
| 2590 | { |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2591 | unsigned long flags; |
| 2592 | |
| 2593 | spin_lock_irqsave(&async_umap_flush_lock, flags); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2594 | flush_unmaps(); |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2595 | spin_unlock_irqrestore(&async_umap_flush_lock, flags); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | static void add_unmap(struct dmar_domain *dom, struct iova *iova) |
| 2599 | { |
| 2600 | unsigned long flags; |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2601 | int next, iommu_id; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2602 | struct intel_iommu *iommu; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2603 | |
| 2604 | spin_lock_irqsave(&async_umap_flush_lock, flags); |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2605 | if (list_size == HIGH_WATER_MARK) |
| 2606 | flush_unmaps(); |
| 2607 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2608 | iommu = domain_get_iommu(dom); |
| 2609 | iommu_id = iommu->seq_id; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 2610 | |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2611 | next = deferred_flush[iommu_id].next; |
| 2612 | deferred_flush[iommu_id].domain[next] = dom; |
| 2613 | deferred_flush[iommu_id].iova[next] = iova; |
| 2614 | deferred_flush[iommu_id].next++; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2615 | |
| 2616 | if (!timer_on) { |
| 2617 | mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10)); |
| 2618 | timer_on = 1; |
| 2619 | } |
| 2620 | list_size++; |
| 2621 | spin_unlock_irqrestore(&async_umap_flush_lock, flags); |
| 2622 | } |
| 2623 | |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2624 | static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr, |
| 2625 | size_t size, enum dma_data_direction dir, |
| 2626 | struct dma_attrs *attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2627 | { |
| 2628 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2629 | struct dmar_domain *domain; |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2630 | unsigned long start_pfn, last_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2631 | struct iova *iova; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2632 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2633 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2634 | if (iommu_no_mapping(pdev)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2635 | return; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2636 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2637 | domain = find_domain(pdev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2638 | BUG_ON(!domain); |
| 2639 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2640 | iommu = domain_get_iommu(domain); |
| 2641 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2642 | iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr)); |
| 2643 | if (!iova) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2644 | return; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2645 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2646 | start_pfn = mm_to_dma_pfn(iova->pfn_lo); |
| 2647 | last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2648 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2649 | pr_debug("Device %s unmapping: pfn %lx-%lx\n", |
| 2650 | pci_name(pdev), start_pfn, last_pfn); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2651 | |
| 2652 | /* clear the whole page */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2653 | dma_pte_clear_range(domain, start_pfn, last_pfn); |
| 2654 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2655 | /* free page tables */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2656 | dma_pte_free_pagetable(domain, start_pfn, last_pfn); |
| 2657 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2658 | if (intel_iommu_strict) { |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 2659 | iommu_flush_iotlb_psi(iommu, domain->id, start_pfn, |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2660 | last_pfn - start_pfn + 1); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2661 | /* free iova */ |
| 2662 | __free_iova(&domain->iovad, iova); |
| 2663 | } else { |
| 2664 | add_unmap(domain, iova); |
| 2665 | /* |
| 2666 | * queue up the release of the unmap to save the 1/6th of the |
| 2667 | * cpu used up by the iotlb flush operation... |
| 2668 | */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2669 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2670 | } |
| 2671 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2672 | static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size, |
| 2673 | int dir) |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2674 | { |
| 2675 | intel_unmap_page(dev, dev_addr, size, dir, NULL); |
| 2676 | } |
| 2677 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2678 | static void *intel_alloc_coherent(struct device *hwdev, size_t size, |
| 2679 | dma_addr_t *dma_handle, gfp_t flags) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2680 | { |
| 2681 | void *vaddr; |
| 2682 | int order; |
| 2683 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2684 | size = PAGE_ALIGN(size); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2685 | order = get_order(size); |
| 2686 | flags &= ~(GFP_DMA | GFP_DMA32); |
| 2687 | |
| 2688 | vaddr = (void *)__get_free_pages(flags, order); |
| 2689 | if (!vaddr) |
| 2690 | return NULL; |
| 2691 | memset(vaddr, 0, size); |
| 2692 | |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2693 | *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size, |
| 2694 | DMA_BIDIRECTIONAL, |
| 2695 | hwdev->coherent_dma_mask); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2696 | if (*dma_handle) |
| 2697 | return vaddr; |
| 2698 | free_pages((unsigned long)vaddr, order); |
| 2699 | return NULL; |
| 2700 | } |
| 2701 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2702 | static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr, |
| 2703 | dma_addr_t dma_handle) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2704 | { |
| 2705 | int order; |
| 2706 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2707 | size = PAGE_ALIGN(size); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2708 | order = get_order(size); |
| 2709 | |
| 2710 | intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL); |
| 2711 | free_pages((unsigned long)vaddr, order); |
| 2712 | } |
| 2713 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2714 | static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist, |
| 2715 | int nelems, enum dma_data_direction dir, |
| 2716 | struct dma_attrs *attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2717 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2718 | struct pci_dev *pdev = to_pci_dev(hwdev); |
| 2719 | struct dmar_domain *domain; |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2720 | unsigned long start_pfn, last_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2721 | struct iova *iova; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2722 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2723 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2724 | if (iommu_no_mapping(pdev)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2725 | return; |
| 2726 | |
| 2727 | domain = find_domain(pdev); |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2728 | BUG_ON(!domain); |
| 2729 | |
| 2730 | iommu = domain_get_iommu(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2731 | |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2732 | iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address)); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2733 | if (!iova) |
| 2734 | return; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2735 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2736 | start_pfn = mm_to_dma_pfn(iova->pfn_lo); |
| 2737 | last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2738 | |
| 2739 | /* clear the whole page */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2740 | dma_pte_clear_range(domain, start_pfn, last_pfn); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2741 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2742 | /* free page tables */ |
| 2743 | dma_pte_free_pagetable(domain, start_pfn, last_pfn); |
| 2744 | |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 2745 | iommu_flush_iotlb_psi(iommu, domain->id, start_pfn, |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2746 | (last_pfn - start_pfn + 1)); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2747 | |
| 2748 | /* free iova */ |
| 2749 | __free_iova(&domain->iovad, iova); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2750 | } |
| 2751 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2752 | static int intel_nontranslate_map_sg(struct device *hddev, |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2753 | struct scatterlist *sglist, int nelems, int dir) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2754 | { |
| 2755 | int i; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2756 | struct scatterlist *sg; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2757 | |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2758 | for_each_sg(sglist, sg, nelems, i) { |
FUJITA Tomonori | 12d4d40 | 2007-10-23 09:32:25 +0200 | [diff] [blame] | 2759 | BUG_ON(!sg_page(sg)); |
David Woodhouse | 4cf2e75 | 2009-02-11 17:23:43 +0000 | [diff] [blame] | 2760 | sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2761 | sg->dma_length = sg->length; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2762 | } |
| 2763 | return nelems; |
| 2764 | } |
| 2765 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2766 | static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems, |
| 2767 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2768 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2769 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2770 | struct pci_dev *pdev = to_pci_dev(hwdev); |
| 2771 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2772 | size_t size = 0; |
| 2773 | int prot = 0; |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 2774 | size_t offset_pfn = 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2775 | struct iova *iova = NULL; |
| 2776 | int ret; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2777 | struct scatterlist *sg; |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 2778 | unsigned long start_vpfn; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2779 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2780 | |
| 2781 | BUG_ON(dir == DMA_NONE); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2782 | if (iommu_no_mapping(pdev)) |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2783 | return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2784 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2785 | domain = get_valid_domain_for_dev(pdev); |
| 2786 | if (!domain) |
| 2787 | return 0; |
| 2788 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2789 | iommu = domain_get_iommu(domain); |
| 2790 | |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 2791 | for_each_sg(sglist, sg, nelems, i) |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 2792 | size += aligned_nrpages(sg->offset, sg->length); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2793 | |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2794 | iova = intel_alloc_iova(hwdev, domain, size, pdev->dma_mask); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2795 | if (!iova) { |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2796 | sglist->dma_length = 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2797 | return 0; |
| 2798 | } |
| 2799 | |
| 2800 | /* |
| 2801 | * Check if DMAR supports zero-length reads on write only |
| 2802 | * mappings.. |
| 2803 | */ |
| 2804 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2805 | !cap_zlr(iommu->cap)) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2806 | prot |= DMA_PTE_READ; |
| 2807 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 2808 | prot |= DMA_PTE_WRITE; |
| 2809 | |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 2810 | start_vpfn = mm_to_dma_pfn(iova->pfn_lo); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2811 | |
| 2812 | ret = domain_sg_mapping(domain, start_vpfn, sglist, mm_to_dma_pfn(size), prot); |
| 2813 | if (unlikely(ret)) { |
| 2814 | /* clear the page */ |
| 2815 | dma_pte_clear_range(domain, start_vpfn, |
| 2816 | start_vpfn + size - 1); |
| 2817 | /* free page tables */ |
| 2818 | dma_pte_free_pagetable(domain, start_vpfn, |
| 2819 | start_vpfn + size - 1); |
| 2820 | /* free iova */ |
| 2821 | __free_iova(&domain->iovad, iova); |
| 2822 | return 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2823 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2824 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2825 | /* it's a non-present to present mapping. Only flush if caching mode */ |
| 2826 | if (cap_caching_mode(iommu->cap)) |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 2827 | iommu_flush_iotlb_psi(iommu, 0, start_vpfn, offset_pfn); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2828 | else |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2829 | iommu_flush_write_buffer(iommu); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2830 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2831 | return nelems; |
| 2832 | } |
| 2833 | |
FUJITA Tomonori | dfb805e | 2009-01-28 21:53:17 +0900 | [diff] [blame] | 2834 | static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 2835 | { |
| 2836 | return !dma_addr; |
| 2837 | } |
| 2838 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 2839 | struct dma_map_ops intel_dma_ops = { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2840 | .alloc_coherent = intel_alloc_coherent, |
| 2841 | .free_coherent = intel_free_coherent, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2842 | .map_sg = intel_map_sg, |
| 2843 | .unmap_sg = intel_unmap_sg, |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2844 | .map_page = intel_map_page, |
| 2845 | .unmap_page = intel_unmap_page, |
FUJITA Tomonori | dfb805e | 2009-01-28 21:53:17 +0900 | [diff] [blame] | 2846 | .mapping_error = intel_mapping_error, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2847 | }; |
| 2848 | |
| 2849 | static inline int iommu_domain_cache_init(void) |
| 2850 | { |
| 2851 | int ret = 0; |
| 2852 | |
| 2853 | iommu_domain_cache = kmem_cache_create("iommu_domain", |
| 2854 | sizeof(struct dmar_domain), |
| 2855 | 0, |
| 2856 | SLAB_HWCACHE_ALIGN, |
| 2857 | |
| 2858 | NULL); |
| 2859 | if (!iommu_domain_cache) { |
| 2860 | printk(KERN_ERR "Couldn't create iommu_domain cache\n"); |
| 2861 | ret = -ENOMEM; |
| 2862 | } |
| 2863 | |
| 2864 | return ret; |
| 2865 | } |
| 2866 | |
| 2867 | static inline int iommu_devinfo_cache_init(void) |
| 2868 | { |
| 2869 | int ret = 0; |
| 2870 | |
| 2871 | iommu_devinfo_cache = kmem_cache_create("iommu_devinfo", |
| 2872 | sizeof(struct device_domain_info), |
| 2873 | 0, |
| 2874 | SLAB_HWCACHE_ALIGN, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2875 | NULL); |
| 2876 | if (!iommu_devinfo_cache) { |
| 2877 | printk(KERN_ERR "Couldn't create devinfo cache\n"); |
| 2878 | ret = -ENOMEM; |
| 2879 | } |
| 2880 | |
| 2881 | return ret; |
| 2882 | } |
| 2883 | |
| 2884 | static inline int iommu_iova_cache_init(void) |
| 2885 | { |
| 2886 | int ret = 0; |
| 2887 | |
| 2888 | iommu_iova_cache = kmem_cache_create("iommu_iova", |
| 2889 | sizeof(struct iova), |
| 2890 | 0, |
| 2891 | SLAB_HWCACHE_ALIGN, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2892 | NULL); |
| 2893 | if (!iommu_iova_cache) { |
| 2894 | printk(KERN_ERR "Couldn't create iova cache\n"); |
| 2895 | ret = -ENOMEM; |
| 2896 | } |
| 2897 | |
| 2898 | return ret; |
| 2899 | } |
| 2900 | |
| 2901 | static int __init iommu_init_mempool(void) |
| 2902 | { |
| 2903 | int ret; |
| 2904 | ret = iommu_iova_cache_init(); |
| 2905 | if (ret) |
| 2906 | return ret; |
| 2907 | |
| 2908 | ret = iommu_domain_cache_init(); |
| 2909 | if (ret) |
| 2910 | goto domain_error; |
| 2911 | |
| 2912 | ret = iommu_devinfo_cache_init(); |
| 2913 | if (!ret) |
| 2914 | return ret; |
| 2915 | |
| 2916 | kmem_cache_destroy(iommu_domain_cache); |
| 2917 | domain_error: |
| 2918 | kmem_cache_destroy(iommu_iova_cache); |
| 2919 | |
| 2920 | return -ENOMEM; |
| 2921 | } |
| 2922 | |
| 2923 | static void __init iommu_exit_mempool(void) |
| 2924 | { |
| 2925 | kmem_cache_destroy(iommu_devinfo_cache); |
| 2926 | kmem_cache_destroy(iommu_domain_cache); |
| 2927 | kmem_cache_destroy(iommu_iova_cache); |
| 2928 | |
| 2929 | } |
| 2930 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2931 | static void __init init_no_remapping_devices(void) |
| 2932 | { |
| 2933 | struct dmar_drhd_unit *drhd; |
| 2934 | |
| 2935 | for_each_drhd_unit(drhd) { |
| 2936 | if (!drhd->include_all) { |
| 2937 | int i; |
| 2938 | for (i = 0; i < drhd->devices_cnt; i++) |
| 2939 | if (drhd->devices[i] != NULL) |
| 2940 | break; |
| 2941 | /* ignore DMAR unit if no pci devices exist */ |
| 2942 | if (i == drhd->devices_cnt) |
| 2943 | drhd->ignored = 1; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | if (dmar_map_gfx) |
| 2948 | return; |
| 2949 | |
| 2950 | for_each_drhd_unit(drhd) { |
| 2951 | int i; |
| 2952 | if (drhd->ignored || drhd->include_all) |
| 2953 | continue; |
| 2954 | |
| 2955 | for (i = 0; i < drhd->devices_cnt; i++) |
| 2956 | if (drhd->devices[i] && |
| 2957 | !IS_GFX_DEVICE(drhd->devices[i])) |
| 2958 | break; |
| 2959 | |
| 2960 | if (i < drhd->devices_cnt) |
| 2961 | continue; |
| 2962 | |
| 2963 | /* bypass IOMMU if it is just for gfx devices */ |
| 2964 | drhd->ignored = 1; |
| 2965 | for (i = 0; i < drhd->devices_cnt; i++) { |
| 2966 | if (!drhd->devices[i]) |
| 2967 | continue; |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 2968 | drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2969 | } |
| 2970 | } |
| 2971 | } |
| 2972 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 2973 | #ifdef CONFIG_SUSPEND |
| 2974 | static int init_iommu_hw(void) |
| 2975 | { |
| 2976 | struct dmar_drhd_unit *drhd; |
| 2977 | struct intel_iommu *iommu = NULL; |
| 2978 | |
| 2979 | for_each_active_iommu(iommu, drhd) |
| 2980 | if (iommu->qi) |
| 2981 | dmar_reenable_qi(iommu); |
| 2982 | |
| 2983 | for_each_active_iommu(iommu, drhd) { |
| 2984 | iommu_flush_write_buffer(iommu); |
| 2985 | |
| 2986 | iommu_set_root_entry(iommu); |
| 2987 | |
| 2988 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2989 | DMA_CCMD_GLOBAL_INVL); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 2990 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2991 | DMA_TLB_GLOBAL_FLUSH); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 2992 | iommu_disable_protect_mem_regions(iommu); |
| 2993 | iommu_enable_translation(iommu); |
| 2994 | } |
| 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
| 2999 | static void iommu_flush_all(void) |
| 3000 | { |
| 3001 | struct dmar_drhd_unit *drhd; |
| 3002 | struct intel_iommu *iommu; |
| 3003 | |
| 3004 | for_each_active_iommu(iommu, drhd) { |
| 3005 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3006 | DMA_CCMD_GLOBAL_INVL); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3007 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3008 | DMA_TLB_GLOBAL_FLUSH); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | static int iommu_suspend(struct sys_device *dev, pm_message_t state) |
| 3013 | { |
| 3014 | struct dmar_drhd_unit *drhd; |
| 3015 | struct intel_iommu *iommu = NULL; |
| 3016 | unsigned long flag; |
| 3017 | |
| 3018 | for_each_active_iommu(iommu, drhd) { |
| 3019 | iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS, |
| 3020 | GFP_ATOMIC); |
| 3021 | if (!iommu->iommu_state) |
| 3022 | goto nomem; |
| 3023 | } |
| 3024 | |
| 3025 | iommu_flush_all(); |
| 3026 | |
| 3027 | for_each_active_iommu(iommu, drhd) { |
| 3028 | iommu_disable_translation(iommu); |
| 3029 | |
| 3030 | spin_lock_irqsave(&iommu->register_lock, flag); |
| 3031 | |
| 3032 | iommu->iommu_state[SR_DMAR_FECTL_REG] = |
| 3033 | readl(iommu->reg + DMAR_FECTL_REG); |
| 3034 | iommu->iommu_state[SR_DMAR_FEDATA_REG] = |
| 3035 | readl(iommu->reg + DMAR_FEDATA_REG); |
| 3036 | iommu->iommu_state[SR_DMAR_FEADDR_REG] = |
| 3037 | readl(iommu->reg + DMAR_FEADDR_REG); |
| 3038 | iommu->iommu_state[SR_DMAR_FEUADDR_REG] = |
| 3039 | readl(iommu->reg + DMAR_FEUADDR_REG); |
| 3040 | |
| 3041 | spin_unlock_irqrestore(&iommu->register_lock, flag); |
| 3042 | } |
| 3043 | return 0; |
| 3044 | |
| 3045 | nomem: |
| 3046 | for_each_active_iommu(iommu, drhd) |
| 3047 | kfree(iommu->iommu_state); |
| 3048 | |
| 3049 | return -ENOMEM; |
| 3050 | } |
| 3051 | |
| 3052 | static int iommu_resume(struct sys_device *dev) |
| 3053 | { |
| 3054 | struct dmar_drhd_unit *drhd; |
| 3055 | struct intel_iommu *iommu = NULL; |
| 3056 | unsigned long flag; |
| 3057 | |
| 3058 | if (init_iommu_hw()) { |
| 3059 | WARN(1, "IOMMU setup failed, DMAR can not resume!\n"); |
| 3060 | return -EIO; |
| 3061 | } |
| 3062 | |
| 3063 | for_each_active_iommu(iommu, drhd) { |
| 3064 | |
| 3065 | spin_lock_irqsave(&iommu->register_lock, flag); |
| 3066 | |
| 3067 | writel(iommu->iommu_state[SR_DMAR_FECTL_REG], |
| 3068 | iommu->reg + DMAR_FECTL_REG); |
| 3069 | writel(iommu->iommu_state[SR_DMAR_FEDATA_REG], |
| 3070 | iommu->reg + DMAR_FEDATA_REG); |
| 3071 | writel(iommu->iommu_state[SR_DMAR_FEADDR_REG], |
| 3072 | iommu->reg + DMAR_FEADDR_REG); |
| 3073 | writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG], |
| 3074 | iommu->reg + DMAR_FEUADDR_REG); |
| 3075 | |
| 3076 | spin_unlock_irqrestore(&iommu->register_lock, flag); |
| 3077 | } |
| 3078 | |
| 3079 | for_each_active_iommu(iommu, drhd) |
| 3080 | kfree(iommu->iommu_state); |
| 3081 | |
| 3082 | return 0; |
| 3083 | } |
| 3084 | |
| 3085 | static struct sysdev_class iommu_sysclass = { |
| 3086 | .name = "iommu", |
| 3087 | .resume = iommu_resume, |
| 3088 | .suspend = iommu_suspend, |
| 3089 | }; |
| 3090 | |
| 3091 | static struct sys_device device_iommu = { |
| 3092 | .cls = &iommu_sysclass, |
| 3093 | }; |
| 3094 | |
| 3095 | static int __init init_iommu_sysfs(void) |
| 3096 | { |
| 3097 | int error; |
| 3098 | |
| 3099 | error = sysdev_class_register(&iommu_sysclass); |
| 3100 | if (error) |
| 3101 | return error; |
| 3102 | |
| 3103 | error = sysdev_register(&device_iommu); |
| 3104 | if (error) |
| 3105 | sysdev_class_unregister(&iommu_sysclass); |
| 3106 | |
| 3107 | return error; |
| 3108 | } |
| 3109 | |
| 3110 | #else |
| 3111 | static int __init init_iommu_sysfs(void) |
| 3112 | { |
| 3113 | return 0; |
| 3114 | } |
| 3115 | #endif /* CONFIG_PM */ |
| 3116 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3117 | int __init intel_iommu_init(void) |
| 3118 | { |
| 3119 | int ret = 0; |
| 3120 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3121 | if (dmar_table_init()) |
| 3122 | return -ENODEV; |
| 3123 | |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 3124 | if (dmar_dev_scope_init()) |
| 3125 | return -ENODEV; |
| 3126 | |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 3127 | /* |
| 3128 | * Check the need for DMA-remapping initialization now. |
| 3129 | * Above initialization will also be used by Interrupt-remapping. |
| 3130 | */ |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3131 | if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 3132 | return -ENODEV; |
| 3133 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3134 | iommu_init_mempool(); |
| 3135 | dmar_init_reserved_ranges(); |
| 3136 | |
| 3137 | init_no_remapping_devices(); |
| 3138 | |
| 3139 | ret = init_dmars(); |
| 3140 | if (ret) { |
| 3141 | printk(KERN_ERR "IOMMU: dmar init failed\n"); |
| 3142 | put_iova_domain(&reserved_iova_list); |
| 3143 | iommu_exit_mempool(); |
| 3144 | return ret; |
| 3145 | } |
| 3146 | printk(KERN_INFO |
| 3147 | "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n"); |
| 3148 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3149 | init_timer(&unmap_timer); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3150 | force_iommu = 1; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3151 | |
| 3152 | if (!iommu_pass_through) { |
| 3153 | printk(KERN_INFO |
| 3154 | "Multi-level page-table translation for DMAR.\n"); |
| 3155 | dma_ops = &intel_dma_ops; |
| 3156 | } else |
| 3157 | printk(KERN_INFO |
| 3158 | "DMAR: Pass through translation for DMAR.\n"); |
| 3159 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3160 | init_iommu_sysfs(); |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 3161 | |
| 3162 | register_iommu(&intel_iommu_ops); |
| 3163 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3164 | return 0; |
| 3165 | } |
Keshavamurthy, Anil S | e820482 | 2007-10-21 16:41:55 -0700 | [diff] [blame] | 3166 | |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3167 | static void iommu_detach_dependent_devices(struct intel_iommu *iommu, |
| 3168 | struct pci_dev *pdev) |
| 3169 | { |
| 3170 | struct pci_dev *tmp, *parent; |
| 3171 | |
| 3172 | if (!iommu || !pdev) |
| 3173 | return; |
| 3174 | |
| 3175 | /* dependent device detach */ |
| 3176 | tmp = pci_find_upstream_pcie_bridge(pdev); |
| 3177 | /* Secondary interface's bus number and devfn 0 */ |
| 3178 | if (tmp) { |
| 3179 | parent = pdev->bus->self; |
| 3180 | while (parent != tmp) { |
| 3181 | iommu_detach_dev(iommu, parent->bus->number, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3182 | parent->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3183 | parent = parent->bus->self; |
| 3184 | } |
| 3185 | if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */ |
| 3186 | iommu_detach_dev(iommu, |
| 3187 | tmp->subordinate->number, 0); |
| 3188 | else /* this is a legacy PCI bridge */ |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3189 | iommu_detach_dev(iommu, tmp->bus->number, |
| 3190 | tmp->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3191 | } |
| 3192 | } |
| 3193 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3194 | static void domain_remove_one_dev_info(struct dmar_domain *domain, |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3195 | struct pci_dev *pdev) |
| 3196 | { |
| 3197 | struct device_domain_info *info; |
| 3198 | struct intel_iommu *iommu; |
| 3199 | unsigned long flags; |
| 3200 | int found = 0; |
| 3201 | struct list_head *entry, *tmp; |
| 3202 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3203 | iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number, |
| 3204 | pdev->devfn); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3205 | if (!iommu) |
| 3206 | return; |
| 3207 | |
| 3208 | spin_lock_irqsave(&device_domain_lock, flags); |
| 3209 | list_for_each_safe(entry, tmp, &domain->devices) { |
| 3210 | info = list_entry(entry, struct device_domain_info, link); |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3211 | /* No need to compare PCI domain; it has to be the same */ |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3212 | if (info->bus == pdev->bus->number && |
| 3213 | info->devfn == pdev->devfn) { |
| 3214 | list_del(&info->link); |
| 3215 | list_del(&info->global); |
| 3216 | if (info->dev) |
| 3217 | info->dev->dev.archdata.iommu = NULL; |
| 3218 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 3219 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 3220 | iommu_disable_dev_iotlb(info); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3221 | iommu_detach_dev(iommu, info->bus, info->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3222 | iommu_detach_dependent_devices(iommu, pdev); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3223 | free_devinfo_mem(info); |
| 3224 | |
| 3225 | spin_lock_irqsave(&device_domain_lock, flags); |
| 3226 | |
| 3227 | if (found) |
| 3228 | break; |
| 3229 | else |
| 3230 | continue; |
| 3231 | } |
| 3232 | |
| 3233 | /* if there is no other devices under the same iommu |
| 3234 | * owned by this domain, clear this iommu in iommu_bmp |
| 3235 | * update iommu count and coherency |
| 3236 | */ |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3237 | if (iommu == device_to_iommu(info->segment, info->bus, |
| 3238 | info->devfn)) |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3239 | found = 1; |
| 3240 | } |
| 3241 | |
| 3242 | if (found == 0) { |
| 3243 | unsigned long tmp_flags; |
| 3244 | spin_lock_irqsave(&domain->iommu_lock, tmp_flags); |
| 3245 | clear_bit(iommu->seq_id, &domain->iommu_bmp); |
| 3246 | domain->iommu_count--; |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 3247 | domain_update_iommu_cap(domain); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3248 | spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags); |
| 3249 | } |
| 3250 | |
| 3251 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 3252 | } |
| 3253 | |
| 3254 | static void vm_domain_remove_all_dev_info(struct dmar_domain *domain) |
| 3255 | { |
| 3256 | struct device_domain_info *info; |
| 3257 | struct intel_iommu *iommu; |
| 3258 | unsigned long flags1, flags2; |
| 3259 | |
| 3260 | spin_lock_irqsave(&device_domain_lock, flags1); |
| 3261 | while (!list_empty(&domain->devices)) { |
| 3262 | info = list_entry(domain->devices.next, |
| 3263 | struct device_domain_info, link); |
| 3264 | list_del(&info->link); |
| 3265 | list_del(&info->global); |
| 3266 | if (info->dev) |
| 3267 | info->dev->dev.archdata.iommu = NULL; |
| 3268 | |
| 3269 | spin_unlock_irqrestore(&device_domain_lock, flags1); |
| 3270 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 3271 | iommu_disable_dev_iotlb(info); |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3272 | iommu = device_to_iommu(info->segment, info->bus, info->devfn); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3273 | iommu_detach_dev(iommu, info->bus, info->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3274 | iommu_detach_dependent_devices(iommu, info->dev); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3275 | |
| 3276 | /* clear this iommu in iommu_bmp, update iommu count |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 3277 | * and capabilities |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3278 | */ |
| 3279 | spin_lock_irqsave(&domain->iommu_lock, flags2); |
| 3280 | if (test_and_clear_bit(iommu->seq_id, |
| 3281 | &domain->iommu_bmp)) { |
| 3282 | domain->iommu_count--; |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 3283 | domain_update_iommu_cap(domain); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3284 | } |
| 3285 | spin_unlock_irqrestore(&domain->iommu_lock, flags2); |
| 3286 | |
| 3287 | free_devinfo_mem(info); |
| 3288 | spin_lock_irqsave(&device_domain_lock, flags1); |
| 3289 | } |
| 3290 | spin_unlock_irqrestore(&device_domain_lock, flags1); |
| 3291 | } |
| 3292 | |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3293 | /* domain id for virtual machine, it won't be set in context */ |
| 3294 | static unsigned long vm_domid; |
| 3295 | |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3296 | static int vm_domain_min_agaw(struct dmar_domain *domain) |
| 3297 | { |
| 3298 | int i; |
| 3299 | int min_agaw = domain->agaw; |
| 3300 | |
| 3301 | i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus); |
| 3302 | for (; i < g_num_of_iommus; ) { |
| 3303 | if (min_agaw > g_iommus[i]->agaw) |
| 3304 | min_agaw = g_iommus[i]->agaw; |
| 3305 | |
| 3306 | i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1); |
| 3307 | } |
| 3308 | |
| 3309 | return min_agaw; |
| 3310 | } |
| 3311 | |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3312 | static struct dmar_domain *iommu_alloc_vm_domain(void) |
| 3313 | { |
| 3314 | struct dmar_domain *domain; |
| 3315 | |
| 3316 | domain = alloc_domain_mem(); |
| 3317 | if (!domain) |
| 3318 | return NULL; |
| 3319 | |
| 3320 | domain->id = vm_domid++; |
| 3321 | memset(&domain->iommu_bmp, 0, sizeof(unsigned long)); |
| 3322 | domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE; |
| 3323 | |
| 3324 | return domain; |
| 3325 | } |
| 3326 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3327 | static int md_domain_init(struct dmar_domain *domain, int guest_width) |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3328 | { |
| 3329 | int adjust_width; |
| 3330 | |
| 3331 | init_iova_domain(&domain->iovad, DMA_32BIT_PFN); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3332 | spin_lock_init(&domain->iommu_lock); |
| 3333 | |
| 3334 | domain_reserve_special_ranges(domain); |
| 3335 | |
| 3336 | /* calculate AGAW */ |
| 3337 | domain->gaw = guest_width; |
| 3338 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 3339 | domain->agaw = width_to_agaw(adjust_width); |
| 3340 | |
| 3341 | INIT_LIST_HEAD(&domain->devices); |
| 3342 | |
| 3343 | domain->iommu_count = 0; |
| 3344 | domain->iommu_coherency = 0; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3345 | domain->max_addr = 0; |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3346 | |
| 3347 | /* always allocate the top pgd */ |
| 3348 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(); |
| 3349 | if (!domain->pgd) |
| 3350 | return -ENOMEM; |
| 3351 | domain_flush_cache(domain, domain->pgd, PAGE_SIZE); |
| 3352 | return 0; |
| 3353 | } |
| 3354 | |
| 3355 | static void iommu_free_vm_domain(struct dmar_domain *domain) |
| 3356 | { |
| 3357 | unsigned long flags; |
| 3358 | struct dmar_drhd_unit *drhd; |
| 3359 | struct intel_iommu *iommu; |
| 3360 | unsigned long i; |
| 3361 | unsigned long ndomains; |
| 3362 | |
| 3363 | for_each_drhd_unit(drhd) { |
| 3364 | if (drhd->ignored) |
| 3365 | continue; |
| 3366 | iommu = drhd->iommu; |
| 3367 | |
| 3368 | ndomains = cap_ndoms(iommu->cap); |
| 3369 | i = find_first_bit(iommu->domain_ids, ndomains); |
| 3370 | for (; i < ndomains; ) { |
| 3371 | if (iommu->domains[i] == domain) { |
| 3372 | spin_lock_irqsave(&iommu->lock, flags); |
| 3373 | clear_bit(i, iommu->domain_ids); |
| 3374 | iommu->domains[i] = NULL; |
| 3375 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 3376 | break; |
| 3377 | } |
| 3378 | i = find_next_bit(iommu->domain_ids, ndomains, i+1); |
| 3379 | } |
| 3380 | } |
| 3381 | } |
| 3382 | |
| 3383 | static void vm_domain_exit(struct dmar_domain *domain) |
| 3384 | { |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3385 | /* Domain 0 is reserved, so dont process it */ |
| 3386 | if (!domain) |
| 3387 | return; |
| 3388 | |
| 3389 | vm_domain_remove_all_dev_info(domain); |
| 3390 | /* destroy iovas */ |
| 3391 | put_iova_domain(&domain->iovad); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3392 | |
| 3393 | /* clear ptes */ |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 3394 | dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3395 | |
| 3396 | /* free page tables */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3397 | dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3398 | |
| 3399 | iommu_free_vm_domain(domain); |
| 3400 | free_domain_mem(domain); |
| 3401 | } |
| 3402 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3403 | static int intel_iommu_domain_init(struct iommu_domain *domain) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3404 | { |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3405 | struct dmar_domain *dmar_domain; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3406 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3407 | dmar_domain = iommu_alloc_vm_domain(); |
| 3408 | if (!dmar_domain) { |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3409 | printk(KERN_ERR |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3410 | "intel_iommu_domain_init: dmar_domain == NULL\n"); |
| 3411 | return -ENOMEM; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3412 | } |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3413 | if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3414 | printk(KERN_ERR |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3415 | "intel_iommu_domain_init() failed\n"); |
| 3416 | vm_domain_exit(dmar_domain); |
| 3417 | return -ENOMEM; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3418 | } |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3419 | domain->priv = dmar_domain; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3420 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3421 | return 0; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3422 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3423 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3424 | static void intel_iommu_domain_destroy(struct iommu_domain *domain) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3425 | { |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3426 | struct dmar_domain *dmar_domain = domain->priv; |
| 3427 | |
| 3428 | domain->priv = NULL; |
| 3429 | vm_domain_exit(dmar_domain); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3430 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3431 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3432 | static int intel_iommu_attach_device(struct iommu_domain *domain, |
| 3433 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3434 | { |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3435 | struct dmar_domain *dmar_domain = domain->priv; |
| 3436 | struct pci_dev *pdev = to_pci_dev(dev); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3437 | struct intel_iommu *iommu; |
| 3438 | int addr_width; |
| 3439 | u64 end; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3440 | int ret; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3441 | |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3442 | /* normally pdev is not mapped */ |
| 3443 | if (unlikely(domain_context_mapped(pdev))) { |
| 3444 | struct dmar_domain *old_domain; |
| 3445 | |
| 3446 | old_domain = find_domain(pdev); |
| 3447 | if (old_domain) { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3448 | if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE || |
| 3449 | dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) |
| 3450 | domain_remove_one_dev_info(old_domain, pdev); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3451 | else |
| 3452 | domain_remove_dev_info(old_domain); |
| 3453 | } |
| 3454 | } |
| 3455 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3456 | iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number, |
| 3457 | pdev->devfn); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3458 | if (!iommu) |
| 3459 | return -ENODEV; |
| 3460 | |
| 3461 | /* check if this iommu agaw is sufficient for max mapped address */ |
| 3462 | addr_width = agaw_to_width(iommu->agaw); |
| 3463 | end = DOMAIN_MAX_ADDR(addr_width); |
| 3464 | end = end & VTD_PAGE_MASK; |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3465 | if (end < dmar_domain->max_addr) { |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3466 | printk(KERN_ERR "%s: iommu agaw (%d) is not " |
| 3467 | "sufficient for the mapped address (%llx)\n", |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3468 | __func__, iommu->agaw, dmar_domain->max_addr); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3469 | return -EFAULT; |
| 3470 | } |
| 3471 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3472 | ret = domain_add_dev_info(dmar_domain, pdev); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3473 | if (ret) |
| 3474 | return ret; |
| 3475 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 3476 | ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3477 | return ret; |
| 3478 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3479 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3480 | static void intel_iommu_detach_device(struct iommu_domain *domain, |
| 3481 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3482 | { |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3483 | struct dmar_domain *dmar_domain = domain->priv; |
| 3484 | struct pci_dev *pdev = to_pci_dev(dev); |
| 3485 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3486 | domain_remove_one_dev_info(dmar_domain, pdev); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3487 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3488 | |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3489 | static int intel_iommu_map_range(struct iommu_domain *domain, |
| 3490 | unsigned long iova, phys_addr_t hpa, |
| 3491 | size_t size, int iommu_prot) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3492 | { |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3493 | struct dmar_domain *dmar_domain = domain->priv; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3494 | u64 max_addr; |
| 3495 | int addr_width; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3496 | int prot = 0; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3497 | int ret; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3498 | |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3499 | if (iommu_prot & IOMMU_READ) |
| 3500 | prot |= DMA_PTE_READ; |
| 3501 | if (iommu_prot & IOMMU_WRITE) |
| 3502 | prot |= DMA_PTE_WRITE; |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 3503 | if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping) |
| 3504 | prot |= DMA_PTE_SNP; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3505 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 3506 | max_addr = iova + size; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3507 | if (dmar_domain->max_addr < max_addr) { |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3508 | int min_agaw; |
| 3509 | u64 end; |
| 3510 | |
| 3511 | /* check if minimum agaw is sufficient for mapped address */ |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3512 | min_agaw = vm_domain_min_agaw(dmar_domain); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3513 | addr_width = agaw_to_width(min_agaw); |
| 3514 | end = DOMAIN_MAX_ADDR(addr_width); |
| 3515 | end = end & VTD_PAGE_MASK; |
| 3516 | if (end < max_addr) { |
| 3517 | printk(KERN_ERR "%s: iommu agaw (%d) is not " |
| 3518 | "sufficient for the mapped address (%llx)\n", |
| 3519 | __func__, min_agaw, max_addr); |
| 3520 | return -EFAULT; |
| 3521 | } |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3522 | dmar_domain->max_addr = max_addr; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3523 | } |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 3524 | /* Round up size to next multiple of PAGE_SIZE, if it and |
| 3525 | the low bits of hpa would take us onto the next page */ |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 3526 | size = aligned_nrpages(hpa, size); |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 3527 | ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT, |
| 3528 | hpa >> VTD_PAGE_SHIFT, size, prot); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3529 | return ret; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3530 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3531 | |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3532 | static void intel_iommu_unmap_range(struct iommu_domain *domain, |
| 3533 | unsigned long iova, size_t size) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3534 | { |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3535 | struct dmar_domain *dmar_domain = domain->priv; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3536 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 3537 | dma_pte_clear_range(dmar_domain, iova >> VTD_PAGE_SHIFT, |
| 3538 | (iova + size - 1) >> VTD_PAGE_SHIFT); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3539 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 3540 | if (dmar_domain->max_addr == iova + size) |
| 3541 | dmar_domain->max_addr = iova; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3542 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3543 | |
Joerg Roedel | d14d657 | 2008-12-03 15:06:57 +0100 | [diff] [blame] | 3544 | static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, |
| 3545 | unsigned long iova) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3546 | { |
Joerg Roedel | d14d657 | 2008-12-03 15:06:57 +0100 | [diff] [blame] | 3547 | struct dmar_domain *dmar_domain = domain->priv; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3548 | struct dma_pte *pte; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3549 | u64 phys = 0; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3550 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 3551 | pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3552 | if (pte) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3553 | phys = dma_pte_addr(pte); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3554 | |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3555 | return phys; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3556 | } |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 3557 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 3558 | static int intel_iommu_domain_has_cap(struct iommu_domain *domain, |
| 3559 | unsigned long cap) |
| 3560 | { |
| 3561 | struct dmar_domain *dmar_domain = domain->priv; |
| 3562 | |
| 3563 | if (cap == IOMMU_CAP_CACHE_COHERENCY) |
| 3564 | return dmar_domain->iommu_snooping; |
| 3565 | |
| 3566 | return 0; |
| 3567 | } |
| 3568 | |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 3569 | static struct iommu_ops intel_iommu_ops = { |
| 3570 | .domain_init = intel_iommu_domain_init, |
| 3571 | .domain_destroy = intel_iommu_domain_destroy, |
| 3572 | .attach_dev = intel_iommu_attach_device, |
| 3573 | .detach_dev = intel_iommu_detach_device, |
| 3574 | .map = intel_iommu_map_range, |
| 3575 | .unmap = intel_iommu_unmap_range, |
| 3576 | .iova_to_phys = intel_iommu_iova_to_phys, |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 3577 | .domain_has_cap = intel_iommu_domain_has_cap, |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 3578 | }; |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 3579 | |
| 3580 | static void __devinit quirk_iommu_rwbf(struct pci_dev *dev) |
| 3581 | { |
| 3582 | /* |
| 3583 | * Mobile 4 Series Chipset neglects to set RWBF capability, |
| 3584 | * but needs it: |
| 3585 | */ |
| 3586 | printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n"); |
| 3587 | rwbf_quirk = 1; |
| 3588 | } |
| 3589 | |
| 3590 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf); |