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> |
Christoph Hellwig | d657c5c | 2018-03-19 11:38:20 +0100 | [diff] [blame] | 34 | #include <linux/dma-direct.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 35 | #include <linux/mempool.h> |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 36 | #include <linux/memory.h> |
Omer Peleg | aa47324 | 2016-04-20 11:33:02 +0300 | [diff] [blame] | 37 | #include <linux/cpu.h> |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 38 | #include <linux/timer.h> |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 39 | #include <linux/io.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 40 | #include <linux/iova.h> |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 41 | #include <linux/iommu.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 42 | #include <linux/intel-iommu.h> |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 43 | #include <linux/syscore_ops.h> |
Shane Wang | 69575d3 | 2009-09-01 18:25:07 -0700 | [diff] [blame] | 44 | #include <linux/tboot.h> |
Stephen Rothwell | adb2fe0 | 2009-08-31 15:24:23 +1000 | [diff] [blame] | 45 | #include <linux/dmi.h> |
Joerg Roedel | 5cdede2 | 2011-04-04 15:55:18 +0200 | [diff] [blame] | 46 | #include <linux/pci-ats.h> |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 47 | #include <linux/memblock.h> |
Akinobu Mita | 3674643 | 2014-06-04 16:06:51 -0700 | [diff] [blame] | 48 | #include <linux/dma-contiguous.h> |
Christoph Hellwig | fec777c | 2018-03-19 11:38:15 +0100 | [diff] [blame] | 49 | #include <linux/dma-direct.h> |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 50 | #include <linux/crash_dump.h> |
Suresh Siddha | 8a8f422 | 2012-03-30 11:47:08 -0700 | [diff] [blame] | 51 | #include <asm/irq_remapping.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 52 | #include <asm/cacheflush.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 53 | #include <asm/iommu.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 54 | |
Joerg Roedel | 078e1ee | 2012-09-26 12:44:43 +0200 | [diff] [blame] | 55 | #include "irq_remapping.h" |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 56 | #include "intel-pasid.h" |
Joerg Roedel | 078e1ee | 2012-09-26 12:44:43 +0200 | [diff] [blame] | 57 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 58 | #define ROOT_SIZE VTD_PAGE_SIZE |
| 59 | #define CONTEXT_SIZE VTD_PAGE_SIZE |
| 60 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 61 | #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) |
David Woodhouse | 18436af | 2015-03-25 15:05:47 +0000 | [diff] [blame] | 62 | #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] | 63 | #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA) |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 64 | #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 65 | |
| 66 | #define IOAPIC_RANGE_START (0xfee00000) |
| 67 | #define IOAPIC_RANGE_END (0xfeefffff) |
| 68 | #define IOVA_START_ADDR (0x1000) |
| 69 | |
Sohil Mehta | 5e3b4a1 | 2017-12-20 11:59:24 -0800 | [diff] [blame] | 70 | #define DEFAULT_DOMAIN_ADDRESS_WIDTH 57 |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 71 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 72 | #define MAX_AGAW_WIDTH 64 |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 73 | #define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT) |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 74 | |
David Woodhouse | 2ebe315 | 2009-09-19 07:34:04 -0700 | [diff] [blame] | 75 | #define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1) |
| 76 | #define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1) |
| 77 | |
| 78 | /* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR |
| 79 | to match. That way, we can use 'unsigned long' for PFNs with impunity. */ |
| 80 | #define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \ |
| 81 | __DOMAIN_MAX_PFN(gaw), (unsigned long)-1)) |
| 82 | #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] | 83 | |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 84 | /* IO virtual address start page frame number */ |
| 85 | #define IOVA_START_PFN (1) |
| 86 | |
Mark McLoughlin | f27be03 | 2008-11-20 15:49:43 +0000 | [diff] [blame] | 87 | #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 88 | |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 89 | /* page table handling */ |
| 90 | #define LEVEL_STRIDE (9) |
| 91 | #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1) |
| 92 | |
Ohad Ben-Cohen | 6d1c56a | 2011-11-10 11:32:30 +0200 | [diff] [blame] | 93 | /* |
| 94 | * This bitmap is used to advertise the page sizes our hardware support |
| 95 | * to the IOMMU core, which will then use this information to split |
| 96 | * physically contiguous memory regions it is mapping into page sizes |
| 97 | * that we support. |
| 98 | * |
| 99 | * Traditionally the IOMMU core just handed us the mappings directly, |
| 100 | * after making sure the size is an order of a 4KiB page and that the |
| 101 | * mapping has natural alignment. |
| 102 | * |
| 103 | * To retain this behavior, we currently advertise that we support |
| 104 | * all page sizes that are an order of 4KiB. |
| 105 | * |
| 106 | * If at some point we'd like to utilize the IOMMU core's new behavior, |
| 107 | * we could change this to advertise the real page sizes we support. |
| 108 | */ |
| 109 | #define INTEL_IOMMU_PGSIZES (~0xFFFUL) |
| 110 | |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 111 | static inline int agaw_to_level(int agaw) |
| 112 | { |
| 113 | return agaw + 2; |
| 114 | } |
| 115 | |
| 116 | static inline int agaw_to_width(int agaw) |
| 117 | { |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 118 | return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH); |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static inline int width_to_agaw(int width) |
| 122 | { |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 123 | return DIV_ROUND_UP(width - 30, LEVEL_STRIDE); |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static inline unsigned int level_to_offset_bits(int level) |
| 127 | { |
| 128 | return (level - 1) * LEVEL_STRIDE; |
| 129 | } |
| 130 | |
| 131 | static inline int pfn_level_offset(unsigned long pfn, int level) |
| 132 | { |
| 133 | return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK; |
| 134 | } |
| 135 | |
| 136 | static inline unsigned long level_mask(int level) |
| 137 | { |
| 138 | return -1UL << level_to_offset_bits(level); |
| 139 | } |
| 140 | |
| 141 | static inline unsigned long level_size(int level) |
| 142 | { |
| 143 | return 1UL << level_to_offset_bits(level); |
| 144 | } |
| 145 | |
| 146 | static inline unsigned long align_to_level(unsigned long pfn, int level) |
| 147 | { |
| 148 | return (pfn + level_size(level) - 1) & level_mask(level); |
| 149 | } |
David Woodhouse | fd18de5 | 2009-05-10 23:57:41 +0100 | [diff] [blame] | 150 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 151 | static inline unsigned long lvl_to_nr_pages(unsigned int lvl) |
| 152 | { |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 153 | 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] | 154 | } |
| 155 | |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 156 | /* VT-d pages must always be _smaller_ than MM pages. Otherwise things |
| 157 | are never going to work. */ |
| 158 | static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn) |
| 159 | { |
| 160 | return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 161 | } |
| 162 | |
| 163 | static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn) |
| 164 | { |
| 165 | return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 166 | } |
| 167 | static inline unsigned long page_to_dma_pfn(struct page *pg) |
| 168 | { |
| 169 | return mm_to_dma_pfn(page_to_pfn(pg)); |
| 170 | } |
| 171 | static inline unsigned long virt_to_dma_pfn(void *p) |
| 172 | { |
| 173 | return page_to_dma_pfn(virt_to_page(p)); |
| 174 | } |
| 175 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 176 | /* global iommu list, set NULL for ignored DMAR units */ |
| 177 | static struct intel_iommu **g_iommus; |
| 178 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 179 | static void __init check_tylersburg_isoch(void); |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 180 | static int rwbf_quirk; |
| 181 | |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 182 | /* |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 183 | * set to 1 to panic kernel if can't successfully enable VT-d |
| 184 | * (used when kernel is launched w/ TXT) |
| 185 | */ |
| 186 | static int force_on = 0; |
Shaohua Li | bfd20f1 | 2017-04-26 09:18:35 -0700 | [diff] [blame] | 187 | int intel_iommu_tboot_noforce; |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 188 | |
| 189 | /* |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 190 | * 0: Present |
| 191 | * 1-11: Reserved |
| 192 | * 12-63: Context Ptr (12 - (haw-1)) |
| 193 | * 64-127: Reserved |
| 194 | */ |
| 195 | struct root_entry { |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 196 | u64 lo; |
| 197 | u64 hi; |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 198 | }; |
| 199 | #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry)) |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 200 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 201 | /* |
| 202 | * Take a root_entry and return the Lower Context Table Pointer (LCTP) |
| 203 | * if marked present. |
| 204 | */ |
| 205 | static phys_addr_t root_entry_lctp(struct root_entry *re) |
| 206 | { |
| 207 | if (!(re->lo & 1)) |
| 208 | return 0; |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 209 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 210 | return re->lo & VTD_PAGE_MASK; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Take a root_entry and return the Upper Context Table Pointer (UCTP) |
| 215 | * if marked present. |
| 216 | */ |
| 217 | static phys_addr_t root_entry_uctp(struct root_entry *re) |
| 218 | { |
| 219 | if (!(re->hi & 1)) |
| 220 | return 0; |
| 221 | |
| 222 | return re->hi & VTD_PAGE_MASK; |
| 223 | } |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 224 | /* |
| 225 | * low 64 bits: |
| 226 | * 0: present |
| 227 | * 1: fault processing disable |
| 228 | * 2-3: translation type |
| 229 | * 12-63: address space root |
| 230 | * high 64 bits: |
| 231 | * 0-2: address width |
| 232 | * 3-6: aval |
| 233 | * 8-23: domain id |
| 234 | */ |
| 235 | struct context_entry { |
| 236 | u64 lo; |
| 237 | u64 hi; |
| 238 | }; |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 239 | |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 240 | static inline void context_clear_pasid_enable(struct context_entry *context) |
| 241 | { |
| 242 | context->lo &= ~(1ULL << 11); |
| 243 | } |
| 244 | |
| 245 | static inline bool context_pasid_enabled(struct context_entry *context) |
| 246 | { |
| 247 | return !!(context->lo & (1ULL << 11)); |
| 248 | } |
| 249 | |
| 250 | static inline void context_set_copied(struct context_entry *context) |
| 251 | { |
| 252 | context->hi |= (1ull << 3); |
| 253 | } |
| 254 | |
| 255 | static inline bool context_copied(struct context_entry *context) |
| 256 | { |
| 257 | return !!(context->hi & (1ULL << 3)); |
| 258 | } |
| 259 | |
| 260 | static inline bool __context_present(struct context_entry *context) |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 261 | { |
| 262 | return (context->lo & 1); |
| 263 | } |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 264 | |
| 265 | static inline bool context_present(struct context_entry *context) |
| 266 | { |
| 267 | return context_pasid_enabled(context) ? |
| 268 | __context_present(context) : |
| 269 | __context_present(context) && !context_copied(context); |
| 270 | } |
| 271 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 272 | static inline void context_set_present(struct context_entry *context) |
| 273 | { |
| 274 | context->lo |= 1; |
| 275 | } |
| 276 | |
| 277 | static inline void context_set_fault_enable(struct context_entry *context) |
| 278 | { |
| 279 | context->lo &= (((u64)-1) << 2) | 1; |
| 280 | } |
| 281 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 282 | static inline void context_set_translation_type(struct context_entry *context, |
| 283 | unsigned long value) |
| 284 | { |
| 285 | context->lo &= (((u64)-1) << 4) | 3; |
| 286 | context->lo |= (value & 3) << 2; |
| 287 | } |
| 288 | |
| 289 | static inline void context_set_address_root(struct context_entry *context, |
| 290 | unsigned long value) |
| 291 | { |
Li, Zhen-Hua | 1a2262f | 2014-11-05 15:30:19 +0800 | [diff] [blame] | 292 | context->lo &= ~VTD_PAGE_MASK; |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 293 | context->lo |= value & VTD_PAGE_MASK; |
| 294 | } |
| 295 | |
| 296 | static inline void context_set_address_width(struct context_entry *context, |
| 297 | unsigned long value) |
| 298 | { |
| 299 | context->hi |= value & 7; |
| 300 | } |
| 301 | |
| 302 | static inline void context_set_domain_id(struct context_entry *context, |
| 303 | unsigned long value) |
| 304 | { |
| 305 | context->hi |= (value & ((1 << 16) - 1)) << 8; |
| 306 | } |
| 307 | |
Joerg Roedel | dbcd861 | 2015-06-12 12:02:09 +0200 | [diff] [blame] | 308 | static inline int context_domain_id(struct context_entry *c) |
| 309 | { |
| 310 | return((c->hi >> 8) & 0xffff); |
| 311 | } |
| 312 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 313 | static inline void context_clear_entry(struct context_entry *context) |
| 314 | { |
| 315 | context->lo = 0; |
| 316 | context->hi = 0; |
| 317 | } |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 318 | |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 319 | /* |
| 320 | * 0: readable |
| 321 | * 1: writable |
| 322 | * 2-6: reserved |
| 323 | * 7: super page |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 324 | * 8-10: available |
| 325 | * 11: snoop behavior |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 326 | * 12-63: Host physcial address |
| 327 | */ |
| 328 | struct dma_pte { |
| 329 | u64 val; |
| 330 | }; |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 331 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 332 | static inline void dma_clear_pte(struct dma_pte *pte) |
| 333 | { |
| 334 | pte->val = 0; |
| 335 | } |
| 336 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 337 | static inline u64 dma_pte_addr(struct dma_pte *pte) |
| 338 | { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 339 | #ifdef CONFIG_64BIT |
| 340 | return pte->val & VTD_PAGE_MASK; |
| 341 | #else |
| 342 | /* Must have a full atomic 64-bit read */ |
David Woodhouse | 1a8bd48 | 2010-08-10 01:38:53 +0100 | [diff] [blame] | 343 | return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK; |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 344 | #endif |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 347 | static inline bool dma_pte_present(struct dma_pte *pte) |
| 348 | { |
| 349 | return (pte->val & 3) != 0; |
| 350 | } |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 351 | |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 352 | static inline bool dma_pte_superpage(struct dma_pte *pte) |
| 353 | { |
Joerg Roedel | c3c75eb | 2014-07-04 11:19:10 +0200 | [diff] [blame] | 354 | return (pte->val & DMA_PTE_LARGE_PAGE); |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 355 | } |
| 356 | |
David Woodhouse | 75e6bf9 | 2009-07-02 11:21:16 +0100 | [diff] [blame] | 357 | static inline int first_pte_in_page(struct dma_pte *pte) |
| 358 | { |
| 359 | return !((unsigned long)pte & ~VTD_PAGE_MASK); |
| 360 | } |
| 361 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 362 | /* |
| 363 | * This domain is a statically identity mapping domain. |
| 364 | * 1. This domain creats a static 1:1 mapping to all usable memory. |
| 365 | * 2. It maps to each iommu if successful. |
| 366 | * 3. Each iommu mapps to this domain if successful. |
| 367 | */ |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 368 | static struct dmar_domain *si_domain; |
| 369 | static int hw_pass_through = 1; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 370 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 371 | /* |
| 372 | * Domain represents a virtual machine, more than one devices |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 373 | * across iommus may be owned in one domain, e.g. kvm guest. |
| 374 | */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 375 | #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0) |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 376 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 377 | /* si_domain contains mulitple devices */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 378 | #define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 379 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 380 | #define for_each_domain_iommu(idx, domain) \ |
| 381 | for (idx = 0; idx < g_num_of_iommus; idx++) \ |
| 382 | if (domain->iommu_refcnt[idx]) |
| 383 | |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 384 | struct dmar_rmrr_unit { |
| 385 | struct list_head list; /* list of rmrr units */ |
| 386 | struct acpi_dmar_header *hdr; /* ACPI header */ |
| 387 | u64 base_address; /* reserved base address*/ |
| 388 | u64 end_address; /* reserved end address */ |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 389 | struct dmar_dev_scope *devices; /* target devices */ |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 390 | int devices_cnt; /* target device count */ |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 391 | struct iommu_resv_region *resv; /* reserved region handle */ |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | struct dmar_atsr_unit { |
| 395 | struct list_head list; /* list of ATSR units */ |
| 396 | struct acpi_dmar_header *hdr; /* ACPI header */ |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 397 | struct dmar_dev_scope *devices; /* target devices */ |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 398 | int devices_cnt; /* target device count */ |
| 399 | u8 include_all:1; /* include all ports */ |
| 400 | }; |
| 401 | |
| 402 | static LIST_HEAD(dmar_atsr_units); |
| 403 | static LIST_HEAD(dmar_rmrr_units); |
| 404 | |
| 405 | #define for_each_rmrr_units(rmrr) \ |
| 406 | list_for_each_entry(rmrr, &dmar_rmrr_units, list) |
| 407 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 408 | /* bitmap for indexing intel_iommus */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 409 | static int g_num_of_iommus; |
| 410 | |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 411 | static void domain_exit(struct dmar_domain *domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 412 | static void domain_remove_dev_info(struct dmar_domain *domain); |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 413 | static void dmar_remove_one_dev_info(struct dmar_domain *domain, |
| 414 | struct device *dev); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 415 | static void __dmar_remove_one_dev_info(struct device_domain_info *info); |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 416 | static void domain_context_clear(struct intel_iommu *iommu, |
| 417 | struct device *dev); |
Jiang Liu | 2a46ddf | 2014-07-11 14:19:30 +0800 | [diff] [blame] | 418 | static int domain_detach_iommu(struct dmar_domain *domain, |
| 419 | struct intel_iommu *iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 420 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 421 | #ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 422 | int dmar_disabled = 0; |
| 423 | #else |
| 424 | int dmar_disabled = 1; |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 425 | #endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/ |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 426 | |
Eugeni Dodonov | 8bc1f85 | 2011-11-23 16:42:14 -0200 | [diff] [blame] | 427 | int intel_iommu_enabled = 0; |
| 428 | EXPORT_SYMBOL_GPL(intel_iommu_enabled); |
| 429 | |
David Woodhouse | 2d9e667 | 2010-06-15 10:57:57 +0100 | [diff] [blame] | 430 | static int dmar_map_gfx = 1; |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 431 | static int dmar_forcedac; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 432 | static int intel_iommu_strict; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 433 | static int intel_iommu_superpage = 1; |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 434 | static int intel_iommu_ecs = 1; |
David Woodhouse | ae853dd | 2015-09-09 11:58:59 +0100 | [diff] [blame] | 435 | static int iommu_identity_mapping; |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 436 | |
David Woodhouse | ae853dd | 2015-09-09 11:58:59 +0100 | [diff] [blame] | 437 | #define IDENTMAP_ALL 1 |
| 438 | #define IDENTMAP_GFX 2 |
| 439 | #define IDENTMAP_AZALIA 4 |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 440 | |
Lu Baolu | ab96746 | 2018-05-04 13:08:18 +0800 | [diff] [blame] | 441 | #define ecs_enabled(iommu) (intel_iommu_ecs && ecap_ecs(iommu->ecap)) |
| 442 | #define pasid_enabled(iommu) (ecs_enabled(iommu) && ecap_pasid(iommu->ecap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 443 | |
David Woodhouse | c0771df | 2011-10-14 20:59:46 +0100 | [diff] [blame] | 444 | int intel_iommu_gfx_mapped; |
| 445 | EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped); |
| 446 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 447 | #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1)) |
| 448 | static DEFINE_SPINLOCK(device_domain_lock); |
| 449 | static LIST_HEAD(device_domain_list); |
| 450 | |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 451 | /* |
| 452 | * Iterate over elements in device_domain_list and call the specified |
| 453 | * callback @fn against each element. This helper should only be used |
| 454 | * in the context where the device_domain_lock has already been holden. |
| 455 | */ |
| 456 | int for_each_device_domain(int (*fn)(struct device_domain_info *info, |
| 457 | void *data), void *data) |
| 458 | { |
| 459 | int ret = 0; |
| 460 | struct device_domain_info *info; |
| 461 | |
| 462 | assert_spin_locked(&device_domain_lock); |
| 463 | list_for_each_entry(info, &device_domain_list, global) { |
| 464 | ret = fn(info, data); |
| 465 | if (ret) |
| 466 | return ret; |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 472 | const struct iommu_ops intel_iommu_ops; |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 473 | |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 474 | static bool translation_pre_enabled(struct intel_iommu *iommu) |
| 475 | { |
| 476 | return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED); |
| 477 | } |
| 478 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 479 | static void clear_translation_pre_enabled(struct intel_iommu *iommu) |
| 480 | { |
| 481 | iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED; |
| 482 | } |
| 483 | |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 484 | static void init_translation_status(struct intel_iommu *iommu) |
| 485 | { |
| 486 | u32 gsts; |
| 487 | |
| 488 | gsts = readl(iommu->reg + DMAR_GSTS_REG); |
| 489 | if (gsts & DMA_GSTS_TES) |
| 490 | iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED; |
| 491 | } |
| 492 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 493 | /* Convert generic 'struct iommu_domain to private struct dmar_domain */ |
| 494 | static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom) |
| 495 | { |
| 496 | return container_of(dom, struct dmar_domain, domain); |
| 497 | } |
| 498 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 499 | static int __init intel_iommu_setup(char *str) |
| 500 | { |
| 501 | if (!str) |
| 502 | return -EINVAL; |
| 503 | while (*str) { |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 504 | if (!strncmp(str, "on", 2)) { |
| 505 | dmar_disabled = 0; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 506 | pr_info("IOMMU enabled\n"); |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 507 | } else if (!strncmp(str, "off", 3)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 508 | dmar_disabled = 1; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 509 | pr_info("IOMMU disabled\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 510 | } else if (!strncmp(str, "igfx_off", 8)) { |
| 511 | dmar_map_gfx = 0; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 512 | pr_info("Disable GFX device mapping\n"); |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 513 | } else if (!strncmp(str, "forcedac", 8)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 514 | pr_info("Forcing DAC for PCI devices\n"); |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 515 | dmar_forcedac = 1; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 516 | } else if (!strncmp(str, "strict", 6)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 517 | pr_info("Disable batched IOTLB flush\n"); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 518 | intel_iommu_strict = 1; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 519 | } else if (!strncmp(str, "sp_off", 6)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 520 | pr_info("Disable supported super page\n"); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 521 | intel_iommu_superpage = 0; |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 522 | } else if (!strncmp(str, "ecs_off", 7)) { |
| 523 | printk(KERN_INFO |
| 524 | "Intel-IOMMU: disable extended context table support\n"); |
| 525 | intel_iommu_ecs = 0; |
Shaohua Li | bfd20f1 | 2017-04-26 09:18:35 -0700 | [diff] [blame] | 526 | } else if (!strncmp(str, "tboot_noforce", 13)) { |
| 527 | printk(KERN_INFO |
| 528 | "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n"); |
| 529 | intel_iommu_tboot_noforce = 1; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | str += strcspn(str, ","); |
| 533 | while (*str == ',') |
| 534 | str++; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | __setup("intel_iommu=", intel_iommu_setup); |
| 539 | |
| 540 | static struct kmem_cache *iommu_domain_cache; |
| 541 | static struct kmem_cache *iommu_devinfo_cache; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 542 | |
Joerg Roedel | 9452d5b | 2015-07-21 10:00:56 +0200 | [diff] [blame] | 543 | static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did) |
| 544 | { |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 545 | struct dmar_domain **domains; |
| 546 | int idx = did >> 8; |
| 547 | |
| 548 | domains = iommu->domains[idx]; |
| 549 | if (!domains) |
| 550 | return NULL; |
| 551 | |
| 552 | return domains[did & 0xff]; |
Joerg Roedel | 9452d5b | 2015-07-21 10:00:56 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void set_iommu_domain(struct intel_iommu *iommu, u16 did, |
| 556 | struct dmar_domain *domain) |
| 557 | { |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 558 | struct dmar_domain **domains; |
| 559 | int idx = did >> 8; |
| 560 | |
| 561 | if (!iommu->domains[idx]) { |
| 562 | size_t size = 256 * sizeof(struct dmar_domain *); |
| 563 | iommu->domains[idx] = kzalloc(size, GFP_ATOMIC); |
| 564 | } |
| 565 | |
| 566 | domains = iommu->domains[idx]; |
| 567 | if (WARN_ON(!domains)) |
| 568 | return; |
| 569 | else |
| 570 | domains[did & 0xff] = domain; |
Joerg Roedel | 9452d5b | 2015-07-21 10:00:56 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 573 | void *alloc_pgtable_page(int node) |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 574 | { |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 575 | struct page *page; |
| 576 | void *vaddr = NULL; |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 577 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 578 | page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0); |
| 579 | if (page) |
| 580 | vaddr = page_address(page); |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 581 | return vaddr; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 584 | void free_pgtable_page(void *vaddr) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 585 | { |
| 586 | free_page((unsigned long)vaddr); |
| 587 | } |
| 588 | |
| 589 | static inline void *alloc_domain_mem(void) |
| 590 | { |
KOSAKI Motohiro | 354bb65 | 2009-11-17 16:21:09 +0900 | [diff] [blame] | 591 | return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 594 | static void free_domain_mem(void *vaddr) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 595 | { |
| 596 | kmem_cache_free(iommu_domain_cache, vaddr); |
| 597 | } |
| 598 | |
| 599 | static inline void * alloc_devinfo_mem(void) |
| 600 | { |
KOSAKI Motohiro | 354bb65 | 2009-11-17 16:21:09 +0900 | [diff] [blame] | 601 | return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static inline void free_devinfo_mem(void *vaddr) |
| 605 | { |
| 606 | kmem_cache_free(iommu_devinfo_cache, vaddr); |
| 607 | } |
| 608 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 609 | static inline int domain_type_is_vm(struct dmar_domain *domain) |
| 610 | { |
| 611 | return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE; |
| 612 | } |
| 613 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 614 | static inline int domain_type_is_si(struct dmar_domain *domain) |
| 615 | { |
| 616 | return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY; |
| 617 | } |
| 618 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 619 | static inline int domain_type_is_vm_or_si(struct dmar_domain *domain) |
| 620 | { |
| 621 | return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE | |
| 622 | DOMAIN_FLAG_STATIC_IDENTITY); |
| 623 | } |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 624 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 625 | static inline int domain_pfn_supported(struct dmar_domain *domain, |
| 626 | unsigned long pfn) |
| 627 | { |
| 628 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
| 629 | |
| 630 | return !(addr_width < BITS_PER_LONG && pfn >> addr_width); |
| 631 | } |
| 632 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 633 | static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw) |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 634 | { |
| 635 | unsigned long sagaw; |
| 636 | int agaw = -1; |
| 637 | |
| 638 | sagaw = cap_sagaw(iommu->cap); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 639 | for (agaw = width_to_agaw(max_gaw); |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 640 | agaw >= 0; agaw--) { |
| 641 | if (test_bit(agaw, &sagaw)) |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | return agaw; |
| 646 | } |
| 647 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 648 | /* |
| 649 | * Calculate max SAGAW for each iommu. |
| 650 | */ |
| 651 | int iommu_calculate_max_sagaw(struct intel_iommu *iommu) |
| 652 | { |
| 653 | return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH); |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * calculate agaw for each iommu. |
| 658 | * "SAGAW" may be different across iommus, use a default agaw, and |
| 659 | * get a supported less agaw for iommus that don't support the default agaw. |
| 660 | */ |
| 661 | int iommu_calculate_agaw(struct intel_iommu *iommu) |
| 662 | { |
| 663 | return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 664 | } |
| 665 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 666 | /* This functionin only returns single iommu in a domain */ |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 667 | struct intel_iommu *domain_get_iommu(struct dmar_domain *domain) |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 668 | { |
| 669 | int iommu_id; |
| 670 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 671 | /* si_domain and vm domain should not get here. */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 672 | BUG_ON(domain_type_is_vm_or_si(domain)); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 673 | for_each_domain_iommu(iommu_id, domain) |
| 674 | break; |
| 675 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 676 | if (iommu_id < 0 || iommu_id >= g_num_of_iommus) |
| 677 | return NULL; |
| 678 | |
| 679 | return g_iommus[iommu_id]; |
| 680 | } |
| 681 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 682 | static void domain_update_iommu_coherency(struct dmar_domain *domain) |
| 683 | { |
David Woodhouse | d050196 | 2014-03-11 17:10:29 -0700 | [diff] [blame] | 684 | struct dmar_drhd_unit *drhd; |
| 685 | struct intel_iommu *iommu; |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 686 | bool found = false; |
| 687 | int i; |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 688 | |
David Woodhouse | d050196 | 2014-03-11 17:10:29 -0700 | [diff] [blame] | 689 | domain->iommu_coherency = 1; |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 690 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 691 | for_each_domain_iommu(i, domain) { |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 692 | found = true; |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 693 | if (!ecap_coherent(g_iommus[i]->ecap)) { |
| 694 | domain->iommu_coherency = 0; |
| 695 | break; |
| 696 | } |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 697 | } |
David Woodhouse | d050196 | 2014-03-11 17:10:29 -0700 | [diff] [blame] | 698 | if (found) |
| 699 | return; |
| 700 | |
| 701 | /* No hardware attached; use lowest common denominator */ |
| 702 | rcu_read_lock(); |
| 703 | for_each_active_iommu(iommu, drhd) { |
| 704 | if (!ecap_coherent(iommu->ecap)) { |
| 705 | domain->iommu_coherency = 0; |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | rcu_read_unlock(); |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 710 | } |
| 711 | |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 712 | static int domain_update_iommu_snooping(struct intel_iommu *skip) |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 713 | { |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 714 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 715 | struct intel_iommu *iommu; |
| 716 | int ret = 1; |
| 717 | |
| 718 | rcu_read_lock(); |
| 719 | for_each_active_iommu(iommu, drhd) { |
| 720 | if (iommu != skip) { |
| 721 | if (!ecap_sc_support(iommu->ecap)) { |
| 722 | ret = 0; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | rcu_read_unlock(); |
| 728 | |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | static int domain_update_iommu_superpage(struct intel_iommu *skip) |
| 733 | { |
| 734 | struct dmar_drhd_unit *drhd; |
| 735 | struct intel_iommu *iommu; |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 736 | int mask = 0xf; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 737 | |
| 738 | if (!intel_iommu_superpage) { |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 739 | return 0; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 740 | } |
| 741 | |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 742 | /* set iommu_superpage to the smallest common denominator */ |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 743 | rcu_read_lock(); |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 744 | for_each_active_iommu(iommu, drhd) { |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 745 | if (iommu != skip) { |
| 746 | mask &= cap_super_page_val(iommu->cap); |
| 747 | if (!mask) |
| 748 | break; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 749 | } |
| 750 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 751 | rcu_read_unlock(); |
| 752 | |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 753 | return fls(mask); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 756 | /* Some capabilities may be different across iommus */ |
| 757 | static void domain_update_iommu_cap(struct dmar_domain *domain) |
| 758 | { |
| 759 | domain_update_iommu_coherency(domain); |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 760 | domain->iommu_snooping = domain_update_iommu_snooping(NULL); |
| 761 | domain->iommu_superpage = domain_update_iommu_superpage(NULL); |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 762 | } |
| 763 | |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 764 | static inline struct context_entry *iommu_context_addr(struct intel_iommu *iommu, |
| 765 | u8 bus, u8 devfn, int alloc) |
| 766 | { |
| 767 | struct root_entry *root = &iommu->root_entry[bus]; |
| 768 | struct context_entry *context; |
| 769 | u64 *entry; |
| 770 | |
Joerg Roedel | 4df4eab | 2015-08-25 10:54:28 +0200 | [diff] [blame] | 771 | entry = &root->lo; |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 772 | if (ecs_enabled(iommu)) { |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 773 | if (devfn >= 0x80) { |
| 774 | devfn -= 0x80; |
| 775 | entry = &root->hi; |
| 776 | } |
| 777 | devfn *= 2; |
| 778 | } |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 779 | if (*entry & 1) |
| 780 | context = phys_to_virt(*entry & VTD_PAGE_MASK); |
| 781 | else { |
| 782 | unsigned long phy_addr; |
| 783 | if (!alloc) |
| 784 | return NULL; |
| 785 | |
| 786 | context = alloc_pgtable_page(iommu->node); |
| 787 | if (!context) |
| 788 | return NULL; |
| 789 | |
| 790 | __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE); |
| 791 | phy_addr = virt_to_phys((void *)context); |
| 792 | *entry = phy_addr | 1; |
| 793 | __iommu_flush_cache(iommu, entry, sizeof(*entry)); |
| 794 | } |
| 795 | return &context[devfn]; |
| 796 | } |
| 797 | |
David Woodhouse | 4ed6a54 | 2015-05-11 14:59:20 +0100 | [diff] [blame] | 798 | static int iommu_dummy(struct device *dev) |
| 799 | { |
| 800 | return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO; |
| 801 | } |
| 802 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 803 | 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] | 804 | { |
| 805 | struct dmar_drhd_unit *drhd = NULL; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 806 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 807 | struct device *tmp; |
| 808 | struct pci_dev *ptmp, *pdev = NULL; |
Yijing Wang | aa4d066 | 2014-05-26 20:14:06 +0800 | [diff] [blame] | 809 | u16 segment = 0; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 810 | int i; |
| 811 | |
David Woodhouse | 4ed6a54 | 2015-05-11 14:59:20 +0100 | [diff] [blame] | 812 | if (iommu_dummy(dev)) |
| 813 | return NULL; |
| 814 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 815 | if (dev_is_pci(dev)) { |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 816 | struct pci_dev *pf_pdev; |
| 817 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 818 | pdev = to_pci_dev(dev); |
Jon Derrick | 5823e33 | 2017-08-30 15:05:59 -0600 | [diff] [blame] | 819 | |
| 820 | #ifdef CONFIG_X86 |
| 821 | /* VMD child devices currently cannot be handled individually */ |
| 822 | if (is_vmd(pdev->bus)) |
| 823 | return NULL; |
| 824 | #endif |
| 825 | |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 826 | /* VFs aren't listed in scope tables; we need to look up |
| 827 | * the PF instead to find the IOMMU. */ |
| 828 | pf_pdev = pci_physfn(pdev); |
| 829 | dev = &pf_pdev->dev; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 830 | segment = pci_domain_nr(pdev->bus); |
Rafael J. Wysocki | ca5b74d | 2015-03-16 23:49:08 +0100 | [diff] [blame] | 831 | } else if (has_acpi_companion(dev)) |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 832 | dev = &ACPI_COMPANION(dev)->dev; |
| 833 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 834 | rcu_read_lock(); |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 835 | for_each_active_iommu(iommu, drhd) { |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 836 | if (pdev && segment != drhd->segment) |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 837 | continue; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 838 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 839 | for_each_active_dev_scope(drhd->devices, |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 840 | drhd->devices_cnt, i, tmp) { |
| 841 | if (tmp == dev) { |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 842 | /* For a VF use its original BDF# not that of the PF |
| 843 | * which we used for the IOMMU lookup. Strictly speaking |
| 844 | * we could do this for all PCI devices; we only need to |
| 845 | * get the BDF# from the scope table for ACPI matches. */ |
Koos Vriezen | 5003ae1 | 2017-03-01 21:02:50 +0100 | [diff] [blame] | 846 | if (pdev && pdev->is_virtfn) |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 847 | goto got_pdev; |
| 848 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 849 | *bus = drhd->devices[i].bus; |
| 850 | *devfn = drhd->devices[i].devfn; |
| 851 | goto out; |
| 852 | } |
| 853 | |
| 854 | if (!pdev || !dev_is_pci(tmp)) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 855 | continue; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 856 | |
| 857 | ptmp = to_pci_dev(tmp); |
| 858 | if (ptmp->subordinate && |
| 859 | ptmp->subordinate->number <= pdev->bus->number && |
| 860 | ptmp->subordinate->busn_res.end >= pdev->bus->number) |
| 861 | goto got_pdev; |
David Woodhouse | 924b623 | 2009-04-04 00:39:25 +0100 | [diff] [blame] | 862 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 863 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 864 | if (pdev && drhd->include_all) { |
| 865 | got_pdev: |
| 866 | *bus = pdev->bus->number; |
| 867 | *devfn = pdev->devfn; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 868 | goto out; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 869 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 870 | } |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 871 | iommu = NULL; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 872 | out: |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 873 | rcu_read_unlock(); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 874 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 875 | return iommu; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 876 | } |
| 877 | |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 878 | static void domain_flush_cache(struct dmar_domain *domain, |
| 879 | void *addr, int size) |
| 880 | { |
| 881 | if (!domain->iommu_coherency) |
| 882 | clflush_cache_range(addr, size); |
| 883 | } |
| 884 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 885 | static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn) |
| 886 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 887 | struct context_entry *context; |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 888 | int ret = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 889 | unsigned long flags; |
| 890 | |
| 891 | spin_lock_irqsave(&iommu->lock, flags); |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 892 | context = iommu_context_addr(iommu, bus, devfn, 0); |
| 893 | if (context) |
| 894 | ret = context_present(context); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 895 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 896 | return ret; |
| 897 | } |
| 898 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 899 | static void free_context_table(struct intel_iommu *iommu) |
| 900 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 901 | int i; |
| 902 | unsigned long flags; |
| 903 | struct context_entry *context; |
| 904 | |
| 905 | spin_lock_irqsave(&iommu->lock, flags); |
| 906 | if (!iommu->root_entry) { |
| 907 | goto out; |
| 908 | } |
| 909 | for (i = 0; i < ROOT_ENTRY_NR; i++) { |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 910 | context = iommu_context_addr(iommu, i, 0, 0); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 911 | if (context) |
| 912 | free_pgtable_page(context); |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 913 | |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 914 | if (!ecs_enabled(iommu)) |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 915 | continue; |
| 916 | |
| 917 | context = iommu_context_addr(iommu, i, 0x80, 0); |
| 918 | if (context) |
| 919 | free_pgtable_page(context); |
| 920 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 921 | } |
| 922 | free_pgtable_page(iommu->root_entry); |
| 923 | iommu->root_entry = NULL; |
| 924 | out: |
| 925 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 926 | } |
| 927 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 928 | static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 929 | unsigned long pfn, int *target_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 930 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 931 | struct dma_pte *parent, *pte = NULL; |
| 932 | int level = agaw_to_level(domain->agaw); |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 933 | int offset; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 934 | |
| 935 | BUG_ON(!domain->pgd); |
Julian Stecklina | f942360 | 2013-10-09 10:03:52 +0200 | [diff] [blame] | 936 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 937 | if (!domain_pfn_supported(domain, pfn)) |
Julian Stecklina | f942360 | 2013-10-09 10:03:52 +0200 | [diff] [blame] | 938 | /* Address beyond IOMMU's addressing capabilities. */ |
| 939 | return NULL; |
| 940 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 941 | parent = domain->pgd; |
| 942 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 943 | while (1) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 944 | void *tmp_page; |
| 945 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 946 | offset = pfn_level_offset(pfn, level); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 947 | pte = &parent[offset]; |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 948 | if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte))) |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 949 | break; |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 950 | if (level == *target_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 951 | break; |
| 952 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 953 | if (!dma_pte_present(pte)) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 954 | uint64_t pteval; |
| 955 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 956 | tmp_page = alloc_pgtable_page(domain->nid); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 957 | |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame] | 958 | if (!tmp_page) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 959 | return NULL; |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame] | 960 | |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 961 | domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE); |
Benjamin LaHaise | 64de5af | 2009-09-16 21:05:55 -0400 | [diff] [blame] | 962 | 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] | 963 | if (cmpxchg64(&pte->val, 0ULL, pteval)) |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 964 | /* Someone else set it while we were thinking; use theirs. */ |
| 965 | free_pgtable_page(tmp_page); |
Yijing Wang | effad4b | 2014-05-26 20:13:47 +0800 | [diff] [blame] | 966 | else |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 967 | domain_flush_cache(domain, pte, sizeof(*pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 968 | } |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 969 | if (level == 1) |
| 970 | break; |
| 971 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 972 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 973 | level--; |
| 974 | } |
| 975 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 976 | if (!*target_level) |
| 977 | *target_level = level; |
| 978 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 979 | return pte; |
| 980 | } |
| 981 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 982 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 983 | /* return address's pte at specific level */ |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 984 | static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain, |
| 985 | unsigned long pfn, |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 986 | int level, int *large_page) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 987 | { |
| 988 | struct dma_pte *parent, *pte = NULL; |
| 989 | int total = agaw_to_level(domain->agaw); |
| 990 | int offset; |
| 991 | |
| 992 | parent = domain->pgd; |
| 993 | while (level <= total) { |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 994 | offset = pfn_level_offset(pfn, total); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 995 | pte = &parent[offset]; |
| 996 | if (level == total) |
| 997 | return pte; |
| 998 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 999 | if (!dma_pte_present(pte)) { |
| 1000 | *large_page = total; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1001 | break; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1002 | } |
| 1003 | |
Yijing Wang | e16922a | 2014-05-20 20:37:51 +0800 | [diff] [blame] | 1004 | if (dma_pte_superpage(pte)) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1005 | *large_page = total; |
| 1006 | return pte; |
| 1007 | } |
| 1008 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 1009 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1010 | total--; |
| 1011 | } |
| 1012 | return NULL; |
| 1013 | } |
| 1014 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1015 | /* clear last level pte, a tlb flush should be followed */ |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 1016 | static void dma_pte_clear_range(struct dmar_domain *domain, |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 1017 | unsigned long start_pfn, |
| 1018 | unsigned long last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1019 | { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1020 | unsigned int large_page = 1; |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 1021 | struct dma_pte *first_pte, *pte; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1022 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 1023 | BUG_ON(!domain_pfn_supported(domain, start_pfn)); |
| 1024 | BUG_ON(!domain_pfn_supported(domain, last_pfn)); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 1025 | BUG_ON(start_pfn > last_pfn); |
David Woodhouse | 66eae84 | 2009-06-27 19:00:32 +0100 | [diff] [blame] | 1026 | |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 1027 | /* we don't need lock here; nobody else touches the iova range */ |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 1028 | do { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1029 | large_page = 1; |
| 1030 | 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] | 1031 | if (!pte) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1032 | start_pfn = align_to_level(start_pfn + 1, large_page + 1); |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 1033 | continue; |
| 1034 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1035 | do { |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 1036 | dma_clear_pte(pte); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 1037 | start_pfn += lvl_to_nr_pages(large_page); |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 1038 | pte++; |
David Woodhouse | 75e6bf9 | 2009-07-02 11:21:16 +0100 | [diff] [blame] | 1039 | } while (start_pfn <= last_pfn && !first_pte_in_page(pte)); |
| 1040 | |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 1041 | domain_flush_cache(domain, first_pte, |
| 1042 | (void *)pte - (void *)first_pte); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 1043 | |
| 1044 | } while (start_pfn && start_pfn <= last_pfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1047 | static void dma_pte_free_level(struct dmar_domain *domain, int level, |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1048 | int retain_level, struct dma_pte *pte, |
| 1049 | unsigned long pfn, unsigned long start_pfn, |
| 1050 | unsigned long last_pfn) |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1051 | { |
| 1052 | pfn = max(start_pfn, pfn); |
| 1053 | pte = &pte[pfn_level_offset(pfn, level)]; |
| 1054 | |
| 1055 | do { |
| 1056 | unsigned long level_pfn; |
| 1057 | struct dma_pte *level_pte; |
| 1058 | |
| 1059 | if (!dma_pte_present(pte) || dma_pte_superpage(pte)) |
| 1060 | goto next; |
| 1061 | |
David Dillow | f7116e1 | 2017-01-30 19:11:11 -0800 | [diff] [blame] | 1062 | level_pfn = pfn & level_mask(level); |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1063 | level_pte = phys_to_virt(dma_pte_addr(pte)); |
| 1064 | |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1065 | if (level > 2) { |
| 1066 | dma_pte_free_level(domain, level - 1, retain_level, |
| 1067 | level_pte, level_pfn, start_pfn, |
| 1068 | last_pfn); |
| 1069 | } |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1070 | |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1071 | /* |
| 1072 | * Free the page table if we're below the level we want to |
| 1073 | * retain and the range covers the entire table. |
| 1074 | */ |
| 1075 | if (level < retain_level && !(start_pfn > level_pfn || |
Alex Williamson | 08336fd | 2014-01-21 15:48:18 -0800 | [diff] [blame] | 1076 | last_pfn < level_pfn + level_size(level) - 1)) { |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1077 | dma_clear_pte(pte); |
| 1078 | domain_flush_cache(domain, pte, sizeof(*pte)); |
| 1079 | free_pgtable_page(level_pte); |
| 1080 | } |
| 1081 | next: |
| 1082 | pfn += level_size(level); |
| 1083 | } while (!first_pte_in_page(++pte) && pfn <= last_pfn); |
| 1084 | } |
| 1085 | |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1086 | /* |
| 1087 | * clear last level (leaf) ptes and free page table pages below the |
| 1088 | * level we wish to keep intact. |
| 1089 | */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1090 | static void dma_pte_free_pagetable(struct dmar_domain *domain, |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 1091 | unsigned long start_pfn, |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1092 | unsigned long last_pfn, |
| 1093 | int retain_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1094 | { |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 1095 | BUG_ON(!domain_pfn_supported(domain, start_pfn)); |
| 1096 | BUG_ON(!domain_pfn_supported(domain, last_pfn)); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 1097 | BUG_ON(start_pfn > last_pfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1098 | |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 1099 | dma_pte_clear_range(domain, start_pfn, last_pfn); |
| 1100 | |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 1101 | /* We don't need lock here; nobody else touches the iova range */ |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1102 | dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level, |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1103 | domain->pgd, 0, start_pfn, last_pfn); |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 1104 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1105 | /* free pgd */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 1106 | if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1107 | free_pgtable_page(domain->pgd); |
| 1108 | domain->pgd = NULL; |
| 1109 | } |
| 1110 | } |
| 1111 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1112 | /* When a page at a given level is being unlinked from its parent, we don't |
| 1113 | need to *modify* it at all. All we need to do is make a list of all the |
| 1114 | pages which can be freed just as soon as we've flushed the IOTLB and we |
| 1115 | know the hardware page-walk will no longer touch them. |
| 1116 | The 'pte' argument is the *parent* PTE, pointing to the page that is to |
| 1117 | be freed. */ |
| 1118 | static struct page *dma_pte_list_pagetables(struct dmar_domain *domain, |
| 1119 | int level, struct dma_pte *pte, |
| 1120 | struct page *freelist) |
| 1121 | { |
| 1122 | struct page *pg; |
| 1123 | |
| 1124 | pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT); |
| 1125 | pg->freelist = freelist; |
| 1126 | freelist = pg; |
| 1127 | |
| 1128 | if (level == 1) |
| 1129 | return freelist; |
| 1130 | |
Jiang Liu | adeb259 | 2014-04-09 10:20:39 +0800 | [diff] [blame] | 1131 | pte = page_address(pg); |
| 1132 | do { |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1133 | if (dma_pte_present(pte) && !dma_pte_superpage(pte)) |
| 1134 | freelist = dma_pte_list_pagetables(domain, level - 1, |
| 1135 | pte, freelist); |
Jiang Liu | adeb259 | 2014-04-09 10:20:39 +0800 | [diff] [blame] | 1136 | pte++; |
| 1137 | } while (!first_pte_in_page(pte)); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1138 | |
| 1139 | return freelist; |
| 1140 | } |
| 1141 | |
| 1142 | static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level, |
| 1143 | struct dma_pte *pte, unsigned long pfn, |
| 1144 | unsigned long start_pfn, |
| 1145 | unsigned long last_pfn, |
| 1146 | struct page *freelist) |
| 1147 | { |
| 1148 | struct dma_pte *first_pte = NULL, *last_pte = NULL; |
| 1149 | |
| 1150 | pfn = max(start_pfn, pfn); |
| 1151 | pte = &pte[pfn_level_offset(pfn, level)]; |
| 1152 | |
| 1153 | do { |
| 1154 | unsigned long level_pfn; |
| 1155 | |
| 1156 | if (!dma_pte_present(pte)) |
| 1157 | goto next; |
| 1158 | |
| 1159 | level_pfn = pfn & level_mask(level); |
| 1160 | |
| 1161 | /* If range covers entire pagetable, free it */ |
| 1162 | if (start_pfn <= level_pfn && |
| 1163 | last_pfn >= level_pfn + level_size(level) - 1) { |
| 1164 | /* These suborbinate page tables are going away entirely. Don't |
| 1165 | bother to clear them; we're just going to *free* them. */ |
| 1166 | if (level > 1 && !dma_pte_superpage(pte)) |
| 1167 | freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist); |
| 1168 | |
| 1169 | dma_clear_pte(pte); |
| 1170 | if (!first_pte) |
| 1171 | first_pte = pte; |
| 1172 | last_pte = pte; |
| 1173 | } else if (level > 1) { |
| 1174 | /* Recurse down into a level that isn't *entirely* obsolete */ |
| 1175 | freelist = dma_pte_clear_level(domain, level - 1, |
| 1176 | phys_to_virt(dma_pte_addr(pte)), |
| 1177 | level_pfn, start_pfn, last_pfn, |
| 1178 | freelist); |
| 1179 | } |
| 1180 | next: |
| 1181 | pfn += level_size(level); |
| 1182 | } while (!first_pte_in_page(++pte) && pfn <= last_pfn); |
| 1183 | |
| 1184 | if (first_pte) |
| 1185 | domain_flush_cache(domain, first_pte, |
| 1186 | (void *)++last_pte - (void *)first_pte); |
| 1187 | |
| 1188 | return freelist; |
| 1189 | } |
| 1190 | |
| 1191 | /* We can't just free the pages because the IOMMU may still be walking |
| 1192 | the page tables, and may have cached the intermediate levels. The |
| 1193 | pages can only be freed after the IOTLB flush has been done. */ |
Joerg Roedel | b690420 | 2015-08-13 11:32:18 +0200 | [diff] [blame] | 1194 | static struct page *domain_unmap(struct dmar_domain *domain, |
| 1195 | unsigned long start_pfn, |
| 1196 | unsigned long last_pfn) |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1197 | { |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1198 | struct page *freelist = NULL; |
| 1199 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 1200 | BUG_ON(!domain_pfn_supported(domain, start_pfn)); |
| 1201 | BUG_ON(!domain_pfn_supported(domain, last_pfn)); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1202 | BUG_ON(start_pfn > last_pfn); |
| 1203 | |
| 1204 | /* we don't need lock here; nobody else touches the iova range */ |
| 1205 | freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw), |
| 1206 | domain->pgd, 0, start_pfn, last_pfn, NULL); |
| 1207 | |
| 1208 | /* free pgd */ |
| 1209 | if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { |
| 1210 | struct page *pgd_page = virt_to_page(domain->pgd); |
| 1211 | pgd_page->freelist = freelist; |
| 1212 | freelist = pgd_page; |
| 1213 | |
| 1214 | domain->pgd = NULL; |
| 1215 | } |
| 1216 | |
| 1217 | return freelist; |
| 1218 | } |
| 1219 | |
Joerg Roedel | b690420 | 2015-08-13 11:32:18 +0200 | [diff] [blame] | 1220 | static void dma_free_pagelist(struct page *freelist) |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1221 | { |
| 1222 | struct page *pg; |
| 1223 | |
| 1224 | while ((pg = freelist)) { |
| 1225 | freelist = pg->freelist; |
| 1226 | free_pgtable_page(page_address(pg)); |
| 1227 | } |
| 1228 | } |
| 1229 | |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1230 | static void iova_entry_free(unsigned long data) |
| 1231 | { |
| 1232 | struct page *freelist = (struct page *)data; |
| 1233 | |
| 1234 | dma_free_pagelist(freelist); |
| 1235 | } |
| 1236 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1237 | /* iommu handling */ |
| 1238 | static int iommu_alloc_root_entry(struct intel_iommu *iommu) |
| 1239 | { |
| 1240 | struct root_entry *root; |
| 1241 | unsigned long flags; |
| 1242 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1243 | root = (struct root_entry *)alloc_pgtable_page(iommu->node); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1244 | if (!root) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1245 | pr_err("Allocating root entry for %s failed\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1246 | iommu->name); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1247 | return -ENOMEM; |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1248 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1249 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1250 | __iommu_flush_cache(iommu, root, ROOT_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1251 | |
| 1252 | spin_lock_irqsave(&iommu->lock, flags); |
| 1253 | iommu->root_entry = root; |
| 1254 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1255 | |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1259 | static void iommu_set_root_entry(struct intel_iommu *iommu) |
| 1260 | { |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1261 | u64 addr; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1262 | u32 sts; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1263 | unsigned long flag; |
| 1264 | |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1265 | addr = virt_to_phys(iommu->root_entry); |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 1266 | if (ecs_enabled(iommu)) |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1267 | addr |= DMA_RTADDR_RTT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1268 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1269 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1270 | dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1271 | |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1272 | writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1273 | |
| 1274 | /* Make sure hardware complete it */ |
| 1275 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1276 | readl, (sts & DMA_GSTS_RTPS), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1277 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1278 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | static void iommu_flush_write_buffer(struct intel_iommu *iommu) |
| 1282 | { |
| 1283 | u32 val; |
| 1284 | unsigned long flag; |
| 1285 | |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 1286 | if (!rwbf_quirk && !cap_rwbf(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1287 | return; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1288 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1289 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
David Woodhouse | 462b60f | 2009-05-10 20:18:18 +0100 | [diff] [blame] | 1290 | writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1291 | |
| 1292 | /* Make sure hardware complete it */ |
| 1293 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1294 | readl, (!(val & DMA_GSTS_WBFS)), val); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1295 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1296 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1300 | static void __iommu_flush_context(struct intel_iommu *iommu, |
| 1301 | u16 did, u16 source_id, u8 function_mask, |
| 1302 | u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1303 | { |
| 1304 | u64 val = 0; |
| 1305 | unsigned long flag; |
| 1306 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1307 | switch (type) { |
| 1308 | case DMA_CCMD_GLOBAL_INVL: |
| 1309 | val = DMA_CCMD_GLOBAL_INVL; |
| 1310 | break; |
| 1311 | case DMA_CCMD_DOMAIN_INVL: |
| 1312 | val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did); |
| 1313 | break; |
| 1314 | case DMA_CCMD_DEVICE_INVL: |
| 1315 | val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did) |
| 1316 | | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask); |
| 1317 | break; |
| 1318 | default: |
| 1319 | BUG(); |
| 1320 | } |
| 1321 | val |= DMA_CCMD_ICC; |
| 1322 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1323 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1324 | dmar_writeq(iommu->reg + DMAR_CCMD_REG, val); |
| 1325 | |
| 1326 | /* Make sure hardware complete it */ |
| 1327 | IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG, |
| 1328 | dmar_readq, (!(val & DMA_CCMD_ICC)), val); |
| 1329 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1330 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1333 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1334 | static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, |
| 1335 | u64 addr, unsigned int size_order, u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1336 | { |
| 1337 | int tlb_offset = ecap_iotlb_offset(iommu->ecap); |
| 1338 | u64 val = 0, val_iva = 0; |
| 1339 | unsigned long flag; |
| 1340 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1341 | switch (type) { |
| 1342 | case DMA_TLB_GLOBAL_FLUSH: |
| 1343 | /* global flush doesn't need set IVA_REG */ |
| 1344 | val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT; |
| 1345 | break; |
| 1346 | case DMA_TLB_DSI_FLUSH: |
| 1347 | val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
| 1348 | break; |
| 1349 | case DMA_TLB_PSI_FLUSH: |
| 1350 | val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1351 | /* IH bit is passed in as part of address */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1352 | val_iva = size_order | addr; |
| 1353 | break; |
| 1354 | default: |
| 1355 | BUG(); |
| 1356 | } |
| 1357 | /* Note: set drain read/write */ |
| 1358 | #if 0 |
| 1359 | /* |
| 1360 | * This is probably to be super secure.. Looks like we can |
| 1361 | * ignore it without any impact. |
| 1362 | */ |
| 1363 | if (cap_read_drain(iommu->cap)) |
| 1364 | val |= DMA_TLB_READ_DRAIN; |
| 1365 | #endif |
| 1366 | if (cap_write_drain(iommu->cap)) |
| 1367 | val |= DMA_TLB_WRITE_DRAIN; |
| 1368 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1369 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1370 | /* Note: Only uses first TLB reg currently */ |
| 1371 | if (val_iva) |
| 1372 | dmar_writeq(iommu->reg + tlb_offset, val_iva); |
| 1373 | dmar_writeq(iommu->reg + tlb_offset + 8, val); |
| 1374 | |
| 1375 | /* Make sure hardware complete it */ |
| 1376 | IOMMU_WAIT_OP(iommu, tlb_offset + 8, |
| 1377 | dmar_readq, (!(val & DMA_TLB_IVT)), val); |
| 1378 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1379 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1380 | |
| 1381 | /* check IOTLB invalidation granularity */ |
| 1382 | if (DMA_TLB_IAIG(val) == 0) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1383 | pr_err("Flush IOTLB failed\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1384 | if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type)) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1385 | pr_debug("TLB flush request %Lx, actual %Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1386 | (unsigned long long)DMA_TLB_IIRG(type), |
| 1387 | (unsigned long long)DMA_TLB_IAIG(val)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 1390 | static struct device_domain_info * |
| 1391 | iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu, |
| 1392 | u8 bus, u8 devfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1393 | { |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1394 | struct device_domain_info *info; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1395 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1396 | assert_spin_locked(&device_domain_lock); |
| 1397 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1398 | if (!iommu->qi) |
| 1399 | return NULL; |
| 1400 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1401 | list_for_each_entry(info, &domain->devices, link) |
Jiang Liu | c3b497c | 2014-07-11 14:19:25 +0800 | [diff] [blame] | 1402 | if (info->iommu == iommu && info->bus == bus && |
| 1403 | info->devfn == devfn) { |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1404 | if (info->ats_supported && info->dev) |
| 1405 | return info; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1406 | break; |
| 1407 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1408 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1409 | return NULL; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1410 | } |
| 1411 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1412 | static void domain_update_iotlb(struct dmar_domain *domain) |
| 1413 | { |
| 1414 | struct device_domain_info *info; |
| 1415 | bool has_iotlb_device = false; |
| 1416 | |
| 1417 | assert_spin_locked(&device_domain_lock); |
| 1418 | |
| 1419 | list_for_each_entry(info, &domain->devices, link) { |
| 1420 | struct pci_dev *pdev; |
| 1421 | |
| 1422 | if (!info->dev || !dev_is_pci(info->dev)) |
| 1423 | continue; |
| 1424 | |
| 1425 | pdev = to_pci_dev(info->dev); |
| 1426 | if (pdev->ats_enabled) { |
| 1427 | has_iotlb_device = true; |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | domain->has_iotlb_device = has_iotlb_device; |
| 1433 | } |
| 1434 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1435 | static void iommu_enable_dev_iotlb(struct device_domain_info *info) |
| 1436 | { |
Bjorn Helgaas | fb0cc3a | 2015-07-20 09:10:36 -0500 | [diff] [blame] | 1437 | struct pci_dev *pdev; |
| 1438 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1439 | assert_spin_locked(&device_domain_lock); |
| 1440 | |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 1441 | if (!info || !dev_is_pci(info->dev)) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1442 | return; |
| 1443 | |
Bjorn Helgaas | fb0cc3a | 2015-07-20 09:10:36 -0500 | [diff] [blame] | 1444 | pdev = to_pci_dev(info->dev); |
Jacob Pan | 1c48db4 | 2018-06-07 09:57:00 -0700 | [diff] [blame] | 1445 | /* For IOMMU that supports device IOTLB throttling (DIT), we assign |
| 1446 | * PFSID to the invalidation desc of a VF such that IOMMU HW can gauge |
| 1447 | * queue depth at PF level. If DIT is not set, PFSID will be treated as |
| 1448 | * reserved, which should be set to 0. |
| 1449 | */ |
| 1450 | if (!ecap_dit(info->iommu->ecap)) |
| 1451 | info->pfsid = 0; |
| 1452 | else { |
| 1453 | struct pci_dev *pf_pdev; |
| 1454 | |
| 1455 | /* pdev will be returned if device is not a vf */ |
| 1456 | pf_pdev = pci_physfn(pdev); |
| 1457 | info->pfsid = PCI_DEVID(pf_pdev->bus->number, pf_pdev->devfn); |
| 1458 | } |
Bjorn Helgaas | fb0cc3a | 2015-07-20 09:10:36 -0500 | [diff] [blame] | 1459 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1460 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 1461 | /* The PCIe spec, in its wisdom, declares that the behaviour of |
| 1462 | the device if you enable PASID support after ATS support is |
| 1463 | undefined. So always enable PASID support on devices which |
| 1464 | have it, even if we can't yet know if we're ever going to |
| 1465 | use it. */ |
| 1466 | if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1)) |
| 1467 | info->pasid_enabled = 1; |
| 1468 | |
| 1469 | if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32)) |
| 1470 | info->pri_enabled = 1; |
| 1471 | #endif |
| 1472 | if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) { |
| 1473 | info->ats_enabled = 1; |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1474 | domain_update_iotlb(info->domain); |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1475 | info->ats_qdep = pci_ats_queue_depth(pdev); |
| 1476 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | static void iommu_disable_dev_iotlb(struct device_domain_info *info) |
| 1480 | { |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1481 | struct pci_dev *pdev; |
| 1482 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1483 | assert_spin_locked(&device_domain_lock); |
| 1484 | |
Jeremy McNicoll | da972fb | 2016-01-14 21:33:06 -0800 | [diff] [blame] | 1485 | if (!dev_is_pci(info->dev)) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1486 | return; |
| 1487 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1488 | pdev = to_pci_dev(info->dev); |
| 1489 | |
| 1490 | if (info->ats_enabled) { |
| 1491 | pci_disable_ats(pdev); |
| 1492 | info->ats_enabled = 0; |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1493 | domain_update_iotlb(info->domain); |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1494 | } |
| 1495 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 1496 | if (info->pri_enabled) { |
| 1497 | pci_disable_pri(pdev); |
| 1498 | info->pri_enabled = 0; |
| 1499 | } |
| 1500 | if (info->pasid_enabled) { |
| 1501 | pci_disable_pasid(pdev); |
| 1502 | info->pasid_enabled = 0; |
| 1503 | } |
| 1504 | #endif |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | static void iommu_flush_dev_iotlb(struct dmar_domain *domain, |
| 1508 | u64 addr, unsigned mask) |
| 1509 | { |
| 1510 | u16 sid, qdep; |
| 1511 | unsigned long flags; |
| 1512 | struct device_domain_info *info; |
| 1513 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1514 | if (!domain->has_iotlb_device) |
| 1515 | return; |
| 1516 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1517 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1518 | list_for_each_entry(info, &domain->devices, link) { |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1519 | if (!info->ats_enabled) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1520 | continue; |
| 1521 | |
| 1522 | sid = info->bus << 8 | info->devfn; |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1523 | qdep = info->ats_qdep; |
Jacob Pan | 1c48db4 | 2018-06-07 09:57:00 -0700 | [diff] [blame] | 1524 | qi_flush_dev_iotlb(info->iommu, sid, info->pfsid, |
| 1525 | qdep, addr, mask); |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1526 | } |
| 1527 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1528 | } |
| 1529 | |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 1530 | static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, |
| 1531 | struct dmar_domain *domain, |
| 1532 | unsigned long pfn, unsigned int pages, |
| 1533 | int ih, int map) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1534 | { |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1535 | unsigned int mask = ilog2(__roundup_pow_of_two(pages)); |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 1536 | uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT; |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 1537 | u16 did = domain->iommu_did[iommu->seq_id]; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1538 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1539 | BUG_ON(pages == 0); |
| 1540 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1541 | if (ih) |
| 1542 | ih = 1 << 6; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1543 | /* |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1544 | * Fallback to domain selective flush if no PSI support or the size is |
| 1545 | * too big. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1546 | * PSI requires page size to be 2 ^ x, and the base address is naturally |
| 1547 | * aligned to the size |
| 1548 | */ |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1549 | if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap)) |
| 1550 | iommu->flush.flush_iotlb(iommu, did, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1551 | DMA_TLB_DSI_FLUSH); |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1552 | else |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1553 | iommu->flush.flush_iotlb(iommu, did, addr | ih, mask, |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1554 | DMA_TLB_PSI_FLUSH); |
Yu Zhao | bf92df3 | 2009-06-29 11:31:45 +0800 | [diff] [blame] | 1555 | |
| 1556 | /* |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1557 | * In caching mode, changes of pages from non-present to present require |
| 1558 | * 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] | 1559 | */ |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1560 | if (!cap_caching_mode(iommu->cap) || !map) |
Peter Xu | 9d2e650 | 2018-01-10 13:51:37 +0800 | [diff] [blame] | 1561 | iommu_flush_dev_iotlb(domain, addr, mask); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Peter Xu | eed91a0 | 2018-05-04 10:34:52 +0800 | [diff] [blame] | 1564 | /* Notification for newly created mappings */ |
| 1565 | static inline void __mapping_notify_one(struct intel_iommu *iommu, |
| 1566 | struct dmar_domain *domain, |
| 1567 | unsigned long pfn, unsigned int pages) |
| 1568 | { |
| 1569 | /* It's a non-present to present mapping. Only flush if caching mode */ |
| 1570 | if (cap_caching_mode(iommu->cap)) |
| 1571 | iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1); |
| 1572 | else |
| 1573 | iommu_flush_write_buffer(iommu); |
| 1574 | } |
| 1575 | |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1576 | static void iommu_flush_iova(struct iova_domain *iovad) |
| 1577 | { |
| 1578 | struct dmar_domain *domain; |
| 1579 | int idx; |
| 1580 | |
| 1581 | domain = container_of(iovad, struct dmar_domain, iovad); |
| 1582 | |
| 1583 | for_each_domain_iommu(idx, domain) { |
| 1584 | struct intel_iommu *iommu = g_iommus[idx]; |
| 1585 | u16 did = domain->iommu_did[iommu->seq_id]; |
| 1586 | |
| 1587 | iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); |
| 1588 | |
| 1589 | if (!cap_caching_mode(iommu->cap)) |
| 1590 | iommu_flush_dev_iotlb(get_iommu_domain(iommu, did), |
| 1591 | 0, MAX_AGAW_PFN_WIDTH); |
| 1592 | } |
| 1593 | } |
| 1594 | |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1595 | static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu) |
| 1596 | { |
| 1597 | u32 pmen; |
| 1598 | unsigned long flags; |
| 1599 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1600 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1601 | pmen = readl(iommu->reg + DMAR_PMEN_REG); |
| 1602 | pmen &= ~DMA_PMEN_EPM; |
| 1603 | writel(pmen, iommu->reg + DMAR_PMEN_REG); |
| 1604 | |
| 1605 | /* wait for the protected region status bit to clear */ |
| 1606 | IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG, |
| 1607 | readl, !(pmen & DMA_PMEN_PRS), pmen); |
| 1608 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1609 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
Jiang Liu | 2a41cce | 2014-07-11 14:19:33 +0800 | [diff] [blame] | 1612 | static void iommu_enable_translation(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1613 | { |
| 1614 | u32 sts; |
| 1615 | unsigned long flags; |
| 1616 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1617 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1618 | iommu->gcmd |= DMA_GCMD_TE; |
| 1619 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1620 | |
| 1621 | /* Make sure hardware complete it */ |
| 1622 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1623 | readl, (sts & DMA_GSTS_TES), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1624 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1625 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Jiang Liu | 2a41cce | 2014-07-11 14:19:33 +0800 | [diff] [blame] | 1628 | static void iommu_disable_translation(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1629 | { |
| 1630 | u32 sts; |
| 1631 | unsigned long flag; |
| 1632 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1633 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1634 | iommu->gcmd &= ~DMA_GCMD_TE; |
| 1635 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 1636 | |
| 1637 | /* Make sure hardware complete it */ |
| 1638 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1639 | readl, (!(sts & DMA_GSTS_TES)), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1640 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1641 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 1644 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1645 | static int iommu_init_domains(struct intel_iommu *iommu) |
| 1646 | { |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1647 | u32 ndomains, nlongs; |
| 1648 | size_t size; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1649 | |
| 1650 | ndomains = cap_ndoms(iommu->cap); |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1651 | pr_debug("%s: Number of Domains supported <%d>\n", |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1652 | iommu->name, ndomains); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1653 | nlongs = BITS_TO_LONGS(ndomains); |
| 1654 | |
Donald Dutile | 94a91b50 | 2009-08-20 16:51:34 -0400 | [diff] [blame] | 1655 | spin_lock_init(&iommu->lock); |
| 1656 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1657 | iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL); |
| 1658 | if (!iommu->domain_ids) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1659 | pr_err("%s: Allocating domain id array failed\n", |
| 1660 | iommu->name); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1661 | return -ENOMEM; |
| 1662 | } |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1663 | |
Wei Yang | 86f004c | 2016-05-21 02:41:51 +0000 | [diff] [blame] | 1664 | size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **); |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1665 | iommu->domains = kzalloc(size, GFP_KERNEL); |
| 1666 | |
| 1667 | if (iommu->domains) { |
| 1668 | size = 256 * sizeof(struct dmar_domain *); |
| 1669 | iommu->domains[0] = kzalloc(size, GFP_KERNEL); |
| 1670 | } |
| 1671 | |
| 1672 | if (!iommu->domains || !iommu->domains[0]) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1673 | pr_err("%s: Allocating domain array failed\n", |
| 1674 | iommu->name); |
Jiang Liu | 852bdb0 | 2014-01-06 14:18:11 +0800 | [diff] [blame] | 1675 | kfree(iommu->domain_ids); |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1676 | kfree(iommu->domains); |
Jiang Liu | 852bdb0 | 2014-01-06 14:18:11 +0800 | [diff] [blame] | 1677 | iommu->domain_ids = NULL; |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1678 | iommu->domains = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1679 | return -ENOMEM; |
| 1680 | } |
| 1681 | |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1682 | |
| 1683 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1684 | /* |
Joerg Roedel | c0e8a6c | 2015-07-21 09:39:46 +0200 | [diff] [blame] | 1685 | * If Caching mode is set, then invalid translations are tagged |
| 1686 | * with domain-id 0, hence we need to pre-allocate it. We also |
| 1687 | * use domain-id 0 as a marker for non-allocated domain-id, so |
| 1688 | * make sure it is not used for a real domain. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1689 | */ |
Joerg Roedel | c0e8a6c | 2015-07-21 09:39:46 +0200 | [diff] [blame] | 1690 | set_bit(0, iommu->domain_ids); |
| 1691 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1692 | return 0; |
| 1693 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1694 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1695 | static void disable_dmar_iommu(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1696 | { |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1697 | struct device_domain_info *info, *tmp; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1698 | unsigned long flags; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1699 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1700 | if (!iommu->domains || !iommu->domain_ids) |
| 1701 | return; |
Jiang Liu | a4eaa86 | 2014-02-19 14:07:30 +0800 | [diff] [blame] | 1702 | |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1703 | again: |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1704 | spin_lock_irqsave(&device_domain_lock, flags); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1705 | list_for_each_entry_safe(info, tmp, &device_domain_list, global) { |
| 1706 | struct dmar_domain *domain; |
| 1707 | |
| 1708 | if (info->iommu != iommu) |
| 1709 | continue; |
| 1710 | |
| 1711 | if (!info->dev || !info->domain) |
| 1712 | continue; |
| 1713 | |
| 1714 | domain = info->domain; |
| 1715 | |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1716 | __dmar_remove_one_dev_info(info); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1717 | |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1718 | if (!domain_type_is_vm_or_si(domain)) { |
| 1719 | /* |
| 1720 | * The domain_exit() function can't be called under |
| 1721 | * device_domain_lock, as it takes this lock itself. |
| 1722 | * So release the lock here and re-run the loop |
| 1723 | * afterwards. |
| 1724 | */ |
| 1725 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1726 | domain_exit(domain); |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1727 | goto again; |
| 1728 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1729 | } |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1730 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1731 | |
| 1732 | if (iommu->gcmd & DMA_GCMD_TE) |
| 1733 | iommu_disable_translation(iommu); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1734 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1735 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1736 | static void free_dmar_iommu(struct intel_iommu *iommu) |
| 1737 | { |
| 1738 | if ((iommu->domains) && (iommu->domain_ids)) { |
Wei Yang | 86f004c | 2016-05-21 02:41:51 +0000 | [diff] [blame] | 1739 | int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8; |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1740 | int i; |
| 1741 | |
| 1742 | for (i = 0; i < elems; i++) |
| 1743 | kfree(iommu->domains[i]); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1744 | kfree(iommu->domains); |
| 1745 | kfree(iommu->domain_ids); |
| 1746 | iommu->domains = NULL; |
| 1747 | iommu->domain_ids = NULL; |
| 1748 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1749 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 1750 | g_iommus[iommu->seq_id] = NULL; |
| 1751 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1752 | /* free context mapping */ |
| 1753 | free_context_table(iommu); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 1754 | |
| 1755 | #ifdef CONFIG_INTEL_IOMMU_SVM |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 1756 | if (pasid_enabled(iommu)) { |
| 1757 | if (ecap_prs(iommu->ecap)) |
| 1758 | intel_svm_finish_prq(iommu); |
Lu Baolu | d973795 | 2018-07-14 15:47:02 +0800 | [diff] [blame^] | 1759 | intel_svm_exit(iommu); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 1760 | } |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 1761 | #endif |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 1764 | static struct dmar_domain *alloc_domain(int flags) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1765 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1766 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1767 | |
| 1768 | domain = alloc_domain_mem(); |
| 1769 | if (!domain) |
| 1770 | return NULL; |
| 1771 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 1772 | memset(domain, 0, sizeof(*domain)); |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1773 | domain->nid = -1; |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 1774 | domain->flags = flags; |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1775 | domain->has_iotlb_device = false; |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 1776 | INIT_LIST_HEAD(&domain->devices); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1777 | |
| 1778 | return domain; |
| 1779 | } |
| 1780 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1781 | /* Must be called with iommu->lock */ |
| 1782 | static int domain_attach_iommu(struct dmar_domain *domain, |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1783 | struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1784 | { |
Jiang Liu | 44bde61 | 2014-07-11 14:19:29 +0800 | [diff] [blame] | 1785 | unsigned long ndomains; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1786 | int num; |
Jiang Liu | 44bde61 | 2014-07-11 14:19:29 +0800 | [diff] [blame] | 1787 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1788 | assert_spin_locked(&device_domain_lock); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1789 | assert_spin_locked(&iommu->lock); |
Jiang Liu | 44bde61 | 2014-07-11 14:19:29 +0800 | [diff] [blame] | 1790 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1791 | domain->iommu_refcnt[iommu->seq_id] += 1; |
| 1792 | domain->iommu_count += 1; |
| 1793 | if (domain->iommu_refcnt[iommu->seq_id] == 1) { |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1794 | ndomains = cap_ndoms(iommu->cap); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1795 | num = find_first_zero_bit(iommu->domain_ids, ndomains); |
| 1796 | |
| 1797 | if (num >= ndomains) { |
| 1798 | pr_err("%s: No free domain ids\n", iommu->name); |
| 1799 | domain->iommu_refcnt[iommu->seq_id] -= 1; |
| 1800 | domain->iommu_count -= 1; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1801 | return -ENOSPC; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1802 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1803 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1804 | set_bit(num, iommu->domain_ids); |
| 1805 | set_iommu_domain(iommu, num, domain); |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1806 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1807 | domain->iommu_did[iommu->seq_id] = num; |
| 1808 | domain->nid = iommu->node; |
| 1809 | |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1810 | domain_update_iommu_cap(domain); |
| 1811 | } |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1812 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1813 | return 0; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | static int domain_detach_iommu(struct dmar_domain *domain, |
| 1817 | struct intel_iommu *iommu) |
| 1818 | { |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1819 | int num, count = INT_MAX; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1820 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1821 | assert_spin_locked(&device_domain_lock); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1822 | assert_spin_locked(&iommu->lock); |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1823 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1824 | domain->iommu_refcnt[iommu->seq_id] -= 1; |
| 1825 | count = --domain->iommu_count; |
| 1826 | if (domain->iommu_refcnt[iommu->seq_id] == 0) { |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1827 | num = domain->iommu_did[iommu->seq_id]; |
| 1828 | clear_bit(num, iommu->domain_ids); |
| 1829 | set_iommu_domain(iommu, num, NULL); |
| 1830 | |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1831 | domain_update_iommu_cap(domain); |
Joerg Roedel | c0e8a6c | 2015-07-21 09:39:46 +0200 | [diff] [blame] | 1832 | domain->iommu_did[iommu->seq_id] = 0; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1833 | } |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1834 | |
| 1835 | return count; |
| 1836 | } |
| 1837 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1838 | static struct iova_domain reserved_iova_list; |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1839 | static struct lock_class_key reserved_rbtree_key; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1840 | |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1841 | static int dmar_init_reserved_ranges(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1842 | { |
| 1843 | struct pci_dev *pdev = NULL; |
| 1844 | struct iova *iova; |
| 1845 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1846 | |
Zhen Lei | aa3ac94 | 2017-09-21 16:52:45 +0100 | [diff] [blame] | 1847 | init_iova_domain(&reserved_iova_list, VTD_PAGE_SIZE, IOVA_START_PFN); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1848 | |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1849 | lockdep_set_class(&reserved_iova_list.iova_rbtree_lock, |
| 1850 | &reserved_rbtree_key); |
| 1851 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1852 | /* IOAPIC ranges shouldn't be accessed by DMA */ |
| 1853 | iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START), |
| 1854 | IOVA_PFN(IOAPIC_RANGE_END)); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1855 | if (!iova) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1856 | pr_err("Reserve IOAPIC range failed\n"); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1857 | return -ENODEV; |
| 1858 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1859 | |
| 1860 | /* Reserve all PCI MMIO to avoid peer-to-peer access */ |
| 1861 | for_each_pci_dev(pdev) { |
| 1862 | struct resource *r; |
| 1863 | |
| 1864 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 1865 | r = &pdev->resource[i]; |
| 1866 | if (!r->flags || !(r->flags & IORESOURCE_MEM)) |
| 1867 | continue; |
David Woodhouse | 1a4a455 | 2009-06-28 16:00:42 +0100 | [diff] [blame] | 1868 | iova = reserve_iova(&reserved_iova_list, |
| 1869 | IOVA_PFN(r->start), |
| 1870 | IOVA_PFN(r->end)); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1871 | if (!iova) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1872 | pr_err("Reserve iova failed\n"); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1873 | return -ENODEV; |
| 1874 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1875 | } |
| 1876 | } |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1877 | return 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | static void domain_reserve_special_ranges(struct dmar_domain *domain) |
| 1881 | { |
| 1882 | copy_reserved_iova(&reserved_iova_list, &domain->iovad); |
| 1883 | } |
| 1884 | |
| 1885 | static inline int guestwidth_to_adjustwidth(int gaw) |
| 1886 | { |
| 1887 | int agaw; |
| 1888 | int r = (gaw - 12) % 9; |
| 1889 | |
| 1890 | if (r == 0) |
| 1891 | agaw = gaw; |
| 1892 | else |
| 1893 | agaw = gaw + 9 - r; |
| 1894 | if (agaw > 64) |
| 1895 | agaw = 64; |
| 1896 | return agaw; |
| 1897 | } |
| 1898 | |
Joerg Roedel | dc534b2 | 2015-07-22 12:44:02 +0200 | [diff] [blame] | 1899 | static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu, |
| 1900 | int guest_width) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1901 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1902 | int adjust_width, agaw; |
| 1903 | unsigned long sagaw; |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1904 | int err; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1905 | |
Zhen Lei | aa3ac94 | 2017-09-21 16:52:45 +0100 | [diff] [blame] | 1906 | init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN); |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1907 | |
| 1908 | err = init_iova_flush_queue(&domain->iovad, |
| 1909 | iommu_flush_iova, iova_entry_free); |
| 1910 | if (err) |
| 1911 | return err; |
| 1912 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1913 | domain_reserve_special_ranges(domain); |
| 1914 | |
| 1915 | /* calculate AGAW */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1916 | if (guest_width > cap_mgaw(iommu->cap)) |
| 1917 | guest_width = cap_mgaw(iommu->cap); |
| 1918 | domain->gaw = guest_width; |
| 1919 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 1920 | agaw = width_to_agaw(adjust_width); |
| 1921 | sagaw = cap_sagaw(iommu->cap); |
| 1922 | if (!test_bit(agaw, &sagaw)) { |
| 1923 | /* hardware doesn't support it, choose a bigger one */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1924 | pr_debug("Hardware doesn't support agaw %d\n", agaw); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1925 | agaw = find_next_bit(&sagaw, 5, agaw); |
| 1926 | if (agaw >= 5) |
| 1927 | return -ENODEV; |
| 1928 | } |
| 1929 | domain->agaw = agaw; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1930 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 1931 | if (ecap_coherent(iommu->ecap)) |
| 1932 | domain->iommu_coherency = 1; |
| 1933 | else |
| 1934 | domain->iommu_coherency = 0; |
| 1935 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 1936 | if (ecap_sc_support(iommu->ecap)) |
| 1937 | domain->iommu_snooping = 1; |
| 1938 | else |
| 1939 | domain->iommu_snooping = 0; |
| 1940 | |
David Woodhouse | 214e39a | 2014-03-19 10:38:49 +0000 | [diff] [blame] | 1941 | if (intel_iommu_superpage) |
| 1942 | domain->iommu_superpage = fls(cap_super_page_val(iommu->cap)); |
| 1943 | else |
| 1944 | domain->iommu_superpage = 0; |
| 1945 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1946 | domain->nid = iommu->node; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1947 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1948 | /* always allocate the top pgd */ |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1949 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1950 | if (!domain->pgd) |
| 1951 | return -ENOMEM; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1952 | __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1953 | return 0; |
| 1954 | } |
| 1955 | |
| 1956 | static void domain_exit(struct dmar_domain *domain) |
| 1957 | { |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1958 | struct page *freelist = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1959 | |
| 1960 | /* Domain 0 is reserved, so dont process it */ |
| 1961 | if (!domain) |
| 1962 | return; |
| 1963 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1964 | /* Remove associated devices and clear attached or cached domains */ |
| 1965 | rcu_read_lock(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1966 | domain_remove_dev_info(domain); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1967 | rcu_read_unlock(); |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 1968 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1969 | /* destroy iovas */ |
| 1970 | put_iova_domain(&domain->iovad); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1971 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1972 | freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1973 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1974 | dma_free_pagelist(freelist); |
| 1975 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1976 | free_domain_mem(domain); |
| 1977 | } |
| 1978 | |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 1979 | static int domain_context_mapping_one(struct dmar_domain *domain, |
| 1980 | struct intel_iommu *iommu, |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1981 | u8 bus, u8 devfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1982 | { |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 1983 | u16 did = domain->iommu_did[iommu->seq_id]; |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1984 | int translation = CONTEXT_TT_MULTI_LEVEL; |
| 1985 | struct device_domain_info *info = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1986 | struct context_entry *context; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1987 | unsigned long flags; |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1988 | struct dma_pte *pgd; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1989 | int ret, agaw; |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1990 | |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 1991 | WARN_ON(did == 0); |
| 1992 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1993 | if (hw_pass_through && domain_type_is_si(domain)) |
| 1994 | translation = CONTEXT_TT_PASS_THROUGH; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1995 | |
| 1996 | pr_debug("Set context mapping for %02x:%02x.%d\n", |
| 1997 | bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1998 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1999 | BUG_ON(!domain->pgd); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2000 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 2001 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2002 | spin_lock(&iommu->lock); |
| 2003 | |
| 2004 | ret = -ENOMEM; |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 2005 | context = iommu_context_addr(iommu, bus, devfn, 1); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2006 | if (!context) |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 2007 | goto out_unlock; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2008 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 2009 | ret = 0; |
| 2010 | if (context_present(context)) |
| 2011 | goto out_unlock; |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 2012 | |
Xunlei Pang | aec0e86 | 2016-12-05 20:09:07 +0800 | [diff] [blame] | 2013 | /* |
| 2014 | * For kdump cases, old valid entries may be cached due to the |
| 2015 | * in-flight DMA and copied pgtable, but there is no unmapping |
| 2016 | * behaviour for them, thus we need an explicit cache flush for |
| 2017 | * the newly-mapped device. For kdump, at this point, the device |
| 2018 | * is supposed to finish reset at its driver probe stage, so no |
| 2019 | * in-flight DMA will exist, and we don't need to worry anymore |
| 2020 | * hereafter. |
| 2021 | */ |
| 2022 | if (context_copied(context)) { |
| 2023 | u16 did_old = context_domain_id(context); |
| 2024 | |
Christos Gkekas | b117e03 | 2017-10-08 23:33:31 +0100 | [diff] [blame] | 2025 | if (did_old < cap_ndoms(iommu->cap)) { |
Xunlei Pang | aec0e86 | 2016-12-05 20:09:07 +0800 | [diff] [blame] | 2026 | iommu->flush.flush_context(iommu, did_old, |
| 2027 | (((u16)bus) << 8) | devfn, |
| 2028 | DMA_CCMD_MASK_NOBIT, |
| 2029 | DMA_CCMD_DEVICE_INVL); |
KarimAllah Ahmed | f73a7ee | 2017-05-05 11:39:59 -0700 | [diff] [blame] | 2030 | iommu->flush.flush_iotlb(iommu, did_old, 0, 0, |
| 2031 | DMA_TLB_DSI_FLUSH); |
| 2032 | } |
Xunlei Pang | aec0e86 | 2016-12-05 20:09:07 +0800 | [diff] [blame] | 2033 | } |
| 2034 | |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 2035 | pgd = domain->pgd; |
| 2036 | |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2037 | context_clear_entry(context); |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2038 | context_set_domain_id(context, did); |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 2039 | |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2040 | /* |
| 2041 | * Skip top levels of page tables for iommu which has less agaw |
| 2042 | * than default. Unnecessary for PT mode. |
| 2043 | */ |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2044 | if (translation != CONTEXT_TT_PASS_THROUGH) { |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2045 | for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) { |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 2046 | ret = -ENOMEM; |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2047 | pgd = phys_to_virt(dma_pte_addr(pgd)); |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 2048 | if (!dma_pte_present(pgd)) |
| 2049 | goto out_unlock; |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 2052 | info = iommu_support_dev_iotlb(domain, iommu, bus, devfn); |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2053 | if (info && info->ats_supported) |
| 2054 | translation = CONTEXT_TT_DEV_IOTLB; |
| 2055 | else |
| 2056 | translation = CONTEXT_TT_MULTI_LEVEL; |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2057 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2058 | context_set_address_root(context, virt_to_phys(pgd)); |
| 2059 | context_set_address_width(context, iommu->agaw); |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2060 | } else { |
| 2061 | /* |
| 2062 | * In pass through mode, AW must be programmed to |
| 2063 | * indicate the largest AGAW value supported by |
| 2064 | * hardware. And ASR is ignored by hardware. |
| 2065 | */ |
| 2066 | context_set_address_width(context, iommu->msagaw); |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2067 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2068 | |
| 2069 | context_set_translation_type(context, translation); |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 2070 | context_set_fault_enable(context); |
| 2071 | context_set_present(context); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2072 | domain_flush_cache(domain, context, sizeof(*context)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2073 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2074 | /* |
| 2075 | * It's a non-present to present mapping. If hardware doesn't cache |
| 2076 | * non-present entry we only need to flush the write-buffer. If the |
| 2077 | * _does_ cache non-present entries, then it does so in the special |
| 2078 | * domain #0, which we have to flush: |
| 2079 | */ |
| 2080 | if (cap_caching_mode(iommu->cap)) { |
| 2081 | iommu->flush.flush_context(iommu, 0, |
| 2082 | (((u16)bus) << 8) | devfn, |
| 2083 | DMA_CCMD_MASK_NOBIT, |
| 2084 | DMA_CCMD_DEVICE_INVL); |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2085 | iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2086 | } else { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2087 | iommu_flush_write_buffer(iommu); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2088 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2089 | iommu_enable_dev_iotlb(info); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 2090 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 2091 | ret = 0; |
| 2092 | |
| 2093 | out_unlock: |
| 2094 | spin_unlock(&iommu->lock); |
| 2095 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 2096 | |
Wei Yang | 5c365d1 | 2016-07-13 13:53:21 +0000 | [diff] [blame] | 2097 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2100 | struct domain_context_mapping_data { |
| 2101 | struct dmar_domain *domain; |
| 2102 | struct intel_iommu *iommu; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2103 | }; |
| 2104 | |
| 2105 | static int domain_context_mapping_cb(struct pci_dev *pdev, |
| 2106 | u16 alias, void *opaque) |
| 2107 | { |
| 2108 | struct domain_context_mapping_data *data = opaque; |
| 2109 | |
| 2110 | return domain_context_mapping_one(data->domain, data->iommu, |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2111 | PCI_BUS_NUM(alias), alias & 0xff); |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2112 | } |
| 2113 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2114 | static int |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2115 | domain_context_mapping(struct dmar_domain *domain, struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2116 | { |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 2117 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 2118 | u8 bus, devfn; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2119 | struct domain_context_mapping_data data; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2120 | |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2121 | iommu = device_to_iommu(dev, &bus, &devfn); |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 2122 | if (!iommu) |
| 2123 | return -ENODEV; |
| 2124 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2125 | if (!dev_is_pci(dev)) |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2126 | return domain_context_mapping_one(domain, iommu, bus, devfn); |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2127 | |
| 2128 | data.domain = domain; |
| 2129 | data.iommu = iommu; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2130 | |
| 2131 | return pci_for_each_dma_alias(to_pci_dev(dev), |
| 2132 | &domain_context_mapping_cb, &data); |
| 2133 | } |
| 2134 | |
| 2135 | static int domain_context_mapped_cb(struct pci_dev *pdev, |
| 2136 | u16 alias, void *opaque) |
| 2137 | { |
| 2138 | struct intel_iommu *iommu = opaque; |
| 2139 | |
| 2140 | return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2141 | } |
| 2142 | |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2143 | static int domain_context_mapped(struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2144 | { |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2145 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 2146 | u8 bus, devfn; |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2147 | |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2148 | iommu = device_to_iommu(dev, &bus, &devfn); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2149 | if (!iommu) |
| 2150 | return -ENODEV; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2151 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2152 | if (!dev_is_pci(dev)) |
| 2153 | return device_context_mapped(iommu, bus, devfn); |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2154 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2155 | return !pci_for_each_dma_alias(to_pci_dev(dev), |
| 2156 | domain_context_mapped_cb, iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 2159 | /* Returns a number of VTD pages, but aligned to MM page size */ |
| 2160 | static inline unsigned long aligned_nrpages(unsigned long host_addr, |
| 2161 | size_t size) |
| 2162 | { |
| 2163 | host_addr &= ~PAGE_MASK; |
| 2164 | return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT; |
| 2165 | } |
| 2166 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2167 | /* Return largest possible superpage level for a given mapping */ |
| 2168 | static inline int hardware_largepage_caps(struct dmar_domain *domain, |
| 2169 | unsigned long iov_pfn, |
| 2170 | unsigned long phy_pfn, |
| 2171 | unsigned long pages) |
| 2172 | { |
| 2173 | int support, level = 1; |
| 2174 | unsigned long pfnmerge; |
| 2175 | |
| 2176 | support = domain->iommu_superpage; |
| 2177 | |
| 2178 | /* To use a large page, the virtual *and* physical addresses |
| 2179 | must be aligned to 2MiB/1GiB/etc. Lower bits set in either |
| 2180 | of them will mean we have to use smaller pages. So just |
| 2181 | merge them and check both at once. */ |
| 2182 | pfnmerge = iov_pfn | phy_pfn; |
| 2183 | |
| 2184 | while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) { |
| 2185 | pages >>= VTD_STRIDE_SHIFT; |
| 2186 | if (!pages) |
| 2187 | break; |
| 2188 | pfnmerge >>= VTD_STRIDE_SHIFT; |
| 2189 | level++; |
| 2190 | support--; |
| 2191 | } |
| 2192 | return level; |
| 2193 | } |
| 2194 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2195 | static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2196 | struct scatterlist *sg, unsigned long phys_pfn, |
| 2197 | unsigned long nr_pages, int prot) |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2198 | { |
| 2199 | struct dma_pte *first_pte = NULL, *pte = NULL; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2200 | phys_addr_t uninitialized_var(pteval); |
Jiang Liu | cc4f14a | 2014-11-26 09:42:10 +0800 | [diff] [blame] | 2201 | unsigned long sg_res = 0; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2202 | unsigned int largepage_lvl = 0; |
| 2203 | unsigned long lvl_pages = 0; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2204 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 2205 | BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1)); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2206 | |
| 2207 | if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) |
| 2208 | return -EINVAL; |
| 2209 | |
| 2210 | prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP; |
| 2211 | |
Jiang Liu | cc4f14a | 2014-11-26 09:42:10 +0800 | [diff] [blame] | 2212 | if (!sg) { |
| 2213 | sg_res = nr_pages; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2214 | pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot; |
| 2215 | } |
| 2216 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2217 | while (nr_pages > 0) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 2218 | uint64_t tmp; |
| 2219 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2220 | if (!sg_res) { |
Robin Murphy | 29a90b7 | 2017-09-28 15:14:01 +0100 | [diff] [blame] | 2221 | unsigned int pgoff = sg->offset & ~PAGE_MASK; |
| 2222 | |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 2223 | sg_res = aligned_nrpages(sg->offset, sg->length); |
Robin Murphy | 29a90b7 | 2017-09-28 15:14:01 +0100 | [diff] [blame] | 2224 | sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2225 | sg->dma_length = sg->length; |
Robin Murphy | 29a90b7 | 2017-09-28 15:14:01 +0100 | [diff] [blame] | 2226 | pteval = (sg_phys(sg) - pgoff) | prot; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2227 | phys_pfn = pteval >> VTD_PAGE_SHIFT; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2228 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2229 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2230 | if (!pte) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2231 | largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res); |
| 2232 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 2233 | first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2234 | if (!pte) |
| 2235 | return -ENOMEM; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2236 | /* It is large page*/ |
Woodhouse, David | 6491d4d | 2012-12-19 13:25:35 +0000 | [diff] [blame] | 2237 | if (largepage_lvl > 1) { |
Christian Zander | ba2374f | 2015-06-10 09:41:45 -0700 | [diff] [blame] | 2238 | unsigned long nr_superpages, end_pfn; |
| 2239 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2240 | pteval |= DMA_PTE_LARGE_PAGE; |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 2241 | lvl_pages = lvl_to_nr_pages(largepage_lvl); |
Christian Zander | ba2374f | 2015-06-10 09:41:45 -0700 | [diff] [blame] | 2242 | |
| 2243 | nr_superpages = sg_res / lvl_pages; |
| 2244 | end_pfn = iov_pfn + nr_superpages * lvl_pages - 1; |
| 2245 | |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 2246 | /* |
| 2247 | * Ensure that old small page tables are |
Christian Zander | ba2374f | 2015-06-10 09:41:45 -0700 | [diff] [blame] | 2248 | * removed to make room for superpage(s). |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 2249 | * We're adding new large pages, so make sure |
| 2250 | * we don't remove their parent tables. |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 2251 | */ |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 2252 | dma_pte_free_pagetable(domain, iov_pfn, end_pfn, |
| 2253 | largepage_lvl + 1); |
Woodhouse, David | 6491d4d | 2012-12-19 13:25:35 +0000 | [diff] [blame] | 2254 | } else { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2255 | pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; |
Woodhouse, David | 6491d4d | 2012-12-19 13:25:35 +0000 | [diff] [blame] | 2256 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2257 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2258 | } |
| 2259 | /* We don't need lock here, nobody else |
| 2260 | * touches the iova range |
| 2261 | */ |
David Woodhouse | 7766a3f | 2009-07-01 20:27:03 +0100 | [diff] [blame] | 2262 | tmp = cmpxchg64_local(&pte->val, 0ULL, pteval); |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 2263 | if (tmp) { |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 2264 | static int dumps = 5; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2265 | pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n", |
| 2266 | iov_pfn, tmp, (unsigned long long)pteval); |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 2267 | if (dumps) { |
| 2268 | dumps--; |
| 2269 | debug_dma_dump_mappings(NULL); |
| 2270 | } |
| 2271 | WARN_ON(1); |
| 2272 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2273 | |
| 2274 | lvl_pages = lvl_to_nr_pages(largepage_lvl); |
| 2275 | |
| 2276 | BUG_ON(nr_pages < lvl_pages); |
| 2277 | BUG_ON(sg_res < lvl_pages); |
| 2278 | |
| 2279 | nr_pages -= lvl_pages; |
| 2280 | iov_pfn += lvl_pages; |
| 2281 | phys_pfn += lvl_pages; |
| 2282 | pteval += lvl_pages * VTD_PAGE_SIZE; |
| 2283 | sg_res -= lvl_pages; |
| 2284 | |
| 2285 | /* If the next PTE would be the first in a new page, then we |
| 2286 | need to flush the cache on the entries we've just written. |
| 2287 | And then we'll need to recalculate 'pte', so clear it and |
| 2288 | let it get set again in the if (!pte) block above. |
| 2289 | |
| 2290 | If we're done (!nr_pages) we need to flush the cache too. |
| 2291 | |
| 2292 | Also if we've been setting superpages, we may need to |
| 2293 | recalculate 'pte' and switch back to smaller pages for the |
| 2294 | end of the mapping, if the trailing size is not enough to |
| 2295 | use another superpage (i.e. sg_res < lvl_pages). */ |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2296 | pte++; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2297 | if (!nr_pages || first_pte_in_page(pte) || |
| 2298 | (largepage_lvl > 1 && sg_res < lvl_pages)) { |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2299 | domain_flush_cache(domain, first_pte, |
| 2300 | (void *)pte - (void *)first_pte); |
| 2301 | pte = NULL; |
| 2302 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2303 | |
| 2304 | if (!sg_res && nr_pages) |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2305 | sg = sg_next(sg); |
| 2306 | } |
| 2307 | return 0; |
| 2308 | } |
| 2309 | |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2310 | static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2311 | struct scatterlist *sg, unsigned long phys_pfn, |
| 2312 | unsigned long nr_pages, int prot) |
| 2313 | { |
| 2314 | int ret; |
| 2315 | struct intel_iommu *iommu; |
| 2316 | |
| 2317 | /* Do the real mapping first */ |
| 2318 | ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot); |
| 2319 | if (ret) |
| 2320 | return ret; |
| 2321 | |
| 2322 | /* Notify about the new mapping */ |
| 2323 | if (domain_type_is_vm(domain)) { |
| 2324 | /* VM typed domains can have more than one IOMMUs */ |
| 2325 | int iommu_id; |
| 2326 | for_each_domain_iommu(iommu_id, domain) { |
| 2327 | iommu = g_iommus[iommu_id]; |
| 2328 | __mapping_notify_one(iommu, domain, iov_pfn, nr_pages); |
| 2329 | } |
| 2330 | } else { |
| 2331 | /* General domains only have one IOMMU */ |
| 2332 | iommu = domain_get_iommu(domain); |
| 2333 | __mapping_notify_one(iommu, domain, iov_pfn, nr_pages); |
| 2334 | } |
| 2335 | |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2339 | static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2340 | struct scatterlist *sg, unsigned long nr_pages, |
| 2341 | int prot) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2342 | { |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2343 | return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot); |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2344 | } |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2345 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2346 | static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2347 | unsigned long phys_pfn, unsigned long nr_pages, |
| 2348 | int prot) |
| 2349 | { |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2350 | 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] | 2351 | } |
| 2352 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 2353 | 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] | 2354 | { |
Filippo Sironi | 5082219 | 2017-08-31 10:58:11 +0200 | [diff] [blame] | 2355 | unsigned long flags; |
| 2356 | struct context_entry *context; |
| 2357 | u16 did_old; |
| 2358 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 2359 | if (!iommu) |
| 2360 | return; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2361 | |
Filippo Sironi | 5082219 | 2017-08-31 10:58:11 +0200 | [diff] [blame] | 2362 | spin_lock_irqsave(&iommu->lock, flags); |
| 2363 | context = iommu_context_addr(iommu, bus, devfn, 0); |
| 2364 | if (!context) { |
| 2365 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 2366 | return; |
| 2367 | } |
| 2368 | did_old = context_domain_id(context); |
| 2369 | context_clear_entry(context); |
| 2370 | __iommu_flush_cache(iommu, context, sizeof(*context)); |
| 2371 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 2372 | iommu->flush.flush_context(iommu, |
| 2373 | did_old, |
| 2374 | (((u16)bus) << 8) | devfn, |
| 2375 | DMA_CCMD_MASK_NOBIT, |
| 2376 | DMA_CCMD_DEVICE_INVL); |
| 2377 | iommu->flush.flush_iotlb(iommu, |
| 2378 | did_old, |
| 2379 | 0, |
| 2380 | 0, |
| 2381 | DMA_TLB_DSI_FLUSH); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
David Woodhouse | 109b9b0 | 2012-05-25 17:43:02 +0100 | [diff] [blame] | 2384 | static inline void unlink_domain_info(struct device_domain_info *info) |
| 2385 | { |
| 2386 | assert_spin_locked(&device_domain_lock); |
| 2387 | list_del(&info->link); |
| 2388 | list_del(&info->global); |
| 2389 | if (info->dev) |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 2390 | info->dev->archdata.iommu = NULL; |
David Woodhouse | 109b9b0 | 2012-05-25 17:43:02 +0100 | [diff] [blame] | 2391 | } |
| 2392 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2393 | static void domain_remove_dev_info(struct dmar_domain *domain) |
| 2394 | { |
Yijing Wang | 3a74ca0 | 2014-05-20 20:37:47 +0800 | [diff] [blame] | 2395 | struct device_domain_info *info, *tmp; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 2396 | unsigned long flags; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2397 | |
| 2398 | spin_lock_irqsave(&device_domain_lock, flags); |
Joerg Roedel | 76f45fe | 2015-07-21 18:25:11 +0200 | [diff] [blame] | 2399 | list_for_each_entry_safe(info, tmp, &domain->devices, link) |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 2400 | __dmar_remove_one_dev_info(info); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2401 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * find_domain |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 2406 | * Note: we use struct device->archdata.iommu stores the info |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2407 | */ |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 2408 | static struct dmar_domain *find_domain(struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2409 | { |
| 2410 | struct device_domain_info *info; |
| 2411 | |
| 2412 | /* No lock here, assumes no domain exit in normal case */ |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 2413 | info = dev->archdata.iommu; |
Peter Xu | b316d02 | 2017-05-22 18:28:51 +0800 | [diff] [blame] | 2414 | if (likely(info)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2415 | return info->domain; |
| 2416 | return NULL; |
| 2417 | } |
| 2418 | |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2419 | static inline struct device_domain_info * |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2420 | dmar_search_domain_by_dev_info(int segment, int bus, int devfn) |
| 2421 | { |
| 2422 | struct device_domain_info *info; |
| 2423 | |
| 2424 | list_for_each_entry(info, &device_domain_list, global) |
David Woodhouse | 41e80dca | 2014-03-09 13:55:54 -0700 | [diff] [blame] | 2425 | if (info->iommu->segment == segment && info->bus == bus && |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2426 | info->devfn == devfn) |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2427 | return info; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2428 | |
| 2429 | return NULL; |
| 2430 | } |
| 2431 | |
Joerg Roedel | 5db3156 | 2015-07-22 12:40:43 +0200 | [diff] [blame] | 2432 | static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu, |
| 2433 | int bus, int devfn, |
| 2434 | struct device *dev, |
| 2435 | struct dmar_domain *domain) |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2436 | { |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2437 | struct dmar_domain *found = NULL; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2438 | struct device_domain_info *info; |
| 2439 | unsigned long flags; |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 2440 | int ret; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2441 | |
| 2442 | info = alloc_devinfo_mem(); |
| 2443 | if (!info) |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2444 | return NULL; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2445 | |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2446 | info->bus = bus; |
| 2447 | info->devfn = devfn; |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2448 | info->ats_supported = info->pasid_supported = info->pri_supported = 0; |
| 2449 | info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0; |
| 2450 | info->ats_qdep = 0; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2451 | info->dev = dev; |
| 2452 | info->domain = domain; |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2453 | info->iommu = iommu; |
Lu Baolu | cc580e4 | 2018-07-14 15:46:59 +0800 | [diff] [blame] | 2454 | info->pasid_table = NULL; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2455 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2456 | if (dev && dev_is_pci(dev)) { |
| 2457 | struct pci_dev *pdev = to_pci_dev(info->dev); |
| 2458 | |
Gil Kupfer | cef7440 | 2018-05-10 17:56:02 -0500 | [diff] [blame] | 2459 | if (!pci_ats_disabled() && |
| 2460 | ecap_dev_iotlb_support(iommu->ecap) && |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2461 | pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) && |
| 2462 | dmar_find_matched_atsr_unit(pdev)) |
| 2463 | info->ats_supported = 1; |
| 2464 | |
| 2465 | if (ecs_enabled(iommu)) { |
| 2466 | if (pasid_enabled(iommu)) { |
| 2467 | int features = pci_pasid_features(pdev); |
| 2468 | if (features >= 0) |
| 2469 | info->pasid_supported = features | 1; |
| 2470 | } |
| 2471 | |
| 2472 | if (info->ats_supported && ecap_prs(iommu->ecap) && |
| 2473 | pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI)) |
| 2474 | info->pri_supported = 1; |
| 2475 | } |
| 2476 | } |
| 2477 | |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2478 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2479 | if (dev) |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 2480 | found = find_domain(dev); |
Joerg Roedel | f303e50 | 2015-07-23 18:37:13 +0200 | [diff] [blame] | 2481 | |
| 2482 | if (!found) { |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2483 | struct device_domain_info *info2; |
David Woodhouse | 41e80dca | 2014-03-09 13:55:54 -0700 | [diff] [blame] | 2484 | info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn); |
Joerg Roedel | f303e50 | 2015-07-23 18:37:13 +0200 | [diff] [blame] | 2485 | if (info2) { |
| 2486 | found = info2->domain; |
| 2487 | info2->dev = dev; |
| 2488 | } |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2489 | } |
Joerg Roedel | f303e50 | 2015-07-23 18:37:13 +0200 | [diff] [blame] | 2490 | |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2491 | if (found) { |
| 2492 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2493 | free_devinfo_mem(info); |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2494 | /* Caller must free the original domain */ |
| 2495 | return found; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2496 | } |
| 2497 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 2498 | spin_lock(&iommu->lock); |
| 2499 | ret = domain_attach_iommu(domain, iommu); |
| 2500 | spin_unlock(&iommu->lock); |
| 2501 | |
| 2502 | if (ret) { |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2503 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Sudip Mukherjee | 499f3aa | 2015-09-18 16:27:07 +0530 | [diff] [blame] | 2504 | free_devinfo_mem(info); |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2505 | return NULL; |
| 2506 | } |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2507 | |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2508 | list_add(&info->link, &domain->devices); |
| 2509 | list_add(&info->global, &device_domain_list); |
| 2510 | if (dev) |
| 2511 | dev->archdata.iommu = info; |
Lu Baolu | a7fc93f | 2018-07-14 15:47:00 +0800 | [diff] [blame] | 2512 | |
| 2513 | if (dev && dev_is_pci(dev) && info->pasid_supported) { |
| 2514 | ret = intel_pasid_alloc_table(dev); |
| 2515 | if (ret) { |
| 2516 | __dmar_remove_one_dev_info(info); |
| 2517 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2518 | return NULL; |
| 2519 | } |
| 2520 | } |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2521 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2522 | |
Joerg Roedel | cc4e257 | 2015-07-22 10:04:36 +0200 | [diff] [blame] | 2523 | if (dev && domain_context_mapping(domain, dev)) { |
| 2524 | pr_err("Domain context map for %s failed\n", dev_name(dev)); |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 2525 | dmar_remove_one_dev_info(domain, dev); |
Joerg Roedel | cc4e257 | 2015-07-22 10:04:36 +0200 | [diff] [blame] | 2526 | return NULL; |
| 2527 | } |
| 2528 | |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2529 | return domain; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2530 | } |
| 2531 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2532 | static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque) |
| 2533 | { |
| 2534 | *(u16 *)opaque = alias; |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2538 | static struct dmar_domain *find_or_alloc_domain(struct device *dev, int gaw) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2539 | { |
Joerg Roedel | cc4e257 | 2015-07-22 10:04:36 +0200 | [diff] [blame] | 2540 | struct device_domain_info *info = NULL; |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2541 | struct dmar_domain *domain = NULL; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2542 | struct intel_iommu *iommu; |
Lu Baolu | fcc35c6 | 2018-05-04 13:08:17 +0800 | [diff] [blame] | 2543 | u16 dma_alias; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2544 | unsigned long flags; |
Yijing Wang | aa4d066 | 2014-05-26 20:14:06 +0800 | [diff] [blame] | 2545 | u8 bus, devfn; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2546 | |
David Woodhouse | 146922e | 2014-03-09 15:44:17 -0700 | [diff] [blame] | 2547 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 2548 | if (!iommu) |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2549 | return NULL; |
| 2550 | |
| 2551 | if (dev_is_pci(dev)) { |
| 2552 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2553 | |
| 2554 | pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias); |
| 2555 | |
| 2556 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2557 | info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus), |
| 2558 | PCI_BUS_NUM(dma_alias), |
| 2559 | dma_alias & 0xff); |
| 2560 | if (info) { |
| 2561 | iommu = info->iommu; |
| 2562 | domain = info->domain; |
| 2563 | } |
| 2564 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2565 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2566 | /* DMA alias already has a domain, use it */ |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2567 | if (info) |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2568 | goto out; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2569 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2570 | |
David Woodhouse | 146922e | 2014-03-09 15:44:17 -0700 | [diff] [blame] | 2571 | /* Allocate and initialize new domain for the device */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 2572 | domain = alloc_domain(0); |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2573 | if (!domain) |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2574 | return NULL; |
Joerg Roedel | dc534b2 | 2015-07-22 12:44:02 +0200 | [diff] [blame] | 2575 | if (domain_init(domain, iommu, gaw)) { |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2576 | domain_exit(domain); |
| 2577 | return NULL; |
| 2578 | } |
| 2579 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2580 | out: |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2581 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2582 | return domain; |
| 2583 | } |
| 2584 | |
| 2585 | static struct dmar_domain *set_domain_for_dev(struct device *dev, |
| 2586 | struct dmar_domain *domain) |
| 2587 | { |
| 2588 | struct intel_iommu *iommu; |
| 2589 | struct dmar_domain *tmp; |
| 2590 | u16 req_id, dma_alias; |
| 2591 | u8 bus, devfn; |
| 2592 | |
| 2593 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 2594 | if (!iommu) |
| 2595 | return NULL; |
| 2596 | |
| 2597 | req_id = ((u16)bus << 8) | devfn; |
| 2598 | |
| 2599 | if (dev_is_pci(dev)) { |
| 2600 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2601 | |
| 2602 | pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias); |
| 2603 | |
| 2604 | /* register PCI DMA alias device */ |
| 2605 | if (req_id != dma_alias) { |
| 2606 | tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias), |
| 2607 | dma_alias & 0xff, NULL, domain); |
| 2608 | |
| 2609 | if (!tmp || tmp != domain) |
| 2610 | return tmp; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2611 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2612 | } |
| 2613 | |
Joerg Roedel | 5db3156 | 2015-07-22 12:40:43 +0200 | [diff] [blame] | 2614 | tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain); |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2615 | if (!tmp || tmp != domain) |
| 2616 | return tmp; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2617 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2618 | return domain; |
| 2619 | } |
| 2620 | |
| 2621 | static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw) |
| 2622 | { |
| 2623 | struct dmar_domain *domain, *tmp; |
| 2624 | |
| 2625 | domain = find_domain(dev); |
| 2626 | if (domain) |
| 2627 | goto out; |
| 2628 | |
| 2629 | domain = find_or_alloc_domain(dev, gaw); |
| 2630 | if (!domain) |
| 2631 | goto out; |
| 2632 | |
| 2633 | tmp = set_domain_for_dev(dev, domain); |
| 2634 | if (!tmp || domain != tmp) { |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2635 | domain_exit(domain); |
| 2636 | domain = tmp; |
| 2637 | } |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2638 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2639 | out: |
| 2640 | |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2641 | return domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2642 | } |
| 2643 | |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2644 | static int iommu_domain_identity_map(struct dmar_domain *domain, |
| 2645 | unsigned long long start, |
| 2646 | unsigned long long end) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2647 | { |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2648 | unsigned long first_vpfn = start >> VTD_PAGE_SHIFT; |
| 2649 | unsigned long last_vpfn = end >> VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2650 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2651 | if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn), |
| 2652 | dma_to_mm_pfn(last_vpfn))) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2653 | pr_err("Reserving iova failed\n"); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2654 | return -ENOMEM; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
Joerg Roedel | af1089c | 2015-07-21 15:45:19 +0200 | [diff] [blame] | 2657 | pr_debug("Mapping reserved region %llx-%llx\n", start, end); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2658 | /* |
| 2659 | * RMRR range might have overlap with physical memory range, |
| 2660 | * clear it first |
| 2661 | */ |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2662 | dma_pte_clear_range(domain, first_vpfn, last_vpfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2663 | |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2664 | return __domain_mapping(domain, first_vpfn, NULL, |
| 2665 | first_vpfn, last_vpfn - first_vpfn + 1, |
| 2666 | DMA_PTE_READ|DMA_PTE_WRITE); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2667 | } |
| 2668 | |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2669 | static int domain_prepare_identity_map(struct device *dev, |
| 2670 | struct dmar_domain *domain, |
| 2671 | unsigned long long start, |
| 2672 | unsigned long long end) |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2673 | { |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2674 | /* For _hardware_ passthrough, don't bother. But for software |
| 2675 | passthrough, we do it anyway -- it may indicate a memory |
| 2676 | range which is reserved in E820, so which didn't get set |
| 2677 | up to start with in si_domain */ |
| 2678 | if (domain == si_domain && hw_pass_through) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2679 | pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n", |
| 2680 | dev_name(dev), start, end); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2681 | return 0; |
| 2682 | } |
| 2683 | |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2684 | pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n", |
| 2685 | dev_name(dev), start, end); |
| 2686 | |
David Woodhouse | 5595b52 | 2009-12-02 09:21:55 +0000 | [diff] [blame] | 2687 | if (end < start) { |
| 2688 | WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n" |
| 2689 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 2690 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 2691 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 2692 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2693 | return -EIO; |
David Woodhouse | 5595b52 | 2009-12-02 09:21:55 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
David Woodhouse | 2ff729f | 2009-08-26 14:25:41 +0100 | [diff] [blame] | 2696 | if (end >> agaw_to_width(domain->agaw)) { |
| 2697 | WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n" |
| 2698 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 2699 | agaw_to_width(domain->agaw), |
| 2700 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 2701 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 2702 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2703 | return -EIO; |
David Woodhouse | 2ff729f | 2009-08-26 14:25:41 +0100 | [diff] [blame] | 2704 | } |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2705 | |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2706 | return iommu_domain_identity_map(domain, start, end); |
| 2707 | } |
| 2708 | |
| 2709 | static int iommu_prepare_identity_map(struct device *dev, |
| 2710 | unsigned long long start, |
| 2711 | unsigned long long end) |
| 2712 | { |
| 2713 | struct dmar_domain *domain; |
| 2714 | int ret; |
| 2715 | |
| 2716 | domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 2717 | if (!domain) |
| 2718 | return -ENOMEM; |
| 2719 | |
| 2720 | ret = domain_prepare_identity_map(dev, domain, start, end); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2721 | if (ret) |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2722 | domain_exit(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2723 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2724 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr, |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2728 | struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2729 | { |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2730 | if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2731 | return 0; |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2732 | return iommu_prepare_identity_map(dev, rmrr->base_address, |
| 2733 | rmrr->end_address); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2734 | } |
| 2735 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 2736 | #ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2737 | static inline void iommu_prepare_isa(void) |
| 2738 | { |
| 2739 | struct pci_dev *pdev; |
| 2740 | int ret; |
| 2741 | |
| 2742 | pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL); |
| 2743 | if (!pdev) |
| 2744 | return; |
| 2745 | |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2746 | pr_info("Prepare 0-16MiB unity mapping for LPC\n"); |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2747 | 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] | 2748 | |
| 2749 | if (ret) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2750 | 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] | 2751 | |
Yijing Wang | 9b27e82 | 2014-05-20 20:37:52 +0800 | [diff] [blame] | 2752 | pci_dev_put(pdev); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2753 | } |
| 2754 | #else |
| 2755 | static inline void iommu_prepare_isa(void) |
| 2756 | { |
| 2757 | return; |
| 2758 | } |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 2759 | #endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */ |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2760 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2761 | static int md_domain_init(struct dmar_domain *domain, int guest_width); |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2762 | |
Matt Kraai | 071e137 | 2009-08-23 22:30:22 -0700 | [diff] [blame] | 2763 | static int __init si_domain_init(int hw) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2764 | { |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2765 | int nid, ret = 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2766 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 2767 | si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2768 | if (!si_domain) |
| 2769 | return -EFAULT; |
| 2770 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2771 | if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
| 2772 | domain_exit(si_domain); |
| 2773 | return -EFAULT; |
| 2774 | } |
| 2775 | |
Joerg Roedel | 0dc7971 | 2015-07-21 15:40:06 +0200 | [diff] [blame] | 2776 | pr_debug("Identity mapping domain allocated\n"); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2777 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2778 | if (hw) |
| 2779 | return 0; |
| 2780 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2781 | for_each_online_node(nid) { |
Tejun Heo | d4bbf7e | 2011-11-28 09:46:22 -0800 | [diff] [blame] | 2782 | unsigned long start_pfn, end_pfn; |
| 2783 | int i; |
| 2784 | |
| 2785 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { |
| 2786 | ret = iommu_domain_identity_map(si_domain, |
| 2787 | PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); |
| 2788 | if (ret) |
| 2789 | return ret; |
| 2790 | } |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2793 | return 0; |
| 2794 | } |
| 2795 | |
David Woodhouse | 9b22662 | 2014-03-09 14:03:28 -0700 | [diff] [blame] | 2796 | static int identity_mapping(struct device *dev) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2797 | { |
| 2798 | struct device_domain_info *info; |
| 2799 | |
| 2800 | if (likely(!iommu_identity_mapping)) |
| 2801 | return 0; |
| 2802 | |
David Woodhouse | 9b22662 | 2014-03-09 14:03:28 -0700 | [diff] [blame] | 2803 | info = dev->archdata.iommu; |
Mike Travis | cb452a4 | 2011-05-28 13:15:03 -0500 | [diff] [blame] | 2804 | if (info && info != DUMMY_DEVICE_DOMAIN_INFO) |
| 2805 | return (info->domain == si_domain); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2806 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2807 | return 0; |
| 2808 | } |
| 2809 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2810 | 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] | 2811 | { |
David Woodhouse | 0ac7266 | 2014-03-09 13:19:22 -0700 | [diff] [blame] | 2812 | struct dmar_domain *ndomain; |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2813 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 2814 | u8 bus, devfn; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2815 | |
David Woodhouse | 5913c9b | 2014-03-09 16:27:31 -0700 | [diff] [blame] | 2816 | iommu = device_to_iommu(dev, &bus, &devfn); |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2817 | if (!iommu) |
| 2818 | return -ENODEV; |
| 2819 | |
Joerg Roedel | 5db3156 | 2015-07-22 12:40:43 +0200 | [diff] [blame] | 2820 | ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain); |
David Woodhouse | 0ac7266 | 2014-03-09 13:19:22 -0700 | [diff] [blame] | 2821 | if (ndomain != domain) |
| 2822 | return -EBUSY; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2823 | |
| 2824 | return 0; |
| 2825 | } |
| 2826 | |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2827 | static bool device_has_rmrr(struct device *dev) |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2828 | { |
| 2829 | struct dmar_rmrr_unit *rmrr; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 2830 | struct device *tmp; |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2831 | int i; |
| 2832 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 2833 | rcu_read_lock(); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2834 | for_each_rmrr_units(rmrr) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 2835 | /* |
| 2836 | * Return TRUE if this RMRR contains the device that |
| 2837 | * is passed in. |
| 2838 | */ |
| 2839 | for_each_active_dev_scope(rmrr->devices, |
| 2840 | rmrr->devices_cnt, i, tmp) |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2841 | if (tmp == dev) { |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 2842 | rcu_read_unlock(); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2843 | return true; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 2844 | } |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2845 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 2846 | rcu_read_unlock(); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2847 | return false; |
| 2848 | } |
| 2849 | |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2850 | /* |
| 2851 | * There are a couple cases where we need to restrict the functionality of |
| 2852 | * devices associated with RMRRs. The first is when evaluating a device for |
| 2853 | * identity mapping because problems exist when devices are moved in and out |
| 2854 | * of domains and their respective RMRR information is lost. This means that |
| 2855 | * a device with associated RMRRs will never be in a "passthrough" domain. |
| 2856 | * The second is use of the device through the IOMMU API. This interface |
| 2857 | * expects to have full control of the IOVA space for the device. We cannot |
| 2858 | * satisfy both the requirement that RMRR access is maintained and have an |
| 2859 | * unencumbered IOVA space. We also have no ability to quiesce the device's |
| 2860 | * use of the RMRR space or even inform the IOMMU API user of the restriction. |
| 2861 | * We therefore prevent devices associated with an RMRR from participating in |
| 2862 | * the IOMMU API, which eliminates them from device assignment. |
| 2863 | * |
| 2864 | * In both cases we assume that PCI USB devices with RMRRs have them largely |
| 2865 | * for historical reasons and that the RMRR space is not actively used post |
| 2866 | * boot. This exclusion may change if vendors begin to abuse it. |
David Woodhouse | 18436af | 2015-03-25 15:05:47 +0000 | [diff] [blame] | 2867 | * |
| 2868 | * The same exception is made for graphics devices, with the requirement that |
| 2869 | * any use of the RMRR regions will be torn down before assigning the device |
| 2870 | * to a guest. |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2871 | */ |
| 2872 | static bool device_is_rmrr_locked(struct device *dev) |
| 2873 | { |
| 2874 | if (!device_has_rmrr(dev)) |
| 2875 | return false; |
| 2876 | |
| 2877 | if (dev_is_pci(dev)) { |
| 2878 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2879 | |
David Woodhouse | 18436af | 2015-03-25 15:05:47 +0000 | [diff] [blame] | 2880 | if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev)) |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2881 | return false; |
| 2882 | } |
| 2883 | |
| 2884 | return true; |
| 2885 | } |
| 2886 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2887 | static int iommu_should_identity_map(struct device *dev, int startup) |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2888 | { |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2889 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2890 | if (dev_is_pci(dev)) { |
| 2891 | struct pci_dev *pdev = to_pci_dev(dev); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2892 | |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2893 | if (device_is_rmrr_locked(dev)) |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2894 | return 0; |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2895 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2896 | if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev)) |
| 2897 | return 1; |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2898 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2899 | if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev)) |
| 2900 | return 1; |
| 2901 | |
| 2902 | if (!(iommu_identity_mapping & IDENTMAP_ALL)) |
| 2903 | return 0; |
| 2904 | |
| 2905 | /* |
| 2906 | * We want to start off with all devices in the 1:1 domain, and |
| 2907 | * take them out later if we find they can't access all of memory. |
| 2908 | * |
| 2909 | * However, we can't do this for PCI devices behind bridges, |
| 2910 | * because all PCI devices behind the same bridge will end up |
| 2911 | * with the same source-id on their transactions. |
| 2912 | * |
| 2913 | * Practically speaking, we can't change things around for these |
| 2914 | * devices at run-time, because we can't be sure there'll be no |
| 2915 | * DMA transactions in flight for any of their siblings. |
| 2916 | * |
| 2917 | * So PCI devices (unless they're on the root bus) as well as |
| 2918 | * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of |
| 2919 | * the 1:1 domain, just in _case_ one of their siblings turns out |
| 2920 | * not to be able to map all of memory. |
| 2921 | */ |
| 2922 | if (!pci_is_pcie(pdev)) { |
| 2923 | if (!pci_is_root_bus(pdev->bus)) |
| 2924 | return 0; |
| 2925 | if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI) |
| 2926 | return 0; |
| 2927 | } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE) |
| 2928 | return 0; |
| 2929 | } else { |
| 2930 | if (device_has_rmrr(dev)) |
| 2931 | return 0; |
| 2932 | } |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2933 | |
David Woodhouse | 3dfc813 | 2009-07-04 19:11:08 +0100 | [diff] [blame] | 2934 | /* |
David Woodhouse | 3dfc813 | 2009-07-04 19:11:08 +0100 | [diff] [blame] | 2935 | * 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] | 2936 | * 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] | 2937 | * take them out of the 1:1 domain later. |
| 2938 | */ |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2939 | if (!startup) { |
| 2940 | /* |
| 2941 | * If the device's dma_mask is less than the system's memory |
| 2942 | * size then this is not a candidate for identity mapping. |
| 2943 | */ |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2944 | u64 dma_mask = *dev->dma_mask; |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2945 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2946 | if (dev->coherent_dma_mask && |
| 2947 | dev->coherent_dma_mask < dma_mask) |
| 2948 | dma_mask = dev->coherent_dma_mask; |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2949 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2950 | return dma_mask >= dma_get_required_mask(dev); |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2951 | } |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2952 | |
| 2953 | return 1; |
| 2954 | } |
| 2955 | |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2956 | static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw) |
| 2957 | { |
| 2958 | int ret; |
| 2959 | |
| 2960 | if (!iommu_should_identity_map(dev, 1)) |
| 2961 | return 0; |
| 2962 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2963 | ret = domain_add_dev_info(si_domain, dev); |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2964 | if (!ret) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2965 | pr_info("%s identity mapping for device %s\n", |
| 2966 | hw ? "Hardware" : "Software", dev_name(dev)); |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2967 | else if (ret == -ENODEV) |
| 2968 | /* device not associated with an iommu */ |
| 2969 | ret = 0; |
| 2970 | |
| 2971 | return ret; |
| 2972 | } |
| 2973 | |
| 2974 | |
Matt Kraai | 071e137 | 2009-08-23 22:30:22 -0700 | [diff] [blame] | 2975 | static int __init iommu_prepare_static_identity_mapping(int hw) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2976 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2977 | struct pci_dev *pdev = NULL; |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2978 | struct dmar_drhd_unit *drhd; |
| 2979 | struct intel_iommu *iommu; |
| 2980 | struct device *dev; |
| 2981 | int i; |
| 2982 | int ret = 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2983 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2984 | for_each_pci_dev(pdev) { |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2985 | ret = dev_prepare_static_identity_mapping(&pdev->dev, hw); |
| 2986 | if (ret) |
| 2987 | return ret; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2990 | for_each_active_iommu(iommu, drhd) |
| 2991 | for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) { |
| 2992 | struct acpi_device_physical_node *pn; |
| 2993 | struct acpi_device *adev; |
| 2994 | |
| 2995 | if (dev->bus != &acpi_bus_type) |
| 2996 | continue; |
Joerg Roedel | 86080cc | 2015-06-12 12:27:16 +0200 | [diff] [blame] | 2997 | |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2998 | adev= to_acpi_device(dev); |
| 2999 | mutex_lock(&adev->physical_node_lock); |
| 3000 | list_for_each_entry(pn, &adev->physical_node_list, node) { |
| 3001 | ret = dev_prepare_static_identity_mapping(pn->dev, hw); |
| 3002 | if (ret) |
| 3003 | break; |
| 3004 | } |
| 3005 | mutex_unlock(&adev->physical_node_lock); |
| 3006 | if (ret) |
| 3007 | return ret; |
| 3008 | } |
| 3009 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3010 | return 0; |
| 3011 | } |
| 3012 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3013 | static void intel_iommu_init_qi(struct intel_iommu *iommu) |
| 3014 | { |
| 3015 | /* |
| 3016 | * Start from the sane iommu hardware state. |
| 3017 | * If the queued invalidation is already initialized by us |
| 3018 | * (for example, while enabling interrupt-remapping) then |
| 3019 | * we got the things already rolling from a sane state. |
| 3020 | */ |
| 3021 | if (!iommu->qi) { |
| 3022 | /* |
| 3023 | * Clear any previous faults. |
| 3024 | */ |
| 3025 | dmar_fault(-1, iommu); |
| 3026 | /* |
| 3027 | * Disable queued invalidation if supported and already enabled |
| 3028 | * before OS handover. |
| 3029 | */ |
| 3030 | dmar_disable_qi(iommu); |
| 3031 | } |
| 3032 | |
| 3033 | if (dmar_enable_qi(iommu)) { |
| 3034 | /* |
| 3035 | * Queued Invalidate not enabled, use Register Based Invalidate |
| 3036 | */ |
| 3037 | iommu->flush.flush_context = __iommu_flush_context; |
| 3038 | iommu->flush.flush_iotlb = __iommu_flush_iotlb; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3039 | pr_info("%s: Using Register based invalidation\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3040 | iommu->name); |
| 3041 | } else { |
| 3042 | iommu->flush.flush_context = qi_flush_context; |
| 3043 | iommu->flush.flush_iotlb = qi_flush_iotlb; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3044 | pr_info("%s: Using Queued invalidation\n", iommu->name); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3045 | } |
| 3046 | } |
| 3047 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3048 | static int copy_context_table(struct intel_iommu *iommu, |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3049 | struct root_entry *old_re, |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3050 | struct context_entry **tbl, |
| 3051 | int bus, bool ext) |
| 3052 | { |
Joerg Roedel | dbcd861 | 2015-06-12 12:02:09 +0200 | [diff] [blame] | 3053 | int tbl_idx, pos = 0, idx, devfn, ret = 0, did; |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3054 | struct context_entry *new_ce = NULL, ce; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3055 | struct context_entry *old_ce = NULL; |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3056 | struct root_entry re; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3057 | phys_addr_t old_ce_phys; |
| 3058 | |
| 3059 | tbl_idx = ext ? bus * 2 : bus; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3060 | memcpy(&re, old_re, sizeof(re)); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3061 | |
| 3062 | for (devfn = 0; devfn < 256; devfn++) { |
| 3063 | /* First calculate the correct index */ |
| 3064 | idx = (ext ? devfn * 2 : devfn) % 256; |
| 3065 | |
| 3066 | if (idx == 0) { |
| 3067 | /* First save what we may have and clean up */ |
| 3068 | if (new_ce) { |
| 3069 | tbl[tbl_idx] = new_ce; |
| 3070 | __iommu_flush_cache(iommu, new_ce, |
| 3071 | VTD_PAGE_SIZE); |
| 3072 | pos = 1; |
| 3073 | } |
| 3074 | |
| 3075 | if (old_ce) |
| 3076 | iounmap(old_ce); |
| 3077 | |
| 3078 | ret = 0; |
| 3079 | if (devfn < 0x80) |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3080 | old_ce_phys = root_entry_lctp(&re); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3081 | else |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3082 | old_ce_phys = root_entry_uctp(&re); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3083 | |
| 3084 | if (!old_ce_phys) { |
| 3085 | if (ext && devfn == 0) { |
| 3086 | /* No LCTP, try UCTP */ |
| 3087 | devfn = 0x7f; |
| 3088 | continue; |
| 3089 | } else { |
| 3090 | goto out; |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | ret = -ENOMEM; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3095 | old_ce = memremap(old_ce_phys, PAGE_SIZE, |
| 3096 | MEMREMAP_WB); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3097 | if (!old_ce) |
| 3098 | goto out; |
| 3099 | |
| 3100 | new_ce = alloc_pgtable_page(iommu->node); |
| 3101 | if (!new_ce) |
| 3102 | goto out_unmap; |
| 3103 | |
| 3104 | ret = 0; |
| 3105 | } |
| 3106 | |
| 3107 | /* Now copy the context entry */ |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3108 | memcpy(&ce, old_ce + idx, sizeof(ce)); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3109 | |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 3110 | if (!__context_present(&ce)) |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3111 | continue; |
| 3112 | |
Joerg Roedel | dbcd861 | 2015-06-12 12:02:09 +0200 | [diff] [blame] | 3113 | did = context_domain_id(&ce); |
| 3114 | if (did >= 0 && did < cap_ndoms(iommu->cap)) |
| 3115 | set_bit(did, iommu->domain_ids); |
| 3116 | |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 3117 | /* |
| 3118 | * We need a marker for copied context entries. This |
| 3119 | * marker needs to work for the old format as well as |
| 3120 | * for extended context entries. |
| 3121 | * |
| 3122 | * Bit 67 of the context entry is used. In the old |
| 3123 | * format this bit is available to software, in the |
| 3124 | * extended format it is the PGE bit, but PGE is ignored |
| 3125 | * by HW if PASIDs are disabled (and thus still |
| 3126 | * available). |
| 3127 | * |
| 3128 | * So disable PASIDs first and then mark the entry |
| 3129 | * copied. This means that we don't copy PASID |
| 3130 | * translations from the old kernel, but this is fine as |
| 3131 | * faults there are not fatal. |
| 3132 | */ |
| 3133 | context_clear_pasid_enable(&ce); |
| 3134 | context_set_copied(&ce); |
| 3135 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3136 | new_ce[idx] = ce; |
| 3137 | } |
| 3138 | |
| 3139 | tbl[tbl_idx + pos] = new_ce; |
| 3140 | |
| 3141 | __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE); |
| 3142 | |
| 3143 | out_unmap: |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3144 | memunmap(old_ce); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3145 | |
| 3146 | out: |
| 3147 | return ret; |
| 3148 | } |
| 3149 | |
| 3150 | static int copy_translation_tables(struct intel_iommu *iommu) |
| 3151 | { |
| 3152 | struct context_entry **ctxt_tbls; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3153 | struct root_entry *old_rt; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3154 | phys_addr_t old_rt_phys; |
| 3155 | int ctxt_table_entries; |
| 3156 | unsigned long flags; |
| 3157 | u64 rtaddr_reg; |
| 3158 | int bus, ret; |
Joerg Roedel | c3361f2 | 2015-06-12 12:39:25 +0200 | [diff] [blame] | 3159 | bool new_ext, ext; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3160 | |
| 3161 | rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG); |
| 3162 | ext = !!(rtaddr_reg & DMA_RTADDR_RTT); |
Joerg Roedel | c3361f2 | 2015-06-12 12:39:25 +0200 | [diff] [blame] | 3163 | new_ext = !!ecap_ecs(iommu->ecap); |
| 3164 | |
| 3165 | /* |
| 3166 | * The RTT bit can only be changed when translation is disabled, |
| 3167 | * but disabling translation means to open a window for data |
| 3168 | * corruption. So bail out and don't copy anything if we would |
| 3169 | * have to change the bit. |
| 3170 | */ |
| 3171 | if (new_ext != ext) |
| 3172 | return -EINVAL; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3173 | |
| 3174 | old_rt_phys = rtaddr_reg & VTD_PAGE_MASK; |
| 3175 | if (!old_rt_phys) |
| 3176 | return -EINVAL; |
| 3177 | |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3178 | old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3179 | if (!old_rt) |
| 3180 | return -ENOMEM; |
| 3181 | |
| 3182 | /* This is too big for the stack - allocate it from slab */ |
| 3183 | ctxt_table_entries = ext ? 512 : 256; |
| 3184 | ret = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3185 | ctxt_tbls = kcalloc(ctxt_table_entries, sizeof(void *), GFP_KERNEL); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3186 | if (!ctxt_tbls) |
| 3187 | goto out_unmap; |
| 3188 | |
| 3189 | for (bus = 0; bus < 256; bus++) { |
| 3190 | ret = copy_context_table(iommu, &old_rt[bus], |
| 3191 | ctxt_tbls, bus, ext); |
| 3192 | if (ret) { |
| 3193 | pr_err("%s: Failed to copy context table for bus %d\n", |
| 3194 | iommu->name, bus); |
| 3195 | continue; |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | spin_lock_irqsave(&iommu->lock, flags); |
| 3200 | |
| 3201 | /* Context tables are copied, now write them to the root_entry table */ |
| 3202 | for (bus = 0; bus < 256; bus++) { |
| 3203 | int idx = ext ? bus * 2 : bus; |
| 3204 | u64 val; |
| 3205 | |
| 3206 | if (ctxt_tbls[idx]) { |
| 3207 | val = virt_to_phys(ctxt_tbls[idx]) | 1; |
| 3208 | iommu->root_entry[bus].lo = val; |
| 3209 | } |
| 3210 | |
| 3211 | if (!ext || !ctxt_tbls[idx + 1]) |
| 3212 | continue; |
| 3213 | |
| 3214 | val = virt_to_phys(ctxt_tbls[idx + 1]) | 1; |
| 3215 | iommu->root_entry[bus].hi = val; |
| 3216 | } |
| 3217 | |
| 3218 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 3219 | |
| 3220 | kfree(ctxt_tbls); |
| 3221 | |
| 3222 | __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE); |
| 3223 | |
| 3224 | ret = 0; |
| 3225 | |
| 3226 | out_unmap: |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3227 | memunmap(old_rt); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3228 | |
| 3229 | return ret; |
| 3230 | } |
| 3231 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3232 | static int __init init_dmars(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3233 | { |
| 3234 | struct dmar_drhd_unit *drhd; |
| 3235 | struct dmar_rmrr_unit *rmrr; |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3236 | bool copied_tables = false; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3237 | struct device *dev; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3238 | struct intel_iommu *iommu; |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3239 | int i, ret; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3240 | |
| 3241 | /* |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3242 | * for each drhd |
| 3243 | * allocate root |
| 3244 | * initialize and program root entry to not present |
| 3245 | * endfor |
| 3246 | */ |
| 3247 | for_each_drhd_unit(drhd) { |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3248 | /* |
| 3249 | * lock not needed as this is only incremented in the single |
| 3250 | * threaded kernel __init code path all other access are read |
| 3251 | * only |
| 3252 | */ |
Jiang Liu | 78d8e70 | 2014-11-09 22:47:57 +0800 | [diff] [blame] | 3253 | if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) { |
Mike Travis | 1b198bb | 2012-03-05 15:05:16 -0800 | [diff] [blame] | 3254 | g_num_of_iommus++; |
| 3255 | continue; |
| 3256 | } |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3257 | pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3258 | } |
| 3259 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3260 | /* Preallocate enough resources for IOMMU hot-addition */ |
| 3261 | if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) |
| 3262 | g_num_of_iommus = DMAR_UNITS_SUPPORTED; |
| 3263 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3264 | g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *), |
| 3265 | GFP_KERNEL); |
| 3266 | if (!g_iommus) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3267 | pr_err("Allocating global iommu array failed\n"); |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3268 | ret = -ENOMEM; |
| 3269 | goto error; |
| 3270 | } |
| 3271 | |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3272 | for_each_active_iommu(iommu, drhd) { |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 3273 | /* |
| 3274 | * Find the max pasid size of all IOMMU's in the system. |
| 3275 | * We need to ensure the system pasid table is no bigger |
| 3276 | * than the smallest supported. |
| 3277 | */ |
| 3278 | if (pasid_enabled(iommu)) { |
| 3279 | u32 temp = 2 << ecap_pss(iommu->ecap); |
| 3280 | |
| 3281 | intel_pasid_max_id = min_t(u32, temp, |
| 3282 | intel_pasid_max_id); |
| 3283 | } |
| 3284 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3285 | g_iommus[iommu->seq_id] = iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3286 | |
Joerg Roedel | b63d80d | 2015-06-12 09:14:34 +0200 | [diff] [blame] | 3287 | intel_iommu_init_qi(iommu); |
| 3288 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 3289 | ret = iommu_init_domains(iommu); |
| 3290 | if (ret) |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3291 | goto free_iommu; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 3292 | |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 3293 | init_translation_status(iommu); |
| 3294 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3295 | if (translation_pre_enabled(iommu) && !is_kdump_kernel()) { |
| 3296 | iommu_disable_translation(iommu); |
| 3297 | clear_translation_pre_enabled(iommu); |
| 3298 | pr_warn("Translation was enabled for %s but we are not in kdump mode\n", |
| 3299 | iommu->name); |
| 3300 | } |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 3301 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3302 | /* |
| 3303 | * TBD: |
| 3304 | * we could share the same root & context tables |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3305 | * among all IOMMU's. Need to Split it later. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3306 | */ |
| 3307 | ret = iommu_alloc_root_entry(iommu); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3308 | if (ret) |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3309 | goto free_iommu; |
Joerg Roedel | 5f0a7f7 | 2015-06-12 09:18:53 +0200 | [diff] [blame] | 3310 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3311 | if (translation_pre_enabled(iommu)) { |
| 3312 | pr_info("Translation already enabled - trying to copy translation structures\n"); |
| 3313 | |
| 3314 | ret = copy_translation_tables(iommu); |
| 3315 | if (ret) { |
| 3316 | /* |
| 3317 | * We found the IOMMU with translation |
| 3318 | * enabled - but failed to copy over the |
| 3319 | * old root-entry table. Try to proceed |
| 3320 | * by disabling translation now and |
| 3321 | * allocating a clean root-entry table. |
| 3322 | * This might cause DMAR faults, but |
| 3323 | * probably the dump will still succeed. |
| 3324 | */ |
| 3325 | pr_err("Failed to copy translation tables from previous kernel for %s\n", |
| 3326 | iommu->name); |
| 3327 | iommu_disable_translation(iommu); |
| 3328 | clear_translation_pre_enabled(iommu); |
| 3329 | } else { |
| 3330 | pr_info("Copied translation tables from previous kernel for %s\n", |
| 3331 | iommu->name); |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3332 | copied_tables = true; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3333 | } |
| 3334 | } |
| 3335 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3336 | if (!ecap_pass_through(iommu->ecap)) |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3337 | hw_pass_through = 0; |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 3338 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 3339 | if (pasid_enabled(iommu)) |
Lu Baolu | d973795 | 2018-07-14 15:47:02 +0800 | [diff] [blame^] | 3340 | intel_svm_init(iommu); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 3341 | #endif |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3342 | } |
| 3343 | |
Joerg Roedel | a4c34ff | 2016-06-17 11:29:48 +0200 | [diff] [blame] | 3344 | /* |
| 3345 | * Now that qi is enabled on all iommus, set the root entry and flush |
| 3346 | * caches. This is required on some Intel X58 chipsets, otherwise the |
| 3347 | * flush_context function will loop forever and the boot hangs. |
| 3348 | */ |
| 3349 | for_each_active_iommu(iommu, drhd) { |
| 3350 | iommu_flush_write_buffer(iommu); |
| 3351 | iommu_set_root_entry(iommu); |
| 3352 | iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); |
| 3353 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
| 3354 | } |
| 3355 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3356 | if (iommu_pass_through) |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3357 | iommu_identity_mapping |= IDENTMAP_ALL; |
| 3358 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 3359 | #ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3360 | iommu_identity_mapping |= IDENTMAP_GFX; |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3361 | #endif |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3362 | |
Ashok Raj | 21e722c | 2017-01-30 09:39:53 -0800 | [diff] [blame] | 3363 | check_tylersburg_isoch(); |
| 3364 | |
Joerg Roedel | 86080cc | 2015-06-12 12:27:16 +0200 | [diff] [blame] | 3365 | if (iommu_identity_mapping) { |
| 3366 | ret = si_domain_init(hw_pass_through); |
| 3367 | if (ret) |
| 3368 | goto free_iommu; |
| 3369 | } |
| 3370 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3371 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3372 | /* |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3373 | * If we copied translations from a previous kernel in the kdump |
| 3374 | * case, we can not assign the devices to domains now, as that |
| 3375 | * would eliminate the old mappings. So skip this part and defer |
| 3376 | * the assignment to device driver initialization time. |
| 3377 | */ |
| 3378 | if (copied_tables) |
| 3379 | goto domains_done; |
| 3380 | |
| 3381 | /* |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3382 | * 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] | 3383 | * identity mappings for rmrr, gfx, and isa and may fall back to static |
| 3384 | * identity mapping if iommu_identity_mapping is set. |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3385 | */ |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3386 | if (iommu_identity_mapping) { |
| 3387 | ret = iommu_prepare_static_identity_mapping(hw_pass_through); |
| 3388 | if (ret) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3389 | pr_crit("Failed to setup IOMMU pass-through\n"); |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3390 | goto free_iommu; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3391 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3392 | } |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3393 | /* |
| 3394 | * For each rmrr |
| 3395 | * for each dev attached to rmrr |
| 3396 | * do |
| 3397 | * locate drhd for dev, alloc domain for dev |
| 3398 | * allocate free domain |
| 3399 | * allocate page table entries for rmrr |
| 3400 | * if context not allocated for bus |
| 3401 | * allocate and init context |
| 3402 | * set present in root table for this bus |
| 3403 | * init context with domain, translation etc |
| 3404 | * endfor |
| 3405 | * endfor |
| 3406 | */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3407 | pr_info("Setting RMRR:\n"); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3408 | for_each_rmrr_units(rmrr) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3409 | /* some BIOS lists non-exist devices in DMAR table. */ |
| 3410 | for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3411 | i, dev) { |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 3412 | ret = iommu_prepare_rmrr_dev(rmrr, dev); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3413 | if (ret) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3414 | pr_err("Mapping reserved region failed\n"); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3415 | } |
| 3416 | } |
| 3417 | |
| 3418 | iommu_prepare_isa(); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 3419 | |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3420 | domains_done: |
| 3421 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3422 | /* |
| 3423 | * for each drhd |
| 3424 | * enable fault log |
| 3425 | * global invalidate context cache |
| 3426 | * global invalidate iotlb |
| 3427 | * enable translation |
| 3428 | */ |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3429 | for_each_iommu(iommu, drhd) { |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 3430 | if (drhd->ignored) { |
| 3431 | /* |
| 3432 | * we always have to disable PMRs or DMA may fail on |
| 3433 | * this device |
| 3434 | */ |
| 3435 | if (force_on) |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3436 | iommu_disable_protect_mem_regions(iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3437 | continue; |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 3438 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3439 | |
| 3440 | iommu_flush_write_buffer(iommu); |
| 3441 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 3442 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 3443 | if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) { |
| 3444 | ret = intel_svm_enable_prq(iommu); |
| 3445 | if (ret) |
| 3446 | goto free_iommu; |
| 3447 | } |
| 3448 | #endif |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 3449 | ret = dmar_set_interrupt(iommu); |
| 3450 | if (ret) |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3451 | goto free_iommu; |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 3452 | |
Joerg Roedel | 8939ddf | 2015-06-12 14:40:01 +0200 | [diff] [blame] | 3453 | if (!translation_pre_enabled(iommu)) |
| 3454 | iommu_enable_translation(iommu); |
| 3455 | |
David Woodhouse | b94996c | 2009-09-19 15:28:12 -0700 | [diff] [blame] | 3456 | iommu_disable_protect_mem_regions(iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | return 0; |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3460 | |
| 3461 | free_iommu: |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3462 | for_each_active_iommu(iommu, drhd) { |
| 3463 | disable_dmar_iommu(iommu); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 3464 | free_dmar_iommu(iommu); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3465 | } |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3466 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3467 | kfree(g_iommus); |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3468 | |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3469 | error: |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3470 | return ret; |
| 3471 | } |
| 3472 | |
David Woodhouse | 5a5e02a | 2009-07-04 09:35:44 +0100 | [diff] [blame] | 3473 | /* This takes a number of _MM_ pages, not VTD pages */ |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3474 | static unsigned long intel_alloc_iova(struct device *dev, |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 3475 | struct dmar_domain *domain, |
| 3476 | unsigned long nrpages, uint64_t dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3477 | { |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3478 | unsigned long iova_pfn = 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3479 | |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 3480 | /* Restrict dma_mask to the width that the iommu can handle */ |
| 3481 | dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask); |
Robin Murphy | 8f6429c | 2015-07-16 19:40:12 +0100 | [diff] [blame] | 3482 | /* Ensure we reserve the whole size-aligned region */ |
| 3483 | nrpages = __roundup_pow_of_two(nrpages); |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 3484 | |
| 3485 | if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3486 | /* |
| 3487 | * First try to allocate an io virtual address in |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 3488 | * DMA_BIT_MASK(32) and if that fails then try allocating |
Joe Perches | 3609801 | 2007-12-17 11:40:11 -0800 | [diff] [blame] | 3489 | * from higher range |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3490 | */ |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3491 | iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, |
Tomasz Nowicki | 538d5b3 | 2017-09-20 10:52:02 +0200 | [diff] [blame] | 3492 | IOVA_PFN(DMA_BIT_MASK(32)), false); |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3493 | if (iova_pfn) |
| 3494 | return iova_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3495 | } |
Tomasz Nowicki | 538d5b3 | 2017-09-20 10:52:02 +0200 | [diff] [blame] | 3496 | iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, |
| 3497 | IOVA_PFN(dma_mask), true); |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3498 | if (unlikely(!iova_pfn)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3499 | pr_err("Allocating %ld-page iova for %s failed", |
David Woodhouse | 207e359 | 2014-03-09 16:12:32 -0700 | [diff] [blame] | 3500 | nrpages, dev_name(dev)); |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3501 | return 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3502 | } |
| 3503 | |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3504 | return iova_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 3507 | struct dmar_domain *get_valid_domain_for_dev(struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3508 | { |
Joerg Roedel | 1c5ebba | 2016-08-25 13:52:51 +0200 | [diff] [blame] | 3509 | struct dmar_domain *domain, *tmp; |
Joerg Roedel | b1ce5b7 | 2015-09-23 19:16:01 +0200 | [diff] [blame] | 3510 | struct dmar_rmrr_unit *rmrr; |
Joerg Roedel | b1ce5b7 | 2015-09-23 19:16:01 +0200 | [diff] [blame] | 3511 | struct device *i_dev; |
| 3512 | int i, ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3513 | |
Joerg Roedel | 1c5ebba | 2016-08-25 13:52:51 +0200 | [diff] [blame] | 3514 | domain = find_domain(dev); |
| 3515 | if (domain) |
| 3516 | goto out; |
| 3517 | |
| 3518 | domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 3519 | if (!domain) |
| 3520 | goto out; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3521 | |
Joerg Roedel | b1ce5b7 | 2015-09-23 19:16:01 +0200 | [diff] [blame] | 3522 | /* We have a new domain - setup possible RMRRs for the device */ |
| 3523 | rcu_read_lock(); |
| 3524 | for_each_rmrr_units(rmrr) { |
| 3525 | for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, |
| 3526 | i, i_dev) { |
| 3527 | if (i_dev != dev) |
| 3528 | continue; |
| 3529 | |
| 3530 | ret = domain_prepare_identity_map(dev, domain, |
| 3531 | rmrr->base_address, |
| 3532 | rmrr->end_address); |
| 3533 | if (ret) |
| 3534 | dev_err(dev, "Mapping reserved region failed\n"); |
| 3535 | } |
| 3536 | } |
| 3537 | rcu_read_unlock(); |
| 3538 | |
Joerg Roedel | 1c5ebba | 2016-08-25 13:52:51 +0200 | [diff] [blame] | 3539 | tmp = set_domain_for_dev(dev, domain); |
| 3540 | if (!tmp || domain != tmp) { |
| 3541 | domain_exit(domain); |
| 3542 | domain = tmp; |
| 3543 | } |
| 3544 | |
| 3545 | out: |
| 3546 | |
| 3547 | if (!domain) |
| 3548 | pr_err("Allocating domain for %s failed\n", dev_name(dev)); |
| 3549 | |
| 3550 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3551 | return domain; |
| 3552 | } |
| 3553 | |
David Woodhouse | ecb509e | 2014-03-09 16:29:55 -0700 | [diff] [blame] | 3554 | /* 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] | 3555 | static int iommu_no_mapping(struct device *dev) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3556 | { |
| 3557 | int found; |
| 3558 | |
David Woodhouse | 3d89194 | 2014-03-06 15:59:26 +0000 | [diff] [blame] | 3559 | if (iommu_dummy(dev)) |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 3560 | return 1; |
| 3561 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3562 | if (!iommu_identity_mapping) |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 3563 | return 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3564 | |
David Woodhouse | 9b22662 | 2014-03-09 14:03:28 -0700 | [diff] [blame] | 3565 | found = identity_mapping(dev); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3566 | if (found) { |
David Woodhouse | ecb509e | 2014-03-09 16:29:55 -0700 | [diff] [blame] | 3567 | if (iommu_should_identity_map(dev, 0)) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3568 | return 1; |
| 3569 | else { |
| 3570 | /* |
| 3571 | * 32 bit DMA is removed from si_domain and fall back |
| 3572 | * to non-identity mapping. |
| 3573 | */ |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 3574 | dmar_remove_one_dev_info(si_domain, dev); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3575 | pr_info("32bit %s uses non-identity mapping\n", |
| 3576 | dev_name(dev)); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3577 | return 0; |
| 3578 | } |
| 3579 | } else { |
| 3580 | /* |
| 3581 | * In case of a detached 64 bit DMA device from vm, the device |
| 3582 | * is put into si_domain for identity mapping. |
| 3583 | */ |
David Woodhouse | ecb509e | 2014-03-09 16:29:55 -0700 | [diff] [blame] | 3584 | if (iommu_should_identity_map(dev, 0)) { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3585 | int ret; |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 3586 | ret = domain_add_dev_info(si_domain, dev); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3587 | if (!ret) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3588 | pr_info("64bit %s uses identity mapping\n", |
| 3589 | dev_name(dev)); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3590 | return 1; |
| 3591 | } |
| 3592 | } |
| 3593 | } |
| 3594 | |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 3595 | return 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3596 | } |
| 3597 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3598 | 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] | 3599 | size_t size, int dir, u64 dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3600 | { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3601 | struct dmar_domain *domain; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 3602 | phys_addr_t start_paddr; |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3603 | unsigned long iova_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3604 | int prot = 0; |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3605 | int ret; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3606 | struct intel_iommu *iommu; |
Fenghua Yu | 33041ec | 2009-08-04 15:10:59 -0700 | [diff] [blame] | 3607 | unsigned long paddr_pfn = paddr >> PAGE_SHIFT; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3608 | |
| 3609 | BUG_ON(dir == DMA_NONE); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3610 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3611 | if (iommu_no_mapping(dev)) |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3612 | return paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3613 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3614 | domain = get_valid_domain_for_dev(dev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3615 | if (!domain) |
| 3616 | return 0; |
| 3617 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3618 | iommu = domain_get_iommu(domain); |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 3619 | size = aligned_nrpages(paddr, size); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3620 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3621 | iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask); |
| 3622 | if (!iova_pfn) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3623 | goto error; |
| 3624 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3625 | /* |
| 3626 | * Check if DMAR supports zero-length reads on write only |
| 3627 | * mappings.. |
| 3628 | */ |
| 3629 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3630 | !cap_zlr(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3631 | prot |= DMA_PTE_READ; |
| 3632 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 3633 | prot |= DMA_PTE_WRITE; |
| 3634 | /* |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3635 | * 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] | 3636 | * page. Note: if two part of one page are separately mapped, we |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3637 | * 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] | 3638 | * is not a big problem |
| 3639 | */ |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3640 | ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn), |
Fenghua Yu | 33041ec | 2009-08-04 15:10:59 -0700 | [diff] [blame] | 3641 | mm_to_dma_pfn(paddr_pfn), size, prot); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3642 | if (ret) |
| 3643 | goto error; |
| 3644 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3645 | start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT; |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 3646 | start_paddr += paddr & ~PAGE_MASK; |
| 3647 | return start_paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3648 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3649 | error: |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3650 | if (iova_pfn) |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3651 | free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size)); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3652 | pr_err("Device %s request: %zx@%llx dir %d --- failed\n", |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3653 | dev_name(dev), size, (unsigned long long)paddr, dir); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3654 | return 0; |
| 3655 | } |
| 3656 | |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 3657 | static dma_addr_t intel_map_page(struct device *dev, struct page *page, |
| 3658 | unsigned long offset, size_t size, |
| 3659 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3660 | unsigned long attrs) |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 3661 | { |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 3662 | return __intel_map_single(dev, page_to_phys(page) + offset, size, |
David Woodhouse | 46333e3 | 2014-03-10 20:01:21 -0700 | [diff] [blame] | 3663 | dir, *dev->dma_mask); |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 3664 | } |
| 3665 | |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3666 | static void intel_unmap(struct device *dev, dma_addr_t dev_addr, size_t size) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3667 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3668 | struct dmar_domain *domain; |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3669 | unsigned long start_pfn, last_pfn; |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3670 | unsigned long nrpages; |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3671 | unsigned long iova_pfn; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3672 | struct intel_iommu *iommu; |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 3673 | struct page *freelist; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3674 | |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 3675 | if (iommu_no_mapping(dev)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3676 | return; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3677 | |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 3678 | domain = find_domain(dev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3679 | BUG_ON(!domain); |
| 3680 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3681 | iommu = domain_get_iommu(domain); |
| 3682 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3683 | iova_pfn = IOVA_PFN(dev_addr); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3684 | |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3685 | nrpages = aligned_nrpages(dev_addr, size); |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3686 | start_pfn = mm_to_dma_pfn(iova_pfn); |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3687 | last_pfn = start_pfn + nrpages - 1; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3688 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3689 | pr_debug("Device %s unmapping: pfn %lx-%lx\n", |
David Woodhouse | 207e359 | 2014-03-09 16:12:32 -0700 | [diff] [blame] | 3690 | dev_name(dev), start_pfn, last_pfn); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3691 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 3692 | freelist = domain_unmap(domain, start_pfn, last_pfn); |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3693 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3694 | if (intel_iommu_strict) { |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 3695 | iommu_flush_iotlb_psi(iommu, domain, start_pfn, |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3696 | nrpages, !freelist, 0); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3697 | /* free iova */ |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3698 | free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages)); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 3699 | dma_free_pagelist(freelist); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3700 | } else { |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3701 | queue_iova(&domain->iovad, iova_pfn, nrpages, |
| 3702 | (unsigned long)freelist); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3703 | /* |
| 3704 | * queue up the release of the unmap to save the 1/6th of the |
| 3705 | * cpu used up by the iotlb flush operation... |
| 3706 | */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3707 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3708 | } |
| 3709 | |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 3710 | static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr, |
| 3711 | size_t size, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3712 | unsigned long attrs) |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 3713 | { |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3714 | intel_unmap(dev, dev_addr, size); |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 3715 | } |
| 3716 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3717 | static void *intel_alloc_coherent(struct device *dev, size_t size, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 3718 | dma_addr_t *dma_handle, gfp_t flags, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3719 | unsigned long attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3720 | { |
Christoph Hellwig | d657c5c | 2018-03-19 11:38:20 +0100 | [diff] [blame] | 3721 | void *vaddr; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3722 | |
Christoph Hellwig | d657c5c | 2018-03-19 11:38:20 +0100 | [diff] [blame] | 3723 | vaddr = dma_direct_alloc(dev, size, dma_handle, flags, attrs); |
| 3724 | if (iommu_no_mapping(dev) || !vaddr) |
| 3725 | return vaddr; |
Alex Williamson | e8bb910 | 2009-11-04 15:59:34 -0700 | [diff] [blame] | 3726 | |
Christoph Hellwig | d657c5c | 2018-03-19 11:38:20 +0100 | [diff] [blame] | 3727 | *dma_handle = __intel_map_single(dev, virt_to_phys(vaddr), |
| 3728 | PAGE_ALIGN(size), DMA_BIDIRECTIONAL, |
| 3729 | dev->coherent_dma_mask); |
| 3730 | if (!*dma_handle) |
| 3731 | goto out_free_pages; |
| 3732 | return vaddr; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3733 | |
Christoph Hellwig | d657c5c | 2018-03-19 11:38:20 +0100 | [diff] [blame] | 3734 | out_free_pages: |
| 3735 | dma_direct_free(dev, size, vaddr, *dma_handle, attrs); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3736 | return NULL; |
| 3737 | } |
| 3738 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3739 | static void intel_free_coherent(struct device *dev, size_t size, void *vaddr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3740 | dma_addr_t dma_handle, unsigned long attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3741 | { |
Christoph Hellwig | d657c5c | 2018-03-19 11:38:20 +0100 | [diff] [blame] | 3742 | if (!iommu_no_mapping(dev)) |
| 3743 | intel_unmap(dev, dma_handle, PAGE_ALIGN(size)); |
| 3744 | dma_direct_free(dev, size, vaddr, dma_handle, attrs); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3745 | } |
| 3746 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3747 | static void intel_unmap_sg(struct device *dev, struct scatterlist *sglist, |
FUJITA Tomonori | d7ab5c4 | 2009-01-28 21:53:18 +0900 | [diff] [blame] | 3748 | int nelems, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3749 | unsigned long attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3750 | { |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3751 | dma_addr_t startaddr = sg_dma_address(sglist) & PAGE_MASK; |
| 3752 | unsigned long nrpages = 0; |
| 3753 | struct scatterlist *sg; |
| 3754 | int i; |
| 3755 | |
| 3756 | for_each_sg(sglist, sg, nelems, i) { |
| 3757 | nrpages += aligned_nrpages(sg_dma_address(sg), sg_dma_len(sg)); |
| 3758 | } |
| 3759 | |
| 3760 | intel_unmap(dev, startaddr, nrpages << VTD_PAGE_SHIFT); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3761 | } |
| 3762 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3763 | static int intel_nontranslate_map_sg(struct device *hddev, |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3764 | struct scatterlist *sglist, int nelems, int dir) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3765 | { |
| 3766 | int i; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3767 | struct scatterlist *sg; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3768 | |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3769 | for_each_sg(sglist, sg, nelems, i) { |
FUJITA Tomonori | 12d4d40 | 2007-10-23 09:32:25 +0200 | [diff] [blame] | 3770 | BUG_ON(!sg_page(sg)); |
Robin Murphy | 29a90b7 | 2017-09-28 15:14:01 +0100 | [diff] [blame] | 3771 | sg->dma_address = sg_phys(sg); |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3772 | sg->dma_length = sg->length; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3773 | } |
| 3774 | return nelems; |
| 3775 | } |
| 3776 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3777 | static int intel_map_sg(struct device *dev, struct scatterlist *sglist, int nelems, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3778 | enum dma_data_direction dir, unsigned long attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3779 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3780 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3781 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3782 | size_t size = 0; |
| 3783 | int prot = 0; |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3784 | unsigned long iova_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3785 | int ret; |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3786 | struct scatterlist *sg; |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 3787 | unsigned long start_vpfn; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3788 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3789 | |
| 3790 | BUG_ON(dir == DMA_NONE); |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3791 | if (iommu_no_mapping(dev)) |
| 3792 | return intel_nontranslate_map_sg(dev, sglist, nelems, dir); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3793 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3794 | domain = get_valid_domain_for_dev(dev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3795 | if (!domain) |
| 3796 | return 0; |
| 3797 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3798 | iommu = domain_get_iommu(domain); |
| 3799 | |
David Woodhouse | b536d24 | 2009-06-28 14:49:31 +0100 | [diff] [blame] | 3800 | for_each_sg(sglist, sg, nelems, i) |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 3801 | size += aligned_nrpages(sg->offset, sg->length); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3802 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3803 | iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3804 | *dev->dma_mask); |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3805 | if (!iova_pfn) { |
FUJITA Tomonori | c03ab37 | 2007-10-21 16:42:00 -0700 | [diff] [blame] | 3806 | sglist->dma_length = 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3807 | return 0; |
| 3808 | } |
| 3809 | |
| 3810 | /* |
| 3811 | * Check if DMAR supports zero-length reads on write only |
| 3812 | * mappings.. |
| 3813 | */ |
| 3814 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3815 | !cap_zlr(iommu->cap)) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3816 | prot |= DMA_PTE_READ; |
| 3817 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 3818 | prot |= DMA_PTE_WRITE; |
| 3819 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3820 | start_vpfn = mm_to_dma_pfn(iova_pfn); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 3821 | |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 3822 | ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 3823 | if (unlikely(ret)) { |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 3824 | dma_pte_free_pagetable(domain, start_vpfn, |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 3825 | start_vpfn + size - 1, |
| 3826 | agaw_to_level(domain->agaw) + 1); |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3827 | free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size)); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 3828 | return 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3829 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3830 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3831 | return nelems; |
| 3832 | } |
| 3833 | |
FUJITA Tomonori | dfb805e | 2009-01-28 21:53:17 +0900 | [diff] [blame] | 3834 | static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr) |
| 3835 | { |
| 3836 | return !dma_addr; |
| 3837 | } |
| 3838 | |
Arvind Yadav | 01e1932 | 2017-06-28 16:39:32 +0530 | [diff] [blame] | 3839 | const struct dma_map_ops intel_dma_ops = { |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 3840 | .alloc = intel_alloc_coherent, |
| 3841 | .free = intel_free_coherent, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3842 | .map_sg = intel_map_sg, |
| 3843 | .unmap_sg = intel_unmap_sg, |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 3844 | .map_page = intel_map_page, |
| 3845 | .unmap_page = intel_unmap_page, |
FUJITA Tomonori | dfb805e | 2009-01-28 21:53:17 +0900 | [diff] [blame] | 3846 | .mapping_error = intel_mapping_error, |
Christoph Hellwig | 5860acc | 2017-05-22 11:38:27 +0200 | [diff] [blame] | 3847 | #ifdef CONFIG_X86 |
Christoph Hellwig | fec777c | 2018-03-19 11:38:15 +0100 | [diff] [blame] | 3848 | .dma_supported = dma_direct_supported, |
Christoph Hellwig | 5860acc | 2017-05-22 11:38:27 +0200 | [diff] [blame] | 3849 | #endif |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3850 | }; |
| 3851 | |
| 3852 | static inline int iommu_domain_cache_init(void) |
| 3853 | { |
| 3854 | int ret = 0; |
| 3855 | |
| 3856 | iommu_domain_cache = kmem_cache_create("iommu_domain", |
| 3857 | sizeof(struct dmar_domain), |
| 3858 | 0, |
| 3859 | SLAB_HWCACHE_ALIGN, |
| 3860 | |
| 3861 | NULL); |
| 3862 | if (!iommu_domain_cache) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3863 | pr_err("Couldn't create iommu_domain cache\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3864 | ret = -ENOMEM; |
| 3865 | } |
| 3866 | |
| 3867 | return ret; |
| 3868 | } |
| 3869 | |
| 3870 | static inline int iommu_devinfo_cache_init(void) |
| 3871 | { |
| 3872 | int ret = 0; |
| 3873 | |
| 3874 | iommu_devinfo_cache = kmem_cache_create("iommu_devinfo", |
| 3875 | sizeof(struct device_domain_info), |
| 3876 | 0, |
| 3877 | SLAB_HWCACHE_ALIGN, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3878 | NULL); |
| 3879 | if (!iommu_devinfo_cache) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3880 | pr_err("Couldn't create devinfo cache\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3881 | ret = -ENOMEM; |
| 3882 | } |
| 3883 | |
| 3884 | return ret; |
| 3885 | } |
| 3886 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3887 | static int __init iommu_init_mempool(void) |
| 3888 | { |
| 3889 | int ret; |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 3890 | ret = iova_cache_get(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3891 | if (ret) |
| 3892 | return ret; |
| 3893 | |
| 3894 | ret = iommu_domain_cache_init(); |
| 3895 | if (ret) |
| 3896 | goto domain_error; |
| 3897 | |
| 3898 | ret = iommu_devinfo_cache_init(); |
| 3899 | if (!ret) |
| 3900 | return ret; |
| 3901 | |
| 3902 | kmem_cache_destroy(iommu_domain_cache); |
| 3903 | domain_error: |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 3904 | iova_cache_put(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3905 | |
| 3906 | return -ENOMEM; |
| 3907 | } |
| 3908 | |
| 3909 | static void __init iommu_exit_mempool(void) |
| 3910 | { |
| 3911 | kmem_cache_destroy(iommu_devinfo_cache); |
| 3912 | kmem_cache_destroy(iommu_domain_cache); |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 3913 | iova_cache_put(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3914 | } |
| 3915 | |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 3916 | static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev) |
| 3917 | { |
| 3918 | struct dmar_drhd_unit *drhd; |
| 3919 | u32 vtbar; |
| 3920 | int rc; |
| 3921 | |
| 3922 | /* We know that this device on this chipset has its own IOMMU. |
| 3923 | * If we find it under a different IOMMU, then the BIOS is lying |
| 3924 | * to us. Hope that the IOMMU for this device is actually |
| 3925 | * disabled, and it needs no translation... |
| 3926 | */ |
| 3927 | rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar); |
| 3928 | if (rc) { |
| 3929 | /* "can't" happen */ |
| 3930 | dev_info(&pdev->dev, "failed to run vt-d quirk\n"); |
| 3931 | return; |
| 3932 | } |
| 3933 | vtbar &= 0xffff0000; |
| 3934 | |
| 3935 | /* we know that the this iommu should be at offset 0xa000 from vtbar */ |
| 3936 | drhd = dmar_find_matched_drhd_unit(pdev); |
| 3937 | if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000, |
| 3938 | TAINT_FIRMWARE_WORKAROUND, |
| 3939 | "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n")) |
| 3940 | pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO; |
| 3941 | } |
| 3942 | DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu); |
| 3943 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3944 | static void __init init_no_remapping_devices(void) |
| 3945 | { |
| 3946 | struct dmar_drhd_unit *drhd; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3947 | struct device *dev; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3948 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3949 | |
| 3950 | for_each_drhd_unit(drhd) { |
| 3951 | if (!drhd->include_all) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3952 | for_each_active_dev_scope(drhd->devices, |
| 3953 | drhd->devices_cnt, i, dev) |
| 3954 | break; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3955 | /* ignore DMAR unit if no devices exist */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3956 | if (i == drhd->devices_cnt) |
| 3957 | drhd->ignored = 1; |
| 3958 | } |
| 3959 | } |
| 3960 | |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3961 | for_each_active_drhd_unit(drhd) { |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3962 | if (drhd->include_all) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3963 | continue; |
| 3964 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3965 | for_each_active_dev_scope(drhd->devices, |
| 3966 | drhd->devices_cnt, i, dev) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3967 | 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] | 3968 | break; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3969 | if (i < drhd->devices_cnt) |
| 3970 | continue; |
| 3971 | |
David Woodhouse | c0771df | 2011-10-14 20:59:46 +0100 | [diff] [blame] | 3972 | /* This IOMMU has *only* gfx devices. Either bypass it or |
| 3973 | set the gfx_mapped flag, as appropriate */ |
| 3974 | if (dmar_map_gfx) { |
| 3975 | intel_iommu_gfx_mapped = 1; |
| 3976 | } else { |
| 3977 | drhd->ignored = 1; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3978 | for_each_active_dev_scope(drhd->devices, |
| 3979 | drhd->devices_cnt, i, dev) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3980 | dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3981 | } |
| 3982 | } |
| 3983 | } |
| 3984 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3985 | #ifdef CONFIG_SUSPEND |
| 3986 | static int init_iommu_hw(void) |
| 3987 | { |
| 3988 | struct dmar_drhd_unit *drhd; |
| 3989 | struct intel_iommu *iommu = NULL; |
| 3990 | |
| 3991 | for_each_active_iommu(iommu, drhd) |
| 3992 | if (iommu->qi) |
| 3993 | dmar_reenable_qi(iommu); |
| 3994 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3995 | for_each_iommu(iommu, drhd) { |
| 3996 | if (drhd->ignored) { |
| 3997 | /* |
| 3998 | * we always have to disable PMRs or DMA may fail on |
| 3999 | * this device |
| 4000 | */ |
| 4001 | if (force_on) |
| 4002 | iommu_disable_protect_mem_regions(iommu); |
| 4003 | continue; |
| 4004 | } |
| 4005 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4006 | iommu_flush_write_buffer(iommu); |
| 4007 | |
| 4008 | iommu_set_root_entry(iommu); |
| 4009 | |
| 4010 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 4011 | DMA_CCMD_GLOBAL_INVL); |
Jiang Liu | 2a41cce | 2014-07-11 14:19:33 +0800 | [diff] [blame] | 4012 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
| 4013 | iommu_enable_translation(iommu); |
David Woodhouse | b94996c | 2009-09-19 15:28:12 -0700 | [diff] [blame] | 4014 | iommu_disable_protect_mem_regions(iommu); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | return 0; |
| 4018 | } |
| 4019 | |
| 4020 | static void iommu_flush_all(void) |
| 4021 | { |
| 4022 | struct dmar_drhd_unit *drhd; |
| 4023 | struct intel_iommu *iommu; |
| 4024 | |
| 4025 | for_each_active_iommu(iommu, drhd) { |
| 4026 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 4027 | DMA_CCMD_GLOBAL_INVL); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4028 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 4029 | DMA_TLB_GLOBAL_FLUSH); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4033 | static int iommu_suspend(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4034 | { |
| 4035 | struct dmar_drhd_unit *drhd; |
| 4036 | struct intel_iommu *iommu = NULL; |
| 4037 | unsigned long flag; |
| 4038 | |
| 4039 | for_each_active_iommu(iommu, drhd) { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 4040 | iommu->iommu_state = kcalloc(MAX_SR_DMAR_REGS, sizeof(u32), |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4041 | GFP_ATOMIC); |
| 4042 | if (!iommu->iommu_state) |
| 4043 | goto nomem; |
| 4044 | } |
| 4045 | |
| 4046 | iommu_flush_all(); |
| 4047 | |
| 4048 | for_each_active_iommu(iommu, drhd) { |
| 4049 | iommu_disable_translation(iommu); |
| 4050 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4051 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4052 | |
| 4053 | iommu->iommu_state[SR_DMAR_FECTL_REG] = |
| 4054 | readl(iommu->reg + DMAR_FECTL_REG); |
| 4055 | iommu->iommu_state[SR_DMAR_FEDATA_REG] = |
| 4056 | readl(iommu->reg + DMAR_FEDATA_REG); |
| 4057 | iommu->iommu_state[SR_DMAR_FEADDR_REG] = |
| 4058 | readl(iommu->reg + DMAR_FEADDR_REG); |
| 4059 | iommu->iommu_state[SR_DMAR_FEUADDR_REG] = |
| 4060 | readl(iommu->reg + DMAR_FEUADDR_REG); |
| 4061 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4062 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4063 | } |
| 4064 | return 0; |
| 4065 | |
| 4066 | nomem: |
| 4067 | for_each_active_iommu(iommu, drhd) |
| 4068 | kfree(iommu->iommu_state); |
| 4069 | |
| 4070 | return -ENOMEM; |
| 4071 | } |
| 4072 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4073 | static void iommu_resume(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4074 | { |
| 4075 | struct dmar_drhd_unit *drhd; |
| 4076 | struct intel_iommu *iommu = NULL; |
| 4077 | unsigned long flag; |
| 4078 | |
| 4079 | if (init_iommu_hw()) { |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 4080 | if (force_on) |
| 4081 | panic("tboot: IOMMU setup failed, DMAR can not resume!\n"); |
| 4082 | else |
| 4083 | WARN(1, "IOMMU setup failed, DMAR can not resume!\n"); |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4084 | return; |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | for_each_active_iommu(iommu, drhd) { |
| 4088 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4089 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4090 | |
| 4091 | writel(iommu->iommu_state[SR_DMAR_FECTL_REG], |
| 4092 | iommu->reg + DMAR_FECTL_REG); |
| 4093 | writel(iommu->iommu_state[SR_DMAR_FEDATA_REG], |
| 4094 | iommu->reg + DMAR_FEDATA_REG); |
| 4095 | writel(iommu->iommu_state[SR_DMAR_FEADDR_REG], |
| 4096 | iommu->reg + DMAR_FEADDR_REG); |
| 4097 | writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG], |
| 4098 | iommu->reg + DMAR_FEUADDR_REG); |
| 4099 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4100 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | for_each_active_iommu(iommu, drhd) |
| 4104 | kfree(iommu->iommu_state); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4107 | static struct syscore_ops iommu_syscore_ops = { |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4108 | .resume = iommu_resume, |
| 4109 | .suspend = iommu_suspend, |
| 4110 | }; |
| 4111 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4112 | static void __init init_iommu_pm_ops(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4113 | { |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4114 | register_syscore_ops(&iommu_syscore_ops); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | #else |
Rafael J. Wysocki | 99592ba | 2011-06-07 21:32:31 +0200 | [diff] [blame] | 4118 | static inline void init_iommu_pm_ops(void) {} |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4119 | #endif /* CONFIG_PM */ |
| 4120 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4121 | |
Jiang Liu | c2a0b53 | 2014-11-09 22:47:56 +0800 | [diff] [blame] | 4122 | 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] | 4123 | { |
| 4124 | struct acpi_dmar_reserved_memory *rmrr; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4125 | int prot = DMA_PTE_READ|DMA_PTE_WRITE; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4126 | struct dmar_rmrr_unit *rmrru; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4127 | size_t length; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4128 | |
| 4129 | rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL); |
| 4130 | if (!rmrru) |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4131 | goto out; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4132 | |
| 4133 | rmrru->hdr = header; |
| 4134 | rmrr = (struct acpi_dmar_reserved_memory *)header; |
| 4135 | rmrru->base_address = rmrr->base_address; |
| 4136 | rmrru->end_address = rmrr->end_address; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4137 | |
| 4138 | length = rmrr->end_address - rmrr->base_address + 1; |
| 4139 | rmrru->resv = iommu_alloc_resv_region(rmrr->base_address, length, prot, |
| 4140 | IOMMU_RESV_DIRECT); |
| 4141 | if (!rmrru->resv) |
| 4142 | goto free_rmrru; |
| 4143 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 4144 | rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1), |
| 4145 | ((void *)rmrr) + rmrr->header.length, |
| 4146 | &rmrru->devices_cnt); |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4147 | if (rmrru->devices_cnt && rmrru->devices == NULL) |
| 4148 | goto free_all; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4149 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 4150 | list_add(&rmrru->list, &dmar_rmrr_units); |
| 4151 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4152 | return 0; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4153 | free_all: |
| 4154 | kfree(rmrru->resv); |
| 4155 | free_rmrru: |
| 4156 | kfree(rmrru); |
| 4157 | out: |
| 4158 | return -ENOMEM; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4159 | } |
| 4160 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4161 | static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr) |
| 4162 | { |
| 4163 | struct dmar_atsr_unit *atsru; |
| 4164 | struct acpi_dmar_atsr *tmp; |
| 4165 | |
| 4166 | list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) { |
| 4167 | tmp = (struct acpi_dmar_atsr *)atsru->hdr; |
| 4168 | if (atsr->segment != tmp->segment) |
| 4169 | continue; |
| 4170 | if (atsr->header.length != tmp->header.length) |
| 4171 | continue; |
| 4172 | if (memcmp(atsr, tmp, atsr->header.length) == 0) |
| 4173 | return atsru; |
| 4174 | } |
| 4175 | |
| 4176 | return NULL; |
| 4177 | } |
| 4178 | |
| 4179 | int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg) |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4180 | { |
| 4181 | struct acpi_dmar_atsr *atsr; |
| 4182 | struct dmar_atsr_unit *atsru; |
| 4183 | |
Thomas Gleixner | b608fe3 | 2017-05-16 20:42:41 +0200 | [diff] [blame] | 4184 | if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled) |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4185 | return 0; |
| 4186 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4187 | atsr = container_of(hdr, struct acpi_dmar_atsr, header); |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4188 | atsru = dmar_find_atsr(atsr); |
| 4189 | if (atsru) |
| 4190 | return 0; |
| 4191 | |
| 4192 | atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4193 | if (!atsru) |
| 4194 | return -ENOMEM; |
| 4195 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4196 | /* |
| 4197 | * If memory is allocated from slab by ACPI _DSM method, we need to |
| 4198 | * copy the memory content because the memory buffer will be freed |
| 4199 | * on return. |
| 4200 | */ |
| 4201 | atsru->hdr = (void *)(atsru + 1); |
| 4202 | memcpy(atsru->hdr, hdr, hdr->length); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4203 | atsru->include_all = atsr->flags & 0x1; |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 4204 | if (!atsru->include_all) { |
| 4205 | atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1), |
| 4206 | (void *)atsr + atsr->header.length, |
| 4207 | &atsru->devices_cnt); |
| 4208 | if (atsru->devices_cnt && atsru->devices == NULL) { |
| 4209 | kfree(atsru); |
| 4210 | return -ENOMEM; |
| 4211 | } |
| 4212 | } |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4213 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 4214 | list_add_rcu(&atsru->list, &dmar_atsr_units); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4215 | |
| 4216 | return 0; |
| 4217 | } |
| 4218 | |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4219 | static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru) |
| 4220 | { |
| 4221 | dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt); |
| 4222 | kfree(atsru); |
| 4223 | } |
| 4224 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4225 | int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg) |
| 4226 | { |
| 4227 | struct acpi_dmar_atsr *atsr; |
| 4228 | struct dmar_atsr_unit *atsru; |
| 4229 | |
| 4230 | atsr = container_of(hdr, struct acpi_dmar_atsr, header); |
| 4231 | atsru = dmar_find_atsr(atsr); |
| 4232 | if (atsru) { |
| 4233 | list_del_rcu(&atsru->list); |
| 4234 | synchronize_rcu(); |
| 4235 | intel_iommu_free_atsr(atsru); |
| 4236 | } |
| 4237 | |
| 4238 | return 0; |
| 4239 | } |
| 4240 | |
| 4241 | int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg) |
| 4242 | { |
| 4243 | int i; |
| 4244 | struct device *dev; |
| 4245 | struct acpi_dmar_atsr *atsr; |
| 4246 | struct dmar_atsr_unit *atsru; |
| 4247 | |
| 4248 | atsr = container_of(hdr, struct acpi_dmar_atsr, header); |
| 4249 | atsru = dmar_find_atsr(atsr); |
| 4250 | if (!atsru) |
| 4251 | return 0; |
| 4252 | |
Linus Torvalds | 194dc87 | 2016-07-27 20:03:31 -0700 | [diff] [blame] | 4253 | if (!atsru->include_all && atsru->devices && atsru->devices_cnt) { |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4254 | for_each_active_dev_scope(atsru->devices, atsru->devices_cnt, |
| 4255 | i, dev) |
| 4256 | return -EBUSY; |
Linus Torvalds | 194dc87 | 2016-07-27 20:03:31 -0700 | [diff] [blame] | 4257 | } |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4258 | |
| 4259 | return 0; |
| 4260 | } |
| 4261 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4262 | static int intel_iommu_add(struct dmar_drhd_unit *dmaru) |
| 4263 | { |
| 4264 | int sp, ret = 0; |
| 4265 | struct intel_iommu *iommu = dmaru->iommu; |
| 4266 | |
| 4267 | if (g_iommus[iommu->seq_id]) |
| 4268 | return 0; |
| 4269 | |
| 4270 | if (hw_pass_through && !ecap_pass_through(iommu->ecap)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4271 | pr_warn("%s: Doesn't support hardware pass through.\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4272 | iommu->name); |
| 4273 | return -ENXIO; |
| 4274 | } |
| 4275 | if (!ecap_sc_support(iommu->ecap) && |
| 4276 | domain_update_iommu_snooping(iommu)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4277 | pr_warn("%s: Doesn't support snooping.\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4278 | iommu->name); |
| 4279 | return -ENXIO; |
| 4280 | } |
| 4281 | sp = domain_update_iommu_superpage(iommu) - 1; |
| 4282 | if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4283 | pr_warn("%s: Doesn't support large page.\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4284 | iommu->name); |
| 4285 | return -ENXIO; |
| 4286 | } |
| 4287 | |
| 4288 | /* |
| 4289 | * Disable translation if already enabled prior to OS handover. |
| 4290 | */ |
| 4291 | if (iommu->gcmd & DMA_GCMD_TE) |
| 4292 | iommu_disable_translation(iommu); |
| 4293 | |
| 4294 | g_iommus[iommu->seq_id] = iommu; |
| 4295 | ret = iommu_init_domains(iommu); |
| 4296 | if (ret == 0) |
| 4297 | ret = iommu_alloc_root_entry(iommu); |
| 4298 | if (ret) |
| 4299 | goto out; |
| 4300 | |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 4301 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 4302 | if (pasid_enabled(iommu)) |
Lu Baolu | d973795 | 2018-07-14 15:47:02 +0800 | [diff] [blame^] | 4303 | intel_svm_init(iommu); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 4304 | #endif |
| 4305 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4306 | if (dmaru->ignored) { |
| 4307 | /* |
| 4308 | * we always have to disable PMRs or DMA may fail on this device |
| 4309 | */ |
| 4310 | if (force_on) |
| 4311 | iommu_disable_protect_mem_regions(iommu); |
| 4312 | return 0; |
| 4313 | } |
| 4314 | |
| 4315 | intel_iommu_init_qi(iommu); |
| 4316 | iommu_flush_write_buffer(iommu); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 4317 | |
| 4318 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 4319 | if (pasid_enabled(iommu) && ecap_prs(iommu->ecap)) { |
| 4320 | ret = intel_svm_enable_prq(iommu); |
| 4321 | if (ret) |
| 4322 | goto disable_iommu; |
| 4323 | } |
| 4324 | #endif |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4325 | ret = dmar_set_interrupt(iommu); |
| 4326 | if (ret) |
| 4327 | goto disable_iommu; |
| 4328 | |
| 4329 | iommu_set_root_entry(iommu); |
| 4330 | iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); |
| 4331 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
| 4332 | iommu_enable_translation(iommu); |
| 4333 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4334 | iommu_disable_protect_mem_regions(iommu); |
| 4335 | return 0; |
| 4336 | |
| 4337 | disable_iommu: |
| 4338 | disable_dmar_iommu(iommu); |
| 4339 | out: |
| 4340 | free_dmar_iommu(iommu); |
| 4341 | return ret; |
| 4342 | } |
| 4343 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4344 | int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert) |
| 4345 | { |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4346 | int ret = 0; |
| 4347 | struct intel_iommu *iommu = dmaru->iommu; |
| 4348 | |
| 4349 | if (!intel_iommu_enabled) |
| 4350 | return 0; |
| 4351 | if (iommu == NULL) |
| 4352 | return -EINVAL; |
| 4353 | |
| 4354 | if (insert) { |
| 4355 | ret = intel_iommu_add(dmaru); |
| 4356 | } else { |
| 4357 | disable_dmar_iommu(iommu); |
| 4358 | free_dmar_iommu(iommu); |
| 4359 | } |
| 4360 | |
| 4361 | return ret; |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4362 | } |
| 4363 | |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4364 | static void intel_iommu_free_dmars(void) |
| 4365 | { |
| 4366 | struct dmar_rmrr_unit *rmrru, *rmrr_n; |
| 4367 | struct dmar_atsr_unit *atsru, *atsr_n; |
| 4368 | |
| 4369 | list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) { |
| 4370 | list_del(&rmrru->list); |
| 4371 | dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt); |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4372 | kfree(rmrru->resv); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4373 | kfree(rmrru); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4374 | } |
| 4375 | |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4376 | list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) { |
| 4377 | list_del(&atsru->list); |
| 4378 | intel_iommu_free_atsr(atsru); |
| 4379 | } |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | int dmar_find_matched_atsr_unit(struct pci_dev *dev) |
| 4383 | { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4384 | int i, ret = 1; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4385 | struct pci_bus *bus; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 4386 | struct pci_dev *bridge = NULL; |
| 4387 | struct device *tmp; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4388 | struct acpi_dmar_atsr *atsr; |
| 4389 | struct dmar_atsr_unit *atsru; |
| 4390 | |
| 4391 | dev = pci_physfn(dev); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4392 | for (bus = dev->bus; bus; bus = bus->parent) { |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4393 | bridge = bus->self; |
David Woodhouse | d14053b3 | 2015-10-15 09:28:06 +0100 | [diff] [blame] | 4394 | /* If it's an integrated device, allow ATS */ |
| 4395 | if (!bridge) |
| 4396 | return 1; |
| 4397 | /* Connected via non-PCIe: no ATS */ |
| 4398 | if (!pci_is_pcie(bridge) || |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 4399 | pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4400 | return 0; |
David Woodhouse | d14053b3 | 2015-10-15 09:28:06 +0100 | [diff] [blame] | 4401 | /* If we found the root port, look it up in the ATSR */ |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4402 | if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT) |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4403 | break; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4404 | } |
| 4405 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 4406 | rcu_read_lock(); |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4407 | list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) { |
| 4408 | atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header); |
| 4409 | if (atsr->segment != pci_domain_nr(dev->bus)) |
| 4410 | continue; |
| 4411 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4412 | for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 4413 | if (tmp == &bridge->dev) |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4414 | goto out; |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4415 | |
| 4416 | if (atsru->include_all) |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4417 | goto out; |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4418 | } |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4419 | ret = 0; |
| 4420 | out: |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 4421 | rcu_read_unlock(); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4422 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4423 | return ret; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4424 | } |
| 4425 | |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4426 | int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info) |
| 4427 | { |
| 4428 | int ret = 0; |
| 4429 | struct dmar_rmrr_unit *rmrru; |
| 4430 | struct dmar_atsr_unit *atsru; |
| 4431 | struct acpi_dmar_atsr *atsr; |
| 4432 | struct acpi_dmar_reserved_memory *rmrr; |
| 4433 | |
Thomas Gleixner | b608fe3 | 2017-05-16 20:42:41 +0200 | [diff] [blame] | 4434 | if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4435 | return 0; |
| 4436 | |
| 4437 | list_for_each_entry(rmrru, &dmar_rmrr_units, list) { |
| 4438 | rmrr = container_of(rmrru->hdr, |
| 4439 | struct acpi_dmar_reserved_memory, header); |
| 4440 | if (info->event == BUS_NOTIFY_ADD_DEVICE) { |
| 4441 | ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1), |
| 4442 | ((void *)rmrr) + rmrr->header.length, |
| 4443 | rmrr->segment, rmrru->devices, |
| 4444 | rmrru->devices_cnt); |
Jiang Liu | 27e2495 | 2014-06-20 15:08:06 +0800 | [diff] [blame] | 4445 | if(ret < 0) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4446 | return ret; |
Joerg Roedel | e6a8c9b | 2016-02-29 23:49:47 +0100 | [diff] [blame] | 4447 | } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) { |
Jiang Liu | 27e2495 | 2014-06-20 15:08:06 +0800 | [diff] [blame] | 4448 | dmar_remove_dev_scope(info, rmrr->segment, |
| 4449 | rmrru->devices, rmrru->devices_cnt); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | list_for_each_entry(atsru, &dmar_atsr_units, list) { |
| 4454 | if (atsru->include_all) |
| 4455 | continue; |
| 4456 | |
| 4457 | atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header); |
| 4458 | if (info->event == BUS_NOTIFY_ADD_DEVICE) { |
| 4459 | ret = dmar_insert_dev_scope(info, (void *)(atsr + 1), |
| 4460 | (void *)atsr + atsr->header.length, |
| 4461 | atsr->segment, atsru->devices, |
| 4462 | atsru->devices_cnt); |
| 4463 | if (ret > 0) |
| 4464 | break; |
| 4465 | else if(ret < 0) |
| 4466 | return ret; |
Joerg Roedel | e6a8c9b | 2016-02-29 23:49:47 +0100 | [diff] [blame] | 4467 | } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) { |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4468 | if (dmar_remove_dev_scope(info, atsr->segment, |
| 4469 | atsru->devices, atsru->devices_cnt)) |
| 4470 | break; |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | return 0; |
| 4475 | } |
| 4476 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4477 | /* |
| 4478 | * Here we only respond to action of unbound device from driver. |
| 4479 | * |
| 4480 | * Added device is not attached to its DMAR domain here yet. That will happen |
| 4481 | * when mapping the device to iova. |
| 4482 | */ |
| 4483 | static int device_notifier(struct notifier_block *nb, |
| 4484 | unsigned long action, void *data) |
| 4485 | { |
| 4486 | struct device *dev = data; |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4487 | struct dmar_domain *domain; |
| 4488 | |
David Woodhouse | 3d89194 | 2014-03-06 15:59:26 +0000 | [diff] [blame] | 4489 | if (iommu_dummy(dev)) |
David Woodhouse | 44cd613 | 2009-12-02 10:18:30 +0000 | [diff] [blame] | 4490 | return 0; |
| 4491 | |
Joerg Roedel | 1196c2f | 2014-09-30 13:02:03 +0200 | [diff] [blame] | 4492 | if (action != BUS_NOTIFY_REMOVED_DEVICE) |
Jiang Liu | 7e7dfab | 2014-02-19 14:07:23 +0800 | [diff] [blame] | 4493 | return 0; |
| 4494 | |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 4495 | domain = find_domain(dev); |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4496 | if (!domain) |
| 4497 | return 0; |
| 4498 | |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 4499 | dmar_remove_one_dev_info(domain, dev); |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 4500 | if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices)) |
Jiang Liu | 7e7dfab | 2014-02-19 14:07:23 +0800 | [diff] [blame] | 4501 | domain_exit(domain); |
Alex Williamson | a97590e | 2011-03-04 14:52:16 -0700 | [diff] [blame] | 4502 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4503 | return 0; |
| 4504 | } |
| 4505 | |
| 4506 | static struct notifier_block device_nb = { |
| 4507 | .notifier_call = device_notifier, |
| 4508 | }; |
| 4509 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4510 | static int intel_iommu_memory_notifier(struct notifier_block *nb, |
| 4511 | unsigned long val, void *v) |
| 4512 | { |
| 4513 | struct memory_notify *mhp = v; |
| 4514 | unsigned long long start, end; |
| 4515 | unsigned long start_vpfn, last_vpfn; |
| 4516 | |
| 4517 | switch (val) { |
| 4518 | case MEM_GOING_ONLINE: |
| 4519 | start = mhp->start_pfn << PAGE_SHIFT; |
| 4520 | end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1; |
| 4521 | if (iommu_domain_identity_map(si_domain, start, end)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4522 | pr_warn("Failed to build identity map for [%llx-%llx]\n", |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4523 | start, end); |
| 4524 | return NOTIFY_BAD; |
| 4525 | } |
| 4526 | break; |
| 4527 | |
| 4528 | case MEM_OFFLINE: |
| 4529 | case MEM_CANCEL_ONLINE: |
| 4530 | start_vpfn = mm_to_dma_pfn(mhp->start_pfn); |
| 4531 | last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1); |
| 4532 | while (start_vpfn <= last_vpfn) { |
| 4533 | struct iova *iova; |
| 4534 | struct dmar_drhd_unit *drhd; |
| 4535 | struct intel_iommu *iommu; |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4536 | struct page *freelist; |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4537 | |
| 4538 | iova = find_iova(&si_domain->iovad, start_vpfn); |
| 4539 | if (iova == NULL) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4540 | pr_debug("Failed get IOVA for PFN %lx\n", |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4541 | start_vpfn); |
| 4542 | break; |
| 4543 | } |
| 4544 | |
| 4545 | iova = split_and_remove_iova(&si_domain->iovad, iova, |
| 4546 | start_vpfn, last_vpfn); |
| 4547 | if (iova == NULL) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4548 | pr_warn("Failed to split IOVA PFN [%lx-%lx]\n", |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4549 | start_vpfn, last_vpfn); |
| 4550 | return NOTIFY_BAD; |
| 4551 | } |
| 4552 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4553 | freelist = domain_unmap(si_domain, iova->pfn_lo, |
| 4554 | iova->pfn_hi); |
| 4555 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4556 | rcu_read_lock(); |
| 4557 | for_each_active_iommu(iommu, drhd) |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 4558 | iommu_flush_iotlb_psi(iommu, si_domain, |
Jiang Liu | a156ef9 | 2014-07-11 14:19:36 +0800 | [diff] [blame] | 4559 | iova->pfn_lo, iova_size(iova), |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4560 | !freelist, 0); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4561 | rcu_read_unlock(); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4562 | dma_free_pagelist(freelist); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4563 | |
| 4564 | start_vpfn = iova->pfn_hi + 1; |
| 4565 | free_iova_mem(iova); |
| 4566 | } |
| 4567 | break; |
| 4568 | } |
| 4569 | |
| 4570 | return NOTIFY_OK; |
| 4571 | } |
| 4572 | |
| 4573 | static struct notifier_block intel_iommu_memory_nb = { |
| 4574 | .notifier_call = intel_iommu_memory_notifier, |
| 4575 | .priority = 0 |
| 4576 | }; |
| 4577 | |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 4578 | static void free_all_cpu_cached_iovas(unsigned int cpu) |
| 4579 | { |
| 4580 | int i; |
| 4581 | |
| 4582 | for (i = 0; i < g_num_of_iommus; i++) { |
| 4583 | struct intel_iommu *iommu = g_iommus[i]; |
| 4584 | struct dmar_domain *domain; |
Aaron Campbell | 0caa761 | 2016-07-02 21:23:24 -0300 | [diff] [blame] | 4585 | int did; |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 4586 | |
| 4587 | if (!iommu) |
| 4588 | continue; |
| 4589 | |
Jan Niehusmann | 3bd4f91 | 2016-06-06 14:20:11 +0200 | [diff] [blame] | 4590 | for (did = 0; did < cap_ndoms(iommu->cap); did++) { |
Aaron Campbell | 0caa761 | 2016-07-02 21:23:24 -0300 | [diff] [blame] | 4591 | domain = get_iommu_domain(iommu, (u16)did); |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 4592 | |
| 4593 | if (!domain) |
| 4594 | continue; |
| 4595 | free_cpu_cached_iovas(cpu, &domain->iovad); |
| 4596 | } |
| 4597 | } |
| 4598 | } |
| 4599 | |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4600 | static int intel_iommu_cpu_dead(unsigned int cpu) |
Omer Peleg | aa47324 | 2016-04-20 11:33:02 +0300 | [diff] [blame] | 4601 | { |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4602 | free_all_cpu_cached_iovas(cpu); |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4603 | return 0; |
Omer Peleg | aa47324 | 2016-04-20 11:33:02 +0300 | [diff] [blame] | 4604 | } |
| 4605 | |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4606 | static void intel_disable_iommus(void) |
| 4607 | { |
| 4608 | struct intel_iommu *iommu = NULL; |
| 4609 | struct dmar_drhd_unit *drhd; |
| 4610 | |
| 4611 | for_each_iommu(iommu, drhd) |
| 4612 | iommu_disable_translation(iommu); |
| 4613 | } |
| 4614 | |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4615 | static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev) |
| 4616 | { |
Joerg Roedel | 2926a2aa | 2017-08-14 17:19:26 +0200 | [diff] [blame] | 4617 | struct iommu_device *iommu_dev = dev_to_iommu_device(dev); |
| 4618 | |
| 4619 | return container_of(iommu_dev, struct intel_iommu, iommu); |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4620 | } |
| 4621 | |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4622 | static ssize_t intel_iommu_show_version(struct device *dev, |
| 4623 | struct device_attribute *attr, |
| 4624 | char *buf) |
| 4625 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4626 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4627 | u32 ver = readl(iommu->reg + DMAR_VER_REG); |
| 4628 | return sprintf(buf, "%d:%d\n", |
| 4629 | DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver)); |
| 4630 | } |
| 4631 | static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL); |
| 4632 | |
| 4633 | static ssize_t intel_iommu_show_address(struct device *dev, |
| 4634 | struct device_attribute *attr, |
| 4635 | char *buf) |
| 4636 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4637 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4638 | return sprintf(buf, "%llx\n", iommu->reg_phys); |
| 4639 | } |
| 4640 | static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL); |
| 4641 | |
| 4642 | static ssize_t intel_iommu_show_cap(struct device *dev, |
| 4643 | struct device_attribute *attr, |
| 4644 | char *buf) |
| 4645 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4646 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4647 | return sprintf(buf, "%llx\n", iommu->cap); |
| 4648 | } |
| 4649 | static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL); |
| 4650 | |
| 4651 | static ssize_t intel_iommu_show_ecap(struct device *dev, |
| 4652 | struct device_attribute *attr, |
| 4653 | char *buf) |
| 4654 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4655 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4656 | return sprintf(buf, "%llx\n", iommu->ecap); |
| 4657 | } |
| 4658 | static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL); |
| 4659 | |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4660 | static ssize_t intel_iommu_show_ndoms(struct device *dev, |
| 4661 | struct device_attribute *attr, |
| 4662 | char *buf) |
| 4663 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4664 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4665 | return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap)); |
| 4666 | } |
| 4667 | static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL); |
| 4668 | |
| 4669 | static ssize_t intel_iommu_show_ndoms_used(struct device *dev, |
| 4670 | struct device_attribute *attr, |
| 4671 | char *buf) |
| 4672 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4673 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4674 | return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids, |
| 4675 | cap_ndoms(iommu->cap))); |
| 4676 | } |
| 4677 | static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL); |
| 4678 | |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4679 | static struct attribute *intel_iommu_attrs[] = { |
| 4680 | &dev_attr_version.attr, |
| 4681 | &dev_attr_address.attr, |
| 4682 | &dev_attr_cap.attr, |
| 4683 | &dev_attr_ecap.attr, |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4684 | &dev_attr_domains_supported.attr, |
| 4685 | &dev_attr_domains_used.attr, |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4686 | NULL, |
| 4687 | }; |
| 4688 | |
| 4689 | static struct attribute_group intel_iommu_group = { |
| 4690 | .name = "intel-iommu", |
| 4691 | .attrs = intel_iommu_attrs, |
| 4692 | }; |
| 4693 | |
| 4694 | const struct attribute_group *intel_iommu_groups[] = { |
| 4695 | &intel_iommu_group, |
| 4696 | NULL, |
| 4697 | }; |
| 4698 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4699 | int __init intel_iommu_init(void) |
| 4700 | { |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4701 | int ret = -ENODEV; |
Takao Indoh | 3a93c84 | 2013-04-23 17:35:03 +0900 | [diff] [blame] | 4702 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 4703 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4704 | |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4705 | /* VT-d is required for a TXT/tboot launch, so enforce that */ |
| 4706 | force_on = tboot_force_iommu(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4707 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4708 | if (iommu_init_mempool()) { |
| 4709 | if (force_on) |
| 4710 | panic("tboot: Failed to initialize iommu memory\n"); |
| 4711 | return -ENOMEM; |
| 4712 | } |
| 4713 | |
| 4714 | down_write(&dmar_global_lock); |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4715 | if (dmar_table_init()) { |
| 4716 | if (force_on) |
| 4717 | panic("tboot: Failed to initialize DMAR table\n"); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4718 | goto out_free_dmar; |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 4721 | if (dmar_dev_scope_init() < 0) { |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4722 | if (force_on) |
| 4723 | panic("tboot: Failed to initialize DMAR device scope\n"); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4724 | goto out_free_dmar; |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4725 | } |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 4726 | |
Joerg Roedel | ec154bf | 2017-10-06 15:00:53 +0200 | [diff] [blame] | 4727 | up_write(&dmar_global_lock); |
| 4728 | |
| 4729 | /* |
| 4730 | * The bus notifier takes the dmar_global_lock, so lockdep will |
| 4731 | * complain later when we register it under the lock. |
| 4732 | */ |
| 4733 | dmar_register_bus_notifier(); |
| 4734 | |
| 4735 | down_write(&dmar_global_lock); |
| 4736 | |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4737 | if (no_iommu || dmar_disabled) { |
| 4738 | /* |
Shaohua Li | bfd20f1 | 2017-04-26 09:18:35 -0700 | [diff] [blame] | 4739 | * We exit the function here to ensure IOMMU's remapping and |
| 4740 | * mempool aren't setup, which means that the IOMMU's PMRs |
| 4741 | * won't be disabled via the call to init_dmars(). So disable |
| 4742 | * it explicitly here. The PMRs were setup by tboot prior to |
| 4743 | * calling SENTER, but the kernel is expected to reset/tear |
| 4744 | * down the PMRs. |
| 4745 | */ |
| 4746 | if (intel_iommu_tboot_noforce) { |
| 4747 | for_each_iommu(iommu, drhd) |
| 4748 | iommu_disable_protect_mem_regions(iommu); |
| 4749 | } |
| 4750 | |
| 4751 | /* |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4752 | * Make sure the IOMMUs are switched off, even when we |
| 4753 | * boot into a kexec kernel and the previous kernel left |
| 4754 | * them enabled |
| 4755 | */ |
| 4756 | intel_disable_iommus(); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4757 | goto out_free_dmar; |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4758 | } |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 4759 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4760 | if (list_empty(&dmar_rmrr_units)) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4761 | pr_info("No RMRR found\n"); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4762 | |
| 4763 | if (list_empty(&dmar_atsr_units)) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4764 | pr_info("No ATSR found\n"); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4765 | |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 4766 | if (dmar_init_reserved_ranges()) { |
| 4767 | if (force_on) |
| 4768 | panic("tboot: Failed to reserve iommu ranges\n"); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4769 | goto out_free_reserved_range; |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 4770 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4771 | |
| 4772 | init_no_remapping_devices(); |
| 4773 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 4774 | ret = init_dmars(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4775 | if (ret) { |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4776 | if (force_on) |
| 4777 | panic("tboot: Failed to initialize DMARs\n"); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4778 | pr_err("Initialization failed\n"); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4779 | goto out_free_reserved_range; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4780 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4781 | up_write(&dmar_global_lock); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4782 | pr_info("Intel(R) Virtualization Technology for Directed I/O\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4783 | |
Christoph Hellwig | 4fac807 | 2017-12-24 13:57:08 +0100 | [diff] [blame] | 4784 | #if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB) |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 4785 | swiotlb = 0; |
| 4786 | #endif |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 4787 | dma_ops = &intel_dma_ops; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 4788 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4789 | init_iommu_pm_ops(); |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 4790 | |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 4791 | for_each_active_iommu(iommu, drhd) { |
| 4792 | iommu_device_sysfs_add(&iommu->iommu, NULL, |
| 4793 | intel_iommu_groups, |
| 4794 | "%s", iommu->name); |
| 4795 | iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops); |
| 4796 | iommu_device_register(&iommu->iommu); |
| 4797 | } |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4798 | |
Joerg Roedel | 4236d97d | 2011-09-06 17:56:07 +0200 | [diff] [blame] | 4799 | bus_set_iommu(&pci_bus_type, &intel_iommu_ops); |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4800 | bus_register_notifier(&pci_bus_type, &device_nb); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4801 | if (si_domain && !hw_pass_through) |
| 4802 | register_memory_notifier(&intel_iommu_memory_nb); |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4803 | cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL, |
| 4804 | intel_iommu_cpu_dead); |
Eugeni Dodonov | 8bc1f85 | 2011-11-23 16:42:14 -0200 | [diff] [blame] | 4805 | intel_iommu_enabled = 1; |
| 4806 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4807 | return 0; |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4808 | |
| 4809 | out_free_reserved_range: |
| 4810 | put_iova_domain(&reserved_iova_list); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4811 | out_free_dmar: |
| 4812 | intel_iommu_free_dmars(); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4813 | up_write(&dmar_global_lock); |
| 4814 | iommu_exit_mempool(); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4815 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4816 | } |
Keshavamurthy, Anil S | e820482 | 2007-10-21 16:41:55 -0700 | [diff] [blame] | 4817 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4818 | 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] | 4819 | { |
| 4820 | struct intel_iommu *iommu = opaque; |
| 4821 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4822 | domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff); |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 4823 | return 0; |
| 4824 | } |
| 4825 | |
| 4826 | /* |
| 4827 | * NB - intel-iommu lacks any sort of reference counting for the users of |
| 4828 | * dependent devices. If multiple endpoints have intersecting dependent |
| 4829 | * devices, unbinding the driver from any one of them will possibly leave |
| 4830 | * the others unable to operate. |
| 4831 | */ |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4832 | static void domain_context_clear(struct intel_iommu *iommu, struct device *dev) |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 4833 | { |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 4834 | if (!iommu || !dev || !dev_is_pci(dev)) |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 4835 | return; |
| 4836 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4837 | 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] | 4838 | } |
| 4839 | |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4840 | static void __dmar_remove_one_dev_info(struct device_domain_info *info) |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4841 | { |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4842 | struct intel_iommu *iommu; |
| 4843 | unsigned long flags; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4844 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 4845 | assert_spin_locked(&device_domain_lock); |
| 4846 | |
Joerg Roedel | b608ac3 | 2015-07-21 18:19:08 +0200 | [diff] [blame] | 4847 | if (WARN_ON(!info)) |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4848 | return; |
| 4849 | |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4850 | iommu = info->iommu; |
| 4851 | |
| 4852 | if (info->dev) { |
| 4853 | iommu_disable_dev_iotlb(info); |
| 4854 | domain_context_clear(iommu, info->dev); |
Lu Baolu | a7fc93f | 2018-07-14 15:47:00 +0800 | [diff] [blame] | 4855 | intel_pasid_free_table(info->dev); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4856 | } |
| 4857 | |
Joerg Roedel | b608ac3 | 2015-07-21 18:19:08 +0200 | [diff] [blame] | 4858 | unlink_domain_info(info); |
Roland Dreier | 3e7abe2 | 2011-07-20 06:22:21 -0700 | [diff] [blame] | 4859 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4860 | spin_lock_irqsave(&iommu->lock, flags); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4861 | domain_detach_iommu(info->domain, iommu); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4862 | spin_unlock_irqrestore(&iommu->lock, flags); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4863 | |
| 4864 | free_devinfo_mem(info); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4865 | } |
| 4866 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 4867 | static void dmar_remove_one_dev_info(struct dmar_domain *domain, |
| 4868 | struct device *dev) |
| 4869 | { |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4870 | struct device_domain_info *info; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 4871 | unsigned long flags; |
| 4872 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4873 | spin_lock_irqsave(&device_domain_lock, flags); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4874 | info = dev->archdata.iommu; |
| 4875 | __dmar_remove_one_dev_info(info); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4876 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static int md_domain_init(struct dmar_domain *domain, int guest_width) |
| 4880 | { |
| 4881 | int adjust_width; |
| 4882 | |
Zhen Lei | aa3ac94 | 2017-09-21 16:52:45 +0100 | [diff] [blame] | 4883 | init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4884 | domain_reserve_special_ranges(domain); |
| 4885 | |
| 4886 | /* calculate AGAW */ |
| 4887 | domain->gaw = guest_width; |
| 4888 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 4889 | domain->agaw = width_to_agaw(adjust_width); |
| 4890 | |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4891 | domain->iommu_coherency = 0; |
Sheng Yang | c5b1525 | 2009-08-06 13:31:56 +0800 | [diff] [blame] | 4892 | domain->iommu_snooping = 0; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 4893 | domain->iommu_superpage = 0; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4894 | domain->max_addr = 0; |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4895 | |
| 4896 | /* always allocate the top pgd */ |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 4897 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4898 | if (!domain->pgd) |
| 4899 | return -ENOMEM; |
| 4900 | domain_flush_cache(domain, domain->pgd, PAGE_SIZE); |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4904 | static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4905 | { |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 4906 | struct dmar_domain *dmar_domain; |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4907 | struct iommu_domain *domain; |
| 4908 | |
| 4909 | if (type != IOMMU_DOMAIN_UNMANAGED) |
| 4910 | return NULL; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4911 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 4912 | dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE); |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 4913 | if (!dmar_domain) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4914 | pr_err("Can't allocate dmar_domain\n"); |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4915 | return NULL; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4916 | } |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 4917 | if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4918 | pr_err("Domain initialization failed\n"); |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 4919 | domain_exit(dmar_domain); |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4920 | return NULL; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4921 | } |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 4922 | domain_update_iommu_cap(dmar_domain); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4923 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4924 | domain = &dmar_domain->domain; |
Joerg Roedel | 8a0e715 | 2012-01-26 19:40:54 +0100 | [diff] [blame] | 4925 | domain->geometry.aperture_start = 0; |
| 4926 | domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw); |
| 4927 | domain->geometry.force_aperture = true; |
| 4928 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4929 | return domain; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4930 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4931 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4932 | static void intel_iommu_domain_free(struct iommu_domain *domain) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4933 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4934 | domain_exit(to_dmar_domain(domain)); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4935 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4936 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 4937 | static int intel_iommu_attach_device(struct iommu_domain *domain, |
| 4938 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4939 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4940 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4941 | struct intel_iommu *iommu; |
| 4942 | int addr_width; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 4943 | u8 bus, devfn; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4944 | |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 4945 | if (device_is_rmrr_locked(dev)) { |
| 4946 | dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n"); |
| 4947 | return -EPERM; |
| 4948 | } |
| 4949 | |
David Woodhouse | 7207d8f | 2014-03-09 16:31:06 -0700 | [diff] [blame] | 4950 | /* normally dev is not mapped */ |
| 4951 | if (unlikely(domain_context_mapped(dev))) { |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4952 | struct dmar_domain *old_domain; |
| 4953 | |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 4954 | old_domain = find_domain(dev); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4955 | if (old_domain) { |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4956 | rcu_read_lock(); |
Joerg Roedel | de7e888 | 2015-07-22 11:58:07 +0200 | [diff] [blame] | 4957 | dmar_remove_one_dev_info(old_domain, dev); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4958 | rcu_read_unlock(); |
Joerg Roedel | 62c2216 | 2014-12-09 12:56:45 +0100 | [diff] [blame] | 4959 | |
| 4960 | if (!domain_type_is_vm_or_si(old_domain) && |
| 4961 | list_empty(&old_domain->devices)) |
| 4962 | domain_exit(old_domain); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4963 | } |
| 4964 | } |
| 4965 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 4966 | iommu = device_to_iommu(dev, &bus, &devfn); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4967 | if (!iommu) |
| 4968 | return -ENODEV; |
| 4969 | |
| 4970 | /* check if this iommu agaw is sufficient for max mapped address */ |
| 4971 | addr_width = agaw_to_width(iommu->agaw); |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4972 | if (addr_width > cap_mgaw(iommu->cap)) |
| 4973 | addr_width = cap_mgaw(iommu->cap); |
| 4974 | |
| 4975 | if (dmar_domain->max_addr > (1LL << addr_width)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4976 | pr_err("%s: iommu width (%d) is not " |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4977 | "sufficient for the mapped address (%llx)\n", |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4978 | __func__, addr_width, dmar_domain->max_addr); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4979 | return -EFAULT; |
| 4980 | } |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4981 | dmar_domain->gaw = addr_width; |
| 4982 | |
| 4983 | /* |
| 4984 | * Knock out extra levels of page tables if necessary |
| 4985 | */ |
| 4986 | while (iommu->agaw < dmar_domain->agaw) { |
| 4987 | struct dma_pte *pte; |
| 4988 | |
| 4989 | pte = dmar_domain->pgd; |
| 4990 | if (dma_pte_present(pte)) { |
Sheng Yang | 25cbff1 | 2010-06-12 19:21:42 +0800 | [diff] [blame] | 4991 | dmar_domain->pgd = (struct dma_pte *) |
| 4992 | phys_to_virt(dma_pte_addr(pte)); |
Jan Kiszka | 7a66101 | 2010-11-02 08:05:51 +0100 | [diff] [blame] | 4993 | free_pgtable_page(pte); |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4994 | } |
| 4995 | dmar_domain->agaw--; |
| 4996 | } |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4997 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 4998 | return domain_add_dev_info(dmar_domain, dev); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4999 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5000 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 5001 | static void intel_iommu_detach_device(struct iommu_domain *domain, |
| 5002 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5003 | { |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 5004 | dmar_remove_one_dev_info(to_dmar_domain(domain), dev); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5005 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5006 | |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 5007 | static int intel_iommu_map(struct iommu_domain *domain, |
| 5008 | unsigned long iova, phys_addr_t hpa, |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 5009 | size_t size, int iommu_prot) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5010 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 5011 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5012 | u64 max_addr; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5013 | int prot = 0; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5014 | int ret; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5015 | |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5016 | if (iommu_prot & IOMMU_READ) |
| 5017 | prot |= DMA_PTE_READ; |
| 5018 | if (iommu_prot & IOMMU_WRITE) |
| 5019 | prot |= DMA_PTE_WRITE; |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 5020 | if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping) |
| 5021 | prot |= DMA_PTE_SNP; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5022 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 5023 | max_addr = iova + size; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5024 | if (dmar_domain->max_addr < max_addr) { |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5025 | u64 end; |
| 5026 | |
| 5027 | /* check if minimum agaw is sufficient for mapped address */ |
Tom Lyon | 8954da1 | 2010-05-17 08:19:52 +0100 | [diff] [blame] | 5028 | end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5029 | if (end < max_addr) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5030 | pr_err("%s: iommu width (%d) is not " |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5031 | "sufficient for the mapped address (%llx)\n", |
Tom Lyon | 8954da1 | 2010-05-17 08:19:52 +0100 | [diff] [blame] | 5032 | __func__, dmar_domain->gaw, max_addr); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5033 | return -EFAULT; |
| 5034 | } |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5035 | dmar_domain->max_addr = max_addr; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5036 | } |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 5037 | /* Round up size to next multiple of PAGE_SIZE, if it and |
| 5038 | the low bits of hpa would take us onto the next page */ |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 5039 | size = aligned_nrpages(hpa, size); |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 5040 | ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT, |
| 5041 | hpa >> VTD_PAGE_SHIFT, size, prot); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5042 | return ret; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5043 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5044 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 5045 | static size_t intel_iommu_unmap(struct iommu_domain *domain, |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5046 | unsigned long iova, size_t size) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5047 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 5048 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5049 | struct page *freelist = NULL; |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5050 | unsigned long start_pfn, last_pfn; |
| 5051 | unsigned int npages; |
Joerg Roedel | 42e8c18 | 2015-07-21 15:50:02 +0200 | [diff] [blame] | 5052 | int iommu_id, level = 0; |
Sheng Yang | 4b99d35 | 2009-07-08 11:52:52 +0100 | [diff] [blame] | 5053 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5054 | /* Cope with horrid API which requires us to unmap more than the |
| 5055 | size argument if it happens to be a large-page mapping. */ |
Joerg Roedel | dc02e46 | 2015-08-13 11:15:13 +0200 | [diff] [blame] | 5056 | BUG_ON(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level)); |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5057 | |
| 5058 | if (size < VTD_PAGE_SIZE << level_to_offset_bits(level)) |
| 5059 | size = VTD_PAGE_SIZE << level_to_offset_bits(level); |
| 5060 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5061 | start_pfn = iova >> VTD_PAGE_SHIFT; |
| 5062 | last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT; |
| 5063 | |
| 5064 | freelist = domain_unmap(dmar_domain, start_pfn, last_pfn); |
| 5065 | |
| 5066 | npages = last_pfn - start_pfn + 1; |
| 5067 | |
Shaokun Zhang | f746a02 | 2018-03-22 18:18:06 +0800 | [diff] [blame] | 5068 | for_each_domain_iommu(iommu_id, dmar_domain) |
Joerg Roedel | 42e8c18 | 2015-07-21 15:50:02 +0200 | [diff] [blame] | 5069 | iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain, |
| 5070 | start_pfn, npages, !freelist, 0); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5071 | |
| 5072 | dma_free_pagelist(freelist); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5073 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 5074 | if (dmar_domain->max_addr == iova + size) |
| 5075 | dmar_domain->max_addr = iova; |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 5076 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5077 | return size; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5078 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5079 | |
Joerg Roedel | d14d657 | 2008-12-03 15:06:57 +0100 | [diff] [blame] | 5080 | 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] | 5081 | dma_addr_t iova) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5082 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 5083 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5084 | struct dma_pte *pte; |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5085 | int level = 0; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5086 | u64 phys = 0; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5087 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5088 | 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] | 5089 | if (pte) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5090 | phys = dma_pte_addr(pte); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5091 | |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5092 | return phys; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5093 | } |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 5094 | |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5095 | static bool intel_iommu_capable(enum iommu_cap cap) |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5096 | { |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5097 | if (cap == IOMMU_CAP_CACHE_COHERENCY) |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5098 | return domain_update_iommu_snooping(NULL) == 1; |
Tom Lyon | 323f99c | 2010-07-02 16:56:14 -0400 | [diff] [blame] | 5099 | if (cap == IOMMU_CAP_INTR_REMAP) |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5100 | return irq_remapping_enabled == 1; |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5101 | |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5102 | return false; |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5103 | } |
| 5104 | |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5105 | static int intel_iommu_add_device(struct device *dev) |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5106 | { |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5107 | struct intel_iommu *iommu; |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5108 | struct iommu_group *group; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 5109 | u8 bus, devfn; |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5110 | |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5111 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 5112 | if (!iommu) |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5113 | return -ENODEV; |
| 5114 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 5115 | iommu_device_link(&iommu->iommu, dev); |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5116 | |
Alex Williamson | e17f9ff | 2014-07-03 09:51:37 -0600 | [diff] [blame] | 5117 | group = iommu_group_get_for_dev(dev); |
Alex Williamson | 783f157 | 2012-05-30 14:19:43 -0600 | [diff] [blame] | 5118 | |
Alex Williamson | e17f9ff | 2014-07-03 09:51:37 -0600 | [diff] [blame] | 5119 | if (IS_ERR(group)) |
| 5120 | return PTR_ERR(group); |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5121 | |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5122 | iommu_group_put(group); |
Alex Williamson | e17f9ff | 2014-07-03 09:51:37 -0600 | [diff] [blame] | 5123 | return 0; |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | static void intel_iommu_remove_device(struct device *dev) |
| 5127 | { |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5128 | struct intel_iommu *iommu; |
| 5129 | u8 bus, devfn; |
| 5130 | |
| 5131 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 5132 | if (!iommu) |
| 5133 | return; |
| 5134 | |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5135 | iommu_group_remove_device(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5136 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 5137 | iommu_device_unlink(&iommu->iommu, dev); |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5138 | } |
| 5139 | |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 5140 | static void intel_iommu_get_resv_regions(struct device *device, |
| 5141 | struct list_head *head) |
| 5142 | { |
| 5143 | struct iommu_resv_region *reg; |
| 5144 | struct dmar_rmrr_unit *rmrr; |
| 5145 | struct device *i_dev; |
| 5146 | int i; |
| 5147 | |
| 5148 | rcu_read_lock(); |
| 5149 | for_each_rmrr_units(rmrr) { |
| 5150 | for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, |
| 5151 | i, i_dev) { |
| 5152 | if (i_dev != device) |
| 5153 | continue; |
| 5154 | |
| 5155 | list_add_tail(&rmrr->resv->list, head); |
| 5156 | } |
| 5157 | } |
| 5158 | rcu_read_unlock(); |
| 5159 | |
| 5160 | reg = iommu_alloc_resv_region(IOAPIC_RANGE_START, |
| 5161 | IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1, |
Robin Murphy | 9d3a4de | 2017-03-16 17:00:16 +0000 | [diff] [blame] | 5162 | 0, IOMMU_RESV_MSI); |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 5163 | if (!reg) |
| 5164 | return; |
| 5165 | list_add_tail(®->list, head); |
| 5166 | } |
| 5167 | |
| 5168 | static void intel_iommu_put_resv_regions(struct device *dev, |
| 5169 | struct list_head *head) |
| 5170 | { |
| 5171 | struct iommu_resv_region *entry, *next; |
| 5172 | |
| 5173 | list_for_each_entry_safe(entry, next, head, list) { |
| 5174 | if (entry->type == IOMMU_RESV_RESERVED) |
| 5175 | kfree(entry); |
| 5176 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5177 | } |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 5178 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5179 | #ifdef CONFIG_INTEL_IOMMU_SVM |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5180 | #define MAX_NR_PASID_BITS (20) |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5181 | static inline unsigned long intel_iommu_get_pts(struct device *dev) |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5182 | { |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5183 | int pts, max_pasid; |
| 5184 | |
| 5185 | max_pasid = intel_pasid_get_dev_max_id(dev); |
| 5186 | pts = find_first_bit((unsigned long *)&max_pasid, MAX_NR_PASID_BITS); |
| 5187 | if (pts < 5) |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5188 | return 0; |
| 5189 | |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5190 | return pts - 5; |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5191 | } |
| 5192 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5193 | int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev) |
| 5194 | { |
| 5195 | struct device_domain_info *info; |
| 5196 | struct context_entry *context; |
| 5197 | struct dmar_domain *domain; |
| 5198 | unsigned long flags; |
| 5199 | u64 ctx_lo; |
| 5200 | int ret; |
| 5201 | |
| 5202 | domain = get_valid_domain_for_dev(sdev->dev); |
| 5203 | if (!domain) |
| 5204 | return -EINVAL; |
| 5205 | |
| 5206 | spin_lock_irqsave(&device_domain_lock, flags); |
| 5207 | spin_lock(&iommu->lock); |
| 5208 | |
| 5209 | ret = -EINVAL; |
| 5210 | info = sdev->dev->archdata.iommu; |
| 5211 | if (!info || !info->pasid_supported) |
| 5212 | goto out; |
| 5213 | |
| 5214 | context = iommu_context_addr(iommu, info->bus, info->devfn, 0); |
| 5215 | if (WARN_ON(!context)) |
| 5216 | goto out; |
| 5217 | |
| 5218 | ctx_lo = context[0].lo; |
| 5219 | |
| 5220 | sdev->did = domain->iommu_did[iommu->seq_id]; |
| 5221 | sdev->sid = PCI_DEVID(info->bus, info->devfn); |
| 5222 | |
| 5223 | if (!(ctx_lo & CONTEXT_PASIDE)) { |
Ashok Raj | 11b93eb | 2017-08-08 13:29:28 -0700 | [diff] [blame] | 5224 | if (iommu->pasid_state_table) |
| 5225 | context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table); |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5226 | context[1].lo = (u64)virt_to_phys(info->pasid_table->table) | |
| 5227 | intel_iommu_get_pts(sdev->dev); |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5228 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5229 | wmb(); |
| 5230 | /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both |
| 5231 | * extended to permit requests-with-PASID if the PASIDE bit |
| 5232 | * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH, |
| 5233 | * however, the PASIDE bit is ignored and requests-with-PASID |
| 5234 | * are unconditionally blocked. Which makes less sense. |
| 5235 | * So convert from CONTEXT_TT_PASS_THROUGH to one of the new |
| 5236 | * "guest mode" translation types depending on whether ATS |
| 5237 | * is available or not. Annoyingly, we can't use the new |
| 5238 | * modes *unless* PASIDE is set. */ |
| 5239 | if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) { |
| 5240 | ctx_lo &= ~CONTEXT_TT_MASK; |
| 5241 | if (info->ats_supported) |
| 5242 | ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2; |
| 5243 | else |
| 5244 | ctx_lo |= CONTEXT_TT_PT_PASID << 2; |
| 5245 | } |
| 5246 | ctx_lo |= CONTEXT_PASIDE; |
David Woodhouse | 907fea3 | 2015-10-13 14:11:13 +0100 | [diff] [blame] | 5247 | if (iommu->pasid_state_table) |
| 5248 | ctx_lo |= CONTEXT_DINVE; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 5249 | if (info->pri_supported) |
| 5250 | ctx_lo |= CONTEXT_PRS; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5251 | context[0].lo = ctx_lo; |
| 5252 | wmb(); |
| 5253 | iommu->flush.flush_context(iommu, sdev->did, sdev->sid, |
| 5254 | DMA_CCMD_MASK_NOBIT, |
| 5255 | DMA_CCMD_DEVICE_INVL); |
| 5256 | } |
| 5257 | |
| 5258 | /* Enable PASID support in the device, if it wasn't already */ |
| 5259 | if (!info->pasid_enabled) |
| 5260 | iommu_enable_dev_iotlb(info); |
| 5261 | |
| 5262 | if (info->ats_enabled) { |
| 5263 | sdev->dev_iotlb = 1; |
| 5264 | sdev->qdep = info->ats_qdep; |
| 5265 | if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) |
| 5266 | sdev->qdep = 0; |
| 5267 | } |
| 5268 | ret = 0; |
| 5269 | |
| 5270 | out: |
| 5271 | spin_unlock(&iommu->lock); |
| 5272 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 5273 | |
| 5274 | return ret; |
| 5275 | } |
| 5276 | |
| 5277 | struct intel_iommu *intel_svm_device_to_iommu(struct device *dev) |
| 5278 | { |
| 5279 | struct intel_iommu *iommu; |
| 5280 | u8 bus, devfn; |
| 5281 | |
| 5282 | if (iommu_dummy(dev)) { |
| 5283 | dev_warn(dev, |
| 5284 | "No IOMMU translation for device; cannot enable SVM\n"); |
| 5285 | return NULL; |
| 5286 | } |
| 5287 | |
| 5288 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 5289 | if ((!iommu)) { |
Sudeep Dutt | b9997e3 | 2015-10-18 20:54:37 -0700 | [diff] [blame] | 5290 | dev_err(dev, "No IOMMU for device; cannot enable SVM\n"); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5291 | return NULL; |
| 5292 | } |
| 5293 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5294 | return iommu; |
| 5295 | } |
| 5296 | #endif /* CONFIG_INTEL_IOMMU_SVM */ |
| 5297 | |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 5298 | const struct iommu_ops intel_iommu_ops = { |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 5299 | .capable = intel_iommu_capable, |
| 5300 | .domain_alloc = intel_iommu_domain_alloc, |
| 5301 | .domain_free = intel_iommu_domain_free, |
| 5302 | .attach_dev = intel_iommu_attach_device, |
| 5303 | .detach_dev = intel_iommu_detach_device, |
| 5304 | .map = intel_iommu_map, |
| 5305 | .unmap = intel_iommu_unmap, |
| 5306 | .map_sg = default_iommu_map_sg, |
| 5307 | .iova_to_phys = intel_iommu_iova_to_phys, |
| 5308 | .add_device = intel_iommu_add_device, |
| 5309 | .remove_device = intel_iommu_remove_device, |
| 5310 | .get_resv_regions = intel_iommu_get_resv_regions, |
| 5311 | .put_resv_regions = intel_iommu_put_resv_regions, |
| 5312 | .device_group = pci_device_group, |
| 5313 | .pgsize_bitmap = INTEL_IOMMU_PGSIZES, |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 5314 | }; |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5315 | |
Daniel Vetter | 9452618 | 2013-01-20 23:50:13 +0100 | [diff] [blame] | 5316 | static void quirk_iommu_g4x_gfx(struct pci_dev *dev) |
| 5317 | { |
| 5318 | /* G4x/GM45 integrated gfx dmar support is totally busted. */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5319 | pr_info("Disabling IOMMU for graphics on this chipset\n"); |
Daniel Vetter | 9452618 | 2013-01-20 23:50:13 +0100 | [diff] [blame] | 5320 | dmar_map_gfx = 0; |
| 5321 | } |
| 5322 | |
| 5323 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx); |
| 5324 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx); |
| 5325 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx); |
| 5326 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx); |
| 5327 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx); |
| 5328 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx); |
| 5329 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx); |
| 5330 | |
Greg Kroah-Hartman | d34d651 | 2012-12-21 15:05:21 -0800 | [diff] [blame] | 5331 | static void quirk_iommu_rwbf(struct pci_dev *dev) |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5332 | { |
| 5333 | /* |
| 5334 | * Mobile 4 Series Chipset neglects to set RWBF capability, |
Daniel Vetter | 210561f | 2013-01-21 19:48:59 +0100 | [diff] [blame] | 5335 | * but needs it. Same seems to hold for the desktop versions. |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5336 | */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5337 | pr_info("Forcing write-buffer flush capability\n"); |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5338 | rwbf_quirk = 1; |
| 5339 | } |
| 5340 | |
| 5341 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf); |
Daniel Vetter | 210561f | 2013-01-21 19:48:59 +0100 | [diff] [blame] | 5342 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf); |
| 5343 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf); |
| 5344 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf); |
| 5345 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf); |
| 5346 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf); |
| 5347 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf); |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 5348 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 5349 | #define GGC 0x52 |
| 5350 | #define GGC_MEMORY_SIZE_MASK (0xf << 8) |
| 5351 | #define GGC_MEMORY_SIZE_NONE (0x0 << 8) |
| 5352 | #define GGC_MEMORY_SIZE_1M (0x1 << 8) |
| 5353 | #define GGC_MEMORY_SIZE_2M (0x3 << 8) |
| 5354 | #define GGC_MEMORY_VT_ENABLED (0x8 << 8) |
| 5355 | #define GGC_MEMORY_SIZE_2M_VT (0x9 << 8) |
| 5356 | #define GGC_MEMORY_SIZE_3M_VT (0xa << 8) |
| 5357 | #define GGC_MEMORY_SIZE_4M_VT (0xb << 8) |
| 5358 | |
Greg Kroah-Hartman | d34d651 | 2012-12-21 15:05:21 -0800 | [diff] [blame] | 5359 | static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev) |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 5360 | { |
| 5361 | unsigned short ggc; |
| 5362 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 5363 | if (pci_read_config_word(dev, GGC, &ggc)) |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 5364 | return; |
| 5365 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 5366 | if (!(ggc & GGC_MEMORY_VT_ENABLED)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5367 | 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] | 5368 | dmar_map_gfx = 0; |
David Woodhouse | 6fbcfb3 | 2011-09-25 19:11:14 -0700 | [diff] [blame] | 5369 | } else if (dmar_map_gfx) { |
| 5370 | /* we have to ensure the gfx device is idle before we flush */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5371 | pr_info("Disabling batched IOTLB flush on Ironlake\n"); |
David Woodhouse | 6fbcfb3 | 2011-09-25 19:11:14 -0700 | [diff] [blame] | 5372 | intel_iommu_strict = 1; |
| 5373 | } |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 5374 | } |
| 5375 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt); |
| 5376 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt); |
| 5377 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt); |
| 5378 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt); |
| 5379 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 5380 | /* On Tylersburg chipsets, some BIOSes have been known to enable the |
| 5381 | ISOCH DMAR unit for the Azalia sound device, but not give it any |
| 5382 | TLB entries, which causes it to deadlock. Check for that. We do |
| 5383 | this in a function called from init_dmars(), instead of in a PCI |
| 5384 | quirk, because we don't want to print the obnoxious "BIOS broken" |
| 5385 | message if VT-d is actually disabled. |
| 5386 | */ |
| 5387 | static void __init check_tylersburg_isoch(void) |
| 5388 | { |
| 5389 | struct pci_dev *pdev; |
| 5390 | uint32_t vtisochctrl; |
| 5391 | |
| 5392 | /* If there's no Azalia in the system anyway, forget it. */ |
| 5393 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL); |
| 5394 | if (!pdev) |
| 5395 | return; |
| 5396 | pci_dev_put(pdev); |
| 5397 | |
| 5398 | /* System Management Registers. Might be hidden, in which case |
| 5399 | we can't do the sanity check. But that's OK, because the |
| 5400 | known-broken BIOSes _don't_ actually hide it, so far. */ |
| 5401 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL); |
| 5402 | if (!pdev) |
| 5403 | return; |
| 5404 | |
| 5405 | if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) { |
| 5406 | pci_dev_put(pdev); |
| 5407 | return; |
| 5408 | } |
| 5409 | |
| 5410 | pci_dev_put(pdev); |
| 5411 | |
| 5412 | /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */ |
| 5413 | if (vtisochctrl & 1) |
| 5414 | return; |
| 5415 | |
| 5416 | /* Drop all bits other than the number of TLB entries */ |
| 5417 | vtisochctrl &= 0x1c; |
| 5418 | |
| 5419 | /* If we have the recommended number of TLB entries (16), fine. */ |
| 5420 | if (vtisochctrl == 0x10) |
| 5421 | return; |
| 5422 | |
| 5423 | /* Zero TLB entries? You get to ride the short bus to school. */ |
| 5424 | if (!vtisochctrl) { |
| 5425 | WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n" |
| 5426 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 5427 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 5428 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 5429 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
| 5430 | iommu_identity_mapping |= IDENTMAP_AZALIA; |
| 5431 | return; |
| 5432 | } |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5433 | |
| 5434 | 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] | 5435 | vtisochctrl); |
| 5436 | } |