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