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