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