blob: d4217f7371594afb1609cb35a0e0b3b25fc18b1a [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>
Fenghua Yuf59c7b62009-03-27 14:22:42 -070039#include <linux/sysdev.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070040#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090041#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070042#include "pci.h"
43
Fenghua Yu5b6985c2008-10-16 18:02:32 -070044#define ROOT_SIZE VTD_PAGE_SIZE
45#define CONTEXT_SIZE VTD_PAGE_SIZE
46
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070047#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
48#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
49
50#define IOAPIC_RANGE_START (0xfee00000)
51#define IOAPIC_RANGE_END (0xfeefffff)
52#define IOVA_START_ADDR (0x1000)
53
54#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
55
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070056#define MAX_AGAW_WIDTH 64
57
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070058#define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
59
Mark McLoughlinf27be032008-11-20 15:49:43 +000060#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070061#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070062#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080063
David Woodhousefd18de52009-05-10 23:57:41 +010064#ifndef PHYSICAL_PAGE_MASK
65#define PHYSICAL_PAGE_MASK PAGE_MASK
66#endif
67
David Woodhousedd4e8312009-06-27 16:21:20 +010068/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
69 are never going to work. */
70static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
71{
72 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
73}
74
75static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
76{
77 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
78}
79static inline unsigned long page_to_dma_pfn(struct page *pg)
80{
81 return mm_to_dma_pfn(page_to_pfn(pg));
82}
83static inline unsigned long virt_to_dma_pfn(void *p)
84{
85 return page_to_dma_pfn(virt_to_page(p));
86}
87
Weidong Hand9630fe2008-12-08 11:06:32 +080088/* global iommu list, set NULL for ignored DMAR units */
89static struct intel_iommu **g_iommus;
90
David Woodhouse9af88142009-02-13 23:18:03 +000091static int rwbf_quirk;
92
Mark McLoughlin46b08e12008-11-20 15:49:44 +000093/*
94 * 0: Present
95 * 1-11: Reserved
96 * 12-63: Context Ptr (12 - (haw-1))
97 * 64-127: Reserved
98 */
99struct root_entry {
100 u64 val;
101 u64 rsvd1;
102};
103#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
104static inline bool root_present(struct root_entry *root)
105{
106 return (root->val & 1);
107}
108static inline void set_root_present(struct root_entry *root)
109{
110 root->val |= 1;
111}
112static inline void set_root_value(struct root_entry *root, unsigned long value)
113{
114 root->val |= value & VTD_PAGE_MASK;
115}
116
117static inline struct context_entry *
118get_context_addr_from_root(struct root_entry *root)
119{
120 return (struct context_entry *)
121 (root_present(root)?phys_to_virt(
122 root->val & VTD_PAGE_MASK) :
123 NULL);
124}
125
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000126/*
127 * low 64 bits:
128 * 0: present
129 * 1: fault processing disable
130 * 2-3: translation type
131 * 12-63: address space root
132 * high 64 bits:
133 * 0-2: address width
134 * 3-6: aval
135 * 8-23: domain id
136 */
137struct context_entry {
138 u64 lo;
139 u64 hi;
140};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000141
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000142static inline bool context_present(struct context_entry *context)
143{
144 return (context->lo & 1);
145}
146static inline void context_set_present(struct context_entry *context)
147{
148 context->lo |= 1;
149}
150
151static inline void context_set_fault_enable(struct context_entry *context)
152{
153 context->lo &= (((u64)-1) << 2) | 1;
154}
155
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000156static inline void context_set_translation_type(struct context_entry *context,
157 unsigned long value)
158{
159 context->lo &= (((u64)-1) << 4) | 3;
160 context->lo |= (value & 3) << 2;
161}
162
163static inline void context_set_address_root(struct context_entry *context,
164 unsigned long value)
165{
166 context->lo |= value & VTD_PAGE_MASK;
167}
168
169static inline void context_set_address_width(struct context_entry *context,
170 unsigned long value)
171{
172 context->hi |= value & 7;
173}
174
175static inline void context_set_domain_id(struct context_entry *context,
176 unsigned long value)
177{
178 context->hi |= (value & ((1 << 16) - 1)) << 8;
179}
180
181static inline void context_clear_entry(struct context_entry *context)
182{
183 context->lo = 0;
184 context->hi = 0;
185}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000186
Mark McLoughlin622ba122008-11-20 15:49:46 +0000187/*
188 * 0: readable
189 * 1: writable
190 * 2-6: reserved
191 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800192 * 8-10: available
193 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000194 * 12-63: Host physcial address
195 */
196struct dma_pte {
197 u64 val;
198};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000199
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000200static inline void dma_clear_pte(struct dma_pte *pte)
201{
202 pte->val = 0;
203}
204
205static inline void dma_set_pte_readable(struct dma_pte *pte)
206{
207 pte->val |= DMA_PTE_READ;
208}
209
210static inline void dma_set_pte_writable(struct dma_pte *pte)
211{
212 pte->val |= DMA_PTE_WRITE;
213}
214
Sheng Yang9cf06692009-03-18 15:33:07 +0800215static inline void dma_set_pte_snp(struct dma_pte *pte)
216{
217 pte->val |= DMA_PTE_SNP;
218}
219
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000220static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
221{
222 pte->val = (pte->val & ~3) | (prot & 3);
223}
224
225static inline u64 dma_pte_addr(struct dma_pte *pte)
226{
227 return (pte->val & VTD_PAGE_MASK);
228}
229
David Woodhousedd4e8312009-06-27 16:21:20 +0100230static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn)
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000231{
David Woodhousedd4e8312009-06-27 16:21:20 +0100232 pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000233}
234
235static inline bool dma_pte_present(struct dma_pte *pte)
236{
237 return (pte->val & 3) != 0;
238}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000239
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700240/*
241 * This domain is a statically identity mapping domain.
242 * 1. This domain creats a static 1:1 mapping to all usable memory.
243 * 2. It maps to each iommu if successful.
244 * 3. Each iommu mapps to this domain if successful.
245 */
246struct dmar_domain *si_domain;
247
Weidong Han3b5410e2008-12-08 09:17:15 +0800248/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100249#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800250
Weidong Han1ce28fe2008-12-08 16:35:39 +0800251/* domain represents a virtual machine, more than one devices
252 * across iommus may be owned in one domain, e.g. kvm guest.
253 */
254#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
255
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700256/* si_domain contains mulitple devices */
257#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2)
258
Mark McLoughlin99126f72008-11-20 15:49:47 +0000259struct dmar_domain {
260 int id; /* domain id */
Weidong Han8c11e792008-12-08 15:29:22 +0800261 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000262
263 struct list_head devices; /* all devices' list */
264 struct iova_domain iovad; /* iova's that belong to this domain */
265
266 struct dma_pte *pgd; /* virtual address */
267 spinlock_t mapping_lock; /* page table lock */
268 int gaw; /* max guest address width */
269
270 /* adjusted guest address width, 0 is level 2 30-bit */
271 int agaw;
272
Weidong Han3b5410e2008-12-08 09:17:15 +0800273 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800274
275 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800276 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800277 int iommu_count; /* reference count of iommu */
278 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800279 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000280};
281
Mark McLoughlina647dac2008-11-20 15:49:48 +0000282/* PCI domain-device relationship */
283struct device_domain_info {
284 struct list_head link; /* link to domain siblings */
285 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100286 int segment; /* PCI domain */
287 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000288 u8 devfn; /* PCI devfn number */
289 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800290 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000291 struct dmar_domain *domain; /* pointer to domain */
292};
293
mark gross5e0d2a62008-03-04 15:22:08 -0800294static void flush_unmaps_timeout(unsigned long data);
295
296DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
297
mark gross80b20dd2008-04-18 13:53:58 -0700298#define HIGH_WATER_MARK 250
299struct deferred_flush_tables {
300 int next;
301 struct iova *iova[HIGH_WATER_MARK];
302 struct dmar_domain *domain[HIGH_WATER_MARK];
303};
304
305static struct deferred_flush_tables *deferred_flush;
306
mark gross5e0d2a62008-03-04 15:22:08 -0800307/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800308static int g_num_of_iommus;
309
310static DEFINE_SPINLOCK(async_umap_flush_lock);
311static LIST_HEAD(unmaps_to_do);
312
313static int timer_on;
314static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800315
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700316static void domain_remove_dev_info(struct dmar_domain *domain);
317
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800318#ifdef CONFIG_DMAR_DEFAULT_ON
319int dmar_disabled = 0;
320#else
321int dmar_disabled = 1;
322#endif /*CONFIG_DMAR_DEFAULT_ON*/
323
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700324static int __initdata dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700325static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800326static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700327
328#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
329static DEFINE_SPINLOCK(device_domain_lock);
330static LIST_HEAD(device_domain_list);
331
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100332static struct iommu_ops intel_iommu_ops;
333
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700334static int __init intel_iommu_setup(char *str)
335{
336 if (!str)
337 return -EINVAL;
338 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800339 if (!strncmp(str, "on", 2)) {
340 dmar_disabled = 0;
341 printk(KERN_INFO "Intel-IOMMU: enabled\n");
342 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700343 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800344 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700345 } else if (!strncmp(str, "igfx_off", 8)) {
346 dmar_map_gfx = 0;
347 printk(KERN_INFO
348 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700349 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800350 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700351 "Intel-IOMMU: Forcing DAC for PCI devices\n");
352 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800353 } else if (!strncmp(str, "strict", 6)) {
354 printk(KERN_INFO
355 "Intel-IOMMU: disable batched IOTLB flush\n");
356 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700357 }
358
359 str += strcspn(str, ",");
360 while (*str == ',')
361 str++;
362 }
363 return 0;
364}
365__setup("intel_iommu=", intel_iommu_setup);
366
367static struct kmem_cache *iommu_domain_cache;
368static struct kmem_cache *iommu_devinfo_cache;
369static struct kmem_cache *iommu_iova_cache;
370
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700371static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
372{
373 unsigned int flags;
374 void *vaddr;
375
376 /* trying to avoid low memory issues */
377 flags = current->flags & PF_MEMALLOC;
378 current->flags |= PF_MEMALLOC;
379 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
380 current->flags &= (~PF_MEMALLOC | flags);
381 return vaddr;
382}
383
384
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700385static inline void *alloc_pgtable_page(void)
386{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700387 unsigned int flags;
388 void *vaddr;
389
390 /* trying to avoid low memory issues */
391 flags = current->flags & PF_MEMALLOC;
392 current->flags |= PF_MEMALLOC;
393 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
394 current->flags &= (~PF_MEMALLOC | flags);
395 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700396}
397
398static inline void free_pgtable_page(void *vaddr)
399{
400 free_page((unsigned long)vaddr);
401}
402
403static inline void *alloc_domain_mem(void)
404{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700405 return iommu_kmem_cache_alloc(iommu_domain_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700406}
407
Kay, Allen M38717942008-09-09 18:37:29 +0300408static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700409{
410 kmem_cache_free(iommu_domain_cache, vaddr);
411}
412
413static inline void * alloc_devinfo_mem(void)
414{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700415 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700416}
417
418static inline void free_devinfo_mem(void *vaddr)
419{
420 kmem_cache_free(iommu_devinfo_cache, vaddr);
421}
422
423struct iova *alloc_iova_mem(void)
424{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700425 return iommu_kmem_cache_alloc(iommu_iova_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700426}
427
428void free_iova_mem(struct iova *iova)
429{
430 kmem_cache_free(iommu_iova_cache, iova);
431}
432
Weidong Han1b573682008-12-08 15:34:06 +0800433
434static inline int width_to_agaw(int width);
435
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700436static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800437{
438 unsigned long sagaw;
439 int agaw = -1;
440
441 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700442 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800443 agaw >= 0; agaw--) {
444 if (test_bit(agaw, &sagaw))
445 break;
446 }
447
448 return agaw;
449}
450
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700451/*
452 * Calculate max SAGAW for each iommu.
453 */
454int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
455{
456 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
457}
458
459/*
460 * calculate agaw for each iommu.
461 * "SAGAW" may be different across iommus, use a default agaw, and
462 * get a supported less agaw for iommus that don't support the default agaw.
463 */
464int iommu_calculate_agaw(struct intel_iommu *iommu)
465{
466 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
467}
468
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700469/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800470static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
471{
472 int iommu_id;
473
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700474 /* si_domain and vm domain should not get here. */
Weidong Han1ce28fe2008-12-08 16:35:39 +0800475 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700476 BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
Weidong Han1ce28fe2008-12-08 16:35:39 +0800477
Weidong Han8c11e792008-12-08 15:29:22 +0800478 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
479 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
480 return NULL;
481
482 return g_iommus[iommu_id];
483}
484
Weidong Han8e6040972008-12-08 15:49:06 +0800485static void domain_update_iommu_coherency(struct dmar_domain *domain)
486{
487 int i;
488
489 domain->iommu_coherency = 1;
490
491 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
492 for (; i < g_num_of_iommus; ) {
493 if (!ecap_coherent(g_iommus[i]->ecap)) {
494 domain->iommu_coherency = 0;
495 break;
496 }
497 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
498 }
499}
500
Sheng Yang58c610b2009-03-18 15:33:05 +0800501static void domain_update_iommu_snooping(struct dmar_domain *domain)
502{
503 int i;
504
505 domain->iommu_snooping = 1;
506
507 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
508 for (; i < g_num_of_iommus; ) {
509 if (!ecap_sc_support(g_iommus[i]->ecap)) {
510 domain->iommu_snooping = 0;
511 break;
512 }
513 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
514 }
515}
516
517/* Some capabilities may be different across iommus */
518static void domain_update_iommu_cap(struct dmar_domain *domain)
519{
520 domain_update_iommu_coherency(domain);
521 domain_update_iommu_snooping(domain);
522}
523
David Woodhouse276dbf992009-04-04 01:45:37 +0100524static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800525{
526 struct dmar_drhd_unit *drhd = NULL;
527 int i;
528
529 for_each_drhd_unit(drhd) {
530 if (drhd->ignored)
531 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100532 if (segment != drhd->segment)
533 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800534
David Woodhouse924b6232009-04-04 00:39:25 +0100535 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000536 if (drhd->devices[i] &&
537 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800538 drhd->devices[i]->devfn == devfn)
539 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700540 if (drhd->devices[i] &&
541 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100542 drhd->devices[i]->subordinate->number <= bus &&
543 drhd->devices[i]->subordinate->subordinate >= bus)
544 return drhd->iommu;
545 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800546
547 if (drhd->include_all)
548 return drhd->iommu;
549 }
550
551 return NULL;
552}
553
Weidong Han5331fe62008-12-08 23:00:00 +0800554static void domain_flush_cache(struct dmar_domain *domain,
555 void *addr, int size)
556{
557 if (!domain->iommu_coherency)
558 clflush_cache_range(addr, size);
559}
560
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700561/* Gets context entry for a given bus and devfn */
562static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
563 u8 bus, u8 devfn)
564{
565 struct root_entry *root;
566 struct context_entry *context;
567 unsigned long phy_addr;
568 unsigned long flags;
569
570 spin_lock_irqsave(&iommu->lock, flags);
571 root = &iommu->root_entry[bus];
572 context = get_context_addr_from_root(root);
573 if (!context) {
574 context = (struct context_entry *)alloc_pgtable_page();
575 if (!context) {
576 spin_unlock_irqrestore(&iommu->lock, flags);
577 return NULL;
578 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700579 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700580 phy_addr = virt_to_phys((void *)context);
581 set_root_value(root, phy_addr);
582 set_root_present(root);
583 __iommu_flush_cache(iommu, root, sizeof(*root));
584 }
585 spin_unlock_irqrestore(&iommu->lock, flags);
586 return &context[devfn];
587}
588
589static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
590{
591 struct root_entry *root;
592 struct context_entry *context;
593 int ret;
594 unsigned long flags;
595
596 spin_lock_irqsave(&iommu->lock, flags);
597 root = &iommu->root_entry[bus];
598 context = get_context_addr_from_root(root);
599 if (!context) {
600 ret = 0;
601 goto out;
602 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000603 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700604out:
605 spin_unlock_irqrestore(&iommu->lock, flags);
606 return ret;
607}
608
609static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
610{
611 struct root_entry *root;
612 struct context_entry *context;
613 unsigned long flags;
614
615 spin_lock_irqsave(&iommu->lock, flags);
616 root = &iommu->root_entry[bus];
617 context = get_context_addr_from_root(root);
618 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000619 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700620 __iommu_flush_cache(iommu, &context[devfn], \
621 sizeof(*context));
622 }
623 spin_unlock_irqrestore(&iommu->lock, flags);
624}
625
626static void free_context_table(struct intel_iommu *iommu)
627{
628 struct root_entry *root;
629 int i;
630 unsigned long flags;
631 struct context_entry *context;
632
633 spin_lock_irqsave(&iommu->lock, flags);
634 if (!iommu->root_entry) {
635 goto out;
636 }
637 for (i = 0; i < ROOT_ENTRY_NR; i++) {
638 root = &iommu->root_entry[i];
639 context = get_context_addr_from_root(root);
640 if (context)
641 free_pgtable_page(context);
642 }
643 free_pgtable_page(iommu->root_entry);
644 iommu->root_entry = NULL;
645out:
646 spin_unlock_irqrestore(&iommu->lock, flags);
647}
648
649/* page table handling */
650#define LEVEL_STRIDE (9)
651#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
652
653static inline int agaw_to_level(int agaw)
654{
655 return agaw + 2;
656}
657
658static inline int agaw_to_width(int agaw)
659{
660 return 30 + agaw * LEVEL_STRIDE;
661
662}
663
664static inline int width_to_agaw(int width)
665{
666 return (width - 30) / LEVEL_STRIDE;
667}
668
669static inline unsigned int level_to_offset_bits(int level)
670{
671 return (12 + (level - 1) * LEVEL_STRIDE);
672}
673
David Woodhouse77dfa562009-06-27 16:40:08 +0100674static inline int pfn_level_offset(unsigned long pfn, int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700675{
David Woodhouse77dfa562009-06-27 16:40:08 +0100676 return (pfn >> (level_to_offset_bits(level) - 12)) & LEVEL_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700677}
678
679static inline u64 level_mask(int level)
680{
681 return ((u64)-1 << level_to_offset_bits(level));
682}
683
684static inline u64 level_size(int level)
685{
686 return ((u64)1 << level_to_offset_bits(level));
687}
688
689static inline u64 align_to_level(u64 addr, int level)
690{
691 return ((addr + level_size(level) - 1) & level_mask(level));
692}
693
694static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
695{
696 int addr_width = agaw_to_width(domain->agaw);
697 struct dma_pte *parent, *pte = NULL;
698 int level = agaw_to_level(domain->agaw);
699 int offset;
700 unsigned long flags;
701
702 BUG_ON(!domain->pgd);
David Woodhouse66eae842009-06-27 19:00:32 +0100703 BUG_ON(addr >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700704 parent = domain->pgd;
705
706 spin_lock_irqsave(&domain->mapping_lock, flags);
707 while (level > 0) {
708 void *tmp_page;
709
David Woodhouse77dfa562009-06-27 16:40:08 +0100710 offset = pfn_level_offset(addr >> VTD_PAGE_SHIFT, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700711 pte = &parent[offset];
712 if (level == 1)
713 break;
714
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000715 if (!dma_pte_present(pte)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700716 tmp_page = alloc_pgtable_page();
717
718 if (!tmp_page) {
719 spin_unlock_irqrestore(&domain->mapping_lock,
720 flags);
721 return NULL;
722 }
Weidong Han5331fe62008-12-08 23:00:00 +0800723 domain_flush_cache(domain, tmp_page, PAGE_SIZE);
David Woodhousedd4e8312009-06-27 16:21:20 +0100724 dma_set_pte_pfn(pte, virt_to_dma_pfn(tmp_page));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700725 /*
726 * high level table always sets r/w, last level page
727 * table control read/write
728 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000729 dma_set_pte_readable(pte);
730 dma_set_pte_writable(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800731 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700732 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000733 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700734 level--;
735 }
736
737 spin_unlock_irqrestore(&domain->mapping_lock, flags);
738 return pte;
739}
740
741/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100742static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
743 unsigned long pfn,
744 int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700745{
746 struct dma_pte *parent, *pte = NULL;
747 int total = agaw_to_level(domain->agaw);
748 int offset;
749
750 parent = domain->pgd;
751 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100752 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700753 pte = &parent[offset];
754 if (level == total)
755 return pte;
756
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000757 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700758 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000759 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700760 total--;
761 }
762 return NULL;
763}
764
765/* clear one page's page table */
David Woodhousea75f7cf2009-06-27 17:44:39 +0100766static void dma_pte_clear_one(struct dmar_domain *domain, unsigned long pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700767{
768 struct dma_pte *pte = NULL;
769
770 /* get last level pte */
David Woodhousea75f7cf2009-06-27 17:44:39 +0100771 pte = dma_pfn_level_pte(domain, pfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700772
773 if (pte) {
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000774 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800775 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700776 }
777}
778
779/* clear last level pte, a tlb flush should be followed */
780static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
781{
David Woodhouse04b18e62009-06-27 19:15:01 +0100782 unsigned long start_pfn = IOVA_PFN(start);
783 unsigned long end_pfn = IOVA_PFN(end-1);
784 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700785
David Woodhouse04b18e62009-06-27 19:15:01 +0100786 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
787 BUG_ON(addr_width < BITS_PER_LONG && end_pfn >> addr_width);
David Woodhouse66eae842009-06-27 19:00:32 +0100788
David Woodhouse04b18e62009-06-27 19:15:01 +0100789 /* we don't need lock here; nobody else touches the iova range */
790 while (start_pfn <= end_pfn) {
791 dma_pte_clear_one(domain, start_pfn);
792 start_pfn++;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700793 }
794}
795
796/* free page table pages. last level pte should already be cleared */
797static void dma_pte_free_pagetable(struct dmar_domain *domain,
798 u64 start, u64 end)
799{
800 int addr_width = agaw_to_width(domain->agaw);
801 struct dma_pte *pte;
802 int total = agaw_to_level(domain->agaw);
803 int level;
804 u64 tmp;
805
David Woodhouse66eae842009-06-27 19:00:32 +0100806 BUG_ON(start >> addr_width);
807 BUG_ON(end >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700808
809 /* we don't need lock here, nobody else touches the iova range */
810 level = 2;
811 while (level <= total) {
812 tmp = align_to_level(start, level);
813 if (tmp >= end || (tmp + level_size(level) > end))
814 return;
815
816 while (tmp < end) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100817 pte = dma_pfn_level_pte(domain, tmp >> VTD_PAGE_SHIFT,
818 level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700819 if (pte) {
820 free_pgtable_page(
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000821 phys_to_virt(dma_pte_addr(pte)));
822 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800823 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700824 }
825 tmp += level_size(level);
826 }
827 level++;
828 }
829 /* free pgd */
830 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
831 free_pgtable_page(domain->pgd);
832 domain->pgd = NULL;
833 }
834}
835
836/* iommu handling */
837static int iommu_alloc_root_entry(struct intel_iommu *iommu)
838{
839 struct root_entry *root;
840 unsigned long flags;
841
842 root = (struct root_entry *)alloc_pgtable_page();
843 if (!root)
844 return -ENOMEM;
845
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700846 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700847
848 spin_lock_irqsave(&iommu->lock, flags);
849 iommu->root_entry = root;
850 spin_unlock_irqrestore(&iommu->lock, flags);
851
852 return 0;
853}
854
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700855static void iommu_set_root_entry(struct intel_iommu *iommu)
856{
857 void *addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100858 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700859 unsigned long flag;
860
861 addr = iommu->root_entry;
862
863 spin_lock_irqsave(&iommu->register_lock, flag);
864 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
865
David Woodhousec416daa2009-05-10 20:30:58 +0100866 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700867
868 /* Make sure hardware complete it */
869 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100870 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700871
872 spin_unlock_irqrestore(&iommu->register_lock, flag);
873}
874
875static void iommu_flush_write_buffer(struct intel_iommu *iommu)
876{
877 u32 val;
878 unsigned long flag;
879
David Woodhouse9af88142009-02-13 23:18:03 +0000880 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700881 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700882
883 spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +0100884 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700885
886 /* Make sure hardware complete it */
887 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100888 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700889
890 spin_unlock_irqrestore(&iommu->register_lock, flag);
891}
892
893/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +0100894static void __iommu_flush_context(struct intel_iommu *iommu,
895 u16 did, u16 source_id, u8 function_mask,
896 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700897{
898 u64 val = 0;
899 unsigned long flag;
900
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700901 switch (type) {
902 case DMA_CCMD_GLOBAL_INVL:
903 val = DMA_CCMD_GLOBAL_INVL;
904 break;
905 case DMA_CCMD_DOMAIN_INVL:
906 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
907 break;
908 case DMA_CCMD_DEVICE_INVL:
909 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
910 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
911 break;
912 default:
913 BUG();
914 }
915 val |= DMA_CCMD_ICC;
916
917 spin_lock_irqsave(&iommu->register_lock, flag);
918 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
919
920 /* Make sure hardware complete it */
921 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
922 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
923
924 spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700925}
926
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700927/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100928static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
929 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700930{
931 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
932 u64 val = 0, val_iva = 0;
933 unsigned long flag;
934
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700935 switch (type) {
936 case DMA_TLB_GLOBAL_FLUSH:
937 /* global flush doesn't need set IVA_REG */
938 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
939 break;
940 case DMA_TLB_DSI_FLUSH:
941 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
942 break;
943 case DMA_TLB_PSI_FLUSH:
944 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
945 /* Note: always flush non-leaf currently */
946 val_iva = size_order | addr;
947 break;
948 default:
949 BUG();
950 }
951 /* Note: set drain read/write */
952#if 0
953 /*
954 * This is probably to be super secure.. Looks like we can
955 * ignore it without any impact.
956 */
957 if (cap_read_drain(iommu->cap))
958 val |= DMA_TLB_READ_DRAIN;
959#endif
960 if (cap_write_drain(iommu->cap))
961 val |= DMA_TLB_WRITE_DRAIN;
962
963 spin_lock_irqsave(&iommu->register_lock, flag);
964 /* Note: Only uses first TLB reg currently */
965 if (val_iva)
966 dmar_writeq(iommu->reg + tlb_offset, val_iva);
967 dmar_writeq(iommu->reg + tlb_offset + 8, val);
968
969 /* Make sure hardware complete it */
970 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
971 dmar_readq, (!(val & DMA_TLB_IVT)), val);
972
973 spin_unlock_irqrestore(&iommu->register_lock, flag);
974
975 /* check IOTLB invalidation granularity */
976 if (DMA_TLB_IAIG(val) == 0)
977 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
978 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
979 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700980 (unsigned long long)DMA_TLB_IIRG(type),
981 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700982}
983
Yu Zhao93a23a72009-05-18 13:51:37 +0800984static struct device_domain_info *iommu_support_dev_iotlb(
985 struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700986{
Yu Zhao93a23a72009-05-18 13:51:37 +0800987 int found = 0;
988 unsigned long flags;
989 struct device_domain_info *info;
990 struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);
991
992 if (!ecap_dev_iotlb_support(iommu->ecap))
993 return NULL;
994
995 if (!iommu->qi)
996 return NULL;
997
998 spin_lock_irqsave(&device_domain_lock, flags);
999 list_for_each_entry(info, &domain->devices, link)
1000 if (info->bus == bus && info->devfn == devfn) {
1001 found = 1;
1002 break;
1003 }
1004 spin_unlock_irqrestore(&device_domain_lock, flags);
1005
1006 if (!found || !info->dev)
1007 return NULL;
1008
1009 if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
1010 return NULL;
1011
1012 if (!dmar_find_matched_atsr_unit(info->dev))
1013 return NULL;
1014
1015 info->iommu = iommu;
1016
1017 return info;
1018}
1019
1020static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1021{
1022 if (!info)
1023 return;
1024
1025 pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
1026}
1027
1028static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1029{
1030 if (!info->dev || !pci_ats_enabled(info->dev))
1031 return;
1032
1033 pci_disable_ats(info->dev);
1034}
1035
1036static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1037 u64 addr, unsigned mask)
1038{
1039 u16 sid, qdep;
1040 unsigned long flags;
1041 struct device_domain_info *info;
1042
1043 spin_lock_irqsave(&device_domain_lock, flags);
1044 list_for_each_entry(info, &domain->devices, link) {
1045 if (!info->dev || !pci_ats_enabled(info->dev))
1046 continue;
1047
1048 sid = info->bus << 8 | info->devfn;
1049 qdep = pci_ats_queue_depth(info->dev);
1050 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1051 }
1052 spin_unlock_irqrestore(&device_domain_lock, flags);
1053}
1054
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001055static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
1056 u64 addr, unsigned int pages)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001057{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001058 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001059
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001060 BUG_ON(addr & (~VTD_PAGE_MASK));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001061 BUG_ON(pages == 0);
1062
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001063 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001064 * Fallback to domain selective flush if no PSI support or the size is
1065 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001066 * PSI requires page size to be 2 ^ x, and the base address is naturally
1067 * aligned to the size
1068 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001069 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1070 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001071 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001072 else
1073 iommu->flush.flush_iotlb(iommu, did, addr, mask,
1074 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001075
1076 /*
1077 * In caching mode, domain ID 0 is reserved for non-present to present
1078 * mapping flush. Device IOTLB doesn't need to be flushed in this case.
1079 */
1080 if (!cap_caching_mode(iommu->cap) || did)
Yu Zhao93a23a72009-05-18 13:51:37 +08001081 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001082}
1083
mark grossf8bab732008-02-08 04:18:38 -08001084static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1085{
1086 u32 pmen;
1087 unsigned long flags;
1088
1089 spin_lock_irqsave(&iommu->register_lock, flags);
1090 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1091 pmen &= ~DMA_PMEN_EPM;
1092 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1093
1094 /* wait for the protected region status bit to clear */
1095 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1096 readl, !(pmen & DMA_PMEN_PRS), pmen);
1097
1098 spin_unlock_irqrestore(&iommu->register_lock, flags);
1099}
1100
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001101static int iommu_enable_translation(struct intel_iommu *iommu)
1102{
1103 u32 sts;
1104 unsigned long flags;
1105
1106 spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001107 iommu->gcmd |= DMA_GCMD_TE;
1108 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001109
1110 /* Make sure hardware complete it */
1111 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001112 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001113
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001114 spin_unlock_irqrestore(&iommu->register_lock, flags);
1115 return 0;
1116}
1117
1118static int iommu_disable_translation(struct intel_iommu *iommu)
1119{
1120 u32 sts;
1121 unsigned long flag;
1122
1123 spin_lock_irqsave(&iommu->register_lock, flag);
1124 iommu->gcmd &= ~DMA_GCMD_TE;
1125 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1126
1127 /* Make sure hardware complete it */
1128 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001129 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001130
1131 spin_unlock_irqrestore(&iommu->register_lock, flag);
1132 return 0;
1133}
1134
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001135
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001136static int iommu_init_domains(struct intel_iommu *iommu)
1137{
1138 unsigned long ndomains;
1139 unsigned long nlongs;
1140
1141 ndomains = cap_ndoms(iommu->cap);
1142 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1143 nlongs = BITS_TO_LONGS(ndomains);
1144
1145 /* TBD: there might be 64K domains,
1146 * consider other allocation for future chip
1147 */
1148 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1149 if (!iommu->domain_ids) {
1150 printk(KERN_ERR "Allocating domain id array failed\n");
1151 return -ENOMEM;
1152 }
1153 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1154 GFP_KERNEL);
1155 if (!iommu->domains) {
1156 printk(KERN_ERR "Allocating domain array failed\n");
1157 kfree(iommu->domain_ids);
1158 return -ENOMEM;
1159 }
1160
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001161 spin_lock_init(&iommu->lock);
1162
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001163 /*
1164 * if Caching mode is set, then invalid translations are tagged
1165 * with domainid 0. Hence we need to pre-allocate it.
1166 */
1167 if (cap_caching_mode(iommu->cap))
1168 set_bit(0, iommu->domain_ids);
1169 return 0;
1170}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001171
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001172
1173static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001174static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001175
1176void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001177{
1178 struct dmar_domain *domain;
1179 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001180 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001181
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001182 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1183 for (; i < cap_ndoms(iommu->cap); ) {
1184 domain = iommu->domains[i];
1185 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001186
1187 spin_lock_irqsave(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001188 if (--domain->iommu_count == 0) {
1189 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1190 vm_domain_exit(domain);
1191 else
1192 domain_exit(domain);
1193 }
Weidong Hanc7151a82008-12-08 22:51:37 +08001194 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1195
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001196 i = find_next_bit(iommu->domain_ids,
1197 cap_ndoms(iommu->cap), i+1);
1198 }
1199
1200 if (iommu->gcmd & DMA_GCMD_TE)
1201 iommu_disable_translation(iommu);
1202
1203 if (iommu->irq) {
1204 set_irq_data(iommu->irq, NULL);
1205 /* This will mask the irq */
1206 free_irq(iommu->irq, iommu);
1207 destroy_irq(iommu->irq);
1208 }
1209
1210 kfree(iommu->domains);
1211 kfree(iommu->domain_ids);
1212
Weidong Hand9630fe2008-12-08 11:06:32 +08001213 g_iommus[iommu->seq_id] = NULL;
1214
1215 /* if all iommus are freed, free g_iommus */
1216 for (i = 0; i < g_num_of_iommus; i++) {
1217 if (g_iommus[i])
1218 break;
1219 }
1220
1221 if (i == g_num_of_iommus)
1222 kfree(g_iommus);
1223
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001224 /* free context mapping */
1225 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001226}
1227
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001228static struct dmar_domain *alloc_domain(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001229{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001230 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001231
1232 domain = alloc_domain_mem();
1233 if (!domain)
1234 return NULL;
1235
Weidong Han8c11e792008-12-08 15:29:22 +08001236 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
Weidong Hand71a2f32008-12-07 21:13:41 +08001237 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001238
1239 return domain;
1240}
1241
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001242static int iommu_attach_domain(struct dmar_domain *domain,
1243 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001244{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001245 int num;
1246 unsigned long ndomains;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001247 unsigned long flags;
1248
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001249 ndomains = cap_ndoms(iommu->cap);
Weidong Han8c11e792008-12-08 15:29:22 +08001250
1251 spin_lock_irqsave(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001252
1253 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1254 if (num >= ndomains) {
1255 spin_unlock_irqrestore(&iommu->lock, flags);
1256 printk(KERN_ERR "IOMMU: no free domain ids\n");
1257 return -ENOMEM;
1258 }
1259
1260 domain->id = num;
1261 set_bit(num, iommu->domain_ids);
1262 set_bit(iommu->seq_id, &domain->iommu_bmp);
1263 iommu->domains[num] = domain;
1264 spin_unlock_irqrestore(&iommu->lock, flags);
1265
1266 return 0;
1267}
1268
1269static void iommu_detach_domain(struct dmar_domain *domain,
1270 struct intel_iommu *iommu)
1271{
1272 unsigned long flags;
1273 int num, ndomains;
1274 int found = 0;
1275
1276 spin_lock_irqsave(&iommu->lock, flags);
1277 ndomains = cap_ndoms(iommu->cap);
1278 num = find_first_bit(iommu->domain_ids, ndomains);
1279 for (; num < ndomains; ) {
1280 if (iommu->domains[num] == domain) {
1281 found = 1;
1282 break;
1283 }
1284 num = find_next_bit(iommu->domain_ids,
1285 cap_ndoms(iommu->cap), num+1);
1286 }
1287
1288 if (found) {
1289 clear_bit(num, iommu->domain_ids);
1290 clear_bit(iommu->seq_id, &domain->iommu_bmp);
1291 iommu->domains[num] = NULL;
1292 }
Weidong Han8c11e792008-12-08 15:29:22 +08001293 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001294}
1295
1296static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001297static struct lock_class_key reserved_alloc_key;
1298static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001299
1300static void dmar_init_reserved_ranges(void)
1301{
1302 struct pci_dev *pdev = NULL;
1303 struct iova *iova;
1304 int i;
1305 u64 addr, size;
1306
David Millerf6611972008-02-06 01:36:23 -08001307 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001308
Mark Gross8a443df2008-03-04 14:59:31 -08001309 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1310 &reserved_alloc_key);
1311 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1312 &reserved_rbtree_key);
1313
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001314 /* IOAPIC ranges shouldn't be accessed by DMA */
1315 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1316 IOVA_PFN(IOAPIC_RANGE_END));
1317 if (!iova)
1318 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1319
1320 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1321 for_each_pci_dev(pdev) {
1322 struct resource *r;
1323
1324 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1325 r = &pdev->resource[i];
1326 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1327 continue;
1328 addr = r->start;
David Woodhousefd18de52009-05-10 23:57:41 +01001329 addr &= PHYSICAL_PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001330 size = r->end - addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001331 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001332 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1333 IOVA_PFN(size + addr) - 1);
1334 if (!iova)
1335 printk(KERN_ERR "Reserve iova failed\n");
1336 }
1337 }
1338
1339}
1340
1341static void domain_reserve_special_ranges(struct dmar_domain *domain)
1342{
1343 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1344}
1345
1346static inline int guestwidth_to_adjustwidth(int gaw)
1347{
1348 int agaw;
1349 int r = (gaw - 12) % 9;
1350
1351 if (r == 0)
1352 agaw = gaw;
1353 else
1354 agaw = gaw + 9 - r;
1355 if (agaw > 64)
1356 agaw = 64;
1357 return agaw;
1358}
1359
1360static int domain_init(struct dmar_domain *domain, int guest_width)
1361{
1362 struct intel_iommu *iommu;
1363 int adjust_width, agaw;
1364 unsigned long sagaw;
1365
David Millerf6611972008-02-06 01:36:23 -08001366 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001367 spin_lock_init(&domain->mapping_lock);
Weidong Hanc7151a82008-12-08 22:51:37 +08001368 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001369
1370 domain_reserve_special_ranges(domain);
1371
1372 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001373 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001374 if (guest_width > cap_mgaw(iommu->cap))
1375 guest_width = cap_mgaw(iommu->cap);
1376 domain->gaw = guest_width;
1377 adjust_width = guestwidth_to_adjustwidth(guest_width);
1378 agaw = width_to_agaw(adjust_width);
1379 sagaw = cap_sagaw(iommu->cap);
1380 if (!test_bit(agaw, &sagaw)) {
1381 /* hardware doesn't support it, choose a bigger one */
1382 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1383 agaw = find_next_bit(&sagaw, 5, agaw);
1384 if (agaw >= 5)
1385 return -ENODEV;
1386 }
1387 domain->agaw = agaw;
1388 INIT_LIST_HEAD(&domain->devices);
1389
Weidong Han8e6040972008-12-08 15:49:06 +08001390 if (ecap_coherent(iommu->ecap))
1391 domain->iommu_coherency = 1;
1392 else
1393 domain->iommu_coherency = 0;
1394
Sheng Yang58c610b2009-03-18 15:33:05 +08001395 if (ecap_sc_support(iommu->ecap))
1396 domain->iommu_snooping = 1;
1397 else
1398 domain->iommu_snooping = 0;
1399
Weidong Hanc7151a82008-12-08 22:51:37 +08001400 domain->iommu_count = 1;
1401
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001402 /* always allocate the top pgd */
1403 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1404 if (!domain->pgd)
1405 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001406 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001407 return 0;
1408}
1409
1410static void domain_exit(struct dmar_domain *domain)
1411{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001412 struct dmar_drhd_unit *drhd;
1413 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001414 u64 end;
1415
1416 /* Domain 0 is reserved, so dont process it */
1417 if (!domain)
1418 return;
1419
1420 domain_remove_dev_info(domain);
1421 /* destroy iovas */
1422 put_iova_domain(&domain->iovad);
1423 end = DOMAIN_MAX_ADDR(domain->gaw);
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001424 end = end & (~PAGE_MASK);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001425
1426 /* clear ptes */
1427 dma_pte_clear_range(domain, 0, end);
1428
1429 /* free page tables */
1430 dma_pte_free_pagetable(domain, 0, end);
1431
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001432 for_each_active_iommu(iommu, drhd)
1433 if (test_bit(iommu->seq_id, &domain->iommu_bmp))
1434 iommu_detach_domain(domain, iommu);
1435
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001436 free_domain_mem(domain);
1437}
1438
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001439static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1440 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001441{
1442 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001443 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001444 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001445 struct dma_pte *pgd;
1446 unsigned long num;
1447 unsigned long ndomains;
1448 int id;
1449 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001450 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001451
1452 pr_debug("Set context mapping for %02x:%02x.%d\n",
1453 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001454
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001455 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001456 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1457 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001458
David Woodhouse276dbf992009-04-04 01:45:37 +01001459 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001460 if (!iommu)
1461 return -ENODEV;
1462
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001463 context = device_to_context_entry(iommu, bus, devfn);
1464 if (!context)
1465 return -ENOMEM;
1466 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001467 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001468 spin_unlock_irqrestore(&iommu->lock, flags);
1469 return 0;
1470 }
1471
Weidong Hanea6606b2008-12-08 23:08:15 +08001472 id = domain->id;
1473 pgd = domain->pgd;
1474
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001475 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
1476 domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001477 int found = 0;
1478
1479 /* find an available domain id for this device in iommu */
1480 ndomains = cap_ndoms(iommu->cap);
1481 num = find_first_bit(iommu->domain_ids, ndomains);
1482 for (; num < ndomains; ) {
1483 if (iommu->domains[num] == domain) {
1484 id = num;
1485 found = 1;
1486 break;
1487 }
1488 num = find_next_bit(iommu->domain_ids,
1489 cap_ndoms(iommu->cap), num+1);
1490 }
1491
1492 if (found == 0) {
1493 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1494 if (num >= ndomains) {
1495 spin_unlock_irqrestore(&iommu->lock, flags);
1496 printk(KERN_ERR "IOMMU: no free domain ids\n");
1497 return -EFAULT;
1498 }
1499
1500 set_bit(num, iommu->domain_ids);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001501 set_bit(iommu->seq_id, &domain->iommu_bmp);
Weidong Hanea6606b2008-12-08 23:08:15 +08001502 iommu->domains[num] = domain;
1503 id = num;
1504 }
1505
1506 /* Skip top levels of page tables for
1507 * iommu which has less agaw than default.
1508 */
1509 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1510 pgd = phys_to_virt(dma_pte_addr(pgd));
1511 if (!dma_pte_present(pgd)) {
1512 spin_unlock_irqrestore(&iommu->lock, flags);
1513 return -ENOMEM;
1514 }
1515 }
1516 }
1517
1518 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001519
Yu Zhao93a23a72009-05-18 13:51:37 +08001520 if (translation != CONTEXT_TT_PASS_THROUGH) {
1521 info = iommu_support_dev_iotlb(domain, segment, bus, devfn);
1522 translation = info ? CONTEXT_TT_DEV_IOTLB :
1523 CONTEXT_TT_MULTI_LEVEL;
1524 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001525 /*
1526 * In pass through mode, AW must be programmed to indicate the largest
1527 * AGAW value supported by hardware. And ASR is ignored by hardware.
1528 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001529 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001530 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001531 else {
1532 context_set_address_root(context, virt_to_phys(pgd));
1533 context_set_address_width(context, iommu->agaw);
1534 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001535
1536 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001537 context_set_fault_enable(context);
1538 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001539 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001540
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001541 /*
1542 * It's a non-present to present mapping. If hardware doesn't cache
1543 * non-present entry we only need to flush the write-buffer. If the
1544 * _does_ cache non-present entries, then it does so in the special
1545 * domain #0, which we have to flush:
1546 */
1547 if (cap_caching_mode(iommu->cap)) {
1548 iommu->flush.flush_context(iommu, 0,
1549 (((u16)bus) << 8) | devfn,
1550 DMA_CCMD_MASK_NOBIT,
1551 DMA_CCMD_DEVICE_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001552 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001553 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001554 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001555 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001556 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001557 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001558
1559 spin_lock_irqsave(&domain->iommu_lock, flags);
1560 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1561 domain->iommu_count++;
Sheng Yang58c610b2009-03-18 15:33:05 +08001562 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001563 }
1564 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001565 return 0;
1566}
1567
1568static int
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001569domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1570 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001571{
1572 int ret;
1573 struct pci_dev *tmp, *parent;
1574
David Woodhouse276dbf992009-04-04 01:45:37 +01001575 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001576 pdev->bus->number, pdev->devfn,
1577 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001578 if (ret)
1579 return ret;
1580
1581 /* dependent device mapping */
1582 tmp = pci_find_upstream_pcie_bridge(pdev);
1583 if (!tmp)
1584 return 0;
1585 /* Secondary interface's bus number and devfn 0 */
1586 parent = pdev->bus->self;
1587 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001588 ret = domain_context_mapping_one(domain,
1589 pci_domain_nr(parent->bus),
1590 parent->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001591 parent->devfn, translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001592 if (ret)
1593 return ret;
1594 parent = parent->bus->self;
1595 }
1596 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1597 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001598 pci_domain_nr(tmp->subordinate),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001599 tmp->subordinate->number, 0,
1600 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001601 else /* this is a legacy PCI bridge */
1602 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001603 pci_domain_nr(tmp->bus),
1604 tmp->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001605 tmp->devfn,
1606 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001607}
1608
Weidong Han5331fe62008-12-08 23:00:00 +08001609static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001610{
1611 int ret;
1612 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001613 struct intel_iommu *iommu;
1614
David Woodhouse276dbf992009-04-04 01:45:37 +01001615 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1616 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001617 if (!iommu)
1618 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001619
David Woodhouse276dbf992009-04-04 01:45:37 +01001620 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001621 if (!ret)
1622 return ret;
1623 /* dependent device mapping */
1624 tmp = pci_find_upstream_pcie_bridge(pdev);
1625 if (!tmp)
1626 return ret;
1627 /* Secondary interface's bus number and devfn 0 */
1628 parent = pdev->bus->self;
1629 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001630 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001631 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001632 if (!ret)
1633 return ret;
1634 parent = parent->bus->self;
1635 }
1636 if (tmp->is_pcie)
David Woodhouse276dbf992009-04-04 01:45:37 +01001637 return device_context_mapped(iommu, tmp->subordinate->number,
1638 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001639 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001640 return device_context_mapped(iommu, tmp->bus->number,
1641 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001642}
1643
1644static int
1645domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1646 u64 hpa, size_t size, int prot)
1647{
1648 u64 start_pfn, end_pfn;
1649 struct dma_pte *pte;
1650 int index;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001651 int addr_width = agaw_to_width(domain->agaw);
1652
David Woodhouse66eae842009-06-27 19:00:32 +01001653 BUG_ON(hpa >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001654
1655 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1656 return -EINVAL;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001657 iova &= PAGE_MASK;
1658 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1659 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001660 index = 0;
1661 while (start_pfn < end_pfn) {
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001662 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001663 if (!pte)
1664 return -ENOMEM;
1665 /* We don't need lock here, nobody else
1666 * touches the iova range
1667 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001668 BUG_ON(dma_pte_addr(pte));
David Woodhousedd4e8312009-06-27 16:21:20 +01001669 dma_set_pte_pfn(pte, start_pfn);
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001670 dma_set_pte_prot(pte, prot);
Sheng Yang9cf06692009-03-18 15:33:07 +08001671 if (prot & DMA_PTE_SNP)
1672 dma_set_pte_snp(pte);
Weidong Han5331fe62008-12-08 23:00:00 +08001673 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001674 start_pfn++;
1675 index++;
1676 }
1677 return 0;
1678}
1679
Weidong Hanc7151a82008-12-08 22:51:37 +08001680static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001681{
Weidong Hanc7151a82008-12-08 22:51:37 +08001682 if (!iommu)
1683 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001684
1685 clear_context_table(iommu, bus, devfn);
1686 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001687 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001688 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001689}
1690
1691static void domain_remove_dev_info(struct dmar_domain *domain)
1692{
1693 struct device_domain_info *info;
1694 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001695 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001696
1697 spin_lock_irqsave(&device_domain_lock, flags);
1698 while (!list_empty(&domain->devices)) {
1699 info = list_entry(domain->devices.next,
1700 struct device_domain_info, link);
1701 list_del(&info->link);
1702 list_del(&info->global);
1703 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001704 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001705 spin_unlock_irqrestore(&device_domain_lock, flags);
1706
Yu Zhao93a23a72009-05-18 13:51:37 +08001707 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01001708 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001709 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001710 free_devinfo_mem(info);
1711
1712 spin_lock_irqsave(&device_domain_lock, flags);
1713 }
1714 spin_unlock_irqrestore(&device_domain_lock, flags);
1715}
1716
1717/*
1718 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001719 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001720 */
Kay, Allen M38717942008-09-09 18:37:29 +03001721static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001722find_domain(struct pci_dev *pdev)
1723{
1724 struct device_domain_info *info;
1725
1726 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001727 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001728 if (info)
1729 return info->domain;
1730 return NULL;
1731}
1732
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001733/* domain is initialized */
1734static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1735{
1736 struct dmar_domain *domain, *found = NULL;
1737 struct intel_iommu *iommu;
1738 struct dmar_drhd_unit *drhd;
1739 struct device_domain_info *info, *tmp;
1740 struct pci_dev *dev_tmp;
1741 unsigned long flags;
1742 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001743 int segment;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001744 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001745
1746 domain = find_domain(pdev);
1747 if (domain)
1748 return domain;
1749
David Woodhouse276dbf992009-04-04 01:45:37 +01001750 segment = pci_domain_nr(pdev->bus);
1751
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001752 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1753 if (dev_tmp) {
1754 if (dev_tmp->is_pcie) {
1755 bus = dev_tmp->subordinate->number;
1756 devfn = 0;
1757 } else {
1758 bus = dev_tmp->bus->number;
1759 devfn = dev_tmp->devfn;
1760 }
1761 spin_lock_irqsave(&device_domain_lock, flags);
1762 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001763 if (info->segment == segment &&
1764 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001765 found = info->domain;
1766 break;
1767 }
1768 }
1769 spin_unlock_irqrestore(&device_domain_lock, flags);
1770 /* pcie-pci bridge already has a domain, uses it */
1771 if (found) {
1772 domain = found;
1773 goto found_domain;
1774 }
1775 }
1776
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001777 domain = alloc_domain();
1778 if (!domain)
1779 goto error;
1780
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001781 /* Allocate new domain for the device */
1782 drhd = dmar_find_matched_drhd_unit(pdev);
1783 if (!drhd) {
1784 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1785 pci_name(pdev));
1786 return NULL;
1787 }
1788 iommu = drhd->iommu;
1789
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001790 ret = iommu_attach_domain(domain, iommu);
1791 if (ret) {
1792 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001793 goto error;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001794 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001795
1796 if (domain_init(domain, gaw)) {
1797 domain_exit(domain);
1798 goto error;
1799 }
1800
1801 /* register pcie-to-pci device */
1802 if (dev_tmp) {
1803 info = alloc_devinfo_mem();
1804 if (!info) {
1805 domain_exit(domain);
1806 goto error;
1807 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001808 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001809 info->bus = bus;
1810 info->devfn = devfn;
1811 info->dev = NULL;
1812 info->domain = domain;
1813 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001814 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001815
1816 /* pcie-to-pci bridge already has a domain, uses it */
1817 found = NULL;
1818 spin_lock_irqsave(&device_domain_lock, flags);
1819 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001820 if (tmp->segment == segment &&
1821 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001822 found = tmp->domain;
1823 break;
1824 }
1825 }
1826 if (found) {
1827 free_devinfo_mem(info);
1828 domain_exit(domain);
1829 domain = found;
1830 } else {
1831 list_add(&info->link, &domain->devices);
1832 list_add(&info->global, &device_domain_list);
1833 }
1834 spin_unlock_irqrestore(&device_domain_lock, flags);
1835 }
1836
1837found_domain:
1838 info = alloc_devinfo_mem();
1839 if (!info)
1840 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01001841 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001842 info->bus = pdev->bus->number;
1843 info->devfn = pdev->devfn;
1844 info->dev = pdev;
1845 info->domain = domain;
1846 spin_lock_irqsave(&device_domain_lock, flags);
1847 /* somebody is fast */
1848 found = find_domain(pdev);
1849 if (found != NULL) {
1850 spin_unlock_irqrestore(&device_domain_lock, flags);
1851 if (found != domain) {
1852 domain_exit(domain);
1853 domain = found;
1854 }
1855 free_devinfo_mem(info);
1856 return domain;
1857 }
1858 list_add(&info->link, &domain->devices);
1859 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001860 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001861 spin_unlock_irqrestore(&device_domain_lock, flags);
1862 return domain;
1863error:
1864 /* recheck it here, maybe others set it */
1865 return find_domain(pdev);
1866}
1867
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001868static int iommu_identity_mapping;
1869
David Woodhouseb2132032009-06-26 18:50:28 +01001870static int iommu_domain_identity_map(struct dmar_domain *domain,
1871 unsigned long long start,
1872 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001873{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001874 unsigned long size;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001875 unsigned long long base;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001876
1877 /* The address might not be aligned */
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001878 base = start & PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001879 size = end - base;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001880 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001881 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1882 IOVA_PFN(base + size) - 1)) {
1883 printk(KERN_ERR "IOMMU: reserve iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01001884 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001885 }
1886
David Woodhouseb2132032009-06-26 18:50:28 +01001887 pr_debug("Mapping reserved region %lx@%llx for domain %d\n",
1888 size, base, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001889 /*
1890 * RMRR range might have overlap with physical memory range,
1891 * clear it first
1892 */
1893 dma_pte_clear_range(domain, base, base + size);
1894
David Woodhouseb2132032009-06-26 18:50:28 +01001895 return domain_page_mapping(domain, base, base, size,
1896 DMA_PTE_READ|DMA_PTE_WRITE);
1897}
1898
1899static int iommu_prepare_identity_map(struct pci_dev *pdev,
1900 unsigned long long start,
1901 unsigned long long end)
1902{
1903 struct dmar_domain *domain;
1904 int ret;
1905
1906 printk(KERN_INFO
1907 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1908 pci_name(pdev), start, end);
1909
David Woodhousec7ab48d2009-06-26 19:10:36 +01001910 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01001911 if (!domain)
1912 return -ENOMEM;
1913
1914 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001915 if (ret)
1916 goto error;
1917
1918 /* context entry init */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001919 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01001920 if (ret)
1921 goto error;
1922
1923 return 0;
1924
1925 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001926 domain_exit(domain);
1927 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001928}
1929
1930static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1931 struct pci_dev *pdev)
1932{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001933 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001934 return 0;
1935 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1936 rmrr->end_address + 1);
1937}
1938
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001939#ifdef CONFIG_DMAR_FLOPPY_WA
1940static inline void iommu_prepare_isa(void)
1941{
1942 struct pci_dev *pdev;
1943 int ret;
1944
1945 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1946 if (!pdev)
1947 return;
1948
David Woodhousec7ab48d2009-06-26 19:10:36 +01001949 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001950 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1951
1952 if (ret)
David Woodhousec7ab48d2009-06-26 19:10:36 +01001953 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
1954 "floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001955
1956}
1957#else
1958static inline void iommu_prepare_isa(void)
1959{
1960 return;
1961}
1962#endif /* !CONFIG_DMAR_FLPY_WA */
1963
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001964/* Initialize each context entry as pass through.*/
1965static int __init init_context_pass_through(void)
1966{
1967 struct pci_dev *pdev = NULL;
1968 struct dmar_domain *domain;
1969 int ret;
1970
1971 for_each_pci_dev(pdev) {
1972 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1973 ret = domain_context_mapping(domain, pdev,
1974 CONTEXT_TT_PASS_THROUGH);
1975 if (ret)
1976 return ret;
1977 }
1978 return 0;
1979}
1980
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001981static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01001982
1983static int __init si_domain_work_fn(unsigned long start_pfn,
1984 unsigned long end_pfn, void *datax)
1985{
1986 int *ret = datax;
1987
1988 *ret = iommu_domain_identity_map(si_domain,
1989 (uint64_t)start_pfn << PAGE_SHIFT,
1990 (uint64_t)end_pfn << PAGE_SHIFT);
1991 return *ret;
1992
1993}
1994
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001995static int si_domain_init(void)
1996{
1997 struct dmar_drhd_unit *drhd;
1998 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01001999 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002000
2001 si_domain = alloc_domain();
2002 if (!si_domain)
2003 return -EFAULT;
2004
David Woodhousec7ab48d2009-06-26 19:10:36 +01002005 pr_debug("Identity mapping domain is domain %d\n", si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002006
2007 for_each_active_iommu(iommu, drhd) {
2008 ret = iommu_attach_domain(si_domain, iommu);
2009 if (ret) {
2010 domain_exit(si_domain);
2011 return -EFAULT;
2012 }
2013 }
2014
2015 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2016 domain_exit(si_domain);
2017 return -EFAULT;
2018 }
2019
2020 si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY;
2021
David Woodhousec7ab48d2009-06-26 19:10:36 +01002022 for_each_online_node(nid) {
2023 work_with_active_regions(nid, si_domain_work_fn, &ret);
2024 if (ret)
2025 return ret;
2026 }
2027
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002028 return 0;
2029}
2030
2031static void domain_remove_one_dev_info(struct dmar_domain *domain,
2032 struct pci_dev *pdev);
2033static int identity_mapping(struct pci_dev *pdev)
2034{
2035 struct device_domain_info *info;
2036
2037 if (likely(!iommu_identity_mapping))
2038 return 0;
2039
2040
2041 list_for_each_entry(info, &si_domain->devices, link)
2042 if (info->dev == pdev)
2043 return 1;
2044 return 0;
2045}
2046
2047static int domain_add_dev_info(struct dmar_domain *domain,
2048 struct pci_dev *pdev)
2049{
2050 struct device_domain_info *info;
2051 unsigned long flags;
2052
2053 info = alloc_devinfo_mem();
2054 if (!info)
2055 return -ENOMEM;
2056
2057 info->segment = pci_domain_nr(pdev->bus);
2058 info->bus = pdev->bus->number;
2059 info->devfn = pdev->devfn;
2060 info->dev = pdev;
2061 info->domain = domain;
2062
2063 spin_lock_irqsave(&device_domain_lock, flags);
2064 list_add(&info->link, &domain->devices);
2065 list_add(&info->global, &device_domain_list);
2066 pdev->dev.archdata.iommu = info;
2067 spin_unlock_irqrestore(&device_domain_lock, flags);
2068
2069 return 0;
2070}
2071
2072static int iommu_prepare_static_identity_mapping(void)
2073{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002074 struct pci_dev *pdev = NULL;
2075 int ret;
2076
2077 ret = si_domain_init();
2078 if (ret)
2079 return -EFAULT;
2080
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002081 for_each_pci_dev(pdev) {
David Woodhousec7ab48d2009-06-26 19:10:36 +01002082 printk(KERN_INFO "IOMMU: identity mapping for device %s\n",
2083 pci_name(pdev));
2084
2085 ret = domain_context_mapping(si_domain, pdev,
2086 CONTEXT_TT_MULTI_LEVEL);
2087 if (ret)
2088 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002089 ret = domain_add_dev_info(si_domain, pdev);
2090 if (ret)
2091 return ret;
2092 }
2093
2094 return 0;
2095}
2096
2097int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002098{
2099 struct dmar_drhd_unit *drhd;
2100 struct dmar_rmrr_unit *rmrr;
2101 struct pci_dev *pdev;
2102 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002103 int i, ret;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002104 int pass_through = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002105
2106 /*
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002107 * In case pass through can not be enabled, iommu tries to use identity
2108 * mapping.
2109 */
2110 if (iommu_pass_through)
2111 iommu_identity_mapping = 1;
2112
2113 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002114 * for each drhd
2115 * allocate root
2116 * initialize and program root entry to not present
2117 * endfor
2118 */
2119 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002120 g_num_of_iommus++;
2121 /*
2122 * lock not needed as this is only incremented in the single
2123 * threaded kernel __init code path all other access are read
2124 * only
2125 */
2126 }
2127
Weidong Hand9630fe2008-12-08 11:06:32 +08002128 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2129 GFP_KERNEL);
2130 if (!g_iommus) {
2131 printk(KERN_ERR "Allocating global iommu array failed\n");
2132 ret = -ENOMEM;
2133 goto error;
2134 }
2135
mark gross80b20dd2008-04-18 13:53:58 -07002136 deferred_flush = kzalloc(g_num_of_iommus *
2137 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2138 if (!deferred_flush) {
Weidong Hand9630fe2008-12-08 11:06:32 +08002139 kfree(g_iommus);
mark gross5e0d2a62008-03-04 15:22:08 -08002140 ret = -ENOMEM;
2141 goto error;
2142 }
2143
mark gross5e0d2a62008-03-04 15:22:08 -08002144 for_each_drhd_unit(drhd) {
2145 if (drhd->ignored)
2146 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002147
2148 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08002149 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002150
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002151 ret = iommu_init_domains(iommu);
2152 if (ret)
2153 goto error;
2154
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002155 /*
2156 * TBD:
2157 * we could share the same root & context tables
2158 * amoung all IOMMU's. Need to Split it later.
2159 */
2160 ret = iommu_alloc_root_entry(iommu);
2161 if (ret) {
2162 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
2163 goto error;
2164 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002165 if (!ecap_pass_through(iommu->ecap))
2166 pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002167 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002168 if (iommu_pass_through)
2169 if (!pass_through) {
2170 printk(KERN_INFO
2171 "Pass Through is not supported by hardware.\n");
2172 iommu_pass_through = 0;
2173 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002174
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002175 /*
2176 * Start from the sane iommu hardware state.
2177 */
Youquan Songa77b67d2008-10-16 16:31:56 -07002178 for_each_drhd_unit(drhd) {
2179 if (drhd->ignored)
2180 continue;
2181
2182 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002183
2184 /*
2185 * If the queued invalidation is already initialized by us
2186 * (for example, while enabling interrupt-remapping) then
2187 * we got the things already rolling from a sane state.
2188 */
2189 if (iommu->qi)
2190 continue;
2191
2192 /*
2193 * Clear any previous faults.
2194 */
2195 dmar_fault(-1, iommu);
2196 /*
2197 * Disable queued invalidation if supported and already enabled
2198 * before OS handover.
2199 */
2200 dmar_disable_qi(iommu);
2201 }
2202
2203 for_each_drhd_unit(drhd) {
2204 if (drhd->ignored)
2205 continue;
2206
2207 iommu = drhd->iommu;
2208
Youquan Songa77b67d2008-10-16 16:31:56 -07002209 if (dmar_enable_qi(iommu)) {
2210 /*
2211 * Queued Invalidate not enabled, use Register Based
2212 * Invalidate
2213 */
2214 iommu->flush.flush_context = __iommu_flush_context;
2215 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
2216 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002217 "invalidation\n",
2218 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002219 } else {
2220 iommu->flush.flush_context = qi_flush_context;
2221 iommu->flush.flush_iotlb = qi_flush_iotlb;
2222 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002223 "invalidation\n",
2224 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002225 }
2226 }
2227
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002228 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002229 * If pass through is set and enabled, context entries of all pci
2230 * devices are intialized by pass through translation type.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002231 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002232 if (iommu_pass_through) {
2233 ret = init_context_pass_through();
2234 if (ret) {
2235 printk(KERN_ERR "IOMMU: Pass through init failed.\n");
2236 iommu_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002237 }
2238 }
2239
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002240 /*
2241 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002242 * identity mappings for rmrr, gfx, and isa and may fall back to static
2243 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002244 */
2245 if (!iommu_pass_through) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002246 if (iommu_identity_mapping)
2247 iommu_prepare_static_identity_mapping();
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002248 /*
2249 * For each rmrr
2250 * for each dev attached to rmrr
2251 * do
2252 * locate drhd for dev, alloc domain for dev
2253 * allocate free domain
2254 * allocate page table entries for rmrr
2255 * if context not allocated for bus
2256 * allocate and init context
2257 * set present in root table for this bus
2258 * init context with domain, translation etc
2259 * endfor
2260 * endfor
2261 */
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002262 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002263 for_each_rmrr_units(rmrr) {
2264 for (i = 0; i < rmrr->devices_cnt; i++) {
2265 pdev = rmrr->devices[i];
2266 /*
2267 * some BIOS lists non-exist devices in DMAR
2268 * table.
2269 */
2270 if (!pdev)
2271 continue;
2272 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2273 if (ret)
2274 printk(KERN_ERR
2275 "IOMMU: mapping reserved region failed\n");
2276 }
2277 }
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002278
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002279 iommu_prepare_isa();
2280 }
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002281
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002282 /*
2283 * for each drhd
2284 * enable fault log
2285 * global invalidate context cache
2286 * global invalidate iotlb
2287 * enable translation
2288 */
2289 for_each_drhd_unit(drhd) {
2290 if (drhd->ignored)
2291 continue;
2292 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002293
2294 iommu_flush_write_buffer(iommu);
2295
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002296 ret = dmar_set_interrupt(iommu);
2297 if (ret)
2298 goto error;
2299
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002300 iommu_set_root_entry(iommu);
2301
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002302 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002303 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002304 iommu_disable_protect_mem_regions(iommu);
2305
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002306 ret = iommu_enable_translation(iommu);
2307 if (ret)
2308 goto error;
2309 }
2310
2311 return 0;
2312error:
2313 for_each_drhd_unit(drhd) {
2314 if (drhd->ignored)
2315 continue;
2316 iommu = drhd->iommu;
2317 free_iommu(iommu);
2318 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002319 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002320 return ret;
2321}
2322
2323static inline u64 aligned_size(u64 host_addr, size_t size)
2324{
2325 u64 addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002326 addr = (host_addr & (~PAGE_MASK)) + size;
2327 return PAGE_ALIGN(addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002328}
2329
2330struct iova *
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002331iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002332{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002333 struct iova *piova;
2334
2335 /* Make sure it's in range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002336 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002337 if (!size || (IOVA_START_ADDR + size > end))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002338 return NULL;
2339
2340 piova = alloc_iova(&domain->iovad,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002341 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002342 return piova;
2343}
2344
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002345static struct iova *
2346__intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002347 size_t size, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002348{
2349 struct pci_dev *pdev = to_pci_dev(dev);
2350 struct iova *iova = NULL;
2351
Yang Hongyang284901a2009-04-06 19:01:15 -07002352 if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002353 iova = iommu_alloc_iova(domain, size, dma_mask);
2354 else {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002355 /*
2356 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002357 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002358 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002359 */
Yang Hongyang284901a2009-04-06 19:01:15 -07002360 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002361 if (!iova)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002362 iova = iommu_alloc_iova(domain, size, dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002363 }
2364
2365 if (!iova) {
2366 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2367 return NULL;
2368 }
2369
2370 return iova;
2371}
2372
2373static struct dmar_domain *
2374get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002375{
2376 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002377 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002378
2379 domain = get_domain_for_dev(pdev,
2380 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2381 if (!domain) {
2382 printk(KERN_ERR
2383 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002384 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002385 }
2386
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002387 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002388 if (unlikely(!domain_context_mapped(pdev))) {
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002389 ret = domain_context_mapping(domain, pdev,
2390 CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002391 if (ret) {
2392 printk(KERN_ERR
2393 "Domain context map for %s failed",
2394 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002395 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002396 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002397 }
2398
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002399 return domain;
2400}
2401
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002402static int iommu_dummy(struct pci_dev *pdev)
2403{
2404 return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
2405}
2406
2407/* Check if the pdev needs to go through non-identity map and unmap process.*/
2408static int iommu_no_mapping(struct pci_dev *pdev)
2409{
2410 int found;
2411
2412 if (!iommu_identity_mapping)
2413 return iommu_dummy(pdev);
2414
2415 found = identity_mapping(pdev);
2416 if (found) {
2417 if (pdev->dma_mask > DMA_BIT_MASK(32))
2418 return 1;
2419 else {
2420 /*
2421 * 32 bit DMA is removed from si_domain and fall back
2422 * to non-identity mapping.
2423 */
2424 domain_remove_one_dev_info(si_domain, pdev);
2425 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
2426 pci_name(pdev));
2427 return 0;
2428 }
2429 } else {
2430 /*
2431 * In case of a detached 64 bit DMA device from vm, the device
2432 * is put into si_domain for identity mapping.
2433 */
2434 if (pdev->dma_mask > DMA_BIT_MASK(32)) {
2435 int ret;
2436 ret = domain_add_dev_info(si_domain, pdev);
2437 if (!ret) {
2438 printk(KERN_INFO "64bit %s uses identity mapping\n",
2439 pci_name(pdev));
2440 return 1;
2441 }
2442 }
2443 }
2444
2445 return iommu_dummy(pdev);
2446}
2447
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002448static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2449 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002450{
2451 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002452 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002453 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002454 struct iova *iova;
2455 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002456 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002457 struct intel_iommu *iommu;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002458
2459 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002460
2461 if (iommu_no_mapping(pdev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002462 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002463
2464 domain = get_valid_domain_for_dev(pdev);
2465 if (!domain)
2466 return 0;
2467
Weidong Han8c11e792008-12-08 15:29:22 +08002468 iommu = domain_get_iommu(domain);
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002469 size = aligned_size((u64)paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002470
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002471 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002472 if (!iova)
2473 goto error;
2474
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002475 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002476
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002477 /*
2478 * Check if DMAR supports zero-length reads on write only
2479 * mappings..
2480 */
2481 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002482 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002483 prot |= DMA_PTE_READ;
2484 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2485 prot |= DMA_PTE_WRITE;
2486 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002487 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002488 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002489 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002490 * is not a big problem
2491 */
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002492 ret = domain_page_mapping(domain, start_paddr,
David Woodhousefd18de52009-05-10 23:57:41 +01002493 ((u64)paddr) & PHYSICAL_PAGE_MASK,
2494 size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002495 if (ret)
2496 goto error;
2497
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002498 /* it's a non-present to present mapping. Only flush if caching mode */
2499 if (cap_caching_mode(iommu->cap))
2500 iommu_flush_iotlb_psi(iommu, 0, start_paddr,
2501 size >> VTD_PAGE_SHIFT);
2502 else
Weidong Han8c11e792008-12-08 15:29:22 +08002503 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002504
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002505 return start_paddr + ((u64)paddr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002506
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002507error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002508 if (iova)
2509 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002510 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002511 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002512 return 0;
2513}
2514
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002515static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2516 unsigned long offset, size_t size,
2517 enum dma_data_direction dir,
2518 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002519{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002520 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2521 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002522}
2523
mark gross5e0d2a62008-03-04 15:22:08 -08002524static void flush_unmaps(void)
2525{
mark gross80b20dd2008-04-18 13:53:58 -07002526 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002527
mark gross5e0d2a62008-03-04 15:22:08 -08002528 timer_on = 0;
2529
2530 /* just flush them all */
2531 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002532 struct intel_iommu *iommu = g_iommus[i];
2533 if (!iommu)
2534 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002535
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002536 if (!deferred_flush[i].next)
2537 continue;
2538
2539 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08002540 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002541 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08002542 unsigned long mask;
2543 struct iova *iova = deferred_flush[i].iova[j];
2544
2545 mask = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT;
2546 mask = ilog2(mask >> VTD_PAGE_SHIFT);
2547 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
2548 iova->pfn_lo << PAGE_SHIFT, mask);
2549 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
mark gross80b20dd2008-04-18 13:53:58 -07002550 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002551 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002552 }
2553
mark gross5e0d2a62008-03-04 15:22:08 -08002554 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002555}
2556
2557static void flush_unmaps_timeout(unsigned long data)
2558{
mark gross80b20dd2008-04-18 13:53:58 -07002559 unsigned long flags;
2560
2561 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002562 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002563 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002564}
2565
2566static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2567{
2568 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002569 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002570 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002571
2572 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002573 if (list_size == HIGH_WATER_MARK)
2574 flush_unmaps();
2575
Weidong Han8c11e792008-12-08 15:29:22 +08002576 iommu = domain_get_iommu(dom);
2577 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002578
mark gross80b20dd2008-04-18 13:53:58 -07002579 next = deferred_flush[iommu_id].next;
2580 deferred_flush[iommu_id].domain[next] = dom;
2581 deferred_flush[iommu_id].iova[next] = iova;
2582 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002583
2584 if (!timer_on) {
2585 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2586 timer_on = 1;
2587 }
2588 list_size++;
2589 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2590}
2591
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002592static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2593 size_t size, enum dma_data_direction dir,
2594 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002595{
2596 struct pci_dev *pdev = to_pci_dev(dev);
2597 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002598 unsigned long start_addr;
2599 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002600 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002601
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002602 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002603 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002604
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002605 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002606 BUG_ON(!domain);
2607
Weidong Han8c11e792008-12-08 15:29:22 +08002608 iommu = domain_get_iommu(domain);
2609
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002610 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2611 if (!iova)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002612 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002613
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002614 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002615 size = aligned_size((u64)dev_addr, size);
2616
David Woodhouse4cf2e752009-02-11 17:23:43 +00002617 pr_debug("Device %s unmapping: %zx@%llx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002618 pci_name(pdev), size, (unsigned long long)start_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002619
2620 /* clear the whole page */
2621 dma_pte_clear_range(domain, start_addr, start_addr + size);
2622 /* free page tables */
2623 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
mark gross5e0d2a62008-03-04 15:22:08 -08002624 if (intel_iommu_strict) {
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002625 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2626 size >> VTD_PAGE_SHIFT);
mark gross5e0d2a62008-03-04 15:22:08 -08002627 /* free iova */
2628 __free_iova(&domain->iovad, iova);
2629 } else {
2630 add_unmap(domain, iova);
2631 /*
2632 * queue up the release of the unmap to save the 1/6th of the
2633 * cpu used up by the iotlb flush operation...
2634 */
mark gross5e0d2a62008-03-04 15:22:08 -08002635 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002636}
2637
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002638static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2639 int dir)
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002640{
2641 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2642}
2643
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002644static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2645 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002646{
2647 void *vaddr;
2648 int order;
2649
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002650 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002651 order = get_order(size);
2652 flags &= ~(GFP_DMA | GFP_DMA32);
2653
2654 vaddr = (void *)__get_free_pages(flags, order);
2655 if (!vaddr)
2656 return NULL;
2657 memset(vaddr, 0, size);
2658
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002659 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2660 DMA_BIDIRECTIONAL,
2661 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002662 if (*dma_handle)
2663 return vaddr;
2664 free_pages((unsigned long)vaddr, order);
2665 return NULL;
2666}
2667
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002668static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2669 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002670{
2671 int order;
2672
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002673 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002674 order = get_order(size);
2675
2676 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2677 free_pages((unsigned long)vaddr, order);
2678}
2679
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002680static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2681 int nelems, enum dma_data_direction dir,
2682 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002683{
2684 int i;
2685 struct pci_dev *pdev = to_pci_dev(hwdev);
2686 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002687 unsigned long start_addr;
2688 struct iova *iova;
2689 size_t size = 0;
David Woodhouse4cf2e752009-02-11 17:23:43 +00002690 phys_addr_t addr;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002691 struct scatterlist *sg;
Weidong Han8c11e792008-12-08 15:29:22 +08002692 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002693
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002694 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002695 return;
2696
2697 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002698 BUG_ON(!domain);
2699
2700 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002701
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002702 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002703 if (!iova)
2704 return;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002705 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002706 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002707 size += aligned_size((u64)addr, sg->length);
2708 }
2709
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002710 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002711
2712 /* clear the whole page */
2713 dma_pte_clear_range(domain, start_addr, start_addr + size);
2714 /* free page tables */
2715 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2716
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002717 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2718 size >> VTD_PAGE_SHIFT);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002719
2720 /* free iova */
2721 __free_iova(&domain->iovad, iova);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002722}
2723
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002724static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002725 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002726{
2727 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002728 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002729
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002730 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002731 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002732 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002733 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002734 }
2735 return nelems;
2736}
2737
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002738static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2739 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002740{
David Woodhouse4cf2e752009-02-11 17:23:43 +00002741 phys_addr_t addr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002742 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002743 struct pci_dev *pdev = to_pci_dev(hwdev);
2744 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002745 size_t size = 0;
2746 int prot = 0;
2747 size_t offset = 0;
2748 struct iova *iova = NULL;
2749 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002750 struct scatterlist *sg;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002751 unsigned long start_addr;
Weidong Han8c11e792008-12-08 15:29:22 +08002752 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002753
2754 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002755 if (iommu_no_mapping(pdev))
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002756 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002757
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002758 domain = get_valid_domain_for_dev(pdev);
2759 if (!domain)
2760 return 0;
2761
Weidong Han8c11e792008-12-08 15:29:22 +08002762 iommu = domain_get_iommu(domain);
2763
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002764 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002765 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002766 size += aligned_size((u64)addr, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002767 }
2768
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002769 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002770 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002771 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002772 return 0;
2773 }
2774
2775 /*
2776 * Check if DMAR supports zero-length reads on write only
2777 * mappings..
2778 */
2779 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002780 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002781 prot |= DMA_PTE_READ;
2782 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2783 prot |= DMA_PTE_WRITE;
2784
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002785 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002786 offset = 0;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002787 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002788 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002789 size = aligned_size((u64)addr, sg->length);
2790 ret = domain_page_mapping(domain, start_addr + offset,
David Woodhousefd18de52009-05-10 23:57:41 +01002791 ((u64)addr) & PHYSICAL_PAGE_MASK,
2792 size, prot);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002793 if (ret) {
2794 /* clear the page */
2795 dma_pte_clear_range(domain, start_addr,
2796 start_addr + offset);
2797 /* free page tables */
2798 dma_pte_free_pagetable(domain, start_addr,
2799 start_addr + offset);
2800 /* free iova */
2801 __free_iova(&domain->iovad, iova);
2802 return 0;
2803 }
2804 sg->dma_address = start_addr + offset +
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002805 ((u64)addr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002806 sg->dma_length = sg->length;
2807 offset += size;
2808 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002809
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002810 /* it's a non-present to present mapping. Only flush if caching mode */
2811 if (cap_caching_mode(iommu->cap))
2812 iommu_flush_iotlb_psi(iommu, 0, start_addr,
2813 offset >> VTD_PAGE_SHIFT);
2814 else
Weidong Han8c11e792008-12-08 15:29:22 +08002815 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002816
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002817 return nelems;
2818}
2819
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002820static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2821{
2822 return !dma_addr;
2823}
2824
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002825struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002826 .alloc_coherent = intel_alloc_coherent,
2827 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002828 .map_sg = intel_map_sg,
2829 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002830 .map_page = intel_map_page,
2831 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002832 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002833};
2834
2835static inline int iommu_domain_cache_init(void)
2836{
2837 int ret = 0;
2838
2839 iommu_domain_cache = kmem_cache_create("iommu_domain",
2840 sizeof(struct dmar_domain),
2841 0,
2842 SLAB_HWCACHE_ALIGN,
2843
2844 NULL);
2845 if (!iommu_domain_cache) {
2846 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2847 ret = -ENOMEM;
2848 }
2849
2850 return ret;
2851}
2852
2853static inline int iommu_devinfo_cache_init(void)
2854{
2855 int ret = 0;
2856
2857 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2858 sizeof(struct device_domain_info),
2859 0,
2860 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002861 NULL);
2862 if (!iommu_devinfo_cache) {
2863 printk(KERN_ERR "Couldn't create devinfo cache\n");
2864 ret = -ENOMEM;
2865 }
2866
2867 return ret;
2868}
2869
2870static inline int iommu_iova_cache_init(void)
2871{
2872 int ret = 0;
2873
2874 iommu_iova_cache = kmem_cache_create("iommu_iova",
2875 sizeof(struct iova),
2876 0,
2877 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002878 NULL);
2879 if (!iommu_iova_cache) {
2880 printk(KERN_ERR "Couldn't create iova cache\n");
2881 ret = -ENOMEM;
2882 }
2883
2884 return ret;
2885}
2886
2887static int __init iommu_init_mempool(void)
2888{
2889 int ret;
2890 ret = iommu_iova_cache_init();
2891 if (ret)
2892 return ret;
2893
2894 ret = iommu_domain_cache_init();
2895 if (ret)
2896 goto domain_error;
2897
2898 ret = iommu_devinfo_cache_init();
2899 if (!ret)
2900 return ret;
2901
2902 kmem_cache_destroy(iommu_domain_cache);
2903domain_error:
2904 kmem_cache_destroy(iommu_iova_cache);
2905
2906 return -ENOMEM;
2907}
2908
2909static void __init iommu_exit_mempool(void)
2910{
2911 kmem_cache_destroy(iommu_devinfo_cache);
2912 kmem_cache_destroy(iommu_domain_cache);
2913 kmem_cache_destroy(iommu_iova_cache);
2914
2915}
2916
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002917static void __init init_no_remapping_devices(void)
2918{
2919 struct dmar_drhd_unit *drhd;
2920
2921 for_each_drhd_unit(drhd) {
2922 if (!drhd->include_all) {
2923 int i;
2924 for (i = 0; i < drhd->devices_cnt; i++)
2925 if (drhd->devices[i] != NULL)
2926 break;
2927 /* ignore DMAR unit if no pci devices exist */
2928 if (i == drhd->devices_cnt)
2929 drhd->ignored = 1;
2930 }
2931 }
2932
2933 if (dmar_map_gfx)
2934 return;
2935
2936 for_each_drhd_unit(drhd) {
2937 int i;
2938 if (drhd->ignored || drhd->include_all)
2939 continue;
2940
2941 for (i = 0; i < drhd->devices_cnt; i++)
2942 if (drhd->devices[i] &&
2943 !IS_GFX_DEVICE(drhd->devices[i]))
2944 break;
2945
2946 if (i < drhd->devices_cnt)
2947 continue;
2948
2949 /* bypass IOMMU if it is just for gfx devices */
2950 drhd->ignored = 1;
2951 for (i = 0; i < drhd->devices_cnt; i++) {
2952 if (!drhd->devices[i])
2953 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002954 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002955 }
2956 }
2957}
2958
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002959#ifdef CONFIG_SUSPEND
2960static int init_iommu_hw(void)
2961{
2962 struct dmar_drhd_unit *drhd;
2963 struct intel_iommu *iommu = NULL;
2964
2965 for_each_active_iommu(iommu, drhd)
2966 if (iommu->qi)
2967 dmar_reenable_qi(iommu);
2968
2969 for_each_active_iommu(iommu, drhd) {
2970 iommu_flush_write_buffer(iommu);
2971
2972 iommu_set_root_entry(iommu);
2973
2974 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002975 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002976 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002977 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002978 iommu_disable_protect_mem_regions(iommu);
2979 iommu_enable_translation(iommu);
2980 }
2981
2982 return 0;
2983}
2984
2985static void iommu_flush_all(void)
2986{
2987 struct dmar_drhd_unit *drhd;
2988 struct intel_iommu *iommu;
2989
2990 for_each_active_iommu(iommu, drhd) {
2991 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002992 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002993 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002994 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002995 }
2996}
2997
2998static int iommu_suspend(struct sys_device *dev, pm_message_t state)
2999{
3000 struct dmar_drhd_unit *drhd;
3001 struct intel_iommu *iommu = NULL;
3002 unsigned long flag;
3003
3004 for_each_active_iommu(iommu, drhd) {
3005 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3006 GFP_ATOMIC);
3007 if (!iommu->iommu_state)
3008 goto nomem;
3009 }
3010
3011 iommu_flush_all();
3012
3013 for_each_active_iommu(iommu, drhd) {
3014 iommu_disable_translation(iommu);
3015
3016 spin_lock_irqsave(&iommu->register_lock, flag);
3017
3018 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3019 readl(iommu->reg + DMAR_FECTL_REG);
3020 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3021 readl(iommu->reg + DMAR_FEDATA_REG);
3022 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3023 readl(iommu->reg + DMAR_FEADDR_REG);
3024 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3025 readl(iommu->reg + DMAR_FEUADDR_REG);
3026
3027 spin_unlock_irqrestore(&iommu->register_lock, flag);
3028 }
3029 return 0;
3030
3031nomem:
3032 for_each_active_iommu(iommu, drhd)
3033 kfree(iommu->iommu_state);
3034
3035 return -ENOMEM;
3036}
3037
3038static int iommu_resume(struct sys_device *dev)
3039{
3040 struct dmar_drhd_unit *drhd;
3041 struct intel_iommu *iommu = NULL;
3042 unsigned long flag;
3043
3044 if (init_iommu_hw()) {
3045 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
3046 return -EIO;
3047 }
3048
3049 for_each_active_iommu(iommu, drhd) {
3050
3051 spin_lock_irqsave(&iommu->register_lock, flag);
3052
3053 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3054 iommu->reg + DMAR_FECTL_REG);
3055 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3056 iommu->reg + DMAR_FEDATA_REG);
3057 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3058 iommu->reg + DMAR_FEADDR_REG);
3059 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3060 iommu->reg + DMAR_FEUADDR_REG);
3061
3062 spin_unlock_irqrestore(&iommu->register_lock, flag);
3063 }
3064
3065 for_each_active_iommu(iommu, drhd)
3066 kfree(iommu->iommu_state);
3067
3068 return 0;
3069}
3070
3071static struct sysdev_class iommu_sysclass = {
3072 .name = "iommu",
3073 .resume = iommu_resume,
3074 .suspend = iommu_suspend,
3075};
3076
3077static struct sys_device device_iommu = {
3078 .cls = &iommu_sysclass,
3079};
3080
3081static int __init init_iommu_sysfs(void)
3082{
3083 int error;
3084
3085 error = sysdev_class_register(&iommu_sysclass);
3086 if (error)
3087 return error;
3088
3089 error = sysdev_register(&device_iommu);
3090 if (error)
3091 sysdev_class_unregister(&iommu_sysclass);
3092
3093 return error;
3094}
3095
3096#else
3097static int __init init_iommu_sysfs(void)
3098{
3099 return 0;
3100}
3101#endif /* CONFIG_PM */
3102
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003103int __init intel_iommu_init(void)
3104{
3105 int ret = 0;
3106
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003107 if (dmar_table_init())
3108 return -ENODEV;
3109
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003110 if (dmar_dev_scope_init())
3111 return -ENODEV;
3112
Suresh Siddha2ae21012008-07-10 11:16:43 -07003113 /*
3114 * Check the need for DMA-remapping initialization now.
3115 * Above initialization will also be used by Interrupt-remapping.
3116 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003117 if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled)
Suresh Siddha2ae21012008-07-10 11:16:43 -07003118 return -ENODEV;
3119
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003120 iommu_init_mempool();
3121 dmar_init_reserved_ranges();
3122
3123 init_no_remapping_devices();
3124
3125 ret = init_dmars();
3126 if (ret) {
3127 printk(KERN_ERR "IOMMU: dmar init failed\n");
3128 put_iova_domain(&reserved_iova_list);
3129 iommu_exit_mempool();
3130 return ret;
3131 }
3132 printk(KERN_INFO
3133 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
3134
mark gross5e0d2a62008-03-04 15:22:08 -08003135 init_timer(&unmap_timer);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003136 force_iommu = 1;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003137
3138 if (!iommu_pass_through) {
3139 printk(KERN_INFO
3140 "Multi-level page-table translation for DMAR.\n");
3141 dma_ops = &intel_dma_ops;
3142 } else
3143 printk(KERN_INFO
3144 "DMAR: Pass through translation for DMAR.\n");
3145
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003146 init_iommu_sysfs();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003147
3148 register_iommu(&intel_iommu_ops);
3149
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003150 return 0;
3151}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07003152
Han, Weidong3199aa62009-02-26 17:31:12 +08003153static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
3154 struct pci_dev *pdev)
3155{
3156 struct pci_dev *tmp, *parent;
3157
3158 if (!iommu || !pdev)
3159 return;
3160
3161 /* dependent device detach */
3162 tmp = pci_find_upstream_pcie_bridge(pdev);
3163 /* Secondary interface's bus number and devfn 0 */
3164 if (tmp) {
3165 parent = pdev->bus->self;
3166 while (parent != tmp) {
3167 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01003168 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003169 parent = parent->bus->self;
3170 }
3171 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
3172 iommu_detach_dev(iommu,
3173 tmp->subordinate->number, 0);
3174 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01003175 iommu_detach_dev(iommu, tmp->bus->number,
3176 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003177 }
3178}
3179
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003180static void domain_remove_one_dev_info(struct dmar_domain *domain,
Weidong Hanc7151a82008-12-08 22:51:37 +08003181 struct pci_dev *pdev)
3182{
3183 struct device_domain_info *info;
3184 struct intel_iommu *iommu;
3185 unsigned long flags;
3186 int found = 0;
3187 struct list_head *entry, *tmp;
3188
David Woodhouse276dbf992009-04-04 01:45:37 +01003189 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3190 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003191 if (!iommu)
3192 return;
3193
3194 spin_lock_irqsave(&device_domain_lock, flags);
3195 list_for_each_safe(entry, tmp, &domain->devices) {
3196 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01003197 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08003198 if (info->bus == pdev->bus->number &&
3199 info->devfn == pdev->devfn) {
3200 list_del(&info->link);
3201 list_del(&info->global);
3202 if (info->dev)
3203 info->dev->dev.archdata.iommu = NULL;
3204 spin_unlock_irqrestore(&device_domain_lock, flags);
3205
Yu Zhao93a23a72009-05-18 13:51:37 +08003206 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08003207 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003208 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003209 free_devinfo_mem(info);
3210
3211 spin_lock_irqsave(&device_domain_lock, flags);
3212
3213 if (found)
3214 break;
3215 else
3216 continue;
3217 }
3218
3219 /* if there is no other devices under the same iommu
3220 * owned by this domain, clear this iommu in iommu_bmp
3221 * update iommu count and coherency
3222 */
David Woodhouse276dbf992009-04-04 01:45:37 +01003223 if (iommu == device_to_iommu(info->segment, info->bus,
3224 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08003225 found = 1;
3226 }
3227
3228 if (found == 0) {
3229 unsigned long tmp_flags;
3230 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3231 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3232 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003233 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003234 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
3235 }
3236
3237 spin_unlock_irqrestore(&device_domain_lock, flags);
3238}
3239
3240static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3241{
3242 struct device_domain_info *info;
3243 struct intel_iommu *iommu;
3244 unsigned long flags1, flags2;
3245
3246 spin_lock_irqsave(&device_domain_lock, flags1);
3247 while (!list_empty(&domain->devices)) {
3248 info = list_entry(domain->devices.next,
3249 struct device_domain_info, link);
3250 list_del(&info->link);
3251 list_del(&info->global);
3252 if (info->dev)
3253 info->dev->dev.archdata.iommu = NULL;
3254
3255 spin_unlock_irqrestore(&device_domain_lock, flags1);
3256
Yu Zhao93a23a72009-05-18 13:51:37 +08003257 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01003258 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003259 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003260 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003261
3262 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08003263 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08003264 */
3265 spin_lock_irqsave(&domain->iommu_lock, flags2);
3266 if (test_and_clear_bit(iommu->seq_id,
3267 &domain->iommu_bmp)) {
3268 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003269 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003270 }
3271 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3272
3273 free_devinfo_mem(info);
3274 spin_lock_irqsave(&device_domain_lock, flags1);
3275 }
3276 spin_unlock_irqrestore(&device_domain_lock, flags1);
3277}
3278
Weidong Han5e98c4b2008-12-08 23:03:27 +08003279/* domain id for virtual machine, it won't be set in context */
3280static unsigned long vm_domid;
3281
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003282static int vm_domain_min_agaw(struct dmar_domain *domain)
3283{
3284 int i;
3285 int min_agaw = domain->agaw;
3286
3287 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
3288 for (; i < g_num_of_iommus; ) {
3289 if (min_agaw > g_iommus[i]->agaw)
3290 min_agaw = g_iommus[i]->agaw;
3291
3292 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
3293 }
3294
3295 return min_agaw;
3296}
3297
Weidong Han5e98c4b2008-12-08 23:03:27 +08003298static struct dmar_domain *iommu_alloc_vm_domain(void)
3299{
3300 struct dmar_domain *domain;
3301
3302 domain = alloc_domain_mem();
3303 if (!domain)
3304 return NULL;
3305
3306 domain->id = vm_domid++;
3307 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3308 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3309
3310 return domain;
3311}
3312
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003313static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08003314{
3315 int adjust_width;
3316
3317 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3318 spin_lock_init(&domain->mapping_lock);
3319 spin_lock_init(&domain->iommu_lock);
3320
3321 domain_reserve_special_ranges(domain);
3322
3323 /* calculate AGAW */
3324 domain->gaw = guest_width;
3325 adjust_width = guestwidth_to_adjustwidth(guest_width);
3326 domain->agaw = width_to_agaw(adjust_width);
3327
3328 INIT_LIST_HEAD(&domain->devices);
3329
3330 domain->iommu_count = 0;
3331 domain->iommu_coherency = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003332 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003333
3334 /* always allocate the top pgd */
3335 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3336 if (!domain->pgd)
3337 return -ENOMEM;
3338 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3339 return 0;
3340}
3341
3342static void iommu_free_vm_domain(struct dmar_domain *domain)
3343{
3344 unsigned long flags;
3345 struct dmar_drhd_unit *drhd;
3346 struct intel_iommu *iommu;
3347 unsigned long i;
3348 unsigned long ndomains;
3349
3350 for_each_drhd_unit(drhd) {
3351 if (drhd->ignored)
3352 continue;
3353 iommu = drhd->iommu;
3354
3355 ndomains = cap_ndoms(iommu->cap);
3356 i = find_first_bit(iommu->domain_ids, ndomains);
3357 for (; i < ndomains; ) {
3358 if (iommu->domains[i] == domain) {
3359 spin_lock_irqsave(&iommu->lock, flags);
3360 clear_bit(i, iommu->domain_ids);
3361 iommu->domains[i] = NULL;
3362 spin_unlock_irqrestore(&iommu->lock, flags);
3363 break;
3364 }
3365 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3366 }
3367 }
3368}
3369
3370static void vm_domain_exit(struct dmar_domain *domain)
3371{
3372 u64 end;
3373
3374 /* Domain 0 is reserved, so dont process it */
3375 if (!domain)
3376 return;
3377
3378 vm_domain_remove_all_dev_info(domain);
3379 /* destroy iovas */
3380 put_iova_domain(&domain->iovad);
3381 end = DOMAIN_MAX_ADDR(domain->gaw);
3382 end = end & (~VTD_PAGE_MASK);
3383
3384 /* clear ptes */
3385 dma_pte_clear_range(domain, 0, end);
3386
3387 /* free page tables */
3388 dma_pte_free_pagetable(domain, 0, end);
3389
3390 iommu_free_vm_domain(domain);
3391 free_domain_mem(domain);
3392}
3393
Joerg Roedel5d450802008-12-03 14:52:32 +01003394static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003395{
Joerg Roedel5d450802008-12-03 14:52:32 +01003396 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003397
Joerg Roedel5d450802008-12-03 14:52:32 +01003398 dmar_domain = iommu_alloc_vm_domain();
3399 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003400 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003401 "intel_iommu_domain_init: dmar_domain == NULL\n");
3402 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003403 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003404 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003405 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003406 "intel_iommu_domain_init() failed\n");
3407 vm_domain_exit(dmar_domain);
3408 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003409 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003410 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003411
Joerg Roedel5d450802008-12-03 14:52:32 +01003412 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003413}
Kay, Allen M38717942008-09-09 18:37:29 +03003414
Joerg Roedel5d450802008-12-03 14:52:32 +01003415static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003416{
Joerg Roedel5d450802008-12-03 14:52:32 +01003417 struct dmar_domain *dmar_domain = domain->priv;
3418
3419 domain->priv = NULL;
3420 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003421}
Kay, Allen M38717942008-09-09 18:37:29 +03003422
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003423static int intel_iommu_attach_device(struct iommu_domain *domain,
3424 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003425{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003426 struct dmar_domain *dmar_domain = domain->priv;
3427 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003428 struct intel_iommu *iommu;
3429 int addr_width;
3430 u64 end;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003431 int ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003432
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003433 /* normally pdev is not mapped */
3434 if (unlikely(domain_context_mapped(pdev))) {
3435 struct dmar_domain *old_domain;
3436
3437 old_domain = find_domain(pdev);
3438 if (old_domain) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003439 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
3440 dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)
3441 domain_remove_one_dev_info(old_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003442 else
3443 domain_remove_dev_info(old_domain);
3444 }
3445 }
3446
David Woodhouse276dbf992009-04-04 01:45:37 +01003447 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3448 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003449 if (!iommu)
3450 return -ENODEV;
3451
3452 /* check if this iommu agaw is sufficient for max mapped address */
3453 addr_width = agaw_to_width(iommu->agaw);
3454 end = DOMAIN_MAX_ADDR(addr_width);
3455 end = end & VTD_PAGE_MASK;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003456 if (end < dmar_domain->max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003457 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3458 "sufficient for the mapped address (%llx)\n",
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003459 __func__, iommu->agaw, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003460 return -EFAULT;
3461 }
3462
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003463 ret = domain_add_dev_info(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003464 if (ret)
3465 return ret;
3466
Yu Zhao93a23a72009-05-18 13:51:37 +08003467 ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003468 return ret;
3469}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003470
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003471static void intel_iommu_detach_device(struct iommu_domain *domain,
3472 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003473{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003474 struct dmar_domain *dmar_domain = domain->priv;
3475 struct pci_dev *pdev = to_pci_dev(dev);
3476
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003477 domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003478}
Kay, Allen M38717942008-09-09 18:37:29 +03003479
Joerg Roedeldde57a22008-12-03 15:04:09 +01003480static int intel_iommu_map_range(struct iommu_domain *domain,
3481 unsigned long iova, phys_addr_t hpa,
3482 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003483{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003484 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003485 u64 max_addr;
3486 int addr_width;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003487 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003488 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003489
Joerg Roedeldde57a22008-12-03 15:04:09 +01003490 if (iommu_prot & IOMMU_READ)
3491 prot |= DMA_PTE_READ;
3492 if (iommu_prot & IOMMU_WRITE)
3493 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08003494 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3495 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003496
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003497 max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003498 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003499 int min_agaw;
3500 u64 end;
3501
3502 /* check if minimum agaw is sufficient for mapped address */
Joerg Roedeldde57a22008-12-03 15:04:09 +01003503 min_agaw = vm_domain_min_agaw(dmar_domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003504 addr_width = agaw_to_width(min_agaw);
3505 end = DOMAIN_MAX_ADDR(addr_width);
3506 end = end & VTD_PAGE_MASK;
3507 if (end < max_addr) {
3508 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3509 "sufficient for the mapped address (%llx)\n",
3510 __func__, min_agaw, max_addr);
3511 return -EFAULT;
3512 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003513 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003514 }
3515
Joerg Roedeldde57a22008-12-03 15:04:09 +01003516 ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003517 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003518}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003519
Joerg Roedeldde57a22008-12-03 15:04:09 +01003520static void intel_iommu_unmap_range(struct iommu_domain *domain,
3521 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003522{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003523 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003524 dma_addr_t base;
3525
3526 /* The address might not be aligned */
3527 base = iova & VTD_PAGE_MASK;
3528 size = VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003529 dma_pte_clear_range(dmar_domain, base, base + size);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003530
Joerg Roedeldde57a22008-12-03 15:04:09 +01003531 if (dmar_domain->max_addr == base + size)
3532 dmar_domain->max_addr = base;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003533}
Kay, Allen M38717942008-09-09 18:37:29 +03003534
Joerg Roedeld14d6572008-12-03 15:06:57 +01003535static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3536 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003537{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003538 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003539 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003540 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003541
Joerg Roedeld14d6572008-12-03 15:06:57 +01003542 pte = addr_to_dma_pte(dmar_domain, iova);
Kay, Allen M38717942008-09-09 18:37:29 +03003543 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003544 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003545
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003546 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003547}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003548
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003549static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3550 unsigned long cap)
3551{
3552 struct dmar_domain *dmar_domain = domain->priv;
3553
3554 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3555 return dmar_domain->iommu_snooping;
3556
3557 return 0;
3558}
3559
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003560static struct iommu_ops intel_iommu_ops = {
3561 .domain_init = intel_iommu_domain_init,
3562 .domain_destroy = intel_iommu_domain_destroy,
3563 .attach_dev = intel_iommu_attach_device,
3564 .detach_dev = intel_iommu_detach_device,
3565 .map = intel_iommu_map_range,
3566 .unmap = intel_iommu_unmap_range,
3567 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003568 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003569};
David Woodhouse9af88142009-02-13 23:18:03 +00003570
3571static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3572{
3573 /*
3574 * Mobile 4 Series Chipset neglects to set RWBF capability,
3575 * but needs it:
3576 */
3577 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3578 rwbf_quirk = 1;
3579}
3580
3581DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);