blob: 98be0b55ac0b5a29ae93dc70792838a3d6d0d686 [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
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100118static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
119{
120 return 1 << ((lvl - 1) * LEVEL_STRIDE);
121}
122
David Woodhousedd4e8312009-06-27 16:21:20 +0100123/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
124 are never going to work. */
125static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
126{
127 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
128}
129
130static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
131{
132 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
133}
134static inline unsigned long page_to_dma_pfn(struct page *pg)
135{
136 return mm_to_dma_pfn(page_to_pfn(pg));
137}
138static inline unsigned long virt_to_dma_pfn(void *p)
139{
140 return page_to_dma_pfn(virt_to_page(p));
141}
142
Weidong Hand9630fe2008-12-08 11:06:32 +0800143/* global iommu list, set NULL for ignored DMAR units */
144static struct intel_iommu **g_iommus;
145
David Woodhousee0fc7e02009-09-30 09:12:17 -0700146static void __init check_tylersburg_isoch(void);
David Woodhouse9af88142009-02-13 23:18:03 +0000147static int rwbf_quirk;
148
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000149/*
Joseph Cihulab7792602011-05-03 00:08:37 -0700150 * set to 1 to panic kernel if can't successfully enable VT-d
151 * (used when kernel is launched w/ TXT)
152 */
153static int force_on = 0;
154
155/*
Mark McLoughlin46b08e12008-11-20 15:49:44 +0000156 * 0: Present
157 * 1-11: Reserved
158 * 12-63: Context Ptr (12 - (haw-1))
159 * 64-127: Reserved
160 */
161struct root_entry {
162 u64 val;
163 u64 rsvd1;
164};
165#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
166static inline bool root_present(struct root_entry *root)
167{
168 return (root->val & 1);
169}
170static inline void set_root_present(struct root_entry *root)
171{
172 root->val |= 1;
173}
174static inline void set_root_value(struct root_entry *root, unsigned long value)
175{
176 root->val |= value & VTD_PAGE_MASK;
177}
178
179static inline struct context_entry *
180get_context_addr_from_root(struct root_entry *root)
181{
182 return (struct context_entry *)
183 (root_present(root)?phys_to_virt(
184 root->val & VTD_PAGE_MASK) :
185 NULL);
186}
187
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000188/*
189 * low 64 bits:
190 * 0: present
191 * 1: fault processing disable
192 * 2-3: translation type
193 * 12-63: address space root
194 * high 64 bits:
195 * 0-2: address width
196 * 3-6: aval
197 * 8-23: domain id
198 */
199struct context_entry {
200 u64 lo;
201 u64 hi;
202};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000203
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000204static inline bool context_present(struct context_entry *context)
205{
206 return (context->lo & 1);
207}
208static inline void context_set_present(struct context_entry *context)
209{
210 context->lo |= 1;
211}
212
213static inline void context_set_fault_enable(struct context_entry *context)
214{
215 context->lo &= (((u64)-1) << 2) | 1;
216}
217
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000218static inline void context_set_translation_type(struct context_entry *context,
219 unsigned long value)
220{
221 context->lo &= (((u64)-1) << 4) | 3;
222 context->lo |= (value & 3) << 2;
223}
224
225static inline void context_set_address_root(struct context_entry *context,
226 unsigned long value)
227{
228 context->lo |= value & VTD_PAGE_MASK;
229}
230
231static inline void context_set_address_width(struct context_entry *context,
232 unsigned long value)
233{
234 context->hi |= value & 7;
235}
236
237static inline void context_set_domain_id(struct context_entry *context,
238 unsigned long value)
239{
240 context->hi |= (value & ((1 << 16) - 1)) << 8;
241}
242
243static inline void context_clear_entry(struct context_entry *context)
244{
245 context->lo = 0;
246 context->hi = 0;
247}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000248
Mark McLoughlin622ba122008-11-20 15:49:46 +0000249/*
250 * 0: readable
251 * 1: writable
252 * 2-6: reserved
253 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800254 * 8-10: available
255 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000256 * 12-63: Host physcial address
257 */
258struct dma_pte {
259 u64 val;
260};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000261
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000262static inline void dma_clear_pte(struct dma_pte *pte)
263{
264 pte->val = 0;
265}
266
267static inline void dma_set_pte_readable(struct dma_pte *pte)
268{
269 pte->val |= DMA_PTE_READ;
270}
271
272static inline void dma_set_pte_writable(struct dma_pte *pte)
273{
274 pte->val |= DMA_PTE_WRITE;
275}
276
Sheng Yang9cf06692009-03-18 15:33:07 +0800277static inline void dma_set_pte_snp(struct dma_pte *pte)
278{
279 pte->val |= DMA_PTE_SNP;
280}
281
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000282static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
283{
284 pte->val = (pte->val & ~3) | (prot & 3);
285}
286
287static inline u64 dma_pte_addr(struct dma_pte *pte)
288{
David Woodhousec85994e2009-07-01 19:21:24 +0100289#ifdef CONFIG_64BIT
290 return pte->val & VTD_PAGE_MASK;
291#else
292 /* Must have a full atomic 64-bit read */
David Woodhouse1a8bd482010-08-10 01:38:53 +0100293 return __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
David Woodhousec85994e2009-07-01 19:21:24 +0100294#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000295}
296
David Woodhousedd4e8312009-06-27 16:21:20 +0100297static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn)
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000298{
David Woodhousedd4e8312009-06-27 16:21:20 +0100299 pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000300}
301
302static inline bool dma_pte_present(struct dma_pte *pte)
303{
304 return (pte->val & 3) != 0;
305}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000306
David Woodhouse75e6bf92009-07-02 11:21:16 +0100307static inline int first_pte_in_page(struct dma_pte *pte)
308{
309 return !((unsigned long)pte & ~VTD_PAGE_MASK);
310}
311
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700312/*
313 * This domain is a statically identity mapping domain.
314 * 1. This domain creats a static 1:1 mapping to all usable memory.
315 * 2. It maps to each iommu if successful.
316 * 3. Each iommu mapps to this domain if successful.
317 */
David Woodhouse19943b02009-08-04 16:19:20 +0100318static struct dmar_domain *si_domain;
319static int hw_pass_through = 1;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700320
Weidong Han3b5410e2008-12-08 09:17:15 +0800321/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100322#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800323
Weidong Han1ce28fe2008-12-08 16:35:39 +0800324/* domain represents a virtual machine, more than one devices
325 * across iommus may be owned in one domain, e.g. kvm guest.
326 */
327#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
328
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700329/* si_domain contains mulitple devices */
330#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2)
331
Mark McLoughlin99126f72008-11-20 15:49:47 +0000332struct dmar_domain {
333 int id; /* domain id */
Suresh Siddha4c923d42009-10-02 11:01:24 -0700334 int nid; /* node id */
Weidong Han8c11e792008-12-08 15:29:22 +0800335 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000336
337 struct list_head devices; /* all devices' list */
338 struct iova_domain iovad; /* iova's that belong to this domain */
339
340 struct dma_pte *pgd; /* virtual address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000341 int gaw; /* max guest address width */
342
343 /* adjusted guest address width, 0 is level 2 30-bit */
344 int agaw;
345
Weidong Han3b5410e2008-12-08 09:17:15 +0800346 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800347
348 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800349 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800350 int iommu_count; /* reference count of iommu */
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100351 int iommu_superpage;/* Level of superpages supported:
352 0 == 4KiB (no superpages), 1 == 2MiB,
353 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
Weidong Hanc7151a82008-12-08 22:51:37 +0800354 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800355 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000356};
357
Mark McLoughlina647dac2008-11-20 15:49:48 +0000358/* PCI domain-device relationship */
359struct device_domain_info {
360 struct list_head link; /* link to domain siblings */
361 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100362 int segment; /* PCI domain */
363 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000364 u8 devfn; /* PCI devfn number */
Stefan Assmann45e829e2009-12-03 06:49:24 -0500365 struct pci_dev *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800366 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000367 struct dmar_domain *domain; /* pointer to domain */
368};
369
mark gross5e0d2a62008-03-04 15:22:08 -0800370static void flush_unmaps_timeout(unsigned long data);
371
372DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
373
mark gross80b20dd2008-04-18 13:53:58 -0700374#define HIGH_WATER_MARK 250
375struct deferred_flush_tables {
376 int next;
377 struct iova *iova[HIGH_WATER_MARK];
378 struct dmar_domain *domain[HIGH_WATER_MARK];
379};
380
381static struct deferred_flush_tables *deferred_flush;
382
mark gross5e0d2a62008-03-04 15:22:08 -0800383/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800384static int g_num_of_iommus;
385
386static DEFINE_SPINLOCK(async_umap_flush_lock);
387static LIST_HEAD(unmaps_to_do);
388
389static int timer_on;
390static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800391
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700392static void domain_remove_dev_info(struct dmar_domain *domain);
393
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800394#ifdef CONFIG_DMAR_DEFAULT_ON
395int dmar_disabled = 0;
396#else
397int dmar_disabled = 1;
398#endif /*CONFIG_DMAR_DEFAULT_ON*/
399
David Woodhouse2d9e6672010-06-15 10:57:57 +0100400static int dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700401static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800402static int intel_iommu_strict;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100403static int intel_iommu_superpage = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700404
405#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
406static DEFINE_SPINLOCK(device_domain_lock);
407static LIST_HEAD(device_domain_list);
408
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100409static struct iommu_ops intel_iommu_ops;
410
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700411static int __init intel_iommu_setup(char *str)
412{
413 if (!str)
414 return -EINVAL;
415 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800416 if (!strncmp(str, "on", 2)) {
417 dmar_disabled = 0;
418 printk(KERN_INFO "Intel-IOMMU: enabled\n");
419 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700420 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800421 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700422 } else if (!strncmp(str, "igfx_off", 8)) {
423 dmar_map_gfx = 0;
424 printk(KERN_INFO
425 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700426 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800427 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700428 "Intel-IOMMU: Forcing DAC for PCI devices\n");
429 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800430 } else if (!strncmp(str, "strict", 6)) {
431 printk(KERN_INFO
432 "Intel-IOMMU: disable batched IOTLB flush\n");
433 intel_iommu_strict = 1;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100434 } else if (!strncmp(str, "sp_off", 6)) {
435 printk(KERN_INFO
436 "Intel-IOMMU: disable supported super page\n");
437 intel_iommu_superpage = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700438 }
439
440 str += strcspn(str, ",");
441 while (*str == ',')
442 str++;
443 }
444 return 0;
445}
446__setup("intel_iommu=", intel_iommu_setup);
447
448static struct kmem_cache *iommu_domain_cache;
449static struct kmem_cache *iommu_devinfo_cache;
450static struct kmem_cache *iommu_iova_cache;
451
Suresh Siddha4c923d42009-10-02 11:01:24 -0700452static inline void *alloc_pgtable_page(int node)
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700453{
Suresh Siddha4c923d42009-10-02 11:01:24 -0700454 struct page *page;
455 void *vaddr = NULL;
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700456
Suresh Siddha4c923d42009-10-02 11:01:24 -0700457 page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
458 if (page)
459 vaddr = page_address(page);
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700460 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700461}
462
463static inline void free_pgtable_page(void *vaddr)
464{
465 free_page((unsigned long)vaddr);
466}
467
468static inline void *alloc_domain_mem(void)
469{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900470 return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700471}
472
Kay, Allen M38717942008-09-09 18:37:29 +0300473static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700474{
475 kmem_cache_free(iommu_domain_cache, vaddr);
476}
477
478static inline void * alloc_devinfo_mem(void)
479{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900480 return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700481}
482
483static inline void free_devinfo_mem(void *vaddr)
484{
485 kmem_cache_free(iommu_devinfo_cache, vaddr);
486}
487
488struct iova *alloc_iova_mem(void)
489{
KOSAKI Motohiro354bb652009-11-17 16:21:09 +0900490 return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700491}
492
493void free_iova_mem(struct iova *iova)
494{
495 kmem_cache_free(iommu_iova_cache, iova);
496}
497
Weidong Han1b573682008-12-08 15:34:06 +0800498
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700499static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800500{
501 unsigned long sagaw;
502 int agaw = -1;
503
504 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700505 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800506 agaw >= 0; agaw--) {
507 if (test_bit(agaw, &sagaw))
508 break;
509 }
510
511 return agaw;
512}
513
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700514/*
515 * Calculate max SAGAW for each iommu.
516 */
517int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
518{
519 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
520}
521
522/*
523 * calculate agaw for each iommu.
524 * "SAGAW" may be different across iommus, use a default agaw, and
525 * get a supported less agaw for iommus that don't support the default agaw.
526 */
527int iommu_calculate_agaw(struct intel_iommu *iommu)
528{
529 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
530}
531
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700532/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800533static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
534{
535 int iommu_id;
536
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700537 /* si_domain and vm domain should not get here. */
Weidong Han1ce28fe2008-12-08 16:35:39 +0800538 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700539 BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
Weidong Han1ce28fe2008-12-08 16:35:39 +0800540
Weidong Han8c11e792008-12-08 15:29:22 +0800541 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
542 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
543 return NULL;
544
545 return g_iommus[iommu_id];
546}
547
Weidong Han8e6040972008-12-08 15:49:06 +0800548static void domain_update_iommu_coherency(struct dmar_domain *domain)
549{
550 int i;
551
552 domain->iommu_coherency = 1;
553
Akinobu Mitaa45946a2010-03-11 14:04:08 -0800554 for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) {
Weidong Han8e6040972008-12-08 15:49:06 +0800555 if (!ecap_coherent(g_iommus[i]->ecap)) {
556 domain->iommu_coherency = 0;
557 break;
558 }
Weidong Han8e6040972008-12-08 15:49:06 +0800559 }
560}
561
Sheng Yang58c610b2009-03-18 15:33:05 +0800562static void domain_update_iommu_snooping(struct dmar_domain *domain)
563{
564 int i;
565
566 domain->iommu_snooping = 1;
567
Akinobu Mitaa45946a2010-03-11 14:04:08 -0800568 for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) {
Sheng Yang58c610b2009-03-18 15:33:05 +0800569 if (!ecap_sc_support(g_iommus[i]->ecap)) {
570 domain->iommu_snooping = 0;
571 break;
572 }
Sheng Yang58c610b2009-03-18 15:33:05 +0800573 }
574}
575
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100576static void domain_update_iommu_superpage(struct dmar_domain *domain)
577{
578 int i, mask = 0xf;
579
580 if (!intel_iommu_superpage) {
581 domain->iommu_superpage = 0;
582 return;
583 }
584
585 domain->iommu_superpage = 4; /* 1TiB */
586
587 for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) {
588 mask |= cap_super_page_val(g_iommus[i]->cap);
589 if (!mask) {
590 break;
591 }
592 }
593 domain->iommu_superpage = fls(mask);
594}
595
Sheng Yang58c610b2009-03-18 15:33:05 +0800596/* Some capabilities may be different across iommus */
597static void domain_update_iommu_cap(struct dmar_domain *domain)
598{
599 domain_update_iommu_coherency(domain);
600 domain_update_iommu_snooping(domain);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100601 domain_update_iommu_superpage(domain);
Sheng Yang58c610b2009-03-18 15:33:05 +0800602}
603
David Woodhouse276dbf992009-04-04 01:45:37 +0100604static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800605{
606 struct dmar_drhd_unit *drhd = NULL;
607 int i;
608
609 for_each_drhd_unit(drhd) {
610 if (drhd->ignored)
611 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100612 if (segment != drhd->segment)
613 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800614
David Woodhouse924b6232009-04-04 00:39:25 +0100615 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000616 if (drhd->devices[i] &&
617 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800618 drhd->devices[i]->devfn == devfn)
619 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700620 if (drhd->devices[i] &&
621 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100622 drhd->devices[i]->subordinate->number <= bus &&
623 drhd->devices[i]->subordinate->subordinate >= bus)
624 return drhd->iommu;
625 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800626
627 if (drhd->include_all)
628 return drhd->iommu;
629 }
630
631 return NULL;
632}
633
Weidong Han5331fe62008-12-08 23:00:00 +0800634static void domain_flush_cache(struct dmar_domain *domain,
635 void *addr, int size)
636{
637 if (!domain->iommu_coherency)
638 clflush_cache_range(addr, size);
639}
640
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700641/* Gets context entry for a given bus and devfn */
642static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
643 u8 bus, u8 devfn)
644{
645 struct root_entry *root;
646 struct context_entry *context;
647 unsigned long phy_addr;
648 unsigned long flags;
649
650 spin_lock_irqsave(&iommu->lock, flags);
651 root = &iommu->root_entry[bus];
652 context = get_context_addr_from_root(root);
653 if (!context) {
Suresh Siddha4c923d42009-10-02 11:01:24 -0700654 context = (struct context_entry *)
655 alloc_pgtable_page(iommu->node);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700656 if (!context) {
657 spin_unlock_irqrestore(&iommu->lock, flags);
658 return NULL;
659 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700660 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700661 phy_addr = virt_to_phys((void *)context);
662 set_root_value(root, phy_addr);
663 set_root_present(root);
664 __iommu_flush_cache(iommu, root, sizeof(*root));
665 }
666 spin_unlock_irqrestore(&iommu->lock, flags);
667 return &context[devfn];
668}
669
670static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
671{
672 struct root_entry *root;
673 struct context_entry *context;
674 int ret;
675 unsigned long flags;
676
677 spin_lock_irqsave(&iommu->lock, flags);
678 root = &iommu->root_entry[bus];
679 context = get_context_addr_from_root(root);
680 if (!context) {
681 ret = 0;
682 goto out;
683 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000684 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700685out:
686 spin_unlock_irqrestore(&iommu->lock, flags);
687 return ret;
688}
689
690static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
691{
692 struct root_entry *root;
693 struct context_entry *context;
694 unsigned long flags;
695
696 spin_lock_irqsave(&iommu->lock, flags);
697 root = &iommu->root_entry[bus];
698 context = get_context_addr_from_root(root);
699 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000700 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700701 __iommu_flush_cache(iommu, &context[devfn], \
702 sizeof(*context));
703 }
704 spin_unlock_irqrestore(&iommu->lock, flags);
705}
706
707static void free_context_table(struct intel_iommu *iommu)
708{
709 struct root_entry *root;
710 int i;
711 unsigned long flags;
712 struct context_entry *context;
713
714 spin_lock_irqsave(&iommu->lock, flags);
715 if (!iommu->root_entry) {
716 goto out;
717 }
718 for (i = 0; i < ROOT_ENTRY_NR; i++) {
719 root = &iommu->root_entry[i];
720 context = get_context_addr_from_root(root);
721 if (context)
722 free_pgtable_page(context);
723 }
724 free_pgtable_page(iommu->root_entry);
725 iommu->root_entry = NULL;
726out:
727 spin_unlock_irqrestore(&iommu->lock, flags);
728}
729
David Woodhouseb026fd22009-06-28 10:37:25 +0100730static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100731 unsigned long pfn, int large_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700732{
David Woodhouseb026fd22009-06-28 10:37:25 +0100733 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700734 struct dma_pte *parent, *pte = NULL;
735 int level = agaw_to_level(domain->agaw);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100736 int offset, target_level;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700737
738 BUG_ON(!domain->pgd);
David Woodhouseb026fd22009-06-28 10:37:25 +0100739 BUG_ON(addr_width < BITS_PER_LONG && pfn >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700740 parent = domain->pgd;
741
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100742 /* Search pte */
743 if (!large_level)
744 target_level = 1;
745 else
746 target_level = large_level;
747
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700748 while (level > 0) {
749 void *tmp_page;
750
David Woodhouseb026fd22009-06-28 10:37:25 +0100751 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700752 pte = &parent[offset];
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100753 if (!large_level && (pte->val & DMA_PTE_LARGE_PAGE))
754 break;
755 if (level == target_level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700756 break;
757
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000758 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100759 uint64_t pteval;
760
Suresh Siddha4c923d42009-10-02 11:01:24 -0700761 tmp_page = alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700762
David Woodhouse206a73c2009-07-01 19:30:28 +0100763 if (!tmp_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700764 return NULL;
David Woodhouse206a73c2009-07-01 19:30:28 +0100765
David Woodhousec85994e2009-07-01 19:21:24 +0100766 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
Benjamin LaHaise64de5af2009-09-16 21:05:55 -0400767 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 +0100768 if (cmpxchg64(&pte->val, 0ULL, pteval)) {
769 /* Someone else set it while we were thinking; use theirs. */
770 free_pgtable_page(tmp_page);
771 } else {
772 dma_pte_addr(pte);
773 domain_flush_cache(domain, pte, sizeof(*pte));
774 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700775 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000776 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700777 level--;
778 }
779
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700780 return pte;
781}
782
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100783
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700784/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100785static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
786 unsigned long pfn,
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100787 int level, int *large_page)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700788{
789 struct dma_pte *parent, *pte = NULL;
790 int total = agaw_to_level(domain->agaw);
791 int offset;
792
793 parent = domain->pgd;
794 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100795 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700796 pte = &parent[offset];
797 if (level == total)
798 return pte;
799
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100800 if (!dma_pte_present(pte)) {
801 *large_page = total;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700802 break;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100803 }
804
805 if (pte->val & DMA_PTE_LARGE_PAGE) {
806 *large_page = total;
807 return pte;
808 }
809
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000810 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700811 total--;
812 }
813 return NULL;
814}
815
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700816/* clear last level pte, a tlb flush should be followed */
David Woodhouse595badf2009-06-27 22:09:11 +0100817static void dma_pte_clear_range(struct dmar_domain *domain,
818 unsigned long start_pfn,
819 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700820{
David Woodhouse04b18e62009-06-27 19:15:01 +0100821 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100822 unsigned int large_page = 1;
David Woodhouse310a5ab2009-06-28 18:52:20 +0100823 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700824
David Woodhouse04b18e62009-06-27 19:15:01 +0100825 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
David Woodhouse595badf2009-06-27 22:09:11 +0100826 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse59c36282009-09-19 07:36:28 -0700827 BUG_ON(start_pfn > last_pfn);
David Woodhouse66eae842009-06-27 19:00:32 +0100828
David Woodhouse04b18e62009-06-27 19:15:01 +0100829 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse59c36282009-09-19 07:36:28 -0700830 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100831 large_page = 1;
832 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100833 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100834 start_pfn = align_to_level(start_pfn + 1, large_page + 1);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100835 continue;
836 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100837 do {
David Woodhouse310a5ab2009-06-28 18:52:20 +0100838 dma_clear_pte(pte);
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100839 start_pfn += lvl_to_nr_pages(large_page);
David Woodhouse310a5ab2009-06-28 18:52:20 +0100840 pte++;
David Woodhouse75e6bf92009-07-02 11:21:16 +0100841 } while (start_pfn <= last_pfn && !first_pte_in_page(pte));
842
David Woodhouse310a5ab2009-06-28 18:52:20 +0100843 domain_flush_cache(domain, first_pte,
844 (void *)pte - (void *)first_pte);
David Woodhouse59c36282009-09-19 07:36:28 -0700845
846 } while (start_pfn && start_pfn <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700847}
848
849/* free page table pages. last level pte should already be cleared */
850static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +0100851 unsigned long start_pfn,
852 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700853{
David Woodhouse6660c632009-06-27 22:41:00 +0100854 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhousef3a0a522009-06-30 03:40:07 +0100855 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700856 int total = agaw_to_level(domain->agaw);
857 int level;
David Woodhouse6660c632009-06-27 22:41:00 +0100858 unsigned long tmp;
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100859 int large_page = 2;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700860
David Woodhouse6660c632009-06-27 22:41:00 +0100861 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
862 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse59c36282009-09-19 07:36:28 -0700863 BUG_ON(start_pfn > last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700864
David Woodhousef3a0a522009-06-30 03:40:07 +0100865 /* We don't need lock here; nobody else touches the iova range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700866 level = 2;
867 while (level <= total) {
David Woodhouse6660c632009-06-27 22:41:00 +0100868 tmp = align_to_level(start_pfn, level);
869
David Woodhousef3a0a522009-06-30 03:40:07 +0100870 /* If we can't even clear one PTE at this level, we're done */
David Woodhouse6660c632009-06-27 22:41:00 +0100871 if (tmp + level_size(level) - 1 > last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700872 return;
873
David Woodhouse59c36282009-09-19 07:36:28 -0700874 do {
Youquan Song6dd9a7c2011-05-25 19:13:49 +0100875 large_page = level;
876 first_pte = pte = dma_pfn_level_pte(domain, tmp, level, &large_page);
877 if (large_page > level)
878 level = large_page + 1;
David Woodhousef3a0a522009-06-30 03:40:07 +0100879 if (!pte) {
880 tmp = align_to_level(tmp + 1, level + 1);
881 continue;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700882 }
David Woodhouse75e6bf92009-07-02 11:21:16 +0100883 do {
David Woodhouse6a43e572009-07-02 12:02:34 +0100884 if (dma_pte_present(pte)) {
885 free_pgtable_page(phys_to_virt(dma_pte_addr(pte)));
886 dma_clear_pte(pte);
887 }
David Woodhousef3a0a522009-06-30 03:40:07 +0100888 pte++;
889 tmp += level_size(level);
David Woodhouse75e6bf92009-07-02 11:21:16 +0100890 } while (!first_pte_in_page(pte) &&
891 tmp + level_size(level) - 1 <= last_pfn);
892
David Woodhousef3a0a522009-06-30 03:40:07 +0100893 domain_flush_cache(domain, first_pte,
894 (void *)pte - (void *)first_pte);
895
David Woodhouse59c36282009-09-19 07:36:28 -0700896 } while (tmp && tmp + level_size(level) - 1 <= last_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700897 level++;
898 }
899 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +0100900 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700901 free_pgtable_page(domain->pgd);
902 domain->pgd = NULL;
903 }
904}
905
906/* iommu handling */
907static int iommu_alloc_root_entry(struct intel_iommu *iommu)
908{
909 struct root_entry *root;
910 unsigned long flags;
911
Suresh Siddha4c923d42009-10-02 11:01:24 -0700912 root = (struct root_entry *)alloc_pgtable_page(iommu->node);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700913 if (!root)
914 return -ENOMEM;
915
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700916 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700917
918 spin_lock_irqsave(&iommu->lock, flags);
919 iommu->root_entry = root;
920 spin_unlock_irqrestore(&iommu->lock, flags);
921
922 return 0;
923}
924
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700925static void iommu_set_root_entry(struct intel_iommu *iommu)
926{
927 void *addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100928 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700929 unsigned long flag;
930
931 addr = iommu->root_entry;
932
933 spin_lock_irqsave(&iommu->register_lock, flag);
934 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
935
David Woodhousec416daa2009-05-10 20:30:58 +0100936 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700937
938 /* Make sure hardware complete it */
939 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100940 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700941
942 spin_unlock_irqrestore(&iommu->register_lock, flag);
943}
944
945static void iommu_flush_write_buffer(struct intel_iommu *iommu)
946{
947 u32 val;
948 unsigned long flag;
949
David Woodhouse9af88142009-02-13 23:18:03 +0000950 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700951 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700952
953 spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +0100954 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700955
956 /* Make sure hardware complete it */
957 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100958 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700959
960 spin_unlock_irqrestore(&iommu->register_lock, flag);
961}
962
963/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +0100964static void __iommu_flush_context(struct intel_iommu *iommu,
965 u16 did, u16 source_id, u8 function_mask,
966 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700967{
968 u64 val = 0;
969 unsigned long flag;
970
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700971 switch (type) {
972 case DMA_CCMD_GLOBAL_INVL:
973 val = DMA_CCMD_GLOBAL_INVL;
974 break;
975 case DMA_CCMD_DOMAIN_INVL:
976 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
977 break;
978 case DMA_CCMD_DEVICE_INVL:
979 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
980 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
981 break;
982 default:
983 BUG();
984 }
985 val |= DMA_CCMD_ICC;
986
987 spin_lock_irqsave(&iommu->register_lock, flag);
988 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
989
990 /* Make sure hardware complete it */
991 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
992 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
993
994 spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700995}
996
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700997/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100998static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
999 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001000{
1001 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
1002 u64 val = 0, val_iva = 0;
1003 unsigned long flag;
1004
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001005 switch (type) {
1006 case DMA_TLB_GLOBAL_FLUSH:
1007 /* global flush doesn't need set IVA_REG */
1008 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
1009 break;
1010 case DMA_TLB_DSI_FLUSH:
1011 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1012 break;
1013 case DMA_TLB_PSI_FLUSH:
1014 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
1015 /* Note: always flush non-leaf currently */
1016 val_iva = size_order | addr;
1017 break;
1018 default:
1019 BUG();
1020 }
1021 /* Note: set drain read/write */
1022#if 0
1023 /*
1024 * This is probably to be super secure.. Looks like we can
1025 * ignore it without any impact.
1026 */
1027 if (cap_read_drain(iommu->cap))
1028 val |= DMA_TLB_READ_DRAIN;
1029#endif
1030 if (cap_write_drain(iommu->cap))
1031 val |= DMA_TLB_WRITE_DRAIN;
1032
1033 spin_lock_irqsave(&iommu->register_lock, flag);
1034 /* Note: Only uses first TLB reg currently */
1035 if (val_iva)
1036 dmar_writeq(iommu->reg + tlb_offset, val_iva);
1037 dmar_writeq(iommu->reg + tlb_offset + 8, val);
1038
1039 /* Make sure hardware complete it */
1040 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
1041 dmar_readq, (!(val & DMA_TLB_IVT)), val);
1042
1043 spin_unlock_irqrestore(&iommu->register_lock, flag);
1044
1045 /* check IOTLB invalidation granularity */
1046 if (DMA_TLB_IAIG(val) == 0)
1047 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
1048 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
1049 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001050 (unsigned long long)DMA_TLB_IIRG(type),
1051 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001052}
1053
Yu Zhao93a23a72009-05-18 13:51:37 +08001054static struct device_domain_info *iommu_support_dev_iotlb(
1055 struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001056{
Yu Zhao93a23a72009-05-18 13:51:37 +08001057 int found = 0;
1058 unsigned long flags;
1059 struct device_domain_info *info;
1060 struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);
1061
1062 if (!ecap_dev_iotlb_support(iommu->ecap))
1063 return NULL;
1064
1065 if (!iommu->qi)
1066 return NULL;
1067
1068 spin_lock_irqsave(&device_domain_lock, flags);
1069 list_for_each_entry(info, &domain->devices, link)
1070 if (info->bus == bus && info->devfn == devfn) {
1071 found = 1;
1072 break;
1073 }
1074 spin_unlock_irqrestore(&device_domain_lock, flags);
1075
1076 if (!found || !info->dev)
1077 return NULL;
1078
1079 if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
1080 return NULL;
1081
1082 if (!dmar_find_matched_atsr_unit(info->dev))
1083 return NULL;
1084
1085 info->iommu = iommu;
1086
1087 return info;
1088}
1089
1090static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1091{
1092 if (!info)
1093 return;
1094
1095 pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
1096}
1097
1098static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1099{
1100 if (!info->dev || !pci_ats_enabled(info->dev))
1101 return;
1102
1103 pci_disable_ats(info->dev);
1104}
1105
1106static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1107 u64 addr, unsigned mask)
1108{
1109 u16 sid, qdep;
1110 unsigned long flags;
1111 struct device_domain_info *info;
1112
1113 spin_lock_irqsave(&device_domain_lock, flags);
1114 list_for_each_entry(info, &domain->devices, link) {
1115 if (!info->dev || !pci_ats_enabled(info->dev))
1116 continue;
1117
1118 sid = info->bus << 8 | info->devfn;
1119 qdep = pci_ats_queue_depth(info->dev);
1120 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1121 }
1122 spin_unlock_irqrestore(&device_domain_lock, flags);
1123}
1124
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001125static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
Nadav Amit82653632010-04-01 13:24:40 +03001126 unsigned long pfn, unsigned int pages, int map)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001127{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001128 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001129 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001130
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001131 BUG_ON(pages == 0);
1132
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001133 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001134 * Fallback to domain selective flush if no PSI support or the size is
1135 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001136 * PSI requires page size to be 2 ^ x, and the base address is naturally
1137 * aligned to the size
1138 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001139 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1140 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001141 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001142 else
1143 iommu->flush.flush_iotlb(iommu, did, addr, mask,
1144 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001145
1146 /*
Nadav Amit82653632010-04-01 13:24:40 +03001147 * In caching mode, changes of pages from non-present to present require
1148 * flush. However, device IOTLB doesn't need to be flushed in this case.
Yu Zhaobf92df32009-06-29 11:31:45 +08001149 */
Nadav Amit82653632010-04-01 13:24:40 +03001150 if (!cap_caching_mode(iommu->cap) || !map)
Yu Zhao93a23a72009-05-18 13:51:37 +08001151 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001152}
1153
mark grossf8bab732008-02-08 04:18:38 -08001154static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1155{
1156 u32 pmen;
1157 unsigned long flags;
1158
1159 spin_lock_irqsave(&iommu->register_lock, flags);
1160 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1161 pmen &= ~DMA_PMEN_EPM;
1162 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1163
1164 /* wait for the protected region status bit to clear */
1165 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1166 readl, !(pmen & DMA_PMEN_PRS), pmen);
1167
1168 spin_unlock_irqrestore(&iommu->register_lock, flags);
1169}
1170
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001171static int iommu_enable_translation(struct intel_iommu *iommu)
1172{
1173 u32 sts;
1174 unsigned long flags;
1175
1176 spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001177 iommu->gcmd |= DMA_GCMD_TE;
1178 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001179
1180 /* Make sure hardware complete it */
1181 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001182 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001183
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001184 spin_unlock_irqrestore(&iommu->register_lock, flags);
1185 return 0;
1186}
1187
1188static int iommu_disable_translation(struct intel_iommu *iommu)
1189{
1190 u32 sts;
1191 unsigned long flag;
1192
1193 spin_lock_irqsave(&iommu->register_lock, flag);
1194 iommu->gcmd &= ~DMA_GCMD_TE;
1195 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1196
1197 /* Make sure hardware complete it */
1198 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001199 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001200
1201 spin_unlock_irqrestore(&iommu->register_lock, flag);
1202 return 0;
1203}
1204
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001205
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001206static int iommu_init_domains(struct intel_iommu *iommu)
1207{
1208 unsigned long ndomains;
1209 unsigned long nlongs;
1210
1211 ndomains = cap_ndoms(iommu->cap);
Yinghai Lu680a7522010-04-08 19:58:23 +01001212 pr_debug("IOMMU %d: Number of Domains supportd <%ld>\n", iommu->seq_id,
1213 ndomains);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001214 nlongs = BITS_TO_LONGS(ndomains);
1215
Donald Dutile94a91b52009-08-20 16:51:34 -04001216 spin_lock_init(&iommu->lock);
1217
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001218 /* TBD: there might be 64K domains,
1219 * consider other allocation for future chip
1220 */
1221 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1222 if (!iommu->domain_ids) {
1223 printk(KERN_ERR "Allocating domain id array failed\n");
1224 return -ENOMEM;
1225 }
1226 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1227 GFP_KERNEL);
1228 if (!iommu->domains) {
1229 printk(KERN_ERR "Allocating domain array failed\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001230 return -ENOMEM;
1231 }
1232
1233 /*
1234 * if Caching mode is set, then invalid translations are tagged
1235 * with domainid 0. Hence we need to pre-allocate it.
1236 */
1237 if (cap_caching_mode(iommu->cap))
1238 set_bit(0, iommu->domain_ids);
1239 return 0;
1240}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001241
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001242
1243static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001244static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001245
1246void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001247{
1248 struct dmar_domain *domain;
1249 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001250 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001251
Donald Dutile94a91b52009-08-20 16:51:34 -04001252 if ((iommu->domains) && (iommu->domain_ids)) {
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001253 for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
Donald Dutile94a91b52009-08-20 16:51:34 -04001254 domain = iommu->domains[i];
1255 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001256
Donald Dutile94a91b52009-08-20 16:51:34 -04001257 spin_lock_irqsave(&domain->iommu_lock, flags);
1258 if (--domain->iommu_count == 0) {
1259 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1260 vm_domain_exit(domain);
1261 else
1262 domain_exit(domain);
1263 }
1264 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001265 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001266 }
1267
1268 if (iommu->gcmd & DMA_GCMD_TE)
1269 iommu_disable_translation(iommu);
1270
1271 if (iommu->irq) {
Thomas Gleixnerdced35a2011-03-28 17:49:12 +02001272 irq_set_handler_data(iommu->irq, NULL);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001273 /* This will mask the irq */
1274 free_irq(iommu->irq, iommu);
1275 destroy_irq(iommu->irq);
1276 }
1277
1278 kfree(iommu->domains);
1279 kfree(iommu->domain_ids);
1280
Weidong Hand9630fe2008-12-08 11:06:32 +08001281 g_iommus[iommu->seq_id] = NULL;
1282
1283 /* if all iommus are freed, free g_iommus */
1284 for (i = 0; i < g_num_of_iommus; i++) {
1285 if (g_iommus[i])
1286 break;
1287 }
1288
1289 if (i == g_num_of_iommus)
1290 kfree(g_iommus);
1291
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292 /* free context mapping */
1293 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001294}
1295
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001296static struct dmar_domain *alloc_domain(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001297{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001298 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001299
1300 domain = alloc_domain_mem();
1301 if (!domain)
1302 return NULL;
1303
Suresh Siddha4c923d42009-10-02 11:01:24 -07001304 domain->nid = -1;
Weidong Han8c11e792008-12-08 15:29:22 +08001305 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
Weidong Hand71a2f32008-12-07 21:13:41 +08001306 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001307
1308 return domain;
1309}
1310
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001311static int iommu_attach_domain(struct dmar_domain *domain,
1312 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001313{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001314 int num;
1315 unsigned long ndomains;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001316 unsigned long flags;
1317
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001318 ndomains = cap_ndoms(iommu->cap);
Weidong Han8c11e792008-12-08 15:29:22 +08001319
1320 spin_lock_irqsave(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001321
1322 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1323 if (num >= ndomains) {
1324 spin_unlock_irqrestore(&iommu->lock, flags);
1325 printk(KERN_ERR "IOMMU: no free domain ids\n");
1326 return -ENOMEM;
1327 }
1328
1329 domain->id = num;
1330 set_bit(num, iommu->domain_ids);
1331 set_bit(iommu->seq_id, &domain->iommu_bmp);
1332 iommu->domains[num] = domain;
1333 spin_unlock_irqrestore(&iommu->lock, flags);
1334
1335 return 0;
1336}
1337
1338static void iommu_detach_domain(struct dmar_domain *domain,
1339 struct intel_iommu *iommu)
1340{
1341 unsigned long flags;
1342 int num, ndomains;
1343 int found = 0;
1344
1345 spin_lock_irqsave(&iommu->lock, flags);
1346 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001347 for_each_set_bit(num, iommu->domain_ids, ndomains) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001348 if (iommu->domains[num] == domain) {
1349 found = 1;
1350 break;
1351 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001352 }
1353
1354 if (found) {
1355 clear_bit(num, iommu->domain_ids);
1356 clear_bit(iommu->seq_id, &domain->iommu_bmp);
1357 iommu->domains[num] = NULL;
1358 }
Weidong Han8c11e792008-12-08 15:29:22 +08001359 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001360}
1361
1362static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001363static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001364
Joseph Cihula51a63e62011-03-21 11:04:24 -07001365static int dmar_init_reserved_ranges(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001366{
1367 struct pci_dev *pdev = NULL;
1368 struct iova *iova;
1369 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001370
David Millerf6611972008-02-06 01:36:23 -08001371 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001372
Mark Gross8a443df2008-03-04 14:59:31 -08001373 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1374 &reserved_rbtree_key);
1375
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001376 /* IOAPIC ranges shouldn't be accessed by DMA */
1377 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1378 IOVA_PFN(IOAPIC_RANGE_END));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001379 if (!iova) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001380 printk(KERN_ERR "Reserve IOAPIC range failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001381 return -ENODEV;
1382 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001383
1384 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1385 for_each_pci_dev(pdev) {
1386 struct resource *r;
1387
1388 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1389 r = &pdev->resource[i];
1390 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1391 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001392 iova = reserve_iova(&reserved_iova_list,
1393 IOVA_PFN(r->start),
1394 IOVA_PFN(r->end));
Joseph Cihula51a63e62011-03-21 11:04:24 -07001395 if (!iova) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001396 printk(KERN_ERR "Reserve iova failed\n");
Joseph Cihula51a63e62011-03-21 11:04:24 -07001397 return -ENODEV;
1398 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001399 }
1400 }
Joseph Cihula51a63e62011-03-21 11:04:24 -07001401 return 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001402}
1403
1404static void domain_reserve_special_ranges(struct dmar_domain *domain)
1405{
1406 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1407}
1408
1409static inline int guestwidth_to_adjustwidth(int gaw)
1410{
1411 int agaw;
1412 int r = (gaw - 12) % 9;
1413
1414 if (r == 0)
1415 agaw = gaw;
1416 else
1417 agaw = gaw + 9 - r;
1418 if (agaw > 64)
1419 agaw = 64;
1420 return agaw;
1421}
1422
1423static int domain_init(struct dmar_domain *domain, int guest_width)
1424{
1425 struct intel_iommu *iommu;
1426 int adjust_width, agaw;
1427 unsigned long sagaw;
1428
David Millerf6611972008-02-06 01:36:23 -08001429 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Weidong Hanc7151a82008-12-08 22:51:37 +08001430 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001431
1432 domain_reserve_special_ranges(domain);
1433
1434 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001435 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001436 if (guest_width > cap_mgaw(iommu->cap))
1437 guest_width = cap_mgaw(iommu->cap);
1438 domain->gaw = guest_width;
1439 adjust_width = guestwidth_to_adjustwidth(guest_width);
1440 agaw = width_to_agaw(adjust_width);
1441 sagaw = cap_sagaw(iommu->cap);
1442 if (!test_bit(agaw, &sagaw)) {
1443 /* hardware doesn't support it, choose a bigger one */
1444 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1445 agaw = find_next_bit(&sagaw, 5, agaw);
1446 if (agaw >= 5)
1447 return -ENODEV;
1448 }
1449 domain->agaw = agaw;
1450 INIT_LIST_HEAD(&domain->devices);
1451
Weidong Han8e6040972008-12-08 15:49:06 +08001452 if (ecap_coherent(iommu->ecap))
1453 domain->iommu_coherency = 1;
1454 else
1455 domain->iommu_coherency = 0;
1456
Sheng Yang58c610b2009-03-18 15:33:05 +08001457 if (ecap_sc_support(iommu->ecap))
1458 domain->iommu_snooping = 1;
1459 else
1460 domain->iommu_snooping = 0;
1461
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001462 domain->iommu_superpage = fls(cap_super_page_val(iommu->cap));
Weidong Hanc7151a82008-12-08 22:51:37 +08001463 domain->iommu_count = 1;
Suresh Siddha4c923d42009-10-02 11:01:24 -07001464 domain->nid = iommu->node;
Weidong Hanc7151a82008-12-08 22:51:37 +08001465
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001466 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07001467 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001468 if (!domain->pgd)
1469 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001470 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001471 return 0;
1472}
1473
1474static void domain_exit(struct dmar_domain *domain)
1475{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001476 struct dmar_drhd_unit *drhd;
1477 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001478
1479 /* Domain 0 is reserved, so dont process it */
1480 if (!domain)
1481 return;
1482
Alex Williamson7b668352011-05-24 12:02:41 +01001483 /* Flush any lazy unmaps that may reference this domain */
1484 if (!intel_iommu_strict)
1485 flush_unmaps_timeout(0);
1486
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001487 domain_remove_dev_info(domain);
1488 /* destroy iovas */
1489 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001490
1491 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01001492 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001493
1494 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01001495 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001496
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001497 for_each_active_iommu(iommu, drhd)
1498 if (test_bit(iommu->seq_id, &domain->iommu_bmp))
1499 iommu_detach_domain(domain, iommu);
1500
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001501 free_domain_mem(domain);
1502}
1503
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001504static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1505 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001506{
1507 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001508 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001509 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001510 struct dma_pte *pgd;
1511 unsigned long num;
1512 unsigned long ndomains;
1513 int id;
1514 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001515 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001516
1517 pr_debug("Set context mapping for %02x:%02x.%d\n",
1518 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001519
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001520 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001521 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1522 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001523
David Woodhouse276dbf992009-04-04 01:45:37 +01001524 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001525 if (!iommu)
1526 return -ENODEV;
1527
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001528 context = device_to_context_entry(iommu, bus, devfn);
1529 if (!context)
1530 return -ENOMEM;
1531 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001532 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001533 spin_unlock_irqrestore(&iommu->lock, flags);
1534 return 0;
1535 }
1536
Weidong Hanea6606b2008-12-08 23:08:15 +08001537 id = domain->id;
1538 pgd = domain->pgd;
1539
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001540 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
1541 domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001542 int found = 0;
1543
1544 /* find an available domain id for this device in iommu */
1545 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08001546 for_each_set_bit(num, iommu->domain_ids, ndomains) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001547 if (iommu->domains[num] == domain) {
1548 id = num;
1549 found = 1;
1550 break;
1551 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001552 }
1553
1554 if (found == 0) {
1555 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1556 if (num >= ndomains) {
1557 spin_unlock_irqrestore(&iommu->lock, flags);
1558 printk(KERN_ERR "IOMMU: no free domain ids\n");
1559 return -EFAULT;
1560 }
1561
1562 set_bit(num, iommu->domain_ids);
1563 iommu->domains[num] = domain;
1564 id = num;
1565 }
1566
1567 /* Skip top levels of page tables for
1568 * iommu which has less agaw than default.
Chris Wright1672af12009-12-02 12:06:34 -08001569 * Unnecessary for PT mode.
Weidong Hanea6606b2008-12-08 23:08:15 +08001570 */
Chris Wright1672af12009-12-02 12:06:34 -08001571 if (translation != CONTEXT_TT_PASS_THROUGH) {
1572 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1573 pgd = phys_to_virt(dma_pte_addr(pgd));
1574 if (!dma_pte_present(pgd)) {
1575 spin_unlock_irqrestore(&iommu->lock, flags);
1576 return -ENOMEM;
1577 }
Weidong Hanea6606b2008-12-08 23:08:15 +08001578 }
1579 }
1580 }
1581
1582 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001583
Yu Zhao93a23a72009-05-18 13:51:37 +08001584 if (translation != CONTEXT_TT_PASS_THROUGH) {
1585 info = iommu_support_dev_iotlb(domain, segment, bus, devfn);
1586 translation = info ? CONTEXT_TT_DEV_IOTLB :
1587 CONTEXT_TT_MULTI_LEVEL;
1588 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001589 /*
1590 * In pass through mode, AW must be programmed to indicate the largest
1591 * AGAW value supported by hardware. And ASR is ignored by hardware.
1592 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001593 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001594 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001595 else {
1596 context_set_address_root(context, virt_to_phys(pgd));
1597 context_set_address_width(context, iommu->agaw);
1598 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001599
1600 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001601 context_set_fault_enable(context);
1602 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001603 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001604
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001605 /*
1606 * It's a non-present to present mapping. If hardware doesn't cache
1607 * non-present entry we only need to flush the write-buffer. If the
1608 * _does_ cache non-present entries, then it does so in the special
1609 * domain #0, which we have to flush:
1610 */
1611 if (cap_caching_mode(iommu->cap)) {
1612 iommu->flush.flush_context(iommu, 0,
1613 (((u16)bus) << 8) | devfn,
1614 DMA_CCMD_MASK_NOBIT,
1615 DMA_CCMD_DEVICE_INVL);
Nadav Amit82653632010-04-01 13:24:40 +03001616 iommu->flush.flush_iotlb(iommu, domain->id, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001617 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001618 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001619 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001620 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001621 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001622
1623 spin_lock_irqsave(&domain->iommu_lock, flags);
1624 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1625 domain->iommu_count++;
Suresh Siddha4c923d42009-10-02 11:01:24 -07001626 if (domain->iommu_count == 1)
1627 domain->nid = iommu->node;
Sheng Yang58c610b2009-03-18 15:33:05 +08001628 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001629 }
1630 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001631 return 0;
1632}
1633
1634static int
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001635domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1636 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001637{
1638 int ret;
1639 struct pci_dev *tmp, *parent;
1640
David Woodhouse276dbf992009-04-04 01:45:37 +01001641 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001642 pdev->bus->number, pdev->devfn,
1643 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001644 if (ret)
1645 return ret;
1646
1647 /* dependent device mapping */
1648 tmp = pci_find_upstream_pcie_bridge(pdev);
1649 if (!tmp)
1650 return 0;
1651 /* Secondary interface's bus number and devfn 0 */
1652 parent = pdev->bus->self;
1653 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001654 ret = domain_context_mapping_one(domain,
1655 pci_domain_nr(parent->bus),
1656 parent->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001657 parent->devfn, translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001658 if (ret)
1659 return ret;
1660 parent = parent->bus->self;
1661 }
Stefan Assmann45e829e2009-12-03 06:49:24 -05001662 if (pci_is_pcie(tmp)) /* this is a PCIe-to-PCI bridge */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001663 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001664 pci_domain_nr(tmp->subordinate),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001665 tmp->subordinate->number, 0,
1666 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001667 else /* this is a legacy PCI bridge */
1668 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001669 pci_domain_nr(tmp->bus),
1670 tmp->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001671 tmp->devfn,
1672 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001673}
1674
Weidong Han5331fe62008-12-08 23:00:00 +08001675static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001676{
1677 int ret;
1678 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001679 struct intel_iommu *iommu;
1680
David Woodhouse276dbf992009-04-04 01:45:37 +01001681 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1682 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001683 if (!iommu)
1684 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001685
David Woodhouse276dbf992009-04-04 01:45:37 +01001686 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001687 if (!ret)
1688 return ret;
1689 /* dependent device mapping */
1690 tmp = pci_find_upstream_pcie_bridge(pdev);
1691 if (!tmp)
1692 return ret;
1693 /* Secondary interface's bus number and devfn 0 */
1694 parent = pdev->bus->self;
1695 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001696 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001697 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001698 if (!ret)
1699 return ret;
1700 parent = parent->bus->self;
1701 }
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09001702 if (pci_is_pcie(tmp))
David Woodhouse276dbf992009-04-04 01:45:37 +01001703 return device_context_mapped(iommu, tmp->subordinate->number,
1704 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001705 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001706 return device_context_mapped(iommu, tmp->bus->number,
1707 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001708}
1709
Fenghua Yuf5329592009-08-04 15:09:37 -07001710/* Returns a number of VTD pages, but aligned to MM page size */
1711static inline unsigned long aligned_nrpages(unsigned long host_addr,
1712 size_t size)
1713{
1714 host_addr &= ~PAGE_MASK;
1715 return PAGE_ALIGN(host_addr + size) >> VTD_PAGE_SHIFT;
1716}
1717
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001718/* Return largest possible superpage level for a given mapping */
1719static inline int hardware_largepage_caps(struct dmar_domain *domain,
1720 unsigned long iov_pfn,
1721 unsigned long phy_pfn,
1722 unsigned long pages)
1723{
1724 int support, level = 1;
1725 unsigned long pfnmerge;
1726
1727 support = domain->iommu_superpage;
1728
1729 /* To use a large page, the virtual *and* physical addresses
1730 must be aligned to 2MiB/1GiB/etc. Lower bits set in either
1731 of them will mean we have to use smaller pages. So just
1732 merge them and check both at once. */
1733 pfnmerge = iov_pfn | phy_pfn;
1734
1735 while (support && !(pfnmerge & ~VTD_STRIDE_MASK)) {
1736 pages >>= VTD_STRIDE_SHIFT;
1737 if (!pages)
1738 break;
1739 pfnmerge >>= VTD_STRIDE_SHIFT;
1740 level++;
1741 support--;
1742 }
1743 return level;
1744}
1745
David Woodhouse9051aa02009-06-29 12:30:54 +01001746static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1747 struct scatterlist *sg, unsigned long phys_pfn,
1748 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01001749{
1750 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01001751 phys_addr_t uninitialized_var(pteval);
David Woodhousee1605492009-06-29 11:17:38 +01001752 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhouse9051aa02009-06-29 12:30:54 +01001753 unsigned long sg_res;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001754 unsigned int largepage_lvl = 0;
1755 unsigned long lvl_pages = 0;
David Woodhousee1605492009-06-29 11:17:38 +01001756
1757 BUG_ON(addr_width < BITS_PER_LONG && (iov_pfn + nr_pages - 1) >> addr_width);
1758
1759 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1760 return -EINVAL;
1761
1762 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
1763
David Woodhouse9051aa02009-06-29 12:30:54 +01001764 if (sg)
1765 sg_res = 0;
1766 else {
1767 sg_res = nr_pages + 1;
1768 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
1769 }
1770
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001771 while (nr_pages > 0) {
David Woodhousec85994e2009-07-01 19:21:24 +01001772 uint64_t tmp;
1773
David Woodhousee1605492009-06-29 11:17:38 +01001774 if (!sg_res) {
Fenghua Yuf5329592009-08-04 15:09:37 -07001775 sg_res = aligned_nrpages(sg->offset, sg->length);
David Woodhousee1605492009-06-29 11:17:38 +01001776 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
1777 sg->dma_length = sg->length;
1778 pteval = page_to_phys(sg_page(sg)) | prot;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001779 phys_pfn = pteval >> VTD_PAGE_SHIFT;
David Woodhousee1605492009-06-29 11:17:38 +01001780 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001781
David Woodhousee1605492009-06-29 11:17:38 +01001782 if (!pte) {
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001783 largepage_lvl = hardware_largepage_caps(domain, iov_pfn, phys_pfn, sg_res);
1784
1785 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn, largepage_lvl);
David Woodhousee1605492009-06-29 11:17:38 +01001786 if (!pte)
1787 return -ENOMEM;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001788 /* It is large page*/
1789 if (largepage_lvl > 1)
1790 pteval |= DMA_PTE_LARGE_PAGE;
1791 else
1792 pteval &= ~(uint64_t)DMA_PTE_LARGE_PAGE;
1793
David Woodhousee1605492009-06-29 11:17:38 +01001794 }
1795 /* We don't need lock here, nobody else
1796 * touches the iova range
1797 */
David Woodhouse7766a3f2009-07-01 20:27:03 +01001798 tmp = cmpxchg64_local(&pte->val, 0ULL, pteval);
David Woodhousec85994e2009-07-01 19:21:24 +01001799 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01001800 static int dumps = 5;
David Woodhousec85994e2009-07-01 19:21:24 +01001801 printk(KERN_CRIT "ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
1802 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01001803 if (dumps) {
1804 dumps--;
1805 debug_dma_dump_mappings(NULL);
1806 }
1807 WARN_ON(1);
1808 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001809
1810 lvl_pages = lvl_to_nr_pages(largepage_lvl);
1811
1812 BUG_ON(nr_pages < lvl_pages);
1813 BUG_ON(sg_res < lvl_pages);
1814
1815 nr_pages -= lvl_pages;
1816 iov_pfn += lvl_pages;
1817 phys_pfn += lvl_pages;
1818 pteval += lvl_pages * VTD_PAGE_SIZE;
1819 sg_res -= lvl_pages;
1820
1821 /* If the next PTE would be the first in a new page, then we
1822 need to flush the cache on the entries we've just written.
1823 And then we'll need to recalculate 'pte', so clear it and
1824 let it get set again in the if (!pte) block above.
1825
1826 If we're done (!nr_pages) we need to flush the cache too.
1827
1828 Also if we've been setting superpages, we may need to
1829 recalculate 'pte' and switch back to smaller pages for the
1830 end of the mapping, if the trailing size is not enough to
1831 use another superpage (i.e. sg_res < lvl_pages). */
David Woodhousee1605492009-06-29 11:17:38 +01001832 pte++;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001833 if (!nr_pages || first_pte_in_page(pte) ||
1834 (largepage_lvl > 1 && sg_res < lvl_pages)) {
David Woodhousee1605492009-06-29 11:17:38 +01001835 domain_flush_cache(domain, first_pte,
1836 (void *)pte - (void *)first_pte);
1837 pte = NULL;
1838 }
Youquan Song6dd9a7c2011-05-25 19:13:49 +01001839
1840 if (!sg_res && nr_pages)
David Woodhousee1605492009-06-29 11:17:38 +01001841 sg = sg_next(sg);
1842 }
1843 return 0;
1844}
1845
David Woodhouse9051aa02009-06-29 12:30:54 +01001846static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1847 struct scatterlist *sg, unsigned long nr_pages,
1848 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001849{
David Woodhouse9051aa02009-06-29 12:30:54 +01001850 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
1851}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001852
David Woodhouse9051aa02009-06-29 12:30:54 +01001853static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1854 unsigned long phys_pfn, unsigned long nr_pages,
1855 int prot)
1856{
1857 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001858}
1859
Weidong Hanc7151a82008-12-08 22:51:37 +08001860static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001861{
Weidong Hanc7151a82008-12-08 22:51:37 +08001862 if (!iommu)
1863 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001864
1865 clear_context_table(iommu, bus, devfn);
1866 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001867 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001868 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001869}
1870
1871static void domain_remove_dev_info(struct dmar_domain *domain)
1872{
1873 struct device_domain_info *info;
1874 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001875 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001876
1877 spin_lock_irqsave(&device_domain_lock, flags);
1878 while (!list_empty(&domain->devices)) {
1879 info = list_entry(domain->devices.next,
1880 struct device_domain_info, link);
1881 list_del(&info->link);
1882 list_del(&info->global);
1883 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001884 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001885 spin_unlock_irqrestore(&device_domain_lock, flags);
1886
Yu Zhao93a23a72009-05-18 13:51:37 +08001887 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01001888 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001889 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001890 free_devinfo_mem(info);
1891
1892 spin_lock_irqsave(&device_domain_lock, flags);
1893 }
1894 spin_unlock_irqrestore(&device_domain_lock, flags);
1895}
1896
1897/*
1898 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001899 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001900 */
Kay, Allen M38717942008-09-09 18:37:29 +03001901static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001902find_domain(struct pci_dev *pdev)
1903{
1904 struct device_domain_info *info;
1905
1906 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001907 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001908 if (info)
1909 return info->domain;
1910 return NULL;
1911}
1912
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001913/* domain is initialized */
1914static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1915{
1916 struct dmar_domain *domain, *found = NULL;
1917 struct intel_iommu *iommu;
1918 struct dmar_drhd_unit *drhd;
1919 struct device_domain_info *info, *tmp;
1920 struct pci_dev *dev_tmp;
1921 unsigned long flags;
1922 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001923 int segment;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001924 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001925
1926 domain = find_domain(pdev);
1927 if (domain)
1928 return domain;
1929
David Woodhouse276dbf992009-04-04 01:45:37 +01001930 segment = pci_domain_nr(pdev->bus);
1931
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001932 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1933 if (dev_tmp) {
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09001934 if (pci_is_pcie(dev_tmp)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001935 bus = dev_tmp->subordinate->number;
1936 devfn = 0;
1937 } else {
1938 bus = dev_tmp->bus->number;
1939 devfn = dev_tmp->devfn;
1940 }
1941 spin_lock_irqsave(&device_domain_lock, flags);
1942 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001943 if (info->segment == segment &&
1944 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001945 found = info->domain;
1946 break;
1947 }
1948 }
1949 spin_unlock_irqrestore(&device_domain_lock, flags);
1950 /* pcie-pci bridge already has a domain, uses it */
1951 if (found) {
1952 domain = found;
1953 goto found_domain;
1954 }
1955 }
1956
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001957 domain = alloc_domain();
1958 if (!domain)
1959 goto error;
1960
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001961 /* Allocate new domain for the device */
1962 drhd = dmar_find_matched_drhd_unit(pdev);
1963 if (!drhd) {
1964 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1965 pci_name(pdev));
1966 return NULL;
1967 }
1968 iommu = drhd->iommu;
1969
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001970 ret = iommu_attach_domain(domain, iommu);
1971 if (ret) {
Alex Williamson2fe9723d2011-03-04 14:52:30 -07001972 free_domain_mem(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001973 goto error;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001974 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001975
1976 if (domain_init(domain, gaw)) {
1977 domain_exit(domain);
1978 goto error;
1979 }
1980
1981 /* register pcie-to-pci device */
1982 if (dev_tmp) {
1983 info = alloc_devinfo_mem();
1984 if (!info) {
1985 domain_exit(domain);
1986 goto error;
1987 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001988 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001989 info->bus = bus;
1990 info->devfn = devfn;
1991 info->dev = NULL;
1992 info->domain = domain;
1993 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001994 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001995
1996 /* pcie-to-pci bridge already has a domain, uses it */
1997 found = NULL;
1998 spin_lock_irqsave(&device_domain_lock, flags);
1999 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01002000 if (tmp->segment == segment &&
2001 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002002 found = tmp->domain;
2003 break;
2004 }
2005 }
2006 if (found) {
Jiri Slaby00dfff72010-06-14 17:17:32 +02002007 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002008 free_devinfo_mem(info);
2009 domain_exit(domain);
2010 domain = found;
2011 } else {
2012 list_add(&info->link, &domain->devices);
2013 list_add(&info->global, &device_domain_list);
Jiri Slaby00dfff72010-06-14 17:17:32 +02002014 spin_unlock_irqrestore(&device_domain_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002015 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002016 }
2017
2018found_domain:
2019 info = alloc_devinfo_mem();
2020 if (!info)
2021 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01002022 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002023 info->bus = pdev->bus->number;
2024 info->devfn = pdev->devfn;
2025 info->dev = pdev;
2026 info->domain = domain;
2027 spin_lock_irqsave(&device_domain_lock, flags);
2028 /* somebody is fast */
2029 found = find_domain(pdev);
2030 if (found != NULL) {
2031 spin_unlock_irqrestore(&device_domain_lock, flags);
2032 if (found != domain) {
2033 domain_exit(domain);
2034 domain = found;
2035 }
2036 free_devinfo_mem(info);
2037 return domain;
2038 }
2039 list_add(&info->link, &domain->devices);
2040 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002041 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002042 spin_unlock_irqrestore(&device_domain_lock, flags);
2043 return domain;
2044error:
2045 /* recheck it here, maybe others set it */
2046 return find_domain(pdev);
2047}
2048
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002049static int iommu_identity_mapping;
David Woodhousee0fc7e02009-09-30 09:12:17 -07002050#define IDENTMAP_ALL 1
2051#define IDENTMAP_GFX 2
2052#define IDENTMAP_AZALIA 4
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002053
David Woodhouseb2132032009-06-26 18:50:28 +01002054static int iommu_domain_identity_map(struct dmar_domain *domain,
2055 unsigned long long start,
2056 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002057{
David Woodhousec5395d52009-06-28 16:35:56 +01002058 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
2059 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002060
David Woodhousec5395d52009-06-28 16:35:56 +01002061 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
2062 dma_to_mm_pfn(last_vpfn))) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002063 printk(KERN_ERR "IOMMU: reserve iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01002064 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002065 }
2066
David Woodhousec5395d52009-06-28 16:35:56 +01002067 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
2068 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002069 /*
2070 * RMRR range might have overlap with physical memory range,
2071 * clear it first
2072 */
David Woodhousec5395d52009-06-28 16:35:56 +01002073 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002074
David Woodhousec5395d52009-06-28 16:35:56 +01002075 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
2076 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01002077 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01002078}
2079
2080static int iommu_prepare_identity_map(struct pci_dev *pdev,
2081 unsigned long long start,
2082 unsigned long long end)
2083{
2084 struct dmar_domain *domain;
2085 int ret;
2086
David Woodhousec7ab48d2009-06-26 19:10:36 +01002087 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01002088 if (!domain)
2089 return -ENOMEM;
2090
David Woodhouse19943b02009-08-04 16:19:20 +01002091 /* For _hardware_ passthrough, don't bother. But for software
2092 passthrough, we do it anyway -- it may indicate a memory
2093 range which is reserved in E820, so which didn't get set
2094 up to start with in si_domain */
2095 if (domain == si_domain && hw_pass_through) {
2096 printk("Ignoring identity map for HW passthrough device %s [0x%Lx - 0x%Lx]\n",
2097 pci_name(pdev), start, end);
2098 return 0;
2099 }
2100
2101 printk(KERN_INFO
2102 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
2103 pci_name(pdev), start, end);
David Woodhouse2ff729f2009-08-26 14:25:41 +01002104
David Woodhouse5595b522009-12-02 09:21:55 +00002105 if (end < start) {
2106 WARN(1, "Your BIOS is broken; RMRR ends before it starts!\n"
2107 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2108 dmi_get_system_info(DMI_BIOS_VENDOR),
2109 dmi_get_system_info(DMI_BIOS_VERSION),
2110 dmi_get_system_info(DMI_PRODUCT_VERSION));
2111 ret = -EIO;
2112 goto error;
2113 }
2114
David Woodhouse2ff729f2009-08-26 14:25:41 +01002115 if (end >> agaw_to_width(domain->agaw)) {
2116 WARN(1, "Your BIOS is broken; RMRR exceeds permitted address width (%d bits)\n"
2117 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
2118 agaw_to_width(domain->agaw),
2119 dmi_get_system_info(DMI_BIOS_VENDOR),
2120 dmi_get_system_info(DMI_BIOS_VERSION),
2121 dmi_get_system_info(DMI_PRODUCT_VERSION));
2122 ret = -EIO;
2123 goto error;
2124 }
David Woodhouse19943b02009-08-04 16:19:20 +01002125
David Woodhouseb2132032009-06-26 18:50:28 +01002126 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002127 if (ret)
2128 goto error;
2129
2130 /* context entry init */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002131 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01002132 if (ret)
2133 goto error;
2134
2135 return 0;
2136
2137 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002138 domain_exit(domain);
2139 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002140}
2141
2142static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
2143 struct pci_dev *pdev)
2144{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002145 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002146 return 0;
2147 return iommu_prepare_identity_map(pdev, rmrr->base_address,
2148 rmrr->end_address + 1);
2149}
2150
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002151#ifdef CONFIG_DMAR_FLOPPY_WA
2152static inline void iommu_prepare_isa(void)
2153{
2154 struct pci_dev *pdev;
2155 int ret;
2156
2157 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
2158 if (!pdev)
2159 return;
2160
David Woodhousec7ab48d2009-06-26 19:10:36 +01002161 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002162 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
2163
2164 if (ret)
David Woodhousec7ab48d2009-06-26 19:10:36 +01002165 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
2166 "floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002167
2168}
2169#else
2170static inline void iommu_prepare_isa(void)
2171{
2172 return;
2173}
2174#endif /* !CONFIG_DMAR_FLPY_WA */
2175
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002176static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002177
2178static int __init si_domain_work_fn(unsigned long start_pfn,
2179 unsigned long end_pfn, void *datax)
2180{
2181 int *ret = datax;
2182
2183 *ret = iommu_domain_identity_map(si_domain,
2184 (uint64_t)start_pfn << PAGE_SHIFT,
2185 (uint64_t)end_pfn << PAGE_SHIFT);
2186 return *ret;
2187
2188}
2189
Matt Kraai071e1372009-08-23 22:30:22 -07002190static int __init si_domain_init(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002191{
2192 struct dmar_drhd_unit *drhd;
2193 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01002194 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002195
2196 si_domain = alloc_domain();
2197 if (!si_domain)
2198 return -EFAULT;
2199
David Woodhousec7ab48d2009-06-26 19:10:36 +01002200 pr_debug("Identity mapping domain is domain %d\n", si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002201
2202 for_each_active_iommu(iommu, drhd) {
2203 ret = iommu_attach_domain(si_domain, iommu);
2204 if (ret) {
2205 domain_exit(si_domain);
2206 return -EFAULT;
2207 }
2208 }
2209
2210 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2211 domain_exit(si_domain);
2212 return -EFAULT;
2213 }
2214
2215 si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY;
2216
David Woodhouse19943b02009-08-04 16:19:20 +01002217 if (hw)
2218 return 0;
2219
David Woodhousec7ab48d2009-06-26 19:10:36 +01002220 for_each_online_node(nid) {
2221 work_with_active_regions(nid, si_domain_work_fn, &ret);
2222 if (ret)
2223 return ret;
2224 }
2225
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002226 return 0;
2227}
2228
2229static void domain_remove_one_dev_info(struct dmar_domain *domain,
2230 struct pci_dev *pdev);
2231static int identity_mapping(struct pci_dev *pdev)
2232{
2233 struct device_domain_info *info;
2234
2235 if (likely(!iommu_identity_mapping))
2236 return 0;
2237
Mike Traviscb452a42011-05-28 13:15:03 -05002238 info = pdev->dev.archdata.iommu;
2239 if (info && info != DUMMY_DEVICE_DOMAIN_INFO)
2240 return (info->domain == si_domain);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002241
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002242 return 0;
2243}
2244
2245static int domain_add_dev_info(struct dmar_domain *domain,
David Woodhouse5fe60f42009-08-09 10:53:41 +01002246 struct pci_dev *pdev,
2247 int translation)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002248{
2249 struct device_domain_info *info;
2250 unsigned long flags;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002251 int ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002252
2253 info = alloc_devinfo_mem();
2254 if (!info)
2255 return -ENOMEM;
2256
David Woodhouse5fe60f42009-08-09 10:53:41 +01002257 ret = domain_context_mapping(domain, pdev, translation);
2258 if (ret) {
2259 free_devinfo_mem(info);
2260 return ret;
2261 }
2262
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002263 info->segment = pci_domain_nr(pdev->bus);
2264 info->bus = pdev->bus->number;
2265 info->devfn = pdev->devfn;
2266 info->dev = pdev;
2267 info->domain = domain;
2268
2269 spin_lock_irqsave(&device_domain_lock, flags);
2270 list_add(&info->link, &domain->devices);
2271 list_add(&info->global, &device_domain_list);
2272 pdev->dev.archdata.iommu = info;
2273 spin_unlock_irqrestore(&device_domain_lock, flags);
2274
2275 return 0;
2276}
2277
David Woodhouse6941af22009-07-04 18:24:27 +01002278static int iommu_should_identity_map(struct pci_dev *pdev, int startup)
2279{
David Woodhousee0fc7e02009-09-30 09:12:17 -07002280 if ((iommu_identity_mapping & IDENTMAP_AZALIA) && IS_AZALIA(pdev))
2281 return 1;
2282
2283 if ((iommu_identity_mapping & IDENTMAP_GFX) && IS_GFX_DEVICE(pdev))
2284 return 1;
2285
2286 if (!(iommu_identity_mapping & IDENTMAP_ALL))
2287 return 0;
David Woodhouse6941af22009-07-04 18:24:27 +01002288
David Woodhouse3dfc8132009-07-04 19:11:08 +01002289 /*
2290 * We want to start off with all devices in the 1:1 domain, and
2291 * take them out later if we find they can't access all of memory.
2292 *
2293 * However, we can't do this for PCI devices behind bridges,
2294 * because all PCI devices behind the same bridge will end up
2295 * with the same source-id on their transactions.
2296 *
2297 * Practically speaking, we can't change things around for these
2298 * devices at run-time, because we can't be sure there'll be no
2299 * DMA transactions in flight for any of their siblings.
2300 *
2301 * So PCI devices (unless they're on the root bus) as well as
2302 * their parent PCI-PCI or PCIe-PCI bridges must be left _out_ of
2303 * the 1:1 domain, just in _case_ one of their siblings turns out
2304 * not to be able to map all of memory.
2305 */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +09002306 if (!pci_is_pcie(pdev)) {
David Woodhouse3dfc8132009-07-04 19:11:08 +01002307 if (!pci_is_root_bus(pdev->bus))
2308 return 0;
2309 if (pdev->class >> 8 == PCI_CLASS_BRIDGE_PCI)
2310 return 0;
2311 } else if (pdev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
2312 return 0;
2313
2314 /*
2315 * At boot time, we don't yet know if devices will be 64-bit capable.
2316 * Assume that they will -- if they turn out not to be, then we can
2317 * take them out of the 1:1 domain later.
2318 */
Chris Wright8fcc5372011-05-28 13:15:02 -05002319 if (!startup) {
2320 /*
2321 * If the device's dma_mask is less than the system's memory
2322 * size then this is not a candidate for identity mapping.
2323 */
2324 u64 dma_mask = pdev->dma_mask;
2325
2326 if (pdev->dev.coherent_dma_mask &&
2327 pdev->dev.coherent_dma_mask < dma_mask)
2328 dma_mask = pdev->dev.coherent_dma_mask;
2329
2330 return dma_mask >= dma_get_required_mask(&pdev->dev);
2331 }
David Woodhouse6941af22009-07-04 18:24:27 +01002332
2333 return 1;
2334}
2335
Matt Kraai071e1372009-08-23 22:30:22 -07002336static int __init iommu_prepare_static_identity_mapping(int hw)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002337{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002338 struct pci_dev *pdev = NULL;
2339 int ret;
2340
David Woodhouse19943b02009-08-04 16:19:20 +01002341 ret = si_domain_init(hw);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002342 if (ret)
2343 return -EFAULT;
2344
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002345 for_each_pci_dev(pdev) {
David Woodhouse6941af22009-07-04 18:24:27 +01002346 if (iommu_should_identity_map(pdev, 1)) {
David Woodhouse19943b02009-08-04 16:19:20 +01002347 printk(KERN_INFO "IOMMU: %s identity mapping for device %s\n",
2348 hw ? "hardware" : "software", pci_name(pdev));
David Woodhousec7ab48d2009-06-26 19:10:36 +01002349
David Woodhouse5fe60f42009-08-09 10:53:41 +01002350 ret = domain_add_dev_info(si_domain, pdev,
David Woodhouse19943b02009-08-04 16:19:20 +01002351 hw ? CONTEXT_TT_PASS_THROUGH :
David Woodhouse62edf5d2009-07-04 10:59:46 +01002352 CONTEXT_TT_MULTI_LEVEL);
2353 if (ret)
2354 return ret;
David Woodhouse62edf5d2009-07-04 10:59:46 +01002355 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002356 }
2357
2358 return 0;
2359}
2360
Joseph Cihulab7792602011-05-03 00:08:37 -07002361static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002362{
2363 struct dmar_drhd_unit *drhd;
2364 struct dmar_rmrr_unit *rmrr;
2365 struct pci_dev *pdev;
2366 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002367 int i, ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002368
2369 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002370 * for each drhd
2371 * allocate root
2372 * initialize and program root entry to not present
2373 * endfor
2374 */
2375 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002376 g_num_of_iommus++;
2377 /*
2378 * lock not needed as this is only incremented in the single
2379 * threaded kernel __init code path all other access are read
2380 * only
2381 */
2382 }
2383
Weidong Hand9630fe2008-12-08 11:06:32 +08002384 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2385 GFP_KERNEL);
2386 if (!g_iommus) {
2387 printk(KERN_ERR "Allocating global iommu array failed\n");
2388 ret = -ENOMEM;
2389 goto error;
2390 }
2391
mark gross80b20dd2008-04-18 13:53:58 -07002392 deferred_flush = kzalloc(g_num_of_iommus *
2393 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2394 if (!deferred_flush) {
mark gross5e0d2a62008-03-04 15:22:08 -08002395 ret = -ENOMEM;
2396 goto error;
2397 }
2398
mark gross5e0d2a62008-03-04 15:22:08 -08002399 for_each_drhd_unit(drhd) {
2400 if (drhd->ignored)
2401 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002402
2403 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08002404 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002405
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002406 ret = iommu_init_domains(iommu);
2407 if (ret)
2408 goto error;
2409
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002410 /*
2411 * TBD:
2412 * we could share the same root & context tables
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002413 * among all IOMMU's. Need to Split it later.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002414 */
2415 ret = iommu_alloc_root_entry(iommu);
2416 if (ret) {
2417 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
2418 goto error;
2419 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002420 if (!ecap_pass_through(iommu->ecap))
David Woodhouse19943b02009-08-04 16:19:20 +01002421 hw_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002422 }
2423
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002424 /*
2425 * Start from the sane iommu hardware state.
2426 */
Youquan Songa77b67d2008-10-16 16:31:56 -07002427 for_each_drhd_unit(drhd) {
2428 if (drhd->ignored)
2429 continue;
2430
2431 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002432
2433 /*
2434 * If the queued invalidation is already initialized by us
2435 * (for example, while enabling interrupt-remapping) then
2436 * we got the things already rolling from a sane state.
2437 */
2438 if (iommu->qi)
2439 continue;
2440
2441 /*
2442 * Clear any previous faults.
2443 */
2444 dmar_fault(-1, iommu);
2445 /*
2446 * Disable queued invalidation if supported and already enabled
2447 * before OS handover.
2448 */
2449 dmar_disable_qi(iommu);
2450 }
2451
2452 for_each_drhd_unit(drhd) {
2453 if (drhd->ignored)
2454 continue;
2455
2456 iommu = drhd->iommu;
2457
Youquan Songa77b67d2008-10-16 16:31:56 -07002458 if (dmar_enable_qi(iommu)) {
2459 /*
2460 * Queued Invalidate not enabled, use Register Based
2461 * Invalidate
2462 */
2463 iommu->flush.flush_context = __iommu_flush_context;
2464 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
Yinghai Lu680a7522010-04-08 19:58:23 +01002465 printk(KERN_INFO "IOMMU %d 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002466 "invalidation\n",
Yinghai Lu680a7522010-04-08 19:58:23 +01002467 iommu->seq_id,
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002468 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002469 } else {
2470 iommu->flush.flush_context = qi_flush_context;
2471 iommu->flush.flush_iotlb = qi_flush_iotlb;
Yinghai Lu680a7522010-04-08 19:58:23 +01002472 printk(KERN_INFO "IOMMU %d 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002473 "invalidation\n",
Yinghai Lu680a7522010-04-08 19:58:23 +01002474 iommu->seq_id,
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002475 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002476 }
2477 }
2478
David Woodhouse19943b02009-08-04 16:19:20 +01002479 if (iommu_pass_through)
David Woodhousee0fc7e02009-09-30 09:12:17 -07002480 iommu_identity_mapping |= IDENTMAP_ALL;
2481
David Woodhouse19943b02009-08-04 16:19:20 +01002482#ifdef CONFIG_DMAR_BROKEN_GFX_WA
David Woodhousee0fc7e02009-09-30 09:12:17 -07002483 iommu_identity_mapping |= IDENTMAP_GFX;
David Woodhouse19943b02009-08-04 16:19:20 +01002484#endif
David Woodhousee0fc7e02009-09-30 09:12:17 -07002485
2486 check_tylersburg_isoch();
2487
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002488 /*
2489 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002490 * identity mappings for rmrr, gfx, and isa and may fall back to static
2491 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002492 */
David Woodhouse19943b02009-08-04 16:19:20 +01002493 if (iommu_identity_mapping) {
2494 ret = iommu_prepare_static_identity_mapping(hw_pass_through);
2495 if (ret) {
2496 printk(KERN_CRIT "Failed to setup IOMMU pass-through\n");
2497 goto error;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002498 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002499 }
David Woodhouse19943b02009-08-04 16:19:20 +01002500 /*
2501 * For each rmrr
2502 * for each dev attached to rmrr
2503 * do
2504 * locate drhd for dev, alloc domain for dev
2505 * allocate free domain
2506 * allocate page table entries for rmrr
2507 * if context not allocated for bus
2508 * allocate and init context
2509 * set present in root table for this bus
2510 * init context with domain, translation etc
2511 * endfor
2512 * endfor
2513 */
2514 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
2515 for_each_rmrr_units(rmrr) {
2516 for (i = 0; i < rmrr->devices_cnt; i++) {
2517 pdev = rmrr->devices[i];
2518 /*
2519 * some BIOS lists non-exist devices in DMAR
2520 * table.
2521 */
2522 if (!pdev)
2523 continue;
2524 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2525 if (ret)
2526 printk(KERN_ERR
2527 "IOMMU: mapping reserved region failed\n");
2528 }
2529 }
2530
2531 iommu_prepare_isa();
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002532
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002533 /*
2534 * for each drhd
2535 * enable fault log
2536 * global invalidate context cache
2537 * global invalidate iotlb
2538 * enable translation
2539 */
2540 for_each_drhd_unit(drhd) {
Joseph Cihula51a63e62011-03-21 11:04:24 -07002541 if (drhd->ignored) {
2542 /*
2543 * we always have to disable PMRs or DMA may fail on
2544 * this device
2545 */
2546 if (force_on)
2547 iommu_disable_protect_mem_regions(drhd->iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002548 continue;
Joseph Cihula51a63e62011-03-21 11:04:24 -07002549 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002550 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002551
2552 iommu_flush_write_buffer(iommu);
2553
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002554 ret = dmar_set_interrupt(iommu);
2555 if (ret)
2556 goto error;
2557
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002558 iommu_set_root_entry(iommu);
2559
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002560 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002561 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002562
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002563 ret = iommu_enable_translation(iommu);
2564 if (ret)
2565 goto error;
David Woodhouseb94996c2009-09-19 15:28:12 -07002566
2567 iommu_disable_protect_mem_regions(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002568 }
2569
2570 return 0;
2571error:
2572 for_each_drhd_unit(drhd) {
2573 if (drhd->ignored)
2574 continue;
2575 iommu = drhd->iommu;
2576 free_iommu(iommu);
2577 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002578 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002579 return ret;
2580}
2581
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002582/* This takes a number of _MM_ pages, not VTD pages */
David Woodhouse875764d2009-06-28 21:20:51 +01002583static struct iova *intel_alloc_iova(struct device *dev,
2584 struct dmar_domain *domain,
2585 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002586{
2587 struct pci_dev *pdev = to_pci_dev(dev);
2588 struct iova *iova = NULL;
2589
David Woodhouse875764d2009-06-28 21:20:51 +01002590 /* Restrict dma_mask to the width that the iommu can handle */
2591 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
2592
2593 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002594 /*
2595 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002596 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002597 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002598 */
David Woodhouse875764d2009-06-28 21:20:51 +01002599 iova = alloc_iova(&domain->iovad, nrpages,
2600 IOVA_PFN(DMA_BIT_MASK(32)), 1);
2601 if (iova)
2602 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002603 }
David Woodhouse875764d2009-06-28 21:20:51 +01002604 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
2605 if (unlikely(!iova)) {
2606 printk(KERN_ERR "Allocating %ld-page iova for %s failed",
2607 nrpages, pci_name(pdev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002608 return NULL;
2609 }
2610
2611 return iova;
2612}
2613
David Woodhouse147202a2009-07-07 19:43:20 +01002614static struct dmar_domain *__get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002615{
2616 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002617 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002618
2619 domain = get_domain_for_dev(pdev,
2620 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2621 if (!domain) {
2622 printk(KERN_ERR
2623 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002624 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002625 }
2626
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002627 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002628 if (unlikely(!domain_context_mapped(pdev))) {
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002629 ret = domain_context_mapping(domain, pdev,
2630 CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002631 if (ret) {
2632 printk(KERN_ERR
2633 "Domain context map for %s failed",
2634 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002635 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002636 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002637 }
2638
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002639 return domain;
2640}
2641
David Woodhouse147202a2009-07-07 19:43:20 +01002642static inline struct dmar_domain *get_valid_domain_for_dev(struct pci_dev *dev)
2643{
2644 struct device_domain_info *info;
2645
2646 /* No lock here, assumes no domain exit in normal case */
2647 info = dev->dev.archdata.iommu;
2648 if (likely(info))
2649 return info->domain;
2650
2651 return __get_valid_domain_for_dev(dev);
2652}
2653
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002654static int iommu_dummy(struct pci_dev *pdev)
2655{
2656 return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
2657}
2658
2659/* Check if the pdev needs to go through non-identity map and unmap process.*/
David Woodhouse73676832009-07-04 14:08:36 +01002660static int iommu_no_mapping(struct device *dev)
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002661{
David Woodhouse73676832009-07-04 14:08:36 +01002662 struct pci_dev *pdev;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002663 int found;
2664
David Woodhouse73676832009-07-04 14:08:36 +01002665 if (unlikely(dev->bus != &pci_bus_type))
2666 return 1;
2667
2668 pdev = to_pci_dev(dev);
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002669 if (iommu_dummy(pdev))
2670 return 1;
2671
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002672 if (!iommu_identity_mapping)
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002673 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002674
2675 found = identity_mapping(pdev);
2676 if (found) {
David Woodhouse6941af22009-07-04 18:24:27 +01002677 if (iommu_should_identity_map(pdev, 0))
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002678 return 1;
2679 else {
2680 /*
2681 * 32 bit DMA is removed from si_domain and fall back
2682 * to non-identity mapping.
2683 */
2684 domain_remove_one_dev_info(si_domain, pdev);
2685 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
2686 pci_name(pdev));
2687 return 0;
2688 }
2689 } else {
2690 /*
2691 * In case of a detached 64 bit DMA device from vm, the device
2692 * is put into si_domain for identity mapping.
2693 */
David Woodhouse6941af22009-07-04 18:24:27 +01002694 if (iommu_should_identity_map(pdev, 0)) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002695 int ret;
David Woodhouse5fe60f42009-08-09 10:53:41 +01002696 ret = domain_add_dev_info(si_domain, pdev,
2697 hw_pass_through ?
2698 CONTEXT_TT_PASS_THROUGH :
2699 CONTEXT_TT_MULTI_LEVEL);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002700 if (!ret) {
2701 printk(KERN_INFO "64bit %s uses identity mapping\n",
2702 pci_name(pdev));
2703 return 1;
2704 }
2705 }
2706 }
2707
David Woodhouse1e4c64c2009-07-04 10:40:38 +01002708 return 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002709}
2710
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002711static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2712 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002713{
2714 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002715 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002716 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002717 struct iova *iova;
2718 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002719 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002720 struct intel_iommu *iommu;
Fenghua Yu33041ec2009-08-04 15:10:59 -07002721 unsigned long paddr_pfn = paddr >> PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002722
2723 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002724
David Woodhouse73676832009-07-04 14:08:36 +01002725 if (iommu_no_mapping(hwdev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002726 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002727
2728 domain = get_valid_domain_for_dev(pdev);
2729 if (!domain)
2730 return 0;
2731
Weidong Han8c11e792008-12-08 15:29:22 +08002732 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01002733 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002734
David Woodhouse5a5e02a2009-07-04 09:35:44 +01002735 iova = intel_alloc_iova(hwdev, domain, dma_to_mm_pfn(size),
2736 pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002737 if (!iova)
2738 goto error;
2739
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002740 /*
2741 * Check if DMAR supports zero-length reads on write only
2742 * mappings..
2743 */
2744 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002745 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002746 prot |= DMA_PTE_READ;
2747 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2748 prot |= DMA_PTE_WRITE;
2749 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002750 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002751 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002752 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002753 * is not a big problem
2754 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01002755 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
Fenghua Yu33041ec2009-08-04 15:10:59 -07002756 mm_to_dma_pfn(paddr_pfn), size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002757 if (ret)
2758 goto error;
2759
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002760 /* it's a non-present to present mapping. Only flush if caching mode */
2761 if (cap_caching_mode(iommu->cap))
Nadav Amit82653632010-04-01 13:24:40 +03002762 iommu_flush_iotlb_psi(iommu, domain->id, mm_to_dma_pfn(iova->pfn_lo), size, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002763 else
Weidong Han8c11e792008-12-08 15:29:22 +08002764 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002765
David Woodhouse03d6a242009-06-28 15:33:46 +01002766 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
2767 start_paddr += paddr & ~PAGE_MASK;
2768 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002769
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002770error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002771 if (iova)
2772 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002773 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002774 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002775 return 0;
2776}
2777
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002778static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2779 unsigned long offset, size_t size,
2780 enum dma_data_direction dir,
2781 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002782{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002783 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2784 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002785}
2786
mark gross5e0d2a62008-03-04 15:22:08 -08002787static void flush_unmaps(void)
2788{
mark gross80b20dd2008-04-18 13:53:58 -07002789 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002790
mark gross5e0d2a62008-03-04 15:22:08 -08002791 timer_on = 0;
2792
2793 /* just flush them all */
2794 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002795 struct intel_iommu *iommu = g_iommus[i];
2796 if (!iommu)
2797 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002798
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002799 if (!deferred_flush[i].next)
2800 continue;
2801
Nadav Amit78d5f0f2010-04-08 23:00:41 +03002802 /* In caching mode, global flushes turn emulation expensive */
2803 if (!cap_caching_mode(iommu->cap))
2804 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08002805 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002806 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08002807 unsigned long mask;
2808 struct iova *iova = deferred_flush[i].iova[j];
Nadav Amit78d5f0f2010-04-08 23:00:41 +03002809 struct dmar_domain *domain = deferred_flush[i].domain[j];
Yu Zhao93a23a72009-05-18 13:51:37 +08002810
Nadav Amit78d5f0f2010-04-08 23:00:41 +03002811 /* On real hardware multiple invalidations are expensive */
2812 if (cap_caching_mode(iommu->cap))
2813 iommu_flush_iotlb_psi(iommu, domain->id,
2814 iova->pfn_lo, iova->pfn_hi - iova->pfn_lo + 1, 0);
2815 else {
2816 mask = ilog2(mm_to_dma_pfn(iova->pfn_hi - iova->pfn_lo + 1));
2817 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
2818 (uint64_t)iova->pfn_lo << PAGE_SHIFT, mask);
2819 }
Yu Zhao93a23a72009-05-18 13:51:37 +08002820 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
mark gross80b20dd2008-04-18 13:53:58 -07002821 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002822 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002823 }
2824
mark gross5e0d2a62008-03-04 15:22:08 -08002825 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002826}
2827
2828static void flush_unmaps_timeout(unsigned long data)
2829{
mark gross80b20dd2008-04-18 13:53:58 -07002830 unsigned long flags;
2831
2832 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002833 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002834 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002835}
2836
2837static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2838{
2839 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002840 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002841 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002842
2843 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002844 if (list_size == HIGH_WATER_MARK)
2845 flush_unmaps();
2846
Weidong Han8c11e792008-12-08 15:29:22 +08002847 iommu = domain_get_iommu(dom);
2848 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002849
mark gross80b20dd2008-04-18 13:53:58 -07002850 next = deferred_flush[iommu_id].next;
2851 deferred_flush[iommu_id].domain[next] = dom;
2852 deferred_flush[iommu_id].iova[next] = iova;
2853 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002854
2855 if (!timer_on) {
2856 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2857 timer_on = 1;
2858 }
2859 list_size++;
2860 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2861}
2862
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002863static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2864 size_t size, enum dma_data_direction dir,
2865 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002866{
2867 struct pci_dev *pdev = to_pci_dev(dev);
2868 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002869 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002870 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002871 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002872
David Woodhouse73676832009-07-04 14:08:36 +01002873 if (iommu_no_mapping(dev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002874 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002875
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002876 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002877 BUG_ON(!domain);
2878
Weidong Han8c11e792008-12-08 15:29:22 +08002879 iommu = domain_get_iommu(domain);
2880
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002881 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
David Woodhouse85b98272009-07-01 19:27:53 +01002882 if (WARN_ONCE(!iova, "Driver unmaps unmatched page at PFN %llx\n",
2883 (unsigned long long)dev_addr))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002884 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002885
David Woodhoused794dc92009-06-28 00:27:49 +01002886 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2887 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002888
David Woodhoused794dc92009-06-28 00:27:49 +01002889 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
2890 pci_name(pdev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002891
2892 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002893 dma_pte_clear_range(domain, start_pfn, last_pfn);
2894
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002895 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01002896 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2897
mark gross5e0d2a62008-03-04 15:22:08 -08002898 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01002899 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
Nadav Amit82653632010-04-01 13:24:40 +03002900 last_pfn - start_pfn + 1, 0);
mark gross5e0d2a62008-03-04 15:22:08 -08002901 /* free iova */
2902 __free_iova(&domain->iovad, iova);
2903 } else {
2904 add_unmap(domain, iova);
2905 /*
2906 * queue up the release of the unmap to save the 1/6th of the
2907 * cpu used up by the iotlb flush operation...
2908 */
mark gross5e0d2a62008-03-04 15:22:08 -08002909 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002910}
2911
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002912static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2913 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002914{
2915 void *vaddr;
2916 int order;
2917
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002918 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002919 order = get_order(size);
Alex Williamsone8bb9102009-11-04 15:59:34 -07002920
2921 if (!iommu_no_mapping(hwdev))
2922 flags &= ~(GFP_DMA | GFP_DMA32);
2923 else if (hwdev->coherent_dma_mask < dma_get_required_mask(hwdev)) {
2924 if (hwdev->coherent_dma_mask < DMA_BIT_MASK(32))
2925 flags |= GFP_DMA;
2926 else
2927 flags |= GFP_DMA32;
2928 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002929
2930 vaddr = (void *)__get_free_pages(flags, order);
2931 if (!vaddr)
2932 return NULL;
2933 memset(vaddr, 0, size);
2934
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002935 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2936 DMA_BIDIRECTIONAL,
2937 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002938 if (*dma_handle)
2939 return vaddr;
2940 free_pages((unsigned long)vaddr, order);
2941 return NULL;
2942}
2943
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002944static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2945 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002946{
2947 int order;
2948
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002949 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002950 order = get_order(size);
2951
David Woodhouse0db9b7a2009-07-14 02:01:57 +01002952 intel_unmap_page(hwdev, dma_handle, size, DMA_BIDIRECTIONAL, NULL);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002953 free_pages((unsigned long)vaddr, order);
2954}
2955
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002956static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2957 int nelems, enum dma_data_direction dir,
2958 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002959{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002960 struct pci_dev *pdev = to_pci_dev(hwdev);
2961 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002962 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002963 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002964 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002965
David Woodhouse73676832009-07-04 14:08:36 +01002966 if (iommu_no_mapping(hwdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002967 return;
2968
2969 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002970 BUG_ON(!domain);
2971
2972 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002973
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002974 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
David Woodhouse85b98272009-07-01 19:27:53 +01002975 if (WARN_ONCE(!iova, "Driver unmaps unmatched sglist at PFN %llx\n",
2976 (unsigned long long)sglist[0].dma_address))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002977 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002978
David Woodhoused794dc92009-06-28 00:27:49 +01002979 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2980 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002981
2982 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002983 dma_pte_clear_range(domain, start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002984
David Woodhoused794dc92009-06-28 00:27:49 +01002985 /* free page tables */
2986 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2987
David Woodhouseacea0012009-07-14 01:55:11 +01002988 if (intel_iommu_strict) {
2989 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
Nadav Amit82653632010-04-01 13:24:40 +03002990 last_pfn - start_pfn + 1, 0);
David Woodhouseacea0012009-07-14 01:55:11 +01002991 /* free iova */
2992 __free_iova(&domain->iovad, iova);
2993 } else {
2994 add_unmap(domain, iova);
2995 /*
2996 * queue up the release of the unmap to save the 1/6th of the
2997 * cpu used up by the iotlb flush operation...
2998 */
2999 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003000}
3001
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003002static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003003 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003004{
3005 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003006 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003007
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003008 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02003009 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00003010 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003011 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003012 }
3013 return nelems;
3014}
3015
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09003016static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
3017 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003018{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003019 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003020 struct pci_dev *pdev = to_pci_dev(hwdev);
3021 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003022 size_t size = 0;
3023 int prot = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003024 struct iova *iova = NULL;
3025 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003026 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01003027 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08003028 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003029
3030 BUG_ON(dir == DMA_NONE);
David Woodhouse73676832009-07-04 14:08:36 +01003031 if (iommu_no_mapping(hwdev))
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003032 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003033
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003034 domain = get_valid_domain_for_dev(pdev);
3035 if (!domain)
3036 return 0;
3037
Weidong Han8c11e792008-12-08 15:29:22 +08003038 iommu = domain_get_iommu(domain);
3039
David Woodhouseb536d242009-06-28 14:49:31 +01003040 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01003041 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003042
David Woodhouse5a5e02a2009-07-04 09:35:44 +01003043 iova = intel_alloc_iova(hwdev, domain, dma_to_mm_pfn(size),
3044 pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003045 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07003046 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003047 return 0;
3048 }
3049
3050 /*
3051 * Check if DMAR supports zero-length reads on write only
3052 * mappings..
3053 */
3054 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08003055 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003056 prot |= DMA_PTE_READ;
3057 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
3058 prot |= DMA_PTE_WRITE;
3059
David Woodhouseb536d242009-06-28 14:49:31 +01003060 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01003061
Fenghua Yuf5329592009-08-04 15:09:37 -07003062 ret = domain_sg_mapping(domain, start_vpfn, sglist, size, prot);
David Woodhousee1605492009-06-29 11:17:38 +01003063 if (unlikely(ret)) {
3064 /* clear the page */
3065 dma_pte_clear_range(domain, start_vpfn,
3066 start_vpfn + size - 1);
3067 /* free page tables */
3068 dma_pte_free_pagetable(domain, start_vpfn,
3069 start_vpfn + size - 1);
3070 /* free iova */
3071 __free_iova(&domain->iovad, iova);
3072 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07003073 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003074
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003075 /* it's a non-present to present mapping. Only flush if caching mode */
3076 if (cap_caching_mode(iommu->cap))
Nadav Amit82653632010-04-01 13:24:40 +03003077 iommu_flush_iotlb_psi(iommu, domain->id, start_vpfn, size, 1);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003078 else
Weidong Han8c11e792008-12-08 15:29:22 +08003079 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003080
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003081 return nelems;
3082}
3083
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003084static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
3085{
3086 return !dma_addr;
3087}
3088
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09003089struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003090 .alloc_coherent = intel_alloc_coherent,
3091 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003092 .map_sg = intel_map_sg,
3093 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09003094 .map_page = intel_map_page,
3095 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09003096 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003097};
3098
3099static inline int iommu_domain_cache_init(void)
3100{
3101 int ret = 0;
3102
3103 iommu_domain_cache = kmem_cache_create("iommu_domain",
3104 sizeof(struct dmar_domain),
3105 0,
3106 SLAB_HWCACHE_ALIGN,
3107
3108 NULL);
3109 if (!iommu_domain_cache) {
3110 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
3111 ret = -ENOMEM;
3112 }
3113
3114 return ret;
3115}
3116
3117static inline int iommu_devinfo_cache_init(void)
3118{
3119 int ret = 0;
3120
3121 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
3122 sizeof(struct device_domain_info),
3123 0,
3124 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003125 NULL);
3126 if (!iommu_devinfo_cache) {
3127 printk(KERN_ERR "Couldn't create devinfo cache\n");
3128 ret = -ENOMEM;
3129 }
3130
3131 return ret;
3132}
3133
3134static inline int iommu_iova_cache_init(void)
3135{
3136 int ret = 0;
3137
3138 iommu_iova_cache = kmem_cache_create("iommu_iova",
3139 sizeof(struct iova),
3140 0,
3141 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003142 NULL);
3143 if (!iommu_iova_cache) {
3144 printk(KERN_ERR "Couldn't create iova cache\n");
3145 ret = -ENOMEM;
3146 }
3147
3148 return ret;
3149}
3150
3151static int __init iommu_init_mempool(void)
3152{
3153 int ret;
3154 ret = iommu_iova_cache_init();
3155 if (ret)
3156 return ret;
3157
3158 ret = iommu_domain_cache_init();
3159 if (ret)
3160 goto domain_error;
3161
3162 ret = iommu_devinfo_cache_init();
3163 if (!ret)
3164 return ret;
3165
3166 kmem_cache_destroy(iommu_domain_cache);
3167domain_error:
3168 kmem_cache_destroy(iommu_iova_cache);
3169
3170 return -ENOMEM;
3171}
3172
3173static void __init iommu_exit_mempool(void)
3174{
3175 kmem_cache_destroy(iommu_devinfo_cache);
3176 kmem_cache_destroy(iommu_domain_cache);
3177 kmem_cache_destroy(iommu_iova_cache);
3178
3179}
3180
Dan Williams556ab452010-07-23 15:47:56 -07003181static void quirk_ioat_snb_local_iommu(struct pci_dev *pdev)
3182{
3183 struct dmar_drhd_unit *drhd;
3184 u32 vtbar;
3185 int rc;
3186
3187 /* We know that this device on this chipset has its own IOMMU.
3188 * If we find it under a different IOMMU, then the BIOS is lying
3189 * to us. Hope that the IOMMU for this device is actually
3190 * disabled, and it needs no translation...
3191 */
3192 rc = pci_bus_read_config_dword(pdev->bus, PCI_DEVFN(0, 0), 0xb0, &vtbar);
3193 if (rc) {
3194 /* "can't" happen */
3195 dev_info(&pdev->dev, "failed to run vt-d quirk\n");
3196 return;
3197 }
3198 vtbar &= 0xffff0000;
3199
3200 /* we know that the this iommu should be at offset 0xa000 from vtbar */
3201 drhd = dmar_find_matched_drhd_unit(pdev);
3202 if (WARN_TAINT_ONCE(!drhd || drhd->reg_base_addr - vtbar != 0xa000,
3203 TAINT_FIRMWARE_WORKAROUND,
3204 "BIOS assigned incorrect VT-d unit for Intel(R) QuickData Technology device\n"))
3205 pdev->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
3206}
3207DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB, quirk_ioat_snb_local_iommu);
3208
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003209static void __init init_no_remapping_devices(void)
3210{
3211 struct dmar_drhd_unit *drhd;
3212
3213 for_each_drhd_unit(drhd) {
3214 if (!drhd->include_all) {
3215 int i;
3216 for (i = 0; i < drhd->devices_cnt; i++)
3217 if (drhd->devices[i] != NULL)
3218 break;
3219 /* ignore DMAR unit if no pci devices exist */
3220 if (i == drhd->devices_cnt)
3221 drhd->ignored = 1;
3222 }
3223 }
3224
3225 if (dmar_map_gfx)
3226 return;
3227
3228 for_each_drhd_unit(drhd) {
3229 int i;
3230 if (drhd->ignored || drhd->include_all)
3231 continue;
3232
3233 for (i = 0; i < drhd->devices_cnt; i++)
3234 if (drhd->devices[i] &&
3235 !IS_GFX_DEVICE(drhd->devices[i]))
3236 break;
3237
3238 if (i < drhd->devices_cnt)
3239 continue;
3240
3241 /* bypass IOMMU if it is just for gfx devices */
3242 drhd->ignored = 1;
3243 for (i = 0; i < drhd->devices_cnt; i++) {
3244 if (!drhd->devices[i])
3245 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07003246 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003247 }
3248 }
3249}
3250
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003251#ifdef CONFIG_SUSPEND
3252static int init_iommu_hw(void)
3253{
3254 struct dmar_drhd_unit *drhd;
3255 struct intel_iommu *iommu = NULL;
3256
3257 for_each_active_iommu(iommu, drhd)
3258 if (iommu->qi)
3259 dmar_reenable_qi(iommu);
3260
Joseph Cihulab7792602011-05-03 00:08:37 -07003261 for_each_iommu(iommu, drhd) {
3262 if (drhd->ignored) {
3263 /*
3264 * we always have to disable PMRs or DMA may fail on
3265 * this device
3266 */
3267 if (force_on)
3268 iommu_disable_protect_mem_regions(iommu);
3269 continue;
3270 }
3271
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003272 iommu_flush_write_buffer(iommu);
3273
3274 iommu_set_root_entry(iommu);
3275
3276 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003277 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003278 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003279 DMA_TLB_GLOBAL_FLUSH);
Joseph Cihulab7792602011-05-03 00:08:37 -07003280 if (iommu_enable_translation(iommu))
3281 return 1;
David Woodhouseb94996c2009-09-19 15:28:12 -07003282 iommu_disable_protect_mem_regions(iommu);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003283 }
3284
3285 return 0;
3286}
3287
3288static void iommu_flush_all(void)
3289{
3290 struct dmar_drhd_unit *drhd;
3291 struct intel_iommu *iommu;
3292
3293 for_each_active_iommu(iommu, drhd) {
3294 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003295 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003296 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003297 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003298 }
3299}
3300
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003301static int iommu_suspend(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003302{
3303 struct dmar_drhd_unit *drhd;
3304 struct intel_iommu *iommu = NULL;
3305 unsigned long flag;
3306
3307 for_each_active_iommu(iommu, drhd) {
3308 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3309 GFP_ATOMIC);
3310 if (!iommu->iommu_state)
3311 goto nomem;
3312 }
3313
3314 iommu_flush_all();
3315
3316 for_each_active_iommu(iommu, drhd) {
3317 iommu_disable_translation(iommu);
3318
3319 spin_lock_irqsave(&iommu->register_lock, flag);
3320
3321 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3322 readl(iommu->reg + DMAR_FECTL_REG);
3323 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3324 readl(iommu->reg + DMAR_FEDATA_REG);
3325 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3326 readl(iommu->reg + DMAR_FEADDR_REG);
3327 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3328 readl(iommu->reg + DMAR_FEUADDR_REG);
3329
3330 spin_unlock_irqrestore(&iommu->register_lock, flag);
3331 }
3332 return 0;
3333
3334nomem:
3335 for_each_active_iommu(iommu, drhd)
3336 kfree(iommu->iommu_state);
3337
3338 return -ENOMEM;
3339}
3340
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003341static void iommu_resume(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003342{
3343 struct dmar_drhd_unit *drhd;
3344 struct intel_iommu *iommu = NULL;
3345 unsigned long flag;
3346
3347 if (init_iommu_hw()) {
Joseph Cihulab7792602011-05-03 00:08:37 -07003348 if (force_on)
3349 panic("tboot: IOMMU setup failed, DMAR can not resume!\n");
3350 else
3351 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003352 return;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003353 }
3354
3355 for_each_active_iommu(iommu, drhd) {
3356
3357 spin_lock_irqsave(&iommu->register_lock, flag);
3358
3359 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3360 iommu->reg + DMAR_FECTL_REG);
3361 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3362 iommu->reg + DMAR_FEDATA_REG);
3363 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3364 iommu->reg + DMAR_FEADDR_REG);
3365 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3366 iommu->reg + DMAR_FEUADDR_REG);
3367
3368 spin_unlock_irqrestore(&iommu->register_lock, flag);
3369 }
3370
3371 for_each_active_iommu(iommu, drhd)
3372 kfree(iommu->iommu_state);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003373}
3374
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003375static struct syscore_ops iommu_syscore_ops = {
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003376 .resume = iommu_resume,
3377 .suspend = iommu_suspend,
3378};
3379
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003380static void __init init_iommu_pm_ops(void)
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003381{
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003382 register_syscore_ops(&iommu_syscore_ops);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003383}
3384
3385#else
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003386static inline int init_iommu_pm_ops(void) { }
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003387#endif /* CONFIG_PM */
3388
Fenghua Yu99dcade2009-11-11 07:23:06 -08003389/*
3390 * Here we only respond to action of unbound device from driver.
3391 *
3392 * Added device is not attached to its DMAR domain here yet. That will happen
3393 * when mapping the device to iova.
3394 */
3395static int device_notifier(struct notifier_block *nb,
3396 unsigned long action, void *data)
3397{
3398 struct device *dev = data;
3399 struct pci_dev *pdev = to_pci_dev(dev);
3400 struct dmar_domain *domain;
3401
David Woodhouse44cd6132009-12-02 10:18:30 +00003402 if (iommu_no_mapping(dev))
3403 return 0;
3404
Fenghua Yu99dcade2009-11-11 07:23:06 -08003405 domain = find_domain(pdev);
3406 if (!domain)
3407 return 0;
3408
Alex Williamsona97590e2011-03-04 14:52:16 -07003409 if (action == BUS_NOTIFY_UNBOUND_DRIVER && !iommu_pass_through) {
Fenghua Yu99dcade2009-11-11 07:23:06 -08003410 domain_remove_one_dev_info(domain, pdev);
3411
Alex Williamsona97590e2011-03-04 14:52:16 -07003412 if (!(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) &&
3413 !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) &&
3414 list_empty(&domain->devices))
3415 domain_exit(domain);
3416 }
3417
Fenghua Yu99dcade2009-11-11 07:23:06 -08003418 return 0;
3419}
3420
3421static struct notifier_block device_nb = {
3422 .notifier_call = device_notifier,
3423};
3424
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003425int __init intel_iommu_init(void)
3426{
3427 int ret = 0;
3428
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003429 /* VT-d is required for a TXT/tboot launch, so enforce that */
3430 force_on = tboot_force_iommu();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003431
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003432 if (dmar_table_init()) {
3433 if (force_on)
3434 panic("tboot: Failed to initialize DMAR table\n");
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003435 return -ENODEV;
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003436 }
3437
3438 if (dmar_dev_scope_init()) {
3439 if (force_on)
3440 panic("tboot: Failed to initialize DMAR device scope\n");
3441 return -ENODEV;
3442 }
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003443
Suresh Siddha2ae21012008-07-10 11:16:43 -07003444 /*
3445 * Check the need for DMA-remapping initialization now.
3446 * Above initialization will also be used by Interrupt-remapping.
3447 */
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003448 if (no_iommu || dmar_disabled)
Suresh Siddha2ae21012008-07-10 11:16:43 -07003449 return -ENODEV;
3450
Joseph Cihula51a63e62011-03-21 11:04:24 -07003451 if (iommu_init_mempool()) {
3452 if (force_on)
3453 panic("tboot: Failed to initialize iommu memory\n");
3454 return -ENODEV;
3455 }
3456
3457 if (dmar_init_reserved_ranges()) {
3458 if (force_on)
3459 panic("tboot: Failed to reserve iommu ranges\n");
3460 return -ENODEV;
3461 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003462
3463 init_no_remapping_devices();
3464
Joseph Cihulab7792602011-05-03 00:08:37 -07003465 ret = init_dmars();
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003466 if (ret) {
Joseph Cihulaa59b50e2009-06-30 19:31:10 -07003467 if (force_on)
3468 panic("tboot: Failed to initialize DMARs\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003469 printk(KERN_ERR "IOMMU: dmar init failed\n");
3470 put_iova_domain(&reserved_iova_list);
3471 iommu_exit_mempool();
3472 return ret;
3473 }
3474 printk(KERN_INFO
3475 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
3476
mark gross5e0d2a62008-03-04 15:22:08 -08003477 init_timer(&unmap_timer);
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003478#ifdef CONFIG_SWIOTLB
3479 swiotlb = 0;
3480#endif
David Woodhouse19943b02009-08-04 16:19:20 +01003481 dma_ops = &intel_dma_ops;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003482
Rafael J. Wysocki134fac32011-03-23 22:16:14 +01003483 init_iommu_pm_ops();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003484
3485 register_iommu(&intel_iommu_ops);
3486
Fenghua Yu99dcade2009-11-11 07:23:06 -08003487 bus_register_notifier(&pci_bus_type, &device_nb);
3488
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003489 return 0;
3490}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07003491
Han, Weidong3199aa62009-02-26 17:31:12 +08003492static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
3493 struct pci_dev *pdev)
3494{
3495 struct pci_dev *tmp, *parent;
3496
3497 if (!iommu || !pdev)
3498 return;
3499
3500 /* dependent device detach */
3501 tmp = pci_find_upstream_pcie_bridge(pdev);
3502 /* Secondary interface's bus number and devfn 0 */
3503 if (tmp) {
3504 parent = pdev->bus->self;
3505 while (parent != tmp) {
3506 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01003507 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003508 parent = parent->bus->self;
3509 }
Stefan Assmann45e829e2009-12-03 06:49:24 -05003510 if (pci_is_pcie(tmp)) /* this is a PCIe-to-PCI bridge */
Han, Weidong3199aa62009-02-26 17:31:12 +08003511 iommu_detach_dev(iommu,
3512 tmp->subordinate->number, 0);
3513 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01003514 iommu_detach_dev(iommu, tmp->bus->number,
3515 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003516 }
3517}
3518
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003519static void domain_remove_one_dev_info(struct dmar_domain *domain,
Weidong Hanc7151a82008-12-08 22:51:37 +08003520 struct pci_dev *pdev)
3521{
3522 struct device_domain_info *info;
3523 struct intel_iommu *iommu;
3524 unsigned long flags;
3525 int found = 0;
3526 struct list_head *entry, *tmp;
3527
David Woodhouse276dbf992009-04-04 01:45:37 +01003528 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3529 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003530 if (!iommu)
3531 return;
3532
3533 spin_lock_irqsave(&device_domain_lock, flags);
3534 list_for_each_safe(entry, tmp, &domain->devices) {
3535 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01003536 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08003537 if (info->bus == pdev->bus->number &&
3538 info->devfn == pdev->devfn) {
3539 list_del(&info->link);
3540 list_del(&info->global);
3541 if (info->dev)
3542 info->dev->dev.archdata.iommu = NULL;
3543 spin_unlock_irqrestore(&device_domain_lock, flags);
3544
Yu Zhao93a23a72009-05-18 13:51:37 +08003545 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08003546 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003547 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003548 free_devinfo_mem(info);
3549
3550 spin_lock_irqsave(&device_domain_lock, flags);
3551
3552 if (found)
3553 break;
3554 else
3555 continue;
3556 }
3557
3558 /* if there is no other devices under the same iommu
3559 * owned by this domain, clear this iommu in iommu_bmp
3560 * update iommu count and coherency
3561 */
David Woodhouse276dbf992009-04-04 01:45:37 +01003562 if (iommu == device_to_iommu(info->segment, info->bus,
3563 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08003564 found = 1;
3565 }
3566
3567 if (found == 0) {
3568 unsigned long tmp_flags;
3569 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3570 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3571 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003572 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003573 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
Alex Williamsona97590e2011-03-04 14:52:16 -07003574
Alex Williamson9b4554b2011-05-24 12:19:04 -04003575 if (!(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) &&
3576 !(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)) {
3577 spin_lock_irqsave(&iommu->lock, tmp_flags);
3578 clear_bit(domain->id, iommu->domain_ids);
3579 iommu->domains[domain->id] = NULL;
3580 spin_unlock_irqrestore(&iommu->lock, tmp_flags);
3581 }
Weidong Hanc7151a82008-12-08 22:51:37 +08003582 }
3583
3584 spin_unlock_irqrestore(&device_domain_lock, flags);
3585}
3586
3587static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3588{
3589 struct device_domain_info *info;
3590 struct intel_iommu *iommu;
3591 unsigned long flags1, flags2;
3592
3593 spin_lock_irqsave(&device_domain_lock, flags1);
3594 while (!list_empty(&domain->devices)) {
3595 info = list_entry(domain->devices.next,
3596 struct device_domain_info, link);
3597 list_del(&info->link);
3598 list_del(&info->global);
3599 if (info->dev)
3600 info->dev->dev.archdata.iommu = NULL;
3601
3602 spin_unlock_irqrestore(&device_domain_lock, flags1);
3603
Yu Zhao93a23a72009-05-18 13:51:37 +08003604 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01003605 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003606 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003607 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003608
3609 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08003610 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08003611 */
3612 spin_lock_irqsave(&domain->iommu_lock, flags2);
3613 if (test_and_clear_bit(iommu->seq_id,
3614 &domain->iommu_bmp)) {
3615 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003616 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003617 }
3618 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3619
3620 free_devinfo_mem(info);
3621 spin_lock_irqsave(&device_domain_lock, flags1);
3622 }
3623 spin_unlock_irqrestore(&device_domain_lock, flags1);
3624}
3625
Weidong Han5e98c4b2008-12-08 23:03:27 +08003626/* domain id for virtual machine, it won't be set in context */
3627static unsigned long vm_domid;
3628
3629static struct dmar_domain *iommu_alloc_vm_domain(void)
3630{
3631 struct dmar_domain *domain;
3632
3633 domain = alloc_domain_mem();
3634 if (!domain)
3635 return NULL;
3636
3637 domain->id = vm_domid++;
Suresh Siddha4c923d42009-10-02 11:01:24 -07003638 domain->nid = -1;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003639 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3640 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3641
3642 return domain;
3643}
3644
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003645static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08003646{
3647 int adjust_width;
3648
3649 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003650 spin_lock_init(&domain->iommu_lock);
3651
3652 domain_reserve_special_ranges(domain);
3653
3654 /* calculate AGAW */
3655 domain->gaw = guest_width;
3656 adjust_width = guestwidth_to_adjustwidth(guest_width);
3657 domain->agaw = width_to_agaw(adjust_width);
3658
3659 INIT_LIST_HEAD(&domain->devices);
3660
3661 domain->iommu_count = 0;
3662 domain->iommu_coherency = 0;
Sheng Yangc5b15252009-08-06 13:31:56 +08003663 domain->iommu_snooping = 0;
Youquan Song6dd9a7c2011-05-25 19:13:49 +01003664 domain->iommu_superpage = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003665 domain->max_addr = 0;
Suresh Siddha4c923d42009-10-02 11:01:24 -07003666 domain->nid = -1;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003667
3668 /* always allocate the top pgd */
Suresh Siddha4c923d42009-10-02 11:01:24 -07003669 domain->pgd = (struct dma_pte *)alloc_pgtable_page(domain->nid);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003670 if (!domain->pgd)
3671 return -ENOMEM;
3672 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3673 return 0;
3674}
3675
3676static void iommu_free_vm_domain(struct dmar_domain *domain)
3677{
3678 unsigned long flags;
3679 struct dmar_drhd_unit *drhd;
3680 struct intel_iommu *iommu;
3681 unsigned long i;
3682 unsigned long ndomains;
3683
3684 for_each_drhd_unit(drhd) {
3685 if (drhd->ignored)
3686 continue;
3687 iommu = drhd->iommu;
3688
3689 ndomains = cap_ndoms(iommu->cap);
Akinobu Mitaa45946a2010-03-11 14:04:08 -08003690 for_each_set_bit(i, iommu->domain_ids, ndomains) {
Weidong Han5e98c4b2008-12-08 23:03:27 +08003691 if (iommu->domains[i] == domain) {
3692 spin_lock_irqsave(&iommu->lock, flags);
3693 clear_bit(i, iommu->domain_ids);
3694 iommu->domains[i] = NULL;
3695 spin_unlock_irqrestore(&iommu->lock, flags);
3696 break;
3697 }
Weidong Han5e98c4b2008-12-08 23:03:27 +08003698 }
3699 }
3700}
3701
3702static void vm_domain_exit(struct dmar_domain *domain)
3703{
Weidong Han5e98c4b2008-12-08 23:03:27 +08003704 /* Domain 0 is reserved, so dont process it */
3705 if (!domain)
3706 return;
3707
3708 vm_domain_remove_all_dev_info(domain);
3709 /* destroy iovas */
3710 put_iova_domain(&domain->iovad);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003711
3712 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01003713 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003714
3715 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01003716 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003717
3718 iommu_free_vm_domain(domain);
3719 free_domain_mem(domain);
3720}
3721
Joerg Roedel5d450802008-12-03 14:52:32 +01003722static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003723{
Joerg Roedel5d450802008-12-03 14:52:32 +01003724 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003725
Joerg Roedel5d450802008-12-03 14:52:32 +01003726 dmar_domain = iommu_alloc_vm_domain();
3727 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003728 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003729 "intel_iommu_domain_init: dmar_domain == NULL\n");
3730 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003731 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003732 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003733 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003734 "intel_iommu_domain_init() failed\n");
3735 vm_domain_exit(dmar_domain);
3736 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003737 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003738 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003739
Joerg Roedel5d450802008-12-03 14:52:32 +01003740 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003741}
Kay, Allen M38717942008-09-09 18:37:29 +03003742
Joerg Roedel5d450802008-12-03 14:52:32 +01003743static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003744{
Joerg Roedel5d450802008-12-03 14:52:32 +01003745 struct dmar_domain *dmar_domain = domain->priv;
3746
3747 domain->priv = NULL;
3748 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003749}
Kay, Allen M38717942008-09-09 18:37:29 +03003750
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003751static int intel_iommu_attach_device(struct iommu_domain *domain,
3752 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003753{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003754 struct dmar_domain *dmar_domain = domain->priv;
3755 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003756 struct intel_iommu *iommu;
3757 int addr_width;
Kay, Allen M38717942008-09-09 18:37:29 +03003758
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003759 /* normally pdev is not mapped */
3760 if (unlikely(domain_context_mapped(pdev))) {
3761 struct dmar_domain *old_domain;
3762
3763 old_domain = find_domain(pdev);
3764 if (old_domain) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003765 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
3766 dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)
3767 domain_remove_one_dev_info(old_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003768 else
3769 domain_remove_dev_info(old_domain);
3770 }
3771 }
3772
David Woodhouse276dbf992009-04-04 01:45:37 +01003773 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3774 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003775 if (!iommu)
3776 return -ENODEV;
3777
3778 /* check if this iommu agaw is sufficient for max mapped address */
3779 addr_width = agaw_to_width(iommu->agaw);
Tom Lyona99c47a2010-05-17 08:20:45 +01003780 if (addr_width > cap_mgaw(iommu->cap))
3781 addr_width = cap_mgaw(iommu->cap);
3782
3783 if (dmar_domain->max_addr > (1LL << addr_width)) {
3784 printk(KERN_ERR "%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003785 "sufficient for the mapped address (%llx)\n",
Tom Lyona99c47a2010-05-17 08:20:45 +01003786 __func__, addr_width, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003787 return -EFAULT;
3788 }
Tom Lyona99c47a2010-05-17 08:20:45 +01003789 dmar_domain->gaw = addr_width;
3790
3791 /*
3792 * Knock out extra levels of page tables if necessary
3793 */
3794 while (iommu->agaw < dmar_domain->agaw) {
3795 struct dma_pte *pte;
3796
3797 pte = dmar_domain->pgd;
3798 if (dma_pte_present(pte)) {
Sheng Yang25cbff12010-06-12 19:21:42 +08003799 dmar_domain->pgd = (struct dma_pte *)
3800 phys_to_virt(dma_pte_addr(pte));
Jan Kiszka7a661012010-11-02 08:05:51 +01003801 free_pgtable_page(pte);
Tom Lyona99c47a2010-05-17 08:20:45 +01003802 }
3803 dmar_domain->agaw--;
3804 }
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003805
David Woodhouse5fe60f42009-08-09 10:53:41 +01003806 return domain_add_dev_info(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003807}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003808
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003809static void intel_iommu_detach_device(struct iommu_domain *domain,
3810 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003811{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003812 struct dmar_domain *dmar_domain = domain->priv;
3813 struct pci_dev *pdev = to_pci_dev(dev);
3814
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003815 domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003816}
Kay, Allen M38717942008-09-09 18:37:29 +03003817
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003818static int intel_iommu_map(struct iommu_domain *domain,
3819 unsigned long iova, phys_addr_t hpa,
3820 int gfp_order, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003821{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003822 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003823 u64 max_addr;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003824 int prot = 0;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003825 size_t size;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003826 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003827
Joerg Roedeldde57a22008-12-03 15:04:09 +01003828 if (iommu_prot & IOMMU_READ)
3829 prot |= DMA_PTE_READ;
3830 if (iommu_prot & IOMMU_WRITE)
3831 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08003832 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3833 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003834
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003835 size = PAGE_SIZE << gfp_order;
David Woodhouse163cc522009-06-28 00:51:17 +01003836 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003837 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003838 u64 end;
3839
3840 /* check if minimum agaw is sufficient for mapped address */
Tom Lyon8954da12010-05-17 08:19:52 +01003841 end = __DOMAIN_MAX_ADDR(dmar_domain->gaw) + 1;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003842 if (end < max_addr) {
Tom Lyon8954da12010-05-17 08:19:52 +01003843 printk(KERN_ERR "%s: iommu width (%d) is not "
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003844 "sufficient for the mapped address (%llx)\n",
Tom Lyon8954da12010-05-17 08:19:52 +01003845 __func__, dmar_domain->gaw, max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003846 return -EFAULT;
3847 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003848 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003849 }
David Woodhousead051222009-06-28 14:22:28 +01003850 /* Round up size to next multiple of PAGE_SIZE, if it and
3851 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01003852 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01003853 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
3854 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003855 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003856}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003857
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003858static int intel_iommu_unmap(struct iommu_domain *domain,
3859 unsigned long iova, int gfp_order)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003860{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003861 struct dmar_domain *dmar_domain = domain->priv;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003862 size_t size = PAGE_SIZE << gfp_order;
Sheng Yang4b99d352009-07-08 11:52:52 +01003863
David Woodhouse163cc522009-06-28 00:51:17 +01003864 dma_pte_clear_range(dmar_domain, iova >> VTD_PAGE_SHIFT,
3865 (iova + size - 1) >> VTD_PAGE_SHIFT);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003866
David Woodhouse163cc522009-06-28 00:51:17 +01003867 if (dmar_domain->max_addr == iova + size)
3868 dmar_domain->max_addr = iova;
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003869
3870 return gfp_order;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003871}
Kay, Allen M38717942008-09-09 18:37:29 +03003872
Joerg Roedeld14d6572008-12-03 15:06:57 +01003873static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3874 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003875{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003876 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003877 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003878 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003879
Youquan Song6dd9a7c2011-05-25 19:13:49 +01003880 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, 0);
Kay, Allen M38717942008-09-09 18:37:29 +03003881 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003882 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003883
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003884 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003885}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003886
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003887static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3888 unsigned long cap)
3889{
3890 struct dmar_domain *dmar_domain = domain->priv;
3891
3892 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3893 return dmar_domain->iommu_snooping;
Tom Lyon323f99c2010-07-02 16:56:14 -04003894 if (cap == IOMMU_CAP_INTR_REMAP)
3895 return intr_remapping_enabled;
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003896
3897 return 0;
3898}
3899
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003900static struct iommu_ops intel_iommu_ops = {
3901 .domain_init = intel_iommu_domain_init,
3902 .domain_destroy = intel_iommu_domain_destroy,
3903 .attach_dev = intel_iommu_attach_device,
3904 .detach_dev = intel_iommu_detach_device,
Joerg Roedelb146a1c9f2010-01-20 17:17:37 +01003905 .map = intel_iommu_map,
3906 .unmap = intel_iommu_unmap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003907 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003908 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003909};
David Woodhouse9af88142009-02-13 23:18:03 +00003910
3911static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3912{
3913 /*
3914 * Mobile 4 Series Chipset neglects to set RWBF capability,
3915 * but needs it:
3916 */
3917 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3918 rwbf_quirk = 1;
David Woodhouse2d9e6672010-06-15 10:57:57 +01003919
3920 /* https://bugzilla.redhat.com/show_bug.cgi?id=538163 */
3921 if (dev->revision == 0x07) {
3922 printk(KERN_INFO "DMAR: Disabling IOMMU for graphics on this chipset\n");
3923 dmar_map_gfx = 0;
3924 }
David Woodhouse9af88142009-02-13 23:18:03 +00003925}
3926
3927DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);
David Woodhousee0fc7e02009-09-30 09:12:17 -07003928
Adam Jacksoneecfd572010-08-25 21:17:34 +01003929#define GGC 0x52
3930#define GGC_MEMORY_SIZE_MASK (0xf << 8)
3931#define GGC_MEMORY_SIZE_NONE (0x0 << 8)
3932#define GGC_MEMORY_SIZE_1M (0x1 << 8)
3933#define GGC_MEMORY_SIZE_2M (0x3 << 8)
3934#define GGC_MEMORY_VT_ENABLED (0x8 << 8)
3935#define GGC_MEMORY_SIZE_2M_VT (0x9 << 8)
3936#define GGC_MEMORY_SIZE_3M_VT (0xa << 8)
3937#define GGC_MEMORY_SIZE_4M_VT (0xb << 8)
3938
David Woodhouse9eecabc2010-09-21 22:28:23 +01003939static void __devinit quirk_calpella_no_shadow_gtt(struct pci_dev *dev)
3940{
3941 unsigned short ggc;
3942
Adam Jacksoneecfd572010-08-25 21:17:34 +01003943 if (pci_read_config_word(dev, GGC, &ggc))
David Woodhouse9eecabc2010-09-21 22:28:23 +01003944 return;
3945
Adam Jacksoneecfd572010-08-25 21:17:34 +01003946 if (!(ggc & GGC_MEMORY_VT_ENABLED)) {
David Woodhouse9eecabc2010-09-21 22:28:23 +01003947 printk(KERN_INFO "DMAR: BIOS has allocated no shadow GTT; disabling IOMMU for graphics\n");
3948 dmar_map_gfx = 0;
3949 }
3950}
3951DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0040, quirk_calpella_no_shadow_gtt);
3952DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0044, quirk_calpella_no_shadow_gtt);
3953DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x0062, quirk_calpella_no_shadow_gtt);
3954DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x006a, quirk_calpella_no_shadow_gtt);
3955
David Woodhousee0fc7e02009-09-30 09:12:17 -07003956/* On Tylersburg chipsets, some BIOSes have been known to enable the
3957 ISOCH DMAR unit for the Azalia sound device, but not give it any
3958 TLB entries, which causes it to deadlock. Check for that. We do
3959 this in a function called from init_dmars(), instead of in a PCI
3960 quirk, because we don't want to print the obnoxious "BIOS broken"
3961 message if VT-d is actually disabled.
3962*/
3963static void __init check_tylersburg_isoch(void)
3964{
3965 struct pci_dev *pdev;
3966 uint32_t vtisochctrl;
3967
3968 /* If there's no Azalia in the system anyway, forget it. */
3969 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x3a3e, NULL);
3970 if (!pdev)
3971 return;
3972 pci_dev_put(pdev);
3973
3974 /* System Management Registers. Might be hidden, in which case
3975 we can't do the sanity check. But that's OK, because the
3976 known-broken BIOSes _don't_ actually hide it, so far. */
3977 pdev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x342e, NULL);
3978 if (!pdev)
3979 return;
3980
3981 if (pci_read_config_dword(pdev, 0x188, &vtisochctrl)) {
3982 pci_dev_put(pdev);
3983 return;
3984 }
3985
3986 pci_dev_put(pdev);
3987
3988 /* If Azalia DMA is routed to the non-isoch DMAR unit, fine. */
3989 if (vtisochctrl & 1)
3990 return;
3991
3992 /* Drop all bits other than the number of TLB entries */
3993 vtisochctrl &= 0x1c;
3994
3995 /* If we have the recommended number of TLB entries (16), fine. */
3996 if (vtisochctrl == 0x10)
3997 return;
3998
3999 /* Zero TLB entries? You get to ride the short bus to school. */
4000 if (!vtisochctrl) {
4001 WARN(1, "Your BIOS is broken; DMA routed to ISOCH DMAR unit but no TLB space.\n"
4002 "BIOS vendor: %s; Ver: %s; Product Version: %s\n",
4003 dmi_get_system_info(DMI_BIOS_VENDOR),
4004 dmi_get_system_info(DMI_BIOS_VERSION),
4005 dmi_get_system_info(DMI_PRODUCT_VERSION));
4006 iommu_identity_mapping |= IDENTMAP_AZALIA;
4007 return;
4008 }
4009
4010 printk(KERN_WARNING "DMAR: Recommended TLB entries for ISOCH unit is 16; your BIOS set %d\n",
4011 vtisochctrl);
4012}