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