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> |
Paul Gortmaker | 54485c3 | 2011-10-29 10:26:25 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 28 | #include <linux/slab.h> |
| 29 | #include <linux/irq.h> |
| 30 | #include <linux/interrupt.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/pci.h> |
| 33 | #include <linux/dmar.h> |
| 34 | #include <linux/dma-mapping.h> |
| 35 | #include <linux/mempool.h> |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 36 | #include <linux/timer.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 37 | #include <linux/iova.h> |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 38 | #include <linux/iommu.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 39 | #include <linux/intel-iommu.h> |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 40 | #include <linux/syscore_ops.h> |
Shane Wang | 69575d3 | 2009-09-01 18:25:07 -0700 | [diff] [blame] | 41 | #include <linux/tboot.h> |
Stephen Rothwell | adb2fe0 | 2009-08-31 15:24:23 +1000 | [diff] [blame] | 42 | #include <linux/dmi.h> |
Joerg Roedel | 5cdede2 | 2011-04-04 15:55:18 +0200 | [diff] [blame] | 43 | #include <linux/pci-ats.h> |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame^] | 44 | #include <linux/memblock.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 45 | #include <asm/cacheflush.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 46 | #include <asm/iommu.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 47 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 48 | #define ROOT_SIZE VTD_PAGE_SIZE |
| 49 | #define CONTEXT_SIZE VTD_PAGE_SIZE |
| 50 | |
Mike Travis | 825507d | 2011-05-28 13:15:06 -0500 | [diff] [blame] | 51 | #define IS_BRIDGE_HOST_DEVICE(pdev) \ |
| 52 | ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 53 | #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) |
| 54 | #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA) |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 55 | #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 56 | |
| 57 | #define IOAPIC_RANGE_START (0xfee00000) |
| 58 | #define IOAPIC_RANGE_END (0xfeefffff) |
| 59 | #define IOVA_START_ADDR (0x1000) |
| 60 | |
| 61 | #define DEFAULT_DOMAIN_ADDRESS_WIDTH 48 |
| 62 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 63 | #define MAX_AGAW_WIDTH 64 |
| 64 | |
David Woodhouse | 2ebe315 | 2009-09-19 07:34:04 -0700 | [diff] [blame] | 65 | #define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1) |
| 66 | #define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1) |
| 67 | |
| 68 | /* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR |
| 69 | to match. That way, we can use 'unsigned long' for PFNs with impunity. */ |
| 70 | #define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \ |
| 71 | __DOMAIN_MAX_PFN(gaw), (unsigned long)-1)) |
| 72 | #define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 73 | |
Mark McLoughlin | f27be03 | 2008-11-20 15:49:43 +0000 | [diff] [blame] | 74 | #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 75 | #define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32)) |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 76 | #define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64)) |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 77 | |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 78 | /* page table handling */ |
| 79 | #define LEVEL_STRIDE (9) |
| 80 | #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1) |
| 81 | |
| 82 | static inline int agaw_to_level(int agaw) |
| 83 | { |
| 84 | return agaw + 2; |
| 85 | } |
| 86 | |
| 87 | static inline int agaw_to_width(int agaw) |
| 88 | { |
| 89 | return 30 + agaw * LEVEL_STRIDE; |
| 90 | } |
| 91 | |
| 92 | static inline int width_to_agaw(int width) |
| 93 | { |
| 94 | return (width - 30) / LEVEL_STRIDE; |
| 95 | } |
| 96 | |
| 97 | static inline unsigned int level_to_offset_bits(int level) |
| 98 | { |
| 99 | return (level - 1) * LEVEL_STRIDE; |
| 100 | } |
| 101 | |
| 102 | static inline int pfn_level_offset(unsigned long pfn, int level) |
| 103 | { |
| 104 | return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK; |
| 105 | } |
| 106 | |
| 107 | static inline unsigned long level_mask(int level) |
| 108 | { |
| 109 | return -1UL << level_to_offset_bits(level); |
| 110 | } |
| 111 | |
| 112 | static inline unsigned long level_size(int level) |
| 113 | { |
| 114 | return 1UL << level_to_offset_bits(level); |
| 115 | } |
| 116 | |
| 117 | static inline unsigned long align_to_level(unsigned long pfn, int level) |
| 118 | { |
| 119 | return (pfn + level_size(level) - 1) & level_mask(level); |
| 120 | } |
David Woodhouse | fd18de5 | 2009-05-10 23:57:41 +0100 | [diff] [blame] | 121 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 122 | static inline unsigned long lvl_to_nr_pages(unsigned int lvl) |
| 123 | { |
| 124 | return 1 << ((lvl - 1) * LEVEL_STRIDE); |
| 125 | } |
| 126 | |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 127 | /* VT-d pages must always be _smaller_ than MM pages. Otherwise things |
| 128 | are never going to work. */ |
| 129 | static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn) |
| 130 | { |
| 131 | return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 132 | } |
| 133 | |
| 134 | static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn) |
| 135 | { |
| 136 | return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 137 | } |
| 138 | static inline unsigned long page_to_dma_pfn(struct page *pg) |
| 139 | { |
| 140 | return mm_to_dma_pfn(page_to_pfn(pg)); |
| 141 | } |
| 142 | static inline unsigned long virt_to_dma_pfn(void *p) |
| 143 | { |
| 144 | return page_to_dma_pfn(virt_to_page(p)); |
| 145 | } |
| 146 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 147 | /* global iommu list, set NULL for ignored DMAR units */ |
| 148 | static struct intel_iommu **g_iommus; |
| 149 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 150 | static void __init check_tylersburg_isoch(void); |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 151 | static int rwbf_quirk; |
| 152 | |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 153 | /* |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 154 | * set to 1 to panic kernel if can't successfully enable VT-d |
| 155 | * (used when kernel is launched w/ TXT) |
| 156 | */ |
| 157 | static int force_on = 0; |
| 158 | |
| 159 | /* |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 160 | * 0: Present |
| 161 | * 1-11: Reserved |
| 162 | * 12-63: Context Ptr (12 - (haw-1)) |
| 163 | * 64-127: Reserved |
| 164 | */ |
| 165 | struct root_entry { |
| 166 | u64 val; |
| 167 | u64 rsvd1; |
| 168 | }; |
| 169 | #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry)) |
| 170 | static inline bool root_present(struct root_entry *root) |
| 171 | { |
| 172 | return (root->val & 1); |
| 173 | } |
| 174 | static inline void set_root_present(struct root_entry *root) |
| 175 | { |
| 176 | root->val |= 1; |
| 177 | } |
| 178 | static inline void set_root_value(struct root_entry *root, unsigned long value) |
| 179 | { |
| 180 | root->val |= value & VTD_PAGE_MASK; |
| 181 | } |
| 182 | |
| 183 | static inline struct context_entry * |
| 184 | get_context_addr_from_root(struct root_entry *root) |
| 185 | { |
| 186 | return (struct context_entry *) |
| 187 | (root_present(root)?phys_to_virt( |
| 188 | root->val & VTD_PAGE_MASK) : |
| 189 | NULL); |
| 190 | } |
| 191 | |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 192 | /* |
| 193 | * low 64 bits: |
| 194 | * 0: present |
| 195 | * 1: fault processing disable |
| 196 | * 2-3: translation type |
| 197 | * 12-63: address space root |
| 198 | * high 64 bits: |
| 199 | * 0-2: address width |
| 200 | * 3-6: aval |
| 201 | * 8-23: domain id |
| 202 | */ |
| 203 | struct context_entry { |
| 204 | u64 lo; |
| 205 | u64 hi; |
| 206 | }; |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 207 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 208 | static inline bool context_present(struct context_entry *context) |
| 209 | { |
| 210 | return (context->lo & 1); |
| 211 | } |
| 212 | static inline void context_set_present(struct context_entry *context) |
| 213 | { |
| 214 | context->lo |= 1; |
| 215 | } |
| 216 | |
| 217 | static inline void context_set_fault_enable(struct context_entry *context) |
| 218 | { |
| 219 | context->lo &= (((u64)-1) << 2) | 1; |
| 220 | } |
| 221 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 222 | static inline void context_set_translation_type(struct context_entry *context, |
| 223 | unsigned long value) |
| 224 | { |
| 225 | context->lo &= (((u64)-1) << 4) | 3; |
| 226 | context->lo |= (value & 3) << 2; |
| 227 | } |
| 228 | |
| 229 | static inline void context_set_address_root(struct context_entry *context, |
| 230 | unsigned long value) |
| 231 | { |
| 232 | context->lo |= value & VTD_PAGE_MASK; |
| 233 | } |
| 234 | |
| 235 | static inline void context_set_address_width(struct context_entry *context, |
| 236 | unsigned long value) |
| 237 | { |
| 238 | context->hi |= value & 7; |
| 239 | } |
| 240 | |
| 241 | static inline void context_set_domain_id(struct context_entry *context, |
| 242 | unsigned long value) |
| 243 | { |
| 244 | context->hi |= (value & ((1 << 16) - 1)) << 8; |
| 245 | } |
| 246 | |
| 247 | static inline void context_clear_entry(struct context_entry *context) |
| 248 | { |
| 249 | context->lo = 0; |
| 250 | context->hi = 0; |
| 251 | } |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 252 | |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 253 | /* |
| 254 | * 0: readable |
| 255 | * 1: writable |
| 256 | * 2-6: reserved |
| 257 | * 7: super page |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 258 | * 8-10: available |
| 259 | * 11: snoop behavior |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 260 | * 12-63: Host physcial address |
| 261 | */ |
| 262 | struct dma_pte { |
| 263 | u64 val; |
| 264 | }; |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 265 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 266 | static inline void dma_clear_pte(struct dma_pte *pte) |
| 267 | { |
| 268 | pte->val = 0; |
| 269 | } |
| 270 | |
| 271 | static inline void dma_set_pte_readable(struct dma_pte *pte) |
| 272 | { |
| 273 | pte->val |= DMA_PTE_READ; |
| 274 | } |
| 275 | |
| 276 | static inline void dma_set_pte_writable(struct dma_pte *pte) |
| 277 | { |
| 278 | pte->val |= DMA_PTE_WRITE; |
| 279 | } |
| 280 | |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 281 | static inline void dma_set_pte_snp(struct dma_pte *pte) |
| 282 | { |
| 283 | pte->val |= DMA_PTE_SNP; |
| 284 | } |
| 285 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 286 | static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot) |
| 287 | { |
| 288 | pte->val = (pte->val & ~3) | (prot & 3); |
| 289 | } |
| 290 | |
| 291 | static inline u64 dma_pte_addr(struct dma_pte *pte) |
| 292 | { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 293 | #ifdef CONFIG_64BIT |
| 294 | return pte->val & VTD_PAGE_MASK; |
| 295 | #else |
| 296 | /* Must have a full atomic 64-bit read */ |
David Woodhouse | 1a8bd48 | 2010-08-10 01:38:53 +0100 | [diff] [blame] | 297 | return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK; |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 298 | #endif |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 299 | } |
| 300 | |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 301 | 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] | 302 | { |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 303 | pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT; |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static inline bool dma_pte_present(struct dma_pte *pte) |
| 307 | { |
| 308 | return (pte->val & 3) != 0; |
| 309 | } |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 310 | |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 311 | static inline bool dma_pte_superpage(struct dma_pte *pte) |
| 312 | { |
| 313 | return (pte->val & (1 << 7)); |
| 314 | } |
| 315 | |
David Woodhouse | 75e6bf9 | 2009-07-02 11:21:16 +0100 | [diff] [blame] | 316 | static inline int first_pte_in_page(struct dma_pte *pte) |
| 317 | { |
| 318 | return !((unsigned long)pte & ~VTD_PAGE_MASK); |
| 319 | } |
| 320 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 321 | /* |
| 322 | * This domain is a statically identity mapping domain. |
| 323 | * 1. This domain creats a static 1:1 mapping to all usable memory. |
| 324 | * 2. It maps to each iommu if successful. |
| 325 | * 3. Each iommu mapps to this domain if successful. |
| 326 | */ |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 327 | static struct dmar_domain *si_domain; |
| 328 | static int hw_pass_through = 1; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 329 | |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 330 | /* devices under the same p2p bridge are owned in one domain */ |
Mike Day | cdc7b83 | 2008-12-12 17:16:30 +0100 | [diff] [blame] | 331 | #define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0) |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 332 | |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 333 | /* domain represents a virtual machine, more than one devices |
| 334 | * across iommus may be owned in one domain, e.g. kvm guest. |
| 335 | */ |
| 336 | #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1) |
| 337 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 338 | /* si_domain contains mulitple devices */ |
| 339 | #define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2) |
| 340 | |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 341 | struct dmar_domain { |
| 342 | int id; /* domain id */ |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 343 | int nid; /* node id */ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 344 | unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/ |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 345 | |
| 346 | struct list_head devices; /* all devices' list */ |
| 347 | struct iova_domain iovad; /* iova's that belong to this domain */ |
| 348 | |
| 349 | struct dma_pte *pgd; /* virtual address */ |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 350 | int gaw; /* max guest address width */ |
| 351 | |
| 352 | /* adjusted guest address width, 0 is level 2 30-bit */ |
| 353 | int agaw; |
| 354 | |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 355 | int flags; /* flags to find out type of domain */ |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 356 | |
| 357 | int iommu_coherency;/* indicate coherency of iommu access */ |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 358 | int iommu_snooping; /* indicate snooping control feature*/ |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 359 | int iommu_count; /* reference count of iommu */ |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 360 | int iommu_superpage;/* Level of superpages supported: |
| 361 | 0 == 4KiB (no superpages), 1 == 2MiB, |
| 362 | 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */ |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 363 | spinlock_t iommu_lock; /* protect iommu set in domain */ |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 364 | u64 max_addr; /* maximum mapped address */ |
Mark McLoughlin | 99126f7 | 2008-11-20 15:49:47 +0000 | [diff] [blame] | 365 | }; |
| 366 | |
Mark McLoughlin | a647dacb | 2008-11-20 15:49:48 +0000 | [diff] [blame] | 367 | /* PCI domain-device relationship */ |
| 368 | struct device_domain_info { |
| 369 | struct list_head link; /* link to domain siblings */ |
| 370 | struct list_head global; /* link to global list */ |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 371 | int segment; /* PCI domain */ |
| 372 | u8 bus; /* PCI bus number */ |
Mark McLoughlin | a647dacb | 2008-11-20 15:49:48 +0000 | [diff] [blame] | 373 | u8 devfn; /* PCI devfn number */ |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 374 | struct pci_dev *dev; /* it's NULL for PCIe-to-PCI bridge */ |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 375 | struct intel_iommu *iommu; /* IOMMU used by this device */ |
Mark McLoughlin | a647dacb | 2008-11-20 15:49:48 +0000 | [diff] [blame] | 376 | struct dmar_domain *domain; /* pointer to domain */ |
| 377 | }; |
| 378 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 379 | static void flush_unmaps_timeout(unsigned long data); |
| 380 | |
| 381 | DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0); |
| 382 | |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 383 | #define HIGH_WATER_MARK 250 |
| 384 | struct deferred_flush_tables { |
| 385 | int next; |
| 386 | struct iova *iova[HIGH_WATER_MARK]; |
| 387 | struct dmar_domain *domain[HIGH_WATER_MARK]; |
| 388 | }; |
| 389 | |
| 390 | static struct deferred_flush_tables *deferred_flush; |
| 391 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 392 | /* bitmap for indexing intel_iommus */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 393 | static int g_num_of_iommus; |
| 394 | |
| 395 | static DEFINE_SPINLOCK(async_umap_flush_lock); |
| 396 | static LIST_HEAD(unmaps_to_do); |
| 397 | |
| 398 | static int timer_on; |
| 399 | static long list_size; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 400 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 401 | static void domain_remove_dev_info(struct dmar_domain *domain); |
| 402 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 403 | #ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 404 | int dmar_disabled = 0; |
| 405 | #else |
| 406 | int dmar_disabled = 1; |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 407 | #endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/ |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 408 | |
David Woodhouse | 2d9e667 | 2010-06-15 10:57:57 +0100 | [diff] [blame] | 409 | static int dmar_map_gfx = 1; |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 410 | static int dmar_forcedac; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 411 | static int intel_iommu_strict; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 412 | static int intel_iommu_superpage = 1; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 413 | |
David Woodhouse | c0771df | 2011-10-14 20:59:46 +0100 | [diff] [blame] | 414 | int intel_iommu_gfx_mapped; |
| 415 | EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped); |
| 416 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 417 | #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1)) |
| 418 | static DEFINE_SPINLOCK(device_domain_lock); |
| 419 | static LIST_HEAD(device_domain_list); |
| 420 | |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 421 | static struct iommu_ops intel_iommu_ops; |
| 422 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 423 | static int __init intel_iommu_setup(char *str) |
| 424 | { |
| 425 | if (!str) |
| 426 | return -EINVAL; |
| 427 | while (*str) { |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 428 | if (!strncmp(str, "on", 2)) { |
| 429 | dmar_disabled = 0; |
| 430 | printk(KERN_INFO "Intel-IOMMU: enabled\n"); |
| 431 | } else if (!strncmp(str, "off", 3)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 432 | dmar_disabled = 1; |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 433 | printk(KERN_INFO "Intel-IOMMU: disabled\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 434 | } else if (!strncmp(str, "igfx_off", 8)) { |
| 435 | dmar_map_gfx = 0; |
| 436 | printk(KERN_INFO |
| 437 | "Intel-IOMMU: disable GFX device mapping\n"); |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 438 | } else if (!strncmp(str, "forcedac", 8)) { |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 439 | printk(KERN_INFO |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 440 | "Intel-IOMMU: Forcing DAC for PCI devices\n"); |
| 441 | dmar_forcedac = 1; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 442 | } else if (!strncmp(str, "strict", 6)) { |
| 443 | printk(KERN_INFO |
| 444 | "Intel-IOMMU: disable batched IOTLB flush\n"); |
| 445 | intel_iommu_strict = 1; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 446 | } else if (!strncmp(str, "sp_off", 6)) { |
| 447 | printk(KERN_INFO |
| 448 | "Intel-IOMMU: disable supported super page\n"); |
| 449 | intel_iommu_superpage = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | str += strcspn(str, ","); |
| 453 | while (*str == ',') |
| 454 | str++; |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | __setup("intel_iommu=", intel_iommu_setup); |
| 459 | |
| 460 | static struct kmem_cache *iommu_domain_cache; |
| 461 | static struct kmem_cache *iommu_devinfo_cache; |
| 462 | static struct kmem_cache *iommu_iova_cache; |
| 463 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 464 | static inline void *alloc_pgtable_page(int node) |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 465 | { |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 466 | struct page *page; |
| 467 | void *vaddr = NULL; |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 468 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 469 | page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0); |
| 470 | if (page) |
| 471 | vaddr = page_address(page); |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 472 | return vaddr; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static inline void free_pgtable_page(void *vaddr) |
| 476 | { |
| 477 | free_page((unsigned long)vaddr); |
| 478 | } |
| 479 | |
| 480 | static inline void *alloc_domain_mem(void) |
| 481 | { |
KOSAKI Motohiro | 354bb65 | 2009-11-17 16:21:09 +0900 | [diff] [blame] | 482 | return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 485 | static void free_domain_mem(void *vaddr) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 486 | { |
| 487 | kmem_cache_free(iommu_domain_cache, vaddr); |
| 488 | } |
| 489 | |
| 490 | static inline void * alloc_devinfo_mem(void) |
| 491 | { |
KOSAKI Motohiro | 354bb65 | 2009-11-17 16:21:09 +0900 | [diff] [blame] | 492 | return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static inline void free_devinfo_mem(void *vaddr) |
| 496 | { |
| 497 | kmem_cache_free(iommu_devinfo_cache, vaddr); |
| 498 | } |
| 499 | |
| 500 | struct iova *alloc_iova_mem(void) |
| 501 | { |
KOSAKI Motohiro | 354bb65 | 2009-11-17 16:21:09 +0900 | [diff] [blame] | 502 | return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void free_iova_mem(struct iova *iova) |
| 506 | { |
| 507 | kmem_cache_free(iommu_iova_cache, iova); |
| 508 | } |
| 509 | |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 510 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 511 | static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw) |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 512 | { |
| 513 | unsigned long sagaw; |
| 514 | int agaw = -1; |
| 515 | |
| 516 | sagaw = cap_sagaw(iommu->cap); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 517 | for (agaw = width_to_agaw(max_gaw); |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 518 | agaw >= 0; agaw--) { |
| 519 | if (test_bit(agaw, &sagaw)) |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | return agaw; |
| 524 | } |
| 525 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 526 | /* |
| 527 | * Calculate max SAGAW for each iommu. |
| 528 | */ |
| 529 | int iommu_calculate_max_sagaw(struct intel_iommu *iommu) |
| 530 | { |
| 531 | return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * calculate agaw for each iommu. |
| 536 | * "SAGAW" may be different across iommus, use a default agaw, and |
| 537 | * get a supported less agaw for iommus that don't support the default agaw. |
| 538 | */ |
| 539 | int iommu_calculate_agaw(struct intel_iommu *iommu) |
| 540 | { |
| 541 | return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 542 | } |
| 543 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 544 | /* This functionin only returns single iommu in a domain */ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 545 | static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain) |
| 546 | { |
| 547 | int iommu_id; |
| 548 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 549 | /* si_domain and vm domain should not get here. */ |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 550 | BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 551 | BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY); |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 552 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 553 | iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus); |
| 554 | if (iommu_id < 0 || iommu_id >= g_num_of_iommus) |
| 555 | return NULL; |
| 556 | |
| 557 | return g_iommus[iommu_id]; |
| 558 | } |
| 559 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 560 | static void domain_update_iommu_coherency(struct dmar_domain *domain) |
| 561 | { |
| 562 | int i; |
| 563 | |
| 564 | domain->iommu_coherency = 1; |
| 565 | |
Akinobu Mita | a45946a | 2010-03-11 14:04:08 -0800 | [diff] [blame] | 566 | for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) { |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 567 | if (!ecap_coherent(g_iommus[i]->ecap)) { |
| 568 | domain->iommu_coherency = 0; |
| 569 | break; |
| 570 | } |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 574 | static void domain_update_iommu_snooping(struct dmar_domain *domain) |
| 575 | { |
| 576 | int i; |
| 577 | |
| 578 | domain->iommu_snooping = 1; |
| 579 | |
Akinobu Mita | a45946a | 2010-03-11 14:04:08 -0800 | [diff] [blame] | 580 | for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) { |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 581 | if (!ecap_sc_support(g_iommus[i]->ecap)) { |
| 582 | domain->iommu_snooping = 0; |
| 583 | break; |
| 584 | } |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 588 | static void domain_update_iommu_superpage(struct dmar_domain *domain) |
| 589 | { |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 590 | struct dmar_drhd_unit *drhd; |
| 591 | struct intel_iommu *iommu = NULL; |
| 592 | int mask = 0xf; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 593 | |
| 594 | if (!intel_iommu_superpage) { |
| 595 | domain->iommu_superpage = 0; |
| 596 | return; |
| 597 | } |
| 598 | |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 599 | /* set iommu_superpage to the smallest common denominator */ |
| 600 | for_each_active_iommu(iommu, drhd) { |
| 601 | mask &= cap_super_page_val(iommu->cap); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 602 | if (!mask) { |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | domain->iommu_superpage = fls(mask); |
| 607 | } |
| 608 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 609 | /* Some capabilities may be different across iommus */ |
| 610 | static void domain_update_iommu_cap(struct dmar_domain *domain) |
| 611 | { |
| 612 | domain_update_iommu_coherency(domain); |
| 613 | domain_update_iommu_snooping(domain); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 614 | domain_update_iommu_superpage(domain); |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 615 | } |
| 616 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 617 | 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] | 618 | { |
| 619 | struct dmar_drhd_unit *drhd = NULL; |
| 620 | int i; |
| 621 | |
| 622 | for_each_drhd_unit(drhd) { |
| 623 | if (drhd->ignored) |
| 624 | continue; |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 625 | if (segment != drhd->segment) |
| 626 | continue; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 627 | |
David Woodhouse | 924b623 | 2009-04-04 00:39:25 +0100 | [diff] [blame] | 628 | for (i = 0; i < drhd->devices_cnt; i++) { |
Dirk Hohndel | 288e487 | 2009-01-11 15:33:51 +0000 | [diff] [blame] | 629 | if (drhd->devices[i] && |
| 630 | drhd->devices[i]->bus->number == bus && |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 631 | drhd->devices[i]->devfn == devfn) |
| 632 | return drhd->iommu; |
David Woodhouse | 4958c5d | 2009-04-06 13:30:01 -0700 | [diff] [blame] | 633 | if (drhd->devices[i] && |
| 634 | drhd->devices[i]->subordinate && |
David Woodhouse | 924b623 | 2009-04-04 00:39:25 +0100 | [diff] [blame] | 635 | drhd->devices[i]->subordinate->number <= bus && |
| 636 | drhd->devices[i]->subordinate->subordinate >= bus) |
| 637 | return drhd->iommu; |
| 638 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 639 | |
| 640 | if (drhd->include_all) |
| 641 | return drhd->iommu; |
| 642 | } |
| 643 | |
| 644 | return NULL; |
| 645 | } |
| 646 | |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 647 | static void domain_flush_cache(struct dmar_domain *domain, |
| 648 | void *addr, int size) |
| 649 | { |
| 650 | if (!domain->iommu_coherency) |
| 651 | clflush_cache_range(addr, size); |
| 652 | } |
| 653 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 654 | /* Gets context entry for a given bus and devfn */ |
| 655 | static struct context_entry * device_to_context_entry(struct intel_iommu *iommu, |
| 656 | u8 bus, u8 devfn) |
| 657 | { |
| 658 | struct root_entry *root; |
| 659 | struct context_entry *context; |
| 660 | unsigned long phy_addr; |
| 661 | unsigned long flags; |
| 662 | |
| 663 | spin_lock_irqsave(&iommu->lock, flags); |
| 664 | root = &iommu->root_entry[bus]; |
| 665 | context = get_context_addr_from_root(root); |
| 666 | if (!context) { |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 667 | context = (struct context_entry *) |
| 668 | alloc_pgtable_page(iommu->node); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 669 | if (!context) { |
| 670 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 671 | return NULL; |
| 672 | } |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 673 | __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 674 | phy_addr = virt_to_phys((void *)context); |
| 675 | set_root_value(root, phy_addr); |
| 676 | set_root_present(root); |
| 677 | __iommu_flush_cache(iommu, root, sizeof(*root)); |
| 678 | } |
| 679 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 680 | return &context[devfn]; |
| 681 | } |
| 682 | |
| 683 | static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn) |
| 684 | { |
| 685 | struct root_entry *root; |
| 686 | struct context_entry *context; |
| 687 | int ret; |
| 688 | unsigned long flags; |
| 689 | |
| 690 | spin_lock_irqsave(&iommu->lock, flags); |
| 691 | root = &iommu->root_entry[bus]; |
| 692 | context = get_context_addr_from_root(root); |
| 693 | if (!context) { |
| 694 | ret = 0; |
| 695 | goto out; |
| 696 | } |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 697 | ret = context_present(&context[devfn]); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 698 | out: |
| 699 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn) |
| 704 | { |
| 705 | struct root_entry *root; |
| 706 | struct context_entry *context; |
| 707 | unsigned long flags; |
| 708 | |
| 709 | spin_lock_irqsave(&iommu->lock, flags); |
| 710 | root = &iommu->root_entry[bus]; |
| 711 | context = get_context_addr_from_root(root); |
| 712 | if (context) { |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 713 | context_clear_entry(&context[devfn]); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 714 | __iommu_flush_cache(iommu, &context[devfn], \ |
| 715 | sizeof(*context)); |
| 716 | } |
| 717 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 718 | } |
| 719 | |
| 720 | static void free_context_table(struct intel_iommu *iommu) |
| 721 | { |
| 722 | struct root_entry *root; |
| 723 | int i; |
| 724 | unsigned long flags; |
| 725 | struct context_entry *context; |
| 726 | |
| 727 | spin_lock_irqsave(&iommu->lock, flags); |
| 728 | if (!iommu->root_entry) { |
| 729 | goto out; |
| 730 | } |
| 731 | for (i = 0; i < ROOT_ENTRY_NR; i++) { |
| 732 | root = &iommu->root_entry[i]; |
| 733 | context = get_context_addr_from_root(root); |
| 734 | if (context) |
| 735 | free_pgtable_page(context); |
| 736 | } |
| 737 | free_pgtable_page(iommu->root_entry); |
| 738 | iommu->root_entry = NULL; |
| 739 | out: |
| 740 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 741 | } |
| 742 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 743 | static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 744 | unsigned long pfn, int target_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 745 | { |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 746 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 747 | struct dma_pte *parent, *pte = NULL; |
| 748 | int level = agaw_to_level(domain->agaw); |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 749 | int offset; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 750 | |
| 751 | BUG_ON(!domain->pgd); |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 752 | BUG_ON(addr_width < BITS_PER_LONG && pfn >> addr_width); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 753 | parent = domain->pgd; |
| 754 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 755 | while (level > 0) { |
| 756 | void *tmp_page; |
| 757 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 758 | offset = pfn_level_offset(pfn, level); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 759 | pte = &parent[offset]; |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 760 | if (!target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte))) |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 761 | break; |
| 762 | if (level == target_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 763 | break; |
| 764 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 765 | if (!dma_pte_present(pte)) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 766 | uint64_t pteval; |
| 767 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 768 | tmp_page = alloc_pgtable_page(domain->nid); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 769 | |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame] | 770 | if (!tmp_page) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 771 | return NULL; |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame] | 772 | |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 773 | domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE); |
Benjamin LaHaise | 64de5af | 2009-09-16 21:05:55 -0400 | [diff] [blame] | 774 | pteval = ((uint64_t)virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE; |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 775 | if (cmpxchg64(&pte->val, 0ULL, pteval)) { |
| 776 | /* Someone else set it while we were thinking; use theirs. */ |
| 777 | free_pgtable_page(tmp_page); |
| 778 | } else { |
| 779 | dma_pte_addr(pte); |
| 780 | domain_flush_cache(domain, pte, sizeof(*pte)); |
| 781 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 782 | } |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 783 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 784 | level--; |
| 785 | } |
| 786 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 787 | return pte; |
| 788 | } |
| 789 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 790 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 791 | /* return address's pte at specific level */ |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 792 | static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain, |
| 793 | unsigned long pfn, |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 794 | int level, int *large_page) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 795 | { |
| 796 | struct dma_pte *parent, *pte = NULL; |
| 797 | int total = agaw_to_level(domain->agaw); |
| 798 | int offset; |
| 799 | |
| 800 | parent = domain->pgd; |
| 801 | while (level <= total) { |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 802 | offset = pfn_level_offset(pfn, total); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 803 | pte = &parent[offset]; |
| 804 | if (level == total) |
| 805 | return pte; |
| 806 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 807 | if (!dma_pte_present(pte)) { |
| 808 | *large_page = total; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 809 | break; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | if (pte->val & DMA_PTE_LARGE_PAGE) { |
| 813 | *large_page = total; |
| 814 | return pte; |
| 815 | } |
| 816 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 817 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 818 | total--; |
| 819 | } |
| 820 | return NULL; |
| 821 | } |
| 822 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 823 | /* clear last level pte, a tlb flush should be followed */ |
Allen Kay | 292827c | 2011-10-14 12:31:54 -0700 | [diff] [blame] | 824 | static int dma_pte_clear_range(struct dmar_domain *domain, |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 825 | unsigned long start_pfn, |
| 826 | unsigned long last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 827 | { |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 828 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 829 | unsigned int large_page = 1; |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 830 | struct dma_pte *first_pte, *pte; |
Allen Kay | 292827c | 2011-10-14 12:31:54 -0700 | [diff] [blame] | 831 | int order; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 832 | |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 833 | BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width); |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 834 | BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 835 | BUG_ON(start_pfn > last_pfn); |
David Woodhouse | 66eae84 | 2009-06-27 19:00:32 +0100 | [diff] [blame] | 836 | |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 837 | /* we don't need lock here; nobody else touches the iova range */ |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 838 | do { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 839 | large_page = 1; |
| 840 | first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page); |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 841 | if (!pte) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 842 | start_pfn = align_to_level(start_pfn + 1, large_page + 1); |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 843 | continue; |
| 844 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 845 | do { |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 846 | dma_clear_pte(pte); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 847 | start_pfn += lvl_to_nr_pages(large_page); |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 848 | pte++; |
David Woodhouse | 75e6bf9 | 2009-07-02 11:21:16 +0100 | [diff] [blame] | 849 | } while (start_pfn <= last_pfn && !first_pte_in_page(pte)); |
| 850 | |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 851 | domain_flush_cache(domain, first_pte, |
| 852 | (void *)pte - (void *)first_pte); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 853 | |
| 854 | } while (start_pfn && start_pfn <= last_pfn); |
Allen Kay | 292827c | 2011-10-14 12:31:54 -0700 | [diff] [blame] | 855 | |
| 856 | order = (large_page - 1) * 9; |
| 857 | return order; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* free page table pages. last level pte should already be cleared */ |
| 861 | static void dma_pte_free_pagetable(struct dmar_domain *domain, |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 862 | unsigned long start_pfn, |
| 863 | unsigned long last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 864 | { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 865 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 866 | struct dma_pte *first_pte, *pte; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 867 | int total = agaw_to_level(domain->agaw); |
| 868 | int level; |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 869 | unsigned long tmp; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 870 | int large_page = 2; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 871 | |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 872 | BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width); |
| 873 | BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 874 | BUG_ON(start_pfn > last_pfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 875 | |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 876 | /* We don't need lock here; nobody else touches the iova range */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 877 | level = 2; |
| 878 | while (level <= total) { |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 879 | tmp = align_to_level(start_pfn, level); |
| 880 | |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 881 | /* If we can't even clear one PTE at this level, we're done */ |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 882 | if (tmp + level_size(level) - 1 > last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 883 | return; |
| 884 | |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 885 | do { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 886 | large_page = level; |
| 887 | first_pte = pte = dma_pfn_level_pte(domain, tmp, level, &large_page); |
| 888 | if (large_page > level) |
| 889 | level = large_page + 1; |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 890 | if (!pte) { |
| 891 | tmp = align_to_level(tmp + 1, level + 1); |
| 892 | continue; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 893 | } |
David Woodhouse | 75e6bf9 | 2009-07-02 11:21:16 +0100 | [diff] [blame] | 894 | do { |
David Woodhouse | 6a43e57 | 2009-07-02 12:02:34 +0100 | [diff] [blame] | 895 | if (dma_pte_present(pte)) { |
| 896 | free_pgtable_page(phys_to_virt(dma_pte_addr(pte))); |
| 897 | dma_clear_pte(pte); |
| 898 | } |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 899 | pte++; |
| 900 | tmp += level_size(level); |
David Woodhouse | 75e6bf9 | 2009-07-02 11:21:16 +0100 | [diff] [blame] | 901 | } while (!first_pte_in_page(pte) && |
| 902 | tmp + level_size(level) - 1 <= last_pfn); |
| 903 | |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 904 | domain_flush_cache(domain, first_pte, |
| 905 | (void *)pte - (void *)first_pte); |
| 906 | |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 907 | } while (tmp && tmp + level_size(level) - 1 <= last_pfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 908 | level++; |
| 909 | } |
| 910 | /* free pgd */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 911 | if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 912 | free_pgtable_page(domain->pgd); |
| 913 | domain->pgd = NULL; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /* iommu handling */ |
| 918 | static int iommu_alloc_root_entry(struct intel_iommu *iommu) |
| 919 | { |
| 920 | struct root_entry *root; |
| 921 | unsigned long flags; |
| 922 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 923 | root = (struct root_entry *)alloc_pgtable_page(iommu->node); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 924 | if (!root) |
| 925 | return -ENOMEM; |
| 926 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 927 | __iommu_flush_cache(iommu, root, ROOT_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 928 | |
| 929 | spin_lock_irqsave(&iommu->lock, flags); |
| 930 | iommu->root_entry = root; |
| 931 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 936 | static void iommu_set_root_entry(struct intel_iommu *iommu) |
| 937 | { |
| 938 | void *addr; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 939 | u32 sts; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 940 | unsigned long flag; |
| 941 | |
| 942 | addr = iommu->root_entry; |
| 943 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 944 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 945 | dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr)); |
| 946 | |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 947 | writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 948 | |
| 949 | /* Make sure hardware complete it */ |
| 950 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 951 | readl, (sts & DMA_GSTS_RTPS), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 952 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 953 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static void iommu_flush_write_buffer(struct intel_iommu *iommu) |
| 957 | { |
| 958 | u32 val; |
| 959 | unsigned long flag; |
| 960 | |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 961 | if (!rwbf_quirk && !cap_rwbf(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 962 | return; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 963 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 964 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
David Woodhouse | 462b60f | 2009-05-10 20:18:18 +0100 | [diff] [blame] | 965 | writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 966 | |
| 967 | /* Make sure hardware complete it */ |
| 968 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 969 | readl, (!(val & DMA_GSTS_WBFS)), val); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 970 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 971 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 975 | static void __iommu_flush_context(struct intel_iommu *iommu, |
| 976 | u16 did, u16 source_id, u8 function_mask, |
| 977 | u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 978 | { |
| 979 | u64 val = 0; |
| 980 | unsigned long flag; |
| 981 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 982 | switch (type) { |
| 983 | case DMA_CCMD_GLOBAL_INVL: |
| 984 | val = DMA_CCMD_GLOBAL_INVL; |
| 985 | break; |
| 986 | case DMA_CCMD_DOMAIN_INVL: |
| 987 | val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did); |
| 988 | break; |
| 989 | case DMA_CCMD_DEVICE_INVL: |
| 990 | val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did) |
| 991 | | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask); |
| 992 | break; |
| 993 | default: |
| 994 | BUG(); |
| 995 | } |
| 996 | val |= DMA_CCMD_ICC; |
| 997 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 998 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 999 | dmar_writeq(iommu->reg + DMAR_CCMD_REG, val); |
| 1000 | |
| 1001 | /* Make sure hardware complete it */ |
| 1002 | IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG, |
| 1003 | dmar_readq, (!(val & DMA_CCMD_ICC)), val); |
| 1004 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1005 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1008 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1009 | static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, |
| 1010 | u64 addr, unsigned int size_order, u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1011 | { |
| 1012 | int tlb_offset = ecap_iotlb_offset(iommu->ecap); |
| 1013 | u64 val = 0, val_iva = 0; |
| 1014 | unsigned long flag; |
| 1015 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1016 | switch (type) { |
| 1017 | case DMA_TLB_GLOBAL_FLUSH: |
| 1018 | /* global flush doesn't need set IVA_REG */ |
| 1019 | val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT; |
| 1020 | break; |
| 1021 | case DMA_TLB_DSI_FLUSH: |
| 1022 | val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
| 1023 | break; |
| 1024 | case DMA_TLB_PSI_FLUSH: |
| 1025 | val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
| 1026 | /* Note: always flush non-leaf currently */ |
| 1027 | val_iva = size_order | addr; |
| 1028 | break; |
| 1029 | default: |
| 1030 | BUG(); |
| 1031 | } |
| 1032 | /* Note: set drain read/write */ |
| 1033 | #if 0 |
| 1034 | /* |
| 1035 | * This is probably to be super secure.. Looks like we can |
| 1036 | * ignore it without any impact. |
| 1037 | */ |
| 1038 | if (cap_read_drain(iommu->cap)) |
| 1039 | val |= DMA_TLB_READ_DRAIN; |
| 1040 | #endif |
| 1041 | if (cap_write_drain(iommu->cap)) |
| 1042 | val |= DMA_TLB_WRITE_DRAIN; |
| 1043 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1044 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1045 | /* Note: Only uses first TLB reg currently */ |
| 1046 | if (val_iva) |
| 1047 | dmar_writeq(iommu->reg + tlb_offset, val_iva); |
| 1048 | dmar_writeq(iommu->reg + tlb_offset + 8, val); |
| 1049 | |
| 1050 | /* Make sure hardware complete it */ |
| 1051 | IOMMU_WAIT_OP(iommu, tlb_offset + 8, |
| 1052 | dmar_readq, (!(val & DMA_TLB_IVT)), val); |
| 1053 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1054 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* check IOTLB invalidation granularity */ |
| 1057 | if (DMA_TLB_IAIG(val) == 0) |
| 1058 | printk(KERN_ERR"IOMMU: flush IOTLB failed\n"); |
| 1059 | if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type)) |
| 1060 | pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1061 | (unsigned long long)DMA_TLB_IIRG(type), |
| 1062 | (unsigned long long)DMA_TLB_IAIG(val)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1065 | static struct device_domain_info *iommu_support_dev_iotlb( |
| 1066 | struct dmar_domain *domain, int segment, u8 bus, u8 devfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1067 | { |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1068 | int found = 0; |
| 1069 | unsigned long flags; |
| 1070 | struct device_domain_info *info; |
| 1071 | struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn); |
| 1072 | |
| 1073 | if (!ecap_dev_iotlb_support(iommu->ecap)) |
| 1074 | return NULL; |
| 1075 | |
| 1076 | if (!iommu->qi) |
| 1077 | return NULL; |
| 1078 | |
| 1079 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1080 | list_for_each_entry(info, &domain->devices, link) |
| 1081 | if (info->bus == bus && info->devfn == devfn) { |
| 1082 | found = 1; |
| 1083 | break; |
| 1084 | } |
| 1085 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1086 | |
| 1087 | if (!found || !info->dev) |
| 1088 | return NULL; |
| 1089 | |
| 1090 | if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS)) |
| 1091 | return NULL; |
| 1092 | |
| 1093 | if (!dmar_find_matched_atsr_unit(info->dev)) |
| 1094 | return NULL; |
| 1095 | |
| 1096 | info->iommu = iommu; |
| 1097 | |
| 1098 | return info; |
| 1099 | } |
| 1100 | |
| 1101 | static void iommu_enable_dev_iotlb(struct device_domain_info *info) |
| 1102 | { |
| 1103 | if (!info) |
| 1104 | return; |
| 1105 | |
| 1106 | pci_enable_ats(info->dev, VTD_PAGE_SHIFT); |
| 1107 | } |
| 1108 | |
| 1109 | static void iommu_disable_dev_iotlb(struct device_domain_info *info) |
| 1110 | { |
| 1111 | if (!info->dev || !pci_ats_enabled(info->dev)) |
| 1112 | return; |
| 1113 | |
| 1114 | pci_disable_ats(info->dev); |
| 1115 | } |
| 1116 | |
| 1117 | static void iommu_flush_dev_iotlb(struct dmar_domain *domain, |
| 1118 | u64 addr, unsigned mask) |
| 1119 | { |
| 1120 | u16 sid, qdep; |
| 1121 | unsigned long flags; |
| 1122 | struct device_domain_info *info; |
| 1123 | |
| 1124 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1125 | list_for_each_entry(info, &domain->devices, link) { |
| 1126 | if (!info->dev || !pci_ats_enabled(info->dev)) |
| 1127 | continue; |
| 1128 | |
| 1129 | sid = info->bus << 8 | info->devfn; |
| 1130 | qdep = pci_ats_queue_depth(info->dev); |
| 1131 | qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask); |
| 1132 | } |
| 1133 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1134 | } |
| 1135 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1136 | static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did, |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1137 | unsigned long pfn, unsigned int pages, int map) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1138 | { |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1139 | unsigned int mask = ilog2(__roundup_pow_of_two(pages)); |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 1140 | uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1141 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1142 | BUG_ON(pages == 0); |
| 1143 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1144 | /* |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1145 | * Fallback to domain selective flush if no PSI support or the size is |
| 1146 | * too big. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1147 | * PSI requires page size to be 2 ^ x, and the base address is naturally |
| 1148 | * aligned to the size |
| 1149 | */ |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1150 | if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap)) |
| 1151 | iommu->flush.flush_iotlb(iommu, did, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1152 | DMA_TLB_DSI_FLUSH); |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1153 | else |
| 1154 | iommu->flush.flush_iotlb(iommu, did, addr, mask, |
| 1155 | DMA_TLB_PSI_FLUSH); |
Yu Zhao | bf92df3 | 2009-06-29 11:31:45 +0800 | [diff] [blame] | 1156 | |
| 1157 | /* |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1158 | * In caching mode, changes of pages from non-present to present require |
| 1159 | * flush. However, device IOTLB doesn't need to be flushed in this case. |
Yu Zhao | bf92df3 | 2009-06-29 11:31:45 +0800 | [diff] [blame] | 1160 | */ |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1161 | if (!cap_caching_mode(iommu->cap) || !map) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1162 | iommu_flush_dev_iotlb(iommu->domains[did], addr, mask); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1165 | static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu) |
| 1166 | { |
| 1167 | u32 pmen; |
| 1168 | unsigned long flags; |
| 1169 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1170 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1171 | pmen = readl(iommu->reg + DMAR_PMEN_REG); |
| 1172 | pmen &= ~DMA_PMEN_EPM; |
| 1173 | writel(pmen, iommu->reg + DMAR_PMEN_REG); |
| 1174 | |
| 1175 | /* wait for the protected region status bit to clear */ |
| 1176 | IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG, |
| 1177 | readl, !(pmen & DMA_PMEN_PRS), pmen); |
| 1178 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1179 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1182 | static int iommu_enable_translation(struct intel_iommu *iommu) |
| 1183 | { |
| 1184 | u32 sts; |
| 1185 | unsigned long flags; |
| 1186 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1187 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1188 | iommu->gcmd |= DMA_GCMD_TE; |
| 1189 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1190 | |
| 1191 | /* Make sure hardware complete it */ |
| 1192 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1193 | readl, (sts & DMA_GSTS_TES), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1194 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1195 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | static int iommu_disable_translation(struct intel_iommu *iommu) |
| 1200 | { |
| 1201 | u32 sts; |
| 1202 | unsigned long flag; |
| 1203 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1204 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1205 | iommu->gcmd &= ~DMA_GCMD_TE; |
| 1206 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 1207 | |
| 1208 | /* Make sure hardware complete it */ |
| 1209 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1210 | readl, (!(sts & DMA_GSTS_TES)), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1211 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1212 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1213 | return 0; |
| 1214 | } |
| 1215 | |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 1216 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1217 | static int iommu_init_domains(struct intel_iommu *iommu) |
| 1218 | { |
| 1219 | unsigned long ndomains; |
| 1220 | unsigned long nlongs; |
| 1221 | |
| 1222 | ndomains = cap_ndoms(iommu->cap); |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 1223 | pr_debug("IOMMU %d: Number of Domains supportd <%ld>\n", iommu->seq_id, |
| 1224 | ndomains); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1225 | nlongs = BITS_TO_LONGS(ndomains); |
| 1226 | |
Donald Dutile | 94a91b50 | 2009-08-20 16:51:34 -0400 | [diff] [blame] | 1227 | spin_lock_init(&iommu->lock); |
| 1228 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1229 | /* TBD: there might be 64K domains, |
| 1230 | * consider other allocation for future chip |
| 1231 | */ |
| 1232 | iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL); |
| 1233 | if (!iommu->domain_ids) { |
| 1234 | printk(KERN_ERR "Allocating domain id array failed\n"); |
| 1235 | return -ENOMEM; |
| 1236 | } |
| 1237 | iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *), |
| 1238 | GFP_KERNEL); |
| 1239 | if (!iommu->domains) { |
| 1240 | printk(KERN_ERR "Allocating domain array failed\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1241 | return -ENOMEM; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * if Caching mode is set, then invalid translations are tagged |
| 1246 | * with domainid 0. Hence we need to pre-allocate it. |
| 1247 | */ |
| 1248 | if (cap_caching_mode(iommu->cap)) |
| 1249 | set_bit(0, iommu->domain_ids); |
| 1250 | return 0; |
| 1251 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1252 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1253 | |
| 1254 | static void domain_exit(struct dmar_domain *domain); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 1255 | static void vm_domain_exit(struct dmar_domain *domain); |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 1256 | |
| 1257 | void free_dmar_iommu(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1258 | { |
| 1259 | struct dmar_domain *domain; |
| 1260 | int i; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1261 | unsigned long flags; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1262 | |
Donald Dutile | 94a91b50 | 2009-08-20 16:51:34 -0400 | [diff] [blame] | 1263 | if ((iommu->domains) && (iommu->domain_ids)) { |
Akinobu Mita | a45946a | 2010-03-11 14:04:08 -0800 | [diff] [blame] | 1264 | for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) { |
Donald Dutile | 94a91b50 | 2009-08-20 16:51:34 -0400 | [diff] [blame] | 1265 | domain = iommu->domains[i]; |
| 1266 | clear_bit(i, iommu->domain_ids); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1267 | |
Donald Dutile | 94a91b50 | 2009-08-20 16:51:34 -0400 | [diff] [blame] | 1268 | spin_lock_irqsave(&domain->iommu_lock, flags); |
| 1269 | if (--domain->iommu_count == 0) { |
| 1270 | if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) |
| 1271 | vm_domain_exit(domain); |
| 1272 | else |
| 1273 | domain_exit(domain); |
| 1274 | } |
| 1275 | spin_unlock_irqrestore(&domain->iommu_lock, flags); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 1276 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | if (iommu->gcmd & DMA_GCMD_TE) |
| 1280 | iommu_disable_translation(iommu); |
| 1281 | |
| 1282 | if (iommu->irq) { |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 1283 | irq_set_handler_data(iommu->irq, NULL); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1284 | /* This will mask the irq */ |
| 1285 | free_irq(iommu->irq, iommu); |
| 1286 | destroy_irq(iommu->irq); |
| 1287 | } |
| 1288 | |
| 1289 | kfree(iommu->domains); |
| 1290 | kfree(iommu->domain_ids); |
| 1291 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 1292 | g_iommus[iommu->seq_id] = NULL; |
| 1293 | |
| 1294 | /* if all iommus are freed, free g_iommus */ |
| 1295 | for (i = 0; i < g_num_of_iommus; i++) { |
| 1296 | if (g_iommus[i]) |
| 1297 | break; |
| 1298 | } |
| 1299 | |
| 1300 | if (i == g_num_of_iommus) |
| 1301 | kfree(g_iommus); |
| 1302 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1303 | /* free context mapping */ |
| 1304 | free_context_table(iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1307 | static struct dmar_domain *alloc_domain(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1308 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1309 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1310 | |
| 1311 | domain = alloc_domain_mem(); |
| 1312 | if (!domain) |
| 1313 | return NULL; |
| 1314 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1315 | domain->nid = -1; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1316 | memset(&domain->iommu_bmp, 0, sizeof(unsigned long)); |
Weidong Han | d71a2f3 | 2008-12-07 21:13:41 +0800 | [diff] [blame] | 1317 | domain->flags = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1318 | |
| 1319 | return domain; |
| 1320 | } |
| 1321 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1322 | static int iommu_attach_domain(struct dmar_domain *domain, |
| 1323 | struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1324 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1325 | int num; |
| 1326 | unsigned long ndomains; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1327 | unsigned long flags; |
| 1328 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1329 | ndomains = cap_ndoms(iommu->cap); |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1330 | |
| 1331 | spin_lock_irqsave(&iommu->lock, flags); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1332 | |
| 1333 | num = find_first_zero_bit(iommu->domain_ids, ndomains); |
| 1334 | if (num >= ndomains) { |
| 1335 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1336 | printk(KERN_ERR "IOMMU: no free domain ids\n"); |
| 1337 | return -ENOMEM; |
| 1338 | } |
| 1339 | |
| 1340 | domain->id = num; |
| 1341 | set_bit(num, iommu->domain_ids); |
| 1342 | set_bit(iommu->seq_id, &domain->iommu_bmp); |
| 1343 | iommu->domains[num] = domain; |
| 1344 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1345 | |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static void iommu_detach_domain(struct dmar_domain *domain, |
| 1350 | struct intel_iommu *iommu) |
| 1351 | { |
| 1352 | unsigned long flags; |
| 1353 | int num, ndomains; |
| 1354 | int found = 0; |
| 1355 | |
| 1356 | spin_lock_irqsave(&iommu->lock, flags); |
| 1357 | ndomains = cap_ndoms(iommu->cap); |
Akinobu Mita | a45946a | 2010-03-11 14:04:08 -0800 | [diff] [blame] | 1358 | for_each_set_bit(num, iommu->domain_ids, ndomains) { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1359 | if (iommu->domains[num] == domain) { |
| 1360 | found = 1; |
| 1361 | break; |
| 1362 | } |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | if (found) { |
| 1366 | clear_bit(num, iommu->domain_ids); |
| 1367 | clear_bit(iommu->seq_id, &domain->iommu_bmp); |
| 1368 | iommu->domains[num] = NULL; |
| 1369 | } |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1370 | spin_unlock_irqrestore(&iommu->lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | static struct iova_domain reserved_iova_list; |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1374 | static struct lock_class_key reserved_rbtree_key; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1375 | |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1376 | static int dmar_init_reserved_ranges(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1377 | { |
| 1378 | struct pci_dev *pdev = NULL; |
| 1379 | struct iova *iova; |
| 1380 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1381 | |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 1382 | init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1383 | |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1384 | lockdep_set_class(&reserved_iova_list.iova_rbtree_lock, |
| 1385 | &reserved_rbtree_key); |
| 1386 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1387 | /* IOAPIC ranges shouldn't be accessed by DMA */ |
| 1388 | iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START), |
| 1389 | IOVA_PFN(IOAPIC_RANGE_END)); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1390 | if (!iova) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1391 | printk(KERN_ERR "Reserve IOAPIC range failed\n"); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1392 | return -ENODEV; |
| 1393 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1394 | |
| 1395 | /* Reserve all PCI MMIO to avoid peer-to-peer access */ |
| 1396 | for_each_pci_dev(pdev) { |
| 1397 | struct resource *r; |
| 1398 | |
| 1399 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 1400 | r = &pdev->resource[i]; |
| 1401 | if (!r->flags || !(r->flags & IORESOURCE_MEM)) |
| 1402 | continue; |
David Woodhouse | 1a4a455 | 2009-06-28 16:00:42 +0100 | [diff] [blame] | 1403 | iova = reserve_iova(&reserved_iova_list, |
| 1404 | IOVA_PFN(r->start), |
| 1405 | IOVA_PFN(r->end)); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1406 | if (!iova) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1407 | printk(KERN_ERR "Reserve iova failed\n"); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1408 | return -ENODEV; |
| 1409 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1410 | } |
| 1411 | } |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1412 | return 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | static void domain_reserve_special_ranges(struct dmar_domain *domain) |
| 1416 | { |
| 1417 | copy_reserved_iova(&reserved_iova_list, &domain->iovad); |
| 1418 | } |
| 1419 | |
| 1420 | static inline int guestwidth_to_adjustwidth(int gaw) |
| 1421 | { |
| 1422 | int agaw; |
| 1423 | int r = (gaw - 12) % 9; |
| 1424 | |
| 1425 | if (r == 0) |
| 1426 | agaw = gaw; |
| 1427 | else |
| 1428 | agaw = gaw + 9 - r; |
| 1429 | if (agaw > 64) |
| 1430 | agaw = 64; |
| 1431 | return agaw; |
| 1432 | } |
| 1433 | |
| 1434 | static int domain_init(struct dmar_domain *domain, int guest_width) |
| 1435 | { |
| 1436 | struct intel_iommu *iommu; |
| 1437 | int adjust_width, agaw; |
| 1438 | unsigned long sagaw; |
| 1439 | |
David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 1440 | init_iova_domain(&domain->iovad, DMA_32BIT_PFN); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1441 | spin_lock_init(&domain->iommu_lock); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1442 | |
| 1443 | domain_reserve_special_ranges(domain); |
| 1444 | |
| 1445 | /* calculate AGAW */ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1446 | iommu = domain_get_iommu(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1447 | if (guest_width > cap_mgaw(iommu->cap)) |
| 1448 | guest_width = cap_mgaw(iommu->cap); |
| 1449 | domain->gaw = guest_width; |
| 1450 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 1451 | agaw = width_to_agaw(adjust_width); |
| 1452 | sagaw = cap_sagaw(iommu->cap); |
| 1453 | if (!test_bit(agaw, &sagaw)) { |
| 1454 | /* hardware doesn't support it, choose a bigger one */ |
| 1455 | pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw); |
| 1456 | agaw = find_next_bit(&sagaw, 5, agaw); |
| 1457 | if (agaw >= 5) |
| 1458 | return -ENODEV; |
| 1459 | } |
| 1460 | domain->agaw = agaw; |
| 1461 | INIT_LIST_HEAD(&domain->devices); |
| 1462 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 1463 | if (ecap_coherent(iommu->ecap)) |
| 1464 | domain->iommu_coherency = 1; |
| 1465 | else |
| 1466 | domain->iommu_coherency = 0; |
| 1467 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 1468 | if (ecap_sc_support(iommu->ecap)) |
| 1469 | domain->iommu_snooping = 1; |
| 1470 | else |
| 1471 | domain->iommu_snooping = 0; |
| 1472 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1473 | domain->iommu_superpage = fls(cap_super_page_val(iommu->cap)); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1474 | domain->iommu_count = 1; |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1475 | domain->nid = iommu->node; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1476 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1477 | /* always allocate the top pgd */ |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1478 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1479 | if (!domain->pgd) |
| 1480 | return -ENOMEM; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1481 | __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | static void domain_exit(struct dmar_domain *domain) |
| 1486 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1487 | struct dmar_drhd_unit *drhd; |
| 1488 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1489 | |
| 1490 | /* Domain 0 is reserved, so dont process it */ |
| 1491 | if (!domain) |
| 1492 | return; |
| 1493 | |
Alex Williamson | 7b66835 | 2011-05-24 12:02:41 +0100 | [diff] [blame] | 1494 | /* Flush any lazy unmaps that may reference this domain */ |
| 1495 | if (!intel_iommu_strict) |
| 1496 | flush_unmaps_timeout(0); |
| 1497 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1498 | domain_remove_dev_info(domain); |
| 1499 | /* destroy iovas */ |
| 1500 | put_iova_domain(&domain->iovad); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1501 | |
| 1502 | /* clear ptes */ |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 1503 | dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1504 | |
| 1505 | /* free page tables */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 1506 | dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1507 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1508 | for_each_active_iommu(iommu, drhd) |
| 1509 | if (test_bit(iommu->seq_id, &domain->iommu_bmp)) |
| 1510 | iommu_detach_domain(domain, iommu); |
| 1511 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1512 | free_domain_mem(domain); |
| 1513 | } |
| 1514 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1515 | static int domain_context_mapping_one(struct dmar_domain *domain, int segment, |
| 1516 | u8 bus, u8 devfn, int translation) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1517 | { |
| 1518 | struct context_entry *context; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1519 | unsigned long flags; |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1520 | struct intel_iommu *iommu; |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1521 | struct dma_pte *pgd; |
| 1522 | unsigned long num; |
| 1523 | unsigned long ndomains; |
| 1524 | int id; |
| 1525 | int agaw; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1526 | struct device_domain_info *info = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1527 | |
| 1528 | pr_debug("Set context mapping for %02x:%02x.%d\n", |
| 1529 | bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1530 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1531 | BUG_ON(!domain->pgd); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1532 | BUG_ON(translation != CONTEXT_TT_PASS_THROUGH && |
| 1533 | translation != CONTEXT_TT_MULTI_LEVEL); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1534 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1535 | iommu = device_to_iommu(segment, bus, devfn); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1536 | if (!iommu) |
| 1537 | return -ENODEV; |
| 1538 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1539 | context = device_to_context_entry(iommu, bus, devfn); |
| 1540 | if (!context) |
| 1541 | return -ENOMEM; |
| 1542 | spin_lock_irqsave(&iommu->lock, flags); |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 1543 | if (context_present(context)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1544 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1548 | id = domain->id; |
| 1549 | pgd = domain->pgd; |
| 1550 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1551 | if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE || |
| 1552 | domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) { |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1553 | int found = 0; |
| 1554 | |
| 1555 | /* find an available domain id for this device in iommu */ |
| 1556 | ndomains = cap_ndoms(iommu->cap); |
Akinobu Mita | a45946a | 2010-03-11 14:04:08 -0800 | [diff] [blame] | 1557 | for_each_set_bit(num, iommu->domain_ids, ndomains) { |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1558 | if (iommu->domains[num] == domain) { |
| 1559 | id = num; |
| 1560 | found = 1; |
| 1561 | break; |
| 1562 | } |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | if (found == 0) { |
| 1566 | num = find_first_zero_bit(iommu->domain_ids, ndomains); |
| 1567 | if (num >= ndomains) { |
| 1568 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1569 | printk(KERN_ERR "IOMMU: no free domain ids\n"); |
| 1570 | return -EFAULT; |
| 1571 | } |
| 1572 | |
| 1573 | set_bit(num, iommu->domain_ids); |
| 1574 | iommu->domains[num] = domain; |
| 1575 | id = num; |
| 1576 | } |
| 1577 | |
| 1578 | /* Skip top levels of page tables for |
| 1579 | * iommu which has less agaw than default. |
Chris Wright | 1672af1 | 2009-12-02 12:06:34 -0800 | [diff] [blame] | 1580 | * Unnecessary for PT mode. |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1581 | */ |
Chris Wright | 1672af1 | 2009-12-02 12:06:34 -0800 | [diff] [blame] | 1582 | if (translation != CONTEXT_TT_PASS_THROUGH) { |
| 1583 | for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) { |
| 1584 | pgd = phys_to_virt(dma_pte_addr(pgd)); |
| 1585 | if (!dma_pte_present(pgd)) { |
| 1586 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1587 | return -ENOMEM; |
| 1588 | } |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | context_set_domain_id(context, id); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1594 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1595 | if (translation != CONTEXT_TT_PASS_THROUGH) { |
| 1596 | info = iommu_support_dev_iotlb(domain, segment, bus, devfn); |
| 1597 | translation = info ? CONTEXT_TT_DEV_IOTLB : |
| 1598 | CONTEXT_TT_MULTI_LEVEL; |
| 1599 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1600 | /* |
| 1601 | * In pass through mode, AW must be programmed to indicate the largest |
| 1602 | * AGAW value supported by hardware. And ASR is ignored by hardware. |
| 1603 | */ |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1604 | if (unlikely(translation == CONTEXT_TT_PASS_THROUGH)) |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1605 | context_set_address_width(context, iommu->msagaw); |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1606 | else { |
| 1607 | context_set_address_root(context, virt_to_phys(pgd)); |
| 1608 | context_set_address_width(context, iommu->agaw); |
| 1609 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1610 | |
| 1611 | context_set_translation_type(context, translation); |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 1612 | context_set_fault_enable(context); |
| 1613 | context_set_present(context); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1614 | domain_flush_cache(domain, context, sizeof(*context)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1615 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1616 | /* |
| 1617 | * It's a non-present to present mapping. If hardware doesn't cache |
| 1618 | * non-present entry we only need to flush the write-buffer. If the |
| 1619 | * _does_ cache non-present entries, then it does so in the special |
| 1620 | * domain #0, which we have to flush: |
| 1621 | */ |
| 1622 | if (cap_caching_mode(iommu->cap)) { |
| 1623 | iommu->flush.flush_context(iommu, 0, |
| 1624 | (((u16)bus) << 8) | devfn, |
| 1625 | DMA_CCMD_MASK_NOBIT, |
| 1626 | DMA_CCMD_DEVICE_INVL); |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1627 | iommu->flush.flush_iotlb(iommu, domain->id, 0, 0, DMA_TLB_DSI_FLUSH); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1628 | } else { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1629 | iommu_flush_write_buffer(iommu); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1630 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1631 | iommu_enable_dev_iotlb(info); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1632 | spin_unlock_irqrestore(&iommu->lock, flags); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1633 | |
| 1634 | spin_lock_irqsave(&domain->iommu_lock, flags); |
| 1635 | if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) { |
| 1636 | domain->iommu_count++; |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1637 | if (domain->iommu_count == 1) |
| 1638 | domain->nid = iommu->node; |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 1639 | domain_update_iommu_cap(domain); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1640 | } |
| 1641 | spin_unlock_irqrestore(&domain->iommu_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1642 | return 0; |
| 1643 | } |
| 1644 | |
| 1645 | static int |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1646 | domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev, |
| 1647 | int translation) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1648 | { |
| 1649 | int ret; |
| 1650 | struct pci_dev *tmp, *parent; |
| 1651 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1652 | ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus), |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1653 | pdev->bus->number, pdev->devfn, |
| 1654 | translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1655 | if (ret) |
| 1656 | return ret; |
| 1657 | |
| 1658 | /* dependent device mapping */ |
| 1659 | tmp = pci_find_upstream_pcie_bridge(pdev); |
| 1660 | if (!tmp) |
| 1661 | return 0; |
| 1662 | /* Secondary interface's bus number and devfn 0 */ |
| 1663 | parent = pdev->bus->self; |
| 1664 | while (parent != tmp) { |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1665 | ret = domain_context_mapping_one(domain, |
| 1666 | pci_domain_nr(parent->bus), |
| 1667 | parent->bus->number, |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1668 | parent->devfn, translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1669 | if (ret) |
| 1670 | return ret; |
| 1671 | parent = parent->bus->self; |
| 1672 | } |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 1673 | if (pci_is_pcie(tmp)) /* this is a PCIe-to-PCI bridge */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1674 | return domain_context_mapping_one(domain, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1675 | pci_domain_nr(tmp->subordinate), |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1676 | tmp->subordinate->number, 0, |
| 1677 | translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1678 | else /* this is a legacy PCI bridge */ |
| 1679 | return domain_context_mapping_one(domain, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1680 | pci_domain_nr(tmp->bus), |
| 1681 | tmp->bus->number, |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1682 | tmp->devfn, |
| 1683 | translation); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1686 | static int domain_context_mapped(struct pci_dev *pdev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1687 | { |
| 1688 | int ret; |
| 1689 | struct pci_dev *tmp, *parent; |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1690 | struct intel_iommu *iommu; |
| 1691 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1692 | iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number, |
| 1693 | pdev->devfn); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1694 | if (!iommu) |
| 1695 | return -ENODEV; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1696 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1697 | ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1698 | if (!ret) |
| 1699 | return ret; |
| 1700 | /* dependent device mapping */ |
| 1701 | tmp = pci_find_upstream_pcie_bridge(pdev); |
| 1702 | if (!tmp) |
| 1703 | return ret; |
| 1704 | /* Secondary interface's bus number and devfn 0 */ |
| 1705 | parent = pdev->bus->self; |
| 1706 | while (parent != tmp) { |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1707 | ret = device_context_mapped(iommu, parent->bus->number, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1708 | parent->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1709 | if (!ret) |
| 1710 | return ret; |
| 1711 | parent = parent->bus->self; |
| 1712 | } |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 1713 | if (pci_is_pcie(tmp)) |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1714 | return device_context_mapped(iommu, tmp->subordinate->number, |
| 1715 | 0); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1716 | else |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1717 | return device_context_mapped(iommu, tmp->bus->number, |
| 1718 | tmp->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 1721 | /* Returns a number of VTD pages, but aligned to MM page size */ |
| 1722 | static inline unsigned long aligned_nrpages(unsigned long host_addr, |
| 1723 | size_t size) |
| 1724 | { |
| 1725 | host_addr &= ~PAGE_MASK; |
| 1726 | return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT; |
| 1727 | } |
| 1728 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1729 | /* Return largest possible superpage level for a given mapping */ |
| 1730 | static inline int hardware_largepage_caps(struct dmar_domain *domain, |
| 1731 | unsigned long iov_pfn, |
| 1732 | unsigned long phy_pfn, |
| 1733 | unsigned long pages) |
| 1734 | { |
| 1735 | int support, level = 1; |
| 1736 | unsigned long pfnmerge; |
| 1737 | |
| 1738 | support = domain->iommu_superpage; |
| 1739 | |
| 1740 | /* To use a large page, the virtual *and* physical addresses |
| 1741 | must be aligned to 2MiB/1GiB/etc. Lower bits set in either |
| 1742 | of them will mean we have to use smaller pages. So just |
| 1743 | merge them and check both at once. */ |
| 1744 | pfnmerge = iov_pfn | phy_pfn; |
| 1745 | |
| 1746 | while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) { |
| 1747 | pages >>= VTD_STRIDE_SHIFT; |
| 1748 | if (!pages) |
| 1749 | break; |
| 1750 | pfnmerge >>= VTD_STRIDE_SHIFT; |
| 1751 | level++; |
| 1752 | support--; |
| 1753 | } |
| 1754 | return level; |
| 1755 | } |
| 1756 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1757 | static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 1758 | struct scatterlist *sg, unsigned long phys_pfn, |
| 1759 | unsigned long nr_pages, int prot) |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1760 | { |
| 1761 | struct dma_pte *first_pte = NULL, *pte = NULL; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1762 | phys_addr_t uninitialized_var(pteval); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1763 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1764 | unsigned long sg_res; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1765 | unsigned int largepage_lvl = 0; |
| 1766 | unsigned long lvl_pages = 0; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1767 | |
| 1768 | BUG_ON(addr_width < BITS_PER_LONG && (iov_pfn + nr_pages - 1) >> addr_width); |
| 1769 | |
| 1770 | if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) |
| 1771 | return -EINVAL; |
| 1772 | |
| 1773 | prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP; |
| 1774 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1775 | if (sg) |
| 1776 | sg_res = 0; |
| 1777 | else { |
| 1778 | sg_res = nr_pages + 1; |
| 1779 | pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot; |
| 1780 | } |
| 1781 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1782 | while (nr_pages > 0) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 1783 | uint64_t tmp; |
| 1784 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1785 | if (!sg_res) { |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 1786 | sg_res = aligned_nrpages(sg->offset, sg->length); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1787 | sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset; |
| 1788 | sg->dma_length = sg->length; |
| 1789 | pteval = page_to_phys(sg_page(sg)) | prot; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1790 | phys_pfn = pteval >> VTD_PAGE_SHIFT; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1791 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1792 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1793 | if (!pte) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1794 | largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res); |
| 1795 | |
| 1796 | first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, largepage_lvl); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1797 | if (!pte) |
| 1798 | return -ENOMEM; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1799 | /* It is large page*/ |
| 1800 | if (largepage_lvl > 1) |
| 1801 | pteval |= DMA_PTE_LARGE_PAGE; |
| 1802 | else |
| 1803 | pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; |
| 1804 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1805 | } |
| 1806 | /* We don't need lock here, nobody else |
| 1807 | * touches the iova range |
| 1808 | */ |
David Woodhouse | 7766a3f | 2009-07-01 20:27:03 +0100 | [diff] [blame] | 1809 | tmp = cmpxchg64_local(&pte->val, 0ULL, pteval); |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 1810 | if (tmp) { |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 1811 | static int dumps = 5; |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 1812 | printk(KERN_CRIT "ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n", |
| 1813 | iov_pfn, tmp, (unsigned long long)pteval); |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 1814 | if (dumps) { |
| 1815 | dumps--; |
| 1816 | debug_dma_dump_mappings(NULL); |
| 1817 | } |
| 1818 | WARN_ON(1); |
| 1819 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1820 | |
| 1821 | lvl_pages = lvl_to_nr_pages(largepage_lvl); |
| 1822 | |
| 1823 | BUG_ON(nr_pages < lvl_pages); |
| 1824 | BUG_ON(sg_res < lvl_pages); |
| 1825 | |
| 1826 | nr_pages -= lvl_pages; |
| 1827 | iov_pfn += lvl_pages; |
| 1828 | phys_pfn += lvl_pages; |
| 1829 | pteval += lvl_pages * VTD_PAGE_SIZE; |
| 1830 | sg_res -= lvl_pages; |
| 1831 | |
| 1832 | /* If the next PTE would be the first in a new page, then we |
| 1833 | need to flush the cache on the entries we've just written. |
| 1834 | And then we'll need to recalculate 'pte', so clear it and |
| 1835 | let it get set again in the if (!pte) block above. |
| 1836 | |
| 1837 | If we're done (!nr_pages) we need to flush the cache too. |
| 1838 | |
| 1839 | Also if we've been setting superpages, we may need to |
| 1840 | recalculate 'pte' and switch back to smaller pages for the |
| 1841 | end of the mapping, if the trailing size is not enough to |
| 1842 | use another superpage (i.e. sg_res < lvl_pages). */ |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1843 | pte++; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1844 | if (!nr_pages || first_pte_in_page(pte) || |
| 1845 | (largepage_lvl > 1 && sg_res < lvl_pages)) { |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1846 | domain_flush_cache(domain, first_pte, |
| 1847 | (void *)pte - (void *)first_pte); |
| 1848 | pte = NULL; |
| 1849 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1850 | |
| 1851 | if (!sg_res && nr_pages) |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 1852 | sg = sg_next(sg); |
| 1853 | } |
| 1854 | return 0; |
| 1855 | } |
| 1856 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1857 | static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 1858 | struct scatterlist *sg, unsigned long nr_pages, |
| 1859 | int prot) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1860 | { |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1861 | return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot); |
| 1862 | } |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1863 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 1864 | static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 1865 | unsigned long phys_pfn, unsigned long nr_pages, |
| 1866 | int prot) |
| 1867 | { |
| 1868 | return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1871 | 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] | 1872 | { |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1873 | if (!iommu) |
| 1874 | return; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 1875 | |
| 1876 | clear_context_table(iommu, bus, devfn); |
| 1877 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1878 | DMA_CCMD_GLOBAL_INVL); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1879 | 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] | 1880 | } |
| 1881 | |
| 1882 | static void domain_remove_dev_info(struct dmar_domain *domain) |
| 1883 | { |
| 1884 | struct device_domain_info *info; |
| 1885 | unsigned long flags; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1886 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1887 | |
| 1888 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1889 | while (!list_empty(&domain->devices)) { |
| 1890 | info = list_entry(domain->devices.next, |
| 1891 | struct device_domain_info, link); |
| 1892 | list_del(&info->link); |
| 1893 | list_del(&info->global); |
| 1894 | if (info->dev) |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1895 | info->dev->dev.archdata.iommu = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1896 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1897 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1898 | iommu_disable_dev_iotlb(info); |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1899 | iommu = device_to_iommu(info->segment, info->bus, info->devfn); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1900 | iommu_detach_dev(iommu, info->bus, info->devfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1901 | free_devinfo_mem(info); |
| 1902 | |
| 1903 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1904 | } |
| 1905 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * find_domain |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1910 | * 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] | 1911 | */ |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 1912 | static struct dmar_domain * |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1913 | find_domain(struct pci_dev *pdev) |
| 1914 | { |
| 1915 | struct device_domain_info *info; |
| 1916 | |
| 1917 | /* No lock here, assumes no domain exit in normal case */ |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 1918 | info = pdev->dev.archdata.iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1919 | if (info) |
| 1920 | return info->domain; |
| 1921 | return NULL; |
| 1922 | } |
| 1923 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1924 | /* domain is initialized */ |
| 1925 | static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw) |
| 1926 | { |
| 1927 | struct dmar_domain *domain, *found = NULL; |
| 1928 | struct intel_iommu *iommu; |
| 1929 | struct dmar_drhd_unit *drhd; |
| 1930 | struct device_domain_info *info, *tmp; |
| 1931 | struct pci_dev *dev_tmp; |
| 1932 | unsigned long flags; |
| 1933 | int bus = 0, devfn = 0; |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1934 | int segment; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1935 | int ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1936 | |
| 1937 | domain = find_domain(pdev); |
| 1938 | if (domain) |
| 1939 | return domain; |
| 1940 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1941 | segment = pci_domain_nr(pdev->bus); |
| 1942 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1943 | dev_tmp = pci_find_upstream_pcie_bridge(pdev); |
| 1944 | if (dev_tmp) { |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 1945 | if (pci_is_pcie(dev_tmp)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1946 | bus = dev_tmp->subordinate->number; |
| 1947 | devfn = 0; |
| 1948 | } else { |
| 1949 | bus = dev_tmp->bus->number; |
| 1950 | devfn = dev_tmp->devfn; |
| 1951 | } |
| 1952 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1953 | list_for_each_entry(info, &device_domain_list, global) { |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1954 | if (info->segment == segment && |
| 1955 | info->bus == bus && info->devfn == devfn) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1956 | found = info->domain; |
| 1957 | break; |
| 1958 | } |
| 1959 | } |
| 1960 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1961 | /* pcie-pci bridge already has a domain, uses it */ |
| 1962 | if (found) { |
| 1963 | domain = found; |
| 1964 | goto found_domain; |
| 1965 | } |
| 1966 | } |
| 1967 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1968 | domain = alloc_domain(); |
| 1969 | if (!domain) |
| 1970 | goto error; |
| 1971 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1972 | /* Allocate new domain for the device */ |
| 1973 | drhd = dmar_find_matched_drhd_unit(pdev); |
| 1974 | if (!drhd) { |
| 1975 | printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n", |
| 1976 | pci_name(pdev)); |
| 1977 | return NULL; |
| 1978 | } |
| 1979 | iommu = drhd->iommu; |
| 1980 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1981 | ret = iommu_attach_domain(domain, iommu); |
| 1982 | if (ret) { |
Alex Williamson | 2fe9723d | 2011-03-04 14:52:30 -0700 | [diff] [blame] | 1983 | free_domain_mem(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1984 | goto error; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1985 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1986 | |
| 1987 | if (domain_init(domain, gaw)) { |
| 1988 | domain_exit(domain); |
| 1989 | goto error; |
| 1990 | } |
| 1991 | |
| 1992 | /* register pcie-to-pci device */ |
| 1993 | if (dev_tmp) { |
| 1994 | info = alloc_devinfo_mem(); |
| 1995 | if (!info) { |
| 1996 | domain_exit(domain); |
| 1997 | goto error; |
| 1998 | } |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 1999 | info->segment = segment; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2000 | info->bus = bus; |
| 2001 | info->devfn = devfn; |
| 2002 | info->dev = NULL; |
| 2003 | info->domain = domain; |
| 2004 | /* This domain is shared by devices under p2p bridge */ |
Weidong Han | 3b5410e | 2008-12-08 09:17:15 +0800 | [diff] [blame] | 2005 | domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2006 | |
| 2007 | /* pcie-to-pci bridge already has a domain, uses it */ |
| 2008 | found = NULL; |
| 2009 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2010 | list_for_each_entry(tmp, &device_domain_list, global) { |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 2011 | if (tmp->segment == segment && |
| 2012 | tmp->bus == bus && tmp->devfn == devfn) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2013 | found = tmp->domain; |
| 2014 | break; |
| 2015 | } |
| 2016 | } |
| 2017 | if (found) { |
Jiri Slaby | 00dfff7 | 2010-06-14 17:17:32 +0200 | [diff] [blame] | 2018 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2019 | free_devinfo_mem(info); |
| 2020 | domain_exit(domain); |
| 2021 | domain = found; |
| 2022 | } else { |
| 2023 | list_add(&info->link, &domain->devices); |
| 2024 | list_add(&info->global, &device_domain_list); |
Jiri Slaby | 00dfff7 | 2010-06-14 17:17:32 +0200 | [diff] [blame] | 2025 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2026 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | found_domain: |
| 2030 | info = alloc_devinfo_mem(); |
| 2031 | if (!info) |
| 2032 | goto error; |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 2033 | info->segment = segment; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2034 | info->bus = pdev->bus->number; |
| 2035 | info->devfn = pdev->devfn; |
| 2036 | info->dev = pdev; |
| 2037 | info->domain = domain; |
| 2038 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2039 | /* somebody is fast */ |
| 2040 | found = find_domain(pdev); |
| 2041 | if (found != NULL) { |
| 2042 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2043 | if (found != domain) { |
| 2044 | domain_exit(domain); |
| 2045 | domain = found; |
| 2046 | } |
| 2047 | free_devinfo_mem(info); |
| 2048 | return domain; |
| 2049 | } |
| 2050 | list_add(&info->link, &domain->devices); |
| 2051 | list_add(&info->global, &device_domain_list); |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 2052 | pdev->dev.archdata.iommu = info; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2053 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2054 | return domain; |
| 2055 | error: |
| 2056 | /* recheck it here, maybe others set it */ |
| 2057 | return find_domain(pdev); |
| 2058 | } |
| 2059 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2060 | static int iommu_identity_mapping; |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2061 | #define IDENTMAP_ALL 1 |
| 2062 | #define IDENTMAP_GFX 2 |
| 2063 | #define IDENTMAP_AZALIA 4 |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2064 | |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2065 | static int iommu_domain_identity_map(struct dmar_domain *domain, |
| 2066 | unsigned long long start, |
| 2067 | unsigned long long end) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2068 | { |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2069 | unsigned long first_vpfn = start >> VTD_PAGE_SHIFT; |
| 2070 | unsigned long last_vpfn = end >> VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2071 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2072 | if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn), |
| 2073 | dma_to_mm_pfn(last_vpfn))) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2074 | printk(KERN_ERR "IOMMU: reserve iova failed\n"); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2075 | return -ENOMEM; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2078 | pr_debug("Mapping reserved region %llx-%llx for domain %d\n", |
| 2079 | start, end, domain->id); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2080 | /* |
| 2081 | * RMRR range might have overlap with physical memory range, |
| 2082 | * clear it first |
| 2083 | */ |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2084 | dma_pte_clear_range(domain, first_vpfn, last_vpfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2085 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2086 | return domain_pfn_mapping(domain, first_vpfn, first_vpfn, |
| 2087 | last_vpfn - first_vpfn + 1, |
David Woodhouse | 61df744 | 2009-06-28 11:55:58 +0100 | [diff] [blame] | 2088 | DMA_PTE_READ|DMA_PTE_WRITE); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | static int iommu_prepare_identity_map(struct pci_dev *pdev, |
| 2092 | unsigned long long start, |
| 2093 | unsigned long long end) |
| 2094 | { |
| 2095 | struct dmar_domain *domain; |
| 2096 | int ret; |
| 2097 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2098 | domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2099 | if (!domain) |
| 2100 | return -ENOMEM; |
| 2101 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2102 | /* For _hardware_ passthrough, don't bother. But for software |
| 2103 | passthrough, we do it anyway -- it may indicate a memory |
| 2104 | range which is reserved in E820, so which didn't get set |
| 2105 | up to start with in si_domain */ |
| 2106 | if (domain == si_domain && hw_pass_through) { |
| 2107 | printk("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n", |
| 2108 | pci_name(pdev), start, end); |
| 2109 | return 0; |
| 2110 | } |
| 2111 | |
| 2112 | printk(KERN_INFO |
| 2113 | "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n", |
| 2114 | pci_name(pdev), start, end); |
David Woodhouse | 2ff729f | 2009-08-26 14:25:41 +0100 | [diff] [blame] | 2115 | |
David Woodhouse | 5595b52 | 2009-12-02 09:21:55 +0000 | [diff] [blame] | 2116 | if (end < start) { |
| 2117 | WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n" |
| 2118 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 2119 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 2120 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 2121 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
| 2122 | ret = -EIO; |
| 2123 | goto error; |
| 2124 | } |
| 2125 | |
David Woodhouse | 2ff729f | 2009-08-26 14:25:41 +0100 | [diff] [blame] | 2126 | if (end >> agaw_to_width(domain->agaw)) { |
| 2127 | WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n" |
| 2128 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 2129 | agaw_to_width(domain->agaw), |
| 2130 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 2131 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 2132 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
| 2133 | ret = -EIO; |
| 2134 | goto error; |
| 2135 | } |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2136 | |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2137 | ret = iommu_domain_identity_map(domain, start, end); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2138 | if (ret) |
| 2139 | goto error; |
| 2140 | |
| 2141 | /* context entry init */ |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2142 | ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2143 | if (ret) |
| 2144 | goto error; |
| 2145 | |
| 2146 | return 0; |
| 2147 | |
| 2148 | error: |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2149 | domain_exit(domain); |
| 2150 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr, |
| 2154 | struct pci_dev *pdev) |
| 2155 | { |
Keshavamurthy, Anil S | 358dd8a | 2007-10-21 16:41:59 -0700 | [diff] [blame] | 2156 | if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2157 | return 0; |
| 2158 | return iommu_prepare_identity_map(pdev, rmrr->base_address, |
David Woodhouse | 70e535d | 2011-05-31 00:22:52 +0100 | [diff] [blame] | 2159 | rmrr->end_address); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 2162 | #ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2163 | static inline void iommu_prepare_isa(void) |
| 2164 | { |
| 2165 | struct pci_dev *pdev; |
| 2166 | int ret; |
| 2167 | |
| 2168 | pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL); |
| 2169 | if (!pdev) |
| 2170 | return; |
| 2171 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2172 | printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n"); |
David Woodhouse | 70e535d | 2011-05-31 00:22:52 +0100 | [diff] [blame] | 2173 | ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024 - 1); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2174 | |
| 2175 | if (ret) |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2176 | printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; " |
| 2177 | "floppy might not work\n"); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2178 | |
| 2179 | } |
| 2180 | #else |
| 2181 | static inline void iommu_prepare_isa(void) |
| 2182 | { |
| 2183 | return; |
| 2184 | } |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 2185 | #endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */ |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2186 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2187 | static int md_domain_init(struct dmar_domain *domain, int guest_width); |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2188 | |
Matt Kraai | 071e137 | 2009-08-23 22:30:22 -0700 | [diff] [blame] | 2189 | static int __init si_domain_init(int hw) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2190 | { |
| 2191 | struct dmar_drhd_unit *drhd; |
| 2192 | struct intel_iommu *iommu; |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2193 | int nid, ret = 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2194 | |
| 2195 | si_domain = alloc_domain(); |
| 2196 | if (!si_domain) |
| 2197 | return -EFAULT; |
| 2198 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2199 | pr_debug("Identity mapping domain is domain %d\n", si_domain->id); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2200 | |
| 2201 | for_each_active_iommu(iommu, drhd) { |
| 2202 | ret = iommu_attach_domain(si_domain, iommu); |
| 2203 | if (ret) { |
| 2204 | domain_exit(si_domain); |
| 2205 | return -EFAULT; |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
| 2210 | domain_exit(si_domain); |
| 2211 | return -EFAULT; |
| 2212 | } |
| 2213 | |
| 2214 | si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY; |
| 2215 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2216 | if (hw) |
| 2217 | return 0; |
| 2218 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2219 | for_each_online_node(nid) { |
Tejun Heo | d4bbf7e | 2011-11-28 09:46:22 -0800 | [diff] [blame] | 2220 | unsigned long start_pfn, end_pfn; |
| 2221 | int i; |
| 2222 | |
| 2223 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { |
| 2224 | ret = iommu_domain_identity_map(si_domain, |
| 2225 | PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); |
| 2226 | if (ret) |
| 2227 | return ret; |
| 2228 | } |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2229 | } |
| 2230 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | |
| 2234 | static void domain_remove_one_dev_info(struct dmar_domain *domain, |
| 2235 | struct pci_dev *pdev); |
| 2236 | static int identity_mapping(struct pci_dev *pdev) |
| 2237 | { |
| 2238 | struct device_domain_info *info; |
| 2239 | |
| 2240 | if (likely(!iommu_identity_mapping)) |
| 2241 | return 0; |
| 2242 | |
Mike Travis | cb452a4 | 2011-05-28 13:15:03 -0500 | [diff] [blame] | 2243 | info = pdev->dev.archdata.iommu; |
| 2244 | if (info && info != DUMMY_DEVICE_DOMAIN_INFO) |
| 2245 | return (info->domain == si_domain); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2246 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2247 | return 0; |
| 2248 | } |
| 2249 | |
| 2250 | static int domain_add_dev_info(struct dmar_domain *domain, |
David Woodhouse | 5fe60f4 | 2009-08-09 10:53:41 +0100 | [diff] [blame] | 2251 | struct pci_dev *pdev, |
| 2252 | int translation) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2253 | { |
| 2254 | struct device_domain_info *info; |
| 2255 | unsigned long flags; |
David Woodhouse | 5fe60f4 | 2009-08-09 10:53:41 +0100 | [diff] [blame] | 2256 | int ret; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2257 | |
| 2258 | info = alloc_devinfo_mem(); |
| 2259 | if (!info) |
| 2260 | return -ENOMEM; |
| 2261 | |
David Woodhouse | 5fe60f4 | 2009-08-09 10:53:41 +0100 | [diff] [blame] | 2262 | ret = domain_context_mapping(domain, pdev, translation); |
| 2263 | if (ret) { |
| 2264 | free_devinfo_mem(info); |
| 2265 | return ret; |
| 2266 | } |
| 2267 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2268 | info->segment = pci_domain_nr(pdev->bus); |
| 2269 | info->bus = pdev->bus->number; |
| 2270 | info->devfn = pdev->devfn; |
| 2271 | info->dev = pdev; |
| 2272 | info->domain = domain; |
| 2273 | |
| 2274 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2275 | list_add(&info->link, &domain->devices); |
| 2276 | list_add(&info->global, &device_domain_list); |
| 2277 | pdev->dev.archdata.iommu = info; |
| 2278 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2279 | |
| 2280 | return 0; |
| 2281 | } |
| 2282 | |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2283 | static int iommu_should_identity_map(struct pci_dev *pdev, int startup) |
| 2284 | { |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2285 | if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev)) |
| 2286 | return 1; |
| 2287 | |
| 2288 | if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev)) |
| 2289 | return 1; |
| 2290 | |
| 2291 | if (!(iommu_identity_mapping & IDENTMAP_ALL)) |
| 2292 | return 0; |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2293 | |
David Woodhouse | 3dfc813 | 2009-07-04 19:11:08 +0100 | [diff] [blame] | 2294 | /* |
| 2295 | * We want to start off with all devices in the 1:1 domain, and |
| 2296 | * take them out later if we find they can't access all of memory. |
| 2297 | * |
| 2298 | * However, we can't do this for PCI devices behind bridges, |
| 2299 | * because all PCI devices behind the same bridge will end up |
| 2300 | * with the same source-id on their transactions. |
| 2301 | * |
| 2302 | * Practically speaking, we can't change things around for these |
| 2303 | * devices at run-time, because we can't be sure there'll be no |
| 2304 | * DMA transactions in flight for any of their siblings. |
| 2305 | * |
| 2306 | * So PCI devices (unless they're on the root bus) as well as |
| 2307 | * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of |
| 2308 | * the 1:1 domain, just in _case_ one of their siblings turns out |
| 2309 | * not to be able to map all of memory. |
| 2310 | */ |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 2311 | if (!pci_is_pcie(pdev)) { |
David Woodhouse | 3dfc813 | 2009-07-04 19:11:08 +0100 | [diff] [blame] | 2312 | if (!pci_is_root_bus(pdev->bus)) |
| 2313 | return 0; |
| 2314 | if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI) |
| 2315 | return 0; |
| 2316 | } else if (pdev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) |
| 2317 | return 0; |
| 2318 | |
| 2319 | /* |
| 2320 | * At boot time, we don't yet know if devices will be 64-bit capable. |
| 2321 | * Assume that they will -- if they turn out not to be, then we can |
| 2322 | * take them out of the 1:1 domain later. |
| 2323 | */ |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2324 | if (!startup) { |
| 2325 | /* |
| 2326 | * If the device's dma_mask is less than the system's memory |
| 2327 | * size then this is not a candidate for identity mapping. |
| 2328 | */ |
| 2329 | u64 dma_mask = pdev->dma_mask; |
| 2330 | |
| 2331 | if (pdev->dev.coherent_dma_mask && |
| 2332 | pdev->dev.coherent_dma_mask < dma_mask) |
| 2333 | dma_mask = pdev->dev.coherent_dma_mask; |
| 2334 | |
| 2335 | return dma_mask >= dma_get_required_mask(&pdev->dev); |
| 2336 | } |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2337 | |
| 2338 | return 1; |
| 2339 | } |
| 2340 | |
Matt Kraai | 071e137 | 2009-08-23 22:30:22 -0700 | [diff] [blame] | 2341 | static int __init iommu_prepare_static_identity_mapping(int hw) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2342 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2343 | struct pci_dev *pdev = NULL; |
| 2344 | int ret; |
| 2345 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2346 | ret = si_domain_init(hw); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2347 | if (ret) |
| 2348 | return -EFAULT; |
| 2349 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2350 | for_each_pci_dev(pdev) { |
Mike Travis | 825507d | 2011-05-28 13:15:06 -0500 | [diff] [blame] | 2351 | /* Skip Host/PCI Bridge devices */ |
| 2352 | if (IS_BRIDGE_HOST_DEVICE(pdev)) |
| 2353 | continue; |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2354 | if (iommu_should_identity_map(pdev, 1)) { |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2355 | printk(KERN_INFO "IOMMU: %s identity mapping for device %s\n", |
| 2356 | hw ? "hardware" : "software", pci_name(pdev)); |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2357 | |
David Woodhouse | 5fe60f4 | 2009-08-09 10:53:41 +0100 | [diff] [blame] | 2358 | ret = domain_add_dev_info(si_domain, pdev, |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2359 | hw ? CONTEXT_TT_PASS_THROUGH : |
David Woodhouse | 62edf5d | 2009-07-04 10:59:46 +0100 | [diff] [blame] | 2360 | CONTEXT_TT_MULTI_LEVEL); |
| 2361 | if (ret) |
| 2362 | return ret; |
David Woodhouse | 62edf5d | 2009-07-04 10:59:46 +0100 | [diff] [blame] | 2363 | } |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | return 0; |
| 2367 | } |
| 2368 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 2369 | static int __init init_dmars(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2370 | { |
| 2371 | struct dmar_drhd_unit *drhd; |
| 2372 | struct dmar_rmrr_unit *rmrr; |
| 2373 | struct pci_dev *pdev; |
| 2374 | struct intel_iommu *iommu; |
Suresh Siddha | 9d783ba | 2009-03-16 17:04:55 -0700 | [diff] [blame] | 2375 | int i, ret; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2376 | |
| 2377 | /* |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2378 | * for each drhd |
| 2379 | * allocate root |
| 2380 | * initialize and program root entry to not present |
| 2381 | * endfor |
| 2382 | */ |
| 2383 | for_each_drhd_unit(drhd) { |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2384 | g_num_of_iommus++; |
| 2385 | /* |
| 2386 | * lock not needed as this is only incremented in the single |
| 2387 | * threaded kernel __init code path all other access are read |
| 2388 | * only |
| 2389 | */ |
| 2390 | } |
| 2391 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 2392 | g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *), |
| 2393 | GFP_KERNEL); |
| 2394 | if (!g_iommus) { |
| 2395 | printk(KERN_ERR "Allocating global iommu array failed\n"); |
| 2396 | ret = -ENOMEM; |
| 2397 | goto error; |
| 2398 | } |
| 2399 | |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2400 | deferred_flush = kzalloc(g_num_of_iommus * |
| 2401 | sizeof(struct deferred_flush_tables), GFP_KERNEL); |
| 2402 | if (!deferred_flush) { |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2403 | ret = -ENOMEM; |
| 2404 | goto error; |
| 2405 | } |
| 2406 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2407 | for_each_drhd_unit(drhd) { |
| 2408 | if (drhd->ignored) |
| 2409 | continue; |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 2410 | |
| 2411 | iommu = drhd->iommu; |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 2412 | g_iommus[iommu->seq_id] = iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2413 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 2414 | ret = iommu_init_domains(iommu); |
| 2415 | if (ret) |
| 2416 | goto error; |
| 2417 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2418 | /* |
| 2419 | * TBD: |
| 2420 | * we could share the same root & context tables |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2421 | * among all IOMMU's. Need to Split it later. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2422 | */ |
| 2423 | ret = iommu_alloc_root_entry(iommu); |
| 2424 | if (ret) { |
| 2425 | printk(KERN_ERR "IOMMU: allocate root entry failed\n"); |
| 2426 | goto error; |
| 2427 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2428 | if (!ecap_pass_through(iommu->ecap)) |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2429 | hw_pass_through = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 2432 | /* |
| 2433 | * Start from the sane iommu hardware state. |
| 2434 | */ |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2435 | for_each_drhd_unit(drhd) { |
| 2436 | if (drhd->ignored) |
| 2437 | continue; |
| 2438 | |
| 2439 | iommu = drhd->iommu; |
Suresh Siddha | 1531a6a | 2009-03-16 17:04:57 -0700 | [diff] [blame] | 2440 | |
| 2441 | /* |
| 2442 | * If the queued invalidation is already initialized by us |
| 2443 | * (for example, while enabling interrupt-remapping) then |
| 2444 | * we got the things already rolling from a sane state. |
| 2445 | */ |
| 2446 | if (iommu->qi) |
| 2447 | continue; |
| 2448 | |
| 2449 | /* |
| 2450 | * Clear any previous faults. |
| 2451 | */ |
| 2452 | dmar_fault(-1, iommu); |
| 2453 | /* |
| 2454 | * Disable queued invalidation if supported and already enabled |
| 2455 | * before OS handover. |
| 2456 | */ |
| 2457 | dmar_disable_qi(iommu); |
| 2458 | } |
| 2459 | |
| 2460 | for_each_drhd_unit(drhd) { |
| 2461 | if (drhd->ignored) |
| 2462 | continue; |
| 2463 | |
| 2464 | iommu = drhd->iommu; |
| 2465 | |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2466 | if (dmar_enable_qi(iommu)) { |
| 2467 | /* |
| 2468 | * Queued Invalidate not enabled, use Register Based |
| 2469 | * Invalidate |
| 2470 | */ |
| 2471 | iommu->flush.flush_context = __iommu_flush_context; |
| 2472 | iommu->flush.flush_iotlb = __iommu_flush_iotlb; |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 2473 | printk(KERN_INFO "IOMMU %d 0x%Lx: using Register based " |
FUJITA Tomonori | b4e0f9e | 2008-11-19 13:53:42 +0900 | [diff] [blame] | 2474 | "invalidation\n", |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 2475 | iommu->seq_id, |
FUJITA Tomonori | b4e0f9e | 2008-11-19 13:53:42 +0900 | [diff] [blame] | 2476 | (unsigned long long)drhd->reg_base_addr); |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2477 | } else { |
| 2478 | iommu->flush.flush_context = qi_flush_context; |
| 2479 | iommu->flush.flush_iotlb = qi_flush_iotlb; |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 2480 | printk(KERN_INFO "IOMMU %d 0x%Lx: using Queued " |
FUJITA Tomonori | b4e0f9e | 2008-11-19 13:53:42 +0900 | [diff] [blame] | 2481 | "invalidation\n", |
Yinghai Lu | 680a752 | 2010-04-08 19:58:23 +0100 | [diff] [blame] | 2482 | iommu->seq_id, |
FUJITA Tomonori | b4e0f9e | 2008-11-19 13:53:42 +0900 | [diff] [blame] | 2483 | (unsigned long long)drhd->reg_base_addr); |
Youquan Song | a77b67d | 2008-10-16 16:31:56 -0700 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2487 | if (iommu_pass_through) |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2488 | iommu_identity_mapping |= IDENTMAP_ALL; |
| 2489 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 2490 | #ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2491 | iommu_identity_mapping |= IDENTMAP_GFX; |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2492 | #endif |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2493 | |
| 2494 | check_tylersburg_isoch(); |
| 2495 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2496 | /* |
| 2497 | * 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] | 2498 | * identity mappings for rmrr, gfx, and isa and may fall back to static |
| 2499 | * identity mapping if iommu_identity_mapping is set. |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2500 | */ |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2501 | if (iommu_identity_mapping) { |
| 2502 | ret = iommu_prepare_static_identity_mapping(hw_pass_through); |
| 2503 | if (ret) { |
| 2504 | printk(KERN_CRIT "Failed to setup IOMMU pass-through\n"); |
| 2505 | goto error; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2506 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2507 | } |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2508 | /* |
| 2509 | * For each rmrr |
| 2510 | * for each dev attached to rmrr |
| 2511 | * do |
| 2512 | * locate drhd for dev, alloc domain for dev |
| 2513 | * allocate free domain |
| 2514 | * allocate page table entries for rmrr |
| 2515 | * if context not allocated for bus |
| 2516 | * allocate and init context |
| 2517 | * set present in root table for this bus |
| 2518 | * init context with domain, translation etc |
| 2519 | * endfor |
| 2520 | * endfor |
| 2521 | */ |
| 2522 | printk(KERN_INFO "IOMMU: Setting RMRR:\n"); |
| 2523 | for_each_rmrr_units(rmrr) { |
| 2524 | for (i = 0; i < rmrr->devices_cnt; i++) { |
| 2525 | pdev = rmrr->devices[i]; |
| 2526 | /* |
| 2527 | * some BIOS lists non-exist devices in DMAR |
| 2528 | * table. |
| 2529 | */ |
| 2530 | if (!pdev) |
| 2531 | continue; |
| 2532 | ret = iommu_prepare_rmrr_dev(rmrr, pdev); |
| 2533 | if (ret) |
| 2534 | printk(KERN_ERR |
| 2535 | "IOMMU: mapping reserved region failed\n"); |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | iommu_prepare_isa(); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2540 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2541 | /* |
| 2542 | * for each drhd |
| 2543 | * enable fault log |
| 2544 | * global invalidate context cache |
| 2545 | * global invalidate iotlb |
| 2546 | * enable translation |
| 2547 | */ |
| 2548 | for_each_drhd_unit(drhd) { |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 2549 | if (drhd->ignored) { |
| 2550 | /* |
| 2551 | * we always have to disable PMRs or DMA may fail on |
| 2552 | * this device |
| 2553 | */ |
| 2554 | if (force_on) |
| 2555 | iommu_disable_protect_mem_regions(drhd->iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2556 | continue; |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 2557 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2558 | iommu = drhd->iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2559 | |
| 2560 | iommu_flush_write_buffer(iommu); |
| 2561 | |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 2562 | ret = dmar_set_interrupt(iommu); |
| 2563 | if (ret) |
| 2564 | goto error; |
| 2565 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2566 | iommu_set_root_entry(iommu); |
| 2567 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2568 | iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2569 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 2570 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2571 | ret = iommu_enable_translation(iommu); |
| 2572 | if (ret) |
| 2573 | goto error; |
David Woodhouse | b94996c | 2009-09-19 15:28:12 -0700 | [diff] [blame] | 2574 | |
| 2575 | iommu_disable_protect_mem_regions(iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | return 0; |
| 2579 | error: |
| 2580 | for_each_drhd_unit(drhd) { |
| 2581 | if (drhd->ignored) |
| 2582 | continue; |
| 2583 | iommu = drhd->iommu; |
| 2584 | free_iommu(iommu); |
| 2585 | } |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 2586 | kfree(g_iommus); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2587 | return ret; |
| 2588 | } |
| 2589 | |
David Woodhouse | 5a5e02a | 2009-07-04 09:35:44 +0100 | [diff] [blame] | 2590 | /* This takes a number of _MM_ pages, not VTD pages */ |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2591 | static struct iova *intel_alloc_iova(struct device *dev, |
| 2592 | struct dmar_domain *domain, |
| 2593 | unsigned long nrpages, uint64_t dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2594 | { |
| 2595 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2596 | struct iova *iova = NULL; |
| 2597 | |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2598 | /* Restrict dma_mask to the width that the iommu can handle */ |
| 2599 | dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask); |
| 2600 | |
| 2601 | if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2602 | /* |
| 2603 | * First try to allocate an io virtual address in |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 2604 | * DMA_BIT_MASK(32) and if that fails then try allocating |
Joe Perches | 3609801 | 2007-12-17 11:40:11 -0800 | [diff] [blame] | 2605 | * from higher range |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2606 | */ |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2607 | iova = alloc_iova(&domain->iovad, nrpages, |
| 2608 | IOVA_PFN(DMA_BIT_MASK(32)), 1); |
| 2609 | if (iova) |
| 2610 | return iova; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2611 | } |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 2612 | iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1); |
| 2613 | if (unlikely(!iova)) { |
| 2614 | printk(KERN_ERR "Allocating %ld-page iova for %s failed", |
| 2615 | nrpages, pci_name(pdev)); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2616 | return NULL; |
| 2617 | } |
| 2618 | |
| 2619 | return iova; |
| 2620 | } |
| 2621 | |
David Woodhouse | 147202a | 2009-07-07 19:43:20 +0100 | [diff] [blame] | 2622 | static struct dmar_domain *__get_valid_domain_for_dev(struct pci_dev *pdev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2623 | { |
| 2624 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2625 | int ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2626 | |
| 2627 | domain = get_domain_for_dev(pdev, |
| 2628 | DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 2629 | if (!domain) { |
| 2630 | printk(KERN_ERR |
| 2631 | "Allocating domain for %s failed", pci_name(pdev)); |
Al Viro | 4fe05bb | 2007-10-29 04:51:16 +0000 | [diff] [blame] | 2632 | return NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2635 | /* make sure context mapping is ok */ |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2636 | if (unlikely(!domain_context_mapped(pdev))) { |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2637 | ret = domain_context_mapping(domain, pdev, |
| 2638 | CONTEXT_TT_MULTI_LEVEL); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2639 | if (ret) { |
| 2640 | printk(KERN_ERR |
| 2641 | "Domain context map for %s failed", |
| 2642 | pci_name(pdev)); |
Al Viro | 4fe05bb | 2007-10-29 04:51:16 +0000 | [diff] [blame] | 2643 | return NULL; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2644 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2647 | return domain; |
| 2648 | } |
| 2649 | |
David Woodhouse | 147202a | 2009-07-07 19:43:20 +0100 | [diff] [blame] | 2650 | static inline struct dmar_domain *get_valid_domain_for_dev(struct pci_dev *dev) |
| 2651 | { |
| 2652 | struct device_domain_info *info; |
| 2653 | |
| 2654 | /* No lock here, assumes no domain exit in normal case */ |
| 2655 | info = dev->dev.archdata.iommu; |
| 2656 | if (likely(info)) |
| 2657 | return info->domain; |
| 2658 | |
| 2659 | return __get_valid_domain_for_dev(dev); |
| 2660 | } |
| 2661 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2662 | static int iommu_dummy(struct pci_dev *pdev) |
| 2663 | { |
| 2664 | return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO; |
| 2665 | } |
| 2666 | |
| 2667 | /* Check if the pdev needs to go through non-identity map and unmap process.*/ |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 2668 | static int iommu_no_mapping(struct device *dev) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2669 | { |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 2670 | struct pci_dev *pdev; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2671 | int found; |
| 2672 | |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 2673 | if (unlikely(dev->bus != &pci_bus_type)) |
| 2674 | return 1; |
| 2675 | |
| 2676 | pdev = to_pci_dev(dev); |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 2677 | if (iommu_dummy(pdev)) |
| 2678 | return 1; |
| 2679 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2680 | if (!iommu_identity_mapping) |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 2681 | return 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2682 | |
| 2683 | found = identity_mapping(pdev); |
| 2684 | if (found) { |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2685 | if (iommu_should_identity_map(pdev, 0)) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2686 | return 1; |
| 2687 | else { |
| 2688 | /* |
| 2689 | * 32 bit DMA is removed from si_domain and fall back |
| 2690 | * to non-identity mapping. |
| 2691 | */ |
| 2692 | domain_remove_one_dev_info(si_domain, pdev); |
| 2693 | printk(KERN_INFO "32bit %s uses non-identity mapping\n", |
| 2694 | pci_name(pdev)); |
| 2695 | return 0; |
| 2696 | } |
| 2697 | } else { |
| 2698 | /* |
| 2699 | * In case of a detached 64 bit DMA device from vm, the device |
| 2700 | * is put into si_domain for identity mapping. |
| 2701 | */ |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2702 | if (iommu_should_identity_map(pdev, 0)) { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2703 | int ret; |
David Woodhouse | 5fe60f4 | 2009-08-09 10:53:41 +0100 | [diff] [blame] | 2704 | ret = domain_add_dev_info(si_domain, pdev, |
| 2705 | hw_pass_through ? |
| 2706 | CONTEXT_TT_PASS_THROUGH : |
| 2707 | CONTEXT_TT_MULTI_LEVEL); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2708 | if (!ret) { |
| 2709 | printk(KERN_INFO "64bit %s uses identity mapping\n", |
| 2710 | pci_name(pdev)); |
| 2711 | return 1; |
| 2712 | } |
| 2713 | } |
| 2714 | } |
| 2715 | |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 2716 | return 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2719 | static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr, |
| 2720 | size_t size, int dir, u64 dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2721 | { |
| 2722 | struct pci_dev *pdev = to_pci_dev(hwdev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2723 | struct dmar_domain *domain; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2724 | phys_addr_t start_paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2725 | struct iova *iova; |
| 2726 | int prot = 0; |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2727 | int ret; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2728 | struct intel_iommu *iommu; |
Fenghua Yu | 33041ec | 2009-08-04 15:10:59 -0700 | [diff] [blame] | 2729 | unsigned long paddr_pfn = paddr >> PAGE_SHIFT; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2730 | |
| 2731 | BUG_ON(dir == DMA_NONE); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2732 | |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 2733 | if (iommu_no_mapping(hwdev)) |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2734 | return paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2735 | |
| 2736 | domain = get_valid_domain_for_dev(pdev); |
| 2737 | if (!domain) |
| 2738 | return 0; |
| 2739 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2740 | iommu = domain_get_iommu(domain); |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 2741 | size = aligned_nrpages(paddr, size); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2742 | |
Mike Travis | c681d0b | 2011-05-28 13:15:05 -0500 | [diff] [blame] | 2743 | iova = intel_alloc_iova(hwdev, domain, dma_to_mm_pfn(size), dma_mask); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2744 | if (!iova) |
| 2745 | goto error; |
| 2746 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2747 | /* |
| 2748 | * Check if DMAR supports zero-length reads on write only |
| 2749 | * mappings.. |
| 2750 | */ |
| 2751 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2752 | !cap_zlr(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2753 | prot |= DMA_PTE_READ; |
| 2754 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 2755 | prot |= DMA_PTE_WRITE; |
| 2756 | /* |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2757 | * 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] | 2758 | * page. Note: if two part of one page are separately mapped, we |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 2759 | * 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] | 2760 | * is not a big problem |
| 2761 | */ |
David Woodhouse | 0ab36de | 2009-06-28 14:01:43 +0100 | [diff] [blame] | 2762 | ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo), |
Fenghua Yu | 33041ec | 2009-08-04 15:10:59 -0700 | [diff] [blame] | 2763 | mm_to_dma_pfn(paddr_pfn), size, prot); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2764 | if (ret) |
| 2765 | goto error; |
| 2766 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2767 | /* it's a non-present to present mapping. Only flush if caching mode */ |
| 2768 | if (cap_caching_mode(iommu->cap)) |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 2769 | iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 1); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 2770 | else |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2771 | iommu_flush_write_buffer(iommu); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2772 | |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 2773 | start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT; |
| 2774 | start_paddr += paddr & ~PAGE_MASK; |
| 2775 | return start_paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2776 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2777 | error: |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2778 | if (iova) |
| 2779 | __free_iova(&domain->iovad, iova); |
David Woodhouse | 4cf2e75 | 2009-02-11 17:23:43 +0000 | [diff] [blame] | 2780 | printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2781 | pci_name(pdev), size, (unsigned long long)paddr, dir); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2782 | return 0; |
| 2783 | } |
| 2784 | |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2785 | static dma_addr_t intel_map_page(struct device *dev, struct page *page, |
| 2786 | unsigned long offset, size_t size, |
| 2787 | enum dma_data_direction dir, |
| 2788 | struct dma_attrs *attrs) |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2789 | { |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2790 | return __intel_map_single(dev, page_to_phys(page) + offset, size, |
| 2791 | dir, to_pci_dev(dev)->dma_mask); |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2792 | } |
| 2793 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2794 | static void flush_unmaps(void) |
| 2795 | { |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2796 | int i, j; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2797 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2798 | timer_on = 0; |
| 2799 | |
| 2800 | /* just flush them all */ |
| 2801 | for (i = 0; i < g_num_of_iommus; i++) { |
Weidong Han | a2bb845 | 2008-12-08 11:24:12 +0800 | [diff] [blame] | 2802 | struct intel_iommu *iommu = g_iommus[i]; |
| 2803 | if (!iommu) |
| 2804 | continue; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 2805 | |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 2806 | if (!deferred_flush[i].next) |
| 2807 | continue; |
| 2808 | |
Nadav Amit | 78d5f0f | 2010-04-08 23:00:41 +0300 | [diff] [blame] | 2809 | /* In caching mode, global flushes turn emulation expensive */ |
| 2810 | if (!cap_caching_mode(iommu->cap)) |
| 2811 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2812 | DMA_TLB_GLOBAL_FLUSH); |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 2813 | for (j = 0; j < deferred_flush[i].next; j++) { |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2814 | unsigned long mask; |
| 2815 | struct iova *iova = deferred_flush[i].iova[j]; |
Nadav Amit | 78d5f0f | 2010-04-08 23:00:41 +0300 | [diff] [blame] | 2816 | struct dmar_domain *domain = deferred_flush[i].domain[j]; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2817 | |
Nadav Amit | 78d5f0f | 2010-04-08 23:00:41 +0300 | [diff] [blame] | 2818 | /* On real hardware multiple invalidations are expensive */ |
| 2819 | if (cap_caching_mode(iommu->cap)) |
| 2820 | iommu_flush_iotlb_psi(iommu, domain->id, |
| 2821 | iova->pfn_lo, iova->pfn_hi - iova->pfn_lo + 1, 0); |
| 2822 | else { |
| 2823 | mask = ilog2(mm_to_dma_pfn(iova->pfn_hi - iova->pfn_lo + 1)); |
| 2824 | iommu_flush_dev_iotlb(deferred_flush[i].domain[j], |
| 2825 | (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask); |
| 2826 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2827 | __free_iova(&deferred_flush[i].domain[j]->iovad, iova); |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2828 | } |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 2829 | deferred_flush[i].next = 0; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2830 | } |
| 2831 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2832 | list_size = 0; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | static void flush_unmaps_timeout(unsigned long data) |
| 2836 | { |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2837 | unsigned long flags; |
| 2838 | |
| 2839 | spin_lock_irqsave(&async_umap_flush_lock, flags); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2840 | flush_unmaps(); |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2841 | spin_unlock_irqrestore(&async_umap_flush_lock, flags); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | static void add_unmap(struct dmar_domain *dom, struct iova *iova) |
| 2845 | { |
| 2846 | unsigned long flags; |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2847 | int next, iommu_id; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2848 | struct intel_iommu *iommu; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2849 | |
| 2850 | spin_lock_irqsave(&async_umap_flush_lock, flags); |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2851 | if (list_size == HIGH_WATER_MARK) |
| 2852 | flush_unmaps(); |
| 2853 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2854 | iommu = domain_get_iommu(dom); |
| 2855 | iommu_id = iommu->seq_id; |
Suresh Siddha | c42d9f3 | 2008-07-10 11:16:36 -0700 | [diff] [blame] | 2856 | |
mark gross | 80b20dd | 2008-04-18 13:53:58 -0700 | [diff] [blame] | 2857 | next = deferred_flush[iommu_id].next; |
| 2858 | deferred_flush[iommu_id].domain[next] = dom; |
| 2859 | deferred_flush[iommu_id].iova[next] = iova; |
| 2860 | deferred_flush[iommu_id].next++; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2861 | |
| 2862 | if (!timer_on) { |
| 2863 | mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10)); |
| 2864 | timer_on = 1; |
| 2865 | } |
| 2866 | list_size++; |
| 2867 | spin_unlock_irqrestore(&async_umap_flush_lock, flags); |
| 2868 | } |
| 2869 | |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 2870 | static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr, |
| 2871 | size_t size, enum dma_data_direction dir, |
| 2872 | struct dma_attrs *attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2873 | { |
| 2874 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2875 | struct dmar_domain *domain; |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2876 | unsigned long start_pfn, last_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2877 | struct iova *iova; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2878 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2879 | |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 2880 | if (iommu_no_mapping(dev)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2881 | return; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2882 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2883 | domain = find_domain(pdev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2884 | BUG_ON(!domain); |
| 2885 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2886 | iommu = domain_get_iommu(domain); |
| 2887 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2888 | iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr)); |
David Woodhouse | 85b9827 | 2009-07-01 19:27:53 +0100 | [diff] [blame] | 2889 | if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n", |
| 2890 | (unsigned long long)dev_addr)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2891 | return; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2892 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2893 | start_pfn = mm_to_dma_pfn(iova->pfn_lo); |
| 2894 | last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2895 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2896 | pr_debug("Device %s unmapping: pfn %lx-%lx\n", |
| 2897 | pci_name(pdev), start_pfn, last_pfn); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2898 | |
| 2899 | /* clear the whole page */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2900 | dma_pte_clear_range(domain, start_pfn, last_pfn); |
| 2901 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2902 | /* free page tables */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2903 | dma_pte_free_pagetable(domain, start_pfn, last_pfn); |
| 2904 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2905 | if (intel_iommu_strict) { |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 2906 | iommu_flush_iotlb_psi(iommu, domain->id, start_pfn, |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 2907 | last_pfn - start_pfn + 1, 0); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2908 | /* free iova */ |
| 2909 | __free_iova(&domain->iovad, iova); |
| 2910 | } else { |
| 2911 | add_unmap(domain, iova); |
| 2912 | /* |
| 2913 | * queue up the release of the unmap to save the 1/6th of the |
| 2914 | * cpu used up by the iotlb flush operation... |
| 2915 | */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 2916 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2919 | static void *intel_alloc_coherent(struct device *hwdev, size_t size, |
| 2920 | dma_addr_t *dma_handle, gfp_t flags) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2921 | { |
| 2922 | void *vaddr; |
| 2923 | int order; |
| 2924 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2925 | size = PAGE_ALIGN(size); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2926 | order = get_order(size); |
Alex Williamson | e8bb910 | 2009-11-04 15:59:34 -0700 | [diff] [blame] | 2927 | |
| 2928 | if (!iommu_no_mapping(hwdev)) |
| 2929 | flags &= ~(GFP_DMA | GFP_DMA32); |
| 2930 | else if (hwdev->coherent_dma_mask < dma_get_required_mask(hwdev)) { |
| 2931 | if (hwdev->coherent_dma_mask < DMA_BIT_MASK(32)) |
| 2932 | flags |= GFP_DMA; |
| 2933 | else |
| 2934 | flags |= GFP_DMA32; |
| 2935 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2936 | |
| 2937 | vaddr = (void *)__get_free_pages(flags, order); |
| 2938 | if (!vaddr) |
| 2939 | return NULL; |
| 2940 | memset(vaddr, 0, size); |
| 2941 | |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 2942 | *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size, |
| 2943 | DMA_BIDIRECTIONAL, |
| 2944 | hwdev->coherent_dma_mask); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2945 | if (*dma_handle) |
| 2946 | return vaddr; |
| 2947 | free_pages((unsigned long)vaddr, order); |
| 2948 | return NULL; |
| 2949 | } |
| 2950 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2951 | static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr, |
| 2952 | dma_addr_t dma_handle) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2953 | { |
| 2954 | int order; |
| 2955 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2956 | size = PAGE_ALIGN(size); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2957 | order = get_order(size); |
| 2958 | |
David Woodhouse | 0db9b7a | 2009-07-14 02:01:57 +0100 | [diff] [blame] | 2959 | intel_unmap_page(hwdev, dma_handle, size, DMA_BIDIRECTIONAL, NULL); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2960 | free_pages((unsigned long)vaddr, order); |
| 2961 | } |
| 2962 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 2963 | static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist, |
| 2964 | int nelems, enum dma_data_direction dir, |
| 2965 | struct dma_attrs *attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2966 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2967 | struct pci_dev *pdev = to_pci_dev(hwdev); |
| 2968 | struct dmar_domain *domain; |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2969 | unsigned long start_pfn, last_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2970 | struct iova *iova; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2971 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2972 | |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 2973 | if (iommu_no_mapping(hwdev)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2974 | return; |
| 2975 | |
| 2976 | domain = find_domain(pdev); |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2977 | BUG_ON(!domain); |
| 2978 | |
| 2979 | iommu = domain_get_iommu(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2980 | |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 2981 | iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address)); |
David Woodhouse | 85b9827 | 2009-07-01 19:27:53 +0100 | [diff] [blame] | 2982 | if (WARN_ONCE(!iova, "Driver unmaps unmatched sglist at PFN %llx\n", |
| 2983 | (unsigned long long)sglist[0].dma_address)) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2984 | return; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2985 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2986 | start_pfn = mm_to_dma_pfn(iova->pfn_lo); |
| 2987 | last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2988 | |
| 2989 | /* clear the whole page */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2990 | dma_pte_clear_range(domain, start_pfn, last_pfn); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 2991 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 2992 | /* free page tables */ |
| 2993 | dma_pte_free_pagetable(domain, start_pfn, last_pfn); |
| 2994 | |
David Woodhouse | acea001 | 2009-07-14 01:55:11 +0100 | [diff] [blame] | 2995 | if (intel_iommu_strict) { |
| 2996 | iommu_flush_iotlb_psi(iommu, domain->id, start_pfn, |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 2997 | last_pfn - start_pfn + 1, 0); |
David Woodhouse | acea001 | 2009-07-14 01:55:11 +0100 | [diff] [blame] | 2998 | /* free iova */ |
| 2999 | __free_iova(&domain->iovad, iova); |
| 3000 | } else { |
| 3001 | add_unmap(domain, iova); |
| 3002 | /* |
| 3003 | * queue up the release of the unmap to save the 1/6th of the |
| 3004 | * cpu used up by the iotlb flush operation... |
| 3005 | */ |
| 3006 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3007 | } |
| 3008 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3009 | static int intel_nontranslate_map_sg(struct device *hddev, |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3010 | struct scatterlist *sglist, int nelems, int dir) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3011 | { |
| 3012 | int i; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3013 | struct scatterlist *sg; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3014 | |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3015 | for_each_sg(sglist, sg, nelems, i) { |
FUJITA Tomonori | 12d4d40 | 2007-10-23 09:32:25 +0200 | [diff] [blame] | 3016 | BUG_ON(!sg_page(sg)); |
David Woodhouse | 4cf2e75 | 2009-02-11 17:23:43 +0000 | [diff] [blame] | 3017 | sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3018 | sg->dma_length = sg->length; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3019 | } |
| 3020 | return nelems; |
| 3021 | } |
| 3022 | |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 3023 | static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems, |
| 3024 | enum dma_data_direction dir, struct dma_attrs *attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3025 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3026 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3027 | struct pci_dev *pdev = to_pci_dev(hwdev); |
| 3028 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3029 | size_t size = 0; |
| 3030 | int prot = 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3031 | struct iova *iova = NULL; |
| 3032 | int ret; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3033 | struct scatterlist *sg; |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 3034 | unsigned long start_vpfn; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3035 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3036 | |
| 3037 | BUG_ON(dir == DMA_NONE); |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 3038 | if (iommu_no_mapping(hwdev)) |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3039 | return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3040 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3041 | domain = get_valid_domain_for_dev(pdev); |
| 3042 | if (!domain) |
| 3043 | return 0; |
| 3044 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3045 | iommu = domain_get_iommu(domain); |
| 3046 | |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 3047 | for_each_sg(sglist, sg, nelems, i) |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 3048 | size += aligned_nrpages(sg->offset, sg->length); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3049 | |
David Woodhouse | 5a5e02a | 2009-07-04 09:35:44 +0100 | [diff] [blame] | 3050 | iova = intel_alloc_iova(hwdev, domain, dma_to_mm_pfn(size), |
| 3051 | pdev->dma_mask); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3052 | if (!iova) { |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3053 | sglist->dma_length = 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3054 | return 0; |
| 3055 | } |
| 3056 | |
| 3057 | /* |
| 3058 | * Check if DMAR supports zero-length reads on write only |
| 3059 | * mappings.. |
| 3060 | */ |
| 3061 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3062 | !cap_zlr(iommu->cap)) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3063 | prot |= DMA_PTE_READ; |
| 3064 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 3065 | prot |= DMA_PTE_WRITE; |
| 3066 | |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 3067 | start_vpfn = mm_to_dma_pfn(iova->pfn_lo); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 3068 | |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 3069 | ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 3070 | if (unlikely(ret)) { |
| 3071 | /* clear the page */ |
| 3072 | dma_pte_clear_range(domain, start_vpfn, |
| 3073 | start_vpfn + size - 1); |
| 3074 | /* free page tables */ |
| 3075 | dma_pte_free_pagetable(domain, start_vpfn, |
| 3076 | start_vpfn + size - 1); |
| 3077 | /* free iova */ |
| 3078 | __free_iova(&domain->iovad, iova); |
| 3079 | return 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3080 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3081 | |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3082 | /* it's a non-present to present mapping. Only flush if caching mode */ |
| 3083 | if (cap_caching_mode(iommu->cap)) |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 3084 | iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 1); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3085 | else |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3086 | iommu_flush_write_buffer(iommu); |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3087 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3088 | return nelems; |
| 3089 | } |
| 3090 | |
FUJITA Tomonori | dfb805e | 2009-01-28 21:53:17 +0900 | [diff] [blame] | 3091 | static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 3092 | { |
| 3093 | return !dma_addr; |
| 3094 | } |
| 3095 | |
FUJITA Tomonori | 160c1d8 | 2009-01-05 23:59:02 +0900 | [diff] [blame] | 3096 | struct dma_map_ops intel_dma_ops = { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3097 | .alloc_coherent = intel_alloc_coherent, |
| 3098 | .free_coherent = intel_free_coherent, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3099 | .map_sg = intel_map_sg, |
| 3100 | .unmap_sg = intel_unmap_sg, |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 3101 | .map_page = intel_map_page, |
| 3102 | .unmap_page = intel_unmap_page, |
FUJITA Tomonori | dfb805e | 2009-01-28 21:53:17 +0900 | [diff] [blame] | 3103 | .mapping_error = intel_mapping_error, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3104 | }; |
| 3105 | |
| 3106 | static inline int iommu_domain_cache_init(void) |
| 3107 | { |
| 3108 | int ret = 0; |
| 3109 | |
| 3110 | iommu_domain_cache = kmem_cache_create("iommu_domain", |
| 3111 | sizeof(struct dmar_domain), |
| 3112 | 0, |
| 3113 | SLAB_HWCACHE_ALIGN, |
| 3114 | |
| 3115 | NULL); |
| 3116 | if (!iommu_domain_cache) { |
| 3117 | printk(KERN_ERR "Couldn't create iommu_domain cache\n"); |
| 3118 | ret = -ENOMEM; |
| 3119 | } |
| 3120 | |
| 3121 | return ret; |
| 3122 | } |
| 3123 | |
| 3124 | static inline int iommu_devinfo_cache_init(void) |
| 3125 | { |
| 3126 | int ret = 0; |
| 3127 | |
| 3128 | iommu_devinfo_cache = kmem_cache_create("iommu_devinfo", |
| 3129 | sizeof(struct device_domain_info), |
| 3130 | 0, |
| 3131 | SLAB_HWCACHE_ALIGN, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3132 | NULL); |
| 3133 | if (!iommu_devinfo_cache) { |
| 3134 | printk(KERN_ERR "Couldn't create devinfo cache\n"); |
| 3135 | ret = -ENOMEM; |
| 3136 | } |
| 3137 | |
| 3138 | return ret; |
| 3139 | } |
| 3140 | |
| 3141 | static inline int iommu_iova_cache_init(void) |
| 3142 | { |
| 3143 | int ret = 0; |
| 3144 | |
| 3145 | iommu_iova_cache = kmem_cache_create("iommu_iova", |
| 3146 | sizeof(struct iova), |
| 3147 | 0, |
| 3148 | SLAB_HWCACHE_ALIGN, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3149 | NULL); |
| 3150 | if (!iommu_iova_cache) { |
| 3151 | printk(KERN_ERR "Couldn't create iova cache\n"); |
| 3152 | ret = -ENOMEM; |
| 3153 | } |
| 3154 | |
| 3155 | return ret; |
| 3156 | } |
| 3157 | |
| 3158 | static int __init iommu_init_mempool(void) |
| 3159 | { |
| 3160 | int ret; |
| 3161 | ret = iommu_iova_cache_init(); |
| 3162 | if (ret) |
| 3163 | return ret; |
| 3164 | |
| 3165 | ret = iommu_domain_cache_init(); |
| 3166 | if (ret) |
| 3167 | goto domain_error; |
| 3168 | |
| 3169 | ret = iommu_devinfo_cache_init(); |
| 3170 | if (!ret) |
| 3171 | return ret; |
| 3172 | |
| 3173 | kmem_cache_destroy(iommu_domain_cache); |
| 3174 | domain_error: |
| 3175 | kmem_cache_destroy(iommu_iova_cache); |
| 3176 | |
| 3177 | return -ENOMEM; |
| 3178 | } |
| 3179 | |
| 3180 | static void __init iommu_exit_mempool(void) |
| 3181 | { |
| 3182 | kmem_cache_destroy(iommu_devinfo_cache); |
| 3183 | kmem_cache_destroy(iommu_domain_cache); |
| 3184 | kmem_cache_destroy(iommu_iova_cache); |
| 3185 | |
| 3186 | } |
| 3187 | |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 3188 | static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev) |
| 3189 | { |
| 3190 | struct dmar_drhd_unit *drhd; |
| 3191 | u32 vtbar; |
| 3192 | int rc; |
| 3193 | |
| 3194 | /* We know that this device on this chipset has its own IOMMU. |
| 3195 | * If we find it under a different IOMMU, then the BIOS is lying |
| 3196 | * to us. Hope that the IOMMU for this device is actually |
| 3197 | * disabled, and it needs no translation... |
| 3198 | */ |
| 3199 | rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar); |
| 3200 | if (rc) { |
| 3201 | /* "can't" happen */ |
| 3202 | dev_info(&pdev->dev, "failed to run vt-d quirk\n"); |
| 3203 | return; |
| 3204 | } |
| 3205 | vtbar &= 0xffff0000; |
| 3206 | |
| 3207 | /* we know that the this iommu should be at offset 0xa000 from vtbar */ |
| 3208 | drhd = dmar_find_matched_drhd_unit(pdev); |
| 3209 | if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000, |
| 3210 | TAINT_FIRMWARE_WORKAROUND, |
| 3211 | "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n")) |
| 3212 | pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO; |
| 3213 | } |
| 3214 | DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu); |
| 3215 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3216 | static void __init init_no_remapping_devices(void) |
| 3217 | { |
| 3218 | struct dmar_drhd_unit *drhd; |
| 3219 | |
| 3220 | for_each_drhd_unit(drhd) { |
| 3221 | if (!drhd->include_all) { |
| 3222 | int i; |
| 3223 | for (i = 0; i < drhd->devices_cnt; i++) |
| 3224 | if (drhd->devices[i] != NULL) |
| 3225 | break; |
| 3226 | /* ignore DMAR unit if no pci devices exist */ |
| 3227 | if (i == drhd->devices_cnt) |
| 3228 | drhd->ignored = 1; |
| 3229 | } |
| 3230 | } |
| 3231 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3232 | for_each_drhd_unit(drhd) { |
| 3233 | int i; |
| 3234 | if (drhd->ignored || drhd->include_all) |
| 3235 | continue; |
| 3236 | |
| 3237 | for (i = 0; i < drhd->devices_cnt; i++) |
| 3238 | if (drhd->devices[i] && |
David Woodhouse | c0771df | 2011-10-14 20:59:46 +0100 | [diff] [blame] | 3239 | !IS_GFX_DEVICE(drhd->devices[i])) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3240 | break; |
| 3241 | |
| 3242 | if (i < drhd->devices_cnt) |
| 3243 | continue; |
| 3244 | |
David Woodhouse | c0771df | 2011-10-14 20:59:46 +0100 | [diff] [blame] | 3245 | /* This IOMMU has *only* gfx devices. Either bypass it or |
| 3246 | set the gfx_mapped flag, as appropriate */ |
| 3247 | if (dmar_map_gfx) { |
| 3248 | intel_iommu_gfx_mapped = 1; |
| 3249 | } else { |
| 3250 | drhd->ignored = 1; |
| 3251 | for (i = 0; i < drhd->devices_cnt; i++) { |
| 3252 | if (!drhd->devices[i]) |
| 3253 | continue; |
| 3254 | drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO; |
| 3255 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3256 | } |
| 3257 | } |
| 3258 | } |
| 3259 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3260 | #ifdef CONFIG_SUSPEND |
| 3261 | static int init_iommu_hw(void) |
| 3262 | { |
| 3263 | struct dmar_drhd_unit *drhd; |
| 3264 | struct intel_iommu *iommu = NULL; |
| 3265 | |
| 3266 | for_each_active_iommu(iommu, drhd) |
| 3267 | if (iommu->qi) |
| 3268 | dmar_reenable_qi(iommu); |
| 3269 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3270 | for_each_iommu(iommu, drhd) { |
| 3271 | if (drhd->ignored) { |
| 3272 | /* |
| 3273 | * we always have to disable PMRs or DMA may fail on |
| 3274 | * this device |
| 3275 | */ |
| 3276 | if (force_on) |
| 3277 | iommu_disable_protect_mem_regions(iommu); |
| 3278 | continue; |
| 3279 | } |
| 3280 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3281 | iommu_flush_write_buffer(iommu); |
| 3282 | |
| 3283 | iommu_set_root_entry(iommu); |
| 3284 | |
| 3285 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3286 | DMA_CCMD_GLOBAL_INVL); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3287 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3288 | DMA_TLB_GLOBAL_FLUSH); |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3289 | if (iommu_enable_translation(iommu)) |
| 3290 | return 1; |
David Woodhouse | b94996c | 2009-09-19 15:28:12 -0700 | [diff] [blame] | 3291 | iommu_disable_protect_mem_regions(iommu); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | return 0; |
| 3295 | } |
| 3296 | |
| 3297 | static void iommu_flush_all(void) |
| 3298 | { |
| 3299 | struct dmar_drhd_unit *drhd; |
| 3300 | struct intel_iommu *iommu; |
| 3301 | |
| 3302 | for_each_active_iommu(iommu, drhd) { |
| 3303 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3304 | DMA_CCMD_GLOBAL_INVL); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3305 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 3306 | DMA_TLB_GLOBAL_FLUSH); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3307 | } |
| 3308 | } |
| 3309 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 3310 | static int iommu_suspend(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3311 | { |
| 3312 | struct dmar_drhd_unit *drhd; |
| 3313 | struct intel_iommu *iommu = NULL; |
| 3314 | unsigned long flag; |
| 3315 | |
| 3316 | for_each_active_iommu(iommu, drhd) { |
| 3317 | iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS, |
| 3318 | GFP_ATOMIC); |
| 3319 | if (!iommu->iommu_state) |
| 3320 | goto nomem; |
| 3321 | } |
| 3322 | |
| 3323 | iommu_flush_all(); |
| 3324 | |
| 3325 | for_each_active_iommu(iommu, drhd) { |
| 3326 | iommu_disable_translation(iommu); |
| 3327 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 3328 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3329 | |
| 3330 | iommu->iommu_state[SR_DMAR_FECTL_REG] = |
| 3331 | readl(iommu->reg + DMAR_FECTL_REG); |
| 3332 | iommu->iommu_state[SR_DMAR_FEDATA_REG] = |
| 3333 | readl(iommu->reg + DMAR_FEDATA_REG); |
| 3334 | iommu->iommu_state[SR_DMAR_FEADDR_REG] = |
| 3335 | readl(iommu->reg + DMAR_FEADDR_REG); |
| 3336 | iommu->iommu_state[SR_DMAR_FEUADDR_REG] = |
| 3337 | readl(iommu->reg + DMAR_FEUADDR_REG); |
| 3338 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 3339 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3340 | } |
| 3341 | return 0; |
| 3342 | |
| 3343 | nomem: |
| 3344 | for_each_active_iommu(iommu, drhd) |
| 3345 | kfree(iommu->iommu_state); |
| 3346 | |
| 3347 | return -ENOMEM; |
| 3348 | } |
| 3349 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 3350 | static void iommu_resume(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3351 | { |
| 3352 | struct dmar_drhd_unit *drhd; |
| 3353 | struct intel_iommu *iommu = NULL; |
| 3354 | unsigned long flag; |
| 3355 | |
| 3356 | if (init_iommu_hw()) { |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3357 | if (force_on) |
| 3358 | panic("tboot: IOMMU setup failed, DMAR can not resume!\n"); |
| 3359 | else |
| 3360 | WARN(1, "IOMMU setup failed, DMAR can not resume!\n"); |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 3361 | return; |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | for_each_active_iommu(iommu, drhd) { |
| 3365 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 3366 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3367 | |
| 3368 | writel(iommu->iommu_state[SR_DMAR_FECTL_REG], |
| 3369 | iommu->reg + DMAR_FECTL_REG); |
| 3370 | writel(iommu->iommu_state[SR_DMAR_FEDATA_REG], |
| 3371 | iommu->reg + DMAR_FEDATA_REG); |
| 3372 | writel(iommu->iommu_state[SR_DMAR_FEADDR_REG], |
| 3373 | iommu->reg + DMAR_FEADDR_REG); |
| 3374 | writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG], |
| 3375 | iommu->reg + DMAR_FEUADDR_REG); |
| 3376 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 3377 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | for_each_active_iommu(iommu, drhd) |
| 3381 | kfree(iommu->iommu_state); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3382 | } |
| 3383 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 3384 | static struct syscore_ops iommu_syscore_ops = { |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3385 | .resume = iommu_resume, |
| 3386 | .suspend = iommu_suspend, |
| 3387 | }; |
| 3388 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 3389 | static void __init init_iommu_pm_ops(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3390 | { |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 3391 | register_syscore_ops(&iommu_syscore_ops); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | #else |
Rafael J. Wysocki | 99592ba | 2011-06-07 21:32:31 +0200 | [diff] [blame] | 3395 | static inline void init_iommu_pm_ops(void) {} |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3396 | #endif /* CONFIG_PM */ |
| 3397 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 3398 | LIST_HEAD(dmar_rmrr_units); |
| 3399 | |
| 3400 | static void __init dmar_register_rmrr_unit(struct dmar_rmrr_unit *rmrr) |
| 3401 | { |
| 3402 | list_add(&rmrr->list, &dmar_rmrr_units); |
| 3403 | } |
| 3404 | |
| 3405 | |
| 3406 | int __init dmar_parse_one_rmrr(struct acpi_dmar_header *header) |
| 3407 | { |
| 3408 | struct acpi_dmar_reserved_memory *rmrr; |
| 3409 | struct dmar_rmrr_unit *rmrru; |
| 3410 | |
| 3411 | rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL); |
| 3412 | if (!rmrru) |
| 3413 | return -ENOMEM; |
| 3414 | |
| 3415 | rmrru->hdr = header; |
| 3416 | rmrr = (struct acpi_dmar_reserved_memory *)header; |
| 3417 | rmrru->base_address = rmrr->base_address; |
| 3418 | rmrru->end_address = rmrr->end_address; |
| 3419 | |
| 3420 | dmar_register_rmrr_unit(rmrru); |
| 3421 | return 0; |
| 3422 | } |
| 3423 | |
| 3424 | static int __init |
| 3425 | rmrr_parse_dev(struct dmar_rmrr_unit *rmrru) |
| 3426 | { |
| 3427 | struct acpi_dmar_reserved_memory *rmrr; |
| 3428 | int ret; |
| 3429 | |
| 3430 | rmrr = (struct acpi_dmar_reserved_memory *) rmrru->hdr; |
| 3431 | ret = dmar_parse_dev_scope((void *)(rmrr + 1), |
| 3432 | ((void *)rmrr) + rmrr->header.length, |
| 3433 | &rmrru->devices_cnt, &rmrru->devices, rmrr->segment); |
| 3434 | |
| 3435 | if (ret || (rmrru->devices_cnt == 0)) { |
| 3436 | list_del(&rmrru->list); |
| 3437 | kfree(rmrru); |
| 3438 | } |
| 3439 | return ret; |
| 3440 | } |
| 3441 | |
| 3442 | static LIST_HEAD(dmar_atsr_units); |
| 3443 | |
| 3444 | int __init dmar_parse_one_atsr(struct acpi_dmar_header *hdr) |
| 3445 | { |
| 3446 | struct acpi_dmar_atsr *atsr; |
| 3447 | struct dmar_atsr_unit *atsru; |
| 3448 | |
| 3449 | atsr = container_of(hdr, struct acpi_dmar_atsr, header); |
| 3450 | atsru = kzalloc(sizeof(*atsru), GFP_KERNEL); |
| 3451 | if (!atsru) |
| 3452 | return -ENOMEM; |
| 3453 | |
| 3454 | atsru->hdr = hdr; |
| 3455 | atsru->include_all = atsr->flags & 0x1; |
| 3456 | |
| 3457 | list_add(&atsru->list, &dmar_atsr_units); |
| 3458 | |
| 3459 | return 0; |
| 3460 | } |
| 3461 | |
| 3462 | static int __init atsr_parse_dev(struct dmar_atsr_unit *atsru) |
| 3463 | { |
| 3464 | int rc; |
| 3465 | struct acpi_dmar_atsr *atsr; |
| 3466 | |
| 3467 | if (atsru->include_all) |
| 3468 | return 0; |
| 3469 | |
| 3470 | atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header); |
| 3471 | rc = dmar_parse_dev_scope((void *)(atsr + 1), |
| 3472 | (void *)atsr + atsr->header.length, |
| 3473 | &atsru->devices_cnt, &atsru->devices, |
| 3474 | atsr->segment); |
| 3475 | if (rc || !atsru->devices_cnt) { |
| 3476 | list_del(&atsru->list); |
| 3477 | kfree(atsru); |
| 3478 | } |
| 3479 | |
| 3480 | return rc; |
| 3481 | } |
| 3482 | |
| 3483 | int dmar_find_matched_atsr_unit(struct pci_dev *dev) |
| 3484 | { |
| 3485 | int i; |
| 3486 | struct pci_bus *bus; |
| 3487 | struct acpi_dmar_atsr *atsr; |
| 3488 | struct dmar_atsr_unit *atsru; |
| 3489 | |
| 3490 | dev = pci_physfn(dev); |
| 3491 | |
| 3492 | list_for_each_entry(atsru, &dmar_atsr_units, list) { |
| 3493 | atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header); |
| 3494 | if (atsr->segment == pci_domain_nr(dev->bus)) |
| 3495 | goto found; |
| 3496 | } |
| 3497 | |
| 3498 | return 0; |
| 3499 | |
| 3500 | found: |
| 3501 | for (bus = dev->bus; bus; bus = bus->parent) { |
| 3502 | struct pci_dev *bridge = bus->self; |
| 3503 | |
| 3504 | if (!bridge || !pci_is_pcie(bridge) || |
| 3505 | bridge->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE) |
| 3506 | return 0; |
| 3507 | |
| 3508 | if (bridge->pcie_type == PCI_EXP_TYPE_ROOT_PORT) { |
| 3509 | for (i = 0; i < atsru->devices_cnt; i++) |
| 3510 | if (atsru->devices[i] == bridge) |
| 3511 | return 1; |
| 3512 | break; |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | if (atsru->include_all) |
| 3517 | return 1; |
| 3518 | |
| 3519 | return 0; |
| 3520 | } |
| 3521 | |
| 3522 | int dmar_parse_rmrr_atsr_dev(void) |
| 3523 | { |
| 3524 | struct dmar_rmrr_unit *rmrr, *rmrr_n; |
| 3525 | struct dmar_atsr_unit *atsr, *atsr_n; |
| 3526 | int ret = 0; |
| 3527 | |
| 3528 | list_for_each_entry_safe(rmrr, rmrr_n, &dmar_rmrr_units, list) { |
| 3529 | ret = rmrr_parse_dev(rmrr); |
| 3530 | if (ret) |
| 3531 | return ret; |
| 3532 | } |
| 3533 | |
| 3534 | list_for_each_entry_safe(atsr, atsr_n, &dmar_atsr_units, list) { |
| 3535 | ret = atsr_parse_dev(atsr); |
| 3536 | if (ret) |
| 3537 | return ret; |
| 3538 | } |
| 3539 | |
| 3540 | return ret; |
| 3541 | } |
| 3542 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 3543 | /* |
| 3544 | * Here we only respond to action of unbound device from driver. |
| 3545 | * |
| 3546 | * Added device is not attached to its DMAR domain here yet. That will happen |
| 3547 | * when mapping the device to iova. |
| 3548 | */ |
| 3549 | static int device_notifier(struct notifier_block *nb, |
| 3550 | unsigned long action, void *data) |
| 3551 | { |
| 3552 | struct device *dev = data; |
| 3553 | struct pci_dev *pdev = to_pci_dev(dev); |
| 3554 | struct dmar_domain *domain; |
| 3555 | |
David Woodhouse | 44cd613 | 2009-12-02 10:18:30 +0000 | [diff] [blame] | 3556 | if (iommu_no_mapping(dev)) |
| 3557 | return 0; |
| 3558 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 3559 | domain = find_domain(pdev); |
| 3560 | if (!domain) |
| 3561 | return 0; |
| 3562 | |
Alex Williamson | a97590e | 2011-03-04 14:52:16 -0700 | [diff] [blame] | 3563 | if (action == BUS_NOTIFY_UNBOUND_DRIVER && !iommu_pass_through) { |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 3564 | domain_remove_one_dev_info(domain, pdev); |
| 3565 | |
Alex Williamson | a97590e | 2011-03-04 14:52:16 -0700 | [diff] [blame] | 3566 | if (!(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) && |
| 3567 | !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) && |
| 3568 | list_empty(&domain->devices)) |
| 3569 | domain_exit(domain); |
| 3570 | } |
| 3571 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 3572 | return 0; |
| 3573 | } |
| 3574 | |
| 3575 | static struct notifier_block device_nb = { |
| 3576 | .notifier_call = device_notifier, |
| 3577 | }; |
| 3578 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3579 | int __init intel_iommu_init(void) |
| 3580 | { |
| 3581 | int ret = 0; |
| 3582 | |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 3583 | /* VT-d is required for a TXT/tboot launch, so enforce that */ |
| 3584 | force_on = tboot_force_iommu(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3585 | |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 3586 | if (dmar_table_init()) { |
| 3587 | if (force_on) |
| 3588 | panic("tboot: Failed to initialize DMAR table\n"); |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 3589 | return -ENODEV; |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 3592 | if (dmar_dev_scope_init() < 0) { |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 3593 | if (force_on) |
| 3594 | panic("tboot: Failed to initialize DMAR device scope\n"); |
| 3595 | return -ENODEV; |
| 3596 | } |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 3597 | |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 3598 | if (no_iommu || dmar_disabled) |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 3599 | return -ENODEV; |
| 3600 | |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 3601 | if (iommu_init_mempool()) { |
| 3602 | if (force_on) |
| 3603 | panic("tboot: Failed to initialize iommu memory\n"); |
| 3604 | return -ENODEV; |
| 3605 | } |
| 3606 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 3607 | if (list_empty(&dmar_rmrr_units)) |
| 3608 | printk(KERN_INFO "DMAR: No RMRR found\n"); |
| 3609 | |
| 3610 | if (list_empty(&dmar_atsr_units)) |
| 3611 | printk(KERN_INFO "DMAR: No ATSR found\n"); |
| 3612 | |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 3613 | if (dmar_init_reserved_ranges()) { |
| 3614 | if (force_on) |
| 3615 | panic("tboot: Failed to reserve iommu ranges\n"); |
| 3616 | return -ENODEV; |
| 3617 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3618 | |
| 3619 | init_no_remapping_devices(); |
| 3620 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3621 | ret = init_dmars(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3622 | if (ret) { |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 3623 | if (force_on) |
| 3624 | panic("tboot: Failed to initialize DMARs\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3625 | printk(KERN_ERR "IOMMU: dmar init failed\n"); |
| 3626 | put_iova_domain(&reserved_iova_list); |
| 3627 | iommu_exit_mempool(); |
| 3628 | return ret; |
| 3629 | } |
| 3630 | printk(KERN_INFO |
| 3631 | "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n"); |
| 3632 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3633 | init_timer(&unmap_timer); |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 3634 | #ifdef CONFIG_SWIOTLB |
| 3635 | swiotlb = 0; |
| 3636 | #endif |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3637 | dma_ops = &intel_dma_ops; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3638 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 3639 | init_iommu_pm_ops(); |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 3640 | |
Joerg Roedel | 4236d97d | 2011-09-06 17:56:07 +0200 | [diff] [blame] | 3641 | bus_set_iommu(&pci_bus_type, &intel_iommu_ops); |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 3642 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 3643 | bus_register_notifier(&pci_bus_type, &device_nb); |
| 3644 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3645 | return 0; |
| 3646 | } |
Keshavamurthy, Anil S | e820482 | 2007-10-21 16:41:55 -0700 | [diff] [blame] | 3647 | |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3648 | static void iommu_detach_dependent_devices(struct intel_iommu *iommu, |
| 3649 | struct pci_dev *pdev) |
| 3650 | { |
| 3651 | struct pci_dev *tmp, *parent; |
| 3652 | |
| 3653 | if (!iommu || !pdev) |
| 3654 | return; |
| 3655 | |
| 3656 | /* dependent device detach */ |
| 3657 | tmp = pci_find_upstream_pcie_bridge(pdev); |
| 3658 | /* Secondary interface's bus number and devfn 0 */ |
| 3659 | if (tmp) { |
| 3660 | parent = pdev->bus->self; |
| 3661 | while (parent != tmp) { |
| 3662 | iommu_detach_dev(iommu, parent->bus->number, |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3663 | parent->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3664 | parent = parent->bus->self; |
| 3665 | } |
Stefan Assmann | 45e829e | 2009-12-03 06:49:24 -0500 | [diff] [blame] | 3666 | if (pci_is_pcie(tmp)) /* this is a PCIe-to-PCI bridge */ |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3667 | iommu_detach_dev(iommu, |
| 3668 | tmp->subordinate->number, 0); |
| 3669 | else /* this is a legacy PCI bridge */ |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3670 | iommu_detach_dev(iommu, tmp->bus->number, |
| 3671 | tmp->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3672 | } |
| 3673 | } |
| 3674 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3675 | static void domain_remove_one_dev_info(struct dmar_domain *domain, |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3676 | struct pci_dev *pdev) |
| 3677 | { |
| 3678 | struct device_domain_info *info; |
| 3679 | struct intel_iommu *iommu; |
| 3680 | unsigned long flags; |
| 3681 | int found = 0; |
| 3682 | struct list_head *entry, *tmp; |
| 3683 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3684 | iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number, |
| 3685 | pdev->devfn); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3686 | if (!iommu) |
| 3687 | return; |
| 3688 | |
| 3689 | spin_lock_irqsave(&device_domain_lock, flags); |
| 3690 | list_for_each_safe(entry, tmp, &domain->devices) { |
| 3691 | info = list_entry(entry, struct device_domain_info, link); |
Mike Habeck | 8519dc4 | 2011-05-28 13:15:07 -0500 | [diff] [blame] | 3692 | if (info->segment == pci_domain_nr(pdev->bus) && |
| 3693 | info->bus == pdev->bus->number && |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3694 | info->devfn == pdev->devfn) { |
| 3695 | list_del(&info->link); |
| 3696 | list_del(&info->global); |
| 3697 | if (info->dev) |
| 3698 | info->dev->dev.archdata.iommu = NULL; |
| 3699 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 3700 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 3701 | iommu_disable_dev_iotlb(info); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3702 | iommu_detach_dev(iommu, info->bus, info->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3703 | iommu_detach_dependent_devices(iommu, pdev); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3704 | free_devinfo_mem(info); |
| 3705 | |
| 3706 | spin_lock_irqsave(&device_domain_lock, flags); |
| 3707 | |
| 3708 | if (found) |
| 3709 | break; |
| 3710 | else |
| 3711 | continue; |
| 3712 | } |
| 3713 | |
| 3714 | /* if there is no other devices under the same iommu |
| 3715 | * owned by this domain, clear this iommu in iommu_bmp |
| 3716 | * update iommu count and coherency |
| 3717 | */ |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3718 | if (iommu == device_to_iommu(info->segment, info->bus, |
| 3719 | info->devfn)) |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3720 | found = 1; |
| 3721 | } |
| 3722 | |
Roland Dreier | 3e7abe2 | 2011-07-20 06:22:21 -0700 | [diff] [blame] | 3723 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 3724 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3725 | if (found == 0) { |
| 3726 | unsigned long tmp_flags; |
| 3727 | spin_lock_irqsave(&domain->iommu_lock, tmp_flags); |
| 3728 | clear_bit(iommu->seq_id, &domain->iommu_bmp); |
| 3729 | domain->iommu_count--; |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 3730 | domain_update_iommu_cap(domain); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3731 | spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags); |
Alex Williamson | a97590e | 2011-03-04 14:52:16 -0700 | [diff] [blame] | 3732 | |
Alex Williamson | 9b4554b | 2011-05-24 12:19:04 -0400 | [diff] [blame] | 3733 | if (!(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) && |
| 3734 | !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)) { |
| 3735 | spin_lock_irqsave(&iommu->lock, tmp_flags); |
| 3736 | clear_bit(domain->id, iommu->domain_ids); |
| 3737 | iommu->domains[domain->id] = NULL; |
| 3738 | spin_unlock_irqrestore(&iommu->lock, tmp_flags); |
| 3739 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3740 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | static void vm_domain_remove_all_dev_info(struct dmar_domain *domain) |
| 3744 | { |
| 3745 | struct device_domain_info *info; |
| 3746 | struct intel_iommu *iommu; |
| 3747 | unsigned long flags1, flags2; |
| 3748 | |
| 3749 | spin_lock_irqsave(&device_domain_lock, flags1); |
| 3750 | while (!list_empty(&domain->devices)) { |
| 3751 | info = list_entry(domain->devices.next, |
| 3752 | struct device_domain_info, link); |
| 3753 | list_del(&info->link); |
| 3754 | list_del(&info->global); |
| 3755 | if (info->dev) |
| 3756 | info->dev->dev.archdata.iommu = NULL; |
| 3757 | |
| 3758 | spin_unlock_irqrestore(&device_domain_lock, flags1); |
| 3759 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 3760 | iommu_disable_dev_iotlb(info); |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3761 | iommu = device_to_iommu(info->segment, info->bus, info->devfn); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3762 | iommu_detach_dev(iommu, info->bus, info->devfn); |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 3763 | iommu_detach_dependent_devices(iommu, info->dev); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3764 | |
| 3765 | /* clear this iommu in iommu_bmp, update iommu count |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 3766 | * and capabilities |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3767 | */ |
| 3768 | spin_lock_irqsave(&domain->iommu_lock, flags2); |
| 3769 | if (test_and_clear_bit(iommu->seq_id, |
| 3770 | &domain->iommu_bmp)) { |
| 3771 | domain->iommu_count--; |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 3772 | domain_update_iommu_cap(domain); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 3773 | } |
| 3774 | spin_unlock_irqrestore(&domain->iommu_lock, flags2); |
| 3775 | |
| 3776 | free_devinfo_mem(info); |
| 3777 | spin_lock_irqsave(&device_domain_lock, flags1); |
| 3778 | } |
| 3779 | spin_unlock_irqrestore(&device_domain_lock, flags1); |
| 3780 | } |
| 3781 | |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3782 | /* domain id for virtual machine, it won't be set in context */ |
| 3783 | static unsigned long vm_domid; |
| 3784 | |
| 3785 | static struct dmar_domain *iommu_alloc_vm_domain(void) |
| 3786 | { |
| 3787 | struct dmar_domain *domain; |
| 3788 | |
| 3789 | domain = alloc_domain_mem(); |
| 3790 | if (!domain) |
| 3791 | return NULL; |
| 3792 | |
| 3793 | domain->id = vm_domid++; |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 3794 | domain->nid = -1; |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3795 | memset(&domain->iommu_bmp, 0, sizeof(unsigned long)); |
| 3796 | domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE; |
| 3797 | |
| 3798 | return domain; |
| 3799 | } |
| 3800 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3801 | static int md_domain_init(struct dmar_domain *domain, int guest_width) |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3802 | { |
| 3803 | int adjust_width; |
| 3804 | |
| 3805 | init_iova_domain(&domain->iovad, DMA_32BIT_PFN); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3806 | spin_lock_init(&domain->iommu_lock); |
| 3807 | |
| 3808 | domain_reserve_special_ranges(domain); |
| 3809 | |
| 3810 | /* calculate AGAW */ |
| 3811 | domain->gaw = guest_width; |
| 3812 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 3813 | domain->agaw = width_to_agaw(adjust_width); |
| 3814 | |
| 3815 | INIT_LIST_HEAD(&domain->devices); |
| 3816 | |
| 3817 | domain->iommu_count = 0; |
| 3818 | domain->iommu_coherency = 0; |
Sheng Yang | c5b1525 | 2009-08-06 13:31:56 +0800 | [diff] [blame] | 3819 | domain->iommu_snooping = 0; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 3820 | domain->iommu_superpage = 0; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3821 | domain->max_addr = 0; |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 3822 | domain->nid = -1; |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3823 | |
| 3824 | /* always allocate the top pgd */ |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 3825 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3826 | if (!domain->pgd) |
| 3827 | return -ENOMEM; |
| 3828 | domain_flush_cache(domain, domain->pgd, PAGE_SIZE); |
| 3829 | return 0; |
| 3830 | } |
| 3831 | |
| 3832 | static void iommu_free_vm_domain(struct dmar_domain *domain) |
| 3833 | { |
| 3834 | unsigned long flags; |
| 3835 | struct dmar_drhd_unit *drhd; |
| 3836 | struct intel_iommu *iommu; |
| 3837 | unsigned long i; |
| 3838 | unsigned long ndomains; |
| 3839 | |
| 3840 | for_each_drhd_unit(drhd) { |
| 3841 | if (drhd->ignored) |
| 3842 | continue; |
| 3843 | iommu = drhd->iommu; |
| 3844 | |
| 3845 | ndomains = cap_ndoms(iommu->cap); |
Akinobu Mita | a45946a | 2010-03-11 14:04:08 -0800 | [diff] [blame] | 3846 | for_each_set_bit(i, iommu->domain_ids, ndomains) { |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3847 | if (iommu->domains[i] == domain) { |
| 3848 | spin_lock_irqsave(&iommu->lock, flags); |
| 3849 | clear_bit(i, iommu->domain_ids); |
| 3850 | iommu->domains[i] = NULL; |
| 3851 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 3852 | break; |
| 3853 | } |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3854 | } |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | static void vm_domain_exit(struct dmar_domain *domain) |
| 3859 | { |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3860 | /* Domain 0 is reserved, so dont process it */ |
| 3861 | if (!domain) |
| 3862 | return; |
| 3863 | |
| 3864 | vm_domain_remove_all_dev_info(domain); |
| 3865 | /* destroy iovas */ |
| 3866 | put_iova_domain(&domain->iovad); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3867 | |
| 3868 | /* clear ptes */ |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 3869 | dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3870 | |
| 3871 | /* free page tables */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3872 | dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 3873 | |
| 3874 | iommu_free_vm_domain(domain); |
| 3875 | free_domain_mem(domain); |
| 3876 | } |
| 3877 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3878 | static int intel_iommu_domain_init(struct iommu_domain *domain) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3879 | { |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3880 | struct dmar_domain *dmar_domain; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3881 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3882 | dmar_domain = iommu_alloc_vm_domain(); |
| 3883 | if (!dmar_domain) { |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3884 | printk(KERN_ERR |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3885 | "intel_iommu_domain_init: dmar_domain == NULL\n"); |
| 3886 | return -ENOMEM; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3887 | } |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3888 | if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3889 | printk(KERN_ERR |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3890 | "intel_iommu_domain_init() failed\n"); |
| 3891 | vm_domain_exit(dmar_domain); |
| 3892 | return -ENOMEM; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3893 | } |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 3894 | domain_update_iommu_cap(dmar_domain); |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3895 | domain->priv = dmar_domain; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3896 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3897 | return 0; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3898 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3899 | |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3900 | static void intel_iommu_domain_destroy(struct iommu_domain *domain) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3901 | { |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 3902 | struct dmar_domain *dmar_domain = domain->priv; |
| 3903 | |
| 3904 | domain->priv = NULL; |
| 3905 | vm_domain_exit(dmar_domain); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3906 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3907 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3908 | static int intel_iommu_attach_device(struct iommu_domain *domain, |
| 3909 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3910 | { |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3911 | struct dmar_domain *dmar_domain = domain->priv; |
| 3912 | struct pci_dev *pdev = to_pci_dev(dev); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3913 | struct intel_iommu *iommu; |
| 3914 | int addr_width; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3915 | |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3916 | /* normally pdev is not mapped */ |
| 3917 | if (unlikely(domain_context_mapped(pdev))) { |
| 3918 | struct dmar_domain *old_domain; |
| 3919 | |
| 3920 | old_domain = find_domain(pdev); |
| 3921 | if (old_domain) { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3922 | if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE || |
| 3923 | dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) |
| 3924 | domain_remove_one_dev_info(old_domain, pdev); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3925 | else |
| 3926 | domain_remove_dev_info(old_domain); |
| 3927 | } |
| 3928 | } |
| 3929 | |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 3930 | iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number, |
| 3931 | pdev->devfn); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3932 | if (!iommu) |
| 3933 | return -ENODEV; |
| 3934 | |
| 3935 | /* check if this iommu agaw is sufficient for max mapped address */ |
| 3936 | addr_width = agaw_to_width(iommu->agaw); |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 3937 | if (addr_width > cap_mgaw(iommu->cap)) |
| 3938 | addr_width = cap_mgaw(iommu->cap); |
| 3939 | |
| 3940 | if (dmar_domain->max_addr > (1LL << addr_width)) { |
| 3941 | printk(KERN_ERR "%s: iommu width (%d) is not " |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3942 | "sufficient for the mapped address (%llx)\n", |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 3943 | __func__, addr_width, dmar_domain->max_addr); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3944 | return -EFAULT; |
| 3945 | } |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 3946 | dmar_domain->gaw = addr_width; |
| 3947 | |
| 3948 | /* |
| 3949 | * Knock out extra levels of page tables if necessary |
| 3950 | */ |
| 3951 | while (iommu->agaw < dmar_domain->agaw) { |
| 3952 | struct dma_pte *pte; |
| 3953 | |
| 3954 | pte = dmar_domain->pgd; |
| 3955 | if (dma_pte_present(pte)) { |
Sheng Yang | 25cbff1 | 2010-06-12 19:21:42 +0800 | [diff] [blame] | 3956 | dmar_domain->pgd = (struct dma_pte *) |
| 3957 | phys_to_virt(dma_pte_addr(pte)); |
Jan Kiszka | 7a66101 | 2010-11-02 08:05:51 +0100 | [diff] [blame] | 3958 | free_pgtable_page(pte); |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 3959 | } |
| 3960 | dmar_domain->agaw--; |
| 3961 | } |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3962 | |
David Woodhouse | 5fe60f4 | 2009-08-09 10:53:41 +0100 | [diff] [blame] | 3963 | return domain_add_dev_info(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3964 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3965 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3966 | static void intel_iommu_detach_device(struct iommu_domain *domain, |
| 3967 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3968 | { |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 3969 | struct dmar_domain *dmar_domain = domain->priv; |
| 3970 | struct pci_dev *pdev = to_pci_dev(dev); |
| 3971 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3972 | domain_remove_one_dev_info(dmar_domain, pdev); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3973 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3974 | |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 3975 | static int intel_iommu_map(struct iommu_domain *domain, |
| 3976 | unsigned long iova, phys_addr_t hpa, |
| 3977 | int gfp_order, int iommu_prot) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 3978 | { |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3979 | struct dmar_domain *dmar_domain = domain->priv; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3980 | u64 max_addr; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3981 | int prot = 0; |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 3982 | size_t size; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 3983 | int ret; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3984 | |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3985 | if (iommu_prot & IOMMU_READ) |
| 3986 | prot |= DMA_PTE_READ; |
| 3987 | if (iommu_prot & IOMMU_WRITE) |
| 3988 | prot |= DMA_PTE_WRITE; |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 3989 | if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping) |
| 3990 | prot |= DMA_PTE_SNP; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3991 | |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 3992 | size = PAGE_SIZE << gfp_order; |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 3993 | max_addr = iova + size; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 3994 | if (dmar_domain->max_addr < max_addr) { |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3995 | u64 end; |
| 3996 | |
| 3997 | /* check if minimum agaw is sufficient for mapped address */ |
Tom Lyon | 8954da1 | 2010-05-17 08:19:52 +0100 | [diff] [blame] | 3998 | end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 3999 | if (end < max_addr) { |
Tom Lyon | 8954da1 | 2010-05-17 08:19:52 +0100 | [diff] [blame] | 4000 | printk(KERN_ERR "%s: iommu width (%d) is not " |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4001 | "sufficient for the mapped address (%llx)\n", |
Tom Lyon | 8954da1 | 2010-05-17 08:19:52 +0100 | [diff] [blame] | 4002 | __func__, dmar_domain->gaw, max_addr); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4003 | return -EFAULT; |
| 4004 | } |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 4005 | dmar_domain->max_addr = max_addr; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4006 | } |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 4007 | /* Round up size to next multiple of PAGE_SIZE, if it and |
| 4008 | the low bits of hpa would take us onto the next page */ |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 4009 | size = aligned_nrpages(hpa, size); |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 4010 | ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT, |
| 4011 | hpa >> VTD_PAGE_SHIFT, size, prot); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4012 | return ret; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4013 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4014 | |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 4015 | static int intel_iommu_unmap(struct iommu_domain *domain, |
| 4016 | unsigned long iova, int gfp_order) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4017 | { |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 4018 | struct dmar_domain *dmar_domain = domain->priv; |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 4019 | size_t size = PAGE_SIZE << gfp_order; |
Allen Kay | 292827c | 2011-10-14 12:31:54 -0700 | [diff] [blame] | 4020 | int order; |
Sheng Yang | 4b99d35 | 2009-07-08 11:52:52 +0100 | [diff] [blame] | 4021 | |
Allen Kay | 292827c | 2011-10-14 12:31:54 -0700 | [diff] [blame] | 4022 | order = dma_pte_clear_range(dmar_domain, iova >> VTD_PAGE_SHIFT, |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 4023 | (iova + size - 1) >> VTD_PAGE_SHIFT); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4024 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 4025 | if (dmar_domain->max_addr == iova + size) |
| 4026 | dmar_domain->max_addr = iova; |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 4027 | |
Allen Kay | 292827c | 2011-10-14 12:31:54 -0700 | [diff] [blame] | 4028 | return order; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4029 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4030 | |
Joerg Roedel | d14d657 | 2008-12-03 15:06:57 +0100 | [diff] [blame] | 4031 | static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, |
| 4032 | unsigned long iova) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4033 | { |
Joerg Roedel | d14d657 | 2008-12-03 15:06:57 +0100 | [diff] [blame] | 4034 | struct dmar_domain *dmar_domain = domain->priv; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4035 | struct dma_pte *pte; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4036 | u64 phys = 0; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4037 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 4038 | pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, 0); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4039 | if (pte) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4040 | phys = dma_pte_addr(pte); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4041 | |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4042 | return phys; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4043 | } |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 4044 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 4045 | static int intel_iommu_domain_has_cap(struct iommu_domain *domain, |
| 4046 | unsigned long cap) |
| 4047 | { |
| 4048 | struct dmar_domain *dmar_domain = domain->priv; |
| 4049 | |
| 4050 | if (cap == IOMMU_CAP_CACHE_COHERENCY) |
| 4051 | return dmar_domain->iommu_snooping; |
Tom Lyon | 323f99c | 2010-07-02 16:56:14 -0400 | [diff] [blame] | 4052 | if (cap == IOMMU_CAP_INTR_REMAP) |
| 4053 | return intr_remapping_enabled; |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 4054 | |
| 4055 | return 0; |
| 4056 | } |
| 4057 | |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 4058 | static struct iommu_ops intel_iommu_ops = { |
| 4059 | .domain_init = intel_iommu_domain_init, |
| 4060 | .domain_destroy = intel_iommu_domain_destroy, |
| 4061 | .attach_dev = intel_iommu_attach_device, |
| 4062 | .detach_dev = intel_iommu_detach_device, |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 4063 | .map = intel_iommu_map, |
| 4064 | .unmap = intel_iommu_unmap, |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 4065 | .iova_to_phys = intel_iommu_iova_to_phys, |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 4066 | .domain_has_cap = intel_iommu_domain_has_cap, |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 4067 | }; |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 4068 | |
| 4069 | static void __devinit quirk_iommu_rwbf(struct pci_dev *dev) |
| 4070 | { |
| 4071 | /* |
| 4072 | * Mobile 4 Series Chipset neglects to set RWBF capability, |
| 4073 | * but needs it: |
| 4074 | */ |
| 4075 | printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n"); |
| 4076 | rwbf_quirk = 1; |
David Woodhouse | 2d9e667 | 2010-06-15 10:57:57 +0100 | [diff] [blame] | 4077 | |
| 4078 | /* https://bugzilla.redhat.com/show_bug.cgi?id=538163 */ |
| 4079 | if (dev->revision == 0x07) { |
| 4080 | printk(KERN_INFO "DMAR: Disabling IOMMU for graphics on this chipset\n"); |
| 4081 | dmar_map_gfx = 0; |
| 4082 | } |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf); |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 4086 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 4087 | #define GGC 0x52 |
| 4088 | #define GGC_MEMORY_SIZE_MASK (0xf << 8) |
| 4089 | #define GGC_MEMORY_SIZE_NONE (0x0 << 8) |
| 4090 | #define GGC_MEMORY_SIZE_1M (0x1 << 8) |
| 4091 | #define GGC_MEMORY_SIZE_2M (0x3 << 8) |
| 4092 | #define GGC_MEMORY_VT_ENABLED (0x8 << 8) |
| 4093 | #define GGC_MEMORY_SIZE_2M_VT (0x9 << 8) |
| 4094 | #define GGC_MEMORY_SIZE_3M_VT (0xa << 8) |
| 4095 | #define GGC_MEMORY_SIZE_4M_VT (0xb << 8) |
| 4096 | |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 4097 | static void __devinit quirk_calpella_no_shadow_gtt(struct pci_dev *dev) |
| 4098 | { |
| 4099 | unsigned short ggc; |
| 4100 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 4101 | if (pci_read_config_word(dev, GGC, &ggc)) |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 4102 | return; |
| 4103 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 4104 | if (!(ggc & GGC_MEMORY_VT_ENABLED)) { |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 4105 | printk(KERN_INFO "DMAR: BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n"); |
| 4106 | dmar_map_gfx = 0; |
David Woodhouse | 6fbcfb3 | 2011-09-25 19:11:14 -0700 | [diff] [blame] | 4107 | } else if (dmar_map_gfx) { |
| 4108 | /* we have to ensure the gfx device is idle before we flush */ |
| 4109 | printk(KERN_INFO "DMAR: Disabling batched IOTLB flush on Ironlake\n"); |
| 4110 | intel_iommu_strict = 1; |
| 4111 | } |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 4112 | } |
| 4113 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt); |
| 4114 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt); |
| 4115 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt); |
| 4116 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt); |
| 4117 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 4118 | /* On Tylersburg chipsets, some BIOSes have been known to enable the |
| 4119 | ISOCH DMAR unit for the Azalia sound device, but not give it any |
| 4120 | TLB entries, which causes it to deadlock. Check for that. We do |
| 4121 | this in a function called from init_dmars(), instead of in a PCI |
| 4122 | quirk, because we don't want to print the obnoxious "BIOS broken" |
| 4123 | message if VT-d is actually disabled. |
| 4124 | */ |
| 4125 | static void __init check_tylersburg_isoch(void) |
| 4126 | { |
| 4127 | struct pci_dev *pdev; |
| 4128 | uint32_t vtisochctrl; |
| 4129 | |
| 4130 | /* If there's no Azalia in the system anyway, forget it. */ |
| 4131 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL); |
| 4132 | if (!pdev) |
| 4133 | return; |
| 4134 | pci_dev_put(pdev); |
| 4135 | |
| 4136 | /* System Management Registers. Might be hidden, in which case |
| 4137 | we can't do the sanity check. But that's OK, because the |
| 4138 | known-broken BIOSes _don't_ actually hide it, so far. */ |
| 4139 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL); |
| 4140 | if (!pdev) |
| 4141 | return; |
| 4142 | |
| 4143 | if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) { |
| 4144 | pci_dev_put(pdev); |
| 4145 | return; |
| 4146 | } |
| 4147 | |
| 4148 | pci_dev_put(pdev); |
| 4149 | |
| 4150 | /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */ |
| 4151 | if (vtisochctrl & 1) |
| 4152 | return; |
| 4153 | |
| 4154 | /* Drop all bits other than the number of TLB entries */ |
| 4155 | vtisochctrl &= 0x1c; |
| 4156 | |
| 4157 | /* If we have the recommended number of TLB entries (16), fine. */ |
| 4158 | if (vtisochctrl == 0x10) |
| 4159 | return; |
| 4160 | |
| 4161 | /* Zero TLB entries? You get to ride the short bus to school. */ |
| 4162 | if (!vtisochctrl) { |
| 4163 | WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n" |
| 4164 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 4165 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 4166 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 4167 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
| 4168 | iommu_identity_mapping |= IDENTMAP_AZALIA; |
| 4169 | return; |
| 4170 | } |
| 4171 | |
| 4172 | printk(KERN_WARNING "DMAR: Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n", |
| 4173 | vtisochctrl); |
| 4174 | } |