blob: fc121967cb5b83105152757e65ba6cb9ea5981b7 [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
Weidong Hand9630fe2008-12-08 11:06:32 +080068/* global iommu list, set NULL for ignored DMAR units */
69static struct intel_iommu **g_iommus;
70
David Woodhouse9af88142009-02-13 23:18:03 +000071static int rwbf_quirk;
72
Mark McLoughlin46b08e12008-11-20 15:49:44 +000073/*
74 * 0: Present
75 * 1-11: Reserved
76 * 12-63: Context Ptr (12 - (haw-1))
77 * 64-127: Reserved
78 */
79struct root_entry {
80 u64 val;
81 u64 rsvd1;
82};
83#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
84static inline bool root_present(struct root_entry *root)
85{
86 return (root->val & 1);
87}
88static inline void set_root_present(struct root_entry *root)
89{
90 root->val |= 1;
91}
92static inline void set_root_value(struct root_entry *root, unsigned long value)
93{
94 root->val |= value & VTD_PAGE_MASK;
95}
96
97static inline struct context_entry *
98get_context_addr_from_root(struct root_entry *root)
99{
100 return (struct context_entry *)
101 (root_present(root)?phys_to_virt(
102 root->val & VTD_PAGE_MASK) :
103 NULL);
104}
105
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000106/*
107 * low 64 bits:
108 * 0: present
109 * 1: fault processing disable
110 * 2-3: translation type
111 * 12-63: address space root
112 * high 64 bits:
113 * 0-2: address width
114 * 3-6: aval
115 * 8-23: domain id
116 */
117struct context_entry {
118 u64 lo;
119 u64 hi;
120};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000121
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000122static inline bool context_present(struct context_entry *context)
123{
124 return (context->lo & 1);
125}
126static inline void context_set_present(struct context_entry *context)
127{
128 context->lo |= 1;
129}
130
131static inline void context_set_fault_enable(struct context_entry *context)
132{
133 context->lo &= (((u64)-1) << 2) | 1;
134}
135
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000136static inline void context_set_translation_type(struct context_entry *context,
137 unsigned long value)
138{
139 context->lo &= (((u64)-1) << 4) | 3;
140 context->lo |= (value & 3) << 2;
141}
142
143static inline void context_set_address_root(struct context_entry *context,
144 unsigned long value)
145{
146 context->lo |= value & VTD_PAGE_MASK;
147}
148
149static inline void context_set_address_width(struct context_entry *context,
150 unsigned long value)
151{
152 context->hi |= value & 7;
153}
154
155static inline void context_set_domain_id(struct context_entry *context,
156 unsigned long value)
157{
158 context->hi |= (value & ((1 << 16) - 1)) << 8;
159}
160
161static inline void context_clear_entry(struct context_entry *context)
162{
163 context->lo = 0;
164 context->hi = 0;
165}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000166
Mark McLoughlin622ba122008-11-20 15:49:46 +0000167/*
168 * 0: readable
169 * 1: writable
170 * 2-6: reserved
171 * 7: super page
Sheng Yang9cf066972009-03-18 15:33:07 +0800172 * 8-10: available
173 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000174 * 12-63: Host physcial address
175 */
176struct dma_pte {
177 u64 val;
178};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000179
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000180static inline void dma_clear_pte(struct dma_pte *pte)
181{
182 pte->val = 0;
183}
184
185static inline void dma_set_pte_readable(struct dma_pte *pte)
186{
187 pte->val |= DMA_PTE_READ;
188}
189
190static inline void dma_set_pte_writable(struct dma_pte *pte)
191{
192 pte->val |= DMA_PTE_WRITE;
193}
194
Sheng Yang9cf066972009-03-18 15:33:07 +0800195static inline void dma_set_pte_snp(struct dma_pte *pte)
196{
197 pte->val |= DMA_PTE_SNP;
198}
199
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000200static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
201{
202 pte->val = (pte->val & ~3) | (prot & 3);
203}
204
205static inline u64 dma_pte_addr(struct dma_pte *pte)
206{
207 return (pte->val & VTD_PAGE_MASK);
208}
209
210static inline void dma_set_pte_addr(struct dma_pte *pte, u64 addr)
211{
212 pte->val |= (addr & VTD_PAGE_MASK);
213}
214
215static inline bool dma_pte_present(struct dma_pte *pte)
216{
217 return (pte->val & 3) != 0;
218}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000219
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700220/*
221 * This domain is a statically identity mapping domain.
222 * 1. This domain creats a static 1:1 mapping to all usable memory.
223 * 2. It maps to each iommu if successful.
224 * 3. Each iommu mapps to this domain if successful.
225 */
226struct dmar_domain *si_domain;
227
Weidong Han3b5410e2008-12-08 09:17:15 +0800228/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100229#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800230
Weidong Han1ce28fe2008-12-08 16:35:39 +0800231/* domain represents a virtual machine, more than one devices
232 * across iommus may be owned in one domain, e.g. kvm guest.
233 */
234#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
235
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700236/* si_domain contains mulitple devices */
237#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2)
238
Mark McLoughlin99126f72008-11-20 15:49:47 +0000239struct dmar_domain {
240 int id; /* domain id */
Weidong Han8c11e792008-12-08 15:29:22 +0800241 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000242
243 struct list_head devices; /* all devices' list */
244 struct iova_domain iovad; /* iova's that belong to this domain */
245
246 struct dma_pte *pgd; /* virtual address */
247 spinlock_t mapping_lock; /* page table lock */
248 int gaw; /* max guest address width */
249
250 /* adjusted guest address width, 0 is level 2 30-bit */
251 int agaw;
252
Weidong Han3b5410e2008-12-08 09:17:15 +0800253 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800254
255 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800256 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800257 int iommu_count; /* reference count of iommu */
258 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800259 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000260};
261
Mark McLoughlina647dac2008-11-20 15:49:48 +0000262/* PCI domain-device relationship */
263struct device_domain_info {
264 struct list_head link; /* link to domain siblings */
265 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100266 int segment; /* PCI domain */
267 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000268 u8 devfn; /* PCI devfn number */
269 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800270 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000271 struct dmar_domain *domain; /* pointer to domain */
272};
273
mark gross5e0d2a62008-03-04 15:22:08 -0800274static void flush_unmaps_timeout(unsigned long data);
275
276DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
277
mark gross80b20dd2008-04-18 13:53:58 -0700278#define HIGH_WATER_MARK 250
279struct deferred_flush_tables {
280 int next;
281 struct iova *iova[HIGH_WATER_MARK];
282 struct dmar_domain *domain[HIGH_WATER_MARK];
283};
284
285static struct deferred_flush_tables *deferred_flush;
286
mark gross5e0d2a62008-03-04 15:22:08 -0800287/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800288static int g_num_of_iommus;
289
290static DEFINE_SPINLOCK(async_umap_flush_lock);
291static LIST_HEAD(unmaps_to_do);
292
293static int timer_on;
294static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800295
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700296static void domain_remove_dev_info(struct dmar_domain *domain);
297
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800298#ifdef CONFIG_DMAR_DEFAULT_ON
299int dmar_disabled = 0;
300#else
301int dmar_disabled = 1;
302#endif /*CONFIG_DMAR_DEFAULT_ON*/
303
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700304static int __initdata dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700305static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800306static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700307
308#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
309static DEFINE_SPINLOCK(device_domain_lock);
310static LIST_HEAD(device_domain_list);
311
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100312static struct iommu_ops intel_iommu_ops;
313
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700314static int __init intel_iommu_setup(char *str)
315{
316 if (!str)
317 return -EINVAL;
318 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800319 if (!strncmp(str, "on", 2)) {
320 dmar_disabled = 0;
321 printk(KERN_INFO "Intel-IOMMU: enabled\n");
322 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700323 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800324 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700325 } else if (!strncmp(str, "igfx_off", 8)) {
326 dmar_map_gfx = 0;
327 printk(KERN_INFO
328 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700329 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800330 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700331 "Intel-IOMMU: Forcing DAC for PCI devices\n");
332 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800333 } else if (!strncmp(str, "strict", 6)) {
334 printk(KERN_INFO
335 "Intel-IOMMU: disable batched IOTLB flush\n");
336 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700337 }
338
339 str += strcspn(str, ",");
340 while (*str == ',')
341 str++;
342 }
343 return 0;
344}
345__setup("intel_iommu=", intel_iommu_setup);
346
347static struct kmem_cache *iommu_domain_cache;
348static struct kmem_cache *iommu_devinfo_cache;
349static struct kmem_cache *iommu_iova_cache;
350
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700351static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
352{
353 unsigned int flags;
354 void *vaddr;
355
356 /* trying to avoid low memory issues */
357 flags = current->flags & PF_MEMALLOC;
358 current->flags |= PF_MEMALLOC;
359 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
360 current->flags &= (~PF_MEMALLOC | flags);
361 return vaddr;
362}
363
364
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700365static inline void *alloc_pgtable_page(void)
366{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700367 unsigned int flags;
368 void *vaddr;
369
370 /* trying to avoid low memory issues */
371 flags = current->flags & PF_MEMALLOC;
372 current->flags |= PF_MEMALLOC;
373 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
374 current->flags &= (~PF_MEMALLOC | flags);
375 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700376}
377
378static inline void free_pgtable_page(void *vaddr)
379{
380 free_page((unsigned long)vaddr);
381}
382
383static inline void *alloc_domain_mem(void)
384{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700385 return iommu_kmem_cache_alloc(iommu_domain_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700386}
387
Kay, Allen M38717942008-09-09 18:37:29 +0300388static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700389{
390 kmem_cache_free(iommu_domain_cache, vaddr);
391}
392
393static inline void * alloc_devinfo_mem(void)
394{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700395 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700396}
397
398static inline void free_devinfo_mem(void *vaddr)
399{
400 kmem_cache_free(iommu_devinfo_cache, vaddr);
401}
402
403struct iova *alloc_iova_mem(void)
404{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700405 return iommu_kmem_cache_alloc(iommu_iova_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700406}
407
408void free_iova_mem(struct iova *iova)
409{
410 kmem_cache_free(iommu_iova_cache, iova);
411}
412
Weidong Han1b573682008-12-08 15:34:06 +0800413
414static inline int width_to_agaw(int width);
415
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700416static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800417{
418 unsigned long sagaw;
419 int agaw = -1;
420
421 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700422 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800423 agaw >= 0; agaw--) {
424 if (test_bit(agaw, &sagaw))
425 break;
426 }
427
428 return agaw;
429}
430
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700431/*
432 * Calculate max SAGAW for each iommu.
433 */
434int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
435{
436 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
437}
438
439/*
440 * calculate agaw for each iommu.
441 * "SAGAW" may be different across iommus, use a default agaw, and
442 * get a supported less agaw for iommus that don't support the default agaw.
443 */
444int iommu_calculate_agaw(struct intel_iommu *iommu)
445{
446 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
447}
448
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700449/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800450static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
451{
452 int iommu_id;
453
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700454 /* si_domain and vm domain should not get here. */
Weidong Han1ce28fe2008-12-08 16:35:39 +0800455 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700456 BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
Weidong Han1ce28fe2008-12-08 16:35:39 +0800457
Weidong Han8c11e792008-12-08 15:29:22 +0800458 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
459 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
460 return NULL;
461
462 return g_iommus[iommu_id];
463}
464
Weidong Han8e6040972008-12-08 15:49:06 +0800465static void domain_update_iommu_coherency(struct dmar_domain *domain)
466{
467 int i;
468
469 domain->iommu_coherency = 1;
470
471 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
472 for (; i < g_num_of_iommus; ) {
473 if (!ecap_coherent(g_iommus[i]->ecap)) {
474 domain->iommu_coherency = 0;
475 break;
476 }
477 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
478 }
479}
480
Sheng Yang58c610b2009-03-18 15:33:05 +0800481static void domain_update_iommu_snooping(struct dmar_domain *domain)
482{
483 int i;
484
485 domain->iommu_snooping = 1;
486
487 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
488 for (; i < g_num_of_iommus; ) {
489 if (!ecap_sc_support(g_iommus[i]->ecap)) {
490 domain->iommu_snooping = 0;
491 break;
492 }
493 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
494 }
495}
496
497/* Some capabilities may be different across iommus */
498static void domain_update_iommu_cap(struct dmar_domain *domain)
499{
500 domain_update_iommu_coherency(domain);
501 domain_update_iommu_snooping(domain);
502}
503
David Woodhouse276dbf992009-04-04 01:45:37 +0100504static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800505{
506 struct dmar_drhd_unit *drhd = NULL;
507 int i;
508
509 for_each_drhd_unit(drhd) {
510 if (drhd->ignored)
511 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100512 if (segment != drhd->segment)
513 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800514
David Woodhouse924b6232009-04-04 00:39:25 +0100515 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000516 if (drhd->devices[i] &&
517 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800518 drhd->devices[i]->devfn == devfn)
519 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700520 if (drhd->devices[i] &&
521 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100522 drhd->devices[i]->subordinate->number <= bus &&
523 drhd->devices[i]->subordinate->subordinate >= bus)
524 return drhd->iommu;
525 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800526
527 if (drhd->include_all)
528 return drhd->iommu;
529 }
530
531 return NULL;
532}
533
Weidong Han5331fe62008-12-08 23:00:00 +0800534static void domain_flush_cache(struct dmar_domain *domain,
535 void *addr, int size)
536{
537 if (!domain->iommu_coherency)
538 clflush_cache_range(addr, size);
539}
540
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700541/* Gets context entry for a given bus and devfn */
542static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
543 u8 bus, u8 devfn)
544{
545 struct root_entry *root;
546 struct context_entry *context;
547 unsigned long phy_addr;
548 unsigned long flags;
549
550 spin_lock_irqsave(&iommu->lock, flags);
551 root = &iommu->root_entry[bus];
552 context = get_context_addr_from_root(root);
553 if (!context) {
554 context = (struct context_entry *)alloc_pgtable_page();
555 if (!context) {
556 spin_unlock_irqrestore(&iommu->lock, flags);
557 return NULL;
558 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700559 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700560 phy_addr = virt_to_phys((void *)context);
561 set_root_value(root, phy_addr);
562 set_root_present(root);
563 __iommu_flush_cache(iommu, root, sizeof(*root));
564 }
565 spin_unlock_irqrestore(&iommu->lock, flags);
566 return &context[devfn];
567}
568
569static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
570{
571 struct root_entry *root;
572 struct context_entry *context;
573 int ret;
574 unsigned long flags;
575
576 spin_lock_irqsave(&iommu->lock, flags);
577 root = &iommu->root_entry[bus];
578 context = get_context_addr_from_root(root);
579 if (!context) {
580 ret = 0;
581 goto out;
582 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000583 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700584out:
585 spin_unlock_irqrestore(&iommu->lock, flags);
586 return ret;
587}
588
589static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
590{
591 struct root_entry *root;
592 struct context_entry *context;
593 unsigned long flags;
594
595 spin_lock_irqsave(&iommu->lock, flags);
596 root = &iommu->root_entry[bus];
597 context = get_context_addr_from_root(root);
598 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000599 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700600 __iommu_flush_cache(iommu, &context[devfn], \
601 sizeof(*context));
602 }
603 spin_unlock_irqrestore(&iommu->lock, flags);
604}
605
606static void free_context_table(struct intel_iommu *iommu)
607{
608 struct root_entry *root;
609 int i;
610 unsigned long flags;
611 struct context_entry *context;
612
613 spin_lock_irqsave(&iommu->lock, flags);
614 if (!iommu->root_entry) {
615 goto out;
616 }
617 for (i = 0; i < ROOT_ENTRY_NR; i++) {
618 root = &iommu->root_entry[i];
619 context = get_context_addr_from_root(root);
620 if (context)
621 free_pgtable_page(context);
622 }
623 free_pgtable_page(iommu->root_entry);
624 iommu->root_entry = NULL;
625out:
626 spin_unlock_irqrestore(&iommu->lock, flags);
627}
628
629/* page table handling */
630#define LEVEL_STRIDE (9)
631#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
632
633static inline int agaw_to_level(int agaw)
634{
635 return agaw + 2;
636}
637
638static inline int agaw_to_width(int agaw)
639{
640 return 30 + agaw * LEVEL_STRIDE;
641
642}
643
644static inline int width_to_agaw(int width)
645{
646 return (width - 30) / LEVEL_STRIDE;
647}
648
649static inline unsigned int level_to_offset_bits(int level)
650{
651 return (12 + (level - 1) * LEVEL_STRIDE);
652}
653
654static inline int address_level_offset(u64 addr, int level)
655{
656 return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
657}
658
659static inline u64 level_mask(int level)
660{
661 return ((u64)-1 << level_to_offset_bits(level));
662}
663
664static inline u64 level_size(int level)
665{
666 return ((u64)1 << level_to_offset_bits(level));
667}
668
669static inline u64 align_to_level(u64 addr, int level)
670{
671 return ((addr + level_size(level) - 1) & level_mask(level));
672}
673
674static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
675{
676 int addr_width = agaw_to_width(domain->agaw);
677 struct dma_pte *parent, *pte = NULL;
678 int level = agaw_to_level(domain->agaw);
679 int offset;
680 unsigned long flags;
681
682 BUG_ON(!domain->pgd);
683
684 addr &= (((u64)1) << addr_width) - 1;
685 parent = domain->pgd;
686
687 spin_lock_irqsave(&domain->mapping_lock, flags);
688 while (level > 0) {
689 void *tmp_page;
690
691 offset = address_level_offset(addr, level);
692 pte = &parent[offset];
693 if (level == 1)
694 break;
695
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000696 if (!dma_pte_present(pte)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700697 tmp_page = alloc_pgtable_page();
698
699 if (!tmp_page) {
700 spin_unlock_irqrestore(&domain->mapping_lock,
701 flags);
702 return NULL;
703 }
Weidong Han5331fe62008-12-08 23:00:00 +0800704 domain_flush_cache(domain, tmp_page, PAGE_SIZE);
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000705 dma_set_pte_addr(pte, virt_to_phys(tmp_page));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700706 /*
707 * high level table always sets r/w, last level page
708 * table control read/write
709 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000710 dma_set_pte_readable(pte);
711 dma_set_pte_writable(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800712 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700713 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000714 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700715 level--;
716 }
717
718 spin_unlock_irqrestore(&domain->mapping_lock, flags);
719 return pte;
720}
721
722/* return address's pte at specific level */
723static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
724 int level)
725{
726 struct dma_pte *parent, *pte = NULL;
727 int total = agaw_to_level(domain->agaw);
728 int offset;
729
730 parent = domain->pgd;
731 while (level <= total) {
732 offset = address_level_offset(addr, total);
733 pte = &parent[offset];
734 if (level == total)
735 return pte;
736
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000737 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700738 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000739 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700740 total--;
741 }
742 return NULL;
743}
744
745/* clear one page's page table */
746static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
747{
748 struct dma_pte *pte = NULL;
749
750 /* get last level pte */
751 pte = dma_addr_level_pte(domain, addr, 1);
752
753 if (pte) {
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000754 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800755 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700756 }
757}
758
759/* clear last level pte, a tlb flush should be followed */
760static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
761{
762 int addr_width = agaw_to_width(domain->agaw);
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800763 int npages;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700764
765 start &= (((u64)1) << addr_width) - 1;
766 end &= (((u64)1) << addr_width) - 1;
767 /* in case it's partial page */
Fenghua Yu31d35682009-04-06 11:21:49 -0700768 start &= PAGE_MASK;
769 end = PAGE_ALIGN(end);
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800770 npages = (end - start) / VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700771
772 /* we don't need lock here, nobody else touches the iova range */
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800773 while (npages--) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700774 dma_pte_clear_one(domain, start);
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700775 start += VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700776 }
777}
778
779/* free page table pages. last level pte should already be cleared */
780static void dma_pte_free_pagetable(struct dmar_domain *domain,
781 u64 start, u64 end)
782{
783 int addr_width = agaw_to_width(domain->agaw);
784 struct dma_pte *pte;
785 int total = agaw_to_level(domain->agaw);
786 int level;
787 u64 tmp;
788
789 start &= (((u64)1) << addr_width) - 1;
790 end &= (((u64)1) << addr_width) - 1;
791
792 /* we don't need lock here, nobody else touches the iova range */
793 level = 2;
794 while (level <= total) {
795 tmp = align_to_level(start, level);
796 if (tmp >= end || (tmp + level_size(level) > end))
797 return;
798
799 while (tmp < end) {
800 pte = dma_addr_level_pte(domain, tmp, level);
801 if (pte) {
802 free_pgtable_page(
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000803 phys_to_virt(dma_pte_addr(pte)));
804 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800805 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700806 }
807 tmp += level_size(level);
808 }
809 level++;
810 }
811 /* free pgd */
812 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
813 free_pgtable_page(domain->pgd);
814 domain->pgd = NULL;
815 }
816}
817
818/* iommu handling */
819static int iommu_alloc_root_entry(struct intel_iommu *iommu)
820{
821 struct root_entry *root;
822 unsigned long flags;
823
824 root = (struct root_entry *)alloc_pgtable_page();
825 if (!root)
826 return -ENOMEM;
827
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700828 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700829
830 spin_lock_irqsave(&iommu->lock, flags);
831 iommu->root_entry = root;
832 spin_unlock_irqrestore(&iommu->lock, flags);
833
834 return 0;
835}
836
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700837static void iommu_set_root_entry(struct intel_iommu *iommu)
838{
839 void *addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100840 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700841 unsigned long flag;
842
843 addr = iommu->root_entry;
844
845 spin_lock_irqsave(&iommu->register_lock, flag);
846 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
847
David Woodhousec416daa2009-05-10 20:30:58 +0100848 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700849
850 /* Make sure hardware complete it */
851 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100852 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700853
854 spin_unlock_irqrestore(&iommu->register_lock, flag);
855}
856
857static void iommu_flush_write_buffer(struct intel_iommu *iommu)
858{
859 u32 val;
860 unsigned long flag;
861
David Woodhouse9af88142009-02-13 23:18:03 +0000862 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700863 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700864
865 spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +0100866 writel(iommu->gcmd | DMA_GCMD_WBF, 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, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700871
872 spin_unlock_irqrestore(&iommu->register_lock, flag);
873}
874
875/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +0100876static void __iommu_flush_context(struct intel_iommu *iommu,
877 u16 did, u16 source_id, u8 function_mask,
878 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700879{
880 u64 val = 0;
881 unsigned long flag;
882
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700883 switch (type) {
884 case DMA_CCMD_GLOBAL_INVL:
885 val = DMA_CCMD_GLOBAL_INVL;
886 break;
887 case DMA_CCMD_DOMAIN_INVL:
888 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
889 break;
890 case DMA_CCMD_DEVICE_INVL:
891 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
892 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
893 break;
894 default:
895 BUG();
896 }
897 val |= DMA_CCMD_ICC;
898
899 spin_lock_irqsave(&iommu->register_lock, flag);
900 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
901
902 /* Make sure hardware complete it */
903 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
904 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
905
906 spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700907}
908
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700909/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100910static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
911 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700912{
913 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
914 u64 val = 0, val_iva = 0;
915 unsigned long flag;
916
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700917 switch (type) {
918 case DMA_TLB_GLOBAL_FLUSH:
919 /* global flush doesn't need set IVA_REG */
920 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
921 break;
922 case DMA_TLB_DSI_FLUSH:
923 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
924 break;
925 case DMA_TLB_PSI_FLUSH:
926 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
927 /* Note: always flush non-leaf currently */
928 val_iva = size_order | addr;
929 break;
930 default:
931 BUG();
932 }
933 /* Note: set drain read/write */
934#if 0
935 /*
936 * This is probably to be super secure.. Looks like we can
937 * ignore it without any impact.
938 */
939 if (cap_read_drain(iommu->cap))
940 val |= DMA_TLB_READ_DRAIN;
941#endif
942 if (cap_write_drain(iommu->cap))
943 val |= DMA_TLB_WRITE_DRAIN;
944
945 spin_lock_irqsave(&iommu->register_lock, flag);
946 /* Note: Only uses first TLB reg currently */
947 if (val_iva)
948 dmar_writeq(iommu->reg + tlb_offset, val_iva);
949 dmar_writeq(iommu->reg + tlb_offset + 8, val);
950
951 /* Make sure hardware complete it */
952 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
953 dmar_readq, (!(val & DMA_TLB_IVT)), val);
954
955 spin_unlock_irqrestore(&iommu->register_lock, flag);
956
957 /* check IOTLB invalidation granularity */
958 if (DMA_TLB_IAIG(val) == 0)
959 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
960 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
961 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700962 (unsigned long long)DMA_TLB_IIRG(type),
963 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700964}
965
Yu Zhao93a23a72009-05-18 13:51:37 +0800966static struct device_domain_info *iommu_support_dev_iotlb(
967 struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700968{
Yu Zhao93a23a72009-05-18 13:51:37 +0800969 int found = 0;
970 unsigned long flags;
971 struct device_domain_info *info;
972 struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);
973
974 if (!ecap_dev_iotlb_support(iommu->ecap))
975 return NULL;
976
977 if (!iommu->qi)
978 return NULL;
979
980 spin_lock_irqsave(&device_domain_lock, flags);
981 list_for_each_entry(info, &domain->devices, link)
982 if (info->bus == bus && info->devfn == devfn) {
983 found = 1;
984 break;
985 }
986 spin_unlock_irqrestore(&device_domain_lock, flags);
987
988 if (!found || !info->dev)
989 return NULL;
990
991 if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
992 return NULL;
993
994 if (!dmar_find_matched_atsr_unit(info->dev))
995 return NULL;
996
997 info->iommu = iommu;
998
999 return info;
1000}
1001
1002static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1003{
1004 if (!info)
1005 return;
1006
1007 pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
1008}
1009
1010static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1011{
1012 if (!info->dev || !pci_ats_enabled(info->dev))
1013 return;
1014
1015 pci_disable_ats(info->dev);
1016}
1017
1018static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1019 u64 addr, unsigned mask)
1020{
1021 u16 sid, qdep;
1022 unsigned long flags;
1023 struct device_domain_info *info;
1024
1025 spin_lock_irqsave(&device_domain_lock, flags);
1026 list_for_each_entry(info, &domain->devices, link) {
1027 if (!info->dev || !pci_ats_enabled(info->dev))
1028 continue;
1029
1030 sid = info->bus << 8 | info->devfn;
1031 qdep = pci_ats_queue_depth(info->dev);
1032 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1033 }
1034 spin_unlock_irqrestore(&device_domain_lock, flags);
1035}
1036
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001037static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
1038 u64 addr, unsigned int pages)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001039{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001040 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001041
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001042 BUG_ON(addr & (~VTD_PAGE_MASK));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001043 BUG_ON(pages == 0);
1044
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001045 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001046 * Fallback to domain selective flush if no PSI support or the size is
1047 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001048 * PSI requires page size to be 2 ^ x, and the base address is naturally
1049 * aligned to the size
1050 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001051 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1052 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001053 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001054 else
1055 iommu->flush.flush_iotlb(iommu, did, addr, mask,
1056 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001057
1058 /*
1059 * In caching mode, domain ID 0 is reserved for non-present to present
1060 * mapping flush. Device IOTLB doesn't need to be flushed in this case.
1061 */
1062 if (!cap_caching_mode(iommu->cap) || did)
Yu Zhao93a23a72009-05-18 13:51:37 +08001063 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001064}
1065
mark grossf8bab732008-02-08 04:18:38 -08001066static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1067{
1068 u32 pmen;
1069 unsigned long flags;
1070
1071 spin_lock_irqsave(&iommu->register_lock, flags);
1072 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1073 pmen &= ~DMA_PMEN_EPM;
1074 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1075
1076 /* wait for the protected region status bit to clear */
1077 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1078 readl, !(pmen & DMA_PMEN_PRS), pmen);
1079
1080 spin_unlock_irqrestore(&iommu->register_lock, flags);
1081}
1082
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001083static int iommu_enable_translation(struct intel_iommu *iommu)
1084{
1085 u32 sts;
1086 unsigned long flags;
1087
1088 spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001089 iommu->gcmd |= DMA_GCMD_TE;
1090 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001091
1092 /* Make sure hardware complete it */
1093 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001094 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001095
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001096 spin_unlock_irqrestore(&iommu->register_lock, flags);
1097 return 0;
1098}
1099
1100static int iommu_disable_translation(struct intel_iommu *iommu)
1101{
1102 u32 sts;
1103 unsigned long flag;
1104
1105 spin_lock_irqsave(&iommu->register_lock, flag);
1106 iommu->gcmd &= ~DMA_GCMD_TE;
1107 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1108
1109 /* Make sure hardware complete it */
1110 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001111 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001112
1113 spin_unlock_irqrestore(&iommu->register_lock, flag);
1114 return 0;
1115}
1116
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001117
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001118static int iommu_init_domains(struct intel_iommu *iommu)
1119{
1120 unsigned long ndomains;
1121 unsigned long nlongs;
1122
1123 ndomains = cap_ndoms(iommu->cap);
1124 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1125 nlongs = BITS_TO_LONGS(ndomains);
1126
1127 /* TBD: there might be 64K domains,
1128 * consider other allocation for future chip
1129 */
1130 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1131 if (!iommu->domain_ids) {
1132 printk(KERN_ERR "Allocating domain id array failed\n");
1133 return -ENOMEM;
1134 }
1135 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1136 GFP_KERNEL);
1137 if (!iommu->domains) {
1138 printk(KERN_ERR "Allocating domain array failed\n");
1139 kfree(iommu->domain_ids);
1140 return -ENOMEM;
1141 }
1142
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001143 spin_lock_init(&iommu->lock);
1144
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001145 /*
1146 * if Caching mode is set, then invalid translations are tagged
1147 * with domainid 0. Hence we need to pre-allocate it.
1148 */
1149 if (cap_caching_mode(iommu->cap))
1150 set_bit(0, iommu->domain_ids);
1151 return 0;
1152}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001153
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001154
1155static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001156static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001157
1158void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001159{
1160 struct dmar_domain *domain;
1161 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001162 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001163
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001164 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1165 for (; i < cap_ndoms(iommu->cap); ) {
1166 domain = iommu->domains[i];
1167 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001168
1169 spin_lock_irqsave(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001170 if (--domain->iommu_count == 0) {
1171 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1172 vm_domain_exit(domain);
1173 else
1174 domain_exit(domain);
1175 }
Weidong Hanc7151a82008-12-08 22:51:37 +08001176 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1177
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001178 i = find_next_bit(iommu->domain_ids,
1179 cap_ndoms(iommu->cap), i+1);
1180 }
1181
1182 if (iommu->gcmd & DMA_GCMD_TE)
1183 iommu_disable_translation(iommu);
1184
1185 if (iommu->irq) {
1186 set_irq_data(iommu->irq, NULL);
1187 /* This will mask the irq */
1188 free_irq(iommu->irq, iommu);
1189 destroy_irq(iommu->irq);
1190 }
1191
1192 kfree(iommu->domains);
1193 kfree(iommu->domain_ids);
1194
Weidong Hand9630fe2008-12-08 11:06:32 +08001195 g_iommus[iommu->seq_id] = NULL;
1196
1197 /* if all iommus are freed, free g_iommus */
1198 for (i = 0; i < g_num_of_iommus; i++) {
1199 if (g_iommus[i])
1200 break;
1201 }
1202
1203 if (i == g_num_of_iommus)
1204 kfree(g_iommus);
1205
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001206 /* free context mapping */
1207 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001208}
1209
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001210static struct dmar_domain *alloc_domain(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001211{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001212 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001213
1214 domain = alloc_domain_mem();
1215 if (!domain)
1216 return NULL;
1217
Weidong Han8c11e792008-12-08 15:29:22 +08001218 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
Weidong Hand71a2f32008-12-07 21:13:41 +08001219 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001220
1221 return domain;
1222}
1223
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001224static int iommu_attach_domain(struct dmar_domain *domain,
1225 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001226{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001227 int num;
1228 unsigned long ndomains;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001229 unsigned long flags;
1230
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001231 ndomains = cap_ndoms(iommu->cap);
Weidong Han8c11e792008-12-08 15:29:22 +08001232
1233 spin_lock_irqsave(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001234
1235 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1236 if (num >= ndomains) {
1237 spin_unlock_irqrestore(&iommu->lock, flags);
1238 printk(KERN_ERR "IOMMU: no free domain ids\n");
1239 return -ENOMEM;
1240 }
1241
1242 domain->id = num;
1243 set_bit(num, iommu->domain_ids);
1244 set_bit(iommu->seq_id, &domain->iommu_bmp);
1245 iommu->domains[num] = domain;
1246 spin_unlock_irqrestore(&iommu->lock, flags);
1247
1248 return 0;
1249}
1250
1251static void iommu_detach_domain(struct dmar_domain *domain,
1252 struct intel_iommu *iommu)
1253{
1254 unsigned long flags;
1255 int num, ndomains;
1256 int found = 0;
1257
1258 spin_lock_irqsave(&iommu->lock, flags);
1259 ndomains = cap_ndoms(iommu->cap);
1260 num = find_first_bit(iommu->domain_ids, ndomains);
1261 for (; num < ndomains; ) {
1262 if (iommu->domains[num] == domain) {
1263 found = 1;
1264 break;
1265 }
1266 num = find_next_bit(iommu->domain_ids,
1267 cap_ndoms(iommu->cap), num+1);
1268 }
1269
1270 if (found) {
1271 clear_bit(num, iommu->domain_ids);
1272 clear_bit(iommu->seq_id, &domain->iommu_bmp);
1273 iommu->domains[num] = NULL;
1274 }
Weidong Han8c11e792008-12-08 15:29:22 +08001275 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001276}
1277
1278static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001279static struct lock_class_key reserved_alloc_key;
1280static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001281
1282static void dmar_init_reserved_ranges(void)
1283{
1284 struct pci_dev *pdev = NULL;
1285 struct iova *iova;
1286 int i;
1287 u64 addr, size;
1288
David Millerf6611972008-02-06 01:36:23 -08001289 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001290
Mark Gross8a443df2008-03-04 14:59:31 -08001291 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1292 &reserved_alloc_key);
1293 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1294 &reserved_rbtree_key);
1295
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001296 /* IOAPIC ranges shouldn't be accessed by DMA */
1297 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1298 IOVA_PFN(IOAPIC_RANGE_END));
1299 if (!iova)
1300 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1301
1302 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1303 for_each_pci_dev(pdev) {
1304 struct resource *r;
1305
1306 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1307 r = &pdev->resource[i];
1308 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1309 continue;
1310 addr = r->start;
David Woodhousefd18de52009-05-10 23:57:41 +01001311 addr &= PHYSICAL_PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001312 size = r->end - addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001313 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001314 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1315 IOVA_PFN(size + addr) - 1);
1316 if (!iova)
1317 printk(KERN_ERR "Reserve iova failed\n");
1318 }
1319 }
1320
1321}
1322
1323static void domain_reserve_special_ranges(struct dmar_domain *domain)
1324{
1325 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1326}
1327
1328static inline int guestwidth_to_adjustwidth(int gaw)
1329{
1330 int agaw;
1331 int r = (gaw - 12) % 9;
1332
1333 if (r == 0)
1334 agaw = gaw;
1335 else
1336 agaw = gaw + 9 - r;
1337 if (agaw > 64)
1338 agaw = 64;
1339 return agaw;
1340}
1341
1342static int domain_init(struct dmar_domain *domain, int guest_width)
1343{
1344 struct intel_iommu *iommu;
1345 int adjust_width, agaw;
1346 unsigned long sagaw;
1347
David Millerf6611972008-02-06 01:36:23 -08001348 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001349 spin_lock_init(&domain->mapping_lock);
Weidong Hanc7151a82008-12-08 22:51:37 +08001350 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001351
1352 domain_reserve_special_ranges(domain);
1353
1354 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001355 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001356 if (guest_width > cap_mgaw(iommu->cap))
1357 guest_width = cap_mgaw(iommu->cap);
1358 domain->gaw = guest_width;
1359 adjust_width = guestwidth_to_adjustwidth(guest_width);
1360 agaw = width_to_agaw(adjust_width);
1361 sagaw = cap_sagaw(iommu->cap);
1362 if (!test_bit(agaw, &sagaw)) {
1363 /* hardware doesn't support it, choose a bigger one */
1364 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1365 agaw = find_next_bit(&sagaw, 5, agaw);
1366 if (agaw >= 5)
1367 return -ENODEV;
1368 }
1369 domain->agaw = agaw;
1370 INIT_LIST_HEAD(&domain->devices);
1371
Weidong Han8e6040972008-12-08 15:49:06 +08001372 if (ecap_coherent(iommu->ecap))
1373 domain->iommu_coherency = 1;
1374 else
1375 domain->iommu_coherency = 0;
1376
Sheng Yang58c610b2009-03-18 15:33:05 +08001377 if (ecap_sc_support(iommu->ecap))
1378 domain->iommu_snooping = 1;
1379 else
1380 domain->iommu_snooping = 0;
1381
Weidong Hanc7151a82008-12-08 22:51:37 +08001382 domain->iommu_count = 1;
1383
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001384 /* always allocate the top pgd */
1385 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1386 if (!domain->pgd)
1387 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001388 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001389 return 0;
1390}
1391
1392static void domain_exit(struct dmar_domain *domain)
1393{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001394 struct dmar_drhd_unit *drhd;
1395 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001396 u64 end;
1397
1398 /* Domain 0 is reserved, so dont process it */
1399 if (!domain)
1400 return;
1401
1402 domain_remove_dev_info(domain);
1403 /* destroy iovas */
1404 put_iova_domain(&domain->iovad);
1405 end = DOMAIN_MAX_ADDR(domain->gaw);
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001406 end = end & (~PAGE_MASK);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001407
1408 /* clear ptes */
1409 dma_pte_clear_range(domain, 0, end);
1410
1411 /* free page tables */
1412 dma_pte_free_pagetable(domain, 0, end);
1413
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001414 for_each_active_iommu(iommu, drhd)
1415 if (test_bit(iommu->seq_id, &domain->iommu_bmp))
1416 iommu_detach_domain(domain, iommu);
1417
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001418 free_domain_mem(domain);
1419}
1420
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001421static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1422 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001423{
1424 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001425 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001426 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001427 struct dma_pte *pgd;
1428 unsigned long num;
1429 unsigned long ndomains;
1430 int id;
1431 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001432 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001433
1434 pr_debug("Set context mapping for %02x:%02x.%d\n",
1435 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001436
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001437 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001438 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1439 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001440
David Woodhouse276dbf992009-04-04 01:45:37 +01001441 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001442 if (!iommu)
1443 return -ENODEV;
1444
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001445 context = device_to_context_entry(iommu, bus, devfn);
1446 if (!context)
1447 return -ENOMEM;
1448 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001449 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001450 spin_unlock_irqrestore(&iommu->lock, flags);
1451 return 0;
1452 }
1453
Weidong Hanea6606b2008-12-08 23:08:15 +08001454 id = domain->id;
1455 pgd = domain->pgd;
1456
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001457 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
1458 domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001459 int found = 0;
1460
1461 /* find an available domain id for this device in iommu */
1462 ndomains = cap_ndoms(iommu->cap);
1463 num = find_first_bit(iommu->domain_ids, ndomains);
1464 for (; num < ndomains; ) {
1465 if (iommu->domains[num] == domain) {
1466 id = num;
1467 found = 1;
1468 break;
1469 }
1470 num = find_next_bit(iommu->domain_ids,
1471 cap_ndoms(iommu->cap), num+1);
1472 }
1473
1474 if (found == 0) {
1475 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1476 if (num >= ndomains) {
1477 spin_unlock_irqrestore(&iommu->lock, flags);
1478 printk(KERN_ERR "IOMMU: no free domain ids\n");
1479 return -EFAULT;
1480 }
1481
1482 set_bit(num, iommu->domain_ids);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001483 set_bit(iommu->seq_id, &domain->iommu_bmp);
Weidong Hanea6606b2008-12-08 23:08:15 +08001484 iommu->domains[num] = domain;
1485 id = num;
1486 }
1487
1488 /* Skip top levels of page tables for
1489 * iommu which has less agaw than default.
1490 */
1491 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1492 pgd = phys_to_virt(dma_pte_addr(pgd));
1493 if (!dma_pte_present(pgd)) {
1494 spin_unlock_irqrestore(&iommu->lock, flags);
1495 return -ENOMEM;
1496 }
1497 }
1498 }
1499
1500 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001501
Yu Zhao93a23a72009-05-18 13:51:37 +08001502 if (translation != CONTEXT_TT_PASS_THROUGH) {
1503 info = iommu_support_dev_iotlb(domain, segment, bus, devfn);
1504 translation = info ? CONTEXT_TT_DEV_IOTLB :
1505 CONTEXT_TT_MULTI_LEVEL;
1506 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001507 /*
1508 * In pass through mode, AW must be programmed to indicate the largest
1509 * AGAW value supported by hardware. And ASR is ignored by hardware.
1510 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001511 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001512 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001513 else {
1514 context_set_address_root(context, virt_to_phys(pgd));
1515 context_set_address_width(context, iommu->agaw);
1516 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001517
1518 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001519 context_set_fault_enable(context);
1520 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001521 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001522
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001523 /*
1524 * It's a non-present to present mapping. If hardware doesn't cache
1525 * non-present entry we only need to flush the write-buffer. If the
1526 * _does_ cache non-present entries, then it does so in the special
1527 * domain #0, which we have to flush:
1528 */
1529 if (cap_caching_mode(iommu->cap)) {
1530 iommu->flush.flush_context(iommu, 0,
1531 (((u16)bus) << 8) | devfn,
1532 DMA_CCMD_MASK_NOBIT,
1533 DMA_CCMD_DEVICE_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001534 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001535 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001536 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001537 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001538 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001539 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001540
1541 spin_lock_irqsave(&domain->iommu_lock, flags);
1542 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1543 domain->iommu_count++;
Sheng Yang58c610b2009-03-18 15:33:05 +08001544 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001545 }
1546 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001547 return 0;
1548}
1549
1550static int
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001551domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1552 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001553{
1554 int ret;
1555 struct pci_dev *tmp, *parent;
1556
David Woodhouse276dbf992009-04-04 01:45:37 +01001557 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001558 pdev->bus->number, pdev->devfn,
1559 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001560 if (ret)
1561 return ret;
1562
1563 /* dependent device mapping */
1564 tmp = pci_find_upstream_pcie_bridge(pdev);
1565 if (!tmp)
1566 return 0;
1567 /* Secondary interface's bus number and devfn 0 */
1568 parent = pdev->bus->self;
1569 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001570 ret = domain_context_mapping_one(domain,
1571 pci_domain_nr(parent->bus),
1572 parent->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001573 parent->devfn, translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001574 if (ret)
1575 return ret;
1576 parent = parent->bus->self;
1577 }
1578 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1579 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001580 pci_domain_nr(tmp->subordinate),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001581 tmp->subordinate->number, 0,
1582 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001583 else /* this is a legacy PCI bridge */
1584 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001585 pci_domain_nr(tmp->bus),
1586 tmp->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001587 tmp->devfn,
1588 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001589}
1590
Weidong Han5331fe62008-12-08 23:00:00 +08001591static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001592{
1593 int ret;
1594 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001595 struct intel_iommu *iommu;
1596
David Woodhouse276dbf992009-04-04 01:45:37 +01001597 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1598 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001599 if (!iommu)
1600 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001601
David Woodhouse276dbf992009-04-04 01:45:37 +01001602 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001603 if (!ret)
1604 return ret;
1605 /* dependent device mapping */
1606 tmp = pci_find_upstream_pcie_bridge(pdev);
1607 if (!tmp)
1608 return ret;
1609 /* Secondary interface's bus number and devfn 0 */
1610 parent = pdev->bus->self;
1611 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001612 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001613 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001614 if (!ret)
1615 return ret;
1616 parent = parent->bus->self;
1617 }
1618 if (tmp->is_pcie)
David Woodhouse276dbf992009-04-04 01:45:37 +01001619 return device_context_mapped(iommu, tmp->subordinate->number,
1620 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001621 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001622 return device_context_mapped(iommu, tmp->bus->number,
1623 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001624}
1625
1626static int
1627domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1628 u64 hpa, size_t size, int prot)
1629{
1630 u64 start_pfn, end_pfn;
1631 struct dma_pte *pte;
1632 int index;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001633 int addr_width = agaw_to_width(domain->agaw);
1634
1635 hpa &= (((u64)1) << addr_width) - 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001636
1637 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1638 return -EINVAL;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001639 iova &= PAGE_MASK;
1640 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1641 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001642 index = 0;
1643 while (start_pfn < end_pfn) {
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001644 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001645 if (!pte)
1646 return -ENOMEM;
1647 /* We don't need lock here, nobody else
1648 * touches the iova range
1649 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001650 BUG_ON(dma_pte_addr(pte));
1651 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
1652 dma_set_pte_prot(pte, prot);
Sheng Yang9cf066972009-03-18 15:33:07 +08001653 if (prot & DMA_PTE_SNP)
1654 dma_set_pte_snp(pte);
Weidong Han5331fe62008-12-08 23:00:00 +08001655 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001656 start_pfn++;
1657 index++;
1658 }
1659 return 0;
1660}
1661
Weidong Hanc7151a82008-12-08 22:51:37 +08001662static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001663{
Weidong Hanc7151a82008-12-08 22:51:37 +08001664 if (!iommu)
1665 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001666
1667 clear_context_table(iommu, bus, devfn);
1668 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001669 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001670 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001671}
1672
1673static void domain_remove_dev_info(struct dmar_domain *domain)
1674{
1675 struct device_domain_info *info;
1676 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001677 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001678
1679 spin_lock_irqsave(&device_domain_lock, flags);
1680 while (!list_empty(&domain->devices)) {
1681 info = list_entry(domain->devices.next,
1682 struct device_domain_info, link);
1683 list_del(&info->link);
1684 list_del(&info->global);
1685 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001686 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001687 spin_unlock_irqrestore(&device_domain_lock, flags);
1688
Yu Zhao93a23a72009-05-18 13:51:37 +08001689 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01001690 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001691 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001692 free_devinfo_mem(info);
1693
1694 spin_lock_irqsave(&device_domain_lock, flags);
1695 }
1696 spin_unlock_irqrestore(&device_domain_lock, flags);
1697}
1698
1699/*
1700 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001701 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001702 */
Kay, Allen M38717942008-09-09 18:37:29 +03001703static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001704find_domain(struct pci_dev *pdev)
1705{
1706 struct device_domain_info *info;
1707
1708 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001709 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001710 if (info)
1711 return info->domain;
1712 return NULL;
1713}
1714
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001715/* domain is initialized */
1716static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1717{
1718 struct dmar_domain *domain, *found = NULL;
1719 struct intel_iommu *iommu;
1720 struct dmar_drhd_unit *drhd;
1721 struct device_domain_info *info, *tmp;
1722 struct pci_dev *dev_tmp;
1723 unsigned long flags;
1724 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001725 int segment;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001726 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001727
1728 domain = find_domain(pdev);
1729 if (domain)
1730 return domain;
1731
David Woodhouse276dbf992009-04-04 01:45:37 +01001732 segment = pci_domain_nr(pdev->bus);
1733
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001734 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1735 if (dev_tmp) {
1736 if (dev_tmp->is_pcie) {
1737 bus = dev_tmp->subordinate->number;
1738 devfn = 0;
1739 } else {
1740 bus = dev_tmp->bus->number;
1741 devfn = dev_tmp->devfn;
1742 }
1743 spin_lock_irqsave(&device_domain_lock, flags);
1744 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001745 if (info->segment == segment &&
1746 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001747 found = info->domain;
1748 break;
1749 }
1750 }
1751 spin_unlock_irqrestore(&device_domain_lock, flags);
1752 /* pcie-pci bridge already has a domain, uses it */
1753 if (found) {
1754 domain = found;
1755 goto found_domain;
1756 }
1757 }
1758
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001759 domain = alloc_domain();
1760 if (!domain)
1761 goto error;
1762
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001763 /* Allocate new domain for the device */
1764 drhd = dmar_find_matched_drhd_unit(pdev);
1765 if (!drhd) {
1766 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1767 pci_name(pdev));
1768 return NULL;
1769 }
1770 iommu = drhd->iommu;
1771
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001772 ret = iommu_attach_domain(domain, iommu);
1773 if (ret) {
1774 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001775 goto error;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001776 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001777
1778 if (domain_init(domain, gaw)) {
1779 domain_exit(domain);
1780 goto error;
1781 }
1782
1783 /* register pcie-to-pci device */
1784 if (dev_tmp) {
1785 info = alloc_devinfo_mem();
1786 if (!info) {
1787 domain_exit(domain);
1788 goto error;
1789 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001790 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001791 info->bus = bus;
1792 info->devfn = devfn;
1793 info->dev = NULL;
1794 info->domain = domain;
1795 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001796 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001797
1798 /* pcie-to-pci bridge already has a domain, uses it */
1799 found = NULL;
1800 spin_lock_irqsave(&device_domain_lock, flags);
1801 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001802 if (tmp->segment == segment &&
1803 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001804 found = tmp->domain;
1805 break;
1806 }
1807 }
1808 if (found) {
1809 free_devinfo_mem(info);
1810 domain_exit(domain);
1811 domain = found;
1812 } else {
1813 list_add(&info->link, &domain->devices);
1814 list_add(&info->global, &device_domain_list);
1815 }
1816 spin_unlock_irqrestore(&device_domain_lock, flags);
1817 }
1818
1819found_domain:
1820 info = alloc_devinfo_mem();
1821 if (!info)
1822 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01001823 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001824 info->bus = pdev->bus->number;
1825 info->devfn = pdev->devfn;
1826 info->dev = pdev;
1827 info->domain = domain;
1828 spin_lock_irqsave(&device_domain_lock, flags);
1829 /* somebody is fast */
1830 found = find_domain(pdev);
1831 if (found != NULL) {
1832 spin_unlock_irqrestore(&device_domain_lock, flags);
1833 if (found != domain) {
1834 domain_exit(domain);
1835 domain = found;
1836 }
1837 free_devinfo_mem(info);
1838 return domain;
1839 }
1840 list_add(&info->link, &domain->devices);
1841 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001842 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001843 spin_unlock_irqrestore(&device_domain_lock, flags);
1844 return domain;
1845error:
1846 /* recheck it here, maybe others set it */
1847 return find_domain(pdev);
1848}
1849
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001850static int iommu_identity_mapping;
1851
David Woodhouseb2132032009-06-26 18:50:28 +01001852static int iommu_domain_identity_map(struct dmar_domain *domain,
1853 unsigned long long start,
1854 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001855{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001856 unsigned long size;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001857 unsigned long long base;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001858
1859 /* The address might not be aligned */
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001860 base = start & PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001861 size = end - base;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001862 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001863 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1864 IOVA_PFN(base + size) - 1)) {
1865 printk(KERN_ERR "IOMMU: reserve iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01001866 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001867 }
1868
David Woodhouseb2132032009-06-26 18:50:28 +01001869 pr_debug("Mapping reserved region %lx@%llx for domain %d\n",
1870 size, base, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001871 /*
1872 * RMRR range might have overlap with physical memory range,
1873 * clear it first
1874 */
1875 dma_pte_clear_range(domain, base, base + size);
1876
David Woodhouseb2132032009-06-26 18:50:28 +01001877 return domain_page_mapping(domain, base, base, size,
1878 DMA_PTE_READ|DMA_PTE_WRITE);
1879}
1880
1881static int iommu_prepare_identity_map(struct pci_dev *pdev,
1882 unsigned long long start,
1883 unsigned long long end)
1884{
1885 struct dmar_domain *domain;
1886 int ret;
1887
1888 printk(KERN_INFO
1889 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1890 pci_name(pdev), start, end);
1891
David Woodhousec7ab48d2009-06-26 19:10:36 +01001892 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01001893 if (!domain)
1894 return -ENOMEM;
1895
1896 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001897 if (ret)
1898 goto error;
1899
1900 /* context entry init */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001901 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01001902 if (ret)
1903 goto error;
1904
1905 return 0;
1906
1907 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001908 domain_exit(domain);
1909 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001910}
1911
1912static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1913 struct pci_dev *pdev)
1914{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001915 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001916 return 0;
1917 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1918 rmrr->end_address + 1);
1919}
1920
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001921#ifdef CONFIG_DMAR_FLOPPY_WA
1922static inline void iommu_prepare_isa(void)
1923{
1924 struct pci_dev *pdev;
1925 int ret;
1926
1927 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1928 if (!pdev)
1929 return;
1930
David Woodhousec7ab48d2009-06-26 19:10:36 +01001931 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001932 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1933
1934 if (ret)
David Woodhousec7ab48d2009-06-26 19:10:36 +01001935 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
1936 "floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001937
1938}
1939#else
1940static inline void iommu_prepare_isa(void)
1941{
1942 return;
1943}
1944#endif /* !CONFIG_DMAR_FLPY_WA */
1945
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001946/* Initialize each context entry as pass through.*/
1947static int __init init_context_pass_through(void)
1948{
1949 struct pci_dev *pdev = NULL;
1950 struct dmar_domain *domain;
1951 int ret;
1952
1953 for_each_pci_dev(pdev) {
1954 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1955 ret = domain_context_mapping(domain, pdev,
1956 CONTEXT_TT_PASS_THROUGH);
1957 if (ret)
1958 return ret;
1959 }
1960 return 0;
1961}
1962
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001963static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01001964
1965static int __init si_domain_work_fn(unsigned long start_pfn,
1966 unsigned long end_pfn, void *datax)
1967{
1968 int *ret = datax;
1969
1970 *ret = iommu_domain_identity_map(si_domain,
1971 (uint64_t)start_pfn << PAGE_SHIFT,
1972 (uint64_t)end_pfn << PAGE_SHIFT);
1973 return *ret;
1974
1975}
1976
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001977static int si_domain_init(void)
1978{
1979 struct dmar_drhd_unit *drhd;
1980 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01001981 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001982
1983 si_domain = alloc_domain();
1984 if (!si_domain)
1985 return -EFAULT;
1986
David Woodhousec7ab48d2009-06-26 19:10:36 +01001987 pr_debug("Identity mapping domain is domain %d\n", si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001988
1989 for_each_active_iommu(iommu, drhd) {
1990 ret = iommu_attach_domain(si_domain, iommu);
1991 if (ret) {
1992 domain_exit(si_domain);
1993 return -EFAULT;
1994 }
1995 }
1996
1997 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
1998 domain_exit(si_domain);
1999 return -EFAULT;
2000 }
2001
2002 si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY;
2003
David Woodhousec7ab48d2009-06-26 19:10:36 +01002004 for_each_online_node(nid) {
2005 work_with_active_regions(nid, si_domain_work_fn, &ret);
2006 if (ret)
2007 return ret;
2008 }
2009
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002010 return 0;
2011}
2012
2013static void domain_remove_one_dev_info(struct dmar_domain *domain,
2014 struct pci_dev *pdev);
2015static int identity_mapping(struct pci_dev *pdev)
2016{
2017 struct device_domain_info *info;
2018
2019 if (likely(!iommu_identity_mapping))
2020 return 0;
2021
2022
2023 list_for_each_entry(info, &si_domain->devices, link)
2024 if (info->dev == pdev)
2025 return 1;
2026 return 0;
2027}
2028
2029static int domain_add_dev_info(struct dmar_domain *domain,
2030 struct pci_dev *pdev)
2031{
2032 struct device_domain_info *info;
2033 unsigned long flags;
2034
2035 info = alloc_devinfo_mem();
2036 if (!info)
2037 return -ENOMEM;
2038
2039 info->segment = pci_domain_nr(pdev->bus);
2040 info->bus = pdev->bus->number;
2041 info->devfn = pdev->devfn;
2042 info->dev = pdev;
2043 info->domain = domain;
2044
2045 spin_lock_irqsave(&device_domain_lock, flags);
2046 list_add(&info->link, &domain->devices);
2047 list_add(&info->global, &device_domain_list);
2048 pdev->dev.archdata.iommu = info;
2049 spin_unlock_irqrestore(&device_domain_lock, flags);
2050
2051 return 0;
2052}
2053
2054static int iommu_prepare_static_identity_mapping(void)
2055{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002056 struct pci_dev *pdev = NULL;
2057 int ret;
2058
2059 ret = si_domain_init();
2060 if (ret)
2061 return -EFAULT;
2062
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002063 for_each_pci_dev(pdev) {
David Woodhousec7ab48d2009-06-26 19:10:36 +01002064 printk(KERN_INFO "IOMMU: identity mapping for device %s\n",
2065 pci_name(pdev));
2066
2067 ret = domain_context_mapping(si_domain, pdev,
2068 CONTEXT_TT_MULTI_LEVEL);
2069 if (ret)
2070 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002071 ret = domain_add_dev_info(si_domain, pdev);
2072 if (ret)
2073 return ret;
2074 }
2075
2076 return 0;
2077}
2078
2079int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002080{
2081 struct dmar_drhd_unit *drhd;
2082 struct dmar_rmrr_unit *rmrr;
2083 struct pci_dev *pdev;
2084 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002085 int i, ret;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002086 int pass_through = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002087
2088 /*
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002089 * In case pass through can not be enabled, iommu tries to use identity
2090 * mapping.
2091 */
2092 if (iommu_pass_through)
2093 iommu_identity_mapping = 1;
2094
2095 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002096 * for each drhd
2097 * allocate root
2098 * initialize and program root entry to not present
2099 * endfor
2100 */
2101 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002102 g_num_of_iommus++;
2103 /*
2104 * lock not needed as this is only incremented in the single
2105 * threaded kernel __init code path all other access are read
2106 * only
2107 */
2108 }
2109
Weidong Hand9630fe2008-12-08 11:06:32 +08002110 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2111 GFP_KERNEL);
2112 if (!g_iommus) {
2113 printk(KERN_ERR "Allocating global iommu array failed\n");
2114 ret = -ENOMEM;
2115 goto error;
2116 }
2117
mark gross80b20dd2008-04-18 13:53:58 -07002118 deferred_flush = kzalloc(g_num_of_iommus *
2119 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2120 if (!deferred_flush) {
Weidong Hand9630fe2008-12-08 11:06:32 +08002121 kfree(g_iommus);
mark gross5e0d2a62008-03-04 15:22:08 -08002122 ret = -ENOMEM;
2123 goto error;
2124 }
2125
mark gross5e0d2a62008-03-04 15:22:08 -08002126 for_each_drhd_unit(drhd) {
2127 if (drhd->ignored)
2128 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002129
2130 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08002131 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002132
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002133 ret = iommu_init_domains(iommu);
2134 if (ret)
2135 goto error;
2136
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002137 /*
2138 * TBD:
2139 * we could share the same root & context tables
2140 * amoung all IOMMU's. Need to Split it later.
2141 */
2142 ret = iommu_alloc_root_entry(iommu);
2143 if (ret) {
2144 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
2145 goto error;
2146 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002147 if (!ecap_pass_through(iommu->ecap))
2148 pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002149 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002150 if (iommu_pass_through)
2151 if (!pass_through) {
2152 printk(KERN_INFO
2153 "Pass Through is not supported by hardware.\n");
2154 iommu_pass_through = 0;
2155 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002156
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002157 /*
2158 * Start from the sane iommu hardware state.
2159 */
Youquan Songa77b67d2008-10-16 16:31:56 -07002160 for_each_drhd_unit(drhd) {
2161 if (drhd->ignored)
2162 continue;
2163
2164 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002165
2166 /*
2167 * If the queued invalidation is already initialized by us
2168 * (for example, while enabling interrupt-remapping) then
2169 * we got the things already rolling from a sane state.
2170 */
2171 if (iommu->qi)
2172 continue;
2173
2174 /*
2175 * Clear any previous faults.
2176 */
2177 dmar_fault(-1, iommu);
2178 /*
2179 * Disable queued invalidation if supported and already enabled
2180 * before OS handover.
2181 */
2182 dmar_disable_qi(iommu);
2183 }
2184
2185 for_each_drhd_unit(drhd) {
2186 if (drhd->ignored)
2187 continue;
2188
2189 iommu = drhd->iommu;
2190
Youquan Songa77b67d2008-10-16 16:31:56 -07002191 if (dmar_enable_qi(iommu)) {
2192 /*
2193 * Queued Invalidate not enabled, use Register Based
2194 * Invalidate
2195 */
2196 iommu->flush.flush_context = __iommu_flush_context;
2197 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
2198 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002199 "invalidation\n",
2200 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002201 } else {
2202 iommu->flush.flush_context = qi_flush_context;
2203 iommu->flush.flush_iotlb = qi_flush_iotlb;
2204 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002205 "invalidation\n",
2206 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002207 }
2208 }
2209
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002210 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002211 * If pass through is set and enabled, context entries of all pci
2212 * devices are intialized by pass through translation type.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002213 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002214 if (iommu_pass_through) {
2215 ret = init_context_pass_through();
2216 if (ret) {
2217 printk(KERN_ERR "IOMMU: Pass through init failed.\n");
2218 iommu_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002219 }
2220 }
2221
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002222 /*
2223 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002224 * identity mappings for rmrr, gfx, and isa and may fall back to static
2225 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002226 */
2227 if (!iommu_pass_through) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002228 if (iommu_identity_mapping)
2229 iommu_prepare_static_identity_mapping();
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002230 /*
2231 * For each rmrr
2232 * for each dev attached to rmrr
2233 * do
2234 * locate drhd for dev, alloc domain for dev
2235 * allocate free domain
2236 * allocate page table entries for rmrr
2237 * if context not allocated for bus
2238 * allocate and init context
2239 * set present in root table for this bus
2240 * init context with domain, translation etc
2241 * endfor
2242 * endfor
2243 */
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002244 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002245 for_each_rmrr_units(rmrr) {
2246 for (i = 0; i < rmrr->devices_cnt; i++) {
2247 pdev = rmrr->devices[i];
2248 /*
2249 * some BIOS lists non-exist devices in DMAR
2250 * table.
2251 */
2252 if (!pdev)
2253 continue;
2254 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2255 if (ret)
2256 printk(KERN_ERR
2257 "IOMMU: mapping reserved region failed\n");
2258 }
2259 }
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002260
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002261 iommu_prepare_isa();
2262 }
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002263
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002264 /*
2265 * for each drhd
2266 * enable fault log
2267 * global invalidate context cache
2268 * global invalidate iotlb
2269 * enable translation
2270 */
2271 for_each_drhd_unit(drhd) {
2272 if (drhd->ignored)
2273 continue;
2274 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002275
2276 iommu_flush_write_buffer(iommu);
2277
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002278 ret = dmar_set_interrupt(iommu);
2279 if (ret)
2280 goto error;
2281
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002282 iommu_set_root_entry(iommu);
2283
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002284 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002285 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002286 iommu_disable_protect_mem_regions(iommu);
2287
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002288 ret = iommu_enable_translation(iommu);
2289 if (ret)
2290 goto error;
2291 }
2292
2293 return 0;
2294error:
2295 for_each_drhd_unit(drhd) {
2296 if (drhd->ignored)
2297 continue;
2298 iommu = drhd->iommu;
2299 free_iommu(iommu);
2300 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002301 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002302 return ret;
2303}
2304
2305static inline u64 aligned_size(u64 host_addr, size_t size)
2306{
2307 u64 addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002308 addr = (host_addr & (~PAGE_MASK)) + size;
2309 return PAGE_ALIGN(addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002310}
2311
2312struct iova *
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002313iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002314{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002315 struct iova *piova;
2316
2317 /* Make sure it's in range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002318 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002319 if (!size || (IOVA_START_ADDR + size > end))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002320 return NULL;
2321
2322 piova = alloc_iova(&domain->iovad,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002323 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002324 return piova;
2325}
2326
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002327static struct iova *
2328__intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002329 size_t size, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002330{
2331 struct pci_dev *pdev = to_pci_dev(dev);
2332 struct iova *iova = NULL;
2333
Yang Hongyang284901a2009-04-06 19:01:15 -07002334 if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002335 iova = iommu_alloc_iova(domain, size, dma_mask);
2336 else {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002337 /*
2338 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002339 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002340 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002341 */
Yang Hongyang284901a2009-04-06 19:01:15 -07002342 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002343 if (!iova)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002344 iova = iommu_alloc_iova(domain, size, dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002345 }
2346
2347 if (!iova) {
2348 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2349 return NULL;
2350 }
2351
2352 return iova;
2353}
2354
2355static struct dmar_domain *
2356get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002357{
2358 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002359 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002360
2361 domain = get_domain_for_dev(pdev,
2362 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2363 if (!domain) {
2364 printk(KERN_ERR
2365 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002366 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002367 }
2368
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002369 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002370 if (unlikely(!domain_context_mapped(pdev))) {
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002371 ret = domain_context_mapping(domain, pdev,
2372 CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002373 if (ret) {
2374 printk(KERN_ERR
2375 "Domain context map for %s failed",
2376 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002377 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002378 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002379 }
2380
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002381 return domain;
2382}
2383
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002384static int iommu_dummy(struct pci_dev *pdev)
2385{
2386 return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
2387}
2388
2389/* Check if the pdev needs to go through non-identity map and unmap process.*/
2390static int iommu_no_mapping(struct pci_dev *pdev)
2391{
2392 int found;
2393
2394 if (!iommu_identity_mapping)
2395 return iommu_dummy(pdev);
2396
2397 found = identity_mapping(pdev);
2398 if (found) {
2399 if (pdev->dma_mask > DMA_BIT_MASK(32))
2400 return 1;
2401 else {
2402 /*
2403 * 32 bit DMA is removed from si_domain and fall back
2404 * to non-identity mapping.
2405 */
2406 domain_remove_one_dev_info(si_domain, pdev);
2407 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
2408 pci_name(pdev));
2409 return 0;
2410 }
2411 } else {
2412 /*
2413 * In case of a detached 64 bit DMA device from vm, the device
2414 * is put into si_domain for identity mapping.
2415 */
2416 if (pdev->dma_mask > DMA_BIT_MASK(32)) {
2417 int ret;
2418 ret = domain_add_dev_info(si_domain, pdev);
2419 if (!ret) {
2420 printk(KERN_INFO "64bit %s uses identity mapping\n",
2421 pci_name(pdev));
2422 return 1;
2423 }
2424 }
2425 }
2426
2427 return iommu_dummy(pdev);
2428}
2429
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002430static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2431 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002432{
2433 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002434 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002435 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002436 struct iova *iova;
2437 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002438 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002439 struct intel_iommu *iommu;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002440
2441 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002442
2443 if (iommu_no_mapping(pdev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002444 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002445
2446 domain = get_valid_domain_for_dev(pdev);
2447 if (!domain)
2448 return 0;
2449
Weidong Han8c11e792008-12-08 15:29:22 +08002450 iommu = domain_get_iommu(domain);
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002451 size = aligned_size((u64)paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002452
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002453 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002454 if (!iova)
2455 goto error;
2456
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002457 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002458
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002459 /*
2460 * Check if DMAR supports zero-length reads on write only
2461 * mappings..
2462 */
2463 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002464 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002465 prot |= DMA_PTE_READ;
2466 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2467 prot |= DMA_PTE_WRITE;
2468 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002469 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002470 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002471 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002472 * is not a big problem
2473 */
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002474 ret = domain_page_mapping(domain, start_paddr,
David Woodhousefd18de52009-05-10 23:57:41 +01002475 ((u64)paddr) & PHYSICAL_PAGE_MASK,
2476 size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002477 if (ret)
2478 goto error;
2479
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002480 /* it's a non-present to present mapping. Only flush if caching mode */
2481 if (cap_caching_mode(iommu->cap))
2482 iommu_flush_iotlb_psi(iommu, 0, start_paddr,
2483 size >> VTD_PAGE_SHIFT);
2484 else
Weidong Han8c11e792008-12-08 15:29:22 +08002485 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002486
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002487 return start_paddr + ((u64)paddr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002488
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002489error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002490 if (iova)
2491 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002492 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002493 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002494 return 0;
2495}
2496
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002497static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2498 unsigned long offset, size_t size,
2499 enum dma_data_direction dir,
2500 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002501{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002502 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2503 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002504}
2505
mark gross5e0d2a62008-03-04 15:22:08 -08002506static void flush_unmaps(void)
2507{
mark gross80b20dd2008-04-18 13:53:58 -07002508 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002509
mark gross5e0d2a62008-03-04 15:22:08 -08002510 timer_on = 0;
2511
2512 /* just flush them all */
2513 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002514 struct intel_iommu *iommu = g_iommus[i];
2515 if (!iommu)
2516 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002517
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002518 if (!deferred_flush[i].next)
2519 continue;
2520
2521 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08002522 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002523 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08002524 unsigned long mask;
2525 struct iova *iova = deferred_flush[i].iova[j];
2526
2527 mask = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT;
2528 mask = ilog2(mask >> VTD_PAGE_SHIFT);
2529 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
2530 iova->pfn_lo << PAGE_SHIFT, mask);
2531 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
mark gross80b20dd2008-04-18 13:53:58 -07002532 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002533 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002534 }
2535
mark gross5e0d2a62008-03-04 15:22:08 -08002536 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002537}
2538
2539static void flush_unmaps_timeout(unsigned long data)
2540{
mark gross80b20dd2008-04-18 13:53:58 -07002541 unsigned long flags;
2542
2543 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002544 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002545 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002546}
2547
2548static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2549{
2550 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002551 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002552 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002553
2554 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002555 if (list_size == HIGH_WATER_MARK)
2556 flush_unmaps();
2557
Weidong Han8c11e792008-12-08 15:29:22 +08002558 iommu = domain_get_iommu(dom);
2559 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002560
mark gross80b20dd2008-04-18 13:53:58 -07002561 next = deferred_flush[iommu_id].next;
2562 deferred_flush[iommu_id].domain[next] = dom;
2563 deferred_flush[iommu_id].iova[next] = iova;
2564 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002565
2566 if (!timer_on) {
2567 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2568 timer_on = 1;
2569 }
2570 list_size++;
2571 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2572}
2573
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002574static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2575 size_t size, enum dma_data_direction dir,
2576 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002577{
2578 struct pci_dev *pdev = to_pci_dev(dev);
2579 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002580 unsigned long start_addr;
2581 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002582 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002583
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002584 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002585 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002586
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002587 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002588 BUG_ON(!domain);
2589
Weidong Han8c11e792008-12-08 15:29:22 +08002590 iommu = domain_get_iommu(domain);
2591
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002592 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2593 if (!iova)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002594 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002595
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002596 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002597 size = aligned_size((u64)dev_addr, size);
2598
David Woodhouse4cf2e752009-02-11 17:23:43 +00002599 pr_debug("Device %s unmapping: %zx@%llx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002600 pci_name(pdev), size, (unsigned long long)start_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002601
2602 /* clear the whole page */
2603 dma_pte_clear_range(domain, start_addr, start_addr + size);
2604 /* free page tables */
2605 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
mark gross5e0d2a62008-03-04 15:22:08 -08002606 if (intel_iommu_strict) {
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002607 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2608 size >> VTD_PAGE_SHIFT);
mark gross5e0d2a62008-03-04 15:22:08 -08002609 /* free iova */
2610 __free_iova(&domain->iovad, iova);
2611 } else {
2612 add_unmap(domain, iova);
2613 /*
2614 * queue up the release of the unmap to save the 1/6th of the
2615 * cpu used up by the iotlb flush operation...
2616 */
mark gross5e0d2a62008-03-04 15:22:08 -08002617 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002618}
2619
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002620static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2621 int dir)
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002622{
2623 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2624}
2625
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002626static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2627 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002628{
2629 void *vaddr;
2630 int order;
2631
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002632 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002633 order = get_order(size);
2634 flags &= ~(GFP_DMA | GFP_DMA32);
2635
2636 vaddr = (void *)__get_free_pages(flags, order);
2637 if (!vaddr)
2638 return NULL;
2639 memset(vaddr, 0, size);
2640
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002641 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2642 DMA_BIDIRECTIONAL,
2643 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002644 if (*dma_handle)
2645 return vaddr;
2646 free_pages((unsigned long)vaddr, order);
2647 return NULL;
2648}
2649
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002650static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2651 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002652{
2653 int order;
2654
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002655 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002656 order = get_order(size);
2657
2658 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2659 free_pages((unsigned long)vaddr, order);
2660}
2661
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002662static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2663 int nelems, enum dma_data_direction dir,
2664 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002665{
2666 int i;
2667 struct pci_dev *pdev = to_pci_dev(hwdev);
2668 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002669 unsigned long start_addr;
2670 struct iova *iova;
2671 size_t size = 0;
David Woodhouse4cf2e752009-02-11 17:23:43 +00002672 phys_addr_t addr;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002673 struct scatterlist *sg;
Weidong Han8c11e792008-12-08 15:29:22 +08002674 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002675
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002676 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002677 return;
2678
2679 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002680 BUG_ON(!domain);
2681
2682 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002683
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002684 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002685 if (!iova)
2686 return;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002687 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002688 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002689 size += aligned_size((u64)addr, sg->length);
2690 }
2691
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002692 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002693
2694 /* clear the whole page */
2695 dma_pte_clear_range(domain, start_addr, start_addr + size);
2696 /* free page tables */
2697 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2698
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002699 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2700 size >> VTD_PAGE_SHIFT);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002701
2702 /* free iova */
2703 __free_iova(&domain->iovad, iova);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002704}
2705
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002706static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002707 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002708{
2709 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002710 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002711
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002712 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002713 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002714 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002715 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002716 }
2717 return nelems;
2718}
2719
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002720static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2721 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002722{
David Woodhouse4cf2e752009-02-11 17:23:43 +00002723 phys_addr_t addr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002724 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002725 struct pci_dev *pdev = to_pci_dev(hwdev);
2726 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002727 size_t size = 0;
2728 int prot = 0;
2729 size_t offset = 0;
2730 struct iova *iova = NULL;
2731 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002732 struct scatterlist *sg;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002733 unsigned long start_addr;
Weidong Han8c11e792008-12-08 15:29:22 +08002734 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002735
2736 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002737 if (iommu_no_mapping(pdev))
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002738 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002739
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002740 domain = get_valid_domain_for_dev(pdev);
2741 if (!domain)
2742 return 0;
2743
Weidong Han8c11e792008-12-08 15:29:22 +08002744 iommu = domain_get_iommu(domain);
2745
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002746 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002747 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002748 size += aligned_size((u64)addr, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002749 }
2750
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002751 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002752 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002753 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002754 return 0;
2755 }
2756
2757 /*
2758 * Check if DMAR supports zero-length reads on write only
2759 * mappings..
2760 */
2761 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002762 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002763 prot |= DMA_PTE_READ;
2764 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2765 prot |= DMA_PTE_WRITE;
2766
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002767 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002768 offset = 0;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002769 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002770 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002771 size = aligned_size((u64)addr, sg->length);
2772 ret = domain_page_mapping(domain, start_addr + offset,
David Woodhousefd18de52009-05-10 23:57:41 +01002773 ((u64)addr) & PHYSICAL_PAGE_MASK,
2774 size, prot);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002775 if (ret) {
2776 /* clear the page */
2777 dma_pte_clear_range(domain, start_addr,
2778 start_addr + offset);
2779 /* free page tables */
2780 dma_pte_free_pagetable(domain, start_addr,
2781 start_addr + offset);
2782 /* free iova */
2783 __free_iova(&domain->iovad, iova);
2784 return 0;
2785 }
2786 sg->dma_address = start_addr + offset +
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002787 ((u64)addr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002788 sg->dma_length = sg->length;
2789 offset += size;
2790 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002791
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002792 /* it's a non-present to present mapping. Only flush if caching mode */
2793 if (cap_caching_mode(iommu->cap))
2794 iommu_flush_iotlb_psi(iommu, 0, start_addr,
2795 offset >> VTD_PAGE_SHIFT);
2796 else
Weidong Han8c11e792008-12-08 15:29:22 +08002797 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002798
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002799 return nelems;
2800}
2801
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002802static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2803{
2804 return !dma_addr;
2805}
2806
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002807struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002808 .alloc_coherent = intel_alloc_coherent,
2809 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002810 .map_sg = intel_map_sg,
2811 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002812 .map_page = intel_map_page,
2813 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002814 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002815};
2816
2817static inline int iommu_domain_cache_init(void)
2818{
2819 int ret = 0;
2820
2821 iommu_domain_cache = kmem_cache_create("iommu_domain",
2822 sizeof(struct dmar_domain),
2823 0,
2824 SLAB_HWCACHE_ALIGN,
2825
2826 NULL);
2827 if (!iommu_domain_cache) {
2828 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2829 ret = -ENOMEM;
2830 }
2831
2832 return ret;
2833}
2834
2835static inline int iommu_devinfo_cache_init(void)
2836{
2837 int ret = 0;
2838
2839 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2840 sizeof(struct device_domain_info),
2841 0,
2842 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002843 NULL);
2844 if (!iommu_devinfo_cache) {
2845 printk(KERN_ERR "Couldn't create devinfo cache\n");
2846 ret = -ENOMEM;
2847 }
2848
2849 return ret;
2850}
2851
2852static inline int iommu_iova_cache_init(void)
2853{
2854 int ret = 0;
2855
2856 iommu_iova_cache = kmem_cache_create("iommu_iova",
2857 sizeof(struct iova),
2858 0,
2859 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002860 NULL);
2861 if (!iommu_iova_cache) {
2862 printk(KERN_ERR "Couldn't create iova cache\n");
2863 ret = -ENOMEM;
2864 }
2865
2866 return ret;
2867}
2868
2869static int __init iommu_init_mempool(void)
2870{
2871 int ret;
2872 ret = iommu_iova_cache_init();
2873 if (ret)
2874 return ret;
2875
2876 ret = iommu_domain_cache_init();
2877 if (ret)
2878 goto domain_error;
2879
2880 ret = iommu_devinfo_cache_init();
2881 if (!ret)
2882 return ret;
2883
2884 kmem_cache_destroy(iommu_domain_cache);
2885domain_error:
2886 kmem_cache_destroy(iommu_iova_cache);
2887
2888 return -ENOMEM;
2889}
2890
2891static void __init iommu_exit_mempool(void)
2892{
2893 kmem_cache_destroy(iommu_devinfo_cache);
2894 kmem_cache_destroy(iommu_domain_cache);
2895 kmem_cache_destroy(iommu_iova_cache);
2896
2897}
2898
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002899static void __init init_no_remapping_devices(void)
2900{
2901 struct dmar_drhd_unit *drhd;
2902
2903 for_each_drhd_unit(drhd) {
2904 if (!drhd->include_all) {
2905 int i;
2906 for (i = 0; i < drhd->devices_cnt; i++)
2907 if (drhd->devices[i] != NULL)
2908 break;
2909 /* ignore DMAR unit if no pci devices exist */
2910 if (i == drhd->devices_cnt)
2911 drhd->ignored = 1;
2912 }
2913 }
2914
2915 if (dmar_map_gfx)
2916 return;
2917
2918 for_each_drhd_unit(drhd) {
2919 int i;
2920 if (drhd->ignored || drhd->include_all)
2921 continue;
2922
2923 for (i = 0; i < drhd->devices_cnt; i++)
2924 if (drhd->devices[i] &&
2925 !IS_GFX_DEVICE(drhd->devices[i]))
2926 break;
2927
2928 if (i < drhd->devices_cnt)
2929 continue;
2930
2931 /* bypass IOMMU if it is just for gfx devices */
2932 drhd->ignored = 1;
2933 for (i = 0; i < drhd->devices_cnt; i++) {
2934 if (!drhd->devices[i])
2935 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002936 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002937 }
2938 }
2939}
2940
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002941#ifdef CONFIG_SUSPEND
2942static int init_iommu_hw(void)
2943{
2944 struct dmar_drhd_unit *drhd;
2945 struct intel_iommu *iommu = NULL;
2946
2947 for_each_active_iommu(iommu, drhd)
2948 if (iommu->qi)
2949 dmar_reenable_qi(iommu);
2950
2951 for_each_active_iommu(iommu, drhd) {
2952 iommu_flush_write_buffer(iommu);
2953
2954 iommu_set_root_entry(iommu);
2955
2956 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002957 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002958 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002959 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002960 iommu_disable_protect_mem_regions(iommu);
2961 iommu_enable_translation(iommu);
2962 }
2963
2964 return 0;
2965}
2966
2967static void iommu_flush_all(void)
2968{
2969 struct dmar_drhd_unit *drhd;
2970 struct intel_iommu *iommu;
2971
2972 for_each_active_iommu(iommu, drhd) {
2973 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002974 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002975 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002976 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002977 }
2978}
2979
2980static int iommu_suspend(struct sys_device *dev, pm_message_t state)
2981{
2982 struct dmar_drhd_unit *drhd;
2983 struct intel_iommu *iommu = NULL;
2984 unsigned long flag;
2985
2986 for_each_active_iommu(iommu, drhd) {
2987 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
2988 GFP_ATOMIC);
2989 if (!iommu->iommu_state)
2990 goto nomem;
2991 }
2992
2993 iommu_flush_all();
2994
2995 for_each_active_iommu(iommu, drhd) {
2996 iommu_disable_translation(iommu);
2997
2998 spin_lock_irqsave(&iommu->register_lock, flag);
2999
3000 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3001 readl(iommu->reg + DMAR_FECTL_REG);
3002 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3003 readl(iommu->reg + DMAR_FEDATA_REG);
3004 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3005 readl(iommu->reg + DMAR_FEADDR_REG);
3006 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3007 readl(iommu->reg + DMAR_FEUADDR_REG);
3008
3009 spin_unlock_irqrestore(&iommu->register_lock, flag);
3010 }
3011 return 0;
3012
3013nomem:
3014 for_each_active_iommu(iommu, drhd)
3015 kfree(iommu->iommu_state);
3016
3017 return -ENOMEM;
3018}
3019
3020static int iommu_resume(struct sys_device *dev)
3021{
3022 struct dmar_drhd_unit *drhd;
3023 struct intel_iommu *iommu = NULL;
3024 unsigned long flag;
3025
3026 if (init_iommu_hw()) {
3027 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
3028 return -EIO;
3029 }
3030
3031 for_each_active_iommu(iommu, drhd) {
3032
3033 spin_lock_irqsave(&iommu->register_lock, flag);
3034
3035 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3036 iommu->reg + DMAR_FECTL_REG);
3037 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3038 iommu->reg + DMAR_FEDATA_REG);
3039 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3040 iommu->reg + DMAR_FEADDR_REG);
3041 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3042 iommu->reg + DMAR_FEUADDR_REG);
3043
3044 spin_unlock_irqrestore(&iommu->register_lock, flag);
3045 }
3046
3047 for_each_active_iommu(iommu, drhd)
3048 kfree(iommu->iommu_state);
3049
3050 return 0;
3051}
3052
3053static struct sysdev_class iommu_sysclass = {
3054 .name = "iommu",
3055 .resume = iommu_resume,
3056 .suspend = iommu_suspend,
3057};
3058
3059static struct sys_device device_iommu = {
3060 .cls = &iommu_sysclass,
3061};
3062
3063static int __init init_iommu_sysfs(void)
3064{
3065 int error;
3066
3067 error = sysdev_class_register(&iommu_sysclass);
3068 if (error)
3069 return error;
3070
3071 error = sysdev_register(&device_iommu);
3072 if (error)
3073 sysdev_class_unregister(&iommu_sysclass);
3074
3075 return error;
3076}
3077
3078#else
3079static int __init init_iommu_sysfs(void)
3080{
3081 return 0;
3082}
3083#endif /* CONFIG_PM */
3084
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003085int __init intel_iommu_init(void)
3086{
3087 int ret = 0;
3088
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003089 if (dmar_table_init())
3090 return -ENODEV;
3091
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003092 if (dmar_dev_scope_init())
3093 return -ENODEV;
3094
Suresh Siddha2ae21012008-07-10 11:16:43 -07003095 /*
3096 * Check the need for DMA-remapping initialization now.
3097 * Above initialization will also be used by Interrupt-remapping.
3098 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003099 if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled)
Suresh Siddha2ae21012008-07-10 11:16:43 -07003100 return -ENODEV;
3101
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003102 iommu_init_mempool();
3103 dmar_init_reserved_ranges();
3104
3105 init_no_remapping_devices();
3106
3107 ret = init_dmars();
3108 if (ret) {
3109 printk(KERN_ERR "IOMMU: dmar init failed\n");
3110 put_iova_domain(&reserved_iova_list);
3111 iommu_exit_mempool();
3112 return ret;
3113 }
3114 printk(KERN_INFO
3115 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
3116
mark gross5e0d2a62008-03-04 15:22:08 -08003117 init_timer(&unmap_timer);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003118 force_iommu = 1;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003119
3120 if (!iommu_pass_through) {
3121 printk(KERN_INFO
3122 "Multi-level page-table translation for DMAR.\n");
3123 dma_ops = &intel_dma_ops;
3124 } else
3125 printk(KERN_INFO
3126 "DMAR: Pass through translation for DMAR.\n");
3127
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003128 init_iommu_sysfs();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003129
3130 register_iommu(&intel_iommu_ops);
3131
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003132 return 0;
3133}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07003134
Han, Weidong3199aa62009-02-26 17:31:12 +08003135static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
3136 struct pci_dev *pdev)
3137{
3138 struct pci_dev *tmp, *parent;
3139
3140 if (!iommu || !pdev)
3141 return;
3142
3143 /* dependent device detach */
3144 tmp = pci_find_upstream_pcie_bridge(pdev);
3145 /* Secondary interface's bus number and devfn 0 */
3146 if (tmp) {
3147 parent = pdev->bus->self;
3148 while (parent != tmp) {
3149 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01003150 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003151 parent = parent->bus->self;
3152 }
3153 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
3154 iommu_detach_dev(iommu,
3155 tmp->subordinate->number, 0);
3156 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01003157 iommu_detach_dev(iommu, tmp->bus->number,
3158 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003159 }
3160}
3161
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003162static void domain_remove_one_dev_info(struct dmar_domain *domain,
Weidong Hanc7151a82008-12-08 22:51:37 +08003163 struct pci_dev *pdev)
3164{
3165 struct device_domain_info *info;
3166 struct intel_iommu *iommu;
3167 unsigned long flags;
3168 int found = 0;
3169 struct list_head *entry, *tmp;
3170
David Woodhouse276dbf992009-04-04 01:45:37 +01003171 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3172 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003173 if (!iommu)
3174 return;
3175
3176 spin_lock_irqsave(&device_domain_lock, flags);
3177 list_for_each_safe(entry, tmp, &domain->devices) {
3178 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01003179 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08003180 if (info->bus == pdev->bus->number &&
3181 info->devfn == pdev->devfn) {
3182 list_del(&info->link);
3183 list_del(&info->global);
3184 if (info->dev)
3185 info->dev->dev.archdata.iommu = NULL;
3186 spin_unlock_irqrestore(&device_domain_lock, flags);
3187
Yu Zhao93a23a72009-05-18 13:51:37 +08003188 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08003189 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003190 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003191 free_devinfo_mem(info);
3192
3193 spin_lock_irqsave(&device_domain_lock, flags);
3194
3195 if (found)
3196 break;
3197 else
3198 continue;
3199 }
3200
3201 /* if there is no other devices under the same iommu
3202 * owned by this domain, clear this iommu in iommu_bmp
3203 * update iommu count and coherency
3204 */
David Woodhouse276dbf992009-04-04 01:45:37 +01003205 if (iommu == device_to_iommu(info->segment, info->bus,
3206 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08003207 found = 1;
3208 }
3209
3210 if (found == 0) {
3211 unsigned long tmp_flags;
3212 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3213 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3214 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003215 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003216 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
3217 }
3218
3219 spin_unlock_irqrestore(&device_domain_lock, flags);
3220}
3221
3222static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3223{
3224 struct device_domain_info *info;
3225 struct intel_iommu *iommu;
3226 unsigned long flags1, flags2;
3227
3228 spin_lock_irqsave(&device_domain_lock, flags1);
3229 while (!list_empty(&domain->devices)) {
3230 info = list_entry(domain->devices.next,
3231 struct device_domain_info, link);
3232 list_del(&info->link);
3233 list_del(&info->global);
3234 if (info->dev)
3235 info->dev->dev.archdata.iommu = NULL;
3236
3237 spin_unlock_irqrestore(&device_domain_lock, flags1);
3238
Yu Zhao93a23a72009-05-18 13:51:37 +08003239 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01003240 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003241 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003242 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003243
3244 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08003245 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08003246 */
3247 spin_lock_irqsave(&domain->iommu_lock, flags2);
3248 if (test_and_clear_bit(iommu->seq_id,
3249 &domain->iommu_bmp)) {
3250 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003251 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003252 }
3253 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3254
3255 free_devinfo_mem(info);
3256 spin_lock_irqsave(&device_domain_lock, flags1);
3257 }
3258 spin_unlock_irqrestore(&device_domain_lock, flags1);
3259}
3260
Weidong Han5e98c4b2008-12-08 23:03:27 +08003261/* domain id for virtual machine, it won't be set in context */
3262static unsigned long vm_domid;
3263
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003264static int vm_domain_min_agaw(struct dmar_domain *domain)
3265{
3266 int i;
3267 int min_agaw = domain->agaw;
3268
3269 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
3270 for (; i < g_num_of_iommus; ) {
3271 if (min_agaw > g_iommus[i]->agaw)
3272 min_agaw = g_iommus[i]->agaw;
3273
3274 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
3275 }
3276
3277 return min_agaw;
3278}
3279
Weidong Han5e98c4b2008-12-08 23:03:27 +08003280static struct dmar_domain *iommu_alloc_vm_domain(void)
3281{
3282 struct dmar_domain *domain;
3283
3284 domain = alloc_domain_mem();
3285 if (!domain)
3286 return NULL;
3287
3288 domain->id = vm_domid++;
3289 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3290 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3291
3292 return domain;
3293}
3294
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003295static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08003296{
3297 int adjust_width;
3298
3299 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3300 spin_lock_init(&domain->mapping_lock);
3301 spin_lock_init(&domain->iommu_lock);
3302
3303 domain_reserve_special_ranges(domain);
3304
3305 /* calculate AGAW */
3306 domain->gaw = guest_width;
3307 adjust_width = guestwidth_to_adjustwidth(guest_width);
3308 domain->agaw = width_to_agaw(adjust_width);
3309
3310 INIT_LIST_HEAD(&domain->devices);
3311
3312 domain->iommu_count = 0;
3313 domain->iommu_coherency = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003314 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003315
3316 /* always allocate the top pgd */
3317 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3318 if (!domain->pgd)
3319 return -ENOMEM;
3320 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3321 return 0;
3322}
3323
3324static void iommu_free_vm_domain(struct dmar_domain *domain)
3325{
3326 unsigned long flags;
3327 struct dmar_drhd_unit *drhd;
3328 struct intel_iommu *iommu;
3329 unsigned long i;
3330 unsigned long ndomains;
3331
3332 for_each_drhd_unit(drhd) {
3333 if (drhd->ignored)
3334 continue;
3335 iommu = drhd->iommu;
3336
3337 ndomains = cap_ndoms(iommu->cap);
3338 i = find_first_bit(iommu->domain_ids, ndomains);
3339 for (; i < ndomains; ) {
3340 if (iommu->domains[i] == domain) {
3341 spin_lock_irqsave(&iommu->lock, flags);
3342 clear_bit(i, iommu->domain_ids);
3343 iommu->domains[i] = NULL;
3344 spin_unlock_irqrestore(&iommu->lock, flags);
3345 break;
3346 }
3347 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3348 }
3349 }
3350}
3351
3352static void vm_domain_exit(struct dmar_domain *domain)
3353{
3354 u64 end;
3355
3356 /* Domain 0 is reserved, so dont process it */
3357 if (!domain)
3358 return;
3359
3360 vm_domain_remove_all_dev_info(domain);
3361 /* destroy iovas */
3362 put_iova_domain(&domain->iovad);
3363 end = DOMAIN_MAX_ADDR(domain->gaw);
3364 end = end & (~VTD_PAGE_MASK);
3365
3366 /* clear ptes */
3367 dma_pte_clear_range(domain, 0, end);
3368
3369 /* free page tables */
3370 dma_pte_free_pagetable(domain, 0, end);
3371
3372 iommu_free_vm_domain(domain);
3373 free_domain_mem(domain);
3374}
3375
Joerg Roedel5d450802008-12-03 14:52:32 +01003376static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003377{
Joerg Roedel5d450802008-12-03 14:52:32 +01003378 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003379
Joerg Roedel5d450802008-12-03 14:52:32 +01003380 dmar_domain = iommu_alloc_vm_domain();
3381 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003382 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003383 "intel_iommu_domain_init: dmar_domain == NULL\n");
3384 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003385 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003386 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003387 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003388 "intel_iommu_domain_init() failed\n");
3389 vm_domain_exit(dmar_domain);
3390 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003391 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003392 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003393
Joerg Roedel5d450802008-12-03 14:52:32 +01003394 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003395}
Kay, Allen M38717942008-09-09 18:37:29 +03003396
Joerg Roedel5d450802008-12-03 14:52:32 +01003397static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003398{
Joerg Roedel5d450802008-12-03 14:52:32 +01003399 struct dmar_domain *dmar_domain = domain->priv;
3400
3401 domain->priv = NULL;
3402 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003403}
Kay, Allen M38717942008-09-09 18:37:29 +03003404
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003405static int intel_iommu_attach_device(struct iommu_domain *domain,
3406 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003407{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003408 struct dmar_domain *dmar_domain = domain->priv;
3409 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003410 struct intel_iommu *iommu;
3411 int addr_width;
3412 u64 end;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003413 int ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003414
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003415 /* normally pdev is not mapped */
3416 if (unlikely(domain_context_mapped(pdev))) {
3417 struct dmar_domain *old_domain;
3418
3419 old_domain = find_domain(pdev);
3420 if (old_domain) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003421 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
3422 dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)
3423 domain_remove_one_dev_info(old_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003424 else
3425 domain_remove_dev_info(old_domain);
3426 }
3427 }
3428
David Woodhouse276dbf992009-04-04 01:45:37 +01003429 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3430 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003431 if (!iommu)
3432 return -ENODEV;
3433
3434 /* check if this iommu agaw is sufficient for max mapped address */
3435 addr_width = agaw_to_width(iommu->agaw);
3436 end = DOMAIN_MAX_ADDR(addr_width);
3437 end = end & VTD_PAGE_MASK;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003438 if (end < dmar_domain->max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003439 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3440 "sufficient for the mapped address (%llx)\n",
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003441 __func__, iommu->agaw, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003442 return -EFAULT;
3443 }
3444
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003445 ret = domain_add_dev_info(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003446 if (ret)
3447 return ret;
3448
Yu Zhao93a23a72009-05-18 13:51:37 +08003449 ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003450 return ret;
3451}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003452
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003453static void intel_iommu_detach_device(struct iommu_domain *domain,
3454 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003455{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003456 struct dmar_domain *dmar_domain = domain->priv;
3457 struct pci_dev *pdev = to_pci_dev(dev);
3458
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003459 domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003460}
Kay, Allen M38717942008-09-09 18:37:29 +03003461
Joerg Roedeldde57a22008-12-03 15:04:09 +01003462static int intel_iommu_map_range(struct iommu_domain *domain,
3463 unsigned long iova, phys_addr_t hpa,
3464 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003465{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003466 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003467 u64 max_addr;
3468 int addr_width;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003469 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003470 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003471
Joerg Roedeldde57a22008-12-03 15:04:09 +01003472 if (iommu_prot & IOMMU_READ)
3473 prot |= DMA_PTE_READ;
3474 if (iommu_prot & IOMMU_WRITE)
3475 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08003476 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3477 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003478
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003479 max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003480 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003481 int min_agaw;
3482 u64 end;
3483
3484 /* check if minimum agaw is sufficient for mapped address */
Joerg Roedeldde57a22008-12-03 15:04:09 +01003485 min_agaw = vm_domain_min_agaw(dmar_domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003486 addr_width = agaw_to_width(min_agaw);
3487 end = DOMAIN_MAX_ADDR(addr_width);
3488 end = end & VTD_PAGE_MASK;
3489 if (end < max_addr) {
3490 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3491 "sufficient for the mapped address (%llx)\n",
3492 __func__, min_agaw, max_addr);
3493 return -EFAULT;
3494 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003495 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003496 }
3497
Joerg Roedeldde57a22008-12-03 15:04:09 +01003498 ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003499 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003500}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003501
Joerg Roedeldde57a22008-12-03 15:04:09 +01003502static void intel_iommu_unmap_range(struct iommu_domain *domain,
3503 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003504{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003505 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003506 dma_addr_t base;
3507
3508 /* The address might not be aligned */
3509 base = iova & VTD_PAGE_MASK;
3510 size = VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003511 dma_pte_clear_range(dmar_domain, base, base + size);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003512
Joerg Roedeldde57a22008-12-03 15:04:09 +01003513 if (dmar_domain->max_addr == base + size)
3514 dmar_domain->max_addr = base;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003515}
Kay, Allen M38717942008-09-09 18:37:29 +03003516
Joerg Roedeld14d6572008-12-03 15:06:57 +01003517static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3518 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003519{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003520 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003521 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003522 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003523
Joerg Roedeld14d6572008-12-03 15:06:57 +01003524 pte = addr_to_dma_pte(dmar_domain, iova);
Kay, Allen M38717942008-09-09 18:37:29 +03003525 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003526 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003527
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003528 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003529}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003530
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003531static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3532 unsigned long cap)
3533{
3534 struct dmar_domain *dmar_domain = domain->priv;
3535
3536 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3537 return dmar_domain->iommu_snooping;
3538
3539 return 0;
3540}
3541
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003542static struct iommu_ops intel_iommu_ops = {
3543 .domain_init = intel_iommu_domain_init,
3544 .domain_destroy = intel_iommu_domain_destroy,
3545 .attach_dev = intel_iommu_attach_device,
3546 .detach_dev = intel_iommu_detach_device,
3547 .map = intel_iommu_map_range,
3548 .unmap = intel_iommu_unmap_range,
3549 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003550 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003551};
David Woodhouse9af88142009-02-13 23:18:03 +00003552
3553static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3554{
3555 /*
3556 * Mobile 4 Series Chipset neglects to set RWBF capability,
3557 * but needs it:
3558 */
3559 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3560 rwbf_quirk = 1;
3561}
3562
3563DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);