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