blob: 4e4e0202e59d863838dfc66ae5ef6d5bbd8e1beb [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
2 * Copyright (c) 2006, Intel Corporation.
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 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
mark gross98bcef52008-02-23 15:23:35 -080017 * Copyright (C) 2006-2008 Intel Corporation
18 * Author: Ashok Raj <ashok.raj@intel.com>
19 * Author: Shaohua Li <shaohua.li@intel.com>
20 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Fenghua Yu5b6985c2008-10-16 18:02:32 -070021 * Author: Fenghua Yu <fenghua.yu@intel.com>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070022 */
23
24#include <linux/init.h>
25#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080026#include <linux/debugfs.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#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>
mark gross5e0d2a62008-03-04 15:22:08 -080035#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030036#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010037#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030038#include <linux/intel-iommu.h>
Rafael J. Wysocki134fac32011-03-23 22:16:14 +010039#include <linux/syscore_ops.h>
Shane Wang69575d32009-09-01 18:25:07 -070040#include <linux/tboot.h>
Stephen Rothwelladb2fe02009-08-31 15:24:23 +100041#include <linux/dmi.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070042#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090043#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070044#include "pci.h"
45
Fenghua Yu5b6985c2008-10-16 18:02:32 -070046#define ROOT_SIZE VTD_PAGE_SIZE
47#define CONTEXT_SIZE VTD_PAGE_SIZE
48
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070049#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
50#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
David Woodhousee0fc7e02009-09-30 09:12:17 -070051#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070052
53#define IOAPIC_RANGE_START (0xfee00000)
54#define IOAPIC_RANGE_END (0xfeefffff)
55#define IOVA_START_ADDR (0x1000)
56
57#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
58
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070059#define MAX_AGAW_WIDTH 64
60
David Woodhouse2ebe3152009-09-19 07:34:04 -070061#define __DOMAIN_MAX_PFN(gaw) ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
62#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)
63
64/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
65 to match. That way, we can use 'unsigned long' for PFNs with impunity. */
66#define DOMAIN_MAX_PFN(gaw) ((unsigned long) min_t(uint64_t, \
67 __DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
68#define DOMAIN_MAX_ADDR(gaw) (((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070069
Mark McLoughlinf27be032008-11-20 15:49:43 +000070#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070071#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070072#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080073
Andrew Mortondf08cdc2010-09-22 13:05:11 -070074/* page table handling */
75#define LEVEL_STRIDE (9)
76#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
77
78static inline int agaw_to_level(int agaw)
79{
80 return agaw + 2;
81}
82
83static inline int agaw_to_width(int agaw)
84{
85 return 30 + agaw * LEVEL_STRIDE;
86}
87
88static inline int width_to_agaw(int width)
89{
90 return (width - 30) / LEVEL_STRIDE;
91}
92
93static inline unsigned int level_to_offset_bits(int level)
94{
95 return (level - 1) * LEVEL_STRIDE;
96}
97
98static inline int pfn_level_offset(unsigned long pfn, int level)
99{
100 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
101}
102
103static inline unsigned long level_mask(int level)
104{
105 return -1UL << level_to_offset_bits(level);
106}
107
108static inline unsigned long level_size(int level)
109{
110 return 1UL << level_to_offset_bits(level);
111}
112
113static inline unsigned long align_to_level(unsigned long pfn, int level)
114{
115 return (pfn + level_size(level) - 1) & level_mask(level);
116}
David Woodhousefd18de52009-05-10 23:57:41 +0100117
David Woodhousedd4e8312009-06-27 16:21:20 +0100118/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
119 are never going to work. */
120static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
121{
122 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
123}
124
125static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
126{
127 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
128}
129static inline unsigned long page_to_dma_pfn(struct page *pg)
130{
131 return mm_to_dma_pfn(page_to_pfn(pg));
132}
133static inline unsigned long virt_to_dma_pfn(void *p)
134{
135 return page_to_dma_pfn(virt_to_page(p));
136}
137
Weidong Hand9630fe2008-12-08 11:06:32 +0800138/* global iommu list, set NULL for ignored DMAR units */
139static struct intel_iommu **g_iommus;
140
David Woodhousee0fc7e02009-09-30 09:12:17 -0700141static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000142static int rwbf_quirk;
143
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000144/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700145 * set to 1 to panic kernel if can't successfully enable VT-d
146 * (used when kernel is launched w/ TXT)
147 */
148static int force_on = 0;
149
150/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000151 * 0: Present
152 * 1-11: Reserved
153 * 12-63: Context Ptr (12 - (haw-1))
154 * 64-127: Reserved
155 */
156struct root_entry {
157 u64 val;
158 u64 rsvd1;
159};
160#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
161static inline bool root_present(struct root_entry *root)
162{
163 return (root->val & 1);
164}
165static inline void set_root_present(struct root_entry *root)
166{
167 root->val |= 1;
168}
169static inline void set_root_value(struct root_entry *root, unsigned long value)
170{
171 root->val |= value & VTD_PAGE_MASK;
172}
173
174static inline struct context_entry *
175get_context_addr_from_root(struct root_entry *root)
176{
177 return (struct context_entry *)
178 (root_present(root)?phys_to_virt(
179 root->val & VTD_PAGE_MASK) :
180 NULL);
181}
182
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000183/*
184 * low 64 bits:
185 * 0: present
186 * 1: fault processing disable
187 * 2-3: translation type
188 * 12-63: address space root
189 * high 64 bits:
190 * 0-2: address width
191 * 3-6: aval
192 * 8-23: domain id
193 */
194struct context_entry {
195 u64 lo;
196 u64 hi;
197};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000198
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000199static inline bool context_present(struct context_entry *context)
200{
201 return (context->lo & 1);
202}
203static inline void context_set_present(struct context_entry *context)
204{
205 context->lo |= 1;
206}
207
208static inline void context_set_fault_enable(struct context_entry *context)
209{
210 context->lo &= (((u64)-1) << 2) | 1;
211}
212
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000213static inline void context_set_translation_type(struct context_entry *context,
214 unsigned long value)
215{
216 context->lo &= (((u64)-1) << 4) | 3;
217 context->lo |= (value & 3) << 2;
218}
219
220static inline void context_set_address_root(struct context_entry *context,
221 unsigned long value)
222{
223 context->lo |= value & VTD_PAGE_MASK;
224}
225
226static inline void context_set_address_width(struct context_entry *context,
227 unsigned long value)
228{
229 context->hi |= value & 7;
230}
231
232static inline void context_set_domain_id(struct context_entry *context,
233 unsigned long value)
234{
235 context->hi |= (value & ((1 << 16) - 1)) << 8;
236}
237
238static inline void context_clear_entry(struct context_entry *context)
239{
240 context->lo = 0;
241 context->hi = 0;
242}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000243
Mark McLoughlin622ba122008-11-20 15:49:46 +0000244/*
245 * 0: readable
246 * 1: writable
247 * 2-6: reserved
248 * 7: super page
Sheng Yang9cf066972009-03-18 15:33:07 +0800249 * 8-10: available
250 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000251 * 12-63: Host physcial address
252 */
253struct dma_pte {
254 u64 val;
255};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000256
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000257static inline void dma_clear_pte(struct dma_pte *pte)
258{
259 pte->val = 0;
260}
261
262static inline void dma_set_pte_readable(struct dma_pte *pte)
263{
264 pte->val |= DMA_PTE_READ;
265}
266
267static inline void dma_set_pte_writable(struct dma_pte *pte)
268{
269 pte->val |= DMA_PTE_WRITE;
270}
271
Sheng Yang9cf066972009-03-18 15:33:07 +0800272static inline void dma_set_pte_snp(struct dma_pte *pte)
273{
274 pte->val |= DMA_PTE_SNP;
275}
276
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000277static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
278{
279 pte->val = (pte->val & ~3) | (prot & 3);
280}
281
282static inline u64 dma_pte_addr(struct dma_pte *pte)
283{
David Woodhousec85994e2009-07-01 19:21:24 +0100284#ifdef CONFIG_64BIT
285 return pte->val & VTD_PAGE_MASK;
286#else
287 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100288 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100289#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000290}
291
David Woodhousedd4e8312009-06-27 16:21:20 +0100292static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn)
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000293{
David Woodhousedd4e8312009-06-27 16:21:20 +0100294 pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000295}
296
297static inline bool dma_pte_present(struct dma_pte *pte)
298{
299 return (pte->val & 3) != 0;
300}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000301
David Woodhouse75e6bf92009-07-02 11:21:16 +0100302static inline int first_pte_in_page(struct dma_pte *pte)
303{
304 return !((unsigned long)pte & ~VTD_PAGE_MASK);
305}
306
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700307/*
308 * This domain is a statically identity mapping domain.
309 * 1. This domain creats a static 1:1 mapping to all usable memory.
310 * 2. It maps to each iommu if successful.
311 * 3. Each iommu mapps to this domain if successful.
312 */
David Woodhouse19943b02009-08-04 16:19:20 +0100313static struct dmar_domain *si_domain;
314static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700315
Weidong Han3b5410e2008-12-08 09:17:15 +0800316/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100317#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800318
Weidong Han1ce28fe2008-12-08 16:35:39 +0800319/* domain represents a virtual machine, more than one devices
320 * across iommus may be owned in one domain, e.g. kvm guest.
321 */
322#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
323
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700324/* si_domain contains mulitple devices */
325#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2)
326
Mark McLoughlin99126f72008-11-20 15:49:47 +0000327struct dmar_domain {
328 int id; /* domain id */
Suresh Siddha4c923d42009-10-02 11:01:24 -0700329 int nid; /* node id */
Weidong Han8c11e792008-12-08 15:29:22 +0800330 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000331
332 struct list_head devices; /* all devices' list */
333 struct iova_domain iovad; /* iova's that belong to this domain */
334
335 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000336 int gaw; /* max guest address width */
337
338 /* adjusted guest address width, 0 is level 2 30-bit */
339 int agaw;
340
Weidong Han3b5410e2008-12-08 09:17:15 +0800341 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800342
343 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800344 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800345 int iommu_count; /* reference count of iommu */
346 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800347 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000348};
349
Mark McLoughlina647dac2008-11-20 15:49:48 +0000350/* PCI domain-device relationship */
351struct device_domain_info {
352 struct list_head link; /* link to domain siblings */
353 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100354 int segment; /* PCI domain */
355 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000356 u8 devfn; /* PCI devfn number */
Stefan Assmann45e829e2009-12-03 06:49:24 -0500357 struct pci_dev *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800358 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000359 struct dmar_domain *domain; /* pointer to domain */
360};
361
mark gross5e0d2a62008-03-04 15:22:08 -0800362static void flush_unmaps_timeout(unsigned long data);
363
364DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
365
mark gross80b20dd2008-04-18 13:53:58 -0700366#define HIGH_WATER_MARK 250
367struct deferred_flush_tables {
368 int next;
369 struct iova *iova[HIGH_WATER_MARK];
370 struct dmar_domain *domain[HIGH_WATER_MARK];
371};
372
373static struct deferred_flush_tables *deferred_flush;
374
mark gross5e0d2a62008-03-04 15:22:08 -0800375/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800376static int g_num_of_iommus;
377
378static DEFINE_SPINLOCK(async_umap_flush_lock);
379static LIST_HEAD(unmaps_to_do);
380
381static int timer_on;
382static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800383
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700384static void domain_remove_dev_info(struct dmar_domain *domain);
385
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800386#ifdef CONFIG_DMAR_DEFAULT_ON
387int dmar_disabled = 0;
388#else
389int dmar_disabled = 1;
390#endif /*CONFIG_DMAR_DEFAULT_ON*/
391
David Woodhouse2d9e6672010-06-15 10:57:57 +0100392static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700393static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800394static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700395
396#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
397static DEFINE_SPINLOCK(device_domain_lock);
398static LIST_HEAD(device_domain_list);
399
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100400static struct iommu_ops intel_iommu_ops;
401
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700402static int __init intel_iommu_setup(char *str)
403{
404 if (!str)
405 return -EINVAL;
406 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800407 if (!strncmp(str, "on", 2)) {
408 dmar_disabled = 0;
409 printk(KERN_INFO "Intel-IOMMU: enabled\n");
410 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700411 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800412 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700413 } else if (!strncmp(str, "igfx_off", 8)) {
414 dmar_map_gfx = 0;
415 printk(KERN_INFO
416 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700417 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800418 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700419 "Intel-IOMMU: Forcing DAC for PCI devices\n");
420 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800421 } else if (!strncmp(str, "strict", 6)) {
422 printk(KERN_INFO
423 "Intel-IOMMU: disable batched IOTLB flush\n");
424 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700425 }
426
427 str += strcspn(str, ",");
428 while (*str == ',')
429 str++;
430 }
431 return 0;
432}
433__setup("intel_iommu=", intel_iommu_setup);
434
435static struct kmem_cache *iommu_domain_cache;
436static struct kmem_cache *iommu_devinfo_cache;
437static struct kmem_cache *iommu_iova_cache;
438
Suresh Siddha4c923d42009-10-02 11:01:24 -0700439static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700440{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700441 struct page *page;
442 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700443
Suresh Siddha4c923d42009-10-02 11:01:24 -0700444 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
445 if (page)
446 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700447 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700448}
449
450static inline void free_pgtable_page(void *vaddr)
451{
452 free_page((unsigned long)vaddr);
453}
454
455static inline void *alloc_domain_mem(void)
456{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900457 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700458}
459
Kay, Allen M38717942008-09-09 18:37:29 +0300460static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700461{
462 kmem_cache_free(iommu_domain_cache, vaddr);
463}
464
465static inline void * alloc_devinfo_mem(void)
466{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900467 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700468}
469
470static inline void free_devinfo_mem(void *vaddr)
471{
472 kmem_cache_free(iommu_devinfo_cache, vaddr);
473}
474
475struct iova *alloc_iova_mem(void)
476{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900477 return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700478}
479
480void free_iova_mem(struct iova *iova)
481{
482 kmem_cache_free(iommu_iova_cache, iova);
483}
484
Weidong Han1b573682008-12-08 15:34:06 +0800485
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700486static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800487{
488 unsigned long sagaw;
489 int agaw = -1;
490
491 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700492 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800493 agaw >= 0; agaw--) {
494 if (test_bit(agaw, &sagaw))
495 break;
496 }
497
498 return agaw;
499}
500
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700501/*
502 * Calculate max SAGAW for each iommu.
503 */
504int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
505{
506 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
507}
508
509/*
510 * calculate agaw for each iommu.
511 * "SAGAW" may be different across iommus, use a default agaw, and
512 * get a supported less agaw for iommus that don't support the default agaw.
513 */
514int iommu_calculate_agaw(struct intel_iommu *iommu)
515{
516 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
517}
518
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700519/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800520static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
521{
522 int iommu_id;
523
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700524 /* si_domain and vm domain should not get here. */
Weidong Han1ce28fe2008-12-08 16:35:39 +0800525 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700526 BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
Weidong Han1ce28fe2008-12-08 16:35:39 +0800527
Weidong Han8c11e792008-12-08 15:29:22 +0800528 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
529 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
530 return NULL;
531
532 return g_iommus[iommu_id];
533}
534
Weidong Han8e6040972008-12-08 15:49:06 +0800535static void domain_update_iommu_coherency(struct dmar_domain *domain)
536{
537 int i;
538
539 domain->iommu_coherency = 1;
540
Akinobu Mitaa45946a2010-03-11 14:04:08 -0800541 for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) {
Weidong Han8e6040972008-12-08 15:49:06 +0800542 if (!ecap_coherent(g_iommus[i]->ecap)) {
543 domain->iommu_coherency = 0;
544 break;
545 }
Weidong Han8e6040972008-12-08 15:49:06 +0800546 }
547}
548
Sheng Yang58c610b2009-03-18 15:33:05 +0800549static void domain_update_iommu_snooping(struct dmar_domain *domain)
550{
551 int i;
552
553 domain->iommu_snooping = 1;
554
Akinobu Mitaa45946a2010-03-11 14:04:08 -0800555 for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) {
Sheng Yang58c610b2009-03-18 15:33:05 +0800556 if (!ecap_sc_support(g_iommus[i]->ecap)) {
557 domain->iommu_snooping = 0;
558 break;
559 }
Sheng Yang58c610b2009-03-18 15:33:05 +0800560 }
561}
562
563/* Some capabilities may be different across iommus */
564static void domain_update_iommu_cap(struct dmar_domain *domain)
565{
566 domain_update_iommu_coherency(domain);
567 domain_update_iommu_snooping(domain);
568}
569
David Woodhouse276dbf992009-04-04 01:45:37 +0100570static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800571{
572 struct dmar_drhd_unit *drhd = NULL;
573 int i;
574
575 for_each_drhd_unit(drhd) {
576 if (drhd->ignored)
577 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100578 if (segment != drhd->segment)
579 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800580
David Woodhouse924b6232009-04-04 00:39:25 +0100581 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000582 if (drhd->devices[i] &&
583 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800584 drhd->devices[i]->devfn == devfn)
585 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700586 if (drhd->devices[i] &&
587 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100588 drhd->devices[i]->subordinate->number <= bus &&
589 drhd->devices[i]->subordinate->subordinate >= bus)
590 return drhd->iommu;
591 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800592
593 if (drhd->include_all)
594 return drhd->iommu;
595 }
596
597 return NULL;
598}
599
Weidong Han5331fe62008-12-08 23:00:00 +0800600static void domain_flush_cache(struct dmar_domain *domain,
601 void *addr, int size)
602{
603 if (!domain->iommu_coherency)
604 clflush_cache_range(addr, size);
605}
606
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700607/* Gets context entry for a given bus and devfn */
608static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
609 u8 bus, u8 devfn)
610{
611 struct root_entry *root;
612 struct context_entry *context;
613 unsigned long phy_addr;
614 unsigned long flags;
615
616 spin_lock_irqsave(&iommu->lock, flags);
617 root = &iommu->root_entry[bus];
618 context = get_context_addr_from_root(root);
619 if (!context) {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700620 context = (struct context_entry *)
621 alloc_pgtable_page(iommu->node);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700622 if (!context) {
623 spin_unlock_irqrestore(&iommu->lock, flags);
624 return NULL;
625 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700626 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700627 phy_addr = virt_to_phys((void *)context);
628 set_root_value(root, phy_addr);
629 set_root_present(root);
630 __iommu_flush_cache(iommu, root, sizeof(*root));
631 }
632 spin_unlock_irqrestore(&iommu->lock, flags);
633 return &context[devfn];
634}
635
636static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
637{
638 struct root_entry *root;
639 struct context_entry *context;
640 int ret;
641 unsigned long flags;
642
643 spin_lock_irqsave(&iommu->lock, flags);
644 root = &iommu->root_entry[bus];
645 context = get_context_addr_from_root(root);
646 if (!context) {
647 ret = 0;
648 goto out;
649 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000650 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700651out:
652 spin_unlock_irqrestore(&iommu->lock, flags);
653 return ret;
654}
655
656static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
657{
658 struct root_entry *root;
659 struct context_entry *context;
660 unsigned long flags;
661
662 spin_lock_irqsave(&iommu->lock, flags);
663 root = &iommu->root_entry[bus];
664 context = get_context_addr_from_root(root);
665 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000666 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700667 __iommu_flush_cache(iommu, &context[devfn], \
668 sizeof(*context));
669 }
670 spin_unlock_irqrestore(&iommu->lock, flags);
671}
672
673static void free_context_table(struct intel_iommu *iommu)
674{
675 struct root_entry *root;
676 int i;
677 unsigned long flags;
678 struct context_entry *context;
679
680 spin_lock_irqsave(&iommu->lock, flags);
681 if (!iommu->root_entry) {
682 goto out;
683 }
684 for (i = 0; i < ROOT_ENTRY_NR; i++) {
685 root = &iommu->root_entry[i];
686 context = get_context_addr_from_root(root);
687 if (context)
688 free_pgtable_page(context);
689 }
690 free_pgtable_page(iommu->root_entry);
691 iommu->root_entry = NULL;
692out:
693 spin_unlock_irqrestore(&iommu->lock, flags);
694}
695
David Woodhouseb026fd22009-06-28 10:37:25 +0100696static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
697 unsigned long pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700698{
David Woodhouseb026fd22009-06-28 10:37:25 +0100699 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700700 struct dma_pte *parent, *pte = NULL;
701 int level = agaw_to_level(domain->agaw);
702 int offset;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700703
704 BUG_ON(!domain->pgd);
David Woodhouseb026fd22009-06-28 10:37:25 +0100705 BUG_ON(addr_width < BITS_PER_LONG && pfn >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700706 parent = domain->pgd;
707
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700708 while (level > 0) {
709 void *tmp_page;
710
David Woodhouseb026fd22009-06-28 10:37:25 +0100711 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700712 pte = &parent[offset];
713 if (level == 1)
714 break;
715
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000716 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100717 uint64_t pteval;
718
Suresh Siddha4c923d42009-10-02 11:01:24 -0700719 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700720
David Woodhouse206a73c12009-07-01 19:30:28 +0100721 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700722 return NULL;
David Woodhouse206a73c12009-07-01 19:30:28 +0100723
David Woodhousec85994e2009-07-01 19:21:24 +0100724 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400725 pteval = ((uint64_t)virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE;
David Woodhousec85994e2009-07-01 19:21:24 +0100726 if (cmpxchg64(&pte->val, 0ULL, pteval)) {
727 /* Someone else set it while we were thinking; use theirs. */
728 free_pgtable_page(tmp_page);
729 } else {
730 dma_pte_addr(pte);
731 domain_flush_cache(domain, pte, sizeof(*pte));
732 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700733 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000734 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700735 level--;
736 }
737
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700738 return pte;
739}
740
741/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100742static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
743 unsigned long pfn,
744 int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700745{
746 struct dma_pte *parent, *pte = NULL;
747 int total = agaw_to_level(domain->agaw);
748 int offset;
749
750 parent = domain->pgd;
751 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100752 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700753 pte = &parent[offset];
754 if (level == total)
755 return pte;
756
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000757 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700758 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000759 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700760 total--;
761 }
762 return NULL;
763}
764
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700765/* clear last level pte, a tlb flush should be followed */
David Woodhouse595badf2009-06-27 22:09:11 +0100766static void dma_pte_clear_range(struct dmar_domain *domain,
767 unsigned long start_pfn,
768 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700769{
David Woodhouse04b18e62009-06-27 19:15:01 +0100770 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhouse310a5ab2009-06-28 18:52:20 +0100771 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700772
David Woodhouse04b18e62009-06-27 19:15:01 +0100773 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
David Woodhouse595badf2009-06-27 22:09:11 +0100774 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse59c36282009-09-19 07:36:28 -0700775 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +0100776
David Woodhouse04b18e62009-06-27 19:15:01 +0100777 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -0700778 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +0100779 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1);
780 if (!pte) {
781 start_pfn = align_to_level(start_pfn + 1, 2);
782 continue;
783 }
David Woodhouse75e6bf92009-07-02 11:21:16 +0100784 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +0100785 dma_clear_pte(pte);
786 start_pfn++;
787 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +0100788 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
789
David Woodhouse310a5ab2009-06-28 18:52:20 +0100790 domain_flush_cache(domain, first_pte,
791 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -0700792
793 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700794}
795
796/* free page table pages. last level pte should already be cleared */
797static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +0100798 unsigned long start_pfn,
799 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700800{
David Woodhouse6660c632009-06-27 22:41:00 +0100801 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhousef3a0a522009-06-30 03:40:07 +0100802 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700803 int total = agaw_to_level(domain->agaw);
804 int level;
David Woodhouse6660c632009-06-27 22:41:00 +0100805 unsigned long tmp;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700806
David Woodhouse6660c632009-06-27 22:41:00 +0100807 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
808 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse59c36282009-09-19 07:36:28 -0700809 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700810
David Woodhousef3a0a522009-06-30 03:40:07 +0100811 /* We don't need lock here; nobody else touches the iova range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700812 level = 2;
813 while (level <= total) {
David Woodhouse6660c632009-06-27 22:41:00 +0100814 tmp = align_to_level(start_pfn, level);
815
David Woodhousef3a0a522009-06-30 03:40:07 +0100816 /* If we can't even clear one PTE at this level, we're done */
David Woodhouse6660c632009-06-27 22:41:00 +0100817 if (tmp + level_size(level) - 1 > last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700818 return;
819
David Woodhouse59c36282009-09-19 07:36:28 -0700820 do {
David Woodhousef3a0a522009-06-30 03:40:07 +0100821 first_pte = pte = dma_pfn_level_pte(domain, tmp, level);
822 if (!pte) {
823 tmp = align_to_level(tmp + 1, level + 1);
824 continue;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700825 }
David Woodhouse75e6bf92009-07-02 11:21:16 +0100826 do {
David Woodhouse6a43e572009-07-02 12:02:34 +0100827 if (dma_pte_present(pte)) {
828 free_pgtable_page(phys_to_virt(dma_pte_addr(pte)));
829 dma_clear_pte(pte);
830 }
David Woodhousef3a0a522009-06-30 03:40:07 +0100831 pte++;
832 tmp += level_size(level);
David Woodhouse75e6bf92009-07-02 11:21:16 +0100833 } while (!first_pte_in_page(pte) &&
834 tmp + level_size(level) - 1 <= last_pfn);
835
David Woodhousef3a0a522009-06-30 03:40:07 +0100836 domain_flush_cache(domain, first_pte,
837 (void *)pte - (void *)first_pte);
838
David Woodhouse59c36282009-09-19 07:36:28 -0700839 } while (tmp && tmp + level_size(level) - 1 <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700840 level++;
841 }
842 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +0100843 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700844 free_pgtable_page(domain->pgd);
845 domain->pgd = NULL;
846 }
847}
848
849/* iommu handling */
850static int iommu_alloc_root_entry(struct intel_iommu *iommu)
851{
852 struct root_entry *root;
853 unsigned long flags;
854
Suresh Siddha4c923d42009-10-02 11:01:24 -0700855 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700856 if (!root)
857 return -ENOMEM;
858
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700859 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700860
861 spin_lock_irqsave(&iommu->lock, flags);
862 iommu->root_entry = root;
863 spin_unlock_irqrestore(&iommu->lock, flags);
864
865 return 0;
866}
867
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700868static void iommu_set_root_entry(struct intel_iommu *iommu)
869{
870 void *addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100871 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700872 unsigned long flag;
873
874 addr = iommu->root_entry;
875
876 spin_lock_irqsave(&iommu->register_lock, flag);
877 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
878
David Woodhousec416daa2009-05-10 20:30:58 +0100879 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700880
881 /* Make sure hardware complete it */
882 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100883 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700884
885 spin_unlock_irqrestore(&iommu->register_lock, flag);
886}
887
888static void iommu_flush_write_buffer(struct intel_iommu *iommu)
889{
890 u32 val;
891 unsigned long flag;
892
David Woodhouse9af88142009-02-13 23:18:03 +0000893 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700894 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700895
896 spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +0100897 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700898
899 /* Make sure hardware complete it */
900 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100901 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700902
903 spin_unlock_irqrestore(&iommu->register_lock, flag);
904}
905
906/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +0100907static void __iommu_flush_context(struct intel_iommu *iommu,
908 u16 did, u16 source_id, u8 function_mask,
909 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700910{
911 u64 val = 0;
912 unsigned long flag;
913
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700914 switch (type) {
915 case DMA_CCMD_GLOBAL_INVL:
916 val = DMA_CCMD_GLOBAL_INVL;
917 break;
918 case DMA_CCMD_DOMAIN_INVL:
919 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
920 break;
921 case DMA_CCMD_DEVICE_INVL:
922 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
923 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
924 break;
925 default:
926 BUG();
927 }
928 val |= DMA_CCMD_ICC;
929
930 spin_lock_irqsave(&iommu->register_lock, flag);
931 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
932
933 /* Make sure hardware complete it */
934 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
935 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
936
937 spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700938}
939
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700940/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100941static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
942 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700943{
944 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
945 u64 val = 0, val_iva = 0;
946 unsigned long flag;
947
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700948 switch (type) {
949 case DMA_TLB_GLOBAL_FLUSH:
950 /* global flush doesn't need set IVA_REG */
951 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
952 break;
953 case DMA_TLB_DSI_FLUSH:
954 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
955 break;
956 case DMA_TLB_PSI_FLUSH:
957 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
958 /* Note: always flush non-leaf currently */
959 val_iva = size_order | addr;
960 break;
961 default:
962 BUG();
963 }
964 /* Note: set drain read/write */
965#if 0
966 /*
967 * This is probably to be super secure.. Looks like we can
968 * ignore it without any impact.
969 */
970 if (cap_read_drain(iommu->cap))
971 val |= DMA_TLB_READ_DRAIN;
972#endif
973 if (cap_write_drain(iommu->cap))
974 val |= DMA_TLB_WRITE_DRAIN;
975
976 spin_lock_irqsave(&iommu->register_lock, flag);
977 /* Note: Only uses first TLB reg currently */
978 if (val_iva)
979 dmar_writeq(iommu->reg + tlb_offset, val_iva);
980 dmar_writeq(iommu->reg + tlb_offset + 8, val);
981
982 /* Make sure hardware complete it */
983 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
984 dmar_readq, (!(val & DMA_TLB_IVT)), val);
985
986 spin_unlock_irqrestore(&iommu->register_lock, flag);
987
988 /* check IOTLB invalidation granularity */
989 if (DMA_TLB_IAIG(val) == 0)
990 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
991 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
992 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700993 (unsigned long long)DMA_TLB_IIRG(type),
994 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700995}
996
Yu Zhao93a23a72009-05-18 13:51:37 +0800997static struct device_domain_info *iommu_support_dev_iotlb(
998 struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700999{
Yu Zhao93a23a72009-05-18 13:51:37 +08001000 int found = 0;
1001 unsigned long flags;
1002 struct device_domain_info *info;
1003 struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);
1004
1005 if (!ecap_dev_iotlb_support(iommu->ecap))
1006 return NULL;
1007
1008 if (!iommu->qi)
1009 return NULL;
1010
1011 spin_lock_irqsave(&device_domain_lock, flags);
1012 list_for_each_entry(info, &domain->devices, link)
1013 if (info->bus == bus && info->devfn == devfn) {
1014 found = 1;
1015 break;
1016 }
1017 spin_unlock_irqrestore(&device_domain_lock, flags);
1018
1019 if (!found || !info->dev)
1020 return NULL;
1021
1022 if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
1023 return NULL;
1024
1025 if (!dmar_find_matched_atsr_unit(info->dev))
1026 return NULL;
1027
1028 info->iommu = iommu;
1029
1030 return info;
1031}
1032
1033static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1034{
1035 if (!info)
1036 return;
1037
1038 pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
1039}
1040
1041static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1042{
1043 if (!info->dev || !pci_ats_enabled(info->dev))
1044 return;
1045
1046 pci_disable_ats(info->dev);
1047}
1048
1049static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1050 u64 addr, unsigned mask)
1051{
1052 u16 sid, qdep;
1053 unsigned long flags;
1054 struct device_domain_info *info;
1055
1056 spin_lock_irqsave(&device_domain_lock, flags);
1057 list_for_each_entry(info, &domain->devices, link) {
1058 if (!info->dev || !pci_ats_enabled(info->dev))
1059 continue;
1060
1061 sid = info->bus << 8 | info->devfn;
1062 qdep = pci_ats_queue_depth(info->dev);
1063 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1064 }
1065 spin_unlock_irqrestore(&device_domain_lock, flags);
1066}
1067
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001068static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
Nadav Amit82653632010-04-01 13:24:40 +03001069 unsigned long pfn, unsigned int pages, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001070{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001071 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001072 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001073
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001074 BUG_ON(pages == 0);
1075
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001076 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001077 * Fallback to domain selective flush if no PSI support or the size is
1078 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001079 * PSI requires page size to be 2 ^ x, and the base address is naturally
1080 * aligned to the size
1081 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001082 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1083 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001084 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001085 else
1086 iommu->flush.flush_iotlb(iommu, did, addr, mask,
1087 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001088
1089 /*
Nadav Amit82653632010-04-01 13:24:40 +03001090 * In caching mode, changes of pages from non-present to present require
1091 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001092 */
Nadav Amit82653632010-04-01 13:24:40 +03001093 if (!cap_caching_mode(iommu->cap) || !map)
Yu Zhao93a23a72009-05-18 13:51:37 +08001094 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001095}
1096
mark grossf8bab732008-02-08 04:18:38 -08001097static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1098{
1099 u32 pmen;
1100 unsigned long flags;
1101
1102 spin_lock_irqsave(&iommu->register_lock, flags);
1103 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1104 pmen &= ~DMA_PMEN_EPM;
1105 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1106
1107 /* wait for the protected region status bit to clear */
1108 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1109 readl, !(pmen & DMA_PMEN_PRS), pmen);
1110
1111 spin_unlock_irqrestore(&iommu->register_lock, flags);
1112}
1113
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001114static int iommu_enable_translation(struct intel_iommu *iommu)
1115{
1116 u32 sts;
1117 unsigned long flags;
1118
1119 spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001120 iommu->gcmd |= DMA_GCMD_TE;
1121 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001122
1123 /* Make sure hardware complete it */
1124 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001125 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001126
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001127 spin_unlock_irqrestore(&iommu->register_lock, flags);
1128 return 0;
1129}
1130
1131static int iommu_disable_translation(struct intel_iommu *iommu)
1132{
1133 u32 sts;
1134 unsigned long flag;
1135
1136 spin_lock_irqsave(&iommu->register_lock, flag);
1137 iommu->gcmd &= ~DMA_GCMD_TE;
1138 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1139
1140 /* Make sure hardware complete it */
1141 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001142 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001143
1144 spin_unlock_irqrestore(&iommu->register_lock, flag);
1145 return 0;
1146}
1147
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001148
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001149static int iommu_init_domains(struct intel_iommu *iommu)
1150{
1151 unsigned long ndomains;
1152 unsigned long nlongs;
1153
1154 ndomains = cap_ndoms(iommu->cap);
Yinghai Lu680a7522010-04-08 19:58:23 +01001155 pr_debug("IOMMU %d: Number of Domains supportd <%ld>\n", iommu->seq_id,
1156 ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001157 nlongs = BITS_TO_LONGS(ndomains);
1158
Donald Dutile94a91b52009-08-20 16:51:34 -04001159 spin_lock_init(&iommu->lock);
1160
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001161 /* TBD: there might be 64K domains,
1162 * consider other allocation for future chip
1163 */
1164 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1165 if (!iommu->domain_ids) {
1166 printk(KERN_ERR "Allocating domain id array failed\n");
1167 return -ENOMEM;
1168 }
1169 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1170 GFP_KERNEL);
1171 if (!iommu->domains) {
1172 printk(KERN_ERR "Allocating domain array failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001173 return -ENOMEM;
1174 }
1175
1176 /*
1177 * if Caching mode is set, then invalid translations are tagged
1178 * with domainid 0. Hence we need to pre-allocate it.
1179 */
1180 if (cap_caching_mode(iommu->cap))
1181 set_bit(0, iommu->domain_ids);
1182 return 0;
1183}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001184
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001185
1186static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001187static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001188
1189void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001190{
1191 struct dmar_domain *domain;
1192 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001193 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001194
Donald Dutile94a91b52009-08-20 16:51:34 -04001195 if ((iommu->domains) && (iommu->domain_ids)) {
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001196 for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
Donald Dutile94a91b52009-08-20 16:51:34 -04001197 domain = iommu->domains[i];
1198 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001199
Donald Dutile94a91b52009-08-20 16:51:34 -04001200 spin_lock_irqsave(&domain->iommu_lock, flags);
1201 if (--domain->iommu_count == 0) {
1202 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1203 vm_domain_exit(domain);
1204 else
1205 domain_exit(domain);
1206 }
1207 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001208 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001209 }
1210
1211 if (iommu->gcmd & DMA_GCMD_TE)
1212 iommu_disable_translation(iommu);
1213
1214 if (iommu->irq) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001215 irq_set_handler_data(iommu->irq, NULL);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001216 /* This will mask the irq */
1217 free_irq(iommu->irq, iommu);
1218 destroy_irq(iommu->irq);
1219 }
1220
1221 kfree(iommu->domains);
1222 kfree(iommu->domain_ids);
1223
Weidong Hand9630fe2008-12-08 11:06:32 +08001224 g_iommus[iommu->seq_id] = NULL;
1225
1226 /* if all iommus are freed, free g_iommus */
1227 for (i = 0; i < g_num_of_iommus; i++) {
1228 if (g_iommus[i])
1229 break;
1230 }
1231
1232 if (i == g_num_of_iommus)
1233 kfree(g_iommus);
1234
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001235 /* free context mapping */
1236 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001237}
1238
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001239static struct dmar_domain *alloc_domain(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001240{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001241 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001242
1243 domain = alloc_domain_mem();
1244 if (!domain)
1245 return NULL;
1246
Suresh Siddha4c923d42009-10-02 11:01:24 -07001247 domain->nid = -1;
Weidong Han8c11e792008-12-08 15:29:22 +08001248 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
Weidong Hand71a2f32008-12-07 21:13:41 +08001249 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001250
1251 return domain;
1252}
1253
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001254static int iommu_attach_domain(struct dmar_domain *domain,
1255 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001256{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001257 int num;
1258 unsigned long ndomains;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001259 unsigned long flags;
1260
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001261 ndomains = cap_ndoms(iommu->cap);
Weidong Han8c11e792008-12-08 15:29:22 +08001262
1263 spin_lock_irqsave(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001264
1265 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1266 if (num >= ndomains) {
1267 spin_unlock_irqrestore(&iommu->lock, flags);
1268 printk(KERN_ERR "IOMMU: no free domain ids\n");
1269 return -ENOMEM;
1270 }
1271
1272 domain->id = num;
1273 set_bit(num, iommu->domain_ids);
1274 set_bit(iommu->seq_id, &domain->iommu_bmp);
1275 iommu->domains[num] = domain;
1276 spin_unlock_irqrestore(&iommu->lock, flags);
1277
1278 return 0;
1279}
1280
1281static void iommu_detach_domain(struct dmar_domain *domain,
1282 struct intel_iommu *iommu)
1283{
1284 unsigned long flags;
1285 int num, ndomains;
1286 int found = 0;
1287
1288 spin_lock_irqsave(&iommu->lock, flags);
1289 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001290 for_each_set_bit(num, iommu->domain_ids, ndomains) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001291 if (iommu->domains[num] == domain) {
1292 found = 1;
1293 break;
1294 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001295 }
1296
1297 if (found) {
1298 clear_bit(num, iommu->domain_ids);
1299 clear_bit(iommu->seq_id, &domain->iommu_bmp);
1300 iommu->domains[num] = NULL;
1301 }
Weidong Han8c11e792008-12-08 15:29:22 +08001302 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001303}
1304
1305static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001306static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001307
Joseph Cihula51a63e62011-03-21 11:04:24 -07001308static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001309{
1310 struct pci_dev *pdev = NULL;
1311 struct iova *iova;
1312 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001313
David Millerf6611972008-02-06 01:36:23 -08001314 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001315
Mark Gross8a443df2008-03-04 14:59:31 -08001316 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1317 &reserved_rbtree_key);
1318
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001319 /* IOAPIC ranges shouldn't be accessed by DMA */
1320 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1321 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001322 if (!iova) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001323 printk(KERN_ERR "Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001324 return -ENODEV;
1325 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001326
1327 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1328 for_each_pci_dev(pdev) {
1329 struct resource *r;
1330
1331 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1332 r = &pdev->resource[i];
1333 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1334 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001335 iova = reserve_iova(&reserved_iova_list,
1336 IOVA_PFN(r->start),
1337 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001338 if (!iova) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001339 printk(KERN_ERR "Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001340 return -ENODEV;
1341 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001342 }
1343 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001344 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001345}
1346
1347static void domain_reserve_special_ranges(struct dmar_domain *domain)
1348{
1349 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1350}
1351
1352static inline int guestwidth_to_adjustwidth(int gaw)
1353{
1354 int agaw;
1355 int r = (gaw - 12) % 9;
1356
1357 if (r == 0)
1358 agaw = gaw;
1359 else
1360 agaw = gaw + 9 - r;
1361 if (agaw > 64)
1362 agaw = 64;
1363 return agaw;
1364}
1365
1366static int domain_init(struct dmar_domain *domain, int guest_width)
1367{
1368 struct intel_iommu *iommu;
1369 int adjust_width, agaw;
1370 unsigned long sagaw;
1371
David Millerf6611972008-02-06 01:36:23 -08001372 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Weidong Hanc7151a82008-12-08 22:51:37 +08001373 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001374
1375 domain_reserve_special_ranges(domain);
1376
1377 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001378 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001379 if (guest_width > cap_mgaw(iommu->cap))
1380 guest_width = cap_mgaw(iommu->cap);
1381 domain->gaw = guest_width;
1382 adjust_width = guestwidth_to_adjustwidth(guest_width);
1383 agaw = width_to_agaw(adjust_width);
1384 sagaw = cap_sagaw(iommu->cap);
1385 if (!test_bit(agaw, &sagaw)) {
1386 /* hardware doesn't support it, choose a bigger one */
1387 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1388 agaw = find_next_bit(&sagaw, 5, agaw);
1389 if (agaw >= 5)
1390 return -ENODEV;
1391 }
1392 domain->agaw = agaw;
1393 INIT_LIST_HEAD(&domain->devices);
1394
Weidong Han8e6040972008-12-08 15:49:06 +08001395 if (ecap_coherent(iommu->ecap))
1396 domain->iommu_coherency = 1;
1397 else
1398 domain->iommu_coherency = 0;
1399
Sheng Yang58c610b2009-03-18 15:33:05 +08001400 if (ecap_sc_support(iommu->ecap))
1401 domain->iommu_snooping = 1;
1402 else
1403 domain->iommu_snooping = 0;
1404
Weidong Hanc7151a82008-12-08 22:51:37 +08001405 domain->iommu_count = 1;
Suresh Siddha4c923d42009-10-02 11:01:24 -07001406 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001407
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001408 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001409 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001410 if (!domain->pgd)
1411 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001412 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001413 return 0;
1414}
1415
1416static void domain_exit(struct dmar_domain *domain)
1417{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001418 struct dmar_drhd_unit *drhd;
1419 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001420
1421 /* Domain 0 is reserved, so dont process it */
1422 if (!domain)
1423 return;
1424
1425 domain_remove_dev_info(domain);
1426 /* destroy iovas */
1427 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001428
1429 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01001430 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001431
1432 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01001433 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001434
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001435 for_each_active_iommu(iommu, drhd)
1436 if (test_bit(iommu->seq_id, &domain->iommu_bmp))
1437 iommu_detach_domain(domain, iommu);
1438
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001439 free_domain_mem(domain);
1440}
1441
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001442static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1443 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001444{
1445 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001446 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001447 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001448 struct dma_pte *pgd;
1449 unsigned long num;
1450 unsigned long ndomains;
1451 int id;
1452 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001453 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001454
1455 pr_debug("Set context mapping for %02x:%02x.%d\n",
1456 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001457
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001458 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001459 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1460 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001461
David Woodhouse276dbf992009-04-04 01:45:37 +01001462 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001463 if (!iommu)
1464 return -ENODEV;
1465
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001466 context = device_to_context_entry(iommu, bus, devfn);
1467 if (!context)
1468 return -ENOMEM;
1469 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001470 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001471 spin_unlock_irqrestore(&iommu->lock, flags);
1472 return 0;
1473 }
1474
Weidong Hanea6606b2008-12-08 23:08:15 +08001475 id = domain->id;
1476 pgd = domain->pgd;
1477
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001478 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
1479 domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001480 int found = 0;
1481
1482 /* find an available domain id for this device in iommu */
1483 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001484 for_each_set_bit(num, iommu->domain_ids, ndomains) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001485 if (iommu->domains[num] == domain) {
1486 id = num;
1487 found = 1;
1488 break;
1489 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001490 }
1491
1492 if (found == 0) {
1493 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1494 if (num >= ndomains) {
1495 spin_unlock_irqrestore(&iommu->lock, flags);
1496 printk(KERN_ERR "IOMMU: no free domain ids\n");
1497 return -EFAULT;
1498 }
1499
1500 set_bit(num, iommu->domain_ids);
1501 iommu->domains[num] = domain;
1502 id = num;
1503 }
1504
1505 /* Skip top levels of page tables for
1506 * iommu which has less agaw than default.
Chris Wright1672af12009-12-02 12:06:34 -08001507 * Unnecessary for PT mode.
Weidong Hanea6606b2008-12-08 23:08:15 +08001508 */
Chris Wright1672af12009-12-02 12:06:34 -08001509 if (translation != CONTEXT_TT_PASS_THROUGH) {
1510 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1511 pgd = phys_to_virt(dma_pte_addr(pgd));
1512 if (!dma_pte_present(pgd)) {
1513 spin_unlock_irqrestore(&iommu->lock, flags);
1514 return -ENOMEM;
1515 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001516 }
1517 }
1518 }
1519
1520 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001521
Yu Zhao93a23a72009-05-18 13:51:37 +08001522 if (translation != CONTEXT_TT_PASS_THROUGH) {
1523 info = iommu_support_dev_iotlb(domain, segment, bus, devfn);
1524 translation = info ? CONTEXT_TT_DEV_IOTLB :
1525 CONTEXT_TT_MULTI_LEVEL;
1526 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001527 /*
1528 * In pass through mode, AW must be programmed to indicate the largest
1529 * AGAW value supported by hardware. And ASR is ignored by hardware.
1530 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001531 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001532 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001533 else {
1534 context_set_address_root(context, virt_to_phys(pgd));
1535 context_set_address_width(context, iommu->agaw);
1536 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001537
1538 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001539 context_set_fault_enable(context);
1540 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001541 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001542
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001543 /*
1544 * It's a non-present to present mapping. If hardware doesn't cache
1545 * non-present entry we only need to flush the write-buffer. If the
1546 * _does_ cache non-present entries, then it does so in the special
1547 * domain #0, which we have to flush:
1548 */
1549 if (cap_caching_mode(iommu->cap)) {
1550 iommu->flush.flush_context(iommu, 0,
1551 (((u16)bus) << 8) | devfn,
1552 DMA_CCMD_MASK_NOBIT,
1553 DMA_CCMD_DEVICE_INVL);
Nadav Amit82653632010-04-01 13:24:40 +03001554 iommu->flush.flush_iotlb(iommu, domain->id, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001555 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001556 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001557 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001558 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001559 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001560
1561 spin_lock_irqsave(&domain->iommu_lock, flags);
1562 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1563 domain->iommu_count++;
Suresh Siddha4c923d42009-10-02 11:01:24 -07001564 if (domain->iommu_count == 1)
1565 domain->nid = iommu->node;
Sheng Yang58c610b2009-03-18 15:33:05 +08001566 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001567 }
1568 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001569 return 0;
1570}
1571
1572static int
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001573domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1574 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001575{
1576 int ret;
1577 struct pci_dev *tmp, *parent;
1578
David Woodhouse276dbf992009-04-04 01:45:37 +01001579 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001580 pdev->bus->number, pdev->devfn,
1581 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001582 if (ret)
1583 return ret;
1584
1585 /* dependent device mapping */
1586 tmp = pci_find_upstream_pcie_bridge(pdev);
1587 if (!tmp)
1588 return 0;
1589 /* Secondary interface's bus number and devfn 0 */
1590 parent = pdev->bus->self;
1591 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001592 ret = domain_context_mapping_one(domain,
1593 pci_domain_nr(parent->bus),
1594 parent->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001595 parent->devfn, translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001596 if (ret)
1597 return ret;
1598 parent = parent->bus->self;
1599 }
Stefan Assmann45e829e2009-12-03 06:49:24 -05001600 if (pci_is_pcie(tmp)) /* this is a PCIe-to-PCI bridge */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001601 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001602 pci_domain_nr(tmp->subordinate),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001603 tmp->subordinate->number, 0,
1604 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001605 else /* this is a legacy PCI bridge */
1606 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001607 pci_domain_nr(tmp->bus),
1608 tmp->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001609 tmp->devfn,
1610 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001611}
1612
Weidong Han5331fe62008-12-08 23:00:00 +08001613static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001614{
1615 int ret;
1616 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001617 struct intel_iommu *iommu;
1618
David Woodhouse276dbf992009-04-04 01:45:37 +01001619 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1620 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001621 if (!iommu)
1622 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001623
David Woodhouse276dbf992009-04-04 01:45:37 +01001624 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001625 if (!ret)
1626 return ret;
1627 /* dependent device mapping */
1628 tmp = pci_find_upstream_pcie_bridge(pdev);
1629 if (!tmp)
1630 return ret;
1631 /* Secondary interface's bus number and devfn 0 */
1632 parent = pdev->bus->self;
1633 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001634 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001635 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001636 if (!ret)
1637 return ret;
1638 parent = parent->bus->self;
1639 }
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09001640 if (pci_is_pcie(tmp))
David Woodhouse276dbf992009-04-04 01:45:37 +01001641 return device_context_mapped(iommu, tmp->subordinate->number,
1642 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001643 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001644 return device_context_mapped(iommu, tmp->bus->number,
1645 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001646}
1647
Fenghua Yuf5329592009-08-04 15:09:37 -07001648/* Returns a number of VTD pages, but aligned to MM page size */
1649static inline unsigned long aligned_nrpages(unsigned long host_addr,
1650 size_t size)
1651{
1652 host_addr &= ~PAGE_MASK;
1653 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
1654}
1655
David Woodhouse9051aa02009-06-29 12:30:54 +01001656static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1657 struct scatterlist *sg, unsigned long phys_pfn,
1658 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01001659{
1660 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01001661 phys_addr_t uninitialized_var(pteval);
David Woodhousee1605492009-06-29 11:17:38 +01001662 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhouse9051aa02009-06-29 12:30:54 +01001663 unsigned long sg_res;
David Woodhousee1605492009-06-29 11:17:38 +01001664
1665 BUG_ON(addr_width < BITS_PER_LONG && (iov_pfn + nr_pages - 1) >> addr_width);
1666
1667 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1668 return -EINVAL;
1669
1670 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
1671
David Woodhouse9051aa02009-06-29 12:30:54 +01001672 if (sg)
1673 sg_res = 0;
1674 else {
1675 sg_res = nr_pages + 1;
1676 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
1677 }
1678
David Woodhousee1605492009-06-29 11:17:38 +01001679 while (nr_pages--) {
David Woodhousec85994e2009-07-01 19:21:24 +01001680 uint64_t tmp;
1681
David Woodhousee1605492009-06-29 11:17:38 +01001682 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07001683 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01001684 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
1685 sg->dma_length = sg->length;
1686 pteval = page_to_phys(sg_page(sg)) | prot;
1687 }
1688 if (!pte) {
1689 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn);
1690 if (!pte)
1691 return -ENOMEM;
1692 }
1693 /* We don't need lock here, nobody else
1694 * touches the iova range
1695 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01001696 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01001697 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01001698 static int dumps = 5;
David Woodhousec85994e2009-07-01 19:21:24 +01001699 printk(KERN_CRIT "ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
1700 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01001701 if (dumps) {
1702 dumps--;
1703 debug_dma_dump_mappings(NULL);
1704 }
1705 WARN_ON(1);
1706 }
David Woodhousee1605492009-06-29 11:17:38 +01001707 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +01001708 if (!nr_pages || first_pte_in_page(pte)) {
David Woodhousee1605492009-06-29 11:17:38 +01001709 domain_flush_cache(domain, first_pte,
1710 (void *)pte - (void *)first_pte);
1711 pte = NULL;
1712 }
1713 iov_pfn++;
1714 pteval += VTD_PAGE_SIZE;
1715 sg_res--;
1716 if (!sg_res)
1717 sg = sg_next(sg);
1718 }
1719 return 0;
1720}
1721
David Woodhouse9051aa02009-06-29 12:30:54 +01001722static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1723 struct scatterlist *sg, unsigned long nr_pages,
1724 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001725{
David Woodhouse9051aa02009-06-29 12:30:54 +01001726 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
1727}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001728
David Woodhouse9051aa02009-06-29 12:30:54 +01001729static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1730 unsigned long phys_pfn, unsigned long nr_pages,
1731 int prot)
1732{
1733 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001734}
1735
Weidong Hanc7151a82008-12-08 22:51:37 +08001736static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001737{
Weidong Hanc7151a82008-12-08 22:51:37 +08001738 if (!iommu)
1739 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001740
1741 clear_context_table(iommu, bus, devfn);
1742 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001743 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001744 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001745}
1746
1747static void domain_remove_dev_info(struct dmar_domain *domain)
1748{
1749 struct device_domain_info *info;
1750 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001751 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001752
1753 spin_lock_irqsave(&device_domain_lock, flags);
1754 while (!list_empty(&domain->devices)) {
1755 info = list_entry(domain->devices.next,
1756 struct device_domain_info, link);
1757 list_del(&info->link);
1758 list_del(&info->global);
1759 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001760 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001761 spin_unlock_irqrestore(&device_domain_lock, flags);
1762
Yu Zhao93a23a72009-05-18 13:51:37 +08001763 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01001764 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001765 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001766 free_devinfo_mem(info);
1767
1768 spin_lock_irqsave(&device_domain_lock, flags);
1769 }
1770 spin_unlock_irqrestore(&device_domain_lock, flags);
1771}
1772
1773/*
1774 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001775 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001776 */
Kay, Allen M38717942008-09-09 18:37:29 +03001777static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001778find_domain(struct pci_dev *pdev)
1779{
1780 struct device_domain_info *info;
1781
1782 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001783 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001784 if (info)
1785 return info->domain;
1786 return NULL;
1787}
1788
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001789/* domain is initialized */
1790static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1791{
1792 struct dmar_domain *domain, *found = NULL;
1793 struct intel_iommu *iommu;
1794 struct dmar_drhd_unit *drhd;
1795 struct device_domain_info *info, *tmp;
1796 struct pci_dev *dev_tmp;
1797 unsigned long flags;
1798 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001799 int segment;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001800 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001801
1802 domain = find_domain(pdev);
1803 if (domain)
1804 return domain;
1805
David Woodhouse276dbf992009-04-04 01:45:37 +01001806 segment = pci_domain_nr(pdev->bus);
1807
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001808 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1809 if (dev_tmp) {
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09001810 if (pci_is_pcie(dev_tmp)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001811 bus = dev_tmp->subordinate->number;
1812 devfn = 0;
1813 } else {
1814 bus = dev_tmp->bus->number;
1815 devfn = dev_tmp->devfn;
1816 }
1817 spin_lock_irqsave(&device_domain_lock, flags);
1818 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001819 if (info->segment == segment &&
1820 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001821 found = info->domain;
1822 break;
1823 }
1824 }
1825 spin_unlock_irqrestore(&device_domain_lock, flags);
1826 /* pcie-pci bridge already has a domain, uses it */
1827 if (found) {
1828 domain = found;
1829 goto found_domain;
1830 }
1831 }
1832
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001833 domain = alloc_domain();
1834 if (!domain)
1835 goto error;
1836
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001837 /* Allocate new domain for the device */
1838 drhd = dmar_find_matched_drhd_unit(pdev);
1839 if (!drhd) {
1840 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1841 pci_name(pdev));
1842 return NULL;
1843 }
1844 iommu = drhd->iommu;
1845
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001846 ret = iommu_attach_domain(domain, iommu);
1847 if (ret) {
Alex Williamson2fe9723d2011-03-04 14:52:30 -07001848 free_domain_mem(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001849 goto error;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001850 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001851
1852 if (domain_init(domain, gaw)) {
1853 domain_exit(domain);
1854 goto error;
1855 }
1856
1857 /* register pcie-to-pci device */
1858 if (dev_tmp) {
1859 info = alloc_devinfo_mem();
1860 if (!info) {
1861 domain_exit(domain);
1862 goto error;
1863 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001864 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001865 info->bus = bus;
1866 info->devfn = devfn;
1867 info->dev = NULL;
1868 info->domain = domain;
1869 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001870 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001871
1872 /* pcie-to-pci bridge already has a domain, uses it */
1873 found = NULL;
1874 spin_lock_irqsave(&device_domain_lock, flags);
1875 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001876 if (tmp->segment == segment &&
1877 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001878 found = tmp->domain;
1879 break;
1880 }
1881 }
1882 if (found) {
Jiri Slaby00dfff72010-06-14 17:17:32 +02001883 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001884 free_devinfo_mem(info);
1885 domain_exit(domain);
1886 domain = found;
1887 } else {
1888 list_add(&info->link, &domain->devices);
1889 list_add(&info->global, &device_domain_list);
Jiri Slaby00dfff72010-06-14 17:17:32 +02001890 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001891 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001892 }
1893
1894found_domain:
1895 info = alloc_devinfo_mem();
1896 if (!info)
1897 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01001898 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001899 info->bus = pdev->bus->number;
1900 info->devfn = pdev->devfn;
1901 info->dev = pdev;
1902 info->domain = domain;
1903 spin_lock_irqsave(&device_domain_lock, flags);
1904 /* somebody is fast */
1905 found = find_domain(pdev);
1906 if (found != NULL) {
1907 spin_unlock_irqrestore(&device_domain_lock, flags);
1908 if (found != domain) {
1909 domain_exit(domain);
1910 domain = found;
1911 }
1912 free_devinfo_mem(info);
1913 return domain;
1914 }
1915 list_add(&info->link, &domain->devices);
1916 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001917 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001918 spin_unlock_irqrestore(&device_domain_lock, flags);
1919 return domain;
1920error:
1921 /* recheck it here, maybe others set it */
1922 return find_domain(pdev);
1923}
1924
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001925static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07001926#define IDENTMAP_ALL 1
1927#define IDENTMAP_GFX 2
1928#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001929
David Woodhouseb2132032009-06-26 18:50:28 +01001930static int iommu_domain_identity_map(struct dmar_domain *domain,
1931 unsigned long long start,
1932 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001933{
David Woodhousec5395d52009-06-28 16:35:56 +01001934 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
1935 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001936
David Woodhousec5395d52009-06-28 16:35:56 +01001937 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
1938 dma_to_mm_pfn(last_vpfn))) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001939 printk(KERN_ERR "IOMMU: reserve iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01001940 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001941 }
1942
David Woodhousec5395d52009-06-28 16:35:56 +01001943 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
1944 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001945 /*
1946 * RMRR range might have overlap with physical memory range,
1947 * clear it first
1948 */
David Woodhousec5395d52009-06-28 16:35:56 +01001949 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001950
David Woodhousec5395d52009-06-28 16:35:56 +01001951 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
1952 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01001953 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01001954}
1955
1956static int iommu_prepare_identity_map(struct pci_dev *pdev,
1957 unsigned long long start,
1958 unsigned long long end)
1959{
1960 struct dmar_domain *domain;
1961 int ret;
1962
David Woodhousec7ab48d2009-06-26 19:10:36 +01001963 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01001964 if (!domain)
1965 return -ENOMEM;
1966
David Woodhouse19943b02009-08-04 16:19:20 +01001967 /* For _hardware_ passthrough, don't bother. But for software
1968 passthrough, we do it anyway -- it may indicate a memory
1969 range which is reserved in E820, so which didn't get set
1970 up to start with in si_domain */
1971 if (domain == si_domain && hw_pass_through) {
1972 printk("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
1973 pci_name(pdev), start, end);
1974 return 0;
1975 }
1976
1977 printk(KERN_INFO
1978 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1979 pci_name(pdev), start, end);
David Woodhouse2ff729f2009-08-26 14:25:41 +01001980
David Woodhouse5595b522009-12-02 09:21:55 +00001981 if (end < start) {
1982 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
1983 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
1984 dmi_get_system_info(DMI_BIOS_VENDOR),
1985 dmi_get_system_info(DMI_BIOS_VERSION),
1986 dmi_get_system_info(DMI_PRODUCT_VERSION));
1987 ret = -EIO;
1988 goto error;
1989 }
1990
David Woodhouse2ff729f2009-08-26 14:25:41 +01001991 if (end >> agaw_to_width(domain->agaw)) {
1992 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
1993 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
1994 agaw_to_width(domain->agaw),
1995 dmi_get_system_info(DMI_BIOS_VENDOR),
1996 dmi_get_system_info(DMI_BIOS_VERSION),
1997 dmi_get_system_info(DMI_PRODUCT_VERSION));
1998 ret = -EIO;
1999 goto error;
2000 }
David Woodhouse19943b02009-08-04 16:19:20 +01002001
David Woodhouseb2132032009-06-26 18:50:28 +01002002 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002003 if (ret)
2004 goto error;
2005
2006 /* context entry init */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002007 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01002008 if (ret)
2009 goto error;
2010
2011 return 0;
2012
2013 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002014 domain_exit(domain);
2015 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002016}
2017
2018static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
2019 struct pci_dev *pdev)
2020{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002021 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002022 return 0;
2023 return iommu_prepare_identity_map(pdev, rmrr->base_address,
2024 rmrr->end_address + 1);
2025}
2026
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002027#ifdef CONFIG_DMAR_FLOPPY_WA
2028static inline void iommu_prepare_isa(void)
2029{
2030 struct pci_dev *pdev;
2031 int ret;
2032
2033 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2034 if (!pdev)
2035 return;
2036
David Woodhousec7ab48d2009-06-26 19:10:36 +01002037 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002038 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
2039
2040 if (ret)
David Woodhousec7ab48d2009-06-26 19:10:36 +01002041 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
2042 "floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002043
2044}
2045#else
2046static inline void iommu_prepare_isa(void)
2047{
2048 return;
2049}
2050#endif /* !CONFIG_DMAR_FLPY_WA */
2051
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002052static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002053
2054static int __init si_domain_work_fn(unsigned long start_pfn,
2055 unsigned long end_pfn, void *datax)
2056{
2057 int *ret = datax;
2058
2059 *ret = iommu_domain_identity_map(si_domain,
2060 (uint64_t)start_pfn << PAGE_SHIFT,
2061 (uint64_t)end_pfn << PAGE_SHIFT);
2062 return *ret;
2063
2064}
2065
Matt Kraai071e1372009-08-23 22:30:22 -07002066static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002067{
2068 struct dmar_drhd_unit *drhd;
2069 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01002070 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002071
2072 si_domain = alloc_domain();
2073 if (!si_domain)
2074 return -EFAULT;
2075
David Woodhousec7ab48d2009-06-26 19:10:36 +01002076 pr_debug("Identity mapping domain is domain %d\n", si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002077
2078 for_each_active_iommu(iommu, drhd) {
2079 ret = iommu_attach_domain(si_domain, iommu);
2080 if (ret) {
2081 domain_exit(si_domain);
2082 return -EFAULT;
2083 }
2084 }
2085
2086 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2087 domain_exit(si_domain);
2088 return -EFAULT;
2089 }
2090
2091 si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY;
2092
David Woodhouse19943b02009-08-04 16:19:20 +01002093 if (hw)
2094 return 0;
2095
David Woodhousec7ab48d2009-06-26 19:10:36 +01002096 for_each_online_node(nid) {
2097 work_with_active_regions(nid, si_domain_work_fn, &ret);
2098 if (ret)
2099 return ret;
2100 }
2101
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002102 return 0;
2103}
2104
2105static void domain_remove_one_dev_info(struct dmar_domain *domain,
2106 struct pci_dev *pdev);
2107static int identity_mapping(struct pci_dev *pdev)
2108{
2109 struct device_domain_info *info;
2110
2111 if (likely(!iommu_identity_mapping))
2112 return 0;
2113
2114
2115 list_for_each_entry(info, &si_domain->devices, link)
2116 if (info->dev == pdev)
2117 return 1;
2118 return 0;
2119}
2120
2121static int domain_add_dev_info(struct dmar_domain *domain,
David Woodhouse5fe60f42009-08-09 10:53:41 +01002122 struct pci_dev *pdev,
2123 int translation)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002124{
2125 struct device_domain_info *info;
2126 unsigned long flags;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002127 int ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002128
2129 info = alloc_devinfo_mem();
2130 if (!info)
2131 return -ENOMEM;
2132
David Woodhouse5fe60f42009-08-09 10:53:41 +01002133 ret = domain_context_mapping(domain, pdev, translation);
2134 if (ret) {
2135 free_devinfo_mem(info);
2136 return ret;
2137 }
2138
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002139 info->segment = pci_domain_nr(pdev->bus);
2140 info->bus = pdev->bus->number;
2141 info->devfn = pdev->devfn;
2142 info->dev = pdev;
2143 info->domain = domain;
2144
2145 spin_lock_irqsave(&device_domain_lock, flags);
2146 list_add(&info->link, &domain->devices);
2147 list_add(&info->global, &device_domain_list);
2148 pdev->dev.archdata.iommu = info;
2149 spin_unlock_irqrestore(&device_domain_lock, flags);
2150
2151 return 0;
2152}
2153
David Woodhouse6941af22009-07-04 18:24:27 +01002154static int iommu_should_identity_map(struct pci_dev *pdev, int startup)
2155{
David Woodhousee0fc7e02009-09-30 09:12:17 -07002156 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2157 return 1;
2158
2159 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2160 return 1;
2161
2162 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2163 return 0;
David Woodhouse6941af22009-07-04 18:24:27 +01002164
David Woodhouse3dfc8132009-07-04 19:11:08 +01002165 /*
2166 * We want to start off with all devices in the 1:1 domain, and
2167 * take them out later if we find they can't access all of memory.
2168 *
2169 * However, we can't do this for PCI devices behind bridges,
2170 * because all PCI devices behind the same bridge will end up
2171 * with the same source-id on their transactions.
2172 *
2173 * Practically speaking, we can't change things around for these
2174 * devices at run-time, because we can't be sure there'll be no
2175 * DMA transactions in flight for any of their siblings.
2176 *
2177 * So PCI devices (unless they're on the root bus) as well as
2178 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2179 * the 1:1 domain, just in _case_ one of their siblings turns out
2180 * not to be able to map all of memory.
2181 */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09002182 if (!pci_is_pcie(pdev)) {
David Woodhouse3dfc8132009-07-04 19:11:08 +01002183 if (!pci_is_root_bus(pdev->bus))
2184 return 0;
2185 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2186 return 0;
2187 } else if (pdev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
2188 return 0;
2189
2190 /*
2191 * At boot time, we don't yet know if devices will be 64-bit capable.
2192 * Assume that they will -- if they turn out not to be, then we can
2193 * take them out of the 1:1 domain later.
2194 */
David Woodhouse6941af22009-07-04 18:24:27 +01002195 if (!startup)
2196 return pdev->dma_mask > DMA_BIT_MASK(32);
2197
2198 return 1;
2199}
2200
Matt Kraai071e1372009-08-23 22:30:22 -07002201static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002202{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002203 struct pci_dev *pdev = NULL;
2204 int ret;
2205
David Woodhouse19943b02009-08-04 16:19:20 +01002206 ret = si_domain_init(hw);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002207 if (ret)
2208 return -EFAULT;
2209
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002210 for_each_pci_dev(pdev) {
David Woodhouse6941af22009-07-04 18:24:27 +01002211 if (iommu_should_identity_map(pdev, 1)) {
David Woodhouse19943b02009-08-04 16:19:20 +01002212 printk(KERN_INFO "IOMMU: %s identity mapping for device %s\n",
2213 hw ? "hardware" : "software", pci_name(pdev));
David Woodhousec7ab48d2009-06-26 19:10:36 +01002214
David Woodhouse5fe60f42009-08-09 10:53:41 +01002215 ret = domain_add_dev_info(si_domain, pdev,
David Woodhouse19943b02009-08-04 16:19:20 +01002216 hw ? CONTEXT_TT_PASS_THROUGH :
David Woodhouse62edf5d2009-07-04 10:59:46 +01002217 CONTEXT_TT_MULTI_LEVEL);
2218 if (ret)
2219 return ret;
David Woodhouse62edf5d2009-07-04 10:59:46 +01002220 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002221 }
2222
2223 return 0;
2224}
2225
Joseph Cihulab7792602011-05-03 00:08:37 -07002226static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002227{
2228 struct dmar_drhd_unit *drhd;
2229 struct dmar_rmrr_unit *rmrr;
2230 struct pci_dev *pdev;
2231 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002232 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002233
2234 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002235 * for each drhd
2236 * allocate root
2237 * initialize and program root entry to not present
2238 * endfor
2239 */
2240 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002241 g_num_of_iommus++;
2242 /*
2243 * lock not needed as this is only incremented in the single
2244 * threaded kernel __init code path all other access are read
2245 * only
2246 */
2247 }
2248
Weidong Hand9630fe2008-12-08 11:06:32 +08002249 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2250 GFP_KERNEL);
2251 if (!g_iommus) {
2252 printk(KERN_ERR "Allocating global iommu array failed\n");
2253 ret = -ENOMEM;
2254 goto error;
2255 }
2256
mark gross80b20dd2008-04-18 13:53:58 -07002257 deferred_flush = kzalloc(g_num_of_iommus *
2258 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2259 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08002260 ret = -ENOMEM;
2261 goto error;
2262 }
2263
mark gross5e0d2a62008-03-04 15:22:08 -08002264 for_each_drhd_unit(drhd) {
2265 if (drhd->ignored)
2266 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002267
2268 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08002269 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002270
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002271 ret = iommu_init_domains(iommu);
2272 if (ret)
2273 goto error;
2274
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002275 /*
2276 * TBD:
2277 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002278 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002279 */
2280 ret = iommu_alloc_root_entry(iommu);
2281 if (ret) {
2282 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
2283 goto error;
2284 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002285 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01002286 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002287 }
2288
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002289 /*
2290 * Start from the sane iommu hardware state.
2291 */
Youquan Songa77b67d2008-10-16 16:31:56 -07002292 for_each_drhd_unit(drhd) {
2293 if (drhd->ignored)
2294 continue;
2295
2296 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002297
2298 /*
2299 * If the queued invalidation is already initialized by us
2300 * (for example, while enabling interrupt-remapping) then
2301 * we got the things already rolling from a sane state.
2302 */
2303 if (iommu->qi)
2304 continue;
2305
2306 /*
2307 * Clear any previous faults.
2308 */
2309 dmar_fault(-1, iommu);
2310 /*
2311 * Disable queued invalidation if supported and already enabled
2312 * before OS handover.
2313 */
2314 dmar_disable_qi(iommu);
2315 }
2316
2317 for_each_drhd_unit(drhd) {
2318 if (drhd->ignored)
2319 continue;
2320
2321 iommu = drhd->iommu;
2322
Youquan Songa77b67d2008-10-16 16:31:56 -07002323 if (dmar_enable_qi(iommu)) {
2324 /*
2325 * Queued Invalidate not enabled, use Register Based
2326 * Invalidate
2327 */
2328 iommu->flush.flush_context = __iommu_flush_context;
2329 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Yinghai Lu680a7522010-04-08 19:58:23 +01002330 printk(KERN_INFO "IOMMU %d 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002331 "invalidation\n",
Yinghai Lu680a7522010-04-08 19:58:23 +01002332 iommu->seq_id,
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002333 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002334 } else {
2335 iommu->flush.flush_context = qi_flush_context;
2336 iommu->flush.flush_iotlb = qi_flush_iotlb;
Yinghai Lu680a7522010-04-08 19:58:23 +01002337 printk(KERN_INFO "IOMMU %d 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002338 "invalidation\n",
Yinghai Lu680a7522010-04-08 19:58:23 +01002339 iommu->seq_id,
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002340 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002341 }
2342 }
2343
David Woodhouse19943b02009-08-04 16:19:20 +01002344 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07002345 iommu_identity_mapping |= IDENTMAP_ALL;
2346
David Woodhouse19943b02009-08-04 16:19:20 +01002347#ifdef CONFIG_DMAR_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07002348 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01002349#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07002350
2351 check_tylersburg_isoch();
2352
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002353 /*
2354 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002355 * identity mappings for rmrr, gfx, and isa and may fall back to static
2356 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002357 */
David Woodhouse19943b02009-08-04 16:19:20 +01002358 if (iommu_identity_mapping) {
2359 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
2360 if (ret) {
2361 printk(KERN_CRIT "Failed to setup IOMMU pass-through\n");
2362 goto error;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002363 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002364 }
David Woodhouse19943b02009-08-04 16:19:20 +01002365 /*
2366 * For each rmrr
2367 * for each dev attached to rmrr
2368 * do
2369 * locate drhd for dev, alloc domain for dev
2370 * allocate free domain
2371 * allocate page table entries for rmrr
2372 * if context not allocated for bus
2373 * allocate and init context
2374 * set present in root table for this bus
2375 * init context with domain, translation etc
2376 * endfor
2377 * endfor
2378 */
2379 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
2380 for_each_rmrr_units(rmrr) {
2381 for (i = 0; i < rmrr->devices_cnt; i++) {
2382 pdev = rmrr->devices[i];
2383 /*
2384 * some BIOS lists non-exist devices in DMAR
2385 * table.
2386 */
2387 if (!pdev)
2388 continue;
2389 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2390 if (ret)
2391 printk(KERN_ERR
2392 "IOMMU: mapping reserved region failed\n");
2393 }
2394 }
2395
2396 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002397
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002398 /*
2399 * for each drhd
2400 * enable fault log
2401 * global invalidate context cache
2402 * global invalidate iotlb
2403 * enable translation
2404 */
2405 for_each_drhd_unit(drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07002406 if (drhd->ignored) {
2407 /*
2408 * we always have to disable PMRs or DMA may fail on
2409 * this device
2410 */
2411 if (force_on)
2412 iommu_disable_protect_mem_regions(drhd->iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002413 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07002414 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002415 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002416
2417 iommu_flush_write_buffer(iommu);
2418
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002419 ret = dmar_set_interrupt(iommu);
2420 if (ret)
2421 goto error;
2422
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002423 iommu_set_root_entry(iommu);
2424
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002425 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002426 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002427
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002428 ret = iommu_enable_translation(iommu);
2429 if (ret)
2430 goto error;
David Woodhouseb94996c2009-09-19 15:28:12 -07002431
2432 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002433 }
2434
2435 return 0;
2436error:
2437 for_each_drhd_unit(drhd) {
2438 if (drhd->ignored)
2439 continue;
2440 iommu = drhd->iommu;
2441 free_iommu(iommu);
2442 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002443 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002444 return ret;
2445}
2446
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002447/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01002448static struct iova *intel_alloc_iova(struct device *dev,
2449 struct dmar_domain *domain,
2450 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002451{
2452 struct pci_dev *pdev = to_pci_dev(dev);
2453 struct iova *iova = NULL;
2454
David Woodhouse875764d2009-06-28 21:20:51 +01002455 /* Restrict dma_mask to the width that the iommu can handle */
2456 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
2457
2458 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002459 /*
2460 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002461 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002462 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002463 */
David Woodhouse875764d2009-06-28 21:20:51 +01002464 iova = alloc_iova(&domain->iovad, nrpages,
2465 IOVA_PFN(DMA_BIT_MASK(32)), 1);
2466 if (iova)
2467 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002468 }
David Woodhouse875764d2009-06-28 21:20:51 +01002469 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
2470 if (unlikely(!iova)) {
2471 printk(KERN_ERR "Allocating %ld-page iova for %s failed",
2472 nrpages, pci_name(pdev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002473 return NULL;
2474 }
2475
2476 return iova;
2477}
2478
David Woodhouse147202a2009-07-07 19:43:20 +01002479static struct dmar_domain *__get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002480{
2481 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002482 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002483
2484 domain = get_domain_for_dev(pdev,
2485 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2486 if (!domain) {
2487 printk(KERN_ERR
2488 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002489 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002490 }
2491
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002492 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002493 if (unlikely(!domain_context_mapped(pdev))) {
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002494 ret = domain_context_mapping(domain, pdev,
2495 CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002496 if (ret) {
2497 printk(KERN_ERR
2498 "Domain context map for %s failed",
2499 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002500 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002501 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002502 }
2503
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002504 return domain;
2505}
2506
David Woodhouse147202a2009-07-07 19:43:20 +01002507static inline struct dmar_domain *get_valid_domain_for_dev(struct pci_dev *dev)
2508{
2509 struct device_domain_info *info;
2510
2511 /* No lock here, assumes no domain exit in normal case */
2512 info = dev->dev.archdata.iommu;
2513 if (likely(info))
2514 return info->domain;
2515
2516 return __get_valid_domain_for_dev(dev);
2517}
2518
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002519static int iommu_dummy(struct pci_dev *pdev)
2520{
2521 return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
2522}
2523
2524/* Check if the pdev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01002525static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002526{
David Woodhouse73676832009-07-04 14:08:36 +01002527 struct pci_dev *pdev;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002528 int found;
2529
David Woodhouse73676832009-07-04 14:08:36 +01002530 if (unlikely(dev->bus != &pci_bus_type))
2531 return 1;
2532
2533 pdev = to_pci_dev(dev);
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002534 if (iommu_dummy(pdev))
2535 return 1;
2536
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002537 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002538 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002539
2540 found = identity_mapping(pdev);
2541 if (found) {
David Woodhouse6941af22009-07-04 18:24:27 +01002542 if (iommu_should_identity_map(pdev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002543 return 1;
2544 else {
2545 /*
2546 * 32 bit DMA is removed from si_domain and fall back
2547 * to non-identity mapping.
2548 */
2549 domain_remove_one_dev_info(si_domain, pdev);
2550 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
2551 pci_name(pdev));
2552 return 0;
2553 }
2554 } else {
2555 /*
2556 * In case of a detached 64 bit DMA device from vm, the device
2557 * is put into si_domain for identity mapping.
2558 */
David Woodhouse6941af22009-07-04 18:24:27 +01002559 if (iommu_should_identity_map(pdev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002560 int ret;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002561 ret = domain_add_dev_info(si_domain, pdev,
2562 hw_pass_through ?
2563 CONTEXT_TT_PASS_THROUGH :
2564 CONTEXT_TT_MULTI_LEVEL);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002565 if (!ret) {
2566 printk(KERN_INFO "64bit %s uses identity mapping\n",
2567 pci_name(pdev));
2568 return 1;
2569 }
2570 }
2571 }
2572
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002573 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002574}
2575
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002576static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2577 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002578{
2579 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002580 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002581 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002582 struct iova *iova;
2583 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002584 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002585 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07002586 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002587
2588 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002589
David Woodhouse73676832009-07-04 14:08:36 +01002590 if (iommu_no_mapping(hwdev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002591 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002592
2593 domain = get_valid_domain_for_dev(pdev);
2594 if (!domain)
2595 return 0;
2596
Weidong Han8c11e792008-12-08 15:29:22 +08002597 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01002598 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002599
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002600 iova = intel_alloc_iova(hwdev, domain, dma_to_mm_pfn(size),
2601 pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002602 if (!iova)
2603 goto error;
2604
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002605 /*
2606 * Check if DMAR supports zero-length reads on write only
2607 * mappings..
2608 */
2609 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002610 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002611 prot |= DMA_PTE_READ;
2612 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2613 prot |= DMA_PTE_WRITE;
2614 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002615 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002616 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002617 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002618 * is not a big problem
2619 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01002620 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07002621 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002622 if (ret)
2623 goto error;
2624
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002625 /* it's a non-present to present mapping. Only flush if caching mode */
2626 if (cap_caching_mode(iommu->cap))
Nadav Amit82653632010-04-01 13:24:40 +03002627 iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002628 else
Weidong Han8c11e792008-12-08 15:29:22 +08002629 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002630
David Woodhouse03d6a242009-06-28 15:33:46 +01002631 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
2632 start_paddr += paddr & ~PAGE_MASK;
2633 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002634
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002635error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002636 if (iova)
2637 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002638 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002639 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002640 return 0;
2641}
2642
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002643static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2644 unsigned long offset, size_t size,
2645 enum dma_data_direction dir,
2646 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002647{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002648 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2649 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002650}
2651
mark gross5e0d2a62008-03-04 15:22:08 -08002652static void flush_unmaps(void)
2653{
mark gross80b20dd2008-04-18 13:53:58 -07002654 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002655
mark gross5e0d2a62008-03-04 15:22:08 -08002656 timer_on = 0;
2657
2658 /* just flush them all */
2659 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002660 struct intel_iommu *iommu = g_iommus[i];
2661 if (!iommu)
2662 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002663
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002664 if (!deferred_flush[i].next)
2665 continue;
2666
Nadav Amit78d5f0f2010-04-08 23:00:41 +03002667 /* In caching mode, global flushes turn emulation expensive */
2668 if (!cap_caching_mode(iommu->cap))
2669 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08002670 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002671 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08002672 unsigned long mask;
2673 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03002674 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08002675
Nadav Amit78d5f0f2010-04-08 23:00:41 +03002676 /* On real hardware multiple invalidations are expensive */
2677 if (cap_caching_mode(iommu->cap))
2678 iommu_flush_iotlb_psi(iommu, domain->id,
2679 iova->pfn_lo, iova->pfn_hi - iova->pfn_lo + 1, 0);
2680 else {
2681 mask = ilog2(mm_to_dma_pfn(iova->pfn_hi - iova->pfn_lo + 1));
2682 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
2683 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
2684 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002685 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
mark gross80b20dd2008-04-18 13:53:58 -07002686 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002687 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002688 }
2689
mark gross5e0d2a62008-03-04 15:22:08 -08002690 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002691}
2692
2693static void flush_unmaps_timeout(unsigned long data)
2694{
mark gross80b20dd2008-04-18 13:53:58 -07002695 unsigned long flags;
2696
2697 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002698 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002699 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002700}
2701
2702static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2703{
2704 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002705 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002706 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002707
2708 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002709 if (list_size == HIGH_WATER_MARK)
2710 flush_unmaps();
2711
Weidong Han8c11e792008-12-08 15:29:22 +08002712 iommu = domain_get_iommu(dom);
2713 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002714
mark gross80b20dd2008-04-18 13:53:58 -07002715 next = deferred_flush[iommu_id].next;
2716 deferred_flush[iommu_id].domain[next] = dom;
2717 deferred_flush[iommu_id].iova[next] = iova;
2718 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002719
2720 if (!timer_on) {
2721 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2722 timer_on = 1;
2723 }
2724 list_size++;
2725 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2726}
2727
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002728static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2729 size_t size, enum dma_data_direction dir,
2730 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002731{
2732 struct pci_dev *pdev = to_pci_dev(dev);
2733 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002734 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002735 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002736 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002737
David Woodhouse73676832009-07-04 14:08:36 +01002738 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002739 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002740
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002741 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002742 BUG_ON(!domain);
2743
Weidong Han8c11e792008-12-08 15:29:22 +08002744 iommu = domain_get_iommu(domain);
2745
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002746 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01002747 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
2748 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002749 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002750
David Woodhoused794dc92009-06-28 00:27:49 +01002751 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2752 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002753
David Woodhoused794dc92009-06-28 00:27:49 +01002754 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
2755 pci_name(pdev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002756
2757 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002758 dma_pte_clear_range(domain, start_pfn, last_pfn);
2759
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002760 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01002761 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2762
mark gross5e0d2a62008-03-04 15:22:08 -08002763 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01002764 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
Nadav Amit82653632010-04-01 13:24:40 +03002765 last_pfn - start_pfn + 1, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08002766 /* free iova */
2767 __free_iova(&domain->iovad, iova);
2768 } else {
2769 add_unmap(domain, iova);
2770 /*
2771 * queue up the release of the unmap to save the 1/6th of the
2772 * cpu used up by the iotlb flush operation...
2773 */
mark gross5e0d2a62008-03-04 15:22:08 -08002774 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002775}
2776
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002777static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2778 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002779{
2780 void *vaddr;
2781 int order;
2782
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002783 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002784 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07002785
2786 if (!iommu_no_mapping(hwdev))
2787 flags &= ~(GFP_DMA | GFP_DMA32);
2788 else if (hwdev->coherent_dma_mask < dma_get_required_mask(hwdev)) {
2789 if (hwdev->coherent_dma_mask < DMA_BIT_MASK(32))
2790 flags |= GFP_DMA;
2791 else
2792 flags |= GFP_DMA32;
2793 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002794
2795 vaddr = (void *)__get_free_pages(flags, order);
2796 if (!vaddr)
2797 return NULL;
2798 memset(vaddr, 0, size);
2799
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002800 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2801 DMA_BIDIRECTIONAL,
2802 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002803 if (*dma_handle)
2804 return vaddr;
2805 free_pages((unsigned long)vaddr, order);
2806 return NULL;
2807}
2808
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002809static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2810 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002811{
2812 int order;
2813
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002814 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002815 order = get_order(size);
2816
David Woodhouse0db9b7a2009-07-14 02:01:57 +01002817 intel_unmap_page(hwdev, dma_handle, size, DMA_BIDIRECTIONAL, NULL);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002818 free_pages((unsigned long)vaddr, order);
2819}
2820
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002821static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2822 int nelems, enum dma_data_direction dir,
2823 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002824{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002825 struct pci_dev *pdev = to_pci_dev(hwdev);
2826 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002827 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002828 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002829 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002830
David Woodhouse73676832009-07-04 14:08:36 +01002831 if (iommu_no_mapping(hwdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002832 return;
2833
2834 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002835 BUG_ON(!domain);
2836
2837 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002838
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002839 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
David Woodhouse85b98272009-07-01 19:27:53 +01002840 if (WARN_ONCE(!iova, "Driver unmaps unmatched sglist at PFN %llx\n",
2841 (unsigned long long)sglist[0].dma_address))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002842 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002843
David Woodhoused794dc92009-06-28 00:27:49 +01002844 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2845 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002846
2847 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002848 dma_pte_clear_range(domain, start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002849
David Woodhoused794dc92009-06-28 00:27:49 +01002850 /* free page tables */
2851 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2852
David Woodhouseacea0012009-07-14 01:55:11 +01002853 if (intel_iommu_strict) {
2854 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
Nadav Amit82653632010-04-01 13:24:40 +03002855 last_pfn - start_pfn + 1, 0);
David Woodhouseacea0012009-07-14 01:55:11 +01002856 /* free iova */
2857 __free_iova(&domain->iovad, iova);
2858 } else {
2859 add_unmap(domain, iova);
2860 /*
2861 * queue up the release of the unmap to save the 1/6th of the
2862 * cpu used up by the iotlb flush operation...
2863 */
2864 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002865}
2866
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002867static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002868 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002869{
2870 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002871 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002872
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002873 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002874 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002875 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002876 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002877 }
2878 return nelems;
2879}
2880
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002881static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2882 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002883{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002884 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002885 struct pci_dev *pdev = to_pci_dev(hwdev);
2886 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002887 size_t size = 0;
2888 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002889 struct iova *iova = NULL;
2890 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002891 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01002892 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08002893 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002894
2895 BUG_ON(dir == DMA_NONE);
David Woodhouse73676832009-07-04 14:08:36 +01002896 if (iommu_no_mapping(hwdev))
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002897 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002898
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002899 domain = get_valid_domain_for_dev(pdev);
2900 if (!domain)
2901 return 0;
2902
Weidong Han8c11e792008-12-08 15:29:22 +08002903 iommu = domain_get_iommu(domain);
2904
David Woodhouseb536d242009-06-28 14:49:31 +01002905 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01002906 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002907
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002908 iova = intel_alloc_iova(hwdev, domain, dma_to_mm_pfn(size),
2909 pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002910 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002911 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002912 return 0;
2913 }
2914
2915 /*
2916 * Check if DMAR supports zero-length reads on write only
2917 * mappings..
2918 */
2919 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002920 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002921 prot |= DMA_PTE_READ;
2922 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2923 prot |= DMA_PTE_WRITE;
2924
David Woodhouseb536d242009-06-28 14:49:31 +01002925 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01002926
Fenghua Yuf5329592009-08-04 15:09:37 -07002927 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01002928 if (unlikely(ret)) {
2929 /* clear the page */
2930 dma_pte_clear_range(domain, start_vpfn,
2931 start_vpfn + size - 1);
2932 /* free page tables */
2933 dma_pte_free_pagetable(domain, start_vpfn,
2934 start_vpfn + size - 1);
2935 /* free iova */
2936 __free_iova(&domain->iovad, iova);
2937 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002938 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002939
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002940 /* it's a non-present to present mapping. Only flush if caching mode */
2941 if (cap_caching_mode(iommu->cap))
Nadav Amit82653632010-04-01 13:24:40 +03002942 iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002943 else
Weidong Han8c11e792008-12-08 15:29:22 +08002944 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002945
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002946 return nelems;
2947}
2948
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002949static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2950{
2951 return !dma_addr;
2952}
2953
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002954struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002955 .alloc_coherent = intel_alloc_coherent,
2956 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002957 .map_sg = intel_map_sg,
2958 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002959 .map_page = intel_map_page,
2960 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002961 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002962};
2963
2964static inline int iommu_domain_cache_init(void)
2965{
2966 int ret = 0;
2967
2968 iommu_domain_cache = kmem_cache_create("iommu_domain",
2969 sizeof(struct dmar_domain),
2970 0,
2971 SLAB_HWCACHE_ALIGN,
2972
2973 NULL);
2974 if (!iommu_domain_cache) {
2975 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2976 ret = -ENOMEM;
2977 }
2978
2979 return ret;
2980}
2981
2982static inline int iommu_devinfo_cache_init(void)
2983{
2984 int ret = 0;
2985
2986 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2987 sizeof(struct device_domain_info),
2988 0,
2989 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002990 NULL);
2991 if (!iommu_devinfo_cache) {
2992 printk(KERN_ERR "Couldn't create devinfo cache\n");
2993 ret = -ENOMEM;
2994 }
2995
2996 return ret;
2997}
2998
2999static inline int iommu_iova_cache_init(void)
3000{
3001 int ret = 0;
3002
3003 iommu_iova_cache = kmem_cache_create("iommu_iova",
3004 sizeof(struct iova),
3005 0,
3006 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003007 NULL);
3008 if (!iommu_iova_cache) {
3009 printk(KERN_ERR "Couldn't create iova cache\n");
3010 ret = -ENOMEM;
3011 }
3012
3013 return ret;
3014}
3015
3016static int __init iommu_init_mempool(void)
3017{
3018 int ret;
3019 ret = iommu_iova_cache_init();
3020 if (ret)
3021 return ret;
3022
3023 ret = iommu_domain_cache_init();
3024 if (ret)
3025 goto domain_error;
3026
3027 ret = iommu_devinfo_cache_init();
3028 if (!ret)
3029 return ret;
3030
3031 kmem_cache_destroy(iommu_domain_cache);
3032domain_error:
3033 kmem_cache_destroy(iommu_iova_cache);
3034
3035 return -ENOMEM;
3036}
3037
3038static void __init iommu_exit_mempool(void)
3039{
3040 kmem_cache_destroy(iommu_devinfo_cache);
3041 kmem_cache_destroy(iommu_domain_cache);
3042 kmem_cache_destroy(iommu_iova_cache);
3043
3044}
3045
Dan Williams556ab452010-07-23 15:47:56 -07003046static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3047{
3048 struct dmar_drhd_unit *drhd;
3049 u32 vtbar;
3050 int rc;
3051
3052 /* We know that this device on this chipset has its own IOMMU.
3053 * If we find it under a different IOMMU, then the BIOS is lying
3054 * to us. Hope that the IOMMU for this device is actually
3055 * disabled, and it needs no translation...
3056 */
3057 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3058 if (rc) {
3059 /* "can't" happen */
3060 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3061 return;
3062 }
3063 vtbar &= 0xffff0000;
3064
3065 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3066 drhd = dmar_find_matched_drhd_unit(pdev);
3067 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3068 TAINT_FIRMWARE_WORKAROUND,
3069 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3070 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3071}
3072DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3073
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003074static void __init init_no_remapping_devices(void)
3075{
3076 struct dmar_drhd_unit *drhd;
3077
3078 for_each_drhd_unit(drhd) {
3079 if (!drhd->include_all) {
3080 int i;
3081 for (i = 0; i < drhd->devices_cnt; i++)
3082 if (drhd->devices[i] != NULL)
3083 break;
3084 /* ignore DMAR unit if no pci devices exist */
3085 if (i == drhd->devices_cnt)
3086 drhd->ignored = 1;
3087 }
3088 }
3089
3090 if (dmar_map_gfx)
3091 return;
3092
3093 for_each_drhd_unit(drhd) {
3094 int i;
3095 if (drhd->ignored || drhd->include_all)
3096 continue;
3097
3098 for (i = 0; i < drhd->devices_cnt; i++)
3099 if (drhd->devices[i] &&
3100 !IS_GFX_DEVICE(drhd->devices[i]))
3101 break;
3102
3103 if (i < drhd->devices_cnt)
3104 continue;
3105
3106 /* bypass IOMMU if it is just for gfx devices */
3107 drhd->ignored = 1;
3108 for (i = 0; i < drhd->devices_cnt; i++) {
3109 if (!drhd->devices[i])
3110 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07003111 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003112 }
3113 }
3114}
3115
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003116#ifdef CONFIG_SUSPEND
3117static int init_iommu_hw(void)
3118{
3119 struct dmar_drhd_unit *drhd;
3120 struct intel_iommu *iommu = NULL;
3121
3122 for_each_active_iommu(iommu, drhd)
3123 if (iommu->qi)
3124 dmar_reenable_qi(iommu);
3125
Joseph Cihulab7792602011-05-03 00:08:37 -07003126 for_each_iommu(iommu, drhd) {
3127 if (drhd->ignored) {
3128 /*
3129 * we always have to disable PMRs or DMA may fail on
3130 * this device
3131 */
3132 if (force_on)
3133 iommu_disable_protect_mem_regions(iommu);
3134 continue;
3135 }
3136
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003137 iommu_flush_write_buffer(iommu);
3138
3139 iommu_set_root_entry(iommu);
3140
3141 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003142 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003143 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003144 DMA_TLB_GLOBAL_FLUSH);
Joseph Cihulab7792602011-05-03 00:08:37 -07003145 if (iommu_enable_translation(iommu))
3146 return 1;
David Woodhouseb94996c2009-09-19 15:28:12 -07003147 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003148 }
3149
3150 return 0;
3151}
3152
3153static void iommu_flush_all(void)
3154{
3155 struct dmar_drhd_unit *drhd;
3156 struct intel_iommu *iommu;
3157
3158 for_each_active_iommu(iommu, drhd) {
3159 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003160 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003161 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003162 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003163 }
3164}
3165
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003166static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003167{
3168 struct dmar_drhd_unit *drhd;
3169 struct intel_iommu *iommu = NULL;
3170 unsigned long flag;
3171
3172 for_each_active_iommu(iommu, drhd) {
3173 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3174 GFP_ATOMIC);
3175 if (!iommu->iommu_state)
3176 goto nomem;
3177 }
3178
3179 iommu_flush_all();
3180
3181 for_each_active_iommu(iommu, drhd) {
3182 iommu_disable_translation(iommu);
3183
3184 spin_lock_irqsave(&iommu->register_lock, flag);
3185
3186 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3187 readl(iommu->reg + DMAR_FECTL_REG);
3188 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3189 readl(iommu->reg + DMAR_FEDATA_REG);
3190 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3191 readl(iommu->reg + DMAR_FEADDR_REG);
3192 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3193 readl(iommu->reg + DMAR_FEUADDR_REG);
3194
3195 spin_unlock_irqrestore(&iommu->register_lock, flag);
3196 }
3197 return 0;
3198
3199nomem:
3200 for_each_active_iommu(iommu, drhd)
3201 kfree(iommu->iommu_state);
3202
3203 return -ENOMEM;
3204}
3205
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003206static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003207{
3208 struct dmar_drhd_unit *drhd;
3209 struct intel_iommu *iommu = NULL;
3210 unsigned long flag;
3211
3212 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003213 if (force_on)
3214 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3215 else
3216 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003217 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003218 }
3219
3220 for_each_active_iommu(iommu, drhd) {
3221
3222 spin_lock_irqsave(&iommu->register_lock, flag);
3223
3224 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3225 iommu->reg + DMAR_FECTL_REG);
3226 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3227 iommu->reg + DMAR_FEDATA_REG);
3228 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3229 iommu->reg + DMAR_FEADDR_REG);
3230 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3231 iommu->reg + DMAR_FEUADDR_REG);
3232
3233 spin_unlock_irqrestore(&iommu->register_lock, flag);
3234 }
3235
3236 for_each_active_iommu(iommu, drhd)
3237 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003238}
3239
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003240static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003241 .resume = iommu_resume,
3242 .suspend = iommu_suspend,
3243};
3244
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003245static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003246{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003247 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003248}
3249
3250#else
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003251static inline int init_iommu_pm_ops(void) { }
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003252#endif /* CONFIG_PM */
3253
Fenghua Yu99dcade2009-11-11 07:23:06 -08003254/*
3255 * Here we only respond to action of unbound device from driver.
3256 *
3257 * Added device is not attached to its DMAR domain here yet. That will happen
3258 * when mapping the device to iova.
3259 */
3260static int device_notifier(struct notifier_block *nb,
3261 unsigned long action, void *data)
3262{
3263 struct device *dev = data;
3264 struct pci_dev *pdev = to_pci_dev(dev);
3265 struct dmar_domain *domain;
3266
David Woodhouse44cd6132009-12-02 10:18:30 +00003267 if (iommu_no_mapping(dev))
3268 return 0;
3269
Fenghua Yu99dcade2009-11-11 07:23:06 -08003270 domain = find_domain(pdev);
3271 if (!domain)
3272 return 0;
3273
Alex Williamsona97590e2011-03-04 14:52:16 -07003274 if (action == BUS_NOTIFY_UNBOUND_DRIVER && !iommu_pass_through) {
Fenghua Yu99dcade2009-11-11 07:23:06 -08003275 domain_remove_one_dev_info(domain, pdev);
3276
Alex Williamsona97590e2011-03-04 14:52:16 -07003277 if (!(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) &&
3278 !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) &&
3279 list_empty(&domain->devices))
3280 domain_exit(domain);
3281 }
3282
Fenghua Yu99dcade2009-11-11 07:23:06 -08003283 return 0;
3284}
3285
3286static struct notifier_block device_nb = {
3287 .notifier_call = device_notifier,
3288};
3289
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003290int __init intel_iommu_init(void)
3291{
3292 int ret = 0;
3293
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003294 /* VT-d is required for a TXT/tboot launch, so enforce that */
3295 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003296
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003297 if (dmar_table_init()) {
3298 if (force_on)
3299 panic("tboot: Failed to initialize DMAR table\n");
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003300 return -ENODEV;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003301 }
3302
3303 if (dmar_dev_scope_init()) {
3304 if (force_on)
3305 panic("tboot: Failed to initialize DMAR device scope\n");
3306 return -ENODEV;
3307 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003308
Suresh Siddha2ae21012008-07-10 11:16:43 -07003309 /*
3310 * Check the need for DMA-remapping initialization now.
3311 * Above initialization will also be used by Interrupt-remapping.
3312 */
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003313 if (no_iommu || dmar_disabled)
Suresh Siddha2ae21012008-07-10 11:16:43 -07003314 return -ENODEV;
3315
Joseph Cihula51a63e62011-03-21 11:04:24 -07003316 if (iommu_init_mempool()) {
3317 if (force_on)
3318 panic("tboot: Failed to initialize iommu memory\n");
3319 return -ENODEV;
3320 }
3321
3322 if (dmar_init_reserved_ranges()) {
3323 if (force_on)
3324 panic("tboot: Failed to reserve iommu ranges\n");
3325 return -ENODEV;
3326 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003327
3328 init_no_remapping_devices();
3329
Joseph Cihulab7792602011-05-03 00:08:37 -07003330 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003331 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003332 if (force_on)
3333 panic("tboot: Failed to initialize DMARs\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003334 printk(KERN_ERR "IOMMU: dmar init failed\n");
3335 put_iova_domain(&reserved_iova_list);
3336 iommu_exit_mempool();
3337 return ret;
3338 }
3339 printk(KERN_INFO
3340 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
3341
mark gross5e0d2a62008-03-04 15:22:08 -08003342 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003343#ifdef CONFIG_SWIOTLB
3344 swiotlb = 0;
3345#endif
David Woodhouse19943b02009-08-04 16:19:20 +01003346 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003347
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003348 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003349
3350 register_iommu(&intel_iommu_ops);
3351
Fenghua Yu99dcade2009-11-11 07:23:06 -08003352 bus_register_notifier(&pci_bus_type, &device_nb);
3353
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003354 return 0;
3355}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07003356
Han, Weidong3199aa62009-02-26 17:31:12 +08003357static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
3358 struct pci_dev *pdev)
3359{
3360 struct pci_dev *tmp, *parent;
3361
3362 if (!iommu || !pdev)
3363 return;
3364
3365 /* dependent device detach */
3366 tmp = pci_find_upstream_pcie_bridge(pdev);
3367 /* Secondary interface's bus number and devfn 0 */
3368 if (tmp) {
3369 parent = pdev->bus->self;
3370 while (parent != tmp) {
3371 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01003372 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003373 parent = parent->bus->self;
3374 }
Stefan Assmann45e829e2009-12-03 06:49:24 -05003375 if (pci_is_pcie(tmp)) /* this is a PCIe-to-PCI bridge */
Han, Weidong3199aa62009-02-26 17:31:12 +08003376 iommu_detach_dev(iommu,
3377 tmp->subordinate->number, 0);
3378 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01003379 iommu_detach_dev(iommu, tmp->bus->number,
3380 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003381 }
3382}
3383
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003384static void domain_remove_one_dev_info(struct dmar_domain *domain,
Weidong Hanc7151a82008-12-08 22:51:37 +08003385 struct pci_dev *pdev)
3386{
3387 struct device_domain_info *info;
3388 struct intel_iommu *iommu;
3389 unsigned long flags;
3390 int found = 0;
3391 struct list_head *entry, *tmp;
3392
David Woodhouse276dbf992009-04-04 01:45:37 +01003393 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3394 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003395 if (!iommu)
3396 return;
3397
3398 spin_lock_irqsave(&device_domain_lock, flags);
3399 list_for_each_safe(entry, tmp, &domain->devices) {
3400 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01003401 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08003402 if (info->bus == pdev->bus->number &&
3403 info->devfn == pdev->devfn) {
3404 list_del(&info->link);
3405 list_del(&info->global);
3406 if (info->dev)
3407 info->dev->dev.archdata.iommu = NULL;
3408 spin_unlock_irqrestore(&device_domain_lock, flags);
3409
Yu Zhao93a23a72009-05-18 13:51:37 +08003410 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08003411 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003412 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003413 free_devinfo_mem(info);
3414
3415 spin_lock_irqsave(&device_domain_lock, flags);
3416
3417 if (found)
3418 break;
3419 else
3420 continue;
3421 }
3422
3423 /* if there is no other devices under the same iommu
3424 * owned by this domain, clear this iommu in iommu_bmp
3425 * update iommu count and coherency
3426 */
David Woodhouse276dbf992009-04-04 01:45:37 +01003427 if (iommu == device_to_iommu(info->segment, info->bus,
3428 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08003429 found = 1;
3430 }
3431
3432 if (found == 0) {
3433 unsigned long tmp_flags;
3434 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3435 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3436 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003437 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003438 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
Alex Williamsona97590e2011-03-04 14:52:16 -07003439
3440 spin_lock_irqsave(&iommu->lock, tmp_flags);
3441 clear_bit(domain->id, iommu->domain_ids);
3442 iommu->domains[domain->id] = NULL;
3443 spin_unlock_irqrestore(&iommu->lock, tmp_flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08003444 }
3445
3446 spin_unlock_irqrestore(&device_domain_lock, flags);
3447}
3448
3449static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3450{
3451 struct device_domain_info *info;
3452 struct intel_iommu *iommu;
3453 unsigned long flags1, flags2;
3454
3455 spin_lock_irqsave(&device_domain_lock, flags1);
3456 while (!list_empty(&domain->devices)) {
3457 info = list_entry(domain->devices.next,
3458 struct device_domain_info, link);
3459 list_del(&info->link);
3460 list_del(&info->global);
3461 if (info->dev)
3462 info->dev->dev.archdata.iommu = NULL;
3463
3464 spin_unlock_irqrestore(&device_domain_lock, flags1);
3465
Yu Zhao93a23a72009-05-18 13:51:37 +08003466 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01003467 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003468 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003469 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003470
3471 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08003472 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08003473 */
3474 spin_lock_irqsave(&domain->iommu_lock, flags2);
3475 if (test_and_clear_bit(iommu->seq_id,
3476 &domain->iommu_bmp)) {
3477 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003478 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003479 }
3480 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3481
3482 free_devinfo_mem(info);
3483 spin_lock_irqsave(&device_domain_lock, flags1);
3484 }
3485 spin_unlock_irqrestore(&device_domain_lock, flags1);
3486}
3487
Weidong Han5e98c4b2008-12-08 23:03:27 +08003488/* domain id for virtual machine, it won't be set in context */
3489static unsigned long vm_domid;
3490
3491static struct dmar_domain *iommu_alloc_vm_domain(void)
3492{
3493 struct dmar_domain *domain;
3494
3495 domain = alloc_domain_mem();
3496 if (!domain)
3497 return NULL;
3498
3499 domain->id = vm_domid++;
Suresh Siddha4c923d42009-10-02 11:01:24 -07003500 domain->nid = -1;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003501 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3502 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3503
3504 return domain;
3505}
3506
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003507static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08003508{
3509 int adjust_width;
3510
3511 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003512 spin_lock_init(&domain->iommu_lock);
3513
3514 domain_reserve_special_ranges(domain);
3515
3516 /* calculate AGAW */
3517 domain->gaw = guest_width;
3518 adjust_width = guestwidth_to_adjustwidth(guest_width);
3519 domain->agaw = width_to_agaw(adjust_width);
3520
3521 INIT_LIST_HEAD(&domain->devices);
3522
3523 domain->iommu_count = 0;
3524 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08003525 domain->iommu_snooping = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003526 domain->max_addr = 0;
Suresh Siddha4c923d42009-10-02 11:01:24 -07003527 domain->nid = -1;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003528
3529 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07003530 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003531 if (!domain->pgd)
3532 return -ENOMEM;
3533 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3534 return 0;
3535}
3536
3537static void iommu_free_vm_domain(struct dmar_domain *domain)
3538{
3539 unsigned long flags;
3540 struct dmar_drhd_unit *drhd;
3541 struct intel_iommu *iommu;
3542 unsigned long i;
3543 unsigned long ndomains;
3544
3545 for_each_drhd_unit(drhd) {
3546 if (drhd->ignored)
3547 continue;
3548 iommu = drhd->iommu;
3549
3550 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08003551 for_each_set_bit(i, iommu->domain_ids, ndomains) {
Weidong Han5e98c4b2008-12-08 23:03:27 +08003552 if (iommu->domains[i] == domain) {
3553 spin_lock_irqsave(&iommu->lock, flags);
3554 clear_bit(i, iommu->domain_ids);
3555 iommu->domains[i] = NULL;
3556 spin_unlock_irqrestore(&iommu->lock, flags);
3557 break;
3558 }
Weidong Han5e98c4b2008-12-08 23:03:27 +08003559 }
3560 }
3561}
3562
3563static void vm_domain_exit(struct dmar_domain *domain)
3564{
Weidong Han5e98c4b2008-12-08 23:03:27 +08003565 /* Domain 0 is reserved, so dont process it */
3566 if (!domain)
3567 return;
3568
3569 vm_domain_remove_all_dev_info(domain);
3570 /* destroy iovas */
3571 put_iova_domain(&domain->iovad);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003572
3573 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01003574 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003575
3576 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01003577 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003578
3579 iommu_free_vm_domain(domain);
3580 free_domain_mem(domain);
3581}
3582
Joerg Roedel5d450802008-12-03 14:52:32 +01003583static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003584{
Joerg Roedel5d450802008-12-03 14:52:32 +01003585 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003586
Joerg Roedel5d450802008-12-03 14:52:32 +01003587 dmar_domain = iommu_alloc_vm_domain();
3588 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003589 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003590 "intel_iommu_domain_init: dmar_domain == NULL\n");
3591 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003592 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003593 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003594 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003595 "intel_iommu_domain_init() failed\n");
3596 vm_domain_exit(dmar_domain);
3597 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003598 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003599 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003600
Joerg Roedel5d450802008-12-03 14:52:32 +01003601 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003602}
Kay, Allen M38717942008-09-09 18:37:29 +03003603
Joerg Roedel5d450802008-12-03 14:52:32 +01003604static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003605{
Joerg Roedel5d450802008-12-03 14:52:32 +01003606 struct dmar_domain *dmar_domain = domain->priv;
3607
3608 domain->priv = NULL;
3609 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003610}
Kay, Allen M38717942008-09-09 18:37:29 +03003611
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003612static int intel_iommu_attach_device(struct iommu_domain *domain,
3613 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003614{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003615 struct dmar_domain *dmar_domain = domain->priv;
3616 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003617 struct intel_iommu *iommu;
3618 int addr_width;
Kay, Allen M38717942008-09-09 18:37:29 +03003619
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003620 /* normally pdev is not mapped */
3621 if (unlikely(domain_context_mapped(pdev))) {
3622 struct dmar_domain *old_domain;
3623
3624 old_domain = find_domain(pdev);
3625 if (old_domain) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003626 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
3627 dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)
3628 domain_remove_one_dev_info(old_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003629 else
3630 domain_remove_dev_info(old_domain);
3631 }
3632 }
3633
David Woodhouse276dbf992009-04-04 01:45:37 +01003634 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3635 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003636 if (!iommu)
3637 return -ENODEV;
3638
3639 /* check if this iommu agaw is sufficient for max mapped address */
3640 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01003641 if (addr_width > cap_mgaw(iommu->cap))
3642 addr_width = cap_mgaw(iommu->cap);
3643
3644 if (dmar_domain->max_addr > (1LL << addr_width)) {
3645 printk(KERN_ERR "%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003646 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01003647 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003648 return -EFAULT;
3649 }
Tom Lyona99c47a2010-05-17 08:20:45 +01003650 dmar_domain->gaw = addr_width;
3651
3652 /*
3653 * Knock out extra levels of page tables if necessary
3654 */
3655 while (iommu->agaw < dmar_domain->agaw) {
3656 struct dma_pte *pte;
3657
3658 pte = dmar_domain->pgd;
3659 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08003660 dmar_domain->pgd = (struct dma_pte *)
3661 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01003662 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01003663 }
3664 dmar_domain->agaw--;
3665 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003666
David Woodhouse5fe60f42009-08-09 10:53:41 +01003667 return domain_add_dev_info(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003668}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003669
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003670static void intel_iommu_detach_device(struct iommu_domain *domain,
3671 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003672{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003673 struct dmar_domain *dmar_domain = domain->priv;
3674 struct pci_dev *pdev = to_pci_dev(dev);
3675
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003676 domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003677}
Kay, Allen M38717942008-09-09 18:37:29 +03003678
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003679static int intel_iommu_map(struct iommu_domain *domain,
3680 unsigned long iova, phys_addr_t hpa,
3681 int gfp_order, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003682{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003683 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003684 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003685 int prot = 0;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003686 size_t size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003687 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003688
Joerg Roedeldde57a22008-12-03 15:04:09 +01003689 if (iommu_prot & IOMMU_READ)
3690 prot |= DMA_PTE_READ;
3691 if (iommu_prot & IOMMU_WRITE)
3692 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08003693 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3694 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003695
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003696 size = PAGE_SIZE << gfp_order;
David Woodhouse163cc522009-06-28 00:51:17 +01003697 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003698 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003699 u64 end;
3700
3701 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01003702 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003703 if (end < max_addr) {
Tom Lyon8954da12010-05-17 08:19:52 +01003704 printk(KERN_ERR "%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003705 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01003706 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003707 return -EFAULT;
3708 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003709 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003710 }
David Woodhousead051222009-06-28 14:22:28 +01003711 /* Round up size to next multiple of PAGE_SIZE, if it and
3712 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01003713 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01003714 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
3715 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003716 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003717}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003718
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003719static int intel_iommu_unmap(struct iommu_domain *domain,
3720 unsigned long iova, int gfp_order)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003721{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003722 struct dmar_domain *dmar_domain = domain->priv;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003723 size_t size = PAGE_SIZE << gfp_order;
Sheng Yang4b99d352009-07-08 11:52:52 +01003724
David Woodhouse163cc522009-06-28 00:51:17 +01003725 dma_pte_clear_range(dmar_domain, iova >> VTD_PAGE_SHIFT,
3726 (iova + size - 1) >> VTD_PAGE_SHIFT);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003727
David Woodhouse163cc522009-06-28 00:51:17 +01003728 if (dmar_domain->max_addr == iova + size)
3729 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003730
3731 return gfp_order;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003732}
Kay, Allen M38717942008-09-09 18:37:29 +03003733
Joerg Roedeld14d6572008-12-03 15:06:57 +01003734static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3735 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003736{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003737 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003738 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003739 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003740
David Woodhouseb026fd22009-06-28 10:37:25 +01003741 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT);
Kay, Allen M38717942008-09-09 18:37:29 +03003742 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003743 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003744
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003745 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003746}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003747
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003748static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3749 unsigned long cap)
3750{
3751 struct dmar_domain *dmar_domain = domain->priv;
3752
3753 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3754 return dmar_domain->iommu_snooping;
Tom Lyon323f99c2010-07-02 16:56:14 -04003755 if (cap == IOMMU_CAP_INTR_REMAP)
3756 return intr_remapping_enabled;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003757
3758 return 0;
3759}
3760
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003761static struct iommu_ops intel_iommu_ops = {
3762 .domain_init = intel_iommu_domain_init,
3763 .domain_destroy = intel_iommu_domain_destroy,
3764 .attach_dev = intel_iommu_attach_device,
3765 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003766 .map = intel_iommu_map,
3767 .unmap = intel_iommu_unmap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003768 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003769 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003770};
David Woodhouse9af88142009-02-13 23:18:03 +00003771
3772static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3773{
3774 /*
3775 * Mobile 4 Series Chipset neglects to set RWBF capability,
3776 * but needs it:
3777 */
3778 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3779 rwbf_quirk = 1;
David Woodhouse2d9e6672010-06-15 10:57:57 +01003780
3781 /* https://bugzilla.redhat.com/show_bug.cgi?id=538163 */
3782 if (dev->revision == 0x07) {
3783 printk(KERN_INFO "DMAR: Disabling IOMMU for graphics on this chipset\n");
3784 dmar_map_gfx = 0;
3785 }
David Woodhouse9af88142009-02-13 23:18:03 +00003786}
3787
3788DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07003789
Adam Jacksoneecfd572010-08-25 21:17:34 +01003790#define GGC 0x52
3791#define GGC_MEMORY_SIZE_MASK (0xf << 8)
3792#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
3793#define GGC_MEMORY_SIZE_1M (0x1 << 8)
3794#define GGC_MEMORY_SIZE_2M (0x3 << 8)
3795#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
3796#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
3797#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
3798#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
3799
David Woodhouse9eecabc2010-09-21 22:28:23 +01003800static void __devinit quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
3801{
3802 unsigned short ggc;
3803
Adam Jacksoneecfd572010-08-25 21:17:34 +01003804 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01003805 return;
3806
Adam Jacksoneecfd572010-08-25 21:17:34 +01003807 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
David Woodhouse9eecabc2010-09-21 22:28:23 +01003808 printk(KERN_INFO "DMAR: BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
3809 dmar_map_gfx = 0;
3810 }
3811}
3812DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
3813DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
3814DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
3815DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
3816
David Woodhousee0fc7e02009-09-30 09:12:17 -07003817/* On Tylersburg chipsets, some BIOSes have been known to enable the
3818 ISOCH DMAR unit for the Azalia sound device, but not give it any
3819 TLB entries, which causes it to deadlock. Check for that. We do
3820 this in a function called from init_dmars(), instead of in a PCI
3821 quirk, because we don't want to print the obnoxious "BIOS broken"
3822 message if VT-d is actually disabled.
3823*/
3824static void __init check_tylersburg_isoch(void)
3825{
3826 struct pci_dev *pdev;
3827 uint32_t vtisochctrl;
3828
3829 /* If there's no Azalia in the system anyway, forget it. */
3830 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
3831 if (!pdev)
3832 return;
3833 pci_dev_put(pdev);
3834
3835 /* System Management Registers. Might be hidden, in which case
3836 we can't do the sanity check. But that's OK, because the
3837 known-broken BIOSes _don't_ actually hide it, so far. */
3838 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
3839 if (!pdev)
3840 return;
3841
3842 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
3843 pci_dev_put(pdev);
3844 return;
3845 }
3846
3847 pci_dev_put(pdev);
3848
3849 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
3850 if (vtisochctrl & 1)
3851 return;
3852
3853 /* Drop all bits other than the number of TLB entries */
3854 vtisochctrl &= 0x1c;
3855
3856 /* If we have the recommended number of TLB entries (16), fine. */
3857 if (vtisochctrl == 0x10)
3858 return;
3859
3860 /* Zero TLB entries? You get to ride the short bus to school. */
3861 if (!vtisochctrl) {
3862 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
3863 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
3864 dmi_get_system_info(DMI_BIOS_VENDOR),
3865 dmi_get_system_info(DMI_BIOS_VERSION),
3866 dmi_get_system_info(DMI_PRODUCT_VERSION));
3867 iommu_identity_mapping |= IDENTMAP_AZALIA;
3868 return;
3869 }
3870
3871 printk(KERN_WARNING "DMAR: Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
3872 vtisochctrl);
3873}