Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1 | /* |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 2 | * Copyright © 2006-2014 Intel Corporation. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 13 | * Authors: David Woodhouse <dwmw2@infradead.org>, |
| 14 | * Ashok Raj <ashok.raj@intel.com>, |
| 15 | * Shaohua Li <shaohua.li@intel.com>, |
| 16 | * Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>, |
| 17 | * Fenghua Yu <fenghua.yu@intel.com> |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 18 | * Joerg Roedel <jroedel@suse.de> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 21 | #define pr_fmt(fmt) "DMAR: " fmt |
| 22 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/bitmap.h> |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 25 | #include <linux/debugfs.h> |
Paul Gortmaker | 54485c3 | 2011-10-29 10:26:25 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/irq.h> |
| 29 | #include <linux/interrupt.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/pci.h> |
| 32 | #include <linux/dmar.h> |
| 33 | #include <linux/dma-mapping.h> |
| 34 | #include <linux/mempool.h> |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 35 | #include <linux/memory.h> |
Omer Peleg | aa47324 | 2016-04-20 11:33:02 +0300 | [diff] [blame] | 36 | #include <linux/cpu.h> |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 37 | #include <linux/timer.h> |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 38 | #include <linux/io.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 39 | #include <linux/iova.h> |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 40 | #include <linux/iommu.h> |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 41 | #include <linux/intel-iommu.h> |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 42 | #include <linux/syscore_ops.h> |
Shane Wang | 69575d3 | 2009-09-01 18:25:07 -0700 | [diff] [blame] | 43 | #include <linux/tboot.h> |
Stephen Rothwell | adb2fe0 | 2009-08-31 15:24:23 +1000 | [diff] [blame] | 44 | #include <linux/dmi.h> |
Joerg Roedel | 5cdede2 | 2011-04-04 15:55:18 +0200 | [diff] [blame] | 45 | #include <linux/pci-ats.h> |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 46 | #include <linux/memblock.h> |
Akinobu Mita | 3674643 | 2014-06-04 16:06:51 -0700 | [diff] [blame] | 47 | #include <linux/dma-contiguous.h> |
Christoph Hellwig | fec777c | 2018-03-19 11:38:15 +0100 | [diff] [blame] | 48 | #include <linux/dma-direct.h> |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 49 | #include <linux/crash_dump.h> |
Suresh Siddha | 8a8f422 | 2012-03-30 11:47:08 -0700 | [diff] [blame] | 50 | #include <asm/irq_remapping.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 51 | #include <asm/cacheflush.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 52 | #include <asm/iommu.h> |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 53 | |
Joerg Roedel | 078e1ee | 2012-09-26 12:44:43 +0200 | [diff] [blame] | 54 | #include "irq_remapping.h" |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 55 | #include "intel-pasid.h" |
Joerg Roedel | 078e1ee | 2012-09-26 12:44:43 +0200 | [diff] [blame] | 56 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 57 | #define ROOT_SIZE VTD_PAGE_SIZE |
| 58 | #define CONTEXT_SIZE VTD_PAGE_SIZE |
| 59 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 60 | #define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) |
David Woodhouse | 18436af | 2015-03-25 15:05:47 +0000 | [diff] [blame] | 61 | #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] | 62 | #define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA) |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 63 | #define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 64 | |
| 65 | #define IOAPIC_RANGE_START (0xfee00000) |
| 66 | #define IOAPIC_RANGE_END (0xfeefffff) |
| 67 | #define IOVA_START_ADDR (0x1000) |
| 68 | |
Sohil Mehta | 5e3b4a1 | 2017-12-20 11:59:24 -0800 | [diff] [blame] | 69 | #define DEFAULT_DOMAIN_ADDRESS_WIDTH 57 |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 70 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 71 | #define MAX_AGAW_WIDTH 64 |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 72 | #define MAX_AGAW_PFN_WIDTH (MAX_AGAW_WIDTH - VTD_PAGE_SHIFT) |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 73 | |
David Woodhouse | 2ebe315 | 2009-09-19 07:34:04 -0700 | [diff] [blame] | 74 | #define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1) |
| 75 | #define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1) |
| 76 | |
| 77 | /* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR |
| 78 | to match. That way, we can use 'unsigned long' for PFNs with impunity. */ |
| 79 | #define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \ |
| 80 | __DOMAIN_MAX_PFN(gaw), (unsigned long)-1)) |
| 81 | #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] | 82 | |
Robin Murphy | 1b72250 | 2015-01-12 17:51:15 +0000 | [diff] [blame] | 83 | /* IO virtual address start page frame number */ |
| 84 | #define IOVA_START_PFN (1) |
| 85 | |
Mark McLoughlin | f27be03 | 2008-11-20 15:49:43 +0000 | [diff] [blame] | 86 | #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT) |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 87 | |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 88 | /* page table handling */ |
| 89 | #define LEVEL_STRIDE (9) |
| 90 | #define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1) |
| 91 | |
Ohad Ben-Cohen | 6d1c56a | 2011-11-10 11:32:30 +0200 | [diff] [blame] | 92 | /* |
| 93 | * This bitmap is used to advertise the page sizes our hardware support |
| 94 | * to the IOMMU core, which will then use this information to split |
| 95 | * physically contiguous memory regions it is mapping into page sizes |
| 96 | * that we support. |
| 97 | * |
| 98 | * Traditionally the IOMMU core just handed us the mappings directly, |
| 99 | * after making sure the size is an order of a 4KiB page and that the |
| 100 | * mapping has natural alignment. |
| 101 | * |
| 102 | * To retain this behavior, we currently advertise that we support |
| 103 | * all page sizes that are an order of 4KiB. |
| 104 | * |
| 105 | * If at some point we'd like to utilize the IOMMU core's new behavior, |
| 106 | * we could change this to advertise the real page sizes we support. |
| 107 | */ |
| 108 | #define INTEL_IOMMU_PGSIZES (~0xFFFUL) |
| 109 | |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 110 | static inline int agaw_to_level(int agaw) |
| 111 | { |
| 112 | return agaw + 2; |
| 113 | } |
| 114 | |
| 115 | static inline int agaw_to_width(int agaw) |
| 116 | { |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 117 | return min_t(int, 30 + agaw * LEVEL_STRIDE, MAX_AGAW_WIDTH); |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static inline int width_to_agaw(int width) |
| 121 | { |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 122 | return DIV_ROUND_UP(width - 30, LEVEL_STRIDE); |
Andrew Morton | df08cdc | 2010-09-22 13:05:11 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static inline unsigned int level_to_offset_bits(int level) |
| 126 | { |
| 127 | return (level - 1) * LEVEL_STRIDE; |
| 128 | } |
| 129 | |
| 130 | static inline int pfn_level_offset(unsigned long pfn, int level) |
| 131 | { |
| 132 | return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK; |
| 133 | } |
| 134 | |
| 135 | static inline unsigned long level_mask(int level) |
| 136 | { |
| 137 | return -1UL << level_to_offset_bits(level); |
| 138 | } |
| 139 | |
| 140 | static inline unsigned long level_size(int level) |
| 141 | { |
| 142 | return 1UL << level_to_offset_bits(level); |
| 143 | } |
| 144 | |
| 145 | static inline unsigned long align_to_level(unsigned long pfn, int level) |
| 146 | { |
| 147 | return (pfn + level_size(level) - 1) & level_mask(level); |
| 148 | } |
David Woodhouse | fd18de5 | 2009-05-10 23:57:41 +0100 | [diff] [blame] | 149 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 150 | static inline unsigned long lvl_to_nr_pages(unsigned int lvl) |
| 151 | { |
Jiang Liu | 5c645b3 | 2014-01-06 14:18:12 +0800 | [diff] [blame] | 152 | 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] | 153 | } |
| 154 | |
David Woodhouse | dd4e831 | 2009-06-27 16:21:20 +0100 | [diff] [blame] | 155 | /* VT-d pages must always be _smaller_ than MM pages. Otherwise things |
| 156 | are never going to work. */ |
| 157 | static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn) |
| 158 | { |
| 159 | return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 160 | } |
| 161 | |
| 162 | static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn) |
| 163 | { |
| 164 | return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT); |
| 165 | } |
| 166 | static inline unsigned long page_to_dma_pfn(struct page *pg) |
| 167 | { |
| 168 | return mm_to_dma_pfn(page_to_pfn(pg)); |
| 169 | } |
| 170 | static inline unsigned long virt_to_dma_pfn(void *p) |
| 171 | { |
| 172 | return page_to_dma_pfn(virt_to_page(p)); |
| 173 | } |
| 174 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 175 | /* global iommu list, set NULL for ignored DMAR units */ |
| 176 | static struct intel_iommu **g_iommus; |
| 177 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 178 | static void __init check_tylersburg_isoch(void); |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 179 | static int rwbf_quirk; |
| 180 | |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 181 | /* |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 182 | * set to 1 to panic kernel if can't successfully enable VT-d |
| 183 | * (used when kernel is launched w/ TXT) |
| 184 | */ |
| 185 | static int force_on = 0; |
Shaohua Li | bfd20f1 | 2017-04-26 09:18:35 -0700 | [diff] [blame] | 186 | int intel_iommu_tboot_noforce; |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 187 | |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 188 | #define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry)) |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 189 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 190 | /* |
| 191 | * Take a root_entry and return the Lower Context Table Pointer (LCTP) |
| 192 | * if marked present. |
| 193 | */ |
| 194 | static phys_addr_t root_entry_lctp(struct root_entry *re) |
| 195 | { |
| 196 | if (!(re->lo & 1)) |
| 197 | return 0; |
Mark McLoughlin | 46b08e1 | 2008-11-20 15:49:44 +0000 | [diff] [blame] | 198 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 199 | return re->lo & VTD_PAGE_MASK; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Take a root_entry and return the Upper Context Table Pointer (UCTP) |
| 204 | * if marked present. |
| 205 | */ |
| 206 | static phys_addr_t root_entry_uctp(struct root_entry *re) |
| 207 | { |
| 208 | if (!(re->hi & 1)) |
| 209 | return 0; |
| 210 | |
| 211 | return re->hi & VTD_PAGE_MASK; |
| 212 | } |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 213 | |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 214 | static inline void context_clear_pasid_enable(struct context_entry *context) |
| 215 | { |
| 216 | context->lo &= ~(1ULL << 11); |
| 217 | } |
| 218 | |
| 219 | static inline bool context_pasid_enabled(struct context_entry *context) |
| 220 | { |
| 221 | return !!(context->lo & (1ULL << 11)); |
| 222 | } |
| 223 | |
| 224 | static inline void context_set_copied(struct context_entry *context) |
| 225 | { |
| 226 | context->hi |= (1ull << 3); |
| 227 | } |
| 228 | |
| 229 | static inline bool context_copied(struct context_entry *context) |
| 230 | { |
| 231 | return !!(context->hi & (1ULL << 3)); |
| 232 | } |
| 233 | |
| 234 | static inline bool __context_present(struct context_entry *context) |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 235 | { |
| 236 | return (context->lo & 1); |
| 237 | } |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 238 | |
Sohil Mehta | 26b8609 | 2018-09-11 17:11:36 -0700 | [diff] [blame] | 239 | bool context_present(struct context_entry *context) |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 240 | { |
| 241 | return context_pasid_enabled(context) ? |
| 242 | __context_present(context) : |
| 243 | __context_present(context) && !context_copied(context); |
| 244 | } |
| 245 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 246 | static inline void context_set_present(struct context_entry *context) |
| 247 | { |
| 248 | context->lo |= 1; |
| 249 | } |
| 250 | |
| 251 | static inline void context_set_fault_enable(struct context_entry *context) |
| 252 | { |
| 253 | context->lo &= (((u64)-1) << 2) | 1; |
| 254 | } |
| 255 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 256 | static inline void context_set_translation_type(struct context_entry *context, |
| 257 | unsigned long value) |
| 258 | { |
| 259 | context->lo &= (((u64)-1) << 4) | 3; |
| 260 | context->lo |= (value & 3) << 2; |
| 261 | } |
| 262 | |
| 263 | static inline void context_set_address_root(struct context_entry *context, |
| 264 | unsigned long value) |
| 265 | { |
Li, Zhen-Hua | 1a2262f | 2014-11-05 15:30:19 +0800 | [diff] [blame] | 266 | context->lo &= ~VTD_PAGE_MASK; |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 267 | context->lo |= value & VTD_PAGE_MASK; |
| 268 | } |
| 269 | |
| 270 | static inline void context_set_address_width(struct context_entry *context, |
| 271 | unsigned long value) |
| 272 | { |
| 273 | context->hi |= value & 7; |
| 274 | } |
| 275 | |
| 276 | static inline void context_set_domain_id(struct context_entry *context, |
| 277 | unsigned long value) |
| 278 | { |
| 279 | context->hi |= (value & ((1 << 16) - 1)) << 8; |
| 280 | } |
| 281 | |
Joerg Roedel | dbcd861 | 2015-06-12 12:02:09 +0200 | [diff] [blame] | 282 | static inline int context_domain_id(struct context_entry *c) |
| 283 | { |
| 284 | return((c->hi >> 8) & 0xffff); |
| 285 | } |
| 286 | |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 287 | static inline void context_clear_entry(struct context_entry *context) |
| 288 | { |
| 289 | context->lo = 0; |
| 290 | context->hi = 0; |
| 291 | } |
Mark McLoughlin | 7a8fc25 | 2008-11-20 15:49:45 +0000 | [diff] [blame] | 292 | |
Mark McLoughlin | 622ba12 | 2008-11-20 15:49:46 +0000 | [diff] [blame] | 293 | /* |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 294 | * This domain is a statically identity mapping domain. |
| 295 | * 1. This domain creats a static 1:1 mapping to all usable memory. |
| 296 | * 2. It maps to each iommu if successful. |
| 297 | * 3. Each iommu mapps to this domain if successful. |
| 298 | */ |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 299 | static struct dmar_domain *si_domain; |
| 300 | static int hw_pass_through = 1; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 301 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 302 | /* |
| 303 | * Domain represents a virtual machine, more than one devices |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 304 | * across iommus may be owned in one domain, e.g. kvm guest. |
| 305 | */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 306 | #define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 0) |
Weidong Han | 1ce28fe | 2008-12-08 16:35:39 +0800 | [diff] [blame] | 307 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 308 | /* si_domain contains mulitple devices */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 309 | #define DOMAIN_FLAG_STATIC_IDENTITY (1 << 1) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 310 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 311 | #define for_each_domain_iommu(idx, domain) \ |
| 312 | for (idx = 0; idx < g_num_of_iommus; idx++) \ |
| 313 | if (domain->iommu_refcnt[idx]) |
| 314 | |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 315 | struct dmar_rmrr_unit { |
| 316 | struct list_head list; /* list of rmrr units */ |
| 317 | struct acpi_dmar_header *hdr; /* ACPI header */ |
| 318 | u64 base_address; /* reserved base address*/ |
| 319 | u64 end_address; /* reserved end address */ |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 320 | struct dmar_dev_scope *devices; /* target devices */ |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 321 | int devices_cnt; /* target device count */ |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 322 | struct iommu_resv_region *resv; /* reserved region handle */ |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | struct dmar_atsr_unit { |
| 326 | struct list_head list; /* list of ATSR units */ |
| 327 | struct acpi_dmar_header *hdr; /* ACPI header */ |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 328 | struct dmar_dev_scope *devices; /* target devices */ |
Jiang Liu | b94e411 | 2014-02-19 14:07:25 +0800 | [diff] [blame] | 329 | int devices_cnt; /* target device count */ |
| 330 | u8 include_all:1; /* include all ports */ |
| 331 | }; |
| 332 | |
| 333 | static LIST_HEAD(dmar_atsr_units); |
| 334 | static LIST_HEAD(dmar_rmrr_units); |
| 335 | |
| 336 | #define for_each_rmrr_units(rmrr) \ |
| 337 | list_for_each_entry(rmrr, &dmar_rmrr_units, list) |
| 338 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 339 | /* bitmap for indexing intel_iommus */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 340 | static int g_num_of_iommus; |
| 341 | |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 342 | static void domain_exit(struct dmar_domain *domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 343 | static void domain_remove_dev_info(struct dmar_domain *domain); |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 344 | static void dmar_remove_one_dev_info(struct dmar_domain *domain, |
| 345 | struct device *dev); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 346 | static void __dmar_remove_one_dev_info(struct device_domain_info *info); |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 347 | static void domain_context_clear(struct intel_iommu *iommu, |
| 348 | struct device *dev); |
Jiang Liu | 2a46ddf | 2014-07-11 14:19:30 +0800 | [diff] [blame] | 349 | static int domain_detach_iommu(struct dmar_domain *domain, |
| 350 | struct intel_iommu *iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 351 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 352 | #ifdef CONFIG_INTEL_IOMMU_DEFAULT_ON |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 353 | int dmar_disabled = 0; |
| 354 | #else |
| 355 | int dmar_disabled = 1; |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 356 | #endif /*CONFIG_INTEL_IOMMU_DEFAULT_ON*/ |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 357 | |
Eugeni Dodonov | 8bc1f85 | 2011-11-23 16:42:14 -0200 | [diff] [blame] | 358 | int intel_iommu_enabled = 0; |
| 359 | EXPORT_SYMBOL_GPL(intel_iommu_enabled); |
| 360 | |
David Woodhouse | 2d9e667 | 2010-06-15 10:57:57 +0100 | [diff] [blame] | 361 | static int dmar_map_gfx = 1; |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 362 | static int dmar_forcedac; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 363 | static int intel_iommu_strict; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 364 | static int intel_iommu_superpage = 1; |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 365 | static int intel_iommu_sm = 1; |
David Woodhouse | ae853dd | 2015-09-09 11:58:59 +0100 | [diff] [blame] | 366 | static int iommu_identity_mapping; |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 367 | |
David Woodhouse | ae853dd | 2015-09-09 11:58:59 +0100 | [diff] [blame] | 368 | #define IDENTMAP_ALL 1 |
| 369 | #define IDENTMAP_GFX 2 |
| 370 | #define IDENTMAP_AZALIA 4 |
David Woodhouse | c83b2f2 | 2015-06-12 10:15:49 +0100 | [diff] [blame] | 371 | |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 372 | #define sm_supported(iommu) (intel_iommu_sm && ecap_smts((iommu)->ecap)) |
| 373 | #define pasid_supported(iommu) (sm_supported(iommu) && \ |
| 374 | ecap_pasid((iommu)->ecap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 375 | |
David Woodhouse | c0771df | 2011-10-14 20:59:46 +0100 | [diff] [blame] | 376 | int intel_iommu_gfx_mapped; |
| 377 | EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped); |
| 378 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 379 | #define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1)) |
| 380 | static DEFINE_SPINLOCK(device_domain_lock); |
| 381 | static LIST_HEAD(device_domain_list); |
| 382 | |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 383 | /* |
| 384 | * Iterate over elements in device_domain_list and call the specified |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 385 | * callback @fn against each element. |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 386 | */ |
| 387 | int for_each_device_domain(int (*fn)(struct device_domain_info *info, |
| 388 | void *data), void *data) |
| 389 | { |
| 390 | int ret = 0; |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 391 | unsigned long flags; |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 392 | struct device_domain_info *info; |
| 393 | |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 394 | spin_lock_irqsave(&device_domain_lock, flags); |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 395 | list_for_each_entry(info, &device_domain_list, global) { |
| 396 | ret = fn(info, data); |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 397 | if (ret) { |
| 398 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 399 | return ret; |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 400 | } |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 401 | } |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 402 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Lu Baolu | 85319dc | 2018-07-14 15:46:58 +0800 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 407 | const struct iommu_ops intel_iommu_ops; |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 408 | |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 409 | static bool translation_pre_enabled(struct intel_iommu *iommu) |
| 410 | { |
| 411 | return (iommu->flags & VTD_FLAG_TRANS_PRE_ENABLED); |
| 412 | } |
| 413 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 414 | static void clear_translation_pre_enabled(struct intel_iommu *iommu) |
| 415 | { |
| 416 | iommu->flags &= ~VTD_FLAG_TRANS_PRE_ENABLED; |
| 417 | } |
| 418 | |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 419 | static void init_translation_status(struct intel_iommu *iommu) |
| 420 | { |
| 421 | u32 gsts; |
| 422 | |
| 423 | gsts = readl(iommu->reg + DMAR_GSTS_REG); |
| 424 | if (gsts & DMA_GSTS_TES) |
| 425 | iommu->flags |= VTD_FLAG_TRANS_PRE_ENABLED; |
| 426 | } |
| 427 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 428 | /* Convert generic 'struct iommu_domain to private struct dmar_domain */ |
| 429 | static struct dmar_domain *to_dmar_domain(struct iommu_domain *dom) |
| 430 | { |
| 431 | return container_of(dom, struct dmar_domain, domain); |
| 432 | } |
| 433 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 434 | static int __init intel_iommu_setup(char *str) |
| 435 | { |
| 436 | if (!str) |
| 437 | return -EINVAL; |
| 438 | while (*str) { |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 439 | if (!strncmp(str, "on", 2)) { |
| 440 | dmar_disabled = 0; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 441 | pr_info("IOMMU enabled\n"); |
Kyle McMartin | 0cd5c3c | 2009-02-04 14:29:19 -0800 | [diff] [blame] | 442 | } else if (!strncmp(str, "off", 3)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 443 | dmar_disabled = 1; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 444 | pr_info("IOMMU disabled\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 445 | } else if (!strncmp(str, "igfx_off", 8)) { |
| 446 | dmar_map_gfx = 0; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 447 | pr_info("Disable GFX device mapping\n"); |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 448 | } else if (!strncmp(str, "forcedac", 8)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 449 | pr_info("Forcing DAC for PCI devices\n"); |
Keshavamurthy, Anil S | 7d3b03c | 2007-10-21 16:41:53 -0700 | [diff] [blame] | 450 | dmar_forcedac = 1; |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 451 | } else if (!strncmp(str, "strict", 6)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 452 | pr_info("Disable batched IOTLB flush\n"); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 453 | intel_iommu_strict = 1; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 454 | } else if (!strncmp(str, "sp_off", 6)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 455 | pr_info("Disable supported super page\n"); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 456 | intel_iommu_superpage = 0; |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 457 | } else if (!strncmp(str, "sm_off", 6)) { |
| 458 | pr_info("Intel-IOMMU: disable scalable mode support\n"); |
| 459 | intel_iommu_sm = 0; |
Shaohua Li | bfd20f1 | 2017-04-26 09:18:35 -0700 | [diff] [blame] | 460 | } else if (!strncmp(str, "tboot_noforce", 13)) { |
| 461 | printk(KERN_INFO |
| 462 | "Intel-IOMMU: not forcing on after tboot. This could expose security risk for tboot\n"); |
| 463 | intel_iommu_tboot_noforce = 1; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | str += strcspn(str, ","); |
| 467 | while (*str == ',') |
| 468 | str++; |
| 469 | } |
| 470 | return 0; |
| 471 | } |
| 472 | __setup("intel_iommu=", intel_iommu_setup); |
| 473 | |
| 474 | static struct kmem_cache *iommu_domain_cache; |
| 475 | static struct kmem_cache *iommu_devinfo_cache; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 476 | |
Joerg Roedel | 9452d5b | 2015-07-21 10:00:56 +0200 | [diff] [blame] | 477 | static struct dmar_domain* get_iommu_domain(struct intel_iommu *iommu, u16 did) |
| 478 | { |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 479 | struct dmar_domain **domains; |
| 480 | int idx = did >> 8; |
| 481 | |
| 482 | domains = iommu->domains[idx]; |
| 483 | if (!domains) |
| 484 | return NULL; |
| 485 | |
| 486 | return domains[did & 0xff]; |
Joerg Roedel | 9452d5b | 2015-07-21 10:00:56 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void set_iommu_domain(struct intel_iommu *iommu, u16 did, |
| 490 | struct dmar_domain *domain) |
| 491 | { |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 492 | struct dmar_domain **domains; |
| 493 | int idx = did >> 8; |
| 494 | |
| 495 | if (!iommu->domains[idx]) { |
| 496 | size_t size = 256 * sizeof(struct dmar_domain *); |
| 497 | iommu->domains[idx] = kzalloc(size, GFP_ATOMIC); |
| 498 | } |
| 499 | |
| 500 | domains = iommu->domains[idx]; |
| 501 | if (WARN_ON(!domains)) |
| 502 | return; |
| 503 | else |
| 504 | domains[did & 0xff] = domain; |
Joerg Roedel | 9452d5b | 2015-07-21 10:00:56 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 507 | void *alloc_pgtable_page(int node) |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 508 | { |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 509 | struct page *page; |
| 510 | void *vaddr = NULL; |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 511 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 512 | page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0); |
| 513 | if (page) |
| 514 | vaddr = page_address(page); |
Keshavamurthy, Anil S | eb3fa7c | 2007-10-21 16:41:52 -0700 | [diff] [blame] | 515 | return vaddr; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 518 | void free_pgtable_page(void *vaddr) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 519 | { |
| 520 | free_page((unsigned long)vaddr); |
| 521 | } |
| 522 | |
| 523 | static inline void *alloc_domain_mem(void) |
| 524 | { |
KOSAKI Motohiro | 354bb65 | 2009-11-17 16:21:09 +0900 | [diff] [blame] | 525 | return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 528 | static void free_domain_mem(void *vaddr) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 529 | { |
| 530 | kmem_cache_free(iommu_domain_cache, vaddr); |
| 531 | } |
| 532 | |
| 533 | static inline void * alloc_devinfo_mem(void) |
| 534 | { |
KOSAKI Motohiro | 354bb65 | 2009-11-17 16:21:09 +0900 | [diff] [blame] | 535 | return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static inline void free_devinfo_mem(void *vaddr) |
| 539 | { |
| 540 | kmem_cache_free(iommu_devinfo_cache, vaddr); |
| 541 | } |
| 542 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 543 | static inline int domain_type_is_vm(struct dmar_domain *domain) |
| 544 | { |
| 545 | return domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE; |
| 546 | } |
| 547 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 548 | static inline int domain_type_is_si(struct dmar_domain *domain) |
| 549 | { |
| 550 | return domain->flags & DOMAIN_FLAG_STATIC_IDENTITY; |
| 551 | } |
| 552 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 553 | static inline int domain_type_is_vm_or_si(struct dmar_domain *domain) |
| 554 | { |
| 555 | return domain->flags & (DOMAIN_FLAG_VIRTUAL_MACHINE | |
| 556 | DOMAIN_FLAG_STATIC_IDENTITY); |
| 557 | } |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 558 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 559 | static inline int domain_pfn_supported(struct dmar_domain *domain, |
| 560 | unsigned long pfn) |
| 561 | { |
| 562 | int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT; |
| 563 | |
| 564 | return !(addr_width < BITS_PER_LONG && pfn >> addr_width); |
| 565 | } |
| 566 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 567 | static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw) |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 568 | { |
| 569 | unsigned long sagaw; |
| 570 | int agaw = -1; |
| 571 | |
| 572 | sagaw = cap_sagaw(iommu->cap); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 573 | for (agaw = width_to_agaw(max_gaw); |
Weidong Han | 1b57368 | 2008-12-08 15:34:06 +0800 | [diff] [blame] | 574 | agaw >= 0; agaw--) { |
| 575 | if (test_bit(agaw, &sagaw)) |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | return agaw; |
| 580 | } |
| 581 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 582 | /* |
| 583 | * Calculate max SAGAW for each iommu. |
| 584 | */ |
| 585 | int iommu_calculate_max_sagaw(struct intel_iommu *iommu) |
| 586 | { |
| 587 | return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH); |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | * calculate agaw for each iommu. |
| 592 | * "SAGAW" may be different across iommus, use a default agaw, and |
| 593 | * get a supported less agaw for iommus that don't support the default agaw. |
| 594 | */ |
| 595 | int iommu_calculate_agaw(struct intel_iommu *iommu) |
| 596 | { |
| 597 | return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 598 | } |
| 599 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 600 | /* This functionin only returns single iommu in a domain */ |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 601 | struct intel_iommu *domain_get_iommu(struct dmar_domain *domain) |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 602 | { |
| 603 | int iommu_id; |
| 604 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 605 | /* si_domain and vm domain should not get here. */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 606 | BUG_ON(domain_type_is_vm_or_si(domain)); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 607 | for_each_domain_iommu(iommu_id, domain) |
| 608 | break; |
| 609 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 610 | if (iommu_id < 0 || iommu_id >= g_num_of_iommus) |
| 611 | return NULL; |
| 612 | |
| 613 | return g_iommus[iommu_id]; |
| 614 | } |
| 615 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 616 | static void domain_update_iommu_coherency(struct dmar_domain *domain) |
| 617 | { |
David Woodhouse | d050196 | 2014-03-11 17:10:29 -0700 | [diff] [blame] | 618 | struct dmar_drhd_unit *drhd; |
| 619 | struct intel_iommu *iommu; |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 620 | bool found = false; |
| 621 | int i; |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 622 | |
David Woodhouse | d050196 | 2014-03-11 17:10:29 -0700 | [diff] [blame] | 623 | domain->iommu_coherency = 1; |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 624 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 625 | for_each_domain_iommu(i, domain) { |
Quentin Lambert | 2f119c7 | 2015-02-06 10:59:53 +0100 | [diff] [blame] | 626 | found = true; |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 627 | if (!ecap_coherent(g_iommus[i]->ecap)) { |
| 628 | domain->iommu_coherency = 0; |
| 629 | break; |
| 630 | } |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 631 | } |
David Woodhouse | d050196 | 2014-03-11 17:10:29 -0700 | [diff] [blame] | 632 | if (found) |
| 633 | return; |
| 634 | |
| 635 | /* No hardware attached; use lowest common denominator */ |
| 636 | rcu_read_lock(); |
| 637 | for_each_active_iommu(iommu, drhd) { |
| 638 | if (!ecap_coherent(iommu->ecap)) { |
| 639 | domain->iommu_coherency = 0; |
| 640 | break; |
| 641 | } |
| 642 | } |
| 643 | rcu_read_unlock(); |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 644 | } |
| 645 | |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 646 | static int domain_update_iommu_snooping(struct intel_iommu *skip) |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 647 | { |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 648 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 649 | struct intel_iommu *iommu; |
| 650 | int ret = 1; |
| 651 | |
| 652 | rcu_read_lock(); |
| 653 | for_each_active_iommu(iommu, drhd) { |
| 654 | if (iommu != skip) { |
| 655 | if (!ecap_sc_support(iommu->ecap)) { |
| 656 | ret = 0; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | rcu_read_unlock(); |
| 662 | |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | static int domain_update_iommu_superpage(struct intel_iommu *skip) |
| 667 | { |
| 668 | struct dmar_drhd_unit *drhd; |
| 669 | struct intel_iommu *iommu; |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 670 | int mask = 0xf; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 671 | |
| 672 | if (!intel_iommu_superpage) { |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 673 | return 0; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 674 | } |
| 675 | |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 676 | /* set iommu_superpage to the smallest common denominator */ |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 677 | rcu_read_lock(); |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 678 | for_each_active_iommu(iommu, drhd) { |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 679 | if (iommu != skip) { |
| 680 | mask &= cap_super_page_val(iommu->cap); |
| 681 | if (!mask) |
| 682 | break; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 683 | } |
| 684 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 685 | rcu_read_unlock(); |
| 686 | |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 687 | return fls(mask); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 690 | /* Some capabilities may be different across iommus */ |
| 691 | static void domain_update_iommu_cap(struct dmar_domain *domain) |
| 692 | { |
| 693 | domain_update_iommu_coherency(domain); |
Jiang Liu | 161f693 | 2014-07-11 14:19:37 +0800 | [diff] [blame] | 694 | domain->iommu_snooping = domain_update_iommu_snooping(NULL); |
| 695 | domain->iommu_superpage = domain_update_iommu_superpage(NULL); |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 696 | } |
| 697 | |
Sohil Mehta | 26b8609 | 2018-09-11 17:11:36 -0700 | [diff] [blame] | 698 | struct context_entry *iommu_context_addr(struct intel_iommu *iommu, u8 bus, |
| 699 | u8 devfn, int alloc) |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 700 | { |
| 701 | struct root_entry *root = &iommu->root_entry[bus]; |
| 702 | struct context_entry *context; |
| 703 | u64 *entry; |
| 704 | |
Joerg Roedel | 4df4eab | 2015-08-25 10:54:28 +0200 | [diff] [blame] | 705 | entry = &root->lo; |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 706 | if (sm_supported(iommu)) { |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 707 | if (devfn >= 0x80) { |
| 708 | devfn -= 0x80; |
| 709 | entry = &root->hi; |
| 710 | } |
| 711 | devfn *= 2; |
| 712 | } |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 713 | if (*entry & 1) |
| 714 | context = phys_to_virt(*entry & VTD_PAGE_MASK); |
| 715 | else { |
| 716 | unsigned long phy_addr; |
| 717 | if (!alloc) |
| 718 | return NULL; |
| 719 | |
| 720 | context = alloc_pgtable_page(iommu->node); |
| 721 | if (!context) |
| 722 | return NULL; |
| 723 | |
| 724 | __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE); |
| 725 | phy_addr = virt_to_phys((void *)context); |
| 726 | *entry = phy_addr | 1; |
| 727 | __iommu_flush_cache(iommu, entry, sizeof(*entry)); |
| 728 | } |
| 729 | return &context[devfn]; |
| 730 | } |
| 731 | |
David Woodhouse | 4ed6a54 | 2015-05-11 14:59:20 +0100 | [diff] [blame] | 732 | static int iommu_dummy(struct device *dev) |
| 733 | { |
| 734 | return dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO; |
| 735 | } |
| 736 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 737 | 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] | 738 | { |
| 739 | struct dmar_drhd_unit *drhd = NULL; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 740 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 741 | struct device *tmp; |
| 742 | struct pci_dev *ptmp, *pdev = NULL; |
Yijing Wang | aa4d066 | 2014-05-26 20:14:06 +0800 | [diff] [blame] | 743 | u16 segment = 0; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 744 | int i; |
| 745 | |
David Woodhouse | 4ed6a54 | 2015-05-11 14:59:20 +0100 | [diff] [blame] | 746 | if (iommu_dummy(dev)) |
| 747 | return NULL; |
| 748 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 749 | if (dev_is_pci(dev)) { |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 750 | struct pci_dev *pf_pdev; |
| 751 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 752 | pdev = to_pci_dev(dev); |
Jon Derrick | 5823e33 | 2017-08-30 15:05:59 -0600 | [diff] [blame] | 753 | |
| 754 | #ifdef CONFIG_X86 |
| 755 | /* VMD child devices currently cannot be handled individually */ |
| 756 | if (is_vmd(pdev->bus)) |
| 757 | return NULL; |
| 758 | #endif |
| 759 | |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 760 | /* VFs aren't listed in scope tables; we need to look up |
| 761 | * the PF instead to find the IOMMU. */ |
| 762 | pf_pdev = pci_physfn(pdev); |
| 763 | dev = &pf_pdev->dev; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 764 | segment = pci_domain_nr(pdev->bus); |
Rafael J. Wysocki | ca5b74d | 2015-03-16 23:49:08 +0100 | [diff] [blame] | 765 | } else if (has_acpi_companion(dev)) |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 766 | dev = &ACPI_COMPANION(dev)->dev; |
| 767 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 768 | rcu_read_lock(); |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 769 | for_each_active_iommu(iommu, drhd) { |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 770 | if (pdev && segment != drhd->segment) |
David Woodhouse | 276dbf99 | 2009-04-04 01:45:37 +0100 | [diff] [blame] | 771 | continue; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 772 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 773 | for_each_active_dev_scope(drhd->devices, |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 774 | drhd->devices_cnt, i, tmp) { |
| 775 | if (tmp == dev) { |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 776 | /* For a VF use its original BDF# not that of the PF |
| 777 | * which we used for the IOMMU lookup. Strictly speaking |
| 778 | * we could do this for all PCI devices; we only need to |
| 779 | * get the BDF# from the scope table for ACPI matches. */ |
Koos Vriezen | 5003ae1 | 2017-03-01 21:02:50 +0100 | [diff] [blame] | 780 | if (pdev && pdev->is_virtfn) |
Ashok Raj | 1c38718 | 2016-10-21 15:32:05 -0700 | [diff] [blame] | 781 | goto got_pdev; |
| 782 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 783 | *bus = drhd->devices[i].bus; |
| 784 | *devfn = drhd->devices[i].devfn; |
| 785 | goto out; |
| 786 | } |
| 787 | |
| 788 | if (!pdev || !dev_is_pci(tmp)) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 789 | continue; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 790 | |
| 791 | ptmp = to_pci_dev(tmp); |
| 792 | if (ptmp->subordinate && |
| 793 | ptmp->subordinate->number <= pdev->bus->number && |
| 794 | ptmp->subordinate->busn_res.end >= pdev->bus->number) |
| 795 | goto got_pdev; |
David Woodhouse | 924b623 | 2009-04-04 00:39:25 +0100 | [diff] [blame] | 796 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 797 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 798 | if (pdev && drhd->include_all) { |
| 799 | got_pdev: |
| 800 | *bus = pdev->bus->number; |
| 801 | *devfn = pdev->devfn; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 802 | goto out; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 803 | } |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 804 | } |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 805 | iommu = NULL; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 806 | out: |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 807 | rcu_read_unlock(); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 808 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 809 | return iommu; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 810 | } |
| 811 | |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 812 | static void domain_flush_cache(struct dmar_domain *domain, |
| 813 | void *addr, int size) |
| 814 | { |
| 815 | if (!domain->iommu_coherency) |
| 816 | clflush_cache_range(addr, size); |
| 817 | } |
| 818 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 819 | static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn) |
| 820 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 821 | struct context_entry *context; |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 822 | int ret = 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 823 | unsigned long flags; |
| 824 | |
| 825 | spin_lock_irqsave(&iommu->lock, flags); |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 826 | context = iommu_context_addr(iommu, bus, devfn, 0); |
| 827 | if (context) |
| 828 | ret = context_present(context); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 829 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 830 | return ret; |
| 831 | } |
| 832 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 833 | static void free_context_table(struct intel_iommu *iommu) |
| 834 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 835 | int i; |
| 836 | unsigned long flags; |
| 837 | struct context_entry *context; |
| 838 | |
| 839 | spin_lock_irqsave(&iommu->lock, flags); |
| 840 | if (!iommu->root_entry) { |
| 841 | goto out; |
| 842 | } |
| 843 | for (i = 0; i < ROOT_ENTRY_NR; i++) { |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 844 | context = iommu_context_addr(iommu, i, 0, 0); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 845 | if (context) |
| 846 | free_pgtable_page(context); |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 847 | |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 848 | if (!sm_supported(iommu)) |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 849 | continue; |
| 850 | |
| 851 | context = iommu_context_addr(iommu, i, 0x80, 0); |
| 852 | if (context) |
| 853 | free_pgtable_page(context); |
| 854 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 855 | } |
| 856 | free_pgtable_page(iommu->root_entry); |
| 857 | iommu->root_entry = NULL; |
| 858 | out: |
| 859 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 860 | } |
| 861 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 862 | static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain, |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 863 | unsigned long pfn, int *target_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 864 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 865 | struct dma_pte *parent, *pte = NULL; |
| 866 | int level = agaw_to_level(domain->agaw); |
Allen Kay | 4399c8b | 2011-10-14 12:32:46 -0700 | [diff] [blame] | 867 | int offset; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 868 | |
| 869 | BUG_ON(!domain->pgd); |
Julian Stecklina | f942360 | 2013-10-09 10:03:52 +0200 | [diff] [blame] | 870 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 871 | if (!domain_pfn_supported(domain, pfn)) |
Julian Stecklina | f942360 | 2013-10-09 10:03:52 +0200 | [diff] [blame] | 872 | /* Address beyond IOMMU's addressing capabilities. */ |
| 873 | return NULL; |
| 874 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 875 | parent = domain->pgd; |
| 876 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 877 | while (1) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 878 | void *tmp_page; |
| 879 | |
David Woodhouse | b026fd2 | 2009-06-28 10:37:25 +0100 | [diff] [blame] | 880 | offset = pfn_level_offset(pfn, level); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 881 | pte = &parent[offset]; |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 882 | if (!*target_level && (dma_pte_superpage(pte) || !dma_pte_present(pte))) |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 883 | break; |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 884 | if (level == *target_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 885 | break; |
| 886 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 887 | if (!dma_pte_present(pte)) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 888 | uint64_t pteval; |
| 889 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 890 | tmp_page = alloc_pgtable_page(domain->nid); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 891 | |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame] | 892 | if (!tmp_page) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 893 | return NULL; |
David Woodhouse | 206a73c | 2009-07-01 19:30:28 +0100 | [diff] [blame] | 894 | |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 895 | domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE); |
Benjamin LaHaise | 64de5af | 2009-09-16 21:05:55 -0400 | [diff] [blame] | 896 | 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] | 897 | if (cmpxchg64(&pte->val, 0ULL, pteval)) |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 898 | /* Someone else set it while we were thinking; use theirs. */ |
| 899 | free_pgtable_page(tmp_page); |
Yijing Wang | effad4b | 2014-05-26 20:13:47 +0800 | [diff] [blame] | 900 | else |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 901 | domain_flush_cache(domain, pte, sizeof(*pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 902 | } |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 903 | if (level == 1) |
| 904 | break; |
| 905 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 906 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 907 | level--; |
| 908 | } |
| 909 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 910 | if (!*target_level) |
| 911 | *target_level = level; |
| 912 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 913 | return pte; |
| 914 | } |
| 915 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 916 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 917 | /* return address's pte at specific level */ |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 918 | static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain, |
| 919 | unsigned long pfn, |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 920 | int level, int *large_page) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 921 | { |
| 922 | struct dma_pte *parent, *pte = NULL; |
| 923 | int total = agaw_to_level(domain->agaw); |
| 924 | int offset; |
| 925 | |
| 926 | parent = domain->pgd; |
| 927 | while (level <= total) { |
David Woodhouse | 90dcfb5 | 2009-06-27 17:14:59 +0100 | [diff] [blame] | 928 | offset = pfn_level_offset(pfn, total); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 929 | pte = &parent[offset]; |
| 930 | if (level == total) |
| 931 | return pte; |
| 932 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 933 | if (!dma_pte_present(pte)) { |
| 934 | *large_page = total; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 935 | break; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Yijing Wang | e16922a | 2014-05-20 20:37:51 +0800 | [diff] [blame] | 938 | if (dma_pte_superpage(pte)) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 939 | *large_page = total; |
| 940 | return pte; |
| 941 | } |
| 942 | |
Mark McLoughlin | 19c239c | 2008-11-21 16:56:53 +0000 | [diff] [blame] | 943 | parent = phys_to_virt(dma_pte_addr(pte)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 944 | total--; |
| 945 | } |
| 946 | return NULL; |
| 947 | } |
| 948 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 949 | /* clear last level pte, a tlb flush should be followed */ |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 950 | static void dma_pte_clear_range(struct dmar_domain *domain, |
David Woodhouse | 595badf5 | 2009-06-27 22:09:11 +0100 | [diff] [blame] | 951 | unsigned long start_pfn, |
| 952 | unsigned long last_pfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 953 | { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 954 | unsigned int large_page = 1; |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 955 | struct dma_pte *first_pte, *pte; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 956 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 957 | BUG_ON(!domain_pfn_supported(domain, start_pfn)); |
| 958 | BUG_ON(!domain_pfn_supported(domain, last_pfn)); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 959 | BUG_ON(start_pfn > last_pfn); |
David Woodhouse | 66eae84 | 2009-06-27 19:00:32 +0100 | [diff] [blame] | 960 | |
David Woodhouse | 04b18e6 | 2009-06-27 19:15:01 +0100 | [diff] [blame] | 961 | /* we don't need lock here; nobody else touches the iova range */ |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 962 | do { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 963 | large_page = 1; |
| 964 | 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] | 965 | if (!pte) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 966 | start_pfn = align_to_level(start_pfn + 1, large_page + 1); |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 967 | continue; |
| 968 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 969 | do { |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 970 | dma_clear_pte(pte); |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 971 | start_pfn += lvl_to_nr_pages(large_page); |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 972 | pte++; |
David Woodhouse | 75e6bf9 | 2009-07-02 11:21:16 +0100 | [diff] [blame] | 973 | } while (start_pfn <= last_pfn && !first_pte_in_page(pte)); |
| 974 | |
David Woodhouse | 310a5ab | 2009-06-28 18:52:20 +0100 | [diff] [blame] | 975 | domain_flush_cache(domain, first_pte, |
| 976 | (void *)pte - (void *)first_pte); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 977 | |
| 978 | } while (start_pfn && start_pfn <= last_pfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 981 | static void dma_pte_free_level(struct dmar_domain *domain, int level, |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 982 | int retain_level, struct dma_pte *pte, |
| 983 | unsigned long pfn, unsigned long start_pfn, |
| 984 | unsigned long last_pfn) |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 985 | { |
| 986 | pfn = max(start_pfn, pfn); |
| 987 | pte = &pte[pfn_level_offset(pfn, level)]; |
| 988 | |
| 989 | do { |
| 990 | unsigned long level_pfn; |
| 991 | struct dma_pte *level_pte; |
| 992 | |
| 993 | if (!dma_pte_present(pte) || dma_pte_superpage(pte)) |
| 994 | goto next; |
| 995 | |
David Dillow | f7116e1 | 2017-01-30 19:11:11 -0800 | [diff] [blame] | 996 | level_pfn = pfn & level_mask(level); |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 997 | level_pte = phys_to_virt(dma_pte_addr(pte)); |
| 998 | |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 999 | if (level > 2) { |
| 1000 | dma_pte_free_level(domain, level - 1, retain_level, |
| 1001 | level_pte, level_pfn, start_pfn, |
| 1002 | last_pfn); |
| 1003 | } |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1004 | |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1005 | /* |
| 1006 | * Free the page table if we're below the level we want to |
| 1007 | * retain and the range covers the entire table. |
| 1008 | */ |
| 1009 | if (level < retain_level && !(start_pfn > level_pfn || |
Alex Williamson | 08336fd | 2014-01-21 15:48:18 -0800 | [diff] [blame] | 1010 | last_pfn < level_pfn + level_size(level) - 1)) { |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1011 | dma_clear_pte(pte); |
| 1012 | domain_flush_cache(domain, pte, sizeof(*pte)); |
| 1013 | free_pgtable_page(level_pte); |
| 1014 | } |
| 1015 | next: |
| 1016 | pfn += level_size(level); |
| 1017 | } while (!first_pte_in_page(++pte) && pfn <= last_pfn); |
| 1018 | } |
| 1019 | |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1020 | /* |
| 1021 | * clear last level (leaf) ptes and free page table pages below the |
| 1022 | * level we wish to keep intact. |
| 1023 | */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1024 | static void dma_pte_free_pagetable(struct dmar_domain *domain, |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 1025 | unsigned long start_pfn, |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1026 | unsigned long last_pfn, |
| 1027 | int retain_level) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1028 | { |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 1029 | BUG_ON(!domain_pfn_supported(domain, start_pfn)); |
| 1030 | BUG_ON(!domain_pfn_supported(domain, last_pfn)); |
David Woodhouse | 59c3628 | 2009-09-19 07:36:28 -0700 | [diff] [blame] | 1031 | BUG_ON(start_pfn > last_pfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1032 | |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 1033 | dma_pte_clear_range(domain, start_pfn, last_pfn); |
| 1034 | |
David Woodhouse | f3a0a52 | 2009-06-30 03:40:07 +0100 | [diff] [blame] | 1035 | /* We don't need lock here; nobody else touches the iova range */ |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 1036 | dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level, |
Alex Williamson | 3269ee0 | 2013-06-15 10:27:19 -0600 | [diff] [blame] | 1037 | domain->pgd, 0, start_pfn, last_pfn); |
David Woodhouse | 6660c63 | 2009-06-27 22:41:00 +0100 | [diff] [blame] | 1038 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1039 | /* free pgd */ |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 1040 | if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1041 | free_pgtable_page(domain->pgd); |
| 1042 | domain->pgd = NULL; |
| 1043 | } |
| 1044 | } |
| 1045 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1046 | /* When a page at a given level is being unlinked from its parent, we don't |
| 1047 | need to *modify* it at all. All we need to do is make a list of all the |
| 1048 | pages which can be freed just as soon as we've flushed the IOTLB and we |
| 1049 | know the hardware page-walk will no longer touch them. |
| 1050 | The 'pte' argument is the *parent* PTE, pointing to the page that is to |
| 1051 | be freed. */ |
| 1052 | static struct page *dma_pte_list_pagetables(struct dmar_domain *domain, |
| 1053 | int level, struct dma_pte *pte, |
| 1054 | struct page *freelist) |
| 1055 | { |
| 1056 | struct page *pg; |
| 1057 | |
| 1058 | pg = pfn_to_page(dma_pte_addr(pte) >> PAGE_SHIFT); |
| 1059 | pg->freelist = freelist; |
| 1060 | freelist = pg; |
| 1061 | |
| 1062 | if (level == 1) |
| 1063 | return freelist; |
| 1064 | |
Jiang Liu | adeb259 | 2014-04-09 10:20:39 +0800 | [diff] [blame] | 1065 | pte = page_address(pg); |
| 1066 | do { |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1067 | if (dma_pte_present(pte) && !dma_pte_superpage(pte)) |
| 1068 | freelist = dma_pte_list_pagetables(domain, level - 1, |
| 1069 | pte, freelist); |
Jiang Liu | adeb259 | 2014-04-09 10:20:39 +0800 | [diff] [blame] | 1070 | pte++; |
| 1071 | } while (!first_pte_in_page(pte)); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1072 | |
| 1073 | return freelist; |
| 1074 | } |
| 1075 | |
| 1076 | static struct page *dma_pte_clear_level(struct dmar_domain *domain, int level, |
| 1077 | struct dma_pte *pte, unsigned long pfn, |
| 1078 | unsigned long start_pfn, |
| 1079 | unsigned long last_pfn, |
| 1080 | struct page *freelist) |
| 1081 | { |
| 1082 | struct dma_pte *first_pte = NULL, *last_pte = NULL; |
| 1083 | |
| 1084 | pfn = max(start_pfn, pfn); |
| 1085 | pte = &pte[pfn_level_offset(pfn, level)]; |
| 1086 | |
| 1087 | do { |
| 1088 | unsigned long level_pfn; |
| 1089 | |
| 1090 | if (!dma_pte_present(pte)) |
| 1091 | goto next; |
| 1092 | |
| 1093 | level_pfn = pfn & level_mask(level); |
| 1094 | |
| 1095 | /* If range covers entire pagetable, free it */ |
| 1096 | if (start_pfn <= level_pfn && |
| 1097 | last_pfn >= level_pfn + level_size(level) - 1) { |
| 1098 | /* These suborbinate page tables are going away entirely. Don't |
| 1099 | bother to clear them; we're just going to *free* them. */ |
| 1100 | if (level > 1 && !dma_pte_superpage(pte)) |
| 1101 | freelist = dma_pte_list_pagetables(domain, level - 1, pte, freelist); |
| 1102 | |
| 1103 | dma_clear_pte(pte); |
| 1104 | if (!first_pte) |
| 1105 | first_pte = pte; |
| 1106 | last_pte = pte; |
| 1107 | } else if (level > 1) { |
| 1108 | /* Recurse down into a level that isn't *entirely* obsolete */ |
| 1109 | freelist = dma_pte_clear_level(domain, level - 1, |
| 1110 | phys_to_virt(dma_pte_addr(pte)), |
| 1111 | level_pfn, start_pfn, last_pfn, |
| 1112 | freelist); |
| 1113 | } |
| 1114 | next: |
| 1115 | pfn += level_size(level); |
| 1116 | } while (!first_pte_in_page(++pte) && pfn <= last_pfn); |
| 1117 | |
| 1118 | if (first_pte) |
| 1119 | domain_flush_cache(domain, first_pte, |
| 1120 | (void *)++last_pte - (void *)first_pte); |
| 1121 | |
| 1122 | return freelist; |
| 1123 | } |
| 1124 | |
| 1125 | /* We can't just free the pages because the IOMMU may still be walking |
| 1126 | the page tables, and may have cached the intermediate levels. The |
| 1127 | pages can only be freed after the IOTLB flush has been done. */ |
Joerg Roedel | b690420 | 2015-08-13 11:32:18 +0200 | [diff] [blame] | 1128 | static struct page *domain_unmap(struct dmar_domain *domain, |
| 1129 | unsigned long start_pfn, |
| 1130 | unsigned long last_pfn) |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1131 | { |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1132 | struct page *freelist = NULL; |
| 1133 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 1134 | BUG_ON(!domain_pfn_supported(domain, start_pfn)); |
| 1135 | BUG_ON(!domain_pfn_supported(domain, last_pfn)); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1136 | BUG_ON(start_pfn > last_pfn); |
| 1137 | |
| 1138 | /* we don't need lock here; nobody else touches the iova range */ |
| 1139 | freelist = dma_pte_clear_level(domain, agaw_to_level(domain->agaw), |
| 1140 | domain->pgd, 0, start_pfn, last_pfn, NULL); |
| 1141 | |
| 1142 | /* free pgd */ |
| 1143 | if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { |
| 1144 | struct page *pgd_page = virt_to_page(domain->pgd); |
| 1145 | pgd_page->freelist = freelist; |
| 1146 | freelist = pgd_page; |
| 1147 | |
| 1148 | domain->pgd = NULL; |
| 1149 | } |
| 1150 | |
| 1151 | return freelist; |
| 1152 | } |
| 1153 | |
Joerg Roedel | b690420 | 2015-08-13 11:32:18 +0200 | [diff] [blame] | 1154 | static void dma_free_pagelist(struct page *freelist) |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1155 | { |
| 1156 | struct page *pg; |
| 1157 | |
| 1158 | while ((pg = freelist)) { |
| 1159 | freelist = pg->freelist; |
| 1160 | free_pgtable_page(page_address(pg)); |
| 1161 | } |
| 1162 | } |
| 1163 | |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1164 | static void iova_entry_free(unsigned long data) |
| 1165 | { |
| 1166 | struct page *freelist = (struct page *)data; |
| 1167 | |
| 1168 | dma_free_pagelist(freelist); |
| 1169 | } |
| 1170 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1171 | /* iommu handling */ |
| 1172 | static int iommu_alloc_root_entry(struct intel_iommu *iommu) |
| 1173 | { |
| 1174 | struct root_entry *root; |
| 1175 | unsigned long flags; |
| 1176 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1177 | root = (struct root_entry *)alloc_pgtable_page(iommu->node); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1178 | if (!root) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1179 | pr_err("Allocating root entry for %s failed\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1180 | iommu->name); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1181 | return -ENOMEM; |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1182 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1183 | |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1184 | __iommu_flush_cache(iommu, root, ROOT_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1185 | |
| 1186 | spin_lock_irqsave(&iommu->lock, flags); |
| 1187 | iommu->root_entry = root; |
| 1188 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1193 | static void iommu_set_root_entry(struct intel_iommu *iommu) |
| 1194 | { |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1195 | u64 addr; |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1196 | u32 sts; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1197 | unsigned long flag; |
| 1198 | |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1199 | addr = virt_to_phys(iommu->root_entry); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1200 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1201 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1202 | dmar_writeq(iommu->reg + DMAR_RTADDR_REG, addr); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1203 | |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1204 | writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* Make sure hardware complete it */ |
| 1207 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1208 | readl, (sts & DMA_GSTS_RTPS), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1209 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1210 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Lu Baolu | 6f7db75 | 2018-12-10 09:59:00 +0800 | [diff] [blame] | 1213 | void iommu_flush_write_buffer(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1214 | { |
| 1215 | u32 val; |
| 1216 | unsigned long flag; |
| 1217 | |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 1218 | if (!rwbf_quirk && !cap_rwbf(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1219 | return; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1220 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1221 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
David Woodhouse | 462b60f | 2009-05-10 20:18:18 +0100 | [diff] [blame] | 1222 | writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1223 | |
| 1224 | /* Make sure hardware complete it */ |
| 1225 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1226 | readl, (!(val & DMA_GSTS_WBFS)), val); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1227 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1228 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 1232 | static void __iommu_flush_context(struct intel_iommu *iommu, |
| 1233 | u16 did, u16 source_id, u8 function_mask, |
| 1234 | u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1235 | { |
| 1236 | u64 val = 0; |
| 1237 | unsigned long flag; |
| 1238 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1239 | switch (type) { |
| 1240 | case DMA_CCMD_GLOBAL_INVL: |
| 1241 | val = DMA_CCMD_GLOBAL_INVL; |
| 1242 | break; |
| 1243 | case DMA_CCMD_DOMAIN_INVL: |
| 1244 | val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did); |
| 1245 | break; |
| 1246 | case DMA_CCMD_DEVICE_INVL: |
| 1247 | val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did) |
| 1248 | | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask); |
| 1249 | break; |
| 1250 | default: |
| 1251 | BUG(); |
| 1252 | } |
| 1253 | val |= DMA_CCMD_ICC; |
| 1254 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1255 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1256 | dmar_writeq(iommu->reg + DMAR_CCMD_REG, val); |
| 1257 | |
| 1258 | /* Make sure hardware complete it */ |
| 1259 | IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG, |
| 1260 | dmar_readq, (!(val & DMA_CCMD_ICC)), val); |
| 1261 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1262 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1265 | /* return value determine if we need a write buffer flush */ |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1266 | static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did, |
| 1267 | u64 addr, unsigned int size_order, u64 type) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1268 | { |
| 1269 | int tlb_offset = ecap_iotlb_offset(iommu->ecap); |
| 1270 | u64 val = 0, val_iva = 0; |
| 1271 | unsigned long flag; |
| 1272 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1273 | switch (type) { |
| 1274 | case DMA_TLB_GLOBAL_FLUSH: |
| 1275 | /* global flush doesn't need set IVA_REG */ |
| 1276 | val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT; |
| 1277 | break; |
| 1278 | case DMA_TLB_DSI_FLUSH: |
| 1279 | val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
| 1280 | break; |
| 1281 | case DMA_TLB_PSI_FLUSH: |
| 1282 | val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1283 | /* IH bit is passed in as part of address */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1284 | val_iva = size_order | addr; |
| 1285 | break; |
| 1286 | default: |
| 1287 | BUG(); |
| 1288 | } |
| 1289 | /* Note: set drain read/write */ |
| 1290 | #if 0 |
| 1291 | /* |
| 1292 | * This is probably to be super secure.. Looks like we can |
| 1293 | * ignore it without any impact. |
| 1294 | */ |
| 1295 | if (cap_read_drain(iommu->cap)) |
| 1296 | val |= DMA_TLB_READ_DRAIN; |
| 1297 | #endif |
| 1298 | if (cap_write_drain(iommu->cap)) |
| 1299 | val |= DMA_TLB_WRITE_DRAIN; |
| 1300 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1301 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1302 | /* Note: Only uses first TLB reg currently */ |
| 1303 | if (val_iva) |
| 1304 | dmar_writeq(iommu->reg + tlb_offset, val_iva); |
| 1305 | dmar_writeq(iommu->reg + tlb_offset + 8, val); |
| 1306 | |
| 1307 | /* Make sure hardware complete it */ |
| 1308 | IOMMU_WAIT_OP(iommu, tlb_offset + 8, |
| 1309 | dmar_readq, (!(val & DMA_TLB_IVT)), val); |
| 1310 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1311 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1312 | |
| 1313 | /* check IOTLB invalidation granularity */ |
| 1314 | if (DMA_TLB_IAIG(val) == 0) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1315 | pr_err("Flush IOTLB failed\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1316 | if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type)) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1317 | pr_debug("TLB flush request %Lx, actual %Lx\n", |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1318 | (unsigned long long)DMA_TLB_IIRG(type), |
| 1319 | (unsigned long long)DMA_TLB_IAIG(val)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 1322 | static struct device_domain_info * |
| 1323 | iommu_support_dev_iotlb (struct dmar_domain *domain, struct intel_iommu *iommu, |
| 1324 | u8 bus, u8 devfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1325 | { |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1326 | struct device_domain_info *info; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1327 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1328 | assert_spin_locked(&device_domain_lock); |
| 1329 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1330 | if (!iommu->qi) |
| 1331 | return NULL; |
| 1332 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1333 | list_for_each_entry(info, &domain->devices, link) |
Jiang Liu | c3b497c | 2014-07-11 14:19:25 +0800 | [diff] [blame] | 1334 | if (info->iommu == iommu && info->bus == bus && |
| 1335 | info->devfn == devfn) { |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1336 | if (info->ats_supported && info->dev) |
| 1337 | return info; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1338 | break; |
| 1339 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1340 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1341 | return NULL; |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1342 | } |
| 1343 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1344 | static void domain_update_iotlb(struct dmar_domain *domain) |
| 1345 | { |
| 1346 | struct device_domain_info *info; |
| 1347 | bool has_iotlb_device = false; |
| 1348 | |
| 1349 | assert_spin_locked(&device_domain_lock); |
| 1350 | |
| 1351 | list_for_each_entry(info, &domain->devices, link) { |
| 1352 | struct pci_dev *pdev; |
| 1353 | |
| 1354 | if (!info->dev || !dev_is_pci(info->dev)) |
| 1355 | continue; |
| 1356 | |
| 1357 | pdev = to_pci_dev(info->dev); |
| 1358 | if (pdev->ats_enabled) { |
| 1359 | has_iotlb_device = true; |
| 1360 | break; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | domain->has_iotlb_device = has_iotlb_device; |
| 1365 | } |
| 1366 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1367 | static void iommu_enable_dev_iotlb(struct device_domain_info *info) |
| 1368 | { |
Bjorn Helgaas | fb0cc3a | 2015-07-20 09:10:36 -0500 | [diff] [blame] | 1369 | struct pci_dev *pdev; |
| 1370 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1371 | assert_spin_locked(&device_domain_lock); |
| 1372 | |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 1373 | if (!info || !dev_is_pci(info->dev)) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1374 | return; |
| 1375 | |
Bjorn Helgaas | fb0cc3a | 2015-07-20 09:10:36 -0500 | [diff] [blame] | 1376 | pdev = to_pci_dev(info->dev); |
Jacob Pan | 1c48db4 | 2018-06-07 09:57:00 -0700 | [diff] [blame] | 1377 | /* For IOMMU that supports device IOTLB throttling (DIT), we assign |
| 1378 | * PFSID to the invalidation desc of a VF such that IOMMU HW can gauge |
| 1379 | * queue depth at PF level. If DIT is not set, PFSID will be treated as |
| 1380 | * reserved, which should be set to 0. |
| 1381 | */ |
| 1382 | if (!ecap_dit(info->iommu->ecap)) |
| 1383 | info->pfsid = 0; |
| 1384 | else { |
| 1385 | struct pci_dev *pf_pdev; |
| 1386 | |
| 1387 | /* pdev will be returned if device is not a vf */ |
| 1388 | pf_pdev = pci_physfn(pdev); |
| 1389 | info->pfsid = PCI_DEVID(pf_pdev->bus->number, pf_pdev->devfn); |
| 1390 | } |
Bjorn Helgaas | fb0cc3a | 2015-07-20 09:10:36 -0500 | [diff] [blame] | 1391 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1392 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 1393 | /* The PCIe spec, in its wisdom, declares that the behaviour of |
| 1394 | the device if you enable PASID support after ATS support is |
| 1395 | undefined. So always enable PASID support on devices which |
| 1396 | have it, even if we can't yet know if we're ever going to |
| 1397 | use it. */ |
| 1398 | if (info->pasid_supported && !pci_enable_pasid(pdev, info->pasid_supported & ~1)) |
| 1399 | info->pasid_enabled = 1; |
| 1400 | |
| 1401 | if (info->pri_supported && !pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32)) |
| 1402 | info->pri_enabled = 1; |
| 1403 | #endif |
| 1404 | if (info->ats_supported && !pci_enable_ats(pdev, VTD_PAGE_SHIFT)) { |
| 1405 | info->ats_enabled = 1; |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1406 | domain_update_iotlb(info->domain); |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1407 | info->ats_qdep = pci_ats_queue_depth(pdev); |
| 1408 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | static void iommu_disable_dev_iotlb(struct device_domain_info *info) |
| 1412 | { |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1413 | struct pci_dev *pdev; |
| 1414 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1415 | assert_spin_locked(&device_domain_lock); |
| 1416 | |
Jeremy McNicoll | da972fb | 2016-01-14 21:33:06 -0800 | [diff] [blame] | 1417 | if (!dev_is_pci(info->dev)) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1418 | return; |
| 1419 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1420 | pdev = to_pci_dev(info->dev); |
| 1421 | |
| 1422 | if (info->ats_enabled) { |
| 1423 | pci_disable_ats(pdev); |
| 1424 | info->ats_enabled = 0; |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1425 | domain_update_iotlb(info->domain); |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1426 | } |
| 1427 | #ifdef CONFIG_INTEL_IOMMU_SVM |
| 1428 | if (info->pri_enabled) { |
| 1429 | pci_disable_pri(pdev); |
| 1430 | info->pri_enabled = 0; |
| 1431 | } |
| 1432 | if (info->pasid_enabled) { |
| 1433 | pci_disable_pasid(pdev); |
| 1434 | info->pasid_enabled = 0; |
| 1435 | } |
| 1436 | #endif |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | static void iommu_flush_dev_iotlb(struct dmar_domain *domain, |
| 1440 | u64 addr, unsigned mask) |
| 1441 | { |
| 1442 | u16 sid, qdep; |
| 1443 | unsigned long flags; |
| 1444 | struct device_domain_info *info; |
| 1445 | |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1446 | if (!domain->has_iotlb_device) |
| 1447 | return; |
| 1448 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1449 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1450 | list_for_each_entry(info, &domain->devices, link) { |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1451 | if (!info->ats_enabled) |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1452 | continue; |
| 1453 | |
| 1454 | sid = info->bus << 8 | info->devfn; |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1455 | qdep = info->ats_qdep; |
Jacob Pan | 1c48db4 | 2018-06-07 09:57:00 -0700 | [diff] [blame] | 1456 | qi_flush_dev_iotlb(info->iommu, sid, info->pfsid, |
| 1457 | qdep, addr, mask); |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1458 | } |
| 1459 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 1460 | } |
| 1461 | |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 1462 | static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, |
| 1463 | struct dmar_domain *domain, |
| 1464 | unsigned long pfn, unsigned int pages, |
| 1465 | int ih, int map) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1466 | { |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1467 | unsigned int mask = ilog2(__roundup_pow_of_two(pages)); |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 1468 | uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT; |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 1469 | u16 did = domain->iommu_did[iommu->seq_id]; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1470 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1471 | BUG_ON(pages == 0); |
| 1472 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1473 | if (ih) |
| 1474 | ih = 1 << 6; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1475 | /* |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1476 | * Fallback to domain selective flush if no PSI support or the size is |
| 1477 | * too big. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1478 | * PSI requires page size to be 2 ^ x, and the base address is naturally |
| 1479 | * aligned to the size |
| 1480 | */ |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1481 | if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap)) |
| 1482 | iommu->flush.flush_iotlb(iommu, did, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 1483 | DMA_TLB_DSI_FLUSH); |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1484 | else |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1485 | iommu->flush.flush_iotlb(iommu, did, addr | ih, mask, |
Yu Zhao | 9dd2fe8 | 2009-05-18 13:51:36 +0800 | [diff] [blame] | 1486 | DMA_TLB_PSI_FLUSH); |
Yu Zhao | bf92df3 | 2009-06-29 11:31:45 +0800 | [diff] [blame] | 1487 | |
| 1488 | /* |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1489 | * In caching mode, changes of pages from non-present to present require |
| 1490 | * 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] | 1491 | */ |
Nadav Amit | 8265363 | 2010-04-01 13:24:40 +0300 | [diff] [blame] | 1492 | if (!cap_caching_mode(iommu->cap) || !map) |
Peter Xu | 9d2e650 | 2018-01-10 13:51:37 +0800 | [diff] [blame] | 1493 | iommu_flush_dev_iotlb(domain, addr, mask); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Peter Xu | eed91a0 | 2018-05-04 10:34:52 +0800 | [diff] [blame] | 1496 | /* Notification for newly created mappings */ |
| 1497 | static inline void __mapping_notify_one(struct intel_iommu *iommu, |
| 1498 | struct dmar_domain *domain, |
| 1499 | unsigned long pfn, unsigned int pages) |
| 1500 | { |
| 1501 | /* It's a non-present to present mapping. Only flush if caching mode */ |
| 1502 | if (cap_caching_mode(iommu->cap)) |
| 1503 | iommu_flush_iotlb_psi(iommu, domain, pfn, pages, 0, 1); |
| 1504 | else |
| 1505 | iommu_flush_write_buffer(iommu); |
| 1506 | } |
| 1507 | |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1508 | static void iommu_flush_iova(struct iova_domain *iovad) |
| 1509 | { |
| 1510 | struct dmar_domain *domain; |
| 1511 | int idx; |
| 1512 | |
| 1513 | domain = container_of(iovad, struct dmar_domain, iovad); |
| 1514 | |
| 1515 | for_each_domain_iommu(idx, domain) { |
| 1516 | struct intel_iommu *iommu = g_iommus[idx]; |
| 1517 | u16 did = domain->iommu_did[iommu->seq_id]; |
| 1518 | |
| 1519 | iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); |
| 1520 | |
| 1521 | if (!cap_caching_mode(iommu->cap)) |
| 1522 | iommu_flush_dev_iotlb(get_iommu_domain(iommu, did), |
| 1523 | 0, MAX_AGAW_PFN_WIDTH); |
| 1524 | } |
| 1525 | } |
| 1526 | |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1527 | static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu) |
| 1528 | { |
| 1529 | u32 pmen; |
| 1530 | unsigned long flags; |
| 1531 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1532 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1533 | pmen = readl(iommu->reg + DMAR_PMEN_REG); |
| 1534 | pmen &= ~DMA_PMEN_EPM; |
| 1535 | writel(pmen, iommu->reg + DMAR_PMEN_REG); |
| 1536 | |
| 1537 | /* wait for the protected region status bit to clear */ |
| 1538 | IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG, |
| 1539 | readl, !(pmen & DMA_PMEN_PRS), pmen); |
| 1540 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1541 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
mark gross | f8bab73 | 2008-02-08 04:18:38 -0800 | [diff] [blame] | 1542 | } |
| 1543 | |
Jiang Liu | 2a41cce | 2014-07-11 14:19:33 +0800 | [diff] [blame] | 1544 | static void iommu_enable_translation(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1545 | { |
| 1546 | u32 sts; |
| 1547 | unsigned long flags; |
| 1548 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1549 | raw_spin_lock_irqsave(&iommu->register_lock, flags); |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1550 | iommu->gcmd |= DMA_GCMD_TE; |
| 1551 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1552 | |
| 1553 | /* Make sure hardware complete it */ |
| 1554 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1555 | readl, (sts & DMA_GSTS_TES), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1556 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1557 | raw_spin_unlock_irqrestore(&iommu->register_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
Jiang Liu | 2a41cce | 2014-07-11 14:19:33 +0800 | [diff] [blame] | 1560 | static void iommu_disable_translation(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1561 | { |
| 1562 | u32 sts; |
| 1563 | unsigned long flag; |
| 1564 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1565 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1566 | iommu->gcmd &= ~DMA_GCMD_TE; |
| 1567 | writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG); |
| 1568 | |
| 1569 | /* Make sure hardware complete it */ |
| 1570 | IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG, |
David Woodhouse | c416daa | 2009-05-10 20:30:58 +0100 | [diff] [blame] | 1571 | readl, (!(sts & DMA_GSTS_TES)), sts); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1572 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 1573 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 1576 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1577 | static int iommu_init_domains(struct intel_iommu *iommu) |
| 1578 | { |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1579 | u32 ndomains, nlongs; |
| 1580 | size_t size; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1581 | |
| 1582 | ndomains = cap_ndoms(iommu->cap); |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1583 | pr_debug("%s: Number of Domains supported <%d>\n", |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1584 | iommu->name, ndomains); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1585 | nlongs = BITS_TO_LONGS(ndomains); |
| 1586 | |
Donald Dutile | 94a91b50 | 2009-08-20 16:51:34 -0400 | [diff] [blame] | 1587 | spin_lock_init(&iommu->lock); |
| 1588 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1589 | iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL); |
| 1590 | if (!iommu->domain_ids) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1591 | pr_err("%s: Allocating domain id array failed\n", |
| 1592 | iommu->name); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1593 | return -ENOMEM; |
| 1594 | } |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1595 | |
Wei Yang | 86f004c | 2016-05-21 02:41:51 +0000 | [diff] [blame] | 1596 | size = (ALIGN(ndomains, 256) >> 8) * sizeof(struct dmar_domain **); |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1597 | iommu->domains = kzalloc(size, GFP_KERNEL); |
| 1598 | |
| 1599 | if (iommu->domains) { |
| 1600 | size = 256 * sizeof(struct dmar_domain *); |
| 1601 | iommu->domains[0] = kzalloc(size, GFP_KERNEL); |
| 1602 | } |
| 1603 | |
| 1604 | if (!iommu->domains || !iommu->domains[0]) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1605 | pr_err("%s: Allocating domain array failed\n", |
| 1606 | iommu->name); |
Jiang Liu | 852bdb0 | 2014-01-06 14:18:11 +0800 | [diff] [blame] | 1607 | kfree(iommu->domain_ids); |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1608 | kfree(iommu->domains); |
Jiang Liu | 852bdb0 | 2014-01-06 14:18:11 +0800 | [diff] [blame] | 1609 | iommu->domain_ids = NULL; |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1610 | iommu->domains = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1611 | return -ENOMEM; |
| 1612 | } |
| 1613 | |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1614 | |
| 1615 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1616 | /* |
Joerg Roedel | c0e8a6c | 2015-07-21 09:39:46 +0200 | [diff] [blame] | 1617 | * If Caching mode is set, then invalid translations are tagged |
| 1618 | * with domain-id 0, hence we need to pre-allocate it. We also |
| 1619 | * use domain-id 0 as a marker for non-allocated domain-id, so |
| 1620 | * make sure it is not used for a real domain. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1621 | */ |
Joerg Roedel | c0e8a6c | 2015-07-21 09:39:46 +0200 | [diff] [blame] | 1622 | set_bit(0, iommu->domain_ids); |
| 1623 | |
Lu Baolu | 3b33d4a | 2018-12-10 09:58:59 +0800 | [diff] [blame] | 1624 | /* |
| 1625 | * Vt-d spec rev3.0 (section 6.2.3.1) requires that each pasid |
| 1626 | * entry for first-level or pass-through translation modes should |
| 1627 | * be programmed with a domain id different from those used for |
| 1628 | * second-level or nested translation. We reserve a domain id for |
| 1629 | * this purpose. |
| 1630 | */ |
| 1631 | if (sm_supported(iommu)) |
| 1632 | set_bit(FLPT_DEFAULT_DID, iommu->domain_ids); |
| 1633 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1636 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1637 | static void disable_dmar_iommu(struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1638 | { |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1639 | struct device_domain_info *info, *tmp; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1640 | unsigned long flags; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1641 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1642 | if (!iommu->domains || !iommu->domain_ids) |
| 1643 | return; |
Jiang Liu | a4eaa86 | 2014-02-19 14:07:30 +0800 | [diff] [blame] | 1644 | |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1645 | again: |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1646 | spin_lock_irqsave(&device_domain_lock, flags); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1647 | list_for_each_entry_safe(info, tmp, &device_domain_list, global) { |
| 1648 | struct dmar_domain *domain; |
| 1649 | |
| 1650 | if (info->iommu != iommu) |
| 1651 | continue; |
| 1652 | |
| 1653 | if (!info->dev || !info->domain) |
| 1654 | continue; |
| 1655 | |
| 1656 | domain = info->domain; |
| 1657 | |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1658 | __dmar_remove_one_dev_info(info); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1659 | |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1660 | if (!domain_type_is_vm_or_si(domain)) { |
| 1661 | /* |
| 1662 | * The domain_exit() function can't be called under |
| 1663 | * device_domain_lock, as it takes this lock itself. |
| 1664 | * So release the lock here and re-run the loop |
| 1665 | * afterwards. |
| 1666 | */ |
| 1667 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1668 | domain_exit(domain); |
Joerg Roedel | bea6403 | 2016-11-08 15:08:26 +0100 | [diff] [blame] | 1669 | goto again; |
| 1670 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1671 | } |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1672 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1673 | |
| 1674 | if (iommu->gcmd & DMA_GCMD_TE) |
| 1675 | iommu_disable_translation(iommu); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1676 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1677 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1678 | static void free_dmar_iommu(struct intel_iommu *iommu) |
| 1679 | { |
| 1680 | if ((iommu->domains) && (iommu->domain_ids)) { |
Wei Yang | 86f004c | 2016-05-21 02:41:51 +0000 | [diff] [blame] | 1681 | int elems = ALIGN(cap_ndoms(iommu->cap), 256) >> 8; |
Joerg Roedel | 8bf4781 | 2015-07-21 10:41:21 +0200 | [diff] [blame] | 1682 | int i; |
| 1683 | |
| 1684 | for (i = 0; i < elems; i++) |
| 1685 | kfree(iommu->domains[i]); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 1686 | kfree(iommu->domains); |
| 1687 | kfree(iommu->domain_ids); |
| 1688 | iommu->domains = NULL; |
| 1689 | iommu->domain_ids = NULL; |
| 1690 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1691 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 1692 | g_iommus[iommu->seq_id] = NULL; |
| 1693 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1694 | /* free context mapping */ |
| 1695 | free_context_table(iommu); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 1696 | |
| 1697 | #ifdef CONFIG_INTEL_IOMMU_SVM |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 1698 | if (pasid_supported(iommu)) { |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 1699 | if (ecap_prs(iommu->ecap)) |
| 1700 | intel_svm_finish_prq(iommu); |
Lu Baolu | d973795 | 2018-07-14 15:47:02 +0800 | [diff] [blame] | 1701 | intel_svm_exit(iommu); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 1702 | } |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 1703 | #endif |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 1706 | static struct dmar_domain *alloc_domain(int flags) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1707 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1708 | struct dmar_domain *domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1709 | |
| 1710 | domain = alloc_domain_mem(); |
| 1711 | if (!domain) |
| 1712 | return NULL; |
| 1713 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 1714 | memset(domain, 0, sizeof(*domain)); |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1715 | domain->nid = -1; |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 1716 | domain->flags = flags; |
Omer Peleg | 0824c59 | 2016-04-20 19:03:35 +0300 | [diff] [blame] | 1717 | domain->has_iotlb_device = false; |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 1718 | INIT_LIST_HEAD(&domain->devices); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1719 | |
| 1720 | return domain; |
| 1721 | } |
| 1722 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1723 | /* Must be called with iommu->lock */ |
| 1724 | static int domain_attach_iommu(struct dmar_domain *domain, |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1725 | struct intel_iommu *iommu) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1726 | { |
Jiang Liu | 44bde61 | 2014-07-11 14:19:29 +0800 | [diff] [blame] | 1727 | unsigned long ndomains; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1728 | int num; |
Jiang Liu | 44bde61 | 2014-07-11 14:19:29 +0800 | [diff] [blame] | 1729 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1730 | assert_spin_locked(&device_domain_lock); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1731 | assert_spin_locked(&iommu->lock); |
Jiang Liu | 44bde61 | 2014-07-11 14:19:29 +0800 | [diff] [blame] | 1732 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1733 | domain->iommu_refcnt[iommu->seq_id] += 1; |
| 1734 | domain->iommu_count += 1; |
| 1735 | if (domain->iommu_refcnt[iommu->seq_id] == 1) { |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1736 | ndomains = cap_ndoms(iommu->cap); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1737 | num = find_first_zero_bit(iommu->domain_ids, ndomains); |
| 1738 | |
| 1739 | if (num >= ndomains) { |
| 1740 | pr_err("%s: No free domain ids\n", iommu->name); |
| 1741 | domain->iommu_refcnt[iommu->seq_id] -= 1; |
| 1742 | domain->iommu_count -= 1; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1743 | return -ENOSPC; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 1744 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1745 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1746 | set_bit(num, iommu->domain_ids); |
| 1747 | set_iommu_domain(iommu, num, domain); |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1748 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1749 | domain->iommu_did[iommu->seq_id] = num; |
| 1750 | domain->nid = iommu->node; |
| 1751 | |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1752 | domain_update_iommu_cap(domain); |
| 1753 | } |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1754 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1755 | return 0; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | static int domain_detach_iommu(struct dmar_domain *domain, |
| 1759 | struct intel_iommu *iommu) |
| 1760 | { |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1761 | int num, count = INT_MAX; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1762 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1763 | assert_spin_locked(&device_domain_lock); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1764 | assert_spin_locked(&iommu->lock); |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1765 | |
Joerg Roedel | 29a2771 | 2015-07-21 17:17:12 +0200 | [diff] [blame] | 1766 | domain->iommu_refcnt[iommu->seq_id] -= 1; |
| 1767 | count = --domain->iommu_count; |
| 1768 | if (domain->iommu_refcnt[iommu->seq_id] == 0) { |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1769 | num = domain->iommu_did[iommu->seq_id]; |
| 1770 | clear_bit(num, iommu->domain_ids); |
| 1771 | set_iommu_domain(iommu, num, NULL); |
| 1772 | |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1773 | domain_update_iommu_cap(domain); |
Joerg Roedel | c0e8a6c | 2015-07-21 09:39:46 +0200 | [diff] [blame] | 1774 | domain->iommu_did[iommu->seq_id] = 0; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1775 | } |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 1776 | |
| 1777 | return count; |
| 1778 | } |
| 1779 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1780 | static struct iova_domain reserved_iova_list; |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1781 | static struct lock_class_key reserved_rbtree_key; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1782 | |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1783 | static int dmar_init_reserved_ranges(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1784 | { |
| 1785 | struct pci_dev *pdev = NULL; |
| 1786 | struct iova *iova; |
| 1787 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1788 | |
Zhen Lei | aa3ac94 | 2017-09-21 16:52:45 +0100 | [diff] [blame] | 1789 | 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] | 1790 | |
Mark Gross | 8a443df | 2008-03-04 14:59:31 -0800 | [diff] [blame] | 1791 | lockdep_set_class(&reserved_iova_list.iova_rbtree_lock, |
| 1792 | &reserved_rbtree_key); |
| 1793 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1794 | /* IOAPIC ranges shouldn't be accessed by DMA */ |
| 1795 | iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START), |
| 1796 | IOVA_PFN(IOAPIC_RANGE_END)); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1797 | if (!iova) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1798 | pr_err("Reserve IOAPIC range failed\n"); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1799 | return -ENODEV; |
| 1800 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1801 | |
| 1802 | /* Reserve all PCI MMIO to avoid peer-to-peer access */ |
| 1803 | for_each_pci_dev(pdev) { |
| 1804 | struct resource *r; |
| 1805 | |
| 1806 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 1807 | r = &pdev->resource[i]; |
| 1808 | if (!r->flags || !(r->flags & IORESOURCE_MEM)) |
| 1809 | continue; |
David Woodhouse | 1a4a455 | 2009-06-28 16:00:42 +0100 | [diff] [blame] | 1810 | iova = reserve_iova(&reserved_iova_list, |
| 1811 | IOVA_PFN(r->start), |
| 1812 | IOVA_PFN(r->end)); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1813 | if (!iova) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1814 | pr_err("Reserve iova failed\n"); |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1815 | return -ENODEV; |
| 1816 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1817 | } |
| 1818 | } |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 1819 | return 0; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static void domain_reserve_special_ranges(struct dmar_domain *domain) |
| 1823 | { |
| 1824 | copy_reserved_iova(&reserved_iova_list, &domain->iovad); |
| 1825 | } |
| 1826 | |
| 1827 | static inline int guestwidth_to_adjustwidth(int gaw) |
| 1828 | { |
| 1829 | int agaw; |
| 1830 | int r = (gaw - 12) % 9; |
| 1831 | |
| 1832 | if (r == 0) |
| 1833 | agaw = gaw; |
| 1834 | else |
| 1835 | agaw = gaw + 9 - r; |
| 1836 | if (agaw > 64) |
| 1837 | agaw = 64; |
| 1838 | return agaw; |
| 1839 | } |
| 1840 | |
Joerg Roedel | dc534b2 | 2015-07-22 12:44:02 +0200 | [diff] [blame] | 1841 | static int domain_init(struct dmar_domain *domain, struct intel_iommu *iommu, |
| 1842 | int guest_width) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1843 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1844 | int adjust_width, agaw; |
| 1845 | unsigned long sagaw; |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1846 | int err; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1847 | |
Zhen Lei | aa3ac94 | 2017-09-21 16:52:45 +0100 | [diff] [blame] | 1848 | init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN); |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 1849 | |
| 1850 | err = init_iova_flush_queue(&domain->iovad, |
| 1851 | iommu_flush_iova, iova_entry_free); |
| 1852 | if (err) |
| 1853 | return err; |
| 1854 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1855 | domain_reserve_special_ranges(domain); |
| 1856 | |
| 1857 | /* calculate AGAW */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1858 | if (guest_width > cap_mgaw(iommu->cap)) |
| 1859 | guest_width = cap_mgaw(iommu->cap); |
| 1860 | domain->gaw = guest_width; |
| 1861 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 1862 | agaw = width_to_agaw(adjust_width); |
| 1863 | sagaw = cap_sagaw(iommu->cap); |
| 1864 | if (!test_bit(agaw, &sagaw)) { |
| 1865 | /* hardware doesn't support it, choose a bigger one */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 1866 | pr_debug("Hardware doesn't support agaw %d\n", agaw); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1867 | agaw = find_next_bit(&sagaw, 5, agaw); |
| 1868 | if (agaw >= 5) |
| 1869 | return -ENODEV; |
| 1870 | } |
| 1871 | domain->agaw = agaw; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1872 | |
Weidong Han | 8e604097 | 2008-12-08 15:49:06 +0800 | [diff] [blame] | 1873 | if (ecap_coherent(iommu->ecap)) |
| 1874 | domain->iommu_coherency = 1; |
| 1875 | else |
| 1876 | domain->iommu_coherency = 0; |
| 1877 | |
Sheng Yang | 58c610b | 2009-03-18 15:33:05 +0800 | [diff] [blame] | 1878 | if (ecap_sc_support(iommu->ecap)) |
| 1879 | domain->iommu_snooping = 1; |
| 1880 | else |
| 1881 | domain->iommu_snooping = 0; |
| 1882 | |
David Woodhouse | 214e39a | 2014-03-19 10:38:49 +0000 | [diff] [blame] | 1883 | if (intel_iommu_superpage) |
| 1884 | domain->iommu_superpage = fls(cap_super_page_val(iommu->cap)); |
| 1885 | else |
| 1886 | domain->iommu_superpage = 0; |
| 1887 | |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1888 | domain->nid = iommu->node; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 1889 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1890 | /* always allocate the top pgd */ |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 1891 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1892 | if (!domain->pgd) |
| 1893 | return -ENOMEM; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 1894 | __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1895 | return 0; |
| 1896 | } |
| 1897 | |
| 1898 | static void domain_exit(struct dmar_domain *domain) |
| 1899 | { |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1900 | struct page *freelist = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1901 | |
| 1902 | /* Domain 0 is reserved, so dont process it */ |
| 1903 | if (!domain) |
| 1904 | return; |
| 1905 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1906 | /* Remove associated devices and clear attached or cached domains */ |
| 1907 | rcu_read_lock(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1908 | domain_remove_dev_info(domain); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 1909 | rcu_read_unlock(); |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 1910 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1911 | /* destroy iovas */ |
| 1912 | put_iova_domain(&domain->iovad); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1913 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1914 | freelist = domain_unmap(domain, 0, DOMAIN_MAX_PFN(domain->gaw)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1915 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 1916 | dma_free_pagelist(freelist); |
| 1917 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1918 | free_domain_mem(domain); |
| 1919 | } |
| 1920 | |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 1921 | static int domain_context_mapping_one(struct dmar_domain *domain, |
| 1922 | struct intel_iommu *iommu, |
Lu Baolu | ca6e322 | 2018-12-10 09:59:02 +0800 | [diff] [blame^] | 1923 | struct pasid_table *table, |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1924 | u8 bus, u8 devfn) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1925 | { |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 1926 | u16 did = domain->iommu_did[iommu->seq_id]; |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1927 | int translation = CONTEXT_TT_MULTI_LEVEL; |
| 1928 | struct device_domain_info *info = NULL; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1929 | struct context_entry *context; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1930 | unsigned long flags; |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1931 | struct dma_pte *pgd; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1932 | int ret, agaw; |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1933 | |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 1934 | WARN_ON(did == 0); |
| 1935 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 1936 | if (hw_pass_through && domain_type_is_si(domain)) |
| 1937 | translation = CONTEXT_TT_PASS_THROUGH; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1938 | |
| 1939 | pr_debug("Set context mapping for %02x:%02x.%d\n", |
| 1940 | bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 1941 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1942 | BUG_ON(!domain->pgd); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 1943 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1944 | spin_lock_irqsave(&device_domain_lock, flags); |
| 1945 | spin_lock(&iommu->lock); |
| 1946 | |
| 1947 | ret = -ENOMEM; |
David Woodhouse | 03ecc32 | 2015-02-13 14:35:21 +0000 | [diff] [blame] | 1948 | context = iommu_context_addr(iommu, bus, devfn, 1); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1949 | if (!context) |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1950 | goto out_unlock; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 1951 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1952 | ret = 0; |
| 1953 | if (context_present(context)) |
| 1954 | goto out_unlock; |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 1955 | |
Xunlei Pang | aec0e86 | 2016-12-05 20:09:07 +0800 | [diff] [blame] | 1956 | /* |
| 1957 | * For kdump cases, old valid entries may be cached due to the |
| 1958 | * in-flight DMA and copied pgtable, but there is no unmapping |
| 1959 | * behaviour for them, thus we need an explicit cache flush for |
| 1960 | * the newly-mapped device. For kdump, at this point, the device |
| 1961 | * is supposed to finish reset at its driver probe stage, so no |
| 1962 | * in-flight DMA will exist, and we don't need to worry anymore |
| 1963 | * hereafter. |
| 1964 | */ |
| 1965 | if (context_copied(context)) { |
| 1966 | u16 did_old = context_domain_id(context); |
| 1967 | |
Christos Gkekas | b117e03 | 2017-10-08 23:33:31 +0100 | [diff] [blame] | 1968 | if (did_old < cap_ndoms(iommu->cap)) { |
Xunlei Pang | aec0e86 | 2016-12-05 20:09:07 +0800 | [diff] [blame] | 1969 | iommu->flush.flush_context(iommu, did_old, |
| 1970 | (((u16)bus) << 8) | devfn, |
| 1971 | DMA_CCMD_MASK_NOBIT, |
| 1972 | DMA_CCMD_DEVICE_INVL); |
KarimAllah Ahmed | f73a7ee | 2017-05-05 11:39:59 -0700 | [diff] [blame] | 1973 | iommu->flush.flush_iotlb(iommu, did_old, 0, 0, |
| 1974 | DMA_TLB_DSI_FLUSH); |
| 1975 | } |
Xunlei Pang | aec0e86 | 2016-12-05 20:09:07 +0800 | [diff] [blame] | 1976 | } |
| 1977 | |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1978 | pgd = domain->pgd; |
| 1979 | |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 1980 | context_clear_entry(context); |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 1981 | context_set_domain_id(context, did); |
Weidong Han | ea6606b | 2008-12-08 23:08:15 +0800 | [diff] [blame] | 1982 | |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 1983 | /* |
| 1984 | * Skip top levels of page tables for iommu which has less agaw |
| 1985 | * than default. Unnecessary for PT mode. |
| 1986 | */ |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 1987 | if (translation != CONTEXT_TT_PASS_THROUGH) { |
Sohil Mehta | 3569dd0 | 2018-11-21 15:29:33 -0800 | [diff] [blame] | 1988 | for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) { |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1989 | ret = -ENOMEM; |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 1990 | pgd = phys_to_virt(dma_pte_addr(pgd)); |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 1991 | if (!dma_pte_present(pgd)) |
| 1992 | goto out_unlock; |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 1995 | info = iommu_support_dev_iotlb(domain, iommu, bus, devfn); |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 1996 | if (info && info->ats_supported) |
| 1997 | translation = CONTEXT_TT_DEV_IOTLB; |
| 1998 | else |
| 1999 | translation = CONTEXT_TT_MULTI_LEVEL; |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2000 | |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2001 | context_set_address_root(context, virt_to_phys(pgd)); |
Sohil Mehta | 3569dd0 | 2018-11-21 15:29:33 -0800 | [diff] [blame] | 2002 | context_set_address_width(context, agaw); |
Joerg Roedel | de24e55 | 2015-07-21 14:53:04 +0200 | [diff] [blame] | 2003 | } else { |
| 2004 | /* |
| 2005 | * In pass through mode, AW must be programmed to |
| 2006 | * indicate the largest AGAW value supported by |
| 2007 | * hardware. And ASR is ignored by hardware. |
| 2008 | */ |
| 2009 | context_set_address_width(context, iommu->msagaw); |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2010 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 2011 | |
| 2012 | context_set_translation_type(context, translation); |
Mark McLoughlin | c07e7d2 | 2008-11-21 16:54:46 +0000 | [diff] [blame] | 2013 | context_set_fault_enable(context); |
| 2014 | context_set_present(context); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2015 | domain_flush_cache(domain, context, sizeof(*context)); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2016 | |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2017 | /* |
| 2018 | * It's a non-present to present mapping. If hardware doesn't cache |
| 2019 | * non-present entry we only need to flush the write-buffer. If the |
| 2020 | * _does_ cache non-present entries, then it does so in the special |
| 2021 | * domain #0, which we have to flush: |
| 2022 | */ |
| 2023 | if (cap_caching_mode(iommu->cap)) { |
| 2024 | iommu->flush.flush_context(iommu, 0, |
| 2025 | (((u16)bus) << 8) | devfn, |
| 2026 | DMA_CCMD_MASK_NOBIT, |
| 2027 | DMA_CCMD_DEVICE_INVL); |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2028 | iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2029 | } else { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2030 | iommu_flush_write_buffer(iommu); |
David Woodhouse | 4c25a2c | 2009-05-10 17:16:06 +0100 | [diff] [blame] | 2031 | } |
Yu Zhao | 93a23a7 | 2009-05-18 13:51:37 +0800 | [diff] [blame] | 2032 | iommu_enable_dev_iotlb(info); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 2033 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 2034 | ret = 0; |
| 2035 | |
| 2036 | out_unlock: |
| 2037 | spin_unlock(&iommu->lock); |
| 2038 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 2039 | |
Wei Yang | 5c365d1 | 2016-07-13 13:53:21 +0000 | [diff] [blame] | 2040 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2043 | struct domain_context_mapping_data { |
| 2044 | struct dmar_domain *domain; |
| 2045 | struct intel_iommu *iommu; |
Lu Baolu | ca6e322 | 2018-12-10 09:59:02 +0800 | [diff] [blame^] | 2046 | struct pasid_table *table; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2047 | }; |
| 2048 | |
| 2049 | static int domain_context_mapping_cb(struct pci_dev *pdev, |
| 2050 | u16 alias, void *opaque) |
| 2051 | { |
| 2052 | struct domain_context_mapping_data *data = opaque; |
| 2053 | |
| 2054 | return domain_context_mapping_one(data->domain, data->iommu, |
Lu Baolu | ca6e322 | 2018-12-10 09:59:02 +0800 | [diff] [blame^] | 2055 | data->table, PCI_BUS_NUM(alias), |
| 2056 | alias & 0xff); |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2057 | } |
| 2058 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2059 | static int |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2060 | domain_context_mapping(struct dmar_domain *domain, struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2061 | { |
Lu Baolu | ca6e322 | 2018-12-10 09:59:02 +0800 | [diff] [blame^] | 2062 | struct domain_context_mapping_data data; |
| 2063 | struct pasid_table *table; |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 2064 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 2065 | u8 bus, devfn; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2066 | |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2067 | iommu = device_to_iommu(dev, &bus, &devfn); |
David Woodhouse | 64ae892 | 2014-03-09 12:52:30 -0700 | [diff] [blame] | 2068 | if (!iommu) |
| 2069 | return -ENODEV; |
| 2070 | |
Lu Baolu | ca6e322 | 2018-12-10 09:59:02 +0800 | [diff] [blame^] | 2071 | table = intel_pasid_get_table(dev); |
| 2072 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2073 | if (!dev_is_pci(dev)) |
Lu Baolu | ca6e322 | 2018-12-10 09:59:02 +0800 | [diff] [blame^] | 2074 | return domain_context_mapping_one(domain, iommu, table, |
| 2075 | bus, devfn); |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2076 | |
| 2077 | data.domain = domain; |
| 2078 | data.iommu = iommu; |
Lu Baolu | ca6e322 | 2018-12-10 09:59:02 +0800 | [diff] [blame^] | 2079 | data.table = table; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2080 | |
| 2081 | return pci_for_each_dma_alias(to_pci_dev(dev), |
| 2082 | &domain_context_mapping_cb, &data); |
| 2083 | } |
| 2084 | |
| 2085 | static int domain_context_mapped_cb(struct pci_dev *pdev, |
| 2086 | u16 alias, void *opaque) |
| 2087 | { |
| 2088 | struct intel_iommu *iommu = opaque; |
| 2089 | |
| 2090 | return !device_context_mapped(iommu, PCI_BUS_NUM(alias), alias & 0xff); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2093 | static int domain_context_mapped(struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2094 | { |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2095 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 2096 | u8 bus, devfn; |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2097 | |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2098 | iommu = device_to_iommu(dev, &bus, &devfn); |
Weidong Han | 5331fe6 | 2008-12-08 23:00:00 +0800 | [diff] [blame] | 2099 | if (!iommu) |
| 2100 | return -ENODEV; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2101 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2102 | if (!dev_is_pci(dev)) |
| 2103 | return device_context_mapped(iommu, bus, devfn); |
David Woodhouse | e1f167f | 2014-03-09 15:24:46 -0700 | [diff] [blame] | 2104 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2105 | return !pci_for_each_dma_alias(to_pci_dev(dev), |
| 2106 | domain_context_mapped_cb, iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 2109 | /* Returns a number of VTD pages, but aligned to MM page size */ |
| 2110 | static inline unsigned long aligned_nrpages(unsigned long host_addr, |
| 2111 | size_t size) |
| 2112 | { |
| 2113 | host_addr &= ~PAGE_MASK; |
| 2114 | return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT; |
| 2115 | } |
| 2116 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2117 | /* Return largest possible superpage level for a given mapping */ |
| 2118 | static inline int hardware_largepage_caps(struct dmar_domain *domain, |
| 2119 | unsigned long iov_pfn, |
| 2120 | unsigned long phy_pfn, |
| 2121 | unsigned long pages) |
| 2122 | { |
| 2123 | int support, level = 1; |
| 2124 | unsigned long pfnmerge; |
| 2125 | |
| 2126 | support = domain->iommu_superpage; |
| 2127 | |
| 2128 | /* To use a large page, the virtual *and* physical addresses |
| 2129 | must be aligned to 2MiB/1GiB/etc. Lower bits set in either |
| 2130 | of them will mean we have to use smaller pages. So just |
| 2131 | merge them and check both at once. */ |
| 2132 | pfnmerge = iov_pfn | phy_pfn; |
| 2133 | |
| 2134 | while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) { |
| 2135 | pages >>= VTD_STRIDE_SHIFT; |
| 2136 | if (!pages) |
| 2137 | break; |
| 2138 | pfnmerge >>= VTD_STRIDE_SHIFT; |
| 2139 | level++; |
| 2140 | support--; |
| 2141 | } |
| 2142 | return level; |
| 2143 | } |
| 2144 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2145 | static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2146 | struct scatterlist *sg, unsigned long phys_pfn, |
| 2147 | unsigned long nr_pages, int prot) |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2148 | { |
| 2149 | struct dma_pte *first_pte = NULL, *pte = NULL; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2150 | phys_addr_t uninitialized_var(pteval); |
Jiang Liu | cc4f14a | 2014-11-26 09:42:10 +0800 | [diff] [blame] | 2151 | unsigned long sg_res = 0; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2152 | unsigned int largepage_lvl = 0; |
| 2153 | unsigned long lvl_pages = 0; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2154 | |
Jiang Liu | 162d1b1 | 2014-07-11 14:19:35 +0800 | [diff] [blame] | 2155 | BUG_ON(!domain_pfn_supported(domain, iov_pfn + nr_pages - 1)); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2156 | |
| 2157 | if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) |
| 2158 | return -EINVAL; |
| 2159 | |
| 2160 | prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP; |
| 2161 | |
Jiang Liu | cc4f14a | 2014-11-26 09:42:10 +0800 | [diff] [blame] | 2162 | if (!sg) { |
| 2163 | sg_res = nr_pages; |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2164 | pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot; |
| 2165 | } |
| 2166 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2167 | while (nr_pages > 0) { |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 2168 | uint64_t tmp; |
| 2169 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2170 | if (!sg_res) { |
Robin Murphy | 29a90b7 | 2017-09-28 15:14:01 +0100 | [diff] [blame] | 2171 | unsigned int pgoff = sg->offset & ~PAGE_MASK; |
| 2172 | |
Fenghua Yu | f532959 | 2009-08-04 15:09:37 -0700 | [diff] [blame] | 2173 | sg_res = aligned_nrpages(sg->offset, sg->length); |
Robin Murphy | 29a90b7 | 2017-09-28 15:14:01 +0100 | [diff] [blame] | 2174 | sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + pgoff; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2175 | sg->dma_length = sg->length; |
Robin Murphy | 29a90b7 | 2017-09-28 15:14:01 +0100 | [diff] [blame] | 2176 | pteval = (sg_phys(sg) - pgoff) | prot; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2177 | phys_pfn = pteval >> VTD_PAGE_SHIFT; |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2178 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2179 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2180 | if (!pte) { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2181 | largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res); |
| 2182 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 2183 | first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl); |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2184 | if (!pte) |
| 2185 | return -ENOMEM; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2186 | /* It is large page*/ |
Woodhouse, David | 6491d4d | 2012-12-19 13:25:35 +0000 | [diff] [blame] | 2187 | if (largepage_lvl > 1) { |
Christian Zander | ba2374f | 2015-06-10 09:41:45 -0700 | [diff] [blame] | 2188 | unsigned long nr_superpages, end_pfn; |
| 2189 | |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2190 | pteval |= DMA_PTE_LARGE_PAGE; |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 2191 | lvl_pages = lvl_to_nr_pages(largepage_lvl); |
Christian Zander | ba2374f | 2015-06-10 09:41:45 -0700 | [diff] [blame] | 2192 | |
| 2193 | nr_superpages = sg_res / lvl_pages; |
| 2194 | end_pfn = iov_pfn + nr_superpages * lvl_pages - 1; |
| 2195 | |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 2196 | /* |
| 2197 | * Ensure that old small page tables are |
Christian Zander | ba2374f | 2015-06-10 09:41:45 -0700 | [diff] [blame] | 2198 | * removed to make room for superpage(s). |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 2199 | * We're adding new large pages, so make sure |
| 2200 | * we don't remove their parent tables. |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 2201 | */ |
David Dillow | bc24c57 | 2017-06-28 19:42:23 -0700 | [diff] [blame] | 2202 | dma_pte_free_pagetable(domain, iov_pfn, end_pfn, |
| 2203 | largepage_lvl + 1); |
Woodhouse, David | 6491d4d | 2012-12-19 13:25:35 +0000 | [diff] [blame] | 2204 | } else { |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2205 | pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE; |
Woodhouse, David | 6491d4d | 2012-12-19 13:25:35 +0000 | [diff] [blame] | 2206 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2207 | |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2208 | } |
| 2209 | /* We don't need lock here, nobody else |
| 2210 | * touches the iova range |
| 2211 | */ |
David Woodhouse | 7766a3f | 2009-07-01 20:27:03 +0100 | [diff] [blame] | 2212 | tmp = cmpxchg64_local(&pte->val, 0ULL, pteval); |
David Woodhouse | c85994e | 2009-07-01 19:21:24 +0100 | [diff] [blame] | 2213 | if (tmp) { |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 2214 | static int dumps = 5; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2215 | pr_crit("ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n", |
| 2216 | iov_pfn, tmp, (unsigned long long)pteval); |
David Woodhouse | 1bf20f0 | 2009-06-29 22:06:43 +0100 | [diff] [blame] | 2217 | if (dumps) { |
| 2218 | dumps--; |
| 2219 | debug_dma_dump_mappings(NULL); |
| 2220 | } |
| 2221 | WARN_ON(1); |
| 2222 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2223 | |
| 2224 | lvl_pages = lvl_to_nr_pages(largepage_lvl); |
| 2225 | |
| 2226 | BUG_ON(nr_pages < lvl_pages); |
| 2227 | BUG_ON(sg_res < lvl_pages); |
| 2228 | |
| 2229 | nr_pages -= lvl_pages; |
| 2230 | iov_pfn += lvl_pages; |
| 2231 | phys_pfn += lvl_pages; |
| 2232 | pteval += lvl_pages * VTD_PAGE_SIZE; |
| 2233 | sg_res -= lvl_pages; |
| 2234 | |
| 2235 | /* If the next PTE would be the first in a new page, then we |
| 2236 | need to flush the cache on the entries we've just written. |
| 2237 | And then we'll need to recalculate 'pte', so clear it and |
| 2238 | let it get set again in the if (!pte) block above. |
| 2239 | |
| 2240 | If we're done (!nr_pages) we need to flush the cache too. |
| 2241 | |
| 2242 | Also if we've been setting superpages, we may need to |
| 2243 | recalculate 'pte' and switch back to smaller pages for the |
| 2244 | end of the mapping, if the trailing size is not enough to |
| 2245 | use another superpage (i.e. sg_res < lvl_pages). */ |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2246 | pte++; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2247 | if (!nr_pages || first_pte_in_page(pte) || |
| 2248 | (largepage_lvl > 1 && sg_res < lvl_pages)) { |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2249 | domain_flush_cache(domain, first_pte, |
| 2250 | (void *)pte - (void *)first_pte); |
| 2251 | pte = NULL; |
| 2252 | } |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 2253 | |
| 2254 | if (!sg_res && nr_pages) |
David Woodhouse | e160549 | 2009-06-29 11:17:38 +0100 | [diff] [blame] | 2255 | sg = sg_next(sg); |
| 2256 | } |
| 2257 | return 0; |
| 2258 | } |
| 2259 | |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2260 | static int domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2261 | struct scatterlist *sg, unsigned long phys_pfn, |
| 2262 | unsigned long nr_pages, int prot) |
| 2263 | { |
| 2264 | int ret; |
| 2265 | struct intel_iommu *iommu; |
| 2266 | |
| 2267 | /* Do the real mapping first */ |
| 2268 | ret = __domain_mapping(domain, iov_pfn, sg, phys_pfn, nr_pages, prot); |
| 2269 | if (ret) |
| 2270 | return ret; |
| 2271 | |
| 2272 | /* Notify about the new mapping */ |
| 2273 | if (domain_type_is_vm(domain)) { |
| 2274 | /* VM typed domains can have more than one IOMMUs */ |
| 2275 | int iommu_id; |
| 2276 | for_each_domain_iommu(iommu_id, domain) { |
| 2277 | iommu = g_iommus[iommu_id]; |
| 2278 | __mapping_notify_one(iommu, domain, iov_pfn, nr_pages); |
| 2279 | } |
| 2280 | } else { |
| 2281 | /* General domains only have one IOMMU */ |
| 2282 | iommu = domain_get_iommu(domain); |
| 2283 | __mapping_notify_one(iommu, domain, iov_pfn, nr_pages); |
| 2284 | } |
| 2285 | |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2289 | static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2290 | struct scatterlist *sg, unsigned long nr_pages, |
| 2291 | int prot) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2292 | { |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2293 | return domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot); |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2294 | } |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 2295 | |
David Woodhouse | 9051aa0 | 2009-06-29 12:30:54 +0100 | [diff] [blame] | 2296 | static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn, |
| 2297 | unsigned long phys_pfn, unsigned long nr_pages, |
| 2298 | int prot) |
| 2299 | { |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2300 | 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] | 2301 | } |
| 2302 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 2303 | 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] | 2304 | { |
Filippo Sironi | 5082219 | 2017-08-31 10:58:11 +0200 | [diff] [blame] | 2305 | unsigned long flags; |
| 2306 | struct context_entry *context; |
| 2307 | u16 did_old; |
| 2308 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 2309 | if (!iommu) |
| 2310 | return; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 2311 | |
Filippo Sironi | 5082219 | 2017-08-31 10:58:11 +0200 | [diff] [blame] | 2312 | spin_lock_irqsave(&iommu->lock, flags); |
| 2313 | context = iommu_context_addr(iommu, bus, devfn, 0); |
| 2314 | if (!context) { |
| 2315 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 2316 | return; |
| 2317 | } |
| 2318 | did_old = context_domain_id(context); |
| 2319 | context_clear_entry(context); |
| 2320 | __iommu_flush_cache(iommu, context, sizeof(*context)); |
| 2321 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 2322 | iommu->flush.flush_context(iommu, |
| 2323 | did_old, |
| 2324 | (((u16)bus) << 8) | devfn, |
| 2325 | DMA_CCMD_MASK_NOBIT, |
| 2326 | DMA_CCMD_DEVICE_INVL); |
| 2327 | iommu->flush.flush_iotlb(iommu, |
| 2328 | did_old, |
| 2329 | 0, |
| 2330 | 0, |
| 2331 | DMA_TLB_DSI_FLUSH); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
David Woodhouse | 109b9b0 | 2012-05-25 17:43:02 +0100 | [diff] [blame] | 2334 | static inline void unlink_domain_info(struct device_domain_info *info) |
| 2335 | { |
| 2336 | assert_spin_locked(&device_domain_lock); |
| 2337 | list_del(&info->link); |
| 2338 | list_del(&info->global); |
| 2339 | if (info->dev) |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 2340 | info->dev->archdata.iommu = NULL; |
David Woodhouse | 109b9b0 | 2012-05-25 17:43:02 +0100 | [diff] [blame] | 2341 | } |
| 2342 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2343 | static void domain_remove_dev_info(struct dmar_domain *domain) |
| 2344 | { |
Yijing Wang | 3a74ca0 | 2014-05-20 20:37:47 +0800 | [diff] [blame] | 2345 | struct device_domain_info *info, *tmp; |
Jiang Liu | fb170fb | 2014-07-11 14:19:28 +0800 | [diff] [blame] | 2346 | unsigned long flags; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2347 | |
| 2348 | spin_lock_irqsave(&device_domain_lock, flags); |
Joerg Roedel | 76f45fe | 2015-07-21 18:25:11 +0200 | [diff] [blame] | 2349 | list_for_each_entry_safe(info, tmp, &domain->devices, link) |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 2350 | __dmar_remove_one_dev_info(info); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2351 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2352 | } |
| 2353 | |
| 2354 | /* |
| 2355 | * find_domain |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 2356 | * Note: we use struct device->archdata.iommu stores the info |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2357 | */ |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 2358 | static struct dmar_domain *find_domain(struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2359 | { |
| 2360 | struct device_domain_info *info; |
| 2361 | |
| 2362 | /* No lock here, assumes no domain exit in normal case */ |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 2363 | info = dev->archdata.iommu; |
Peter Xu | b316d02 | 2017-05-22 18:28:51 +0800 | [diff] [blame] | 2364 | if (likely(info)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2365 | return info->domain; |
| 2366 | return NULL; |
| 2367 | } |
| 2368 | |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2369 | static inline struct device_domain_info * |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2370 | dmar_search_domain_by_dev_info(int segment, int bus, int devfn) |
| 2371 | { |
| 2372 | struct device_domain_info *info; |
| 2373 | |
| 2374 | list_for_each_entry(info, &device_domain_list, global) |
David Woodhouse | 41e80dca | 2014-03-09 13:55:54 -0700 | [diff] [blame] | 2375 | if (info->iommu->segment == segment && info->bus == bus && |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2376 | info->devfn == devfn) |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2377 | return info; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2378 | |
| 2379 | return NULL; |
| 2380 | } |
| 2381 | |
Joerg Roedel | 5db3156 | 2015-07-22 12:40:43 +0200 | [diff] [blame] | 2382 | static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu, |
| 2383 | int bus, int devfn, |
| 2384 | struct device *dev, |
| 2385 | struct dmar_domain *domain) |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2386 | { |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2387 | struct dmar_domain *found = NULL; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2388 | struct device_domain_info *info; |
| 2389 | unsigned long flags; |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 2390 | int ret; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2391 | |
| 2392 | info = alloc_devinfo_mem(); |
| 2393 | if (!info) |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2394 | return NULL; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2395 | |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2396 | info->bus = bus; |
| 2397 | info->devfn = devfn; |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2398 | info->ats_supported = info->pasid_supported = info->pri_supported = 0; |
| 2399 | info->ats_enabled = info->pasid_enabled = info->pri_enabled = 0; |
| 2400 | info->ats_qdep = 0; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2401 | info->dev = dev; |
| 2402 | info->domain = domain; |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2403 | info->iommu = iommu; |
Lu Baolu | cc580e4 | 2018-07-14 15:46:59 +0800 | [diff] [blame] | 2404 | info->pasid_table = NULL; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2405 | |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2406 | if (dev && dev_is_pci(dev)) { |
| 2407 | struct pci_dev *pdev = to_pci_dev(info->dev); |
| 2408 | |
Gil Kupfer | cef7440 | 2018-05-10 17:56:02 -0500 | [diff] [blame] | 2409 | if (!pci_ats_disabled() && |
| 2410 | ecap_dev_iotlb_support(iommu->ecap) && |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2411 | pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_ATS) && |
| 2412 | dmar_find_matched_atsr_unit(pdev)) |
| 2413 | info->ats_supported = 1; |
| 2414 | |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 2415 | if (sm_supported(iommu)) { |
| 2416 | if (pasid_supported(iommu)) { |
David Woodhouse | b16d0cb | 2015-10-12 14:17:37 +0100 | [diff] [blame] | 2417 | int features = pci_pasid_features(pdev); |
| 2418 | if (features >= 0) |
| 2419 | info->pasid_supported = features | 1; |
| 2420 | } |
| 2421 | |
| 2422 | if (info->ats_supported && ecap_prs(iommu->ecap) && |
| 2423 | pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_PRI)) |
| 2424 | info->pri_supported = 1; |
| 2425 | } |
| 2426 | } |
| 2427 | |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2428 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2429 | if (dev) |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 2430 | found = find_domain(dev); |
Joerg Roedel | f303e50 | 2015-07-23 18:37:13 +0200 | [diff] [blame] | 2431 | |
| 2432 | if (!found) { |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2433 | struct device_domain_info *info2; |
David Woodhouse | 41e80dca | 2014-03-09 13:55:54 -0700 | [diff] [blame] | 2434 | info2 = dmar_search_domain_by_dev_info(iommu->segment, bus, devfn); |
Joerg Roedel | f303e50 | 2015-07-23 18:37:13 +0200 | [diff] [blame] | 2435 | if (info2) { |
| 2436 | found = info2->domain; |
| 2437 | info2->dev = dev; |
| 2438 | } |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2439 | } |
Joerg Roedel | f303e50 | 2015-07-23 18:37:13 +0200 | [diff] [blame] | 2440 | |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2441 | if (found) { |
| 2442 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2443 | free_devinfo_mem(info); |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2444 | /* Caller must free the original domain */ |
| 2445 | return found; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2446 | } |
| 2447 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 2448 | spin_lock(&iommu->lock); |
| 2449 | ret = domain_attach_iommu(domain, iommu); |
| 2450 | spin_unlock(&iommu->lock); |
| 2451 | |
| 2452 | if (ret) { |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2453 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Sudip Mukherjee | 499f3aa | 2015-09-18 16:27:07 +0530 | [diff] [blame] | 2454 | free_devinfo_mem(info); |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2455 | return NULL; |
| 2456 | } |
Joerg Roedel | c6c2ceb | 2015-07-22 13:11:53 +0200 | [diff] [blame] | 2457 | |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2458 | list_add(&info->link, &domain->devices); |
| 2459 | list_add(&info->global, &device_domain_list); |
| 2460 | if (dev) |
| 2461 | dev->archdata.iommu = info; |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 2462 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Lu Baolu | a7fc93f | 2018-07-14 15:47:00 +0800 | [diff] [blame] | 2463 | |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 2464 | /* PASID table is mandatory for a PCI device in scalable mode. */ |
| 2465 | if (dev && dev_is_pci(dev) && sm_supported(iommu)) { |
Lu Baolu | a7fc93f | 2018-07-14 15:47:00 +0800 | [diff] [blame] | 2466 | ret = intel_pasid_alloc_table(dev); |
| 2467 | if (ret) { |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 2468 | pr_err("PASID table allocation for %s failed\n", |
| 2469 | dev_name(dev)); |
| 2470 | dmar_remove_one_dev_info(domain, dev); |
| 2471 | return NULL; |
Lu Baolu | a7fc93f | 2018-07-14 15:47:00 +0800 | [diff] [blame] | 2472 | } |
Lu Baolu | ef848b7 | 2018-12-10 09:59:01 +0800 | [diff] [blame] | 2473 | |
| 2474 | /* Setup the PASID entry for requests without PASID: */ |
| 2475 | spin_lock(&iommu->lock); |
| 2476 | if (hw_pass_through && domain_type_is_si(domain)) |
| 2477 | ret = intel_pasid_setup_pass_through(iommu, domain, |
| 2478 | dev, PASID_RID2PASID); |
| 2479 | else |
| 2480 | ret = intel_pasid_setup_second_level(iommu, domain, |
| 2481 | dev, PASID_RID2PASID); |
| 2482 | spin_unlock(&iommu->lock); |
| 2483 | if (ret) { |
| 2484 | pr_err("Setup RID2PASID for %s failed\n", |
| 2485 | dev_name(dev)); |
| 2486 | dmar_remove_one_dev_info(domain, dev); |
| 2487 | return NULL; |
| 2488 | } |
Lu Baolu | a7fc93f | 2018-07-14 15:47:00 +0800 | [diff] [blame] | 2489 | } |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2490 | |
Joerg Roedel | cc4e257 | 2015-07-22 10:04:36 +0200 | [diff] [blame] | 2491 | if (dev && domain_context_mapping(domain, dev)) { |
| 2492 | pr_err("Domain context map for %s failed\n", dev_name(dev)); |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 2493 | dmar_remove_one_dev_info(domain, dev); |
Joerg Roedel | cc4e257 | 2015-07-22 10:04:36 +0200 | [diff] [blame] | 2494 | return NULL; |
| 2495 | } |
| 2496 | |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2497 | return domain; |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2498 | } |
| 2499 | |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2500 | static int get_last_alias(struct pci_dev *pdev, u16 alias, void *opaque) |
| 2501 | { |
| 2502 | *(u16 *)opaque = alias; |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2506 | 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] | 2507 | { |
Joerg Roedel | cc4e257 | 2015-07-22 10:04:36 +0200 | [diff] [blame] | 2508 | struct device_domain_info *info = NULL; |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2509 | struct dmar_domain *domain = NULL; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2510 | struct intel_iommu *iommu; |
Lu Baolu | fcc35c6 | 2018-05-04 13:08:17 +0800 | [diff] [blame] | 2511 | u16 dma_alias; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2512 | unsigned long flags; |
Yijing Wang | aa4d066 | 2014-05-26 20:14:06 +0800 | [diff] [blame] | 2513 | u8 bus, devfn; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2514 | |
David Woodhouse | 146922e | 2014-03-09 15:44:17 -0700 | [diff] [blame] | 2515 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 2516 | if (!iommu) |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2517 | return NULL; |
| 2518 | |
| 2519 | if (dev_is_pci(dev)) { |
| 2520 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2521 | |
| 2522 | pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias); |
| 2523 | |
| 2524 | spin_lock_irqsave(&device_domain_lock, flags); |
| 2525 | info = dmar_search_domain_by_dev_info(pci_domain_nr(pdev->bus), |
| 2526 | PCI_BUS_NUM(dma_alias), |
| 2527 | dma_alias & 0xff); |
| 2528 | if (info) { |
| 2529 | iommu = info->iommu; |
| 2530 | domain = info->domain; |
| 2531 | } |
| 2532 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 2533 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2534 | /* DMA alias already has a domain, use it */ |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2535 | if (info) |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2536 | goto out; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2537 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2538 | |
David Woodhouse | 146922e | 2014-03-09 15:44:17 -0700 | [diff] [blame] | 2539 | /* Allocate and initialize new domain for the device */ |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 2540 | domain = alloc_domain(0); |
Jiang Liu | 745f258 | 2014-02-19 14:07:26 +0800 | [diff] [blame] | 2541 | if (!domain) |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2542 | return NULL; |
Joerg Roedel | dc534b2 | 2015-07-22 12:44:02 +0200 | [diff] [blame] | 2543 | if (domain_init(domain, iommu, gaw)) { |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2544 | domain_exit(domain); |
| 2545 | return NULL; |
| 2546 | } |
| 2547 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2548 | out: |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2549 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2550 | return domain; |
| 2551 | } |
| 2552 | |
| 2553 | static struct dmar_domain *set_domain_for_dev(struct device *dev, |
| 2554 | struct dmar_domain *domain) |
| 2555 | { |
| 2556 | struct intel_iommu *iommu; |
| 2557 | struct dmar_domain *tmp; |
| 2558 | u16 req_id, dma_alias; |
| 2559 | u8 bus, devfn; |
| 2560 | |
| 2561 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 2562 | if (!iommu) |
| 2563 | return NULL; |
| 2564 | |
| 2565 | req_id = ((u16)bus << 8) | devfn; |
| 2566 | |
| 2567 | if (dev_is_pci(dev)) { |
| 2568 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2569 | |
| 2570 | pci_for_each_dma_alias(pdev, get_last_alias, &dma_alias); |
| 2571 | |
| 2572 | /* register PCI DMA alias device */ |
| 2573 | if (req_id != dma_alias) { |
| 2574 | tmp = dmar_insert_one_dev_info(iommu, PCI_BUS_NUM(dma_alias), |
| 2575 | dma_alias & 0xff, NULL, domain); |
| 2576 | |
| 2577 | if (!tmp || tmp != domain) |
| 2578 | return tmp; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2579 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
Joerg Roedel | 5db3156 | 2015-07-22 12:40:43 +0200 | [diff] [blame] | 2582 | tmp = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain); |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2583 | if (!tmp || tmp != domain) |
| 2584 | return tmp; |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2585 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2586 | return domain; |
| 2587 | } |
| 2588 | |
| 2589 | static struct dmar_domain *get_domain_for_dev(struct device *dev, int gaw) |
| 2590 | { |
| 2591 | struct dmar_domain *domain, *tmp; |
| 2592 | |
| 2593 | domain = find_domain(dev); |
| 2594 | if (domain) |
| 2595 | goto out; |
| 2596 | |
| 2597 | domain = find_or_alloc_domain(dev, gaw); |
| 2598 | if (!domain) |
| 2599 | goto out; |
| 2600 | |
| 2601 | tmp = set_domain_for_dev(dev, domain); |
| 2602 | if (!tmp || domain != tmp) { |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 2603 | domain_exit(domain); |
| 2604 | domain = tmp; |
| 2605 | } |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2606 | |
Joerg Roedel | 7620835 | 2016-08-25 14:25:12 +0200 | [diff] [blame] | 2607 | out: |
| 2608 | |
David Woodhouse | b718cd3 | 2014-03-09 13:11:33 -0700 | [diff] [blame] | 2609 | return domain; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2612 | static int iommu_domain_identity_map(struct dmar_domain *domain, |
| 2613 | unsigned long long start, |
| 2614 | unsigned long long end) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2615 | { |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2616 | unsigned long first_vpfn = start >> VTD_PAGE_SHIFT; |
| 2617 | unsigned long last_vpfn = end >> VTD_PAGE_SHIFT; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2618 | |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2619 | if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn), |
| 2620 | dma_to_mm_pfn(last_vpfn))) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2621 | pr_err("Reserving iova failed\n"); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2622 | return -ENOMEM; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
Joerg Roedel | af1089c | 2015-07-21 15:45:19 +0200 | [diff] [blame] | 2625 | pr_debug("Mapping reserved region %llx-%llx\n", start, end); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2626 | /* |
| 2627 | * RMRR range might have overlap with physical memory range, |
| 2628 | * clear it first |
| 2629 | */ |
David Woodhouse | c5395d5 | 2009-06-28 16:35:56 +0100 | [diff] [blame] | 2630 | dma_pte_clear_range(domain, first_vpfn, last_vpfn); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2631 | |
Peter Xu | 87684fd | 2018-05-04 10:34:53 +0800 | [diff] [blame] | 2632 | return __domain_mapping(domain, first_vpfn, NULL, |
| 2633 | first_vpfn, last_vpfn - first_vpfn + 1, |
| 2634 | DMA_PTE_READ|DMA_PTE_WRITE); |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2635 | } |
| 2636 | |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2637 | static int domain_prepare_identity_map(struct device *dev, |
| 2638 | struct dmar_domain *domain, |
| 2639 | unsigned long long start, |
| 2640 | unsigned long long end) |
David Woodhouse | b213203 | 2009-06-26 18:50:28 +0100 | [diff] [blame] | 2641 | { |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2642 | /* For _hardware_ passthrough, don't bother. But for software |
| 2643 | passthrough, we do it anyway -- it may indicate a memory |
| 2644 | range which is reserved in E820, so which didn't get set |
| 2645 | up to start with in si_domain */ |
| 2646 | if (domain == si_domain && hw_pass_through) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2647 | pr_warn("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n", |
| 2648 | dev_name(dev), start, end); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2649 | return 0; |
| 2650 | } |
| 2651 | |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2652 | pr_info("Setting identity map for device %s [0x%Lx - 0x%Lx]\n", |
| 2653 | dev_name(dev), start, end); |
| 2654 | |
David Woodhouse | 5595b52 | 2009-12-02 09:21:55 +0000 | [diff] [blame] | 2655 | if (end < start) { |
| 2656 | WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n" |
| 2657 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 2658 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 2659 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 2660 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2661 | return -EIO; |
David Woodhouse | 5595b52 | 2009-12-02 09:21:55 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
David Woodhouse | 2ff729f | 2009-08-26 14:25:41 +0100 | [diff] [blame] | 2664 | if (end >> agaw_to_width(domain->agaw)) { |
| 2665 | WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n" |
| 2666 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 2667 | agaw_to_width(domain->agaw), |
| 2668 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 2669 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 2670 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2671 | return -EIO; |
David Woodhouse | 2ff729f | 2009-08-26 14:25:41 +0100 | [diff] [blame] | 2672 | } |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2673 | |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2674 | return iommu_domain_identity_map(domain, start, end); |
| 2675 | } |
| 2676 | |
| 2677 | static int iommu_prepare_identity_map(struct device *dev, |
| 2678 | unsigned long long start, |
| 2679 | unsigned long long end) |
| 2680 | { |
| 2681 | struct dmar_domain *domain; |
| 2682 | int ret; |
| 2683 | |
| 2684 | domain = get_domain_for_dev(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 2685 | if (!domain) |
| 2686 | return -ENOMEM; |
| 2687 | |
| 2688 | ret = domain_prepare_identity_map(dev, domain, start, end); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2689 | if (ret) |
Joerg Roedel | d66ce54 | 2015-09-23 19:00:10 +0200 | [diff] [blame] | 2690 | domain_exit(domain); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2691 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2692 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr, |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2696 | struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2697 | { |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2698 | if (dev->archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2699 | return 0; |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2700 | return iommu_prepare_identity_map(dev, rmrr->base_address, |
| 2701 | rmrr->end_address); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 2704 | #ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2705 | static inline void iommu_prepare_isa(void) |
| 2706 | { |
| 2707 | struct pci_dev *pdev; |
| 2708 | int ret; |
| 2709 | |
| 2710 | pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL); |
| 2711 | if (!pdev) |
| 2712 | return; |
| 2713 | |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2714 | pr_info("Prepare 0-16MiB unity mapping for LPC\n"); |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2715 | 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] | 2716 | |
| 2717 | if (ret) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2718 | 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] | 2719 | |
Yijing Wang | 9b27e82 | 2014-05-20 20:37:52 +0800 | [diff] [blame] | 2720 | pci_dev_put(pdev); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2721 | } |
| 2722 | #else |
| 2723 | static inline void iommu_prepare_isa(void) |
| 2724 | { |
| 2725 | return; |
| 2726 | } |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 2727 | #endif /* !CONFIG_INTEL_IOMMU_FLPY_WA */ |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 2728 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2729 | static int md_domain_init(struct dmar_domain *domain, int guest_width); |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2730 | |
Matt Kraai | 071e137 | 2009-08-23 22:30:22 -0700 | [diff] [blame] | 2731 | static int __init si_domain_init(int hw) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2732 | { |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2733 | int nid, ret = 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2734 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 2735 | si_domain = alloc_domain(DOMAIN_FLAG_STATIC_IDENTITY); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2736 | if (!si_domain) |
| 2737 | return -EFAULT; |
| 2738 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2739 | if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
| 2740 | domain_exit(si_domain); |
| 2741 | return -EFAULT; |
| 2742 | } |
| 2743 | |
Joerg Roedel | 0dc7971 | 2015-07-21 15:40:06 +0200 | [diff] [blame] | 2744 | pr_debug("Identity mapping domain allocated\n"); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2745 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 2746 | if (hw) |
| 2747 | return 0; |
| 2748 | |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2749 | for_each_online_node(nid) { |
Tejun Heo | d4bbf7e | 2011-11-28 09:46:22 -0800 | [diff] [blame] | 2750 | unsigned long start_pfn, end_pfn; |
| 2751 | int i; |
| 2752 | |
| 2753 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { |
| 2754 | ret = iommu_domain_identity_map(si_domain, |
| 2755 | PFN_PHYS(start_pfn), PFN_PHYS(end_pfn)); |
| 2756 | if (ret) |
| 2757 | return ret; |
| 2758 | } |
David Woodhouse | c7ab48d | 2009-06-26 19:10:36 +0100 | [diff] [blame] | 2759 | } |
| 2760 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2761 | return 0; |
| 2762 | } |
| 2763 | |
David Woodhouse | 9b22662 | 2014-03-09 14:03:28 -0700 | [diff] [blame] | 2764 | static int identity_mapping(struct device *dev) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2765 | { |
| 2766 | struct device_domain_info *info; |
| 2767 | |
| 2768 | if (likely(!iommu_identity_mapping)) |
| 2769 | return 0; |
| 2770 | |
David Woodhouse | 9b22662 | 2014-03-09 14:03:28 -0700 | [diff] [blame] | 2771 | info = dev->archdata.iommu; |
Mike Travis | cb452a4 | 2011-05-28 13:15:03 -0500 | [diff] [blame] | 2772 | if (info && info != DUMMY_DEVICE_DOMAIN_INFO) |
| 2773 | return (info->domain == si_domain); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2774 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2775 | return 0; |
| 2776 | } |
| 2777 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2778 | 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] | 2779 | { |
David Woodhouse | 0ac7266 | 2014-03-09 13:19:22 -0700 | [diff] [blame] | 2780 | struct dmar_domain *ndomain; |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2781 | struct intel_iommu *iommu; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 2782 | u8 bus, devfn; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2783 | |
David Woodhouse | 5913c9b | 2014-03-09 16:27:31 -0700 | [diff] [blame] | 2784 | iommu = device_to_iommu(dev, &bus, &devfn); |
David Woodhouse | 5a8f40e | 2014-03-09 13:31:18 -0700 | [diff] [blame] | 2785 | if (!iommu) |
| 2786 | return -ENODEV; |
| 2787 | |
Joerg Roedel | 5db3156 | 2015-07-22 12:40:43 +0200 | [diff] [blame] | 2788 | ndomain = dmar_insert_one_dev_info(iommu, bus, devfn, dev, domain); |
David Woodhouse | 0ac7266 | 2014-03-09 13:19:22 -0700 | [diff] [blame] | 2789 | if (ndomain != domain) |
| 2790 | return -EBUSY; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2791 | |
| 2792 | return 0; |
| 2793 | } |
| 2794 | |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2795 | static bool device_has_rmrr(struct device *dev) |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2796 | { |
| 2797 | struct dmar_rmrr_unit *rmrr; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 2798 | struct device *tmp; |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2799 | int i; |
| 2800 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 2801 | rcu_read_lock(); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2802 | for_each_rmrr_units(rmrr) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 2803 | /* |
| 2804 | * Return TRUE if this RMRR contains the device that |
| 2805 | * is passed in. |
| 2806 | */ |
| 2807 | for_each_active_dev_scope(rmrr->devices, |
| 2808 | rmrr->devices_cnt, i, tmp) |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 2809 | if (tmp == dev) { |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 2810 | rcu_read_unlock(); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2811 | return true; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 2812 | } |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2813 | } |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 2814 | rcu_read_unlock(); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2815 | return false; |
| 2816 | } |
| 2817 | |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2818 | /* |
| 2819 | * There are a couple cases where we need to restrict the functionality of |
| 2820 | * devices associated with RMRRs. The first is when evaluating a device for |
| 2821 | * identity mapping because problems exist when devices are moved in and out |
| 2822 | * of domains and their respective RMRR information is lost. This means that |
| 2823 | * a device with associated RMRRs will never be in a "passthrough" domain. |
| 2824 | * The second is use of the device through the IOMMU API. This interface |
| 2825 | * expects to have full control of the IOVA space for the device. We cannot |
| 2826 | * satisfy both the requirement that RMRR access is maintained and have an |
| 2827 | * unencumbered IOVA space. We also have no ability to quiesce the device's |
| 2828 | * use of the RMRR space or even inform the IOMMU API user of the restriction. |
| 2829 | * We therefore prevent devices associated with an RMRR from participating in |
| 2830 | * the IOMMU API, which eliminates them from device assignment. |
| 2831 | * |
| 2832 | * In both cases we assume that PCI USB devices with RMRRs have them largely |
| 2833 | * for historical reasons and that the RMRR space is not actively used post |
| 2834 | * boot. This exclusion may change if vendors begin to abuse it. |
David Woodhouse | 18436af | 2015-03-25 15:05:47 +0000 | [diff] [blame] | 2835 | * |
| 2836 | * The same exception is made for graphics devices, with the requirement that |
| 2837 | * any use of the RMRR regions will be torn down before assigning the device |
| 2838 | * to a guest. |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2839 | */ |
| 2840 | static bool device_is_rmrr_locked(struct device *dev) |
| 2841 | { |
| 2842 | if (!device_has_rmrr(dev)) |
| 2843 | return false; |
| 2844 | |
| 2845 | if (dev_is_pci(dev)) { |
| 2846 | struct pci_dev *pdev = to_pci_dev(dev); |
| 2847 | |
David Woodhouse | 18436af | 2015-03-25 15:05:47 +0000 | [diff] [blame] | 2848 | if (IS_USB_DEVICE(pdev) || IS_GFX_DEVICE(pdev)) |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2849 | return false; |
| 2850 | } |
| 2851 | |
| 2852 | return true; |
| 2853 | } |
| 2854 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2855 | static int iommu_should_identity_map(struct device *dev, int startup) |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2856 | { |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2857 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2858 | if (dev_is_pci(dev)) { |
| 2859 | struct pci_dev *pdev = to_pci_dev(dev); |
Tom Mingarelli | ea2447f7 | 2012-11-20 19:43:17 +0000 | [diff] [blame] | 2860 | |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 2861 | if (device_is_rmrr_locked(dev)) |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2862 | return 0; |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2863 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2864 | if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev)) |
| 2865 | return 1; |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 2866 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2867 | if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev)) |
| 2868 | return 1; |
| 2869 | |
| 2870 | if (!(iommu_identity_mapping & IDENTMAP_ALL)) |
| 2871 | return 0; |
| 2872 | |
| 2873 | /* |
| 2874 | * We want to start off with all devices in the 1:1 domain, and |
| 2875 | * take them out later if we find they can't access all of memory. |
| 2876 | * |
| 2877 | * However, we can't do this for PCI devices behind bridges, |
| 2878 | * because all PCI devices behind the same bridge will end up |
| 2879 | * with the same source-id on their transactions. |
| 2880 | * |
| 2881 | * Practically speaking, we can't change things around for these |
| 2882 | * devices at run-time, because we can't be sure there'll be no |
| 2883 | * DMA transactions in flight for any of their siblings. |
| 2884 | * |
| 2885 | * So PCI devices (unless they're on the root bus) as well as |
| 2886 | * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of |
| 2887 | * the 1:1 domain, just in _case_ one of their siblings turns out |
| 2888 | * not to be able to map all of memory. |
| 2889 | */ |
| 2890 | if (!pci_is_pcie(pdev)) { |
| 2891 | if (!pci_is_root_bus(pdev->bus)) |
| 2892 | return 0; |
| 2893 | if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI) |
| 2894 | return 0; |
| 2895 | } else if (pci_pcie_type(pdev) == PCI_EXP_TYPE_PCI_BRIDGE) |
| 2896 | return 0; |
| 2897 | } else { |
| 2898 | if (device_has_rmrr(dev)) |
| 2899 | return 0; |
| 2900 | } |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2901 | |
David Woodhouse | 3dfc813 | 2009-07-04 19:11:08 +0100 | [diff] [blame] | 2902 | /* |
David Woodhouse | 3dfc813 | 2009-07-04 19:11:08 +0100 | [diff] [blame] | 2903 | * 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] | 2904 | * 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] | 2905 | * take them out of the 1:1 domain later. |
| 2906 | */ |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2907 | if (!startup) { |
| 2908 | /* |
| 2909 | * If the device's dma_mask is less than the system's memory |
| 2910 | * size then this is not a candidate for identity mapping. |
| 2911 | */ |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2912 | u64 dma_mask = *dev->dma_mask; |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2913 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2914 | if (dev->coherent_dma_mask && |
| 2915 | dev->coherent_dma_mask < dma_mask) |
| 2916 | dma_mask = dev->coherent_dma_mask; |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2917 | |
David Woodhouse | 3bdb259 | 2014-03-09 16:03:08 -0700 | [diff] [blame] | 2918 | return dma_mask >= dma_get_required_mask(dev); |
Chris Wright | 8fcc537 | 2011-05-28 13:15:02 -0500 | [diff] [blame] | 2919 | } |
David Woodhouse | 6941af2 | 2009-07-04 18:24:27 +0100 | [diff] [blame] | 2920 | |
| 2921 | return 1; |
| 2922 | } |
| 2923 | |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2924 | static int __init dev_prepare_static_identity_mapping(struct device *dev, int hw) |
| 2925 | { |
| 2926 | int ret; |
| 2927 | |
| 2928 | if (!iommu_should_identity_map(dev, 1)) |
| 2929 | return 0; |
| 2930 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 2931 | ret = domain_add_dev_info(si_domain, dev); |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2932 | if (!ret) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 2933 | pr_info("%s identity mapping for device %s\n", |
| 2934 | hw ? "Hardware" : "Software", dev_name(dev)); |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2935 | else if (ret == -ENODEV) |
| 2936 | /* device not associated with an iommu */ |
| 2937 | ret = 0; |
| 2938 | |
| 2939 | return ret; |
| 2940 | } |
| 2941 | |
| 2942 | |
Matt Kraai | 071e137 | 2009-08-23 22:30:22 -0700 | [diff] [blame] | 2943 | static int __init iommu_prepare_static_identity_mapping(int hw) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2944 | { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2945 | struct pci_dev *pdev = NULL; |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2946 | struct dmar_drhd_unit *drhd; |
| 2947 | struct intel_iommu *iommu; |
| 2948 | struct device *dev; |
| 2949 | int i; |
| 2950 | int ret = 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2951 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2952 | for_each_pci_dev(pdev) { |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2953 | ret = dev_prepare_static_identity_mapping(&pdev->dev, hw); |
| 2954 | if (ret) |
| 2955 | return ret; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2958 | for_each_active_iommu(iommu, drhd) |
| 2959 | for_each_active_dev_scope(drhd->devices, drhd->devices_cnt, i, dev) { |
| 2960 | struct acpi_device_physical_node *pn; |
| 2961 | struct acpi_device *adev; |
| 2962 | |
| 2963 | if (dev->bus != &acpi_bus_type) |
| 2964 | continue; |
Joerg Roedel | 86080cc | 2015-06-12 12:27:16 +0200 | [diff] [blame] | 2965 | |
David Woodhouse | cf04eee | 2014-03-21 16:49:04 +0000 | [diff] [blame] | 2966 | adev= to_acpi_device(dev); |
| 2967 | mutex_lock(&adev->physical_node_lock); |
| 2968 | list_for_each_entry(pn, &adev->physical_node_list, node) { |
| 2969 | ret = dev_prepare_static_identity_mapping(pn->dev, hw); |
| 2970 | if (ret) |
| 2971 | break; |
| 2972 | } |
| 2973 | mutex_unlock(&adev->physical_node_lock); |
| 2974 | if (ret) |
| 2975 | return ret; |
| 2976 | } |
| 2977 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 2978 | return 0; |
| 2979 | } |
| 2980 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 2981 | static void intel_iommu_init_qi(struct intel_iommu *iommu) |
| 2982 | { |
| 2983 | /* |
| 2984 | * Start from the sane iommu hardware state. |
| 2985 | * If the queued invalidation is already initialized by us |
| 2986 | * (for example, while enabling interrupt-remapping) then |
| 2987 | * we got the things already rolling from a sane state. |
| 2988 | */ |
| 2989 | if (!iommu->qi) { |
| 2990 | /* |
| 2991 | * Clear any previous faults. |
| 2992 | */ |
| 2993 | dmar_fault(-1, iommu); |
| 2994 | /* |
| 2995 | * Disable queued invalidation if supported and already enabled |
| 2996 | * before OS handover. |
| 2997 | */ |
| 2998 | dmar_disable_qi(iommu); |
| 2999 | } |
| 3000 | |
| 3001 | if (dmar_enable_qi(iommu)) { |
| 3002 | /* |
| 3003 | * Queued Invalidate not enabled, use Register Based Invalidate |
| 3004 | */ |
| 3005 | iommu->flush.flush_context = __iommu_flush_context; |
| 3006 | iommu->flush.flush_iotlb = __iommu_flush_iotlb; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3007 | pr_info("%s: Using Register based invalidation\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3008 | iommu->name); |
| 3009 | } else { |
| 3010 | iommu->flush.flush_context = qi_flush_context; |
| 3011 | iommu->flush.flush_iotlb = qi_flush_iotlb; |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3012 | pr_info("%s: Using Queued invalidation\n", iommu->name); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3013 | } |
| 3014 | } |
| 3015 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3016 | static int copy_context_table(struct intel_iommu *iommu, |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3017 | struct root_entry *old_re, |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3018 | struct context_entry **tbl, |
| 3019 | int bus, bool ext) |
| 3020 | { |
Joerg Roedel | dbcd861 | 2015-06-12 12:02:09 +0200 | [diff] [blame] | 3021 | int tbl_idx, pos = 0, idx, devfn, ret = 0, did; |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3022 | struct context_entry *new_ce = NULL, ce; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3023 | struct context_entry *old_ce = NULL; |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3024 | struct root_entry re; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3025 | phys_addr_t old_ce_phys; |
| 3026 | |
| 3027 | tbl_idx = ext ? bus * 2 : bus; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3028 | memcpy(&re, old_re, sizeof(re)); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3029 | |
| 3030 | for (devfn = 0; devfn < 256; devfn++) { |
| 3031 | /* First calculate the correct index */ |
| 3032 | idx = (ext ? devfn * 2 : devfn) % 256; |
| 3033 | |
| 3034 | if (idx == 0) { |
| 3035 | /* First save what we may have and clean up */ |
| 3036 | if (new_ce) { |
| 3037 | tbl[tbl_idx] = new_ce; |
| 3038 | __iommu_flush_cache(iommu, new_ce, |
| 3039 | VTD_PAGE_SIZE); |
| 3040 | pos = 1; |
| 3041 | } |
| 3042 | |
| 3043 | if (old_ce) |
| 3044 | iounmap(old_ce); |
| 3045 | |
| 3046 | ret = 0; |
| 3047 | if (devfn < 0x80) |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3048 | old_ce_phys = root_entry_lctp(&re); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3049 | else |
Joerg Roedel | 543c8dc | 2015-08-13 11:56:59 +0200 | [diff] [blame] | 3050 | old_ce_phys = root_entry_uctp(&re); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3051 | |
| 3052 | if (!old_ce_phys) { |
| 3053 | if (ext && devfn == 0) { |
| 3054 | /* No LCTP, try UCTP */ |
| 3055 | devfn = 0x7f; |
| 3056 | continue; |
| 3057 | } else { |
| 3058 | goto out; |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | ret = -ENOMEM; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3063 | old_ce = memremap(old_ce_phys, PAGE_SIZE, |
| 3064 | MEMREMAP_WB); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3065 | if (!old_ce) |
| 3066 | goto out; |
| 3067 | |
| 3068 | new_ce = alloc_pgtable_page(iommu->node); |
| 3069 | if (!new_ce) |
| 3070 | goto out_unmap; |
| 3071 | |
| 3072 | ret = 0; |
| 3073 | } |
| 3074 | |
| 3075 | /* Now copy the context entry */ |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3076 | memcpy(&ce, old_ce + idx, sizeof(ce)); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3077 | |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 3078 | if (!__context_present(&ce)) |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3079 | continue; |
| 3080 | |
Joerg Roedel | dbcd861 | 2015-06-12 12:02:09 +0200 | [diff] [blame] | 3081 | did = context_domain_id(&ce); |
| 3082 | if (did >= 0 && did < cap_ndoms(iommu->cap)) |
| 3083 | set_bit(did, iommu->domain_ids); |
| 3084 | |
Joerg Roedel | cf484d0 | 2015-06-12 12:21:46 +0200 | [diff] [blame] | 3085 | /* |
| 3086 | * We need a marker for copied context entries. This |
| 3087 | * marker needs to work for the old format as well as |
| 3088 | * for extended context entries. |
| 3089 | * |
| 3090 | * Bit 67 of the context entry is used. In the old |
| 3091 | * format this bit is available to software, in the |
| 3092 | * extended format it is the PGE bit, but PGE is ignored |
| 3093 | * by HW if PASIDs are disabled (and thus still |
| 3094 | * available). |
| 3095 | * |
| 3096 | * So disable PASIDs first and then mark the entry |
| 3097 | * copied. This means that we don't copy PASID |
| 3098 | * translations from the old kernel, but this is fine as |
| 3099 | * faults there are not fatal. |
| 3100 | */ |
| 3101 | context_clear_pasid_enable(&ce); |
| 3102 | context_set_copied(&ce); |
| 3103 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3104 | new_ce[idx] = ce; |
| 3105 | } |
| 3106 | |
| 3107 | tbl[tbl_idx + pos] = new_ce; |
| 3108 | |
| 3109 | __iommu_flush_cache(iommu, new_ce, VTD_PAGE_SIZE); |
| 3110 | |
| 3111 | out_unmap: |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3112 | memunmap(old_ce); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3113 | |
| 3114 | out: |
| 3115 | return ret; |
| 3116 | } |
| 3117 | |
| 3118 | static int copy_translation_tables(struct intel_iommu *iommu) |
| 3119 | { |
| 3120 | struct context_entry **ctxt_tbls; |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3121 | struct root_entry *old_rt; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3122 | phys_addr_t old_rt_phys; |
| 3123 | int ctxt_table_entries; |
| 3124 | unsigned long flags; |
| 3125 | u64 rtaddr_reg; |
| 3126 | int bus, ret; |
Joerg Roedel | c3361f2 | 2015-06-12 12:39:25 +0200 | [diff] [blame] | 3127 | bool new_ext, ext; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3128 | |
| 3129 | rtaddr_reg = dmar_readq(iommu->reg + DMAR_RTADDR_REG); |
| 3130 | ext = !!(rtaddr_reg & DMA_RTADDR_RTT); |
Joerg Roedel | c3361f2 | 2015-06-12 12:39:25 +0200 | [diff] [blame] | 3131 | new_ext = !!ecap_ecs(iommu->ecap); |
| 3132 | |
| 3133 | /* |
| 3134 | * The RTT bit can only be changed when translation is disabled, |
| 3135 | * but disabling translation means to open a window for data |
| 3136 | * corruption. So bail out and don't copy anything if we would |
| 3137 | * have to change the bit. |
| 3138 | */ |
| 3139 | if (new_ext != ext) |
| 3140 | return -EINVAL; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3141 | |
| 3142 | old_rt_phys = rtaddr_reg & VTD_PAGE_MASK; |
| 3143 | if (!old_rt_phys) |
| 3144 | return -EINVAL; |
| 3145 | |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3146 | old_rt = memremap(old_rt_phys, PAGE_SIZE, MEMREMAP_WB); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3147 | if (!old_rt) |
| 3148 | return -ENOMEM; |
| 3149 | |
| 3150 | /* This is too big for the stack - allocate it from slab */ |
| 3151 | ctxt_table_entries = ext ? 512 : 256; |
| 3152 | ret = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3153 | ctxt_tbls = kcalloc(ctxt_table_entries, sizeof(void *), GFP_KERNEL); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3154 | if (!ctxt_tbls) |
| 3155 | goto out_unmap; |
| 3156 | |
| 3157 | for (bus = 0; bus < 256; bus++) { |
| 3158 | ret = copy_context_table(iommu, &old_rt[bus], |
| 3159 | ctxt_tbls, bus, ext); |
| 3160 | if (ret) { |
| 3161 | pr_err("%s: Failed to copy context table for bus %d\n", |
| 3162 | iommu->name, bus); |
| 3163 | continue; |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | spin_lock_irqsave(&iommu->lock, flags); |
| 3168 | |
| 3169 | /* Context tables are copied, now write them to the root_entry table */ |
| 3170 | for (bus = 0; bus < 256; bus++) { |
| 3171 | int idx = ext ? bus * 2 : bus; |
| 3172 | u64 val; |
| 3173 | |
| 3174 | if (ctxt_tbls[idx]) { |
| 3175 | val = virt_to_phys(ctxt_tbls[idx]) | 1; |
| 3176 | iommu->root_entry[bus].lo = val; |
| 3177 | } |
| 3178 | |
| 3179 | if (!ext || !ctxt_tbls[idx + 1]) |
| 3180 | continue; |
| 3181 | |
| 3182 | val = virt_to_phys(ctxt_tbls[idx + 1]) | 1; |
| 3183 | iommu->root_entry[bus].hi = val; |
| 3184 | } |
| 3185 | |
| 3186 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 3187 | |
| 3188 | kfree(ctxt_tbls); |
| 3189 | |
| 3190 | __iommu_flush_cache(iommu, iommu->root_entry, PAGE_SIZE); |
| 3191 | |
| 3192 | ret = 0; |
| 3193 | |
| 3194 | out_unmap: |
Dan Williams | dfddb96 | 2015-10-09 18:16:46 -0400 | [diff] [blame] | 3195 | memunmap(old_rt); |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3196 | |
| 3197 | return ret; |
| 3198 | } |
| 3199 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3200 | static int __init init_dmars(void) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3201 | { |
| 3202 | struct dmar_drhd_unit *drhd; |
| 3203 | struct dmar_rmrr_unit *rmrr; |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3204 | bool copied_tables = false; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3205 | struct device *dev; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3206 | struct intel_iommu *iommu; |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3207 | int i, ret; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3208 | |
| 3209 | /* |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3210 | * for each drhd |
| 3211 | * allocate root |
| 3212 | * initialize and program root entry to not present |
| 3213 | * endfor |
| 3214 | */ |
| 3215 | for_each_drhd_unit(drhd) { |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3216 | /* |
| 3217 | * lock not needed as this is only incremented in the single |
| 3218 | * threaded kernel __init code path all other access are read |
| 3219 | * only |
| 3220 | */ |
Jiang Liu | 78d8e70 | 2014-11-09 22:47:57 +0800 | [diff] [blame] | 3221 | if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) { |
Mike Travis | 1b198bb | 2012-03-05 15:05:16 -0800 | [diff] [blame] | 3222 | g_num_of_iommus++; |
| 3223 | continue; |
| 3224 | } |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3225 | pr_err_once("Exceeded %d IOMMUs\n", DMAR_UNITS_SUPPORTED); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3226 | } |
| 3227 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3228 | /* Preallocate enough resources for IOMMU hot-addition */ |
| 3229 | if (g_num_of_iommus < DMAR_UNITS_SUPPORTED) |
| 3230 | g_num_of_iommus = DMAR_UNITS_SUPPORTED; |
| 3231 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3232 | g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *), |
| 3233 | GFP_KERNEL); |
| 3234 | if (!g_iommus) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3235 | pr_err("Allocating global iommu array failed\n"); |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3236 | ret = -ENOMEM; |
| 3237 | goto error; |
| 3238 | } |
| 3239 | |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3240 | for_each_active_iommu(iommu, drhd) { |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 3241 | /* |
| 3242 | * Find the max pasid size of all IOMMU's in the system. |
| 3243 | * We need to ensure the system pasid table is no bigger |
| 3244 | * than the smallest supported. |
| 3245 | */ |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 3246 | if (pasid_supported(iommu)) { |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 3247 | u32 temp = 2 << ecap_pss(iommu->ecap); |
| 3248 | |
| 3249 | intel_pasid_max_id = min_t(u32, temp, |
| 3250 | intel_pasid_max_id); |
| 3251 | } |
| 3252 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3253 | g_iommus[iommu->seq_id] = iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3254 | |
Joerg Roedel | b63d80d | 2015-06-12 09:14:34 +0200 | [diff] [blame] | 3255 | intel_iommu_init_qi(iommu); |
| 3256 | |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 3257 | ret = iommu_init_domains(iommu); |
| 3258 | if (ret) |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3259 | goto free_iommu; |
Suresh Siddha | e61d98d | 2008-07-10 11:16:35 -0700 | [diff] [blame] | 3260 | |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 3261 | init_translation_status(iommu); |
| 3262 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3263 | if (translation_pre_enabled(iommu) && !is_kdump_kernel()) { |
| 3264 | iommu_disable_translation(iommu); |
| 3265 | clear_translation_pre_enabled(iommu); |
| 3266 | pr_warn("Translation was enabled for %s but we are not in kdump mode\n", |
| 3267 | iommu->name); |
| 3268 | } |
Joerg Roedel | 4158c2e | 2015-06-12 10:14:02 +0200 | [diff] [blame] | 3269 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3270 | /* |
| 3271 | * TBD: |
| 3272 | * we could share the same root & context tables |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3273 | * among all IOMMU's. Need to Split it later. |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3274 | */ |
| 3275 | ret = iommu_alloc_root_entry(iommu); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3276 | if (ret) |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3277 | goto free_iommu; |
Joerg Roedel | 5f0a7f7 | 2015-06-12 09:18:53 +0200 | [diff] [blame] | 3278 | |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3279 | if (translation_pre_enabled(iommu)) { |
| 3280 | pr_info("Translation already enabled - trying to copy translation structures\n"); |
| 3281 | |
| 3282 | ret = copy_translation_tables(iommu); |
| 3283 | if (ret) { |
| 3284 | /* |
| 3285 | * We found the IOMMU with translation |
| 3286 | * enabled - but failed to copy over the |
| 3287 | * old root-entry table. Try to proceed |
| 3288 | * by disabling translation now and |
| 3289 | * allocating a clean root-entry table. |
| 3290 | * This might cause DMAR faults, but |
| 3291 | * probably the dump will still succeed. |
| 3292 | */ |
| 3293 | pr_err("Failed to copy translation tables from previous kernel for %s\n", |
| 3294 | iommu->name); |
| 3295 | iommu_disable_translation(iommu); |
| 3296 | clear_translation_pre_enabled(iommu); |
| 3297 | } else { |
| 3298 | pr_info("Copied translation tables from previous kernel for %s\n", |
| 3299 | iommu->name); |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3300 | copied_tables = true; |
Joerg Roedel | 091d42e | 2015-06-12 11:56:10 +0200 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3304 | if (!ecap_pass_through(iommu->ecap)) |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3305 | hw_pass_through = 0; |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 3306 | #ifdef CONFIG_INTEL_IOMMU_SVM |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 3307 | if (pasid_supported(iommu)) |
Lu Baolu | d973795 | 2018-07-14 15:47:02 +0800 | [diff] [blame] | 3308 | intel_svm_init(iommu); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 3309 | #endif |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3310 | } |
| 3311 | |
Joerg Roedel | a4c34ff | 2016-06-17 11:29:48 +0200 | [diff] [blame] | 3312 | /* |
| 3313 | * Now that qi is enabled on all iommus, set the root entry and flush |
| 3314 | * caches. This is required on some Intel X58 chipsets, otherwise the |
| 3315 | * flush_context function will loop forever and the boot hangs. |
| 3316 | */ |
| 3317 | for_each_active_iommu(iommu, drhd) { |
| 3318 | iommu_flush_write_buffer(iommu); |
| 3319 | iommu_set_root_entry(iommu); |
| 3320 | iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); |
| 3321 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
| 3322 | } |
| 3323 | |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3324 | if (iommu_pass_through) |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3325 | iommu_identity_mapping |= IDENTMAP_ALL; |
| 3326 | |
Suresh Siddha | d3f1381 | 2011-08-23 17:05:25 -0700 | [diff] [blame] | 3327 | #ifdef CONFIG_INTEL_IOMMU_BROKEN_GFX_WA |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3328 | iommu_identity_mapping |= IDENTMAP_GFX; |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3329 | #endif |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3330 | |
Ashok Raj | 21e722c | 2017-01-30 09:39:53 -0800 | [diff] [blame] | 3331 | check_tylersburg_isoch(); |
| 3332 | |
Joerg Roedel | 86080cc | 2015-06-12 12:27:16 +0200 | [diff] [blame] | 3333 | if (iommu_identity_mapping) { |
| 3334 | ret = si_domain_init(hw_pass_through); |
| 3335 | if (ret) |
| 3336 | goto free_iommu; |
| 3337 | } |
| 3338 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 3339 | |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3340 | /* |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3341 | * If we copied translations from a previous kernel in the kdump |
| 3342 | * case, we can not assign the devices to domains now, as that |
| 3343 | * would eliminate the old mappings. So skip this part and defer |
| 3344 | * the assignment to device driver initialization time. |
| 3345 | */ |
| 3346 | if (copied_tables) |
| 3347 | goto domains_done; |
| 3348 | |
| 3349 | /* |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3350 | * 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] | 3351 | * identity mappings for rmrr, gfx, and isa and may fall back to static |
| 3352 | * identity mapping if iommu_identity_mapping is set. |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3353 | */ |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3354 | if (iommu_identity_mapping) { |
| 3355 | ret = iommu_prepare_static_identity_mapping(hw_pass_through); |
| 3356 | if (ret) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3357 | pr_crit("Failed to setup IOMMU pass-through\n"); |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3358 | goto free_iommu; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3359 | } |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 3360 | } |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3361 | /* |
| 3362 | * For each rmrr |
| 3363 | * for each dev attached to rmrr |
| 3364 | * do |
| 3365 | * locate drhd for dev, alloc domain for dev |
| 3366 | * allocate free domain |
| 3367 | * allocate page table entries for rmrr |
| 3368 | * if context not allocated for bus |
| 3369 | * allocate and init context |
| 3370 | * set present in root table for this bus |
| 3371 | * init context with domain, translation etc |
| 3372 | * endfor |
| 3373 | * endfor |
| 3374 | */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3375 | pr_info("Setting RMRR:\n"); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3376 | for_each_rmrr_units(rmrr) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3377 | /* some BIOS lists non-exist devices in DMAR table. */ |
| 3378 | for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3379 | i, dev) { |
David Woodhouse | 0b9d975 | 2014-03-09 15:48:15 -0700 | [diff] [blame] | 3380 | ret = iommu_prepare_rmrr_dev(rmrr, dev); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3381 | if (ret) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3382 | pr_err("Mapping reserved region failed\n"); |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | iommu_prepare_isa(); |
Keshavamurthy, Anil S | 49a0429 | 2007-10-21 16:41:57 -0700 | [diff] [blame] | 3387 | |
Joerg Roedel | a87f491 | 2015-06-12 12:32:54 +0200 | [diff] [blame] | 3388 | domains_done: |
| 3389 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3390 | /* |
| 3391 | * for each drhd |
| 3392 | * enable fault log |
| 3393 | * global invalidate context cache |
| 3394 | * global invalidate iotlb |
| 3395 | * enable translation |
| 3396 | */ |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3397 | for_each_iommu(iommu, drhd) { |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 3398 | if (drhd->ignored) { |
| 3399 | /* |
| 3400 | * we always have to disable PMRs or DMA may fail on |
| 3401 | * this device |
| 3402 | */ |
| 3403 | if (force_on) |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3404 | iommu_disable_protect_mem_regions(iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3405 | continue; |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 3406 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3407 | |
| 3408 | iommu_flush_write_buffer(iommu); |
| 3409 | |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 3410 | #ifdef CONFIG_INTEL_IOMMU_SVM |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 3411 | if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) { |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 3412 | ret = intel_svm_enable_prq(iommu); |
| 3413 | if (ret) |
| 3414 | goto free_iommu; |
| 3415 | } |
| 3416 | #endif |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 3417 | ret = dmar_set_interrupt(iommu); |
| 3418 | if (ret) |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3419 | goto free_iommu; |
Keshavamurthy, Anil S | 3460a6d | 2007-10-21 16:41:54 -0700 | [diff] [blame] | 3420 | |
Joerg Roedel | 8939ddf | 2015-06-12 14:40:01 +0200 | [diff] [blame] | 3421 | if (!translation_pre_enabled(iommu)) |
| 3422 | iommu_enable_translation(iommu); |
| 3423 | |
David Woodhouse | b94996c | 2009-09-19 15:28:12 -0700 | [diff] [blame] | 3424 | iommu_disable_protect_mem_regions(iommu); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | return 0; |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3428 | |
| 3429 | free_iommu: |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3430 | for_each_active_iommu(iommu, drhd) { |
| 3431 | disable_dmar_iommu(iommu); |
Jiang Liu | a868e6b | 2014-01-06 14:18:20 +0800 | [diff] [blame] | 3432 | free_dmar_iommu(iommu); |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 3433 | } |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3434 | |
Weidong Han | d9630fe | 2008-12-08 11:06:32 +0800 | [diff] [blame] | 3435 | kfree(g_iommus); |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3436 | |
Jiang Liu | 989d51f | 2014-02-19 14:07:21 +0800 | [diff] [blame] | 3437 | error: |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3438 | return ret; |
| 3439 | } |
| 3440 | |
David Woodhouse | 5a5e02a | 2009-07-04 09:35:44 +0100 | [diff] [blame] | 3441 | /* This takes a number of _MM_ pages, not VTD pages */ |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3442 | static unsigned long intel_alloc_iova(struct device *dev, |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 3443 | struct dmar_domain *domain, |
| 3444 | unsigned long nrpages, uint64_t dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3445 | { |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3446 | unsigned long iova_pfn = 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3447 | |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 3448 | /* Restrict dma_mask to the width that the iommu can handle */ |
| 3449 | 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] | 3450 | /* Ensure we reserve the whole size-aligned region */ |
| 3451 | nrpages = __roundup_pow_of_two(nrpages); |
David Woodhouse | 875764d | 2009-06-28 21:20:51 +0100 | [diff] [blame] | 3452 | |
| 3453 | if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3454 | /* |
| 3455 | * First try to allocate an io virtual address in |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 3456 | * DMA_BIT_MASK(32) and if that fails then try allocating |
Joe Perches | 3609801 | 2007-12-17 11:40:11 -0800 | [diff] [blame] | 3457 | * from higher range |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3458 | */ |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3459 | iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, |
Tomasz Nowicki | 538d5b3 | 2017-09-20 10:52:02 +0200 | [diff] [blame] | 3460 | IOVA_PFN(DMA_BIT_MASK(32)), false); |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3461 | if (iova_pfn) |
| 3462 | return iova_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3463 | } |
Tomasz Nowicki | 538d5b3 | 2017-09-20 10:52:02 +0200 | [diff] [blame] | 3464 | iova_pfn = alloc_iova_fast(&domain->iovad, nrpages, |
| 3465 | IOVA_PFN(dma_mask), true); |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3466 | if (unlikely(!iova_pfn)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3467 | pr_err("Allocating %ld-page iova for %s failed", |
David Woodhouse | 207e359 | 2014-03-09 16:12:32 -0700 | [diff] [blame] | 3468 | nrpages, dev_name(dev)); |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3469 | return 0; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3472 | return iova_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3473 | } |
| 3474 | |
Lu Baolu | 9ddbfb4 | 2018-07-14 15:46:57 +0800 | [diff] [blame] | 3475 | struct dmar_domain *get_valid_domain_for_dev(struct device *dev) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3476 | { |
Joerg Roedel | 1c5ebba | 2016-08-25 13:52:51 +0200 | [diff] [blame] | 3477 | struct dmar_domain *domain, *tmp; |
Joerg Roedel | b1ce5b7 | 2015-09-23 19:16:01 +0200 | [diff] [blame] | 3478 | struct dmar_rmrr_unit *rmrr; |
Joerg Roedel | b1ce5b7 | 2015-09-23 19:16:01 +0200 | [diff] [blame] | 3479 | struct device *i_dev; |
| 3480 | int i, ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3481 | |
Joerg Roedel | 1c5ebba | 2016-08-25 13:52:51 +0200 | [diff] [blame] | 3482 | domain = find_domain(dev); |
| 3483 | if (domain) |
| 3484 | goto out; |
| 3485 | |
| 3486 | domain = find_or_alloc_domain(dev, DEFAULT_DOMAIN_ADDRESS_WIDTH); |
| 3487 | if (!domain) |
| 3488 | goto out; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3489 | |
Joerg Roedel | b1ce5b7 | 2015-09-23 19:16:01 +0200 | [diff] [blame] | 3490 | /* We have a new domain - setup possible RMRRs for the device */ |
| 3491 | rcu_read_lock(); |
| 3492 | for_each_rmrr_units(rmrr) { |
| 3493 | for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, |
| 3494 | i, i_dev) { |
| 3495 | if (i_dev != dev) |
| 3496 | continue; |
| 3497 | |
| 3498 | ret = domain_prepare_identity_map(dev, domain, |
| 3499 | rmrr->base_address, |
| 3500 | rmrr->end_address); |
| 3501 | if (ret) |
| 3502 | dev_err(dev, "Mapping reserved region failed\n"); |
| 3503 | } |
| 3504 | } |
| 3505 | rcu_read_unlock(); |
| 3506 | |
Joerg Roedel | 1c5ebba | 2016-08-25 13:52:51 +0200 | [diff] [blame] | 3507 | tmp = set_domain_for_dev(dev, domain); |
| 3508 | if (!tmp || domain != tmp) { |
| 3509 | domain_exit(domain); |
| 3510 | domain = tmp; |
| 3511 | } |
| 3512 | |
| 3513 | out: |
| 3514 | |
| 3515 | if (!domain) |
| 3516 | pr_err("Allocating domain for %s failed\n", dev_name(dev)); |
| 3517 | |
| 3518 | |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3519 | return domain; |
| 3520 | } |
| 3521 | |
David Woodhouse | ecb509e | 2014-03-09 16:29:55 -0700 | [diff] [blame] | 3522 | /* 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] | 3523 | static int iommu_no_mapping(struct device *dev) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3524 | { |
| 3525 | int found; |
| 3526 | |
David Woodhouse | 3d89194 | 2014-03-06 15:59:26 +0000 | [diff] [blame] | 3527 | if (iommu_dummy(dev)) |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 3528 | return 1; |
| 3529 | |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3530 | if (!iommu_identity_mapping) |
David Woodhouse | 1e4c64c | 2009-07-04 10:40:38 +0100 | [diff] [blame] | 3531 | return 0; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3532 | |
David Woodhouse | 9b22662 | 2014-03-09 14:03:28 -0700 | [diff] [blame] | 3533 | found = identity_mapping(dev); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3534 | if (found) { |
David Woodhouse | ecb509e | 2014-03-09 16:29:55 -0700 | [diff] [blame] | 3535 | if (iommu_should_identity_map(dev, 0)) |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3536 | return 1; |
| 3537 | else { |
| 3538 | /* |
| 3539 | * 32 bit DMA is removed from si_domain and fall back |
| 3540 | * to non-identity mapping. |
| 3541 | */ |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 3542 | dmar_remove_one_dev_info(si_domain, dev); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3543 | pr_info("32bit %s uses non-identity mapping\n", |
| 3544 | dev_name(dev)); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3545 | return 0; |
| 3546 | } |
| 3547 | } else { |
| 3548 | /* |
| 3549 | * In case of a detached 64 bit DMA device from vm, the device |
| 3550 | * is put into si_domain for identity mapping. |
| 3551 | */ |
David Woodhouse | ecb509e | 2014-03-09 16:29:55 -0700 | [diff] [blame] | 3552 | if (iommu_should_identity_map(dev, 0)) { |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3553 | int ret; |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 3554 | ret = domain_add_dev_info(si_domain, dev); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3555 | if (!ret) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3556 | pr_info("64bit %s uses identity mapping\n", |
| 3557 | dev_name(dev)); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3558 | return 1; |
| 3559 | } |
| 3560 | } |
| 3561 | } |
| 3562 | |
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 | } |
| 3565 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3566 | 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] | 3567 | size_t size, int dir, u64 dma_mask) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3568 | { |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3569 | struct dmar_domain *domain; |
Fenghua Yu | 5b6985c | 2008-10-16 18:02:32 -0700 | [diff] [blame] | 3570 | phys_addr_t start_paddr; |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3571 | unsigned long iova_pfn; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3572 | int prot = 0; |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3573 | int ret; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3574 | struct intel_iommu *iommu; |
Fenghua Yu | 33041ec | 2009-08-04 15:10:59 -0700 | [diff] [blame] | 3575 | unsigned long paddr_pfn = paddr >> PAGE_SHIFT; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3576 | |
| 3577 | BUG_ON(dir == DMA_NONE); |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3578 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3579 | if (iommu_no_mapping(dev)) |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3580 | return paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3581 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3582 | domain = get_valid_domain_for_dev(dev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3583 | if (!domain) |
| 3584 | return 0; |
| 3585 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3586 | iommu = domain_get_iommu(domain); |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 3587 | size = aligned_nrpages(paddr, size); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3588 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3589 | iova_pfn = intel_alloc_iova(dev, domain, dma_to_mm_pfn(size), dma_mask); |
| 3590 | if (!iova_pfn) |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3591 | goto error; |
| 3592 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3593 | /* |
| 3594 | * Check if DMAR supports zero-length reads on write only |
| 3595 | * mappings.. |
| 3596 | */ |
| 3597 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \ |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3598 | !cap_zlr(iommu->cap)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3599 | prot |= DMA_PTE_READ; |
| 3600 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 3601 | prot |= DMA_PTE_WRITE; |
| 3602 | /* |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3603 | * 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] | 3604 | * page. Note: if two part of one page are separately mapped, we |
Ingo Molnar | 6865f0d | 2008-04-22 11:09:04 +0200 | [diff] [blame] | 3605 | * 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] | 3606 | * is not a big problem |
| 3607 | */ |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3608 | ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova_pfn), |
Fenghua Yu | 33041ec | 2009-08-04 15:10:59 -0700 | [diff] [blame] | 3609 | mm_to_dma_pfn(paddr_pfn), size, prot); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3610 | if (ret) |
| 3611 | goto error; |
| 3612 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3613 | start_paddr = (phys_addr_t)iova_pfn << PAGE_SHIFT; |
David Woodhouse | 03d6a24 | 2009-06-28 15:33:46 +0100 | [diff] [blame] | 3614 | start_paddr += paddr & ~PAGE_MASK; |
| 3615 | return start_paddr; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3616 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3617 | error: |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3618 | if (iova_pfn) |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3619 | free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(size)); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3620 | pr_err("Device %s request: %zx@%llx dir %d --- failed\n", |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3621 | dev_name(dev), size, (unsigned long long)paddr, dir); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3622 | return 0; |
| 3623 | } |
| 3624 | |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 3625 | static dma_addr_t intel_map_page(struct device *dev, struct page *page, |
| 3626 | unsigned long offset, size_t size, |
| 3627 | enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3628 | unsigned long attrs) |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 3629 | { |
FUJITA Tomonori | ffbbef5 | 2009-01-05 23:47:26 +0900 | [diff] [blame] | 3630 | return __intel_map_single(dev, page_to_phys(page) + offset, size, |
David Woodhouse | 46333e3 | 2014-03-10 20:01:21 -0700 | [diff] [blame] | 3631 | dir, *dev->dma_mask); |
FUJITA Tomonori | bb9e6d6 | 2008-10-15 16:08:28 +0900 | [diff] [blame] | 3632 | } |
| 3633 | |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3634 | 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] | 3635 | { |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3636 | struct dmar_domain *domain; |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3637 | unsigned long start_pfn, last_pfn; |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3638 | unsigned long nrpages; |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3639 | unsigned long iova_pfn; |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3640 | struct intel_iommu *iommu; |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 3641 | struct page *freelist; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3642 | |
David Woodhouse | 7367683 | 2009-07-04 14:08:36 +0100 | [diff] [blame] | 3643 | if (iommu_no_mapping(dev)) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3644 | return; |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 3645 | |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 3646 | domain = find_domain(dev); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3647 | BUG_ON(!domain); |
| 3648 | |
Weidong Han | 8c11e79 | 2008-12-08 15:29:22 +0800 | [diff] [blame] | 3649 | iommu = domain_get_iommu(domain); |
| 3650 | |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3651 | iova_pfn = IOVA_PFN(dev_addr); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3652 | |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3653 | nrpages = aligned_nrpages(dev_addr, size); |
Omer Peleg | 2aac630 | 2016-04-20 11:33:57 +0300 | [diff] [blame] | 3654 | start_pfn = mm_to_dma_pfn(iova_pfn); |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3655 | last_pfn = start_pfn + nrpages - 1; |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3656 | |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3657 | pr_debug("Device %s unmapping: pfn %lx-%lx\n", |
David Woodhouse | 207e359 | 2014-03-09 16:12:32 -0700 | [diff] [blame] | 3658 | dev_name(dev), start_pfn, last_pfn); |
Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 3659 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 3660 | freelist = domain_unmap(domain, start_pfn, last_pfn); |
David Woodhouse | d794dc9 | 2009-06-28 00:27:49 +0100 | [diff] [blame] | 3661 | |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3662 | if (intel_iommu_strict) { |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 3663 | iommu_flush_iotlb_psi(iommu, domain, start_pfn, |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3664 | nrpages, !freelist, 0); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3665 | /* free iova */ |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 3666 | free_iova_fast(&domain->iovad, iova_pfn, dma_to_mm_pfn(nrpages)); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 3667 | dma_free_pagelist(freelist); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3668 | } else { |
Joerg Roedel | 13cf017 | 2017-08-11 11:40:10 +0200 | [diff] [blame] | 3669 | queue_iova(&domain->iovad, iova_pfn, nrpages, |
| 3670 | (unsigned long)freelist); |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3671 | /* |
| 3672 | * queue up the release of the unmap to save the 1/6th of the |
| 3673 | * cpu used up by the iotlb flush operation... |
| 3674 | */ |
mark gross | 5e0d2a6 | 2008-03-04 15:22:08 -0800 | [diff] [blame] | 3675 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3676 | } |
| 3677 | |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 3678 | static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr, |
| 3679 | size_t size, enum dma_data_direction dir, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3680 | unsigned long attrs) |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 3681 | { |
Omer Peleg | 769530e | 2016-04-20 11:33:25 +0300 | [diff] [blame] | 3682 | intel_unmap(dev, dev_addr, size); |
Jiang Liu | d41a4ad | 2014-07-11 14:19:34 +0800 | [diff] [blame] | 3683 | } |
| 3684 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3685 | static void *intel_alloc_coherent(struct device *dev, size_t size, |
Andrzej Pietrasiewicz | baa676f | 2012-03-27 14:28:18 +0200 | [diff] [blame] | 3686 | dma_addr_t *dma_handle, gfp_t flags, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 3687 | unsigned long attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3688 | { |
Christoph Hellwig | 7ec916f | 2018-07-05 13:29:55 -0600 | [diff] [blame] | 3689 | struct page *page = NULL; |
| 3690 | int order; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3691 | |
Christoph Hellwig | 7ec916f | 2018-07-05 13:29:55 -0600 | [diff] [blame] | 3692 | size = PAGE_ALIGN(size); |
| 3693 | order = get_order(size); |
Alex Williamson | e8bb910 | 2009-11-04 15:59:34 -0700 | [diff] [blame] | 3694 | |
Christoph Hellwig | 7ec916f | 2018-07-05 13:29:55 -0600 | [diff] [blame] | 3695 | if (!iommu_no_mapping(dev)) |
| 3696 | flags &= ~(GFP_DMA | GFP_DMA32); |
| 3697 | else if (dev->coherent_dma_mask < dma_get_required_mask(dev)) { |
| 3698 | if (dev->coherent_dma_mask < DMA_BIT_MASK(32)) |
| 3699 | flags |= GFP_DMA; |
| 3700 | else |
| 3701 | flags |= GFP_DMA32; |
| 3702 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3703 | |
Christoph Hellwig | 7ec916f | 2018-07-05 13:29:55 -0600 | [diff] [blame] | 3704 | if (gfpflags_allow_blocking(flags)) { |
| 3705 | unsigned int count = size >> PAGE_SHIFT; |
| 3706 | |
Marek Szyprowski | d834c5a | 2018-08-17 15:49:00 -0700 | [diff] [blame] | 3707 | page = dma_alloc_from_contiguous(dev, count, order, |
| 3708 | flags & __GFP_NOWARN); |
Christoph Hellwig | 7ec916f | 2018-07-05 13:29:55 -0600 | [diff] [blame] | 3709 | if (page && iommu_no_mapping(dev) && |
| 3710 | page_to_phys(page) + size > dev->coherent_dma_mask) { |
| 3711 | dma_release_from_contiguous(dev, page, count); |
| 3712 | page = NULL; |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | if (!page) |
| 3717 | page = alloc_pages(flags, order); |
| 3718 | if (!page) |
| 3719 | return NULL; |
| 3720 | memset(page_address(page), 0, size); |
| 3721 | |
| 3722 | *dma_handle = __intel_map_single(dev, page_to_phys(page), size, |
| 3723 | DMA_BIDIRECTIONAL, |
| 3724 | dev->coherent_dma_mask); |
| 3725 | if (*dma_handle) |
| 3726 | return page_address(page); |
| 3727 | if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) |
| 3728 | __free_pages(page, order); |
| 3729 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3730 | return NULL; |
| 3731 | } |
| 3732 | |
David Woodhouse | 5040a91 | 2014-03-09 16:14:00 -0700 | [diff] [blame] | 3733 | 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] | 3734 | dma_addr_t dma_handle, unsigned long attrs) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3735 | { |
Christoph Hellwig | 7ec916f | 2018-07-05 13:29:55 -0600 | [diff] [blame] | 3736 | int order; |
| 3737 | struct page *page = virt_to_page(vaddr); |
| 3738 | |
| 3739 | size = PAGE_ALIGN(size); |
| 3740 | order = get_order(size); |
| 3741 | |
| 3742 | intel_unmap(dev, dma_handle, size); |
| 3743 | if (!dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT)) |
| 3744 | __free_pages(page, order); |
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 | |
Christoph Hellwig | 02b4da5 | 2018-09-17 19:10:31 +0200 | [diff] [blame] | 3839 | static 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 | fec777c | 2018-03-19 11:38:15 +0100 | [diff] [blame] | 3847 | .dma_supported = dma_direct_supported, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3848 | }; |
| 3849 | |
| 3850 | static inline int iommu_domain_cache_init(void) |
| 3851 | { |
| 3852 | int ret = 0; |
| 3853 | |
| 3854 | iommu_domain_cache = kmem_cache_create("iommu_domain", |
| 3855 | sizeof(struct dmar_domain), |
| 3856 | 0, |
| 3857 | SLAB_HWCACHE_ALIGN, |
| 3858 | |
| 3859 | NULL); |
| 3860 | if (!iommu_domain_cache) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3861 | pr_err("Couldn't create iommu_domain cache\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3862 | ret = -ENOMEM; |
| 3863 | } |
| 3864 | |
| 3865 | return ret; |
| 3866 | } |
| 3867 | |
| 3868 | static inline int iommu_devinfo_cache_init(void) |
| 3869 | { |
| 3870 | int ret = 0; |
| 3871 | |
| 3872 | iommu_devinfo_cache = kmem_cache_create("iommu_devinfo", |
| 3873 | sizeof(struct device_domain_info), |
| 3874 | 0, |
| 3875 | SLAB_HWCACHE_ALIGN, |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3876 | NULL); |
| 3877 | if (!iommu_devinfo_cache) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 3878 | pr_err("Couldn't create devinfo cache\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3879 | ret = -ENOMEM; |
| 3880 | } |
| 3881 | |
| 3882 | return ret; |
| 3883 | } |
| 3884 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3885 | static int __init iommu_init_mempool(void) |
| 3886 | { |
| 3887 | int ret; |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 3888 | ret = iova_cache_get(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3889 | if (ret) |
| 3890 | return ret; |
| 3891 | |
| 3892 | ret = iommu_domain_cache_init(); |
| 3893 | if (ret) |
| 3894 | goto domain_error; |
| 3895 | |
| 3896 | ret = iommu_devinfo_cache_init(); |
| 3897 | if (!ret) |
| 3898 | return ret; |
| 3899 | |
| 3900 | kmem_cache_destroy(iommu_domain_cache); |
| 3901 | domain_error: |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 3902 | iova_cache_put(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3903 | |
| 3904 | return -ENOMEM; |
| 3905 | } |
| 3906 | |
| 3907 | static void __init iommu_exit_mempool(void) |
| 3908 | { |
| 3909 | kmem_cache_destroy(iommu_devinfo_cache); |
| 3910 | kmem_cache_destroy(iommu_domain_cache); |
Sakari Ailus | ae1ff3d | 2015-07-13 14:31:28 +0300 | [diff] [blame] | 3911 | iova_cache_put(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3912 | } |
| 3913 | |
Dan Williams | 556ab45 | 2010-07-23 15:47:56 -0700 | [diff] [blame] | 3914 | static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev) |
| 3915 | { |
| 3916 | struct dmar_drhd_unit *drhd; |
| 3917 | u32 vtbar; |
| 3918 | int rc; |
| 3919 | |
| 3920 | /* We know that this device on this chipset has its own IOMMU. |
| 3921 | * If we find it under a different IOMMU, then the BIOS is lying |
| 3922 | * to us. Hope that the IOMMU for this device is actually |
| 3923 | * disabled, and it needs no translation... |
| 3924 | */ |
| 3925 | rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar); |
| 3926 | if (rc) { |
| 3927 | /* "can't" happen */ |
| 3928 | dev_info(&pdev->dev, "failed to run vt-d quirk\n"); |
| 3929 | return; |
| 3930 | } |
| 3931 | vtbar &= 0xffff0000; |
| 3932 | |
| 3933 | /* we know that the this iommu should be at offset 0xa000 from vtbar */ |
| 3934 | drhd = dmar_find_matched_drhd_unit(pdev); |
| 3935 | if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000, |
| 3936 | TAINT_FIRMWARE_WORKAROUND, |
| 3937 | "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n")) |
| 3938 | pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO; |
| 3939 | } |
| 3940 | DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu); |
| 3941 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3942 | static void __init init_no_remapping_devices(void) |
| 3943 | { |
| 3944 | struct dmar_drhd_unit *drhd; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3945 | struct device *dev; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3946 | int i; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3947 | |
| 3948 | for_each_drhd_unit(drhd) { |
| 3949 | if (!drhd->include_all) { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3950 | for_each_active_dev_scope(drhd->devices, |
| 3951 | drhd->devices_cnt, i, dev) |
| 3952 | break; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3953 | /* ignore DMAR unit if no devices exist */ |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3954 | if (i == drhd->devices_cnt) |
| 3955 | drhd->ignored = 1; |
| 3956 | } |
| 3957 | } |
| 3958 | |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3959 | for_each_active_drhd_unit(drhd) { |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 3960 | if (drhd->include_all) |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3961 | continue; |
| 3962 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3963 | for_each_active_dev_scope(drhd->devices, |
| 3964 | drhd->devices_cnt, i, dev) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3965 | 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] | 3966 | break; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3967 | if (i < drhd->devices_cnt) |
| 3968 | continue; |
| 3969 | |
David Woodhouse | c0771df | 2011-10-14 20:59:46 +0100 | [diff] [blame] | 3970 | /* This IOMMU has *only* gfx devices. Either bypass it or |
| 3971 | set the gfx_mapped flag, as appropriate */ |
| 3972 | if (dmar_map_gfx) { |
| 3973 | intel_iommu_gfx_mapped = 1; |
| 3974 | } else { |
| 3975 | drhd->ignored = 1; |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 3976 | for_each_active_dev_scope(drhd->devices, |
| 3977 | drhd->devices_cnt, i, dev) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 3978 | dev->archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | } |
| 3982 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 3983 | #ifdef CONFIG_SUSPEND |
| 3984 | static int init_iommu_hw(void) |
| 3985 | { |
| 3986 | struct dmar_drhd_unit *drhd; |
| 3987 | struct intel_iommu *iommu = NULL; |
| 3988 | |
| 3989 | for_each_active_iommu(iommu, drhd) |
| 3990 | if (iommu->qi) |
| 3991 | dmar_reenable_qi(iommu); |
| 3992 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 3993 | for_each_iommu(iommu, drhd) { |
| 3994 | if (drhd->ignored) { |
| 3995 | /* |
| 3996 | * we always have to disable PMRs or DMA may fail on |
| 3997 | * this device |
| 3998 | */ |
| 3999 | if (force_on) |
| 4000 | iommu_disable_protect_mem_regions(iommu); |
| 4001 | continue; |
| 4002 | } |
| 4003 | |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4004 | iommu_flush_write_buffer(iommu); |
| 4005 | |
| 4006 | iommu_set_root_entry(iommu); |
| 4007 | |
| 4008 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 4009 | DMA_CCMD_GLOBAL_INVL); |
Jiang Liu | 2a41cce | 2014-07-11 14:19:33 +0800 | [diff] [blame] | 4010 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
| 4011 | iommu_enable_translation(iommu); |
David Woodhouse | b94996c | 2009-09-19 15:28:12 -0700 | [diff] [blame] | 4012 | iommu_disable_protect_mem_regions(iommu); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | return 0; |
| 4016 | } |
| 4017 | |
| 4018 | static void iommu_flush_all(void) |
| 4019 | { |
| 4020 | struct dmar_drhd_unit *drhd; |
| 4021 | struct intel_iommu *iommu; |
| 4022 | |
| 4023 | for_each_active_iommu(iommu, drhd) { |
| 4024 | iommu->flush.flush_context(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 4025 | DMA_CCMD_GLOBAL_INVL); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4026 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, |
David Woodhouse | 1f0ef2a | 2009-05-10 19:58:49 +0100 | [diff] [blame] | 4027 | DMA_TLB_GLOBAL_FLUSH); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4028 | } |
| 4029 | } |
| 4030 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4031 | static int iommu_suspend(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4032 | { |
| 4033 | struct dmar_drhd_unit *drhd; |
| 4034 | struct intel_iommu *iommu = NULL; |
| 4035 | unsigned long flag; |
| 4036 | |
| 4037 | for_each_active_iommu(iommu, drhd) { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 4038 | iommu->iommu_state = kcalloc(MAX_SR_DMAR_REGS, sizeof(u32), |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4039 | GFP_ATOMIC); |
| 4040 | if (!iommu->iommu_state) |
| 4041 | goto nomem; |
| 4042 | } |
| 4043 | |
| 4044 | iommu_flush_all(); |
| 4045 | |
| 4046 | for_each_active_iommu(iommu, drhd) { |
| 4047 | iommu_disable_translation(iommu); |
| 4048 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4049 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4050 | |
| 4051 | iommu->iommu_state[SR_DMAR_FECTL_REG] = |
| 4052 | readl(iommu->reg + DMAR_FECTL_REG); |
| 4053 | iommu->iommu_state[SR_DMAR_FEDATA_REG] = |
| 4054 | readl(iommu->reg + DMAR_FEDATA_REG); |
| 4055 | iommu->iommu_state[SR_DMAR_FEADDR_REG] = |
| 4056 | readl(iommu->reg + DMAR_FEADDR_REG); |
| 4057 | iommu->iommu_state[SR_DMAR_FEUADDR_REG] = |
| 4058 | readl(iommu->reg + DMAR_FEUADDR_REG); |
| 4059 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4060 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4061 | } |
| 4062 | return 0; |
| 4063 | |
| 4064 | nomem: |
| 4065 | for_each_active_iommu(iommu, drhd) |
| 4066 | kfree(iommu->iommu_state); |
| 4067 | |
| 4068 | return -ENOMEM; |
| 4069 | } |
| 4070 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4071 | static void iommu_resume(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4072 | { |
| 4073 | struct dmar_drhd_unit *drhd; |
| 4074 | struct intel_iommu *iommu = NULL; |
| 4075 | unsigned long flag; |
| 4076 | |
| 4077 | if (init_iommu_hw()) { |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 4078 | if (force_on) |
| 4079 | panic("tboot: IOMMU setup failed, DMAR can not resume!\n"); |
| 4080 | else |
| 4081 | WARN(1, "IOMMU setup failed, DMAR can not resume!\n"); |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4082 | return; |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | for_each_active_iommu(iommu, drhd) { |
| 4086 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4087 | raw_spin_lock_irqsave(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4088 | |
| 4089 | writel(iommu->iommu_state[SR_DMAR_FECTL_REG], |
| 4090 | iommu->reg + DMAR_FECTL_REG); |
| 4091 | writel(iommu->iommu_state[SR_DMAR_FEDATA_REG], |
| 4092 | iommu->reg + DMAR_FEDATA_REG); |
| 4093 | writel(iommu->iommu_state[SR_DMAR_FEADDR_REG], |
| 4094 | iommu->reg + DMAR_FEADDR_REG); |
| 4095 | writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG], |
| 4096 | iommu->reg + DMAR_FEUADDR_REG); |
| 4097 | |
Thomas Gleixner | 1f5b3c3 | 2011-07-19 16:19:51 +0200 | [diff] [blame] | 4098 | raw_spin_unlock_irqrestore(&iommu->register_lock, flag); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | for_each_active_iommu(iommu, drhd) |
| 4102 | kfree(iommu->iommu_state); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4103 | } |
| 4104 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4105 | static struct syscore_ops iommu_syscore_ops = { |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4106 | .resume = iommu_resume, |
| 4107 | .suspend = iommu_suspend, |
| 4108 | }; |
| 4109 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4110 | static void __init init_iommu_pm_ops(void) |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4111 | { |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4112 | register_syscore_ops(&iommu_syscore_ops); |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
| 4115 | #else |
Rafael J. Wysocki | 99592ba | 2011-06-07 21:32:31 +0200 | [diff] [blame] | 4116 | static inline void init_iommu_pm_ops(void) {} |
Fenghua Yu | f59c7b6 | 2009-03-27 14:22:42 -0700 | [diff] [blame] | 4117 | #endif /* CONFIG_PM */ |
| 4118 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4119 | |
Jiang Liu | c2a0b53 | 2014-11-09 22:47:56 +0800 | [diff] [blame] | 4120 | 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] | 4121 | { |
| 4122 | struct acpi_dmar_reserved_memory *rmrr; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4123 | int prot = DMA_PTE_READ|DMA_PTE_WRITE; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4124 | struct dmar_rmrr_unit *rmrru; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4125 | size_t length; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4126 | |
| 4127 | rmrru = kzalloc(sizeof(*rmrru), GFP_KERNEL); |
| 4128 | if (!rmrru) |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4129 | goto out; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4130 | |
| 4131 | rmrru->hdr = header; |
| 4132 | rmrr = (struct acpi_dmar_reserved_memory *)header; |
| 4133 | rmrru->base_address = rmrr->base_address; |
| 4134 | rmrru->end_address = rmrr->end_address; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4135 | |
| 4136 | length = rmrr->end_address - rmrr->base_address + 1; |
| 4137 | rmrru->resv = iommu_alloc_resv_region(rmrr->base_address, length, prot, |
| 4138 | IOMMU_RESV_DIRECT); |
| 4139 | if (!rmrru->resv) |
| 4140 | goto free_rmrru; |
| 4141 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 4142 | rmrru->devices = dmar_alloc_dev_scope((void *)(rmrr + 1), |
| 4143 | ((void *)rmrr) + rmrr->header.length, |
| 4144 | &rmrru->devices_cnt); |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4145 | if (rmrru->devices_cnt && rmrru->devices == NULL) |
| 4146 | goto free_all; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4147 | |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 4148 | list_add(&rmrru->list, &dmar_rmrr_units); |
| 4149 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4150 | return 0; |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4151 | free_all: |
| 4152 | kfree(rmrru->resv); |
| 4153 | free_rmrru: |
| 4154 | kfree(rmrru); |
| 4155 | out: |
| 4156 | return -ENOMEM; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4157 | } |
| 4158 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4159 | static struct dmar_atsr_unit *dmar_find_atsr(struct acpi_dmar_atsr *atsr) |
| 4160 | { |
| 4161 | struct dmar_atsr_unit *atsru; |
| 4162 | struct acpi_dmar_atsr *tmp; |
| 4163 | |
| 4164 | list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) { |
| 4165 | tmp = (struct acpi_dmar_atsr *)atsru->hdr; |
| 4166 | if (atsr->segment != tmp->segment) |
| 4167 | continue; |
| 4168 | if (atsr->header.length != tmp->header.length) |
| 4169 | continue; |
| 4170 | if (memcmp(atsr, tmp, atsr->header.length) == 0) |
| 4171 | return atsru; |
| 4172 | } |
| 4173 | |
| 4174 | return NULL; |
| 4175 | } |
| 4176 | |
| 4177 | int dmar_parse_one_atsr(struct acpi_dmar_header *hdr, void *arg) |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4178 | { |
| 4179 | struct acpi_dmar_atsr *atsr; |
| 4180 | struct dmar_atsr_unit *atsru; |
| 4181 | |
Thomas Gleixner | b608fe3 | 2017-05-16 20:42:41 +0200 | [diff] [blame] | 4182 | if (system_state >= SYSTEM_RUNNING && !intel_iommu_enabled) |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4183 | return 0; |
| 4184 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4185 | atsr = container_of(hdr, struct acpi_dmar_atsr, header); |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4186 | atsru = dmar_find_atsr(atsr); |
| 4187 | if (atsru) |
| 4188 | return 0; |
| 4189 | |
| 4190 | atsru = kzalloc(sizeof(*atsru) + hdr->length, GFP_KERNEL); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4191 | if (!atsru) |
| 4192 | return -ENOMEM; |
| 4193 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4194 | /* |
| 4195 | * If memory is allocated from slab by ACPI _DSM method, we need to |
| 4196 | * copy the memory content because the memory buffer will be freed |
| 4197 | * on return. |
| 4198 | */ |
| 4199 | atsru->hdr = (void *)(atsru + 1); |
| 4200 | memcpy(atsru->hdr, hdr, hdr->length); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4201 | atsru->include_all = atsr->flags & 0x1; |
Jiang Liu | 2e45528 | 2014-02-19 14:07:36 +0800 | [diff] [blame] | 4202 | if (!atsru->include_all) { |
| 4203 | atsru->devices = dmar_alloc_dev_scope((void *)(atsr + 1), |
| 4204 | (void *)atsr + atsr->header.length, |
| 4205 | &atsru->devices_cnt); |
| 4206 | if (atsru->devices_cnt && atsru->devices == NULL) { |
| 4207 | kfree(atsru); |
| 4208 | return -ENOMEM; |
| 4209 | } |
| 4210 | } |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4211 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 4212 | list_add_rcu(&atsru->list, &dmar_atsr_units); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4213 | |
| 4214 | return 0; |
| 4215 | } |
| 4216 | |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4217 | static void intel_iommu_free_atsr(struct dmar_atsr_unit *atsru) |
| 4218 | { |
| 4219 | dmar_free_dev_scope(&atsru->devices, &atsru->devices_cnt); |
| 4220 | kfree(atsru); |
| 4221 | } |
| 4222 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4223 | int dmar_release_one_atsr(struct acpi_dmar_header *hdr, void *arg) |
| 4224 | { |
| 4225 | struct acpi_dmar_atsr *atsr; |
| 4226 | struct dmar_atsr_unit *atsru; |
| 4227 | |
| 4228 | atsr = container_of(hdr, struct acpi_dmar_atsr, header); |
| 4229 | atsru = dmar_find_atsr(atsr); |
| 4230 | if (atsru) { |
| 4231 | list_del_rcu(&atsru->list); |
| 4232 | synchronize_rcu(); |
| 4233 | intel_iommu_free_atsr(atsru); |
| 4234 | } |
| 4235 | |
| 4236 | return 0; |
| 4237 | } |
| 4238 | |
| 4239 | int dmar_check_one_atsr(struct acpi_dmar_header *hdr, void *arg) |
| 4240 | { |
| 4241 | int i; |
| 4242 | struct device *dev; |
| 4243 | struct acpi_dmar_atsr *atsr; |
| 4244 | struct dmar_atsr_unit *atsru; |
| 4245 | |
| 4246 | atsr = container_of(hdr, struct acpi_dmar_atsr, header); |
| 4247 | atsru = dmar_find_atsr(atsr); |
| 4248 | if (!atsru) |
| 4249 | return 0; |
| 4250 | |
Linus Torvalds | 194dc87 | 2016-07-27 20:03:31 -0700 | [diff] [blame] | 4251 | if (!atsru->include_all && atsru->devices && atsru->devices_cnt) { |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4252 | for_each_active_dev_scope(atsru->devices, atsru->devices_cnt, |
| 4253 | i, dev) |
| 4254 | return -EBUSY; |
Linus Torvalds | 194dc87 | 2016-07-27 20:03:31 -0700 | [diff] [blame] | 4255 | } |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4256 | |
| 4257 | return 0; |
| 4258 | } |
| 4259 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4260 | static int intel_iommu_add(struct dmar_drhd_unit *dmaru) |
| 4261 | { |
| 4262 | int sp, ret = 0; |
| 4263 | struct intel_iommu *iommu = dmaru->iommu; |
| 4264 | |
| 4265 | if (g_iommus[iommu->seq_id]) |
| 4266 | return 0; |
| 4267 | |
| 4268 | if (hw_pass_through && !ecap_pass_through(iommu->ecap)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4269 | pr_warn("%s: Doesn't support hardware pass through.\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4270 | iommu->name); |
| 4271 | return -ENXIO; |
| 4272 | } |
| 4273 | if (!ecap_sc_support(iommu->ecap) && |
| 4274 | domain_update_iommu_snooping(iommu)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4275 | pr_warn("%s: Doesn't support snooping.\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4276 | iommu->name); |
| 4277 | return -ENXIO; |
| 4278 | } |
| 4279 | sp = domain_update_iommu_superpage(iommu) - 1; |
| 4280 | if (sp >= 0 && !(cap_super_page_val(iommu->cap) & (1 << sp))) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4281 | pr_warn("%s: Doesn't support large page.\n", |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4282 | iommu->name); |
| 4283 | return -ENXIO; |
| 4284 | } |
| 4285 | |
| 4286 | /* |
| 4287 | * Disable translation if already enabled prior to OS handover. |
| 4288 | */ |
| 4289 | if (iommu->gcmd & DMA_GCMD_TE) |
| 4290 | iommu_disable_translation(iommu); |
| 4291 | |
| 4292 | g_iommus[iommu->seq_id] = iommu; |
| 4293 | ret = iommu_init_domains(iommu); |
| 4294 | if (ret == 0) |
| 4295 | ret = iommu_alloc_root_entry(iommu); |
| 4296 | if (ret) |
| 4297 | goto out; |
| 4298 | |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 4299 | #ifdef CONFIG_INTEL_IOMMU_SVM |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 4300 | if (pasid_supported(iommu)) |
Lu Baolu | d973795 | 2018-07-14 15:47:02 +0800 | [diff] [blame] | 4301 | intel_svm_init(iommu); |
David Woodhouse | 8a94ade | 2015-03-24 14:54:56 +0000 | [diff] [blame] | 4302 | #endif |
| 4303 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4304 | if (dmaru->ignored) { |
| 4305 | /* |
| 4306 | * we always have to disable PMRs or DMA may fail on this device |
| 4307 | */ |
| 4308 | if (force_on) |
| 4309 | iommu_disable_protect_mem_regions(iommu); |
| 4310 | return 0; |
| 4311 | } |
| 4312 | |
| 4313 | intel_iommu_init_qi(iommu); |
| 4314 | iommu_flush_write_buffer(iommu); |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 4315 | |
| 4316 | #ifdef CONFIG_INTEL_IOMMU_SVM |
Lu Baolu | 765b6a9 | 2018-12-10 09:58:55 +0800 | [diff] [blame] | 4317 | if (pasid_supported(iommu) && ecap_prs(iommu->ecap)) { |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 4318 | ret = intel_svm_enable_prq(iommu); |
| 4319 | if (ret) |
| 4320 | goto disable_iommu; |
| 4321 | } |
| 4322 | #endif |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4323 | ret = dmar_set_interrupt(iommu); |
| 4324 | if (ret) |
| 4325 | goto disable_iommu; |
| 4326 | |
| 4327 | iommu_set_root_entry(iommu); |
| 4328 | iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL); |
| 4329 | iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); |
| 4330 | iommu_enable_translation(iommu); |
| 4331 | |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4332 | iommu_disable_protect_mem_regions(iommu); |
| 4333 | return 0; |
| 4334 | |
| 4335 | disable_iommu: |
| 4336 | disable_dmar_iommu(iommu); |
| 4337 | out: |
| 4338 | free_dmar_iommu(iommu); |
| 4339 | return ret; |
| 4340 | } |
| 4341 | |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4342 | int dmar_iommu_hotplug(struct dmar_drhd_unit *dmaru, bool insert) |
| 4343 | { |
Jiang Liu | ffebeb4 | 2014-11-09 22:48:02 +0800 | [diff] [blame] | 4344 | int ret = 0; |
| 4345 | struct intel_iommu *iommu = dmaru->iommu; |
| 4346 | |
| 4347 | if (!intel_iommu_enabled) |
| 4348 | return 0; |
| 4349 | if (iommu == NULL) |
| 4350 | return -EINVAL; |
| 4351 | |
| 4352 | if (insert) { |
| 4353 | ret = intel_iommu_add(dmaru); |
| 4354 | } else { |
| 4355 | disable_dmar_iommu(iommu); |
| 4356 | free_dmar_iommu(iommu); |
| 4357 | } |
| 4358 | |
| 4359 | return ret; |
Jiang Liu | 6b19724 | 2014-11-09 22:47:58 +0800 | [diff] [blame] | 4360 | } |
| 4361 | |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4362 | static void intel_iommu_free_dmars(void) |
| 4363 | { |
| 4364 | struct dmar_rmrr_unit *rmrru, *rmrr_n; |
| 4365 | struct dmar_atsr_unit *atsru, *atsr_n; |
| 4366 | |
| 4367 | list_for_each_entry_safe(rmrru, rmrr_n, &dmar_rmrr_units, list) { |
| 4368 | list_del(&rmrru->list); |
| 4369 | dmar_free_dev_scope(&rmrru->devices, &rmrru->devices_cnt); |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 4370 | kfree(rmrru->resv); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4371 | kfree(rmrru); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4374 | list_for_each_entry_safe(atsru, atsr_n, &dmar_atsr_units, list) { |
| 4375 | list_del(&atsru->list); |
| 4376 | intel_iommu_free_atsr(atsru); |
| 4377 | } |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | int dmar_find_matched_atsr_unit(struct pci_dev *dev) |
| 4381 | { |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4382 | int i, ret = 1; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4383 | struct pci_bus *bus; |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 4384 | struct pci_dev *bridge = NULL; |
| 4385 | struct device *tmp; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4386 | struct acpi_dmar_atsr *atsr; |
| 4387 | struct dmar_atsr_unit *atsru; |
| 4388 | |
| 4389 | dev = pci_physfn(dev); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4390 | for (bus = dev->bus; bus; bus = bus->parent) { |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4391 | bridge = bus->self; |
David Woodhouse | d14053b3 | 2015-10-15 09:28:06 +0100 | [diff] [blame] | 4392 | /* If it's an integrated device, allow ATS */ |
| 4393 | if (!bridge) |
| 4394 | return 1; |
| 4395 | /* Connected via non-PCIe: no ATS */ |
| 4396 | if (!pci_is_pcie(bridge) || |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 4397 | pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4398 | return 0; |
David Woodhouse | d14053b3 | 2015-10-15 09:28:06 +0100 | [diff] [blame] | 4399 | /* If we found the root port, look it up in the ATSR */ |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4400 | if (pci_pcie_type(bridge) == PCI_EXP_TYPE_ROOT_PORT) |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4401 | break; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 4404 | rcu_read_lock(); |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4405 | list_for_each_entry_rcu(atsru, &dmar_atsr_units, list) { |
| 4406 | atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header); |
| 4407 | if (atsr->segment != pci_domain_nr(dev->bus)) |
| 4408 | continue; |
| 4409 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4410 | for_each_dev_scope(atsru->devices, atsru->devices_cnt, i, tmp) |
David Woodhouse | 832bd85 | 2014-03-07 15:08:36 +0000 | [diff] [blame] | 4411 | if (tmp == &bridge->dev) |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4412 | goto out; |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4413 | |
| 4414 | if (atsru->include_all) |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4415 | goto out; |
Jiang Liu | b5f82dd | 2014-02-19 14:07:31 +0800 | [diff] [blame] | 4416 | } |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4417 | ret = 0; |
| 4418 | out: |
Jiang Liu | 0e24261 | 2014-02-19 14:07:34 +0800 | [diff] [blame] | 4419 | rcu_read_unlock(); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4420 | |
Jiang Liu | b683b23 | 2014-02-19 14:07:32 +0800 | [diff] [blame] | 4421 | return ret; |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4422 | } |
| 4423 | |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4424 | int dmar_iommu_notify_scope_dev(struct dmar_pci_notify_info *info) |
| 4425 | { |
| 4426 | int ret = 0; |
| 4427 | struct dmar_rmrr_unit *rmrru; |
| 4428 | struct dmar_atsr_unit *atsru; |
| 4429 | struct acpi_dmar_atsr *atsr; |
| 4430 | struct acpi_dmar_reserved_memory *rmrr; |
| 4431 | |
Thomas Gleixner | b608fe3 | 2017-05-16 20:42:41 +0200 | [diff] [blame] | 4432 | if (!intel_iommu_enabled && system_state >= SYSTEM_RUNNING) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4433 | return 0; |
| 4434 | |
| 4435 | list_for_each_entry(rmrru, &dmar_rmrr_units, list) { |
| 4436 | rmrr = container_of(rmrru->hdr, |
| 4437 | struct acpi_dmar_reserved_memory, header); |
| 4438 | if (info->event == BUS_NOTIFY_ADD_DEVICE) { |
| 4439 | ret = dmar_insert_dev_scope(info, (void *)(rmrr + 1), |
| 4440 | ((void *)rmrr) + rmrr->header.length, |
| 4441 | rmrr->segment, rmrru->devices, |
| 4442 | rmrru->devices_cnt); |
Jiang Liu | 27e2495 | 2014-06-20 15:08:06 +0800 | [diff] [blame] | 4443 | if(ret < 0) |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4444 | return ret; |
Joerg Roedel | e6a8c9b | 2016-02-29 23:49:47 +0100 | [diff] [blame] | 4445 | } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) { |
Jiang Liu | 27e2495 | 2014-06-20 15:08:06 +0800 | [diff] [blame] | 4446 | dmar_remove_dev_scope(info, rmrr->segment, |
| 4447 | rmrru->devices, rmrru->devices_cnt); |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | |
| 4451 | list_for_each_entry(atsru, &dmar_atsr_units, list) { |
| 4452 | if (atsru->include_all) |
| 4453 | continue; |
| 4454 | |
| 4455 | atsr = container_of(atsru->hdr, struct acpi_dmar_atsr, header); |
| 4456 | if (info->event == BUS_NOTIFY_ADD_DEVICE) { |
| 4457 | ret = dmar_insert_dev_scope(info, (void *)(atsr + 1), |
| 4458 | (void *)atsr + atsr->header.length, |
| 4459 | atsr->segment, atsru->devices, |
| 4460 | atsru->devices_cnt); |
| 4461 | if (ret > 0) |
| 4462 | break; |
| 4463 | else if(ret < 0) |
| 4464 | return ret; |
Joerg Roedel | e6a8c9b | 2016-02-29 23:49:47 +0100 | [diff] [blame] | 4465 | } else if (info->event == BUS_NOTIFY_REMOVED_DEVICE) { |
Jiang Liu | 59ce051 | 2014-02-19 14:07:35 +0800 | [diff] [blame] | 4466 | if (dmar_remove_dev_scope(info, atsr->segment, |
| 4467 | atsru->devices, atsru->devices_cnt)) |
| 4468 | break; |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | return 0; |
| 4473 | } |
| 4474 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4475 | /* |
| 4476 | * Here we only respond to action of unbound device from driver. |
| 4477 | * |
| 4478 | * Added device is not attached to its DMAR domain here yet. That will happen |
| 4479 | * when mapping the device to iova. |
| 4480 | */ |
| 4481 | static int device_notifier(struct notifier_block *nb, |
| 4482 | unsigned long action, void *data) |
| 4483 | { |
| 4484 | struct device *dev = data; |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4485 | struct dmar_domain *domain; |
| 4486 | |
David Woodhouse | 3d89194 | 2014-03-06 15:59:26 +0000 | [diff] [blame] | 4487 | if (iommu_dummy(dev)) |
David Woodhouse | 44cd613 | 2009-12-02 10:18:30 +0000 | [diff] [blame] | 4488 | return 0; |
| 4489 | |
Joerg Roedel | 1196c2f | 2014-09-30 13:02:03 +0200 | [diff] [blame] | 4490 | if (action != BUS_NOTIFY_REMOVED_DEVICE) |
Jiang Liu | 7e7dfab | 2014-02-19 14:07:23 +0800 | [diff] [blame] | 4491 | return 0; |
| 4492 | |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 4493 | domain = find_domain(dev); |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4494 | if (!domain) |
| 4495 | return 0; |
| 4496 | |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 4497 | dmar_remove_one_dev_info(domain, dev); |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 4498 | if (!domain_type_is_vm_or_si(domain) && list_empty(&domain->devices)) |
Jiang Liu | 7e7dfab | 2014-02-19 14:07:23 +0800 | [diff] [blame] | 4499 | domain_exit(domain); |
Alex Williamson | a97590e | 2011-03-04 14:52:16 -0700 | [diff] [blame] | 4500 | |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | |
| 4504 | static struct notifier_block device_nb = { |
| 4505 | .notifier_call = device_notifier, |
| 4506 | }; |
| 4507 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4508 | static int intel_iommu_memory_notifier(struct notifier_block *nb, |
| 4509 | unsigned long val, void *v) |
| 4510 | { |
| 4511 | struct memory_notify *mhp = v; |
| 4512 | unsigned long long start, end; |
| 4513 | unsigned long start_vpfn, last_vpfn; |
| 4514 | |
| 4515 | switch (val) { |
| 4516 | case MEM_GOING_ONLINE: |
| 4517 | start = mhp->start_pfn << PAGE_SHIFT; |
| 4518 | end = ((mhp->start_pfn + mhp->nr_pages) << PAGE_SHIFT) - 1; |
| 4519 | if (iommu_domain_identity_map(si_domain, start, end)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4520 | pr_warn("Failed to build identity map for [%llx-%llx]\n", |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4521 | start, end); |
| 4522 | return NOTIFY_BAD; |
| 4523 | } |
| 4524 | break; |
| 4525 | |
| 4526 | case MEM_OFFLINE: |
| 4527 | case MEM_CANCEL_ONLINE: |
| 4528 | start_vpfn = mm_to_dma_pfn(mhp->start_pfn); |
| 4529 | last_vpfn = mm_to_dma_pfn(mhp->start_pfn + mhp->nr_pages - 1); |
| 4530 | while (start_vpfn <= last_vpfn) { |
| 4531 | struct iova *iova; |
| 4532 | struct dmar_drhd_unit *drhd; |
| 4533 | struct intel_iommu *iommu; |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4534 | struct page *freelist; |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4535 | |
| 4536 | iova = find_iova(&si_domain->iovad, start_vpfn); |
| 4537 | if (iova == NULL) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4538 | pr_debug("Failed get IOVA for PFN %lx\n", |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4539 | start_vpfn); |
| 4540 | break; |
| 4541 | } |
| 4542 | |
| 4543 | iova = split_and_remove_iova(&si_domain->iovad, iova, |
| 4544 | start_vpfn, last_vpfn); |
| 4545 | if (iova == NULL) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4546 | pr_warn("Failed to split IOVA PFN [%lx-%lx]\n", |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4547 | start_vpfn, last_vpfn); |
| 4548 | return NOTIFY_BAD; |
| 4549 | } |
| 4550 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4551 | freelist = domain_unmap(si_domain, iova->pfn_lo, |
| 4552 | iova->pfn_hi); |
| 4553 | |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4554 | rcu_read_lock(); |
| 4555 | for_each_active_iommu(iommu, drhd) |
Joerg Roedel | a1ddcbe | 2015-07-21 15:20:32 +0200 | [diff] [blame] | 4556 | iommu_flush_iotlb_psi(iommu, si_domain, |
Jiang Liu | a156ef9 | 2014-07-11 14:19:36 +0800 | [diff] [blame] | 4557 | iova->pfn_lo, iova_size(iova), |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4558 | !freelist, 0); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4559 | rcu_read_unlock(); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 4560 | dma_free_pagelist(freelist); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4561 | |
| 4562 | start_vpfn = iova->pfn_hi + 1; |
| 4563 | free_iova_mem(iova); |
| 4564 | } |
| 4565 | break; |
| 4566 | } |
| 4567 | |
| 4568 | return NOTIFY_OK; |
| 4569 | } |
| 4570 | |
| 4571 | static struct notifier_block intel_iommu_memory_nb = { |
| 4572 | .notifier_call = intel_iommu_memory_notifier, |
| 4573 | .priority = 0 |
| 4574 | }; |
| 4575 | |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 4576 | static void free_all_cpu_cached_iovas(unsigned int cpu) |
| 4577 | { |
| 4578 | int i; |
| 4579 | |
| 4580 | for (i = 0; i < g_num_of_iommus; i++) { |
| 4581 | struct intel_iommu *iommu = g_iommus[i]; |
| 4582 | struct dmar_domain *domain; |
Aaron Campbell | 0caa761 | 2016-07-02 21:23:24 -0300 | [diff] [blame] | 4583 | int did; |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 4584 | |
| 4585 | if (!iommu) |
| 4586 | continue; |
| 4587 | |
Jan Niehusmann | 3bd4f91 | 2016-06-06 14:20:11 +0200 | [diff] [blame] | 4588 | for (did = 0; did < cap_ndoms(iommu->cap); did++) { |
Aaron Campbell | 0caa761 | 2016-07-02 21:23:24 -0300 | [diff] [blame] | 4589 | domain = get_iommu_domain(iommu, (u16)did); |
Omer Peleg | 22e2f9f | 2016-04-20 11:34:11 +0300 | [diff] [blame] | 4590 | |
| 4591 | if (!domain) |
| 4592 | continue; |
| 4593 | free_cpu_cached_iovas(cpu, &domain->iovad); |
| 4594 | } |
| 4595 | } |
| 4596 | } |
| 4597 | |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4598 | static int intel_iommu_cpu_dead(unsigned int cpu) |
Omer Peleg | aa47324 | 2016-04-20 11:33:02 +0300 | [diff] [blame] | 4599 | { |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4600 | free_all_cpu_cached_iovas(cpu); |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4601 | return 0; |
Omer Peleg | aa47324 | 2016-04-20 11:33:02 +0300 | [diff] [blame] | 4602 | } |
| 4603 | |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4604 | static void intel_disable_iommus(void) |
| 4605 | { |
| 4606 | struct intel_iommu *iommu = NULL; |
| 4607 | struct dmar_drhd_unit *drhd; |
| 4608 | |
| 4609 | for_each_iommu(iommu, drhd) |
| 4610 | iommu_disable_translation(iommu); |
| 4611 | } |
| 4612 | |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4613 | static inline struct intel_iommu *dev_to_intel_iommu(struct device *dev) |
| 4614 | { |
Joerg Roedel | 2926a2aa | 2017-08-14 17:19:26 +0200 | [diff] [blame] | 4615 | struct iommu_device *iommu_dev = dev_to_iommu_device(dev); |
| 4616 | |
| 4617 | return container_of(iommu_dev, struct intel_iommu, iommu); |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4618 | } |
| 4619 | |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4620 | static ssize_t intel_iommu_show_version(struct device *dev, |
| 4621 | struct device_attribute *attr, |
| 4622 | char *buf) |
| 4623 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4624 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4625 | u32 ver = readl(iommu->reg + DMAR_VER_REG); |
| 4626 | return sprintf(buf, "%d:%d\n", |
| 4627 | DMAR_VER_MAJOR(ver), DMAR_VER_MINOR(ver)); |
| 4628 | } |
| 4629 | static DEVICE_ATTR(version, S_IRUGO, intel_iommu_show_version, NULL); |
| 4630 | |
| 4631 | static ssize_t intel_iommu_show_address(struct device *dev, |
| 4632 | struct device_attribute *attr, |
| 4633 | char *buf) |
| 4634 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4635 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4636 | return sprintf(buf, "%llx\n", iommu->reg_phys); |
| 4637 | } |
| 4638 | static DEVICE_ATTR(address, S_IRUGO, intel_iommu_show_address, NULL); |
| 4639 | |
| 4640 | static ssize_t intel_iommu_show_cap(struct device *dev, |
| 4641 | struct device_attribute *attr, |
| 4642 | char *buf) |
| 4643 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4644 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4645 | return sprintf(buf, "%llx\n", iommu->cap); |
| 4646 | } |
| 4647 | static DEVICE_ATTR(cap, S_IRUGO, intel_iommu_show_cap, NULL); |
| 4648 | |
| 4649 | static ssize_t intel_iommu_show_ecap(struct device *dev, |
| 4650 | struct device_attribute *attr, |
| 4651 | char *buf) |
| 4652 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4653 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4654 | return sprintf(buf, "%llx\n", iommu->ecap); |
| 4655 | } |
| 4656 | static DEVICE_ATTR(ecap, S_IRUGO, intel_iommu_show_ecap, NULL); |
| 4657 | |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4658 | static ssize_t intel_iommu_show_ndoms(struct device *dev, |
| 4659 | struct device_attribute *attr, |
| 4660 | char *buf) |
| 4661 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4662 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4663 | return sprintf(buf, "%ld\n", cap_ndoms(iommu->cap)); |
| 4664 | } |
| 4665 | static DEVICE_ATTR(domains_supported, S_IRUGO, intel_iommu_show_ndoms, NULL); |
| 4666 | |
| 4667 | static ssize_t intel_iommu_show_ndoms_used(struct device *dev, |
| 4668 | struct device_attribute *attr, |
| 4669 | char *buf) |
| 4670 | { |
Joerg Roedel | a7fdb6e | 2017-02-28 13:57:18 +0100 | [diff] [blame] | 4671 | struct intel_iommu *iommu = dev_to_intel_iommu(dev); |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4672 | return sprintf(buf, "%d\n", bitmap_weight(iommu->domain_ids, |
| 4673 | cap_ndoms(iommu->cap))); |
| 4674 | } |
| 4675 | static DEVICE_ATTR(domains_used, S_IRUGO, intel_iommu_show_ndoms_used, NULL); |
| 4676 | |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4677 | static struct attribute *intel_iommu_attrs[] = { |
| 4678 | &dev_attr_version.attr, |
| 4679 | &dev_attr_address.attr, |
| 4680 | &dev_attr_cap.attr, |
| 4681 | &dev_attr_ecap.attr, |
Alex Williamson | 2238c08 | 2015-07-14 15:24:53 -0600 | [diff] [blame] | 4682 | &dev_attr_domains_supported.attr, |
| 4683 | &dev_attr_domains_used.attr, |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4684 | NULL, |
| 4685 | }; |
| 4686 | |
| 4687 | static struct attribute_group intel_iommu_group = { |
| 4688 | .name = "intel-iommu", |
| 4689 | .attrs = intel_iommu_attrs, |
| 4690 | }; |
| 4691 | |
| 4692 | const struct attribute_group *intel_iommu_groups[] = { |
| 4693 | &intel_iommu_group, |
| 4694 | NULL, |
| 4695 | }; |
| 4696 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4697 | int __init intel_iommu_init(void) |
| 4698 | { |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4699 | int ret = -ENODEV; |
Takao Indoh | 3a93c84 | 2013-04-23 17:35:03 +0900 | [diff] [blame] | 4700 | struct dmar_drhd_unit *drhd; |
Jiang Liu | 7c91977 | 2014-01-06 14:18:18 +0800 | [diff] [blame] | 4701 | struct intel_iommu *iommu; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4702 | |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4703 | /* VT-d is required for a TXT/tboot launch, so enforce that */ |
| 4704 | force_on = tboot_force_iommu(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4705 | |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4706 | if (iommu_init_mempool()) { |
| 4707 | if (force_on) |
| 4708 | panic("tboot: Failed to initialize iommu memory\n"); |
| 4709 | return -ENOMEM; |
| 4710 | } |
| 4711 | |
| 4712 | down_write(&dmar_global_lock); |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4713 | if (dmar_table_init()) { |
| 4714 | if (force_on) |
| 4715 | panic("tboot: Failed to initialize DMAR table\n"); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4716 | goto out_free_dmar; |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4717 | } |
| 4718 | |
Suresh Siddha | c2c7286 | 2011-08-23 17:05:19 -0700 | [diff] [blame] | 4719 | if (dmar_dev_scope_init() < 0) { |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4720 | if (force_on) |
| 4721 | panic("tboot: Failed to initialize DMAR device scope\n"); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4722 | goto out_free_dmar; |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4723 | } |
Suresh Siddha | 1886e8a | 2008-07-10 11:16:37 -0700 | [diff] [blame] | 4724 | |
Joerg Roedel | ec154bf | 2017-10-06 15:00:53 +0200 | [diff] [blame] | 4725 | up_write(&dmar_global_lock); |
| 4726 | |
| 4727 | /* |
| 4728 | * The bus notifier takes the dmar_global_lock, so lockdep will |
| 4729 | * complain later when we register it under the lock. |
| 4730 | */ |
| 4731 | dmar_register_bus_notifier(); |
| 4732 | |
| 4733 | down_write(&dmar_global_lock); |
| 4734 | |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4735 | if (no_iommu || dmar_disabled) { |
| 4736 | /* |
Shaohua Li | bfd20f1 | 2017-04-26 09:18:35 -0700 | [diff] [blame] | 4737 | * We exit the function here to ensure IOMMU's remapping and |
| 4738 | * mempool aren't setup, which means that the IOMMU's PMRs |
| 4739 | * won't be disabled via the call to init_dmars(). So disable |
| 4740 | * it explicitly here. The PMRs were setup by tboot prior to |
| 4741 | * calling SENTER, but the kernel is expected to reset/tear |
| 4742 | * down the PMRs. |
| 4743 | */ |
| 4744 | if (intel_iommu_tboot_noforce) { |
| 4745 | for_each_iommu(iommu, drhd) |
| 4746 | iommu_disable_protect_mem_regions(iommu); |
| 4747 | } |
| 4748 | |
| 4749 | /* |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4750 | * Make sure the IOMMUs are switched off, even when we |
| 4751 | * boot into a kexec kernel and the previous kernel left |
| 4752 | * them enabled |
| 4753 | */ |
| 4754 | intel_disable_iommus(); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4755 | goto out_free_dmar; |
Joerg Roedel | 161b28a | 2017-03-28 17:04:52 +0200 | [diff] [blame] | 4756 | } |
Suresh Siddha | 2ae2101 | 2008-07-10 11:16:43 -0700 | [diff] [blame] | 4757 | |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4758 | if (list_empty(&dmar_rmrr_units)) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4759 | pr_info("No RMRR found\n"); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4760 | |
| 4761 | if (list_empty(&dmar_atsr_units)) |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4762 | pr_info("No ATSR found\n"); |
Suresh Siddha | 318fe7d | 2011-08-23 17:05:20 -0700 | [diff] [blame] | 4763 | |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 4764 | if (dmar_init_reserved_ranges()) { |
| 4765 | if (force_on) |
| 4766 | panic("tboot: Failed to reserve iommu ranges\n"); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4767 | goto out_free_reserved_range; |
Joseph Cihula | 51a63e6 | 2011-03-21 11:04:24 -0700 | [diff] [blame] | 4768 | } |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4769 | |
| 4770 | init_no_remapping_devices(); |
| 4771 | |
Joseph Cihula | b779260 | 2011-05-03 00:08:37 -0700 | [diff] [blame] | 4772 | ret = init_dmars(); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4773 | if (ret) { |
Joseph Cihula | a59b50e | 2009-06-30 19:31:10 -0700 | [diff] [blame] | 4774 | if (force_on) |
| 4775 | panic("tboot: Failed to initialize DMARs\n"); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4776 | pr_err("Initialization failed\n"); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4777 | goto out_free_reserved_range; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4778 | } |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4779 | up_write(&dmar_global_lock); |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4780 | pr_info("Intel(R) Virtualization Technology for Directed I/O\n"); |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4781 | |
Christoph Hellwig | 4fac807 | 2017-12-24 13:57:08 +0100 | [diff] [blame] | 4782 | #if defined(CONFIG_X86) && defined(CONFIG_SWIOTLB) |
FUJITA Tomonori | 75f1cdf | 2009-11-10 19:46:20 +0900 | [diff] [blame] | 4783 | swiotlb = 0; |
| 4784 | #endif |
David Woodhouse | 19943b0 | 2009-08-04 16:19:20 +0100 | [diff] [blame] | 4785 | dma_ops = &intel_dma_ops; |
Fenghua Yu | 4ed0d3e | 2009-04-24 17:30:20 -0700 | [diff] [blame] | 4786 | |
Rafael J. Wysocki | 134fac3 | 2011-03-23 22:16:14 +0100 | [diff] [blame] | 4787 | init_iommu_pm_ops(); |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 4788 | |
Joerg Roedel | 39ab955 | 2017-02-01 16:56:46 +0100 | [diff] [blame] | 4789 | for_each_active_iommu(iommu, drhd) { |
| 4790 | iommu_device_sysfs_add(&iommu->iommu, NULL, |
| 4791 | intel_iommu_groups, |
| 4792 | "%s", iommu->name); |
| 4793 | iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops); |
| 4794 | iommu_device_register(&iommu->iommu); |
| 4795 | } |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 4796 | |
Joerg Roedel | 4236d97d | 2011-09-06 17:56:07 +0200 | [diff] [blame] | 4797 | bus_set_iommu(&pci_bus_type, &intel_iommu_ops); |
Fenghua Yu | 99dcade | 2009-11-11 07:23:06 -0800 | [diff] [blame] | 4798 | bus_register_notifier(&pci_bus_type, &device_nb); |
Jiang Liu | 75f0556 | 2014-02-19 14:07:37 +0800 | [diff] [blame] | 4799 | if (si_domain && !hw_pass_through) |
| 4800 | register_memory_notifier(&intel_iommu_memory_nb); |
Anna-Maria Gleixner | 2164761 | 2016-11-27 00:13:41 +0100 | [diff] [blame] | 4801 | cpuhp_setup_state(CPUHP_IOMMU_INTEL_DEAD, "iommu/intel:dead", NULL, |
| 4802 | intel_iommu_cpu_dead); |
Eugeni Dodonov | 8bc1f85 | 2011-11-23 16:42:14 -0200 | [diff] [blame] | 4803 | intel_iommu_enabled = 1; |
Sohil Mehta | ee2636b | 2018-09-11 17:11:38 -0700 | [diff] [blame] | 4804 | intel_iommu_debugfs_init(); |
Eugeni Dodonov | 8bc1f85 | 2011-11-23 16:42:14 -0200 | [diff] [blame] | 4805 | |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4806 | return 0; |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4807 | |
| 4808 | out_free_reserved_range: |
| 4809 | put_iova_domain(&reserved_iova_list); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4810 | out_free_dmar: |
| 4811 | intel_iommu_free_dmars(); |
Jiang Liu | 3a5670e | 2014-02-19 14:07:33 +0800 | [diff] [blame] | 4812 | up_write(&dmar_global_lock); |
| 4813 | iommu_exit_mempool(); |
Jiang Liu | 9bdc531 | 2014-01-06 14:18:27 +0800 | [diff] [blame] | 4814 | return ret; |
Keshavamurthy, Anil S | ba39592 | 2007-10-21 16:41:49 -0700 | [diff] [blame] | 4815 | } |
Keshavamurthy, Anil S | e820482 | 2007-10-21 16:41:55 -0700 | [diff] [blame] | 4816 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4817 | 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] | 4818 | { |
| 4819 | struct intel_iommu *iommu = opaque; |
| 4820 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4821 | domain_context_clear_one(iommu, PCI_BUS_NUM(alias), alias & 0xff); |
Alex Williamson | 579305f | 2014-07-03 09:51:43 -0600 | [diff] [blame] | 4822 | return 0; |
| 4823 | } |
| 4824 | |
| 4825 | /* |
| 4826 | * NB - intel-iommu lacks any sort of reference counting for the users of |
| 4827 | * dependent devices. If multiple endpoints have intersecting dependent |
| 4828 | * devices, unbinding the driver from any one of them will possibly leave |
| 4829 | * the others unable to operate. |
| 4830 | */ |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4831 | static void domain_context_clear(struct intel_iommu *iommu, struct device *dev) |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 4832 | { |
David Woodhouse | 0bcb3e2 | 2014-03-06 17:12:03 +0000 | [diff] [blame] | 4833 | if (!iommu || !dev || !dev_is_pci(dev)) |
Han, Weidong | 3199aa6 | 2009-02-26 17:31:12 +0800 | [diff] [blame] | 4834 | return; |
| 4835 | |
Joerg Roedel | 2452d9d | 2015-07-23 16:20:14 +0200 | [diff] [blame] | 4836 | 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] | 4837 | } |
| 4838 | |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4839 | static void __dmar_remove_one_dev_info(struct device_domain_info *info) |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4840 | { |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4841 | struct intel_iommu *iommu; |
| 4842 | unsigned long flags; |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4843 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 4844 | assert_spin_locked(&device_domain_lock); |
| 4845 | |
Joerg Roedel | b608ac3 | 2015-07-21 18:19:08 +0200 | [diff] [blame] | 4846 | if (WARN_ON(!info)) |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4847 | return; |
| 4848 | |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4849 | iommu = info->iommu; |
| 4850 | |
| 4851 | if (info->dev) { |
Lu Baolu | ef848b7 | 2018-12-10 09:59:01 +0800 | [diff] [blame] | 4852 | if (dev_is_pci(info->dev) && sm_supported(iommu)) |
| 4853 | intel_pasid_tear_down_entry(iommu, info->dev, |
| 4854 | PASID_RID2PASID); |
| 4855 | |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4856 | iommu_disable_dev_iotlb(info); |
| 4857 | domain_context_clear(iommu, info->dev); |
Lu Baolu | a7fc93f | 2018-07-14 15:47:00 +0800 | [diff] [blame] | 4858 | intel_pasid_free_table(info->dev); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4859 | } |
| 4860 | |
Joerg Roedel | b608ac3 | 2015-07-21 18:19:08 +0200 | [diff] [blame] | 4861 | unlink_domain_info(info); |
Roland Dreier | 3e7abe2 | 2011-07-20 06:22:21 -0700 | [diff] [blame] | 4862 | |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4863 | spin_lock_irqsave(&iommu->lock, flags); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4864 | domain_detach_iommu(info->domain, iommu); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4865 | spin_unlock_irqrestore(&iommu->lock, flags); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4866 | |
| 4867 | free_devinfo_mem(info); |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4868 | } |
| 4869 | |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 4870 | static void dmar_remove_one_dev_info(struct dmar_domain *domain, |
| 4871 | struct device *dev) |
| 4872 | { |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4873 | struct device_domain_info *info; |
Joerg Roedel | 55d9404 | 2015-07-22 16:50:40 +0200 | [diff] [blame] | 4874 | unsigned long flags; |
| 4875 | |
Weidong Han | c7151a8 | 2008-12-08 22:51:37 +0800 | [diff] [blame] | 4876 | spin_lock_irqsave(&device_domain_lock, flags); |
Joerg Roedel | 127c761 | 2015-07-23 17:44:46 +0200 | [diff] [blame] | 4877 | info = dev->archdata.iommu; |
| 4878 | __dmar_remove_one_dev_info(info); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4879 | spin_unlock_irqrestore(&device_domain_lock, flags); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | static int md_domain_init(struct dmar_domain *domain, int guest_width) |
| 4883 | { |
| 4884 | int adjust_width; |
| 4885 | |
Zhen Lei | aa3ac94 | 2017-09-21 16:52:45 +0100 | [diff] [blame] | 4886 | init_iova_domain(&domain->iovad, VTD_PAGE_SIZE, IOVA_START_PFN); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4887 | domain_reserve_special_ranges(domain); |
| 4888 | |
| 4889 | /* calculate AGAW */ |
| 4890 | domain->gaw = guest_width; |
| 4891 | adjust_width = guestwidth_to_adjustwidth(guest_width); |
| 4892 | domain->agaw = width_to_agaw(adjust_width); |
| 4893 | |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4894 | domain->iommu_coherency = 0; |
Sheng Yang | c5b1525 | 2009-08-06 13:31:56 +0800 | [diff] [blame] | 4895 | domain->iommu_snooping = 0; |
Youquan Song | 6dd9a7c | 2011-05-25 19:13:49 +0100 | [diff] [blame] | 4896 | domain->iommu_superpage = 0; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4897 | domain->max_addr = 0; |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4898 | |
| 4899 | /* always allocate the top pgd */ |
Suresh Siddha | 4c923d4 | 2009-10-02 11:01:24 -0700 | [diff] [blame] | 4900 | domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid); |
Weidong Han | 5e98c4b | 2008-12-08 23:03:27 +0800 | [diff] [blame] | 4901 | if (!domain->pgd) |
| 4902 | return -ENOMEM; |
| 4903 | domain_flush_cache(domain, domain->pgd, PAGE_SIZE); |
| 4904 | return 0; |
| 4905 | } |
| 4906 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4907 | static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4908 | { |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 4909 | struct dmar_domain *dmar_domain; |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4910 | struct iommu_domain *domain; |
| 4911 | |
| 4912 | if (type != IOMMU_DOMAIN_UNMANAGED) |
| 4913 | return NULL; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4914 | |
Jiang Liu | ab8dfe2 | 2014-07-11 14:19:27 +0800 | [diff] [blame] | 4915 | dmar_domain = alloc_domain(DOMAIN_FLAG_VIRTUAL_MACHINE); |
Joerg Roedel | 5d45080 | 2008-12-03 14:52:32 +0100 | [diff] [blame] | 4916 | if (!dmar_domain) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4917 | pr_err("Can't allocate dmar_domain\n"); |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4918 | return NULL; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4919 | } |
Fenghua Yu | 2c2e2c3 | 2009-06-19 13:47:29 -0700 | [diff] [blame] | 4920 | if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4921 | pr_err("Domain initialization failed\n"); |
Jiang Liu | 92d03cc | 2014-02-19 14:07:28 +0800 | [diff] [blame] | 4922 | domain_exit(dmar_domain); |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4923 | return NULL; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4924 | } |
Allen Kay | 8140a95 | 2011-10-14 12:32:17 -0700 | [diff] [blame] | 4925 | domain_update_iommu_cap(dmar_domain); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4926 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4927 | domain = &dmar_domain->domain; |
Joerg Roedel | 8a0e715 | 2012-01-26 19:40:54 +0100 | [diff] [blame] | 4928 | domain->geometry.aperture_start = 0; |
| 4929 | domain->geometry.aperture_end = __DOMAIN_MAX_ADDR(dmar_domain->gaw); |
| 4930 | domain->geometry.force_aperture = true; |
| 4931 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4932 | return domain; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4933 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4934 | |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4935 | static void intel_iommu_domain_free(struct iommu_domain *domain) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4936 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4937 | domain_exit(to_dmar_domain(domain)); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4938 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4939 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 4940 | static int intel_iommu_attach_device(struct iommu_domain *domain, |
| 4941 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4942 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 4943 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4944 | struct intel_iommu *iommu; |
| 4945 | int addr_width; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 4946 | u8 bus, devfn; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 4947 | |
Alex Williamson | c875d2c | 2014-07-03 09:57:02 -0600 | [diff] [blame] | 4948 | if (device_is_rmrr_locked(dev)) { |
| 4949 | dev_warn(dev, "Device is ineligible for IOMMU domain attach due to platform RMRR requirement. Contact your platform vendor.\n"); |
| 4950 | return -EPERM; |
| 4951 | } |
| 4952 | |
David Woodhouse | 7207d8f | 2014-03-09 16:31:06 -0700 | [diff] [blame] | 4953 | /* normally dev is not mapped */ |
| 4954 | if (unlikely(domain_context_mapped(dev))) { |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4955 | struct dmar_domain *old_domain; |
| 4956 | |
David Woodhouse | 1525a29 | 2014-03-06 16:19:30 +0000 | [diff] [blame] | 4957 | old_domain = find_domain(dev); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4958 | if (old_domain) { |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4959 | rcu_read_lock(); |
Joerg Roedel | de7e888 | 2015-07-22 11:58:07 +0200 | [diff] [blame] | 4960 | dmar_remove_one_dev_info(old_domain, dev); |
Joerg Roedel | d160aca | 2015-07-22 11:52:53 +0200 | [diff] [blame] | 4961 | rcu_read_unlock(); |
Joerg Roedel | 62c2216 | 2014-12-09 12:56:45 +0100 | [diff] [blame] | 4962 | |
| 4963 | if (!domain_type_is_vm_or_si(old_domain) && |
| 4964 | list_empty(&old_domain->devices)) |
| 4965 | domain_exit(old_domain); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 4966 | } |
| 4967 | } |
| 4968 | |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 4969 | iommu = device_to_iommu(dev, &bus, &devfn); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4970 | if (!iommu) |
| 4971 | return -ENODEV; |
| 4972 | |
| 4973 | /* check if this iommu agaw is sufficient for max mapped address */ |
| 4974 | addr_width = agaw_to_width(iommu->agaw); |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4975 | if (addr_width > cap_mgaw(iommu->cap)) |
| 4976 | addr_width = cap_mgaw(iommu->cap); |
| 4977 | |
| 4978 | if (dmar_domain->max_addr > (1LL << addr_width)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 4979 | pr_err("%s: iommu width (%d) is not " |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4980 | "sufficient for the mapped address (%llx)\n", |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4981 | __func__, addr_width, dmar_domain->max_addr); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 4982 | return -EFAULT; |
| 4983 | } |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4984 | dmar_domain->gaw = addr_width; |
| 4985 | |
| 4986 | /* |
| 4987 | * Knock out extra levels of page tables if necessary |
| 4988 | */ |
| 4989 | while (iommu->agaw < dmar_domain->agaw) { |
| 4990 | struct dma_pte *pte; |
| 4991 | |
| 4992 | pte = dmar_domain->pgd; |
| 4993 | if (dma_pte_present(pte)) { |
Sheng Yang | 25cbff1 | 2010-06-12 19:21:42 +0800 | [diff] [blame] | 4994 | dmar_domain->pgd = (struct dma_pte *) |
| 4995 | phys_to_virt(dma_pte_addr(pte)); |
Jan Kiszka | 7a66101 | 2010-11-02 08:05:51 +0100 | [diff] [blame] | 4996 | free_pgtable_page(pte); |
Tom Lyon | a99c47a | 2010-05-17 08:20:45 +0100 | [diff] [blame] | 4997 | } |
| 4998 | dmar_domain->agaw--; |
| 4999 | } |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5000 | |
Joerg Roedel | 28ccce0 | 2015-07-21 14:45:31 +0200 | [diff] [blame] | 5001 | return domain_add_dev_info(dmar_domain, dev); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5002 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5003 | |
Joerg Roedel | 4c5478c | 2008-12-03 14:58:24 +0100 | [diff] [blame] | 5004 | static void intel_iommu_detach_device(struct iommu_domain *domain, |
| 5005 | struct device *dev) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5006 | { |
Joerg Roedel | e6de0f8 | 2015-07-22 16:30:36 +0200 | [diff] [blame] | 5007 | dmar_remove_one_dev_info(to_dmar_domain(domain), dev); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5008 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5009 | |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 5010 | static int intel_iommu_map(struct iommu_domain *domain, |
| 5011 | unsigned long iova, phys_addr_t hpa, |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 5012 | size_t size, int iommu_prot) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5013 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 5014 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5015 | u64 max_addr; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5016 | int prot = 0; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5017 | int ret; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5018 | |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5019 | if (iommu_prot & IOMMU_READ) |
| 5020 | prot |= DMA_PTE_READ; |
| 5021 | if (iommu_prot & IOMMU_WRITE) |
| 5022 | prot |= DMA_PTE_WRITE; |
Sheng Yang | 9cf0669 | 2009-03-18 15:33:07 +0800 | [diff] [blame] | 5023 | if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping) |
| 5024 | prot |= DMA_PTE_SNP; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5025 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 5026 | max_addr = iova + size; |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5027 | if (dmar_domain->max_addr < max_addr) { |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5028 | u64 end; |
| 5029 | |
| 5030 | /* check if minimum agaw is sufficient for mapped address */ |
Tom Lyon | 8954da1 | 2010-05-17 08:19:52 +0100 | [diff] [blame] | 5031 | end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5032 | if (end < max_addr) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5033 | pr_err("%s: iommu width (%d) is not " |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5034 | "sufficient for the mapped address (%llx)\n", |
Tom Lyon | 8954da1 | 2010-05-17 08:19:52 +0100 | [diff] [blame] | 5035 | __func__, dmar_domain->gaw, max_addr); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5036 | return -EFAULT; |
| 5037 | } |
Joerg Roedel | dde57a2 | 2008-12-03 15:04:09 +0100 | [diff] [blame] | 5038 | dmar_domain->max_addr = max_addr; |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5039 | } |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 5040 | /* Round up size to next multiple of PAGE_SIZE, if it and |
| 5041 | the low bits of hpa would take us onto the next page */ |
David Woodhouse | 88cb6a7 | 2009-06-28 15:03:06 +0100 | [diff] [blame] | 5042 | size = aligned_nrpages(hpa, size); |
David Woodhouse | ad05122 | 2009-06-28 14:22:28 +0100 | [diff] [blame] | 5043 | ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT, |
| 5044 | hpa >> VTD_PAGE_SHIFT, size, prot); |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5045 | return ret; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5046 | } |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5047 | |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 5048 | static size_t intel_iommu_unmap(struct iommu_domain *domain, |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5049 | unsigned long iova, size_t size) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5050 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 5051 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5052 | struct page *freelist = NULL; |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5053 | unsigned long start_pfn, last_pfn; |
| 5054 | unsigned int npages; |
Joerg Roedel | 42e8c18 | 2015-07-21 15:50:02 +0200 | [diff] [blame] | 5055 | int iommu_id, level = 0; |
Sheng Yang | 4b99d35 | 2009-07-08 11:52:52 +0100 | [diff] [blame] | 5056 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5057 | /* Cope with horrid API which requires us to unmap more than the |
| 5058 | size argument if it happens to be a large-page mapping. */ |
Joerg Roedel | dc02e46 | 2015-08-13 11:15:13 +0200 | [diff] [blame] | 5059 | 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] | 5060 | |
| 5061 | if (size < VTD_PAGE_SIZE << level_to_offset_bits(level)) |
| 5062 | size = VTD_PAGE_SIZE << level_to_offset_bits(level); |
| 5063 | |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5064 | start_pfn = iova >> VTD_PAGE_SHIFT; |
| 5065 | last_pfn = (iova + size - 1) >> VTD_PAGE_SHIFT; |
| 5066 | |
| 5067 | freelist = domain_unmap(dmar_domain, start_pfn, last_pfn); |
| 5068 | |
| 5069 | npages = last_pfn - start_pfn + 1; |
| 5070 | |
Shaokun Zhang | f746a02 | 2018-03-22 18:18:06 +0800 | [diff] [blame] | 5071 | for_each_domain_iommu(iommu_id, dmar_domain) |
Joerg Roedel | 42e8c18 | 2015-07-21 15:50:02 +0200 | [diff] [blame] | 5072 | iommu_flush_iotlb_psi(g_iommus[iommu_id], dmar_domain, |
| 5073 | start_pfn, npages, !freelist, 0); |
David Woodhouse | ea8ea46 | 2014-03-05 17:09:32 +0000 | [diff] [blame] | 5074 | |
| 5075 | dma_free_pagelist(freelist); |
Weidong Han | fe40f1e | 2008-12-08 23:10:23 +0800 | [diff] [blame] | 5076 | |
David Woodhouse | 163cc52 | 2009-06-28 00:51:17 +0100 | [diff] [blame] | 5077 | if (dmar_domain->max_addr == iova + size) |
| 5078 | dmar_domain->max_addr = iova; |
Joerg Roedel | b146a1c9f | 2010-01-20 17:17:37 +0100 | [diff] [blame] | 5079 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5080 | return size; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5081 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5082 | |
Joerg Roedel | d14d657 | 2008-12-03 15:06:57 +0100 | [diff] [blame] | 5083 | 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] | 5084 | dma_addr_t iova) |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5085 | { |
Joerg Roedel | 00a77de | 2015-03-26 13:43:08 +0100 | [diff] [blame] | 5086 | struct dmar_domain *dmar_domain = to_dmar_domain(domain); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5087 | struct dma_pte *pte; |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5088 | int level = 0; |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5089 | u64 phys = 0; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5090 | |
David Woodhouse | 5cf0a76 | 2014-03-19 16:07:49 +0000 | [diff] [blame] | 5091 | 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] | 5092 | if (pte) |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5093 | phys = dma_pte_addr(pte); |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5094 | |
Weidong Han | faa3d6f | 2008-12-08 23:09:29 +0800 | [diff] [blame] | 5095 | return phys; |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5096 | } |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 5097 | |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5098 | static bool intel_iommu_capable(enum iommu_cap cap) |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5099 | { |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5100 | if (cap == IOMMU_CAP_CACHE_COHERENCY) |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5101 | return domain_update_iommu_snooping(NULL) == 1; |
Tom Lyon | 323f99c | 2010-07-02 16:56:14 -0400 | [diff] [blame] | 5102 | if (cap == IOMMU_CAP_INTR_REMAP) |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5103 | return irq_remapping_enabled == 1; |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5104 | |
Joerg Roedel | 5d587b8 | 2014-09-05 10:50:45 +0200 | [diff] [blame] | 5105 | return false; |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 5106 | } |
| 5107 | |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5108 | static int intel_iommu_add_device(struct device *dev) |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5109 | { |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5110 | struct intel_iommu *iommu; |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5111 | struct iommu_group *group; |
David Woodhouse | 156baca | 2014-03-09 14:00:57 -0700 | [diff] [blame] | 5112 | u8 bus, devfn; |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5113 | |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5114 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 5115 | if (!iommu) |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5116 | return -ENODEV; |
| 5117 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 5118 | iommu_device_link(&iommu->iommu, dev); |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5119 | |
Alex Williamson | e17f9ff | 2014-07-03 09:51:37 -0600 | [diff] [blame] | 5120 | group = iommu_group_get_for_dev(dev); |
Alex Williamson | 783f157 | 2012-05-30 14:19:43 -0600 | [diff] [blame] | 5121 | |
Alex Williamson | e17f9ff | 2014-07-03 09:51:37 -0600 | [diff] [blame] | 5122 | if (IS_ERR(group)) |
| 5123 | return PTR_ERR(group); |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5124 | |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5125 | iommu_group_put(group); |
Alex Williamson | e17f9ff | 2014-07-03 09:51:37 -0600 | [diff] [blame] | 5126 | return 0; |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | static void intel_iommu_remove_device(struct device *dev) |
| 5130 | { |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5131 | struct intel_iommu *iommu; |
| 5132 | u8 bus, devfn; |
| 5133 | |
| 5134 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 5135 | if (!iommu) |
| 5136 | return; |
| 5137 | |
Alex Williamson | abdfdde | 2012-05-30 14:19:19 -0600 | [diff] [blame] | 5138 | iommu_group_remove_device(dev); |
Alex Williamson | a5459cf | 2014-06-12 16:12:31 -0600 | [diff] [blame] | 5139 | |
Joerg Roedel | e3d10af | 2017-02-01 17:23:22 +0100 | [diff] [blame] | 5140 | iommu_device_unlink(&iommu->iommu, dev); |
Alex Williamson | 70ae6f0 | 2011-10-21 15:56:11 -0400 | [diff] [blame] | 5141 | } |
| 5142 | |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 5143 | static void intel_iommu_get_resv_regions(struct device *device, |
| 5144 | struct list_head *head) |
| 5145 | { |
| 5146 | struct iommu_resv_region *reg; |
| 5147 | struct dmar_rmrr_unit *rmrr; |
| 5148 | struct device *i_dev; |
| 5149 | int i; |
| 5150 | |
| 5151 | rcu_read_lock(); |
| 5152 | for_each_rmrr_units(rmrr) { |
| 5153 | for_each_active_dev_scope(rmrr->devices, rmrr->devices_cnt, |
| 5154 | i, i_dev) { |
| 5155 | if (i_dev != device) |
| 5156 | continue; |
| 5157 | |
| 5158 | list_add_tail(&rmrr->resv->list, head); |
| 5159 | } |
| 5160 | } |
| 5161 | rcu_read_unlock(); |
| 5162 | |
| 5163 | reg = iommu_alloc_resv_region(IOAPIC_RANGE_START, |
| 5164 | IOAPIC_RANGE_END - IOAPIC_RANGE_START + 1, |
Robin Murphy | 9d3a4de | 2017-03-16 17:00:16 +0000 | [diff] [blame] | 5165 | 0, IOMMU_RESV_MSI); |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 5166 | if (!reg) |
| 5167 | return; |
| 5168 | list_add_tail(®->list, head); |
| 5169 | } |
| 5170 | |
| 5171 | static void intel_iommu_put_resv_regions(struct device *dev, |
| 5172 | struct list_head *head) |
| 5173 | { |
| 5174 | struct iommu_resv_region *entry, *next; |
| 5175 | |
| 5176 | list_for_each_entry_safe(entry, next, head, list) { |
| 5177 | if (entry->type == IOMMU_RESV_RESERVED) |
| 5178 | kfree(entry); |
| 5179 | } |
Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 5180 | } |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 5181 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5182 | #ifdef CONFIG_INTEL_IOMMU_SVM |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5183 | #define MAX_NR_PASID_BITS (20) |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5184 | static inline unsigned long intel_iommu_get_pts(struct device *dev) |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5185 | { |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5186 | int pts, max_pasid; |
| 5187 | |
| 5188 | max_pasid = intel_pasid_get_dev_max_id(dev); |
| 5189 | pts = find_first_bit((unsigned long *)&max_pasid, MAX_NR_PASID_BITS); |
| 5190 | if (pts < 5) |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5191 | return 0; |
| 5192 | |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5193 | return pts - 5; |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5194 | } |
| 5195 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5196 | int intel_iommu_enable_pasid(struct intel_iommu *iommu, struct intel_svm_dev *sdev) |
| 5197 | { |
| 5198 | struct device_domain_info *info; |
| 5199 | struct context_entry *context; |
| 5200 | struct dmar_domain *domain; |
| 5201 | unsigned long flags; |
| 5202 | u64 ctx_lo; |
| 5203 | int ret; |
| 5204 | |
| 5205 | domain = get_valid_domain_for_dev(sdev->dev); |
| 5206 | if (!domain) |
| 5207 | return -EINVAL; |
| 5208 | |
| 5209 | spin_lock_irqsave(&device_domain_lock, flags); |
| 5210 | spin_lock(&iommu->lock); |
| 5211 | |
| 5212 | ret = -EINVAL; |
| 5213 | info = sdev->dev->archdata.iommu; |
| 5214 | if (!info || !info->pasid_supported) |
| 5215 | goto out; |
| 5216 | |
| 5217 | context = iommu_context_addr(iommu, info->bus, info->devfn, 0); |
| 5218 | if (WARN_ON(!context)) |
| 5219 | goto out; |
| 5220 | |
| 5221 | ctx_lo = context[0].lo; |
| 5222 | |
| 5223 | sdev->did = domain->iommu_did[iommu->seq_id]; |
| 5224 | sdev->sid = PCI_DEVID(info->bus, info->devfn); |
| 5225 | |
| 5226 | if (!(ctx_lo & CONTEXT_PASIDE)) { |
Ashok Raj | 11b93eb | 2017-08-08 13:29:28 -0700 | [diff] [blame] | 5227 | if (iommu->pasid_state_table) |
| 5228 | context[1].hi = (u64)virt_to_phys(iommu->pasid_state_table); |
Lu Baolu | 4774cc5 | 2018-07-14 15:47:01 +0800 | [diff] [blame] | 5229 | context[1].lo = (u64)virt_to_phys(info->pasid_table->table) | |
| 5230 | intel_iommu_get_pts(sdev->dev); |
Jacob Pan | 65ca7f5 | 2016-12-06 10:14:23 -0800 | [diff] [blame] | 5231 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5232 | wmb(); |
| 5233 | /* CONTEXT_TT_MULTI_LEVEL and CONTEXT_TT_DEV_IOTLB are both |
| 5234 | * extended to permit requests-with-PASID if the PASIDE bit |
| 5235 | * is set. which makes sense. For CONTEXT_TT_PASS_THROUGH, |
| 5236 | * however, the PASIDE bit is ignored and requests-with-PASID |
| 5237 | * are unconditionally blocked. Which makes less sense. |
| 5238 | * So convert from CONTEXT_TT_PASS_THROUGH to one of the new |
| 5239 | * "guest mode" translation types depending on whether ATS |
| 5240 | * is available or not. Annoyingly, we can't use the new |
| 5241 | * modes *unless* PASIDE is set. */ |
| 5242 | if ((ctx_lo & CONTEXT_TT_MASK) == (CONTEXT_TT_PASS_THROUGH << 2)) { |
| 5243 | ctx_lo &= ~CONTEXT_TT_MASK; |
| 5244 | if (info->ats_supported) |
| 5245 | ctx_lo |= CONTEXT_TT_PT_PASID_DEV_IOTLB << 2; |
| 5246 | else |
| 5247 | ctx_lo |= CONTEXT_TT_PT_PASID << 2; |
| 5248 | } |
| 5249 | ctx_lo |= CONTEXT_PASIDE; |
David Woodhouse | 907fea3 | 2015-10-13 14:11:13 +0100 | [diff] [blame] | 5250 | if (iommu->pasid_state_table) |
| 5251 | ctx_lo |= CONTEXT_DINVE; |
David Woodhouse | a222a7f | 2015-10-07 23:35:18 +0100 | [diff] [blame] | 5252 | if (info->pri_supported) |
| 5253 | ctx_lo |= CONTEXT_PRS; |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5254 | context[0].lo = ctx_lo; |
| 5255 | wmb(); |
| 5256 | iommu->flush.flush_context(iommu, sdev->did, sdev->sid, |
| 5257 | DMA_CCMD_MASK_NOBIT, |
| 5258 | DMA_CCMD_DEVICE_INVL); |
| 5259 | } |
| 5260 | |
| 5261 | /* Enable PASID support in the device, if it wasn't already */ |
| 5262 | if (!info->pasid_enabled) |
| 5263 | iommu_enable_dev_iotlb(info); |
| 5264 | |
| 5265 | if (info->ats_enabled) { |
| 5266 | sdev->dev_iotlb = 1; |
| 5267 | sdev->qdep = info->ats_qdep; |
| 5268 | if (sdev->qdep >= QI_DEV_EIOTLB_MAX_INVS) |
| 5269 | sdev->qdep = 0; |
| 5270 | } |
| 5271 | ret = 0; |
| 5272 | |
| 5273 | out: |
| 5274 | spin_unlock(&iommu->lock); |
| 5275 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 5276 | |
| 5277 | return ret; |
| 5278 | } |
| 5279 | |
| 5280 | struct intel_iommu *intel_svm_device_to_iommu(struct device *dev) |
| 5281 | { |
| 5282 | struct intel_iommu *iommu; |
| 5283 | u8 bus, devfn; |
| 5284 | |
| 5285 | if (iommu_dummy(dev)) { |
| 5286 | dev_warn(dev, |
| 5287 | "No IOMMU translation for device; cannot enable SVM\n"); |
| 5288 | return NULL; |
| 5289 | } |
| 5290 | |
| 5291 | iommu = device_to_iommu(dev, &bus, &devfn); |
| 5292 | if ((!iommu)) { |
Sudeep Dutt | b9997e3 | 2015-10-18 20:54:37 -0700 | [diff] [blame] | 5293 | dev_err(dev, "No IOMMU for device; cannot enable SVM\n"); |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5294 | return NULL; |
| 5295 | } |
| 5296 | |
David Woodhouse | 2f26e0a | 2015-09-09 11:40:47 +0100 | [diff] [blame] | 5297 | return iommu; |
| 5298 | } |
| 5299 | #endif /* CONFIG_INTEL_IOMMU_SVM */ |
| 5300 | |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 5301 | const struct iommu_ops intel_iommu_ops = { |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 5302 | .capable = intel_iommu_capable, |
| 5303 | .domain_alloc = intel_iommu_domain_alloc, |
| 5304 | .domain_free = intel_iommu_domain_free, |
| 5305 | .attach_dev = intel_iommu_attach_device, |
| 5306 | .detach_dev = intel_iommu_detach_device, |
| 5307 | .map = intel_iommu_map, |
| 5308 | .unmap = intel_iommu_unmap, |
Eric Auger | 0659b8d | 2017-01-19 20:57:53 +0000 | [diff] [blame] | 5309 | .iova_to_phys = intel_iommu_iova_to_phys, |
| 5310 | .add_device = intel_iommu_add_device, |
| 5311 | .remove_device = intel_iommu_remove_device, |
| 5312 | .get_resv_regions = intel_iommu_get_resv_regions, |
| 5313 | .put_resv_regions = intel_iommu_put_resv_regions, |
| 5314 | .device_group = pci_device_group, |
| 5315 | .pgsize_bitmap = INTEL_IOMMU_PGSIZES, |
Joerg Roedel | a8bcbb0d | 2008-12-03 15:14:02 +0100 | [diff] [blame] | 5316 | }; |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5317 | |
Daniel Vetter | 9452618 | 2013-01-20 23:50:13 +0100 | [diff] [blame] | 5318 | static void quirk_iommu_g4x_gfx(struct pci_dev *dev) |
| 5319 | { |
| 5320 | /* G4x/GM45 integrated gfx dmar support is totally busted. */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5321 | pr_info("Disabling IOMMU for graphics on this chipset\n"); |
Daniel Vetter | 9452618 | 2013-01-20 23:50:13 +0100 | [diff] [blame] | 5322 | dmar_map_gfx = 0; |
| 5323 | } |
| 5324 | |
| 5325 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_g4x_gfx); |
| 5326 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_g4x_gfx); |
| 5327 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_g4x_gfx); |
| 5328 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_g4x_gfx); |
| 5329 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_g4x_gfx); |
| 5330 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_g4x_gfx); |
| 5331 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_g4x_gfx); |
| 5332 | |
Greg Kroah-Hartman | d34d651 | 2012-12-21 15:05:21 -0800 | [diff] [blame] | 5333 | static void quirk_iommu_rwbf(struct pci_dev *dev) |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5334 | { |
| 5335 | /* |
| 5336 | * Mobile 4 Series Chipset neglects to set RWBF capability, |
Daniel Vetter | 210561f | 2013-01-21 19:48:59 +0100 | [diff] [blame] | 5337 | * but needs it. Same seems to hold for the desktop versions. |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5338 | */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5339 | pr_info("Forcing write-buffer flush capability\n"); |
David Woodhouse | 9af8814 | 2009-02-13 23:18:03 +0000 | [diff] [blame] | 5340 | rwbf_quirk = 1; |
| 5341 | } |
| 5342 | |
| 5343 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf); |
Daniel Vetter | 210561f | 2013-01-21 19:48:59 +0100 | [diff] [blame] | 5344 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e00, quirk_iommu_rwbf); |
| 5345 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e10, quirk_iommu_rwbf); |
| 5346 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e20, quirk_iommu_rwbf); |
| 5347 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e30, quirk_iommu_rwbf); |
| 5348 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e40, quirk_iommu_rwbf); |
| 5349 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2e90, quirk_iommu_rwbf); |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 5350 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 5351 | #define GGC 0x52 |
| 5352 | #define GGC_MEMORY_SIZE_MASK (0xf << 8) |
| 5353 | #define GGC_MEMORY_SIZE_NONE (0x0 << 8) |
| 5354 | #define GGC_MEMORY_SIZE_1M (0x1 << 8) |
| 5355 | #define GGC_MEMORY_SIZE_2M (0x3 << 8) |
| 5356 | #define GGC_MEMORY_VT_ENABLED (0x8 << 8) |
| 5357 | #define GGC_MEMORY_SIZE_2M_VT (0x9 << 8) |
| 5358 | #define GGC_MEMORY_SIZE_3M_VT (0xa << 8) |
| 5359 | #define GGC_MEMORY_SIZE_4M_VT (0xb << 8) |
| 5360 | |
Greg Kroah-Hartman | d34d651 | 2012-12-21 15:05:21 -0800 | [diff] [blame] | 5361 | static void quirk_calpella_no_shadow_gtt(struct pci_dev *dev) |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 5362 | { |
| 5363 | unsigned short ggc; |
| 5364 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 5365 | if (pci_read_config_word(dev, GGC, &ggc)) |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 5366 | return; |
| 5367 | |
Adam Jackson | eecfd57 | 2010-08-25 21:17:34 +0100 | [diff] [blame] | 5368 | if (!(ggc & GGC_MEMORY_VT_ENABLED)) { |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5369 | 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] | 5370 | dmar_map_gfx = 0; |
David Woodhouse | 6fbcfb3 | 2011-09-25 19:11:14 -0700 | [diff] [blame] | 5371 | } else if (dmar_map_gfx) { |
| 5372 | /* we have to ensure the gfx device is idle before we flush */ |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5373 | pr_info("Disabling batched IOTLB flush on Ironlake\n"); |
David Woodhouse | 6fbcfb3 | 2011-09-25 19:11:14 -0700 | [diff] [blame] | 5374 | intel_iommu_strict = 1; |
| 5375 | } |
David Woodhouse | 9eecabc | 2010-09-21 22:28:23 +0100 | [diff] [blame] | 5376 | } |
| 5377 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt); |
| 5378 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt); |
| 5379 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt); |
| 5380 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt); |
| 5381 | |
David Woodhouse | e0fc7e0 | 2009-09-30 09:12:17 -0700 | [diff] [blame] | 5382 | /* On Tylersburg chipsets, some BIOSes have been known to enable the |
| 5383 | ISOCH DMAR unit for the Azalia sound device, but not give it any |
| 5384 | TLB entries, which causes it to deadlock. Check for that. We do |
| 5385 | this in a function called from init_dmars(), instead of in a PCI |
| 5386 | quirk, because we don't want to print the obnoxious "BIOS broken" |
| 5387 | message if VT-d is actually disabled. |
| 5388 | */ |
| 5389 | static void __init check_tylersburg_isoch(void) |
| 5390 | { |
| 5391 | struct pci_dev *pdev; |
| 5392 | uint32_t vtisochctrl; |
| 5393 | |
| 5394 | /* If there's no Azalia in the system anyway, forget it. */ |
| 5395 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL); |
| 5396 | if (!pdev) |
| 5397 | return; |
| 5398 | pci_dev_put(pdev); |
| 5399 | |
| 5400 | /* System Management Registers. Might be hidden, in which case |
| 5401 | we can't do the sanity check. But that's OK, because the |
| 5402 | known-broken BIOSes _don't_ actually hide it, so far. */ |
| 5403 | pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL); |
| 5404 | if (!pdev) |
| 5405 | return; |
| 5406 | |
| 5407 | if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) { |
| 5408 | pci_dev_put(pdev); |
| 5409 | return; |
| 5410 | } |
| 5411 | |
| 5412 | pci_dev_put(pdev); |
| 5413 | |
| 5414 | /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */ |
| 5415 | if (vtisochctrl & 1) |
| 5416 | return; |
| 5417 | |
| 5418 | /* Drop all bits other than the number of TLB entries */ |
| 5419 | vtisochctrl &= 0x1c; |
| 5420 | |
| 5421 | /* If we have the recommended number of TLB entries (16), fine. */ |
| 5422 | if (vtisochctrl == 0x10) |
| 5423 | return; |
| 5424 | |
| 5425 | /* Zero TLB entries? You get to ride the short bus to school. */ |
| 5426 | if (!vtisochctrl) { |
| 5427 | WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n" |
| 5428 | "BIOS vendor: %s; Ver: %s; Product Version: %s\n", |
| 5429 | dmi_get_system_info(DMI_BIOS_VENDOR), |
| 5430 | dmi_get_system_info(DMI_BIOS_VERSION), |
| 5431 | dmi_get_system_info(DMI_PRODUCT_VERSION)); |
| 5432 | iommu_identity_mapping |= IDENTMAP_AZALIA; |
| 5433 | return; |
| 5434 | } |
Joerg Roedel | 9f10e5b | 2015-06-12 09:57:06 +0200 | [diff] [blame] | 5435 | |
| 5436 | 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] | 5437 | vtisochctrl); |
| 5438 | } |