]> git.ipfire.org Git - people/arne_f/kernel.git/blame - drivers/pci/intel-iommu.c
intel-iommu: Make dma_pte_clear_one() take pfn not address
[people/arne_f/kernel.git] / drivers / pci / intel-iommu.c
CommitLineData
ba395927
KA
1/*
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 *
98bcef56 17 * 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>
5b6985ce 21 * Author: Fenghua Yu <fenghua.yu@intel.com>
ba395927
KA
22 */
23
24#include <linux/init.h>
25#include <linux/bitmap.h>
5e0d2a6f 26#include <linux/debugfs.h>
ba395927
KA
27#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
ba395927
KA
30#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
5e0d2a6f 35#include <linux/timer.h>
38717946 36#include <linux/iova.h>
5d450806 37#include <linux/iommu.h>
38717946 38#include <linux/intel-iommu.h>
f59c7b69 39#include <linux/sysdev.h>
ba395927 40#include <asm/cacheflush.h>
46a7fa27 41#include <asm/iommu.h>
ba395927
KA
42#include "pci.h"
43
5b6985ce
FY
44#define ROOT_SIZE VTD_PAGE_SIZE
45#define CONTEXT_SIZE VTD_PAGE_SIZE
46
ba395927
KA
47#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
4ed0d3e6
FY
56#define MAX_AGAW_WIDTH 64
57
ba395927
KA
58#define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
59
f27be03b 60#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
284901a9 61#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
6a35528a 62#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
5e0d2a6f 63
fd18de50
DW
64#ifndef PHYSICAL_PAGE_MASK
65#define PHYSICAL_PAGE_MASK PAGE_MASK
66#endif
67
dd4e8319
DW
68/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
69 are never going to work. */
70static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
71{
72 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
73}
74
75static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
76{
77 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
78}
79static inline unsigned long page_to_dma_pfn(struct page *pg)
80{
81 return mm_to_dma_pfn(page_to_pfn(pg));
82}
83static inline unsigned long virt_to_dma_pfn(void *p)
84{
85 return page_to_dma_pfn(virt_to_page(p));
86}
87
d9630fe9
WH
88/* global iommu list, set NULL for ignored DMAR units */
89static struct intel_iommu **g_iommus;
90
9af88143
DW
91static int rwbf_quirk;
92
46b08e1a
MM
93/*
94 * 0: Present
95 * 1-11: Reserved
96 * 12-63: Context Ptr (12 - (haw-1))
97 * 64-127: Reserved
98 */
99struct root_entry {
100 u64 val;
101 u64 rsvd1;
102};
103#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
104static inline bool root_present(struct root_entry *root)
105{
106 return (root->val & 1);
107}
108static inline void set_root_present(struct root_entry *root)
109{
110 root->val |= 1;
111}
112static inline void set_root_value(struct root_entry *root, unsigned long value)
113{
114 root->val |= value & VTD_PAGE_MASK;
115}
116
117static inline struct context_entry *
118get_context_addr_from_root(struct root_entry *root)
119{
120 return (struct context_entry *)
121 (root_present(root)?phys_to_virt(
122 root->val & VTD_PAGE_MASK) :
123 NULL);
124}
125
7a8fc25e
MM
126/*
127 * low 64 bits:
128 * 0: present
129 * 1: fault processing disable
130 * 2-3: translation type
131 * 12-63: address space root
132 * high 64 bits:
133 * 0-2: address width
134 * 3-6: aval
135 * 8-23: domain id
136 */
137struct context_entry {
138 u64 lo;
139 u64 hi;
140};
c07e7d21
MM
141
142static inline bool context_present(struct context_entry *context)
143{
144 return (context->lo & 1);
145}
146static inline void context_set_present(struct context_entry *context)
147{
148 context->lo |= 1;
149}
150
151static inline void context_set_fault_enable(struct context_entry *context)
152{
153 context->lo &= (((u64)-1) << 2) | 1;
154}
155
c07e7d21
MM
156static inline void context_set_translation_type(struct context_entry *context,
157 unsigned long value)
158{
159 context->lo &= (((u64)-1) << 4) | 3;
160 context->lo |= (value & 3) << 2;
161}
162
163static inline void context_set_address_root(struct context_entry *context,
164 unsigned long value)
165{
166 context->lo |= value & VTD_PAGE_MASK;
167}
168
169static inline void context_set_address_width(struct context_entry *context,
170 unsigned long value)
171{
172 context->hi |= value & 7;
173}
174
175static inline void context_set_domain_id(struct context_entry *context,
176 unsigned long value)
177{
178 context->hi |= (value & ((1 << 16) - 1)) << 8;
179}
180
181static inline void context_clear_entry(struct context_entry *context)
182{
183 context->lo = 0;
184 context->hi = 0;
185}
7a8fc25e 186
622ba12a
MM
187/*
188 * 0: readable
189 * 1: writable
190 * 2-6: reserved
191 * 7: super page
9cf06697
SY
192 * 8-10: available
193 * 11: snoop behavior
622ba12a
MM
194 * 12-63: Host physcial address
195 */
196struct dma_pte {
197 u64 val;
198};
622ba12a 199
19c239ce
MM
200static inline void dma_clear_pte(struct dma_pte *pte)
201{
202 pte->val = 0;
203}
204
205static inline void dma_set_pte_readable(struct dma_pte *pte)
206{
207 pte->val |= DMA_PTE_READ;
208}
209
210static inline void dma_set_pte_writable(struct dma_pte *pte)
211{
212 pte->val |= DMA_PTE_WRITE;
213}
214
9cf06697
SY
215static inline void dma_set_pte_snp(struct dma_pte *pte)
216{
217 pte->val |= DMA_PTE_SNP;
218}
219
19c239ce
MM
220static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
221{
222 pte->val = (pte->val & ~3) | (prot & 3);
223}
224
225static inline u64 dma_pte_addr(struct dma_pte *pte)
226{
227 return (pte->val & VTD_PAGE_MASK);
228}
229
dd4e8319 230static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn)
19c239ce 231{
dd4e8319 232 pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT;
19c239ce
MM
233}
234
235static inline bool dma_pte_present(struct dma_pte *pte)
236{
237 return (pte->val & 3) != 0;
238}
622ba12a 239
2c2e2c38
FY
240/*
241 * This domain is a statically identity mapping domain.
242 * 1. This domain creats a static 1:1 mapping to all usable memory.
243 * 2. It maps to each iommu if successful.
244 * 3. Each iommu mapps to this domain if successful.
245 */
246struct dmar_domain *si_domain;
247
3b5410e7 248/* devices under the same p2p bridge are owned in one domain */
cdc7b837 249#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
3b5410e7 250
1ce28feb
WH
251/* domain represents a virtual machine, more than one devices
252 * across iommus may be owned in one domain, e.g. kvm guest.
253 */
254#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
255
2c2e2c38
FY
256/* si_domain contains mulitple devices */
257#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2)
258
99126f7c
MM
259struct dmar_domain {
260 int id; /* domain id */
8c11e798 261 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
99126f7c
MM
262
263 struct list_head devices; /* all devices' list */
264 struct iova_domain iovad; /* iova's that belong to this domain */
265
266 struct dma_pte *pgd; /* virtual address */
267 spinlock_t mapping_lock; /* page table lock */
268 int gaw; /* max guest address width */
269
270 /* adjusted guest address width, 0 is level 2 30-bit */
271 int agaw;
272
3b5410e7 273 int flags; /* flags to find out type of domain */
8e604097
WH
274
275 int iommu_coherency;/* indicate coherency of iommu access */
58c610bd 276 int iommu_snooping; /* indicate snooping control feature*/
c7151a8d
WH
277 int iommu_count; /* reference count of iommu */
278 spinlock_t iommu_lock; /* protect iommu set in domain */
fe40f1e0 279 u64 max_addr; /* maximum mapped address */
99126f7c
MM
280};
281
a647dacb
MM
282/* PCI domain-device relationship */
283struct device_domain_info {
284 struct list_head link; /* link to domain siblings */
285 struct list_head global; /* link to global list */
276dbf99
DW
286 int segment; /* PCI domain */
287 u8 bus; /* PCI bus number */
a647dacb
MM
288 u8 devfn; /* PCI devfn number */
289 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
93a23a72 290 struct intel_iommu *iommu; /* IOMMU used by this device */
a647dacb
MM
291 struct dmar_domain *domain; /* pointer to domain */
292};
293
5e0d2a6f 294static void flush_unmaps_timeout(unsigned long data);
295
296DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
297
80b20dd8 298#define HIGH_WATER_MARK 250
299struct deferred_flush_tables {
300 int next;
301 struct iova *iova[HIGH_WATER_MARK];
302 struct dmar_domain *domain[HIGH_WATER_MARK];
303};
304
305static struct deferred_flush_tables *deferred_flush;
306
5e0d2a6f 307/* bitmap for indexing intel_iommus */
5e0d2a6f 308static int g_num_of_iommus;
309
310static DEFINE_SPINLOCK(async_umap_flush_lock);
311static LIST_HEAD(unmaps_to_do);
312
313static int timer_on;
314static long list_size;
5e0d2a6f 315
ba395927
KA
316static void domain_remove_dev_info(struct dmar_domain *domain);
317
0cd5c3c8
KM
318#ifdef CONFIG_DMAR_DEFAULT_ON
319int dmar_disabled = 0;
320#else
321int dmar_disabled = 1;
322#endif /*CONFIG_DMAR_DEFAULT_ON*/
323
ba395927 324static int __initdata dmar_map_gfx = 1;
7d3b03ce 325static int dmar_forcedac;
5e0d2a6f 326static int intel_iommu_strict;
ba395927
KA
327
328#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
329static DEFINE_SPINLOCK(device_domain_lock);
330static LIST_HEAD(device_domain_list);
331
a8bcbb0d
JR
332static struct iommu_ops intel_iommu_ops;
333
ba395927
KA
334static int __init intel_iommu_setup(char *str)
335{
336 if (!str)
337 return -EINVAL;
338 while (*str) {
0cd5c3c8
KM
339 if (!strncmp(str, "on", 2)) {
340 dmar_disabled = 0;
341 printk(KERN_INFO "Intel-IOMMU: enabled\n");
342 } else if (!strncmp(str, "off", 3)) {
ba395927 343 dmar_disabled = 1;
0cd5c3c8 344 printk(KERN_INFO "Intel-IOMMU: disabled\n");
ba395927
KA
345 } else if (!strncmp(str, "igfx_off", 8)) {
346 dmar_map_gfx = 0;
347 printk(KERN_INFO
348 "Intel-IOMMU: disable GFX device mapping\n");
7d3b03ce 349 } else if (!strncmp(str, "forcedac", 8)) {
5e0d2a6f 350 printk(KERN_INFO
7d3b03ce
KA
351 "Intel-IOMMU: Forcing DAC for PCI devices\n");
352 dmar_forcedac = 1;
5e0d2a6f 353 } else if (!strncmp(str, "strict", 6)) {
354 printk(KERN_INFO
355 "Intel-IOMMU: disable batched IOTLB flush\n");
356 intel_iommu_strict = 1;
ba395927
KA
357 }
358
359 str += strcspn(str, ",");
360 while (*str == ',')
361 str++;
362 }
363 return 0;
364}
365__setup("intel_iommu=", intel_iommu_setup);
366
367static struct kmem_cache *iommu_domain_cache;
368static struct kmem_cache *iommu_devinfo_cache;
369static struct kmem_cache *iommu_iova_cache;
370
eb3fa7cb
KA
371static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
372{
373 unsigned int flags;
374 void *vaddr;
375
376 /* trying to avoid low memory issues */
377 flags = current->flags & PF_MEMALLOC;
378 current->flags |= PF_MEMALLOC;
379 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
380 current->flags &= (~PF_MEMALLOC | flags);
381 return vaddr;
382}
383
384
ba395927
KA
385static inline void *alloc_pgtable_page(void)
386{
eb3fa7cb
KA
387 unsigned int flags;
388 void *vaddr;
389
390 /* trying to avoid low memory issues */
391 flags = current->flags & PF_MEMALLOC;
392 current->flags |= PF_MEMALLOC;
393 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
394 current->flags &= (~PF_MEMALLOC | flags);
395 return vaddr;
ba395927
KA
396}
397
398static inline void free_pgtable_page(void *vaddr)
399{
400 free_page((unsigned long)vaddr);
401}
402
403static inline void *alloc_domain_mem(void)
404{
eb3fa7cb 405 return iommu_kmem_cache_alloc(iommu_domain_cache);
ba395927
KA
406}
407
38717946 408static void free_domain_mem(void *vaddr)
ba395927
KA
409{
410 kmem_cache_free(iommu_domain_cache, vaddr);
411}
412
413static inline void * alloc_devinfo_mem(void)
414{
eb3fa7cb 415 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
ba395927
KA
416}
417
418static inline void free_devinfo_mem(void *vaddr)
419{
420 kmem_cache_free(iommu_devinfo_cache, vaddr);
421}
422
423struct iova *alloc_iova_mem(void)
424{
eb3fa7cb 425 return iommu_kmem_cache_alloc(iommu_iova_cache);
ba395927
KA
426}
427
428void free_iova_mem(struct iova *iova)
429{
430 kmem_cache_free(iommu_iova_cache, iova);
431}
432
1b573683
WH
433
434static inline int width_to_agaw(int width);
435
4ed0d3e6 436static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
1b573683
WH
437{
438 unsigned long sagaw;
439 int agaw = -1;
440
441 sagaw = cap_sagaw(iommu->cap);
4ed0d3e6 442 for (agaw = width_to_agaw(max_gaw);
1b573683
WH
443 agaw >= 0; agaw--) {
444 if (test_bit(agaw, &sagaw))
445 break;
446 }
447
448 return agaw;
449}
450
4ed0d3e6
FY
451/*
452 * Calculate max SAGAW for each iommu.
453 */
454int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
455{
456 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
457}
458
459/*
460 * calculate agaw for each iommu.
461 * "SAGAW" may be different across iommus, use a default agaw, and
462 * get a supported less agaw for iommus that don't support the default agaw.
463 */
464int iommu_calculate_agaw(struct intel_iommu *iommu)
465{
466 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
467}
468
2c2e2c38 469/* This functionin only returns single iommu in a domain */
8c11e798
WH
470static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
471{
472 int iommu_id;
473
2c2e2c38 474 /* si_domain and vm domain should not get here. */
1ce28feb 475 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
2c2e2c38 476 BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
1ce28feb 477
8c11e798
WH
478 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
479 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
480 return NULL;
481
482 return g_iommus[iommu_id];
483}
484
8e604097
WH
485static void domain_update_iommu_coherency(struct dmar_domain *domain)
486{
487 int i;
488
489 domain->iommu_coherency = 1;
490
491 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
492 for (; i < g_num_of_iommus; ) {
493 if (!ecap_coherent(g_iommus[i]->ecap)) {
494 domain->iommu_coherency = 0;
495 break;
496 }
497 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
498 }
499}
500
58c610bd
SY
501static void domain_update_iommu_snooping(struct dmar_domain *domain)
502{
503 int i;
504
505 domain->iommu_snooping = 1;
506
507 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
508 for (; i < g_num_of_iommus; ) {
509 if (!ecap_sc_support(g_iommus[i]->ecap)) {
510 domain->iommu_snooping = 0;
511 break;
512 }
513 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
514 }
515}
516
517/* Some capabilities may be different across iommus */
518static void domain_update_iommu_cap(struct dmar_domain *domain)
519{
520 domain_update_iommu_coherency(domain);
521 domain_update_iommu_snooping(domain);
522}
523
276dbf99 524static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
c7151a8d
WH
525{
526 struct dmar_drhd_unit *drhd = NULL;
527 int i;
528
529 for_each_drhd_unit(drhd) {
530 if (drhd->ignored)
531 continue;
276dbf99
DW
532 if (segment != drhd->segment)
533 continue;
c7151a8d 534
924b6231 535 for (i = 0; i < drhd->devices_cnt; i++) {
288e4877
DH
536 if (drhd->devices[i] &&
537 drhd->devices[i]->bus->number == bus &&
c7151a8d
WH
538 drhd->devices[i]->devfn == devfn)
539 return drhd->iommu;
4958c5dc
DW
540 if (drhd->devices[i] &&
541 drhd->devices[i]->subordinate &&
924b6231
DW
542 drhd->devices[i]->subordinate->number <= bus &&
543 drhd->devices[i]->subordinate->subordinate >= bus)
544 return drhd->iommu;
545 }
c7151a8d
WH
546
547 if (drhd->include_all)
548 return drhd->iommu;
549 }
550
551 return NULL;
552}
553
5331fe6f
WH
554static void domain_flush_cache(struct dmar_domain *domain,
555 void *addr, int size)
556{
557 if (!domain->iommu_coherency)
558 clflush_cache_range(addr, size);
559}
560
ba395927
KA
561/* Gets context entry for a given bus and devfn */
562static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
563 u8 bus, u8 devfn)
564{
565 struct root_entry *root;
566 struct context_entry *context;
567 unsigned long phy_addr;
568 unsigned long flags;
569
570 spin_lock_irqsave(&iommu->lock, flags);
571 root = &iommu->root_entry[bus];
572 context = get_context_addr_from_root(root);
573 if (!context) {
574 context = (struct context_entry *)alloc_pgtable_page();
575 if (!context) {
576 spin_unlock_irqrestore(&iommu->lock, flags);
577 return NULL;
578 }
5b6985ce 579 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
ba395927
KA
580 phy_addr = virt_to_phys((void *)context);
581 set_root_value(root, phy_addr);
582 set_root_present(root);
583 __iommu_flush_cache(iommu, root, sizeof(*root));
584 }
585 spin_unlock_irqrestore(&iommu->lock, flags);
586 return &context[devfn];
587}
588
589static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
590{
591 struct root_entry *root;
592 struct context_entry *context;
593 int ret;
594 unsigned long flags;
595
596 spin_lock_irqsave(&iommu->lock, flags);
597 root = &iommu->root_entry[bus];
598 context = get_context_addr_from_root(root);
599 if (!context) {
600 ret = 0;
601 goto out;
602 }
c07e7d21 603 ret = context_present(&context[devfn]);
ba395927
KA
604out:
605 spin_unlock_irqrestore(&iommu->lock, flags);
606 return ret;
607}
608
609static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
610{
611 struct root_entry *root;
612 struct context_entry *context;
613 unsigned long flags;
614
615 spin_lock_irqsave(&iommu->lock, flags);
616 root = &iommu->root_entry[bus];
617 context = get_context_addr_from_root(root);
618 if (context) {
c07e7d21 619 context_clear_entry(&context[devfn]);
ba395927
KA
620 __iommu_flush_cache(iommu, &context[devfn], \
621 sizeof(*context));
622 }
623 spin_unlock_irqrestore(&iommu->lock, flags);
624}
625
626static void free_context_table(struct intel_iommu *iommu)
627{
628 struct root_entry *root;
629 int i;
630 unsigned long flags;
631 struct context_entry *context;
632
633 spin_lock_irqsave(&iommu->lock, flags);
634 if (!iommu->root_entry) {
635 goto out;
636 }
637 for (i = 0; i < ROOT_ENTRY_NR; i++) {
638 root = &iommu->root_entry[i];
639 context = get_context_addr_from_root(root);
640 if (context)
641 free_pgtable_page(context);
642 }
643 free_pgtable_page(iommu->root_entry);
644 iommu->root_entry = NULL;
645out:
646 spin_unlock_irqrestore(&iommu->lock, flags);
647}
648
649/* page table handling */
650#define LEVEL_STRIDE (9)
651#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
652
653static inline int agaw_to_level(int agaw)
654{
655 return agaw + 2;
656}
657
658static inline int agaw_to_width(int agaw)
659{
660 return 30 + agaw * LEVEL_STRIDE;
661
662}
663
664static inline int width_to_agaw(int width)
665{
666 return (width - 30) / LEVEL_STRIDE;
667}
668
669static inline unsigned int level_to_offset_bits(int level)
670{
671 return (12 + (level - 1) * LEVEL_STRIDE);
672}
673
77dfa56c 674static inline int pfn_level_offset(unsigned long pfn, int level)
ba395927 675{
77dfa56c 676 return (pfn >> (level_to_offset_bits(level) - 12)) & LEVEL_MASK;
ba395927
KA
677}
678
679static inline u64 level_mask(int level)
680{
681 return ((u64)-1 << level_to_offset_bits(level));
682}
683
684static inline u64 level_size(int level)
685{
686 return ((u64)1 << level_to_offset_bits(level));
687}
688
689static inline u64 align_to_level(u64 addr, int level)
690{
691 return ((addr + level_size(level) - 1) & level_mask(level));
692}
693
694static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
695{
696 int addr_width = agaw_to_width(domain->agaw);
697 struct dma_pte *parent, *pte = NULL;
698 int level = agaw_to_level(domain->agaw);
699 int offset;
700 unsigned long flags;
701
702 BUG_ON(!domain->pgd);
703
704 addr &= (((u64)1) << addr_width) - 1;
705 parent = domain->pgd;
706
707 spin_lock_irqsave(&domain->mapping_lock, flags);
708 while (level > 0) {
709 void *tmp_page;
710
77dfa56c 711 offset = pfn_level_offset(addr >> VTD_PAGE_SHIFT, level);
ba395927
KA
712 pte = &parent[offset];
713 if (level == 1)
714 break;
715
19c239ce 716 if (!dma_pte_present(pte)) {
ba395927
KA
717 tmp_page = alloc_pgtable_page();
718
719 if (!tmp_page) {
720 spin_unlock_irqrestore(&domain->mapping_lock,
721 flags);
722 return NULL;
723 }
5331fe6f 724 domain_flush_cache(domain, tmp_page, PAGE_SIZE);
dd4e8319 725 dma_set_pte_pfn(pte, virt_to_dma_pfn(tmp_page));
ba395927
KA
726 /*
727 * high level table always sets r/w, last level page
728 * table control read/write
729 */
19c239ce
MM
730 dma_set_pte_readable(pte);
731 dma_set_pte_writable(pte);
5331fe6f 732 domain_flush_cache(domain, pte, sizeof(*pte));
ba395927 733 }
19c239ce 734 parent = phys_to_virt(dma_pte_addr(pte));
ba395927
KA
735 level--;
736 }
737
738 spin_unlock_irqrestore(&domain->mapping_lock, flags);
739 return pte;
740}
741
742/* return address's pte at specific level */
90dcfb5e
DW
743static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
744 unsigned long pfn,
745 int level)
ba395927
KA
746{
747 struct dma_pte *parent, *pte = NULL;
748 int total = agaw_to_level(domain->agaw);
749 int offset;
750
751 parent = domain->pgd;
752 while (level <= total) {
90dcfb5e 753 offset = pfn_level_offset(pfn, total);
ba395927
KA
754 pte = &parent[offset];
755 if (level == total)
756 return pte;
757
19c239ce 758 if (!dma_pte_present(pte))
ba395927 759 break;
19c239ce 760 parent = phys_to_virt(dma_pte_addr(pte));
ba395927
KA
761 total--;
762 }
763 return NULL;
764}
765
766/* clear one page's page table */
a75f7cf9 767static void dma_pte_clear_one(struct dmar_domain *domain, unsigned long pfn)
ba395927
KA
768{
769 struct dma_pte *pte = NULL;
770
771 /* get last level pte */
a75f7cf9 772 pte = dma_pfn_level_pte(domain, pfn, 1);
ba395927
KA
773
774 if (pte) {
19c239ce 775 dma_clear_pte(pte);
5331fe6f 776 domain_flush_cache(domain, pte, sizeof(*pte));
ba395927
KA
777 }
778}
779
780/* clear last level pte, a tlb flush should be followed */
781static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
782{
783 int addr_width = agaw_to_width(domain->agaw);
afeeb7ce 784 int npages;
ba395927
KA
785
786 start &= (((u64)1) << addr_width) - 1;
787 end &= (((u64)1) << addr_width) - 1;
788 /* in case it's partial page */
31d3568d
FY
789 start &= PAGE_MASK;
790 end = PAGE_ALIGN(end);
afeeb7ce 791 npages = (end - start) / VTD_PAGE_SIZE;
ba395927
KA
792
793 /* we don't need lock here, nobody else touches the iova range */
afeeb7ce 794 while (npages--) {
a75f7cf9 795 dma_pte_clear_one(domain, start >> VTD_PAGE_SHIFT);
5b6985ce 796 start += VTD_PAGE_SIZE;
ba395927
KA
797 }
798}
799
800/* free page table pages. last level pte should already be cleared */
801static void dma_pte_free_pagetable(struct dmar_domain *domain,
802 u64 start, u64 end)
803{
804 int addr_width = agaw_to_width(domain->agaw);
805 struct dma_pte *pte;
806 int total = agaw_to_level(domain->agaw);
807 int level;
808 u64 tmp;
809
810 start &= (((u64)1) << addr_width) - 1;
811 end &= (((u64)1) << addr_width) - 1;
812
813 /* we don't need lock here, nobody else touches the iova range */
814 level = 2;
815 while (level <= total) {
816 tmp = align_to_level(start, level);
817 if (tmp >= end || (tmp + level_size(level) > end))
818 return;
819
820 while (tmp < end) {
90dcfb5e
DW
821 pte = dma_pfn_level_pte(domain, tmp >> VTD_PAGE_SHIFT,
822 level);
ba395927
KA
823 if (pte) {
824 free_pgtable_page(
19c239ce
MM
825 phys_to_virt(dma_pte_addr(pte)));
826 dma_clear_pte(pte);
5331fe6f 827 domain_flush_cache(domain, pte, sizeof(*pte));
ba395927
KA
828 }
829 tmp += level_size(level);
830 }
831 level++;
832 }
833 /* free pgd */
834 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
835 free_pgtable_page(domain->pgd);
836 domain->pgd = NULL;
837 }
838}
839
840/* iommu handling */
841static int iommu_alloc_root_entry(struct intel_iommu *iommu)
842{
843 struct root_entry *root;
844 unsigned long flags;
845
846 root = (struct root_entry *)alloc_pgtable_page();
847 if (!root)
848 return -ENOMEM;
849
5b6985ce 850 __iommu_flush_cache(iommu, root, ROOT_SIZE);
ba395927
KA
851
852 spin_lock_irqsave(&iommu->lock, flags);
853 iommu->root_entry = root;
854 spin_unlock_irqrestore(&iommu->lock, flags);
855
856 return 0;
857}
858
ba395927
KA
859static void iommu_set_root_entry(struct intel_iommu *iommu)
860{
861 void *addr;
c416daa9 862 u32 sts;
ba395927
KA
863 unsigned long flag;
864
865 addr = iommu->root_entry;
866
867 spin_lock_irqsave(&iommu->register_lock, flag);
868 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
869
c416daa9 870 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
ba395927
KA
871
872 /* Make sure hardware complete it */
873 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
c416daa9 874 readl, (sts & DMA_GSTS_RTPS), sts);
ba395927
KA
875
876 spin_unlock_irqrestore(&iommu->register_lock, flag);
877}
878
879static void iommu_flush_write_buffer(struct intel_iommu *iommu)
880{
881 u32 val;
882 unsigned long flag;
883
9af88143 884 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
ba395927 885 return;
ba395927
KA
886
887 spin_lock_irqsave(&iommu->register_lock, flag);
462b60f6 888 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
ba395927
KA
889
890 /* Make sure hardware complete it */
891 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
c416daa9 892 readl, (!(val & DMA_GSTS_WBFS)), val);
ba395927
KA
893
894 spin_unlock_irqrestore(&iommu->register_lock, flag);
895}
896
897/* return value determine if we need a write buffer flush */
4c25a2c1
DW
898static void __iommu_flush_context(struct intel_iommu *iommu,
899 u16 did, u16 source_id, u8 function_mask,
900 u64 type)
ba395927
KA
901{
902 u64 val = 0;
903 unsigned long flag;
904
ba395927
KA
905 switch (type) {
906 case DMA_CCMD_GLOBAL_INVL:
907 val = DMA_CCMD_GLOBAL_INVL;
908 break;
909 case DMA_CCMD_DOMAIN_INVL:
910 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
911 break;
912 case DMA_CCMD_DEVICE_INVL:
913 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
914 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
915 break;
916 default:
917 BUG();
918 }
919 val |= DMA_CCMD_ICC;
920
921 spin_lock_irqsave(&iommu->register_lock, flag);
922 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
923
924 /* Make sure hardware complete it */
925 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
926 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
927
928 spin_unlock_irqrestore(&iommu->register_lock, flag);
ba395927
KA
929}
930
ba395927 931/* return value determine if we need a write buffer flush */
1f0ef2aa
DW
932static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
933 u64 addr, unsigned int size_order, u64 type)
ba395927
KA
934{
935 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
936 u64 val = 0, val_iva = 0;
937 unsigned long flag;
938
ba395927
KA
939 switch (type) {
940 case DMA_TLB_GLOBAL_FLUSH:
941 /* global flush doesn't need set IVA_REG */
942 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
943 break;
944 case DMA_TLB_DSI_FLUSH:
945 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
946 break;
947 case DMA_TLB_PSI_FLUSH:
948 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
949 /* Note: always flush non-leaf currently */
950 val_iva = size_order | addr;
951 break;
952 default:
953 BUG();
954 }
955 /* Note: set drain read/write */
956#if 0
957 /*
958 * This is probably to be super secure.. Looks like we can
959 * ignore it without any impact.
960 */
961 if (cap_read_drain(iommu->cap))
962 val |= DMA_TLB_READ_DRAIN;
963#endif
964 if (cap_write_drain(iommu->cap))
965 val |= DMA_TLB_WRITE_DRAIN;
966
967 spin_lock_irqsave(&iommu->register_lock, flag);
968 /* Note: Only uses first TLB reg currently */
969 if (val_iva)
970 dmar_writeq(iommu->reg + tlb_offset, val_iva);
971 dmar_writeq(iommu->reg + tlb_offset + 8, val);
972
973 /* Make sure hardware complete it */
974 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
975 dmar_readq, (!(val & DMA_TLB_IVT)), val);
976
977 spin_unlock_irqrestore(&iommu->register_lock, flag);
978
979 /* check IOTLB invalidation granularity */
980 if (DMA_TLB_IAIG(val) == 0)
981 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
982 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
983 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
5b6985ce
FY
984 (unsigned long long)DMA_TLB_IIRG(type),
985 (unsigned long long)DMA_TLB_IAIG(val));
ba395927
KA
986}
987
93a23a72
YZ
988static struct device_domain_info *iommu_support_dev_iotlb(
989 struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
990{
991 int found = 0;
992 unsigned long flags;
993 struct device_domain_info *info;
994 struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);
995
996 if (!ecap_dev_iotlb_support(iommu->ecap))
997 return NULL;
998
999 if (!iommu->qi)
1000 return NULL;
1001
1002 spin_lock_irqsave(&device_domain_lock, flags);
1003 list_for_each_entry(info, &domain->devices, link)
1004 if (info->bus == bus && info->devfn == devfn) {
1005 found = 1;
1006 break;
1007 }
1008 spin_unlock_irqrestore(&device_domain_lock, flags);
1009
1010 if (!found || !info->dev)
1011 return NULL;
1012
1013 if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
1014 return NULL;
1015
1016 if (!dmar_find_matched_atsr_unit(info->dev))
1017 return NULL;
1018
1019 info->iommu = iommu;
1020
1021 return info;
1022}
1023
1024static void iommu_enable_dev_iotlb(struct device_domain_info *info)
ba395927 1025{
93a23a72
YZ
1026 if (!info)
1027 return;
1028
1029 pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
1030}
1031
1032static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1033{
1034 if (!info->dev || !pci_ats_enabled(info->dev))
1035 return;
1036
1037 pci_disable_ats(info->dev);
1038}
1039
1040static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1041 u64 addr, unsigned mask)
1042{
1043 u16 sid, qdep;
1044 unsigned long flags;
1045 struct device_domain_info *info;
1046
1047 spin_lock_irqsave(&device_domain_lock, flags);
1048 list_for_each_entry(info, &domain->devices, link) {
1049 if (!info->dev || !pci_ats_enabled(info->dev))
1050 continue;
1051
1052 sid = info->bus << 8 | info->devfn;
1053 qdep = pci_ats_queue_depth(info->dev);
1054 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1055 }
1056 spin_unlock_irqrestore(&device_domain_lock, flags);
1057}
1058
1f0ef2aa
DW
1059static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
1060 u64 addr, unsigned int pages)
ba395927 1061{
9dd2fe89 1062 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
ba395927 1063
5b6985ce 1064 BUG_ON(addr & (~VTD_PAGE_MASK));
ba395927
KA
1065 BUG_ON(pages == 0);
1066
ba395927 1067 /*
9dd2fe89
YZ
1068 * Fallback to domain selective flush if no PSI support or the size is
1069 * too big.
ba395927
KA
1070 * PSI requires page size to be 2 ^ x, and the base address is naturally
1071 * aligned to the size
1072 */
9dd2fe89
YZ
1073 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1074 iommu->flush.flush_iotlb(iommu, did, 0, 0,
1f0ef2aa 1075 DMA_TLB_DSI_FLUSH);
9dd2fe89
YZ
1076 else
1077 iommu->flush.flush_iotlb(iommu, did, addr, mask,
1078 DMA_TLB_PSI_FLUSH);
bf92df30
YZ
1079
1080 /*
1081 * In caching mode, domain ID 0 is reserved for non-present to present
1082 * mapping flush. Device IOTLB doesn't need to be flushed in this case.
1083 */
1084 if (!cap_caching_mode(iommu->cap) || did)
93a23a72 1085 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
ba395927
KA
1086}
1087
f8bab735 1088static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1089{
1090 u32 pmen;
1091 unsigned long flags;
1092
1093 spin_lock_irqsave(&iommu->register_lock, flags);
1094 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1095 pmen &= ~DMA_PMEN_EPM;
1096 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1097
1098 /* wait for the protected region status bit to clear */
1099 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1100 readl, !(pmen & DMA_PMEN_PRS), pmen);
1101
1102 spin_unlock_irqrestore(&iommu->register_lock, flags);
1103}
1104
ba395927
KA
1105static int iommu_enable_translation(struct intel_iommu *iommu)
1106{
1107 u32 sts;
1108 unsigned long flags;
1109
1110 spin_lock_irqsave(&iommu->register_lock, flags);
c416daa9
DW
1111 iommu->gcmd |= DMA_GCMD_TE;
1112 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
ba395927
KA
1113
1114 /* Make sure hardware complete it */
1115 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
c416daa9 1116 readl, (sts & DMA_GSTS_TES), sts);
ba395927 1117
ba395927
KA
1118 spin_unlock_irqrestore(&iommu->register_lock, flags);
1119 return 0;
1120}
1121
1122static int iommu_disable_translation(struct intel_iommu *iommu)
1123{
1124 u32 sts;
1125 unsigned long flag;
1126
1127 spin_lock_irqsave(&iommu->register_lock, flag);
1128 iommu->gcmd &= ~DMA_GCMD_TE;
1129 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1130
1131 /* Make sure hardware complete it */
1132 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
c416daa9 1133 readl, (!(sts & DMA_GSTS_TES)), sts);
ba395927
KA
1134
1135 spin_unlock_irqrestore(&iommu->register_lock, flag);
1136 return 0;
1137}
1138
3460a6d9 1139
ba395927
KA
1140static int iommu_init_domains(struct intel_iommu *iommu)
1141{
1142 unsigned long ndomains;
1143 unsigned long nlongs;
1144
1145 ndomains = cap_ndoms(iommu->cap);
1146 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1147 nlongs = BITS_TO_LONGS(ndomains);
1148
1149 /* TBD: there might be 64K domains,
1150 * consider other allocation for future chip
1151 */
1152 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1153 if (!iommu->domain_ids) {
1154 printk(KERN_ERR "Allocating domain id array failed\n");
1155 return -ENOMEM;
1156 }
1157 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1158 GFP_KERNEL);
1159 if (!iommu->domains) {
1160 printk(KERN_ERR "Allocating domain array failed\n");
1161 kfree(iommu->domain_ids);
1162 return -ENOMEM;
1163 }
1164
e61d98d8
SS
1165 spin_lock_init(&iommu->lock);
1166
ba395927
KA
1167 /*
1168 * if Caching mode is set, then invalid translations are tagged
1169 * with domainid 0. Hence we need to pre-allocate it.
1170 */
1171 if (cap_caching_mode(iommu->cap))
1172 set_bit(0, iommu->domain_ids);
1173 return 0;
1174}
ba395927 1175
ba395927
KA
1176
1177static void domain_exit(struct dmar_domain *domain);
5e98c4b1 1178static void vm_domain_exit(struct dmar_domain *domain);
e61d98d8
SS
1179
1180void free_dmar_iommu(struct intel_iommu *iommu)
ba395927
KA
1181{
1182 struct dmar_domain *domain;
1183 int i;
c7151a8d 1184 unsigned long flags;
ba395927 1185
ba395927
KA
1186 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1187 for (; i < cap_ndoms(iommu->cap); ) {
1188 domain = iommu->domains[i];
1189 clear_bit(i, iommu->domain_ids);
c7151a8d
WH
1190
1191 spin_lock_irqsave(&domain->iommu_lock, flags);
5e98c4b1
WH
1192 if (--domain->iommu_count == 0) {
1193 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1194 vm_domain_exit(domain);
1195 else
1196 domain_exit(domain);
1197 }
c7151a8d
WH
1198 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1199
ba395927
KA
1200 i = find_next_bit(iommu->domain_ids,
1201 cap_ndoms(iommu->cap), i+1);
1202 }
1203
1204 if (iommu->gcmd & DMA_GCMD_TE)
1205 iommu_disable_translation(iommu);
1206
1207 if (iommu->irq) {
1208 set_irq_data(iommu->irq, NULL);
1209 /* This will mask the irq */
1210 free_irq(iommu->irq, iommu);
1211 destroy_irq(iommu->irq);
1212 }
1213
1214 kfree(iommu->domains);
1215 kfree(iommu->domain_ids);
1216
d9630fe9
WH
1217 g_iommus[iommu->seq_id] = NULL;
1218
1219 /* if all iommus are freed, free g_iommus */
1220 for (i = 0; i < g_num_of_iommus; i++) {
1221 if (g_iommus[i])
1222 break;
1223 }
1224
1225 if (i == g_num_of_iommus)
1226 kfree(g_iommus);
1227
ba395927
KA
1228 /* free context mapping */
1229 free_context_table(iommu);
ba395927
KA
1230}
1231
2c2e2c38 1232static struct dmar_domain *alloc_domain(void)
ba395927 1233{
ba395927 1234 struct dmar_domain *domain;
ba395927
KA
1235
1236 domain = alloc_domain_mem();
1237 if (!domain)
1238 return NULL;
1239
2c2e2c38
FY
1240 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
1241 domain->flags = 0;
1242
1243 return domain;
1244}
1245
1246static int iommu_attach_domain(struct dmar_domain *domain,
1247 struct intel_iommu *iommu)
1248{
1249 int num;
1250 unsigned long ndomains;
1251 unsigned long flags;
1252
ba395927
KA
1253 ndomains = cap_ndoms(iommu->cap);
1254
1255 spin_lock_irqsave(&iommu->lock, flags);
2c2e2c38 1256
ba395927
KA
1257 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1258 if (num >= ndomains) {
1259 spin_unlock_irqrestore(&iommu->lock, flags);
ba395927 1260 printk(KERN_ERR "IOMMU: no free domain ids\n");
2c2e2c38 1261 return -ENOMEM;
ba395927
KA
1262 }
1263
ba395927 1264 domain->id = num;
2c2e2c38 1265 set_bit(num, iommu->domain_ids);
8c11e798 1266 set_bit(iommu->seq_id, &domain->iommu_bmp);
ba395927
KA
1267 iommu->domains[num] = domain;
1268 spin_unlock_irqrestore(&iommu->lock, flags);
1269
2c2e2c38 1270 return 0;
ba395927
KA
1271}
1272
2c2e2c38
FY
1273static void iommu_detach_domain(struct dmar_domain *domain,
1274 struct intel_iommu *iommu)
ba395927
KA
1275{
1276 unsigned long flags;
2c2e2c38
FY
1277 int num, ndomains;
1278 int found = 0;
ba395927 1279
8c11e798 1280 spin_lock_irqsave(&iommu->lock, flags);
2c2e2c38
FY
1281 ndomains = cap_ndoms(iommu->cap);
1282 num = find_first_bit(iommu->domain_ids, ndomains);
1283 for (; num < ndomains; ) {
1284 if (iommu->domains[num] == domain) {
1285 found = 1;
1286 break;
1287 }
1288 num = find_next_bit(iommu->domain_ids,
1289 cap_ndoms(iommu->cap), num+1);
1290 }
1291
1292 if (found) {
1293 clear_bit(num, iommu->domain_ids);
1294 clear_bit(iommu->seq_id, &domain->iommu_bmp);
1295 iommu->domains[num] = NULL;
1296 }
8c11e798 1297 spin_unlock_irqrestore(&iommu->lock, flags);
ba395927
KA
1298}
1299
1300static struct iova_domain reserved_iova_list;
8a443df4
MG
1301static struct lock_class_key reserved_alloc_key;
1302static struct lock_class_key reserved_rbtree_key;
ba395927
KA
1303
1304static void dmar_init_reserved_ranges(void)
1305{
1306 struct pci_dev *pdev = NULL;
1307 struct iova *iova;
1308 int i;
1309 u64 addr, size;
1310
f661197e 1311 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
ba395927 1312
8a443df4
MG
1313 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1314 &reserved_alloc_key);
1315 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1316 &reserved_rbtree_key);
1317
ba395927
KA
1318 /* IOAPIC ranges shouldn't be accessed by DMA */
1319 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1320 IOVA_PFN(IOAPIC_RANGE_END));
1321 if (!iova)
1322 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1323
1324 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1325 for_each_pci_dev(pdev) {
1326 struct resource *r;
1327
1328 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1329 r = &pdev->resource[i];
1330 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1331 continue;
1332 addr = r->start;
fd18de50 1333 addr &= PHYSICAL_PAGE_MASK;
ba395927 1334 size = r->end - addr;
5b6985ce 1335 size = PAGE_ALIGN(size);
ba395927
KA
1336 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1337 IOVA_PFN(size + addr) - 1);
1338 if (!iova)
1339 printk(KERN_ERR "Reserve iova failed\n");
1340 }
1341 }
1342
1343}
1344
1345static void domain_reserve_special_ranges(struct dmar_domain *domain)
1346{
1347 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1348}
1349
1350static inline int guestwidth_to_adjustwidth(int gaw)
1351{
1352 int agaw;
1353 int r = (gaw - 12) % 9;
1354
1355 if (r == 0)
1356 agaw = gaw;
1357 else
1358 agaw = gaw + 9 - r;
1359 if (agaw > 64)
1360 agaw = 64;
1361 return agaw;
1362}
1363
1364static int domain_init(struct dmar_domain *domain, int guest_width)
1365{
1366 struct intel_iommu *iommu;
1367 int adjust_width, agaw;
1368 unsigned long sagaw;
1369
f661197e 1370 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
ba395927 1371 spin_lock_init(&domain->mapping_lock);
c7151a8d 1372 spin_lock_init(&domain->iommu_lock);
ba395927
KA
1373
1374 domain_reserve_special_ranges(domain);
1375
1376 /* calculate AGAW */
8c11e798 1377 iommu = domain_get_iommu(domain);
ba395927
KA
1378 if (guest_width > cap_mgaw(iommu->cap))
1379 guest_width = cap_mgaw(iommu->cap);
1380 domain->gaw = guest_width;
1381 adjust_width = guestwidth_to_adjustwidth(guest_width);
1382 agaw = width_to_agaw(adjust_width);
1383 sagaw = cap_sagaw(iommu->cap);
1384 if (!test_bit(agaw, &sagaw)) {
1385 /* hardware doesn't support it, choose a bigger one */
1386 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1387 agaw = find_next_bit(&sagaw, 5, agaw);
1388 if (agaw >= 5)
1389 return -ENODEV;
1390 }
1391 domain->agaw = agaw;
1392 INIT_LIST_HEAD(&domain->devices);
1393
8e604097
WH
1394 if (ecap_coherent(iommu->ecap))
1395 domain->iommu_coherency = 1;
1396 else
1397 domain->iommu_coherency = 0;
1398
58c610bd
SY
1399 if (ecap_sc_support(iommu->ecap))
1400 domain->iommu_snooping = 1;
1401 else
1402 domain->iommu_snooping = 0;
1403
c7151a8d
WH
1404 domain->iommu_count = 1;
1405
ba395927
KA
1406 /* always allocate the top pgd */
1407 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1408 if (!domain->pgd)
1409 return -ENOMEM;
5b6985ce 1410 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
ba395927
KA
1411 return 0;
1412}
1413
1414static void domain_exit(struct dmar_domain *domain)
1415{
2c2e2c38
FY
1416 struct dmar_drhd_unit *drhd;
1417 struct intel_iommu *iommu;
ba395927
KA
1418 u64 end;
1419
1420 /* Domain 0 is reserved, so dont process it */
1421 if (!domain)
1422 return;
1423
1424 domain_remove_dev_info(domain);
1425 /* destroy iovas */
1426 put_iova_domain(&domain->iovad);
1427 end = DOMAIN_MAX_ADDR(domain->gaw);
5b6985ce 1428 end = end & (~PAGE_MASK);
ba395927
KA
1429
1430 /* clear ptes */
1431 dma_pte_clear_range(domain, 0, end);
1432
1433 /* free page tables */
1434 dma_pte_free_pagetable(domain, 0, end);
1435
2c2e2c38
FY
1436 for_each_active_iommu(iommu, drhd)
1437 if (test_bit(iommu->seq_id, &domain->iommu_bmp))
1438 iommu_detach_domain(domain, iommu);
1439
ba395927
KA
1440 free_domain_mem(domain);
1441}
1442
4ed0d3e6
FY
1443static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1444 u8 bus, u8 devfn, int translation)
ba395927
KA
1445{
1446 struct context_entry *context;
ba395927 1447 unsigned long flags;
5331fe6f 1448 struct intel_iommu *iommu;
ea6606b0
WH
1449 struct dma_pte *pgd;
1450 unsigned long num;
1451 unsigned long ndomains;
1452 int id;
1453 int agaw;
93a23a72 1454 struct device_domain_info *info = NULL;
ba395927
KA
1455
1456 pr_debug("Set context mapping for %02x:%02x.%d\n",
1457 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
4ed0d3e6 1458
ba395927 1459 BUG_ON(!domain->pgd);
4ed0d3e6
FY
1460 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1461 translation != CONTEXT_TT_MULTI_LEVEL);
5331fe6f 1462
276dbf99 1463 iommu = device_to_iommu(segment, bus, devfn);
5331fe6f
WH
1464 if (!iommu)
1465 return -ENODEV;
1466
ba395927
KA
1467 context = device_to_context_entry(iommu, bus, devfn);
1468 if (!context)
1469 return -ENOMEM;
1470 spin_lock_irqsave(&iommu->lock, flags);
c07e7d21 1471 if (context_present(context)) {
ba395927
KA
1472 spin_unlock_irqrestore(&iommu->lock, flags);
1473 return 0;
1474 }
1475
ea6606b0
WH
1476 id = domain->id;
1477 pgd = domain->pgd;
1478
2c2e2c38
FY
1479 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
1480 domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) {
ea6606b0
WH
1481 int found = 0;
1482
1483 /* find an available domain id for this device in iommu */
1484 ndomains = cap_ndoms(iommu->cap);
1485 num = find_first_bit(iommu->domain_ids, ndomains);
1486 for (; num < ndomains; ) {
1487 if (iommu->domains[num] == domain) {
1488 id = num;
1489 found = 1;
1490 break;
1491 }
1492 num = find_next_bit(iommu->domain_ids,
1493 cap_ndoms(iommu->cap), num+1);
1494 }
1495
1496 if (found == 0) {
1497 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1498 if (num >= ndomains) {
1499 spin_unlock_irqrestore(&iommu->lock, flags);
1500 printk(KERN_ERR "IOMMU: no free domain ids\n");
1501 return -EFAULT;
1502 }
1503
1504 set_bit(num, iommu->domain_ids);
2c2e2c38 1505 set_bit(iommu->seq_id, &domain->iommu_bmp);
ea6606b0
WH
1506 iommu->domains[num] = domain;
1507 id = num;
1508 }
1509
1510 /* Skip top levels of page tables for
1511 * iommu which has less agaw than default.
1512 */
1513 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1514 pgd = phys_to_virt(dma_pte_addr(pgd));
1515 if (!dma_pte_present(pgd)) {
1516 spin_unlock_irqrestore(&iommu->lock, flags);
1517 return -ENOMEM;
1518 }
1519 }
1520 }
1521
1522 context_set_domain_id(context, id);
4ed0d3e6 1523
93a23a72
YZ
1524 if (translation != CONTEXT_TT_PASS_THROUGH) {
1525 info = iommu_support_dev_iotlb(domain, segment, bus, devfn);
1526 translation = info ? CONTEXT_TT_DEV_IOTLB :
1527 CONTEXT_TT_MULTI_LEVEL;
1528 }
4ed0d3e6
FY
1529 /*
1530 * In pass through mode, AW must be programmed to indicate the largest
1531 * AGAW value supported by hardware. And ASR is ignored by hardware.
1532 */
93a23a72 1533 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
4ed0d3e6 1534 context_set_address_width(context, iommu->msagaw);
93a23a72
YZ
1535 else {
1536 context_set_address_root(context, virt_to_phys(pgd));
1537 context_set_address_width(context, iommu->agaw);
1538 }
4ed0d3e6
FY
1539
1540 context_set_translation_type(context, translation);
c07e7d21
MM
1541 context_set_fault_enable(context);
1542 context_set_present(context);
5331fe6f 1543 domain_flush_cache(domain, context, sizeof(*context));
ba395927 1544
4c25a2c1
DW
1545 /*
1546 * It's a non-present to present mapping. If hardware doesn't cache
1547 * non-present entry we only need to flush the write-buffer. If the
1548 * _does_ cache non-present entries, then it does so in the special
1549 * domain #0, which we have to flush:
1550 */
1551 if (cap_caching_mode(iommu->cap)) {
1552 iommu->flush.flush_context(iommu, 0,
1553 (((u16)bus) << 8) | devfn,
1554 DMA_CCMD_MASK_NOBIT,
1555 DMA_CCMD_DEVICE_INVL);
1f0ef2aa 1556 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
4c25a2c1 1557 } else {
ba395927 1558 iommu_flush_write_buffer(iommu);
4c25a2c1 1559 }
93a23a72 1560 iommu_enable_dev_iotlb(info);
ba395927 1561 spin_unlock_irqrestore(&iommu->lock, flags);
c7151a8d
WH
1562
1563 spin_lock_irqsave(&domain->iommu_lock, flags);
1564 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1565 domain->iommu_count++;
58c610bd 1566 domain_update_iommu_cap(domain);
c7151a8d
WH
1567 }
1568 spin_unlock_irqrestore(&domain->iommu_lock, flags);
ba395927
KA
1569 return 0;
1570}
1571
1572static int
4ed0d3e6
FY
1573domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1574 int translation)
ba395927
KA
1575{
1576 int ret;
1577 struct pci_dev *tmp, *parent;
1578
276dbf99 1579 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
4ed0d3e6
FY
1580 pdev->bus->number, pdev->devfn,
1581 translation);
ba395927
KA
1582 if (ret)
1583 return ret;
1584
1585 /* dependent device mapping */
1586 tmp = pci_find_upstream_pcie_bridge(pdev);
1587 if (!tmp)
1588 return 0;
1589 /* Secondary interface's bus number and devfn 0 */
1590 parent = pdev->bus->self;
1591 while (parent != tmp) {
276dbf99
DW
1592 ret = domain_context_mapping_one(domain,
1593 pci_domain_nr(parent->bus),
1594 parent->bus->number,
4ed0d3e6 1595 parent->devfn, translation);
ba395927
KA
1596 if (ret)
1597 return ret;
1598 parent = parent->bus->self;
1599 }
1600 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1601 return domain_context_mapping_one(domain,
276dbf99 1602 pci_domain_nr(tmp->subordinate),
4ed0d3e6
FY
1603 tmp->subordinate->number, 0,
1604 translation);
ba395927
KA
1605 else /* this is a legacy PCI bridge */
1606 return domain_context_mapping_one(domain,
276dbf99
DW
1607 pci_domain_nr(tmp->bus),
1608 tmp->bus->number,
4ed0d3e6
FY
1609 tmp->devfn,
1610 translation);
ba395927
KA
1611}
1612
5331fe6f 1613static int domain_context_mapped(struct pci_dev *pdev)
ba395927
KA
1614{
1615 int ret;
1616 struct pci_dev *tmp, *parent;
5331fe6f
WH
1617 struct intel_iommu *iommu;
1618
276dbf99
DW
1619 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1620 pdev->devfn);
5331fe6f
WH
1621 if (!iommu)
1622 return -ENODEV;
ba395927 1623
276dbf99 1624 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
ba395927
KA
1625 if (!ret)
1626 return ret;
1627 /* dependent device mapping */
1628 tmp = pci_find_upstream_pcie_bridge(pdev);
1629 if (!tmp)
1630 return ret;
1631 /* Secondary interface's bus number and devfn 0 */
1632 parent = pdev->bus->self;
1633 while (parent != tmp) {
8c11e798 1634 ret = device_context_mapped(iommu, parent->bus->number,
276dbf99 1635 parent->devfn);
ba395927
KA
1636 if (!ret)
1637 return ret;
1638 parent = parent->bus->self;
1639 }
1640 if (tmp->is_pcie)
276dbf99
DW
1641 return device_context_mapped(iommu, tmp->subordinate->number,
1642 0);
ba395927 1643 else
276dbf99
DW
1644 return device_context_mapped(iommu, tmp->bus->number,
1645 tmp->devfn);
ba395927
KA
1646}
1647
1648static int
1649domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1650 u64 hpa, size_t size, int prot)
1651{
1652 u64 start_pfn, end_pfn;
1653 struct dma_pte *pte;
1654 int index;
5b6985ce
FY
1655 int addr_width = agaw_to_width(domain->agaw);
1656
1657 hpa &= (((u64)1) << addr_width) - 1;
ba395927
KA
1658
1659 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1660 return -EINVAL;
5b6985ce
FY
1661 iova &= PAGE_MASK;
1662 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1663 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
ba395927
KA
1664 index = 0;
1665 while (start_pfn < end_pfn) {
5b6985ce 1666 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
ba395927
KA
1667 if (!pte)
1668 return -ENOMEM;
1669 /* We don't need lock here, nobody else
1670 * touches the iova range
1671 */
19c239ce 1672 BUG_ON(dma_pte_addr(pte));
dd4e8319 1673 dma_set_pte_pfn(pte, start_pfn);
19c239ce 1674 dma_set_pte_prot(pte, prot);
9cf06697
SY
1675 if (prot & DMA_PTE_SNP)
1676 dma_set_pte_snp(pte);
5331fe6f 1677 domain_flush_cache(domain, pte, sizeof(*pte));
ba395927
KA
1678 start_pfn++;
1679 index++;
1680 }
1681 return 0;
1682}
1683
c7151a8d 1684static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
ba395927 1685{
c7151a8d
WH
1686 if (!iommu)
1687 return;
8c11e798
WH
1688
1689 clear_context_table(iommu, bus, devfn);
1690 iommu->flush.flush_context(iommu, 0, 0, 0,
4c25a2c1 1691 DMA_CCMD_GLOBAL_INVL);
1f0ef2aa 1692 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
ba395927
KA
1693}
1694
1695static void domain_remove_dev_info(struct dmar_domain *domain)
1696{
1697 struct device_domain_info *info;
1698 unsigned long flags;
c7151a8d 1699 struct intel_iommu *iommu;
ba395927
KA
1700
1701 spin_lock_irqsave(&device_domain_lock, flags);
1702 while (!list_empty(&domain->devices)) {
1703 info = list_entry(domain->devices.next,
1704 struct device_domain_info, link);
1705 list_del(&info->link);
1706 list_del(&info->global);
1707 if (info->dev)
358dd8ac 1708 info->dev->dev.archdata.iommu = NULL;
ba395927
KA
1709 spin_unlock_irqrestore(&device_domain_lock, flags);
1710
93a23a72 1711 iommu_disable_dev_iotlb(info);
276dbf99 1712 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
c7151a8d 1713 iommu_detach_dev(iommu, info->bus, info->devfn);
ba395927
KA
1714 free_devinfo_mem(info);
1715
1716 spin_lock_irqsave(&device_domain_lock, flags);
1717 }
1718 spin_unlock_irqrestore(&device_domain_lock, flags);
1719}
1720
1721/*
1722 * find_domain
358dd8ac 1723 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
ba395927 1724 */
38717946 1725static struct dmar_domain *
ba395927
KA
1726find_domain(struct pci_dev *pdev)
1727{
1728 struct device_domain_info *info;
1729
1730 /* No lock here, assumes no domain exit in normal case */
358dd8ac 1731 info = pdev->dev.archdata.iommu;
ba395927
KA
1732 if (info)
1733 return info->domain;
1734 return NULL;
1735}
1736
ba395927
KA
1737/* domain is initialized */
1738static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1739{
1740 struct dmar_domain *domain, *found = NULL;
1741 struct intel_iommu *iommu;
1742 struct dmar_drhd_unit *drhd;
1743 struct device_domain_info *info, *tmp;
1744 struct pci_dev *dev_tmp;
1745 unsigned long flags;
1746 int bus = 0, devfn = 0;
276dbf99 1747 int segment;
2c2e2c38 1748 int ret;
ba395927
KA
1749
1750 domain = find_domain(pdev);
1751 if (domain)
1752 return domain;
1753
276dbf99
DW
1754 segment = pci_domain_nr(pdev->bus);
1755
ba395927
KA
1756 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1757 if (dev_tmp) {
1758 if (dev_tmp->is_pcie) {
1759 bus = dev_tmp->subordinate->number;
1760 devfn = 0;
1761 } else {
1762 bus = dev_tmp->bus->number;
1763 devfn = dev_tmp->devfn;
1764 }
1765 spin_lock_irqsave(&device_domain_lock, flags);
1766 list_for_each_entry(info, &device_domain_list, global) {
276dbf99
DW
1767 if (info->segment == segment &&
1768 info->bus == bus && info->devfn == devfn) {
ba395927
KA
1769 found = info->domain;
1770 break;
1771 }
1772 }
1773 spin_unlock_irqrestore(&device_domain_lock, flags);
1774 /* pcie-pci bridge already has a domain, uses it */
1775 if (found) {
1776 domain = found;
1777 goto found_domain;
1778 }
1779 }
1780
2c2e2c38
FY
1781 domain = alloc_domain();
1782 if (!domain)
1783 goto error;
1784
ba395927
KA
1785 /* Allocate new domain for the device */
1786 drhd = dmar_find_matched_drhd_unit(pdev);
1787 if (!drhd) {
1788 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1789 pci_name(pdev));
1790 return NULL;
1791 }
1792 iommu = drhd->iommu;
1793
2c2e2c38
FY
1794 ret = iommu_attach_domain(domain, iommu);
1795 if (ret) {
1796 domain_exit(domain);
ba395927 1797 goto error;
2c2e2c38 1798 }
ba395927
KA
1799
1800 if (domain_init(domain, gaw)) {
1801 domain_exit(domain);
1802 goto error;
1803 }
1804
1805 /* register pcie-to-pci device */
1806 if (dev_tmp) {
1807 info = alloc_devinfo_mem();
1808 if (!info) {
1809 domain_exit(domain);
1810 goto error;
1811 }
276dbf99 1812 info->segment = segment;
ba395927
KA
1813 info->bus = bus;
1814 info->devfn = devfn;
1815 info->dev = NULL;
1816 info->domain = domain;
1817 /* This domain is shared by devices under p2p bridge */
3b5410e7 1818 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
ba395927
KA
1819
1820 /* pcie-to-pci bridge already has a domain, uses it */
1821 found = NULL;
1822 spin_lock_irqsave(&device_domain_lock, flags);
1823 list_for_each_entry(tmp, &device_domain_list, global) {
276dbf99
DW
1824 if (tmp->segment == segment &&
1825 tmp->bus == bus && tmp->devfn == devfn) {
ba395927
KA
1826 found = tmp->domain;
1827 break;
1828 }
1829 }
1830 if (found) {
1831 free_devinfo_mem(info);
1832 domain_exit(domain);
1833 domain = found;
1834 } else {
1835 list_add(&info->link, &domain->devices);
1836 list_add(&info->global, &device_domain_list);
1837 }
1838 spin_unlock_irqrestore(&device_domain_lock, flags);
1839 }
1840
1841found_domain:
1842 info = alloc_devinfo_mem();
1843 if (!info)
1844 goto error;
276dbf99 1845 info->segment = segment;
ba395927
KA
1846 info->bus = pdev->bus->number;
1847 info->devfn = pdev->devfn;
1848 info->dev = pdev;
1849 info->domain = domain;
1850 spin_lock_irqsave(&device_domain_lock, flags);
1851 /* somebody is fast */
1852 found = find_domain(pdev);
1853 if (found != NULL) {
1854 spin_unlock_irqrestore(&device_domain_lock, flags);
1855 if (found != domain) {
1856 domain_exit(domain);
1857 domain = found;
1858 }
1859 free_devinfo_mem(info);
1860 return domain;
1861 }
1862 list_add(&info->link, &domain->devices);
1863 list_add(&info->global, &device_domain_list);
358dd8ac 1864 pdev->dev.archdata.iommu = info;
ba395927
KA
1865 spin_unlock_irqrestore(&device_domain_lock, flags);
1866 return domain;
1867error:
1868 /* recheck it here, maybe others set it */
1869 return find_domain(pdev);
1870}
1871
2c2e2c38
FY
1872static int iommu_identity_mapping;
1873
b213203e
DW
1874static int iommu_domain_identity_map(struct dmar_domain *domain,
1875 unsigned long long start,
1876 unsigned long long end)
ba395927 1877{
ba395927 1878 unsigned long size;
5b6985ce 1879 unsigned long long base;
ba395927
KA
1880
1881 /* The address might not be aligned */
5b6985ce 1882 base = start & PAGE_MASK;
ba395927 1883 size = end - base;
5b6985ce 1884 size = PAGE_ALIGN(size);
ba395927
KA
1885 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1886 IOVA_PFN(base + size) - 1)) {
1887 printk(KERN_ERR "IOMMU: reserve iova failed\n");
b213203e 1888 return -ENOMEM;
ba395927
KA
1889 }
1890
b213203e
DW
1891 pr_debug("Mapping reserved region %lx@%llx for domain %d\n",
1892 size, base, domain->id);
ba395927
KA
1893 /*
1894 * RMRR range might have overlap with physical memory range,
1895 * clear it first
1896 */
1897 dma_pte_clear_range(domain, base, base + size);
1898
b213203e
DW
1899 return domain_page_mapping(domain, base, base, size,
1900 DMA_PTE_READ|DMA_PTE_WRITE);
1901}
1902
1903static int iommu_prepare_identity_map(struct pci_dev *pdev,
1904 unsigned long long start,
1905 unsigned long long end)
1906{
1907 struct dmar_domain *domain;
1908 int ret;
1909
1910 printk(KERN_INFO
1911 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1912 pci_name(pdev), start, end);
1913
c7ab48d2 1914 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
b213203e
DW
1915 if (!domain)
1916 return -ENOMEM;
1917
1918 ret = iommu_domain_identity_map(domain, start, end);
ba395927
KA
1919 if (ret)
1920 goto error;
1921
1922 /* context entry init */
4ed0d3e6 1923 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
b213203e
DW
1924 if (ret)
1925 goto error;
1926
1927 return 0;
1928
1929 error:
ba395927
KA
1930 domain_exit(domain);
1931 return ret;
ba395927
KA
1932}
1933
1934static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1935 struct pci_dev *pdev)
1936{
358dd8ac 1937 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
ba395927
KA
1938 return 0;
1939 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1940 rmrr->end_address + 1);
1941}
1942
49a0429e
KA
1943#ifdef CONFIG_DMAR_FLOPPY_WA
1944static inline void iommu_prepare_isa(void)
1945{
1946 struct pci_dev *pdev;
1947 int ret;
1948
1949 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1950 if (!pdev)
1951 return;
1952
c7ab48d2 1953 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
49a0429e
KA
1954 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1955
1956 if (ret)
c7ab48d2
DW
1957 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
1958 "floppy might not work\n");
49a0429e
KA
1959
1960}
1961#else
1962static inline void iommu_prepare_isa(void)
1963{
1964 return;
1965}
1966#endif /* !CONFIG_DMAR_FLPY_WA */
1967
4ed0d3e6
FY
1968/* Initialize each context entry as pass through.*/
1969static int __init init_context_pass_through(void)
1970{
1971 struct pci_dev *pdev = NULL;
1972 struct dmar_domain *domain;
1973 int ret;
1974
1975 for_each_pci_dev(pdev) {
1976 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1977 ret = domain_context_mapping(domain, pdev,
1978 CONTEXT_TT_PASS_THROUGH);
1979 if (ret)
1980 return ret;
1981 }
1982 return 0;
1983}
1984
2c2e2c38 1985static int md_domain_init(struct dmar_domain *domain, int guest_width);
c7ab48d2
DW
1986
1987static int __init si_domain_work_fn(unsigned long start_pfn,
1988 unsigned long end_pfn, void *datax)
1989{
1990 int *ret = datax;
1991
1992 *ret = iommu_domain_identity_map(si_domain,
1993 (uint64_t)start_pfn << PAGE_SHIFT,
1994 (uint64_t)end_pfn << PAGE_SHIFT);
1995 return *ret;
1996
1997}
1998
2c2e2c38
FY
1999static int si_domain_init(void)
2000{
2001 struct dmar_drhd_unit *drhd;
2002 struct intel_iommu *iommu;
c7ab48d2 2003 int nid, ret = 0;
2c2e2c38
FY
2004
2005 si_domain = alloc_domain();
2006 if (!si_domain)
2007 return -EFAULT;
2008
c7ab48d2 2009 pr_debug("Identity mapping domain is domain %d\n", si_domain->id);
2c2e2c38
FY
2010
2011 for_each_active_iommu(iommu, drhd) {
2012 ret = iommu_attach_domain(si_domain, iommu);
2013 if (ret) {
2014 domain_exit(si_domain);
2015 return -EFAULT;
2016 }
2017 }
2018
2019 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2020 domain_exit(si_domain);
2021 return -EFAULT;
2022 }
2023
2024 si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY;
2025
c7ab48d2
DW
2026 for_each_online_node(nid) {
2027 work_with_active_regions(nid, si_domain_work_fn, &ret);
2028 if (ret)
2029 return ret;
2030 }
2031
2c2e2c38
FY
2032 return 0;
2033}
2034
2035static void domain_remove_one_dev_info(struct dmar_domain *domain,
2036 struct pci_dev *pdev);
2037static int identity_mapping(struct pci_dev *pdev)
2038{
2039 struct device_domain_info *info;
2040
2041 if (likely(!iommu_identity_mapping))
2042 return 0;
2043
2044
2045 list_for_each_entry(info, &si_domain->devices, link)
2046 if (info->dev == pdev)
2047 return 1;
2048 return 0;
2049}
2050
2051static int domain_add_dev_info(struct dmar_domain *domain,
2052 struct pci_dev *pdev)
2053{
2054 struct device_domain_info *info;
2055 unsigned long flags;
2056
2057 info = alloc_devinfo_mem();
2058 if (!info)
2059 return -ENOMEM;
2060
2061 info->segment = pci_domain_nr(pdev->bus);
2062 info->bus = pdev->bus->number;
2063 info->devfn = pdev->devfn;
2064 info->dev = pdev;
2065 info->domain = domain;
2066
2067 spin_lock_irqsave(&device_domain_lock, flags);
2068 list_add(&info->link, &domain->devices);
2069 list_add(&info->global, &device_domain_list);
2070 pdev->dev.archdata.iommu = info;
2071 spin_unlock_irqrestore(&device_domain_lock, flags);
2072
2073 return 0;
2074}
2075
2076static int iommu_prepare_static_identity_mapping(void)
2077{
2c2e2c38
FY
2078 struct pci_dev *pdev = NULL;
2079 int ret;
2080
2081 ret = si_domain_init();
2082 if (ret)
2083 return -EFAULT;
2084
2c2e2c38 2085 for_each_pci_dev(pdev) {
c7ab48d2
DW
2086 printk(KERN_INFO "IOMMU: identity mapping for device %s\n",
2087 pci_name(pdev));
2088
2089 ret = domain_context_mapping(si_domain, pdev,
2090 CONTEXT_TT_MULTI_LEVEL);
2091 if (ret)
2092 return ret;
2c2e2c38
FY
2093 ret = domain_add_dev_info(si_domain, pdev);
2094 if (ret)
2095 return ret;
2096 }
2097
2098 return 0;
2099}
2100
2101int __init init_dmars(void)
ba395927
KA
2102{
2103 struct dmar_drhd_unit *drhd;
2104 struct dmar_rmrr_unit *rmrr;
2105 struct pci_dev *pdev;
2106 struct intel_iommu *iommu;
9d783ba0 2107 int i, ret;
4ed0d3e6 2108 int pass_through = 1;
ba395927 2109
2c2e2c38
FY
2110 /*
2111 * In case pass through can not be enabled, iommu tries to use identity
2112 * mapping.
2113 */
2114 if (iommu_pass_through)
2115 iommu_identity_mapping = 1;
2116
ba395927
KA
2117 /*
2118 * for each drhd
2119 * allocate root
2120 * initialize and program root entry to not present
2121 * endfor
2122 */
2123 for_each_drhd_unit(drhd) {
5e0d2a6f 2124 g_num_of_iommus++;
2125 /*
2126 * lock not needed as this is only incremented in the single
2127 * threaded kernel __init code path all other access are read
2128 * only
2129 */
2130 }
2131
d9630fe9
WH
2132 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2133 GFP_KERNEL);
2134 if (!g_iommus) {
2135 printk(KERN_ERR "Allocating global iommu array failed\n");
2136 ret = -ENOMEM;
2137 goto error;
2138 }
2139
80b20dd8 2140 deferred_flush = kzalloc(g_num_of_iommus *
2141 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2142 if (!deferred_flush) {
d9630fe9 2143 kfree(g_iommus);
5e0d2a6f 2144 ret = -ENOMEM;
2145 goto error;
2146 }
2147
5e0d2a6f 2148 for_each_drhd_unit(drhd) {
2149 if (drhd->ignored)
2150 continue;
1886e8a9
SS
2151
2152 iommu = drhd->iommu;
d9630fe9 2153 g_iommus[iommu->seq_id] = iommu;
ba395927 2154
e61d98d8
SS
2155 ret = iommu_init_domains(iommu);
2156 if (ret)
2157 goto error;
2158
ba395927
KA
2159 /*
2160 * TBD:
2161 * we could share the same root & context tables
2162 * amoung all IOMMU's. Need to Split it later.
2163 */
2164 ret = iommu_alloc_root_entry(iommu);
2165 if (ret) {
2166 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
2167 goto error;
2168 }
4ed0d3e6
FY
2169 if (!ecap_pass_through(iommu->ecap))
2170 pass_through = 0;
ba395927 2171 }
4ed0d3e6
FY
2172 if (iommu_pass_through)
2173 if (!pass_through) {
2174 printk(KERN_INFO
2175 "Pass Through is not supported by hardware.\n");
2176 iommu_pass_through = 0;
2177 }
ba395927 2178
1531a6a6
SS
2179 /*
2180 * Start from the sane iommu hardware state.
2181 */
a77b67d4
YS
2182 for_each_drhd_unit(drhd) {
2183 if (drhd->ignored)
2184 continue;
2185
2186 iommu = drhd->iommu;
1531a6a6
SS
2187
2188 /*
2189 * If the queued invalidation is already initialized by us
2190 * (for example, while enabling interrupt-remapping) then
2191 * we got the things already rolling from a sane state.
2192 */
2193 if (iommu->qi)
2194 continue;
2195
2196 /*
2197 * Clear any previous faults.
2198 */
2199 dmar_fault(-1, iommu);
2200 /*
2201 * Disable queued invalidation if supported and already enabled
2202 * before OS handover.
2203 */
2204 dmar_disable_qi(iommu);
2205 }
2206
2207 for_each_drhd_unit(drhd) {
2208 if (drhd->ignored)
2209 continue;
2210
2211 iommu = drhd->iommu;
2212
a77b67d4
YS
2213 if (dmar_enable_qi(iommu)) {
2214 /*
2215 * Queued Invalidate not enabled, use Register Based
2216 * Invalidate
2217 */
2218 iommu->flush.flush_context = __iommu_flush_context;
2219 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
2220 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
b4e0f9eb
FT
2221 "invalidation\n",
2222 (unsigned long long)drhd->reg_base_addr);
a77b67d4
YS
2223 } else {
2224 iommu->flush.flush_context = qi_flush_context;
2225 iommu->flush.flush_iotlb = qi_flush_iotlb;
2226 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
b4e0f9eb
FT
2227 "invalidation\n",
2228 (unsigned long long)drhd->reg_base_addr);
a77b67d4
YS
2229 }
2230 }
2231
ba395927 2232 /*
4ed0d3e6
FY
2233 * If pass through is set and enabled, context entries of all pci
2234 * devices are intialized by pass through translation type.
ba395927 2235 */
4ed0d3e6
FY
2236 if (iommu_pass_through) {
2237 ret = init_context_pass_through();
2238 if (ret) {
2239 printk(KERN_ERR "IOMMU: Pass through init failed.\n");
2240 iommu_pass_through = 0;
ba395927
KA
2241 }
2242 }
2243
ba395927 2244 /*
4ed0d3e6 2245 * If pass through is not set or not enabled, setup context entries for
2c2e2c38
FY
2246 * identity mappings for rmrr, gfx, and isa and may fall back to static
2247 * identity mapping if iommu_identity_mapping is set.
ba395927 2248 */
4ed0d3e6 2249 if (!iommu_pass_through) {
2c2e2c38
FY
2250 if (iommu_identity_mapping)
2251 iommu_prepare_static_identity_mapping();
4ed0d3e6
FY
2252 /*
2253 * For each rmrr
2254 * for each dev attached to rmrr
2255 * do
2256 * locate drhd for dev, alloc domain for dev
2257 * allocate free domain
2258 * allocate page table entries for rmrr
2259 * if context not allocated for bus
2260 * allocate and init context
2261 * set present in root table for this bus
2262 * init context with domain, translation etc
2263 * endfor
2264 * endfor
2265 */
2c2e2c38 2266 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
4ed0d3e6
FY
2267 for_each_rmrr_units(rmrr) {
2268 for (i = 0; i < rmrr->devices_cnt; i++) {
2269 pdev = rmrr->devices[i];
2270 /*
2271 * some BIOS lists non-exist devices in DMAR
2272 * table.
2273 */
2274 if (!pdev)
2275 continue;
2276 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2277 if (ret)
2278 printk(KERN_ERR
ba395927 2279 "IOMMU: mapping reserved region failed\n");
4ed0d3e6 2280 }
ba395927 2281 }
ba395927 2282
4ed0d3e6
FY
2283 iommu_prepare_isa();
2284 }
49a0429e 2285
ba395927
KA
2286 /*
2287 * for each drhd
2288 * enable fault log
2289 * global invalidate context cache
2290 * global invalidate iotlb
2291 * enable translation
2292 */
2293 for_each_drhd_unit(drhd) {
2294 if (drhd->ignored)
2295 continue;
2296 iommu = drhd->iommu;
ba395927
KA
2297
2298 iommu_flush_write_buffer(iommu);
2299
3460a6d9
KA
2300 ret = dmar_set_interrupt(iommu);
2301 if (ret)
2302 goto error;
2303
ba395927
KA
2304 iommu_set_root_entry(iommu);
2305
4c25a2c1 2306 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
1f0ef2aa 2307 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
f8bab735 2308 iommu_disable_protect_mem_regions(iommu);
2309
ba395927
KA
2310 ret = iommu_enable_translation(iommu);
2311 if (ret)
2312 goto error;
2313 }
2314
2315 return 0;
2316error:
2317 for_each_drhd_unit(drhd) {
2318 if (drhd->ignored)
2319 continue;
2320 iommu = drhd->iommu;
2321 free_iommu(iommu);
2322 }
d9630fe9 2323 kfree(g_iommus);
ba395927
KA
2324 return ret;
2325}
2326
2327static inline u64 aligned_size(u64 host_addr, size_t size)
2328{
2329 u64 addr;
5b6985ce
FY
2330 addr = (host_addr & (~PAGE_MASK)) + size;
2331 return PAGE_ALIGN(addr);
ba395927
KA
2332}
2333
2334struct iova *
f76aec76 2335iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
ba395927 2336{
ba395927
KA
2337 struct iova *piova;
2338
2339 /* Make sure it's in range */
ba395927 2340 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
f76aec76 2341 if (!size || (IOVA_START_ADDR + size > end))
ba395927
KA
2342 return NULL;
2343
2344 piova = alloc_iova(&domain->iovad,
5b6985ce 2345 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
ba395927
KA
2346 return piova;
2347}
2348
f76aec76
KA
2349static struct iova *
2350__intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
bb9e6d65 2351 size_t size, u64 dma_mask)
ba395927 2352{
ba395927 2353 struct pci_dev *pdev = to_pci_dev(dev);
ba395927 2354 struct iova *iova = NULL;
ba395927 2355
284901a9 2356 if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
bb9e6d65
FT
2357 iova = iommu_alloc_iova(domain, size, dma_mask);
2358 else {
ba395927
KA
2359 /*
2360 * First try to allocate an io virtual address in
284901a9 2361 * DMA_BIT_MASK(32) and if that fails then try allocating
3609801e 2362 * from higher range
ba395927 2363 */
284901a9 2364 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
ba395927 2365 if (!iova)
bb9e6d65 2366 iova = iommu_alloc_iova(domain, size, dma_mask);
ba395927
KA
2367 }
2368
2369 if (!iova) {
2370 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
f76aec76
KA
2371 return NULL;
2372 }
2373
2374 return iova;
2375}
2376
2377static struct dmar_domain *
2378get_valid_domain_for_dev(struct pci_dev *pdev)
2379{
2380 struct dmar_domain *domain;
2381 int ret;
2382
2383 domain = get_domain_for_dev(pdev,
2384 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2385 if (!domain) {
2386 printk(KERN_ERR
2387 "Allocating domain for %s failed", pci_name(pdev));
4fe05bbc 2388 return NULL;
ba395927
KA
2389 }
2390
2391 /* make sure context mapping is ok */
5331fe6f 2392 if (unlikely(!domain_context_mapped(pdev))) {
4ed0d3e6
FY
2393 ret = domain_context_mapping(domain, pdev,
2394 CONTEXT_TT_MULTI_LEVEL);
f76aec76
KA
2395 if (ret) {
2396 printk(KERN_ERR
2397 "Domain context map for %s failed",
2398 pci_name(pdev));
4fe05bbc 2399 return NULL;
f76aec76 2400 }
ba395927
KA
2401 }
2402
f76aec76
KA
2403 return domain;
2404}
2405
2c2e2c38
FY
2406static int iommu_dummy(struct pci_dev *pdev)
2407{
2408 return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
2409}
2410
2411/* Check if the pdev needs to go through non-identity map and unmap process.*/
2412static int iommu_no_mapping(struct pci_dev *pdev)
2413{
2414 int found;
2415
2416 if (!iommu_identity_mapping)
2417 return iommu_dummy(pdev);
2418
2419 found = identity_mapping(pdev);
2420 if (found) {
2421 if (pdev->dma_mask > DMA_BIT_MASK(32))
2422 return 1;
2423 else {
2424 /*
2425 * 32 bit DMA is removed from si_domain and fall back
2426 * to non-identity mapping.
2427 */
2428 domain_remove_one_dev_info(si_domain, pdev);
2429 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
2430 pci_name(pdev));
2431 return 0;
2432 }
2433 } else {
2434 /*
2435 * In case of a detached 64 bit DMA device from vm, the device
2436 * is put into si_domain for identity mapping.
2437 */
2438 if (pdev->dma_mask > DMA_BIT_MASK(32)) {
2439 int ret;
2440 ret = domain_add_dev_info(si_domain, pdev);
2441 if (!ret) {
2442 printk(KERN_INFO "64bit %s uses identity mapping\n",
2443 pci_name(pdev));
2444 return 1;
2445 }
2446 }
2447 }
2448
2449 return iommu_dummy(pdev);
2450}
2451
bb9e6d65
FT
2452static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2453 size_t size, int dir, u64 dma_mask)
f76aec76
KA
2454{
2455 struct pci_dev *pdev = to_pci_dev(hwdev);
f76aec76 2456 struct dmar_domain *domain;
5b6985ce 2457 phys_addr_t start_paddr;
f76aec76
KA
2458 struct iova *iova;
2459 int prot = 0;
6865f0d1 2460 int ret;
8c11e798 2461 struct intel_iommu *iommu;
f76aec76
KA
2462
2463 BUG_ON(dir == DMA_NONE);
2c2e2c38
FY
2464
2465 if (iommu_no_mapping(pdev))
6865f0d1 2466 return paddr;
f76aec76
KA
2467
2468 domain = get_valid_domain_for_dev(pdev);
2469 if (!domain)
2470 return 0;
2471
8c11e798 2472 iommu = domain_get_iommu(domain);
6865f0d1 2473 size = aligned_size((u64)paddr, size);
f76aec76 2474
bb9e6d65 2475 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
f76aec76
KA
2476 if (!iova)
2477 goto error;
2478
5b6985ce 2479 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
f76aec76 2480
ba395927
KA
2481 /*
2482 * Check if DMAR supports zero-length reads on write only
2483 * mappings..
2484 */
2485 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
8c11e798 2486 !cap_zlr(iommu->cap))
ba395927
KA
2487 prot |= DMA_PTE_READ;
2488 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2489 prot |= DMA_PTE_WRITE;
2490 /*
6865f0d1 2491 * paddr - (paddr + size) might be partial page, we should map the whole
ba395927 2492 * page. Note: if two part of one page are separately mapped, we
6865f0d1 2493 * might have two guest_addr mapping to the same host paddr, but this
ba395927
KA
2494 * is not a big problem
2495 */
6865f0d1 2496 ret = domain_page_mapping(domain, start_paddr,
fd18de50
DW
2497 ((u64)paddr) & PHYSICAL_PAGE_MASK,
2498 size, prot);
ba395927
KA
2499 if (ret)
2500 goto error;
2501
1f0ef2aa
DW
2502 /* it's a non-present to present mapping. Only flush if caching mode */
2503 if (cap_caching_mode(iommu->cap))
2504 iommu_flush_iotlb_psi(iommu, 0, start_paddr,
2505 size >> VTD_PAGE_SHIFT);
2506 else
8c11e798 2507 iommu_flush_write_buffer(iommu);
f76aec76 2508
5b6985ce 2509 return start_paddr + ((u64)paddr & (~PAGE_MASK));
ba395927 2510
ba395927 2511error:
f76aec76
KA
2512 if (iova)
2513 __free_iova(&domain->iovad, iova);
4cf2e75d 2514 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
5b6985ce 2515 pci_name(pdev), size, (unsigned long long)paddr, dir);
ba395927
KA
2516 return 0;
2517}
2518
ffbbef5c
FT
2519static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2520 unsigned long offset, size_t size,
2521 enum dma_data_direction dir,
2522 struct dma_attrs *attrs)
bb9e6d65 2523{
ffbbef5c
FT
2524 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2525 dir, to_pci_dev(dev)->dma_mask);
bb9e6d65
FT
2526}
2527
5e0d2a6f 2528static void flush_unmaps(void)
2529{
80b20dd8 2530 int i, j;
5e0d2a6f 2531
5e0d2a6f 2532 timer_on = 0;
2533
2534 /* just flush them all */
2535 for (i = 0; i < g_num_of_iommus; i++) {
a2bb8459
WH
2536 struct intel_iommu *iommu = g_iommus[i];
2537 if (!iommu)
2538 continue;
c42d9f32 2539
9dd2fe89
YZ
2540 if (!deferred_flush[i].next)
2541 continue;
2542
2543 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
93a23a72 2544 DMA_TLB_GLOBAL_FLUSH);
9dd2fe89 2545 for (j = 0; j < deferred_flush[i].next; j++) {
93a23a72
YZ
2546 unsigned long mask;
2547 struct iova *iova = deferred_flush[i].iova[j];
2548
2549 mask = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT;
2550 mask = ilog2(mask >> VTD_PAGE_SHIFT);
2551 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
2552 iova->pfn_lo << PAGE_SHIFT, mask);
2553 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
80b20dd8 2554 }
9dd2fe89 2555 deferred_flush[i].next = 0;
5e0d2a6f 2556 }
2557
5e0d2a6f 2558 list_size = 0;
5e0d2a6f 2559}
2560
2561static void flush_unmaps_timeout(unsigned long data)
2562{
80b20dd8 2563 unsigned long flags;
2564
2565 spin_lock_irqsave(&async_umap_flush_lock, flags);
5e0d2a6f 2566 flush_unmaps();
80b20dd8 2567 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
5e0d2a6f 2568}
2569
2570static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2571{
2572 unsigned long flags;
80b20dd8 2573 int next, iommu_id;
8c11e798 2574 struct intel_iommu *iommu;
5e0d2a6f 2575
2576 spin_lock_irqsave(&async_umap_flush_lock, flags);
80b20dd8 2577 if (list_size == HIGH_WATER_MARK)
2578 flush_unmaps();
2579
8c11e798
WH
2580 iommu = domain_get_iommu(dom);
2581 iommu_id = iommu->seq_id;
c42d9f32 2582
80b20dd8 2583 next = deferred_flush[iommu_id].next;
2584 deferred_flush[iommu_id].domain[next] = dom;
2585 deferred_flush[iommu_id].iova[next] = iova;
2586 deferred_flush[iommu_id].next++;
5e0d2a6f 2587
2588 if (!timer_on) {
2589 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2590 timer_on = 1;
2591 }
2592 list_size++;
2593 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2594}
2595
ffbbef5c
FT
2596static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2597 size_t size, enum dma_data_direction dir,
2598 struct dma_attrs *attrs)
ba395927 2599{
ba395927 2600 struct pci_dev *pdev = to_pci_dev(dev);
f76aec76
KA
2601 struct dmar_domain *domain;
2602 unsigned long start_addr;
ba395927 2603 struct iova *iova;
8c11e798 2604 struct intel_iommu *iommu;
ba395927 2605
2c2e2c38 2606 if (iommu_no_mapping(pdev))
f76aec76 2607 return;
2c2e2c38 2608
ba395927
KA
2609 domain = find_domain(pdev);
2610 BUG_ON(!domain);
2611
8c11e798
WH
2612 iommu = domain_get_iommu(domain);
2613
ba395927 2614 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
f76aec76 2615 if (!iova)
ba395927 2616 return;
ba395927 2617
5b6985ce 2618 start_addr = iova->pfn_lo << PAGE_SHIFT;
f76aec76 2619 size = aligned_size((u64)dev_addr, size);
ba395927 2620
4cf2e75d 2621 pr_debug("Device %s unmapping: %zx@%llx\n",
5b6985ce 2622 pci_name(pdev), size, (unsigned long long)start_addr);
ba395927 2623
f76aec76
KA
2624 /* clear the whole page */
2625 dma_pte_clear_range(domain, start_addr, start_addr + size);
2626 /* free page tables */
2627 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
5e0d2a6f 2628 if (intel_iommu_strict) {
1f0ef2aa
DW
2629 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2630 size >> VTD_PAGE_SHIFT);
5e0d2a6f 2631 /* free iova */
2632 __free_iova(&domain->iovad, iova);
2633 } else {
2634 add_unmap(domain, iova);
2635 /*
2636 * queue up the release of the unmap to save the 1/6th of the
2637 * cpu used up by the iotlb flush operation...
2638 */
5e0d2a6f 2639 }
ba395927
KA
2640}
2641
d7ab5c46
FT
2642static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2643 int dir)
ffbbef5c
FT
2644{
2645 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2646}
2647
d7ab5c46
FT
2648static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2649 dma_addr_t *dma_handle, gfp_t flags)
ba395927
KA
2650{
2651 void *vaddr;
2652 int order;
2653
5b6985ce 2654 size = PAGE_ALIGN(size);
ba395927
KA
2655 order = get_order(size);
2656 flags &= ~(GFP_DMA | GFP_DMA32);
2657
2658 vaddr = (void *)__get_free_pages(flags, order);
2659 if (!vaddr)
2660 return NULL;
2661 memset(vaddr, 0, size);
2662
bb9e6d65
FT
2663 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2664 DMA_BIDIRECTIONAL,
2665 hwdev->coherent_dma_mask);
ba395927
KA
2666 if (*dma_handle)
2667 return vaddr;
2668 free_pages((unsigned long)vaddr, order);
2669 return NULL;
2670}
2671
d7ab5c46
FT
2672static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2673 dma_addr_t dma_handle)
ba395927
KA
2674{
2675 int order;
2676
5b6985ce 2677 size = PAGE_ALIGN(size);
ba395927
KA
2678 order = get_order(size);
2679
2680 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2681 free_pages((unsigned long)vaddr, order);
2682}
2683
d7ab5c46
FT
2684static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2685 int nelems, enum dma_data_direction dir,
2686 struct dma_attrs *attrs)
ba395927
KA
2687{
2688 int i;
2689 struct pci_dev *pdev = to_pci_dev(hwdev);
2690 struct dmar_domain *domain;
f76aec76
KA
2691 unsigned long start_addr;
2692 struct iova *iova;
2693 size_t size = 0;
4cf2e75d 2694 phys_addr_t addr;
c03ab37c 2695 struct scatterlist *sg;
8c11e798 2696 struct intel_iommu *iommu;
ba395927 2697
2c2e2c38 2698 if (iommu_no_mapping(pdev))
ba395927
KA
2699 return;
2700
2701 domain = find_domain(pdev);
8c11e798
WH
2702 BUG_ON(!domain);
2703
2704 iommu = domain_get_iommu(domain);
ba395927 2705
c03ab37c 2706 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
f76aec76
KA
2707 if (!iova)
2708 return;
c03ab37c 2709 for_each_sg(sglist, sg, nelems, i) {
4cf2e75d 2710 addr = page_to_phys(sg_page(sg)) + sg->offset;
f76aec76
KA
2711 size += aligned_size((u64)addr, sg->length);
2712 }
2713
5b6985ce 2714 start_addr = iova->pfn_lo << PAGE_SHIFT;
f76aec76
KA
2715
2716 /* clear the whole page */
2717 dma_pte_clear_range(domain, start_addr, start_addr + size);
2718 /* free page tables */
2719 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2720
1f0ef2aa
DW
2721 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2722 size >> VTD_PAGE_SHIFT);
f76aec76
KA
2723
2724 /* free iova */
2725 __free_iova(&domain->iovad, iova);
ba395927
KA
2726}
2727
ba395927 2728static int intel_nontranslate_map_sg(struct device *hddev,
c03ab37c 2729 struct scatterlist *sglist, int nelems, int dir)
ba395927
KA
2730{
2731 int i;
c03ab37c 2732 struct scatterlist *sg;
ba395927 2733
c03ab37c 2734 for_each_sg(sglist, sg, nelems, i) {
12d4d40e 2735 BUG_ON(!sg_page(sg));
4cf2e75d 2736 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
c03ab37c 2737 sg->dma_length = sg->length;
ba395927
KA
2738 }
2739 return nelems;
2740}
2741
d7ab5c46
FT
2742static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2743 enum dma_data_direction dir, struct dma_attrs *attrs)
ba395927 2744{
4cf2e75d 2745 phys_addr_t addr;
ba395927 2746 int i;
ba395927
KA
2747 struct pci_dev *pdev = to_pci_dev(hwdev);
2748 struct dmar_domain *domain;
f76aec76
KA
2749 size_t size = 0;
2750 int prot = 0;
2751 size_t offset = 0;
2752 struct iova *iova = NULL;
2753 int ret;
c03ab37c 2754 struct scatterlist *sg;
f76aec76 2755 unsigned long start_addr;
8c11e798 2756 struct intel_iommu *iommu;
ba395927
KA
2757
2758 BUG_ON(dir == DMA_NONE);
2c2e2c38 2759 if (iommu_no_mapping(pdev))
c03ab37c 2760 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
ba395927 2761
f76aec76
KA
2762 domain = get_valid_domain_for_dev(pdev);
2763 if (!domain)
2764 return 0;
2765
8c11e798
WH
2766 iommu = domain_get_iommu(domain);
2767
c03ab37c 2768 for_each_sg(sglist, sg, nelems, i) {
4cf2e75d 2769 addr = page_to_phys(sg_page(sg)) + sg->offset;
f76aec76
KA
2770 size += aligned_size((u64)addr, sg->length);
2771 }
2772
bb9e6d65 2773 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
f76aec76 2774 if (!iova) {
c03ab37c 2775 sglist->dma_length = 0;
f76aec76
KA
2776 return 0;
2777 }
2778
2779 /*
2780 * Check if DMAR supports zero-length reads on write only
2781 * mappings..
2782 */
2783 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
8c11e798 2784 !cap_zlr(iommu->cap))
f76aec76
KA
2785 prot |= DMA_PTE_READ;
2786 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2787 prot |= DMA_PTE_WRITE;
2788
5b6985ce 2789 start_addr = iova->pfn_lo << PAGE_SHIFT;
f76aec76 2790 offset = 0;
c03ab37c 2791 for_each_sg(sglist, sg, nelems, i) {
4cf2e75d 2792 addr = page_to_phys(sg_page(sg)) + sg->offset;
f76aec76
KA
2793 size = aligned_size((u64)addr, sg->length);
2794 ret = domain_page_mapping(domain, start_addr + offset,
fd18de50
DW
2795 ((u64)addr) & PHYSICAL_PAGE_MASK,
2796 size, prot);
f76aec76
KA
2797 if (ret) {
2798 /* clear the page */
2799 dma_pte_clear_range(domain, start_addr,
2800 start_addr + offset);
2801 /* free page tables */
2802 dma_pte_free_pagetable(domain, start_addr,
2803 start_addr + offset);
2804 /* free iova */
2805 __free_iova(&domain->iovad, iova);
ba395927
KA
2806 return 0;
2807 }
f76aec76 2808 sg->dma_address = start_addr + offset +
5b6985ce 2809 ((u64)addr & (~PAGE_MASK));
ba395927 2810 sg->dma_length = sg->length;
f76aec76 2811 offset += size;
ba395927
KA
2812 }
2813
1f0ef2aa
DW
2814 /* it's a non-present to present mapping. Only flush if caching mode */
2815 if (cap_caching_mode(iommu->cap))
2816 iommu_flush_iotlb_psi(iommu, 0, start_addr,
2817 offset >> VTD_PAGE_SHIFT);
2818 else
8c11e798 2819 iommu_flush_write_buffer(iommu);
1f0ef2aa 2820
ba395927
KA
2821 return nelems;
2822}
2823
dfb805e8
FT
2824static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2825{
2826 return !dma_addr;
2827}
2828
160c1d8e 2829struct dma_map_ops intel_dma_ops = {
ba395927
KA
2830 .alloc_coherent = intel_alloc_coherent,
2831 .free_coherent = intel_free_coherent,
ba395927
KA
2832 .map_sg = intel_map_sg,
2833 .unmap_sg = intel_unmap_sg,
ffbbef5c
FT
2834 .map_page = intel_map_page,
2835 .unmap_page = intel_unmap_page,
dfb805e8 2836 .mapping_error = intel_mapping_error,
ba395927
KA
2837};
2838
2839static inline int iommu_domain_cache_init(void)
2840{
2841 int ret = 0;
2842
2843 iommu_domain_cache = kmem_cache_create("iommu_domain",
2844 sizeof(struct dmar_domain),
2845 0,
2846 SLAB_HWCACHE_ALIGN,
2847
2848 NULL);
2849 if (!iommu_domain_cache) {
2850 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2851 ret = -ENOMEM;
2852 }
2853
2854 return ret;
2855}
2856
2857static inline int iommu_devinfo_cache_init(void)
2858{
2859 int ret = 0;
2860
2861 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2862 sizeof(struct device_domain_info),
2863 0,
2864 SLAB_HWCACHE_ALIGN,
ba395927
KA
2865 NULL);
2866 if (!iommu_devinfo_cache) {
2867 printk(KERN_ERR "Couldn't create devinfo cache\n");
2868 ret = -ENOMEM;
2869 }
2870
2871 return ret;
2872}
2873
2874static inline int iommu_iova_cache_init(void)
2875{
2876 int ret = 0;
2877
2878 iommu_iova_cache = kmem_cache_create("iommu_iova",
2879 sizeof(struct iova),
2880 0,
2881 SLAB_HWCACHE_ALIGN,
ba395927
KA
2882 NULL);
2883 if (!iommu_iova_cache) {
2884 printk(KERN_ERR "Couldn't create iova cache\n");
2885 ret = -ENOMEM;
2886 }
2887
2888 return ret;
2889}
2890
2891static int __init iommu_init_mempool(void)
2892{
2893 int ret;
2894 ret = iommu_iova_cache_init();
2895 if (ret)
2896 return ret;
2897
2898 ret = iommu_domain_cache_init();
2899 if (ret)
2900 goto domain_error;
2901
2902 ret = iommu_devinfo_cache_init();
2903 if (!ret)
2904 return ret;
2905
2906 kmem_cache_destroy(iommu_domain_cache);
2907domain_error:
2908 kmem_cache_destroy(iommu_iova_cache);
2909
2910 return -ENOMEM;
2911}
2912
2913static void __init iommu_exit_mempool(void)
2914{
2915 kmem_cache_destroy(iommu_devinfo_cache);
2916 kmem_cache_destroy(iommu_domain_cache);
2917 kmem_cache_destroy(iommu_iova_cache);
2918
2919}
2920
ba395927
KA
2921static void __init init_no_remapping_devices(void)
2922{
2923 struct dmar_drhd_unit *drhd;
2924
2925 for_each_drhd_unit(drhd) {
2926 if (!drhd->include_all) {
2927 int i;
2928 for (i = 0; i < drhd->devices_cnt; i++)
2929 if (drhd->devices[i] != NULL)
2930 break;
2931 /* ignore DMAR unit if no pci devices exist */
2932 if (i == drhd->devices_cnt)
2933 drhd->ignored = 1;
2934 }
2935 }
2936
2937 if (dmar_map_gfx)
2938 return;
2939
2940 for_each_drhd_unit(drhd) {
2941 int i;
2942 if (drhd->ignored || drhd->include_all)
2943 continue;
2944
2945 for (i = 0; i < drhd->devices_cnt; i++)
2946 if (drhd->devices[i] &&
2947 !IS_GFX_DEVICE(drhd->devices[i]))
2948 break;
2949
2950 if (i < drhd->devices_cnt)
2951 continue;
2952
2953 /* bypass IOMMU if it is just for gfx devices */
2954 drhd->ignored = 1;
2955 for (i = 0; i < drhd->devices_cnt; i++) {
2956 if (!drhd->devices[i])
2957 continue;
358dd8ac 2958 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
ba395927
KA
2959 }
2960 }
2961}
2962
f59c7b69
FY
2963#ifdef CONFIG_SUSPEND
2964static int init_iommu_hw(void)
2965{
2966 struct dmar_drhd_unit *drhd;
2967 struct intel_iommu *iommu = NULL;
2968
2969 for_each_active_iommu(iommu, drhd)
2970 if (iommu->qi)
2971 dmar_reenable_qi(iommu);
2972
2973 for_each_active_iommu(iommu, drhd) {
2974 iommu_flush_write_buffer(iommu);
2975
2976 iommu_set_root_entry(iommu);
2977
2978 iommu->flush.flush_context(iommu, 0, 0, 0,
1f0ef2aa 2979 DMA_CCMD_GLOBAL_INVL);
f59c7b69 2980 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
1f0ef2aa 2981 DMA_TLB_GLOBAL_FLUSH);
f59c7b69
FY
2982 iommu_disable_protect_mem_regions(iommu);
2983 iommu_enable_translation(iommu);
2984 }
2985
2986 return 0;
2987}
2988
2989static void iommu_flush_all(void)
2990{
2991 struct dmar_drhd_unit *drhd;
2992 struct intel_iommu *iommu;
2993
2994 for_each_active_iommu(iommu, drhd) {
2995 iommu->flush.flush_context(iommu, 0, 0, 0,
1f0ef2aa 2996 DMA_CCMD_GLOBAL_INVL);
f59c7b69 2997 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
1f0ef2aa 2998 DMA_TLB_GLOBAL_FLUSH);
f59c7b69
FY
2999 }
3000}
3001
3002static int iommu_suspend(struct sys_device *dev, pm_message_t state)
3003{
3004 struct dmar_drhd_unit *drhd;
3005 struct intel_iommu *iommu = NULL;
3006 unsigned long flag;
3007
3008 for_each_active_iommu(iommu, drhd) {
3009 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3010 GFP_ATOMIC);
3011 if (!iommu->iommu_state)
3012 goto nomem;
3013 }
3014
3015 iommu_flush_all();
3016
3017 for_each_active_iommu(iommu, drhd) {
3018 iommu_disable_translation(iommu);
3019
3020 spin_lock_irqsave(&iommu->register_lock, flag);
3021
3022 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3023 readl(iommu->reg + DMAR_FECTL_REG);
3024 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3025 readl(iommu->reg + DMAR_FEDATA_REG);
3026 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3027 readl(iommu->reg + DMAR_FEADDR_REG);
3028 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3029 readl(iommu->reg + DMAR_FEUADDR_REG);
3030
3031 spin_unlock_irqrestore(&iommu->register_lock, flag);
3032 }
3033 return 0;
3034
3035nomem:
3036 for_each_active_iommu(iommu, drhd)
3037 kfree(iommu->iommu_state);
3038
3039 return -ENOMEM;
3040}
3041
3042static int iommu_resume(struct sys_device *dev)
3043{
3044 struct dmar_drhd_unit *drhd;
3045 struct intel_iommu *iommu = NULL;
3046 unsigned long flag;
3047
3048 if (init_iommu_hw()) {
3049 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
3050 return -EIO;
3051 }
3052
3053 for_each_active_iommu(iommu, drhd) {
3054
3055 spin_lock_irqsave(&iommu->register_lock, flag);
3056
3057 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3058 iommu->reg + DMAR_FECTL_REG);
3059 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3060 iommu->reg + DMAR_FEDATA_REG);
3061 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3062 iommu->reg + DMAR_FEADDR_REG);
3063 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3064 iommu->reg + DMAR_FEUADDR_REG);
3065
3066 spin_unlock_irqrestore(&iommu->register_lock, flag);
3067 }
3068
3069 for_each_active_iommu(iommu, drhd)
3070 kfree(iommu->iommu_state);
3071
3072 return 0;
3073}
3074
3075static struct sysdev_class iommu_sysclass = {
3076 .name = "iommu",
3077 .resume = iommu_resume,
3078 .suspend = iommu_suspend,
3079};
3080
3081static struct sys_device device_iommu = {
3082 .cls = &iommu_sysclass,
3083};
3084
3085static int __init init_iommu_sysfs(void)
3086{
3087 int error;
3088
3089 error = sysdev_class_register(&iommu_sysclass);
3090 if (error)
3091 return error;
3092
3093 error = sysdev_register(&device_iommu);
3094 if (error)
3095 sysdev_class_unregister(&iommu_sysclass);
3096
3097 return error;
3098}
3099
3100#else
3101static int __init init_iommu_sysfs(void)
3102{
3103 return 0;
3104}
3105#endif /* CONFIG_PM */
3106
ba395927
KA
3107int __init intel_iommu_init(void)
3108{
3109 int ret = 0;
3110
ba395927
KA
3111 if (dmar_table_init())
3112 return -ENODEV;
3113
1886e8a9
SS
3114 if (dmar_dev_scope_init())
3115 return -ENODEV;
3116
2ae21010
SS
3117 /*
3118 * Check the need for DMA-remapping initialization now.
3119 * Above initialization will also be used by Interrupt-remapping.
3120 */
4ed0d3e6 3121 if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled)
2ae21010
SS
3122 return -ENODEV;
3123
ba395927
KA
3124 iommu_init_mempool();
3125 dmar_init_reserved_ranges();
3126
3127 init_no_remapping_devices();
3128
3129 ret = init_dmars();
3130 if (ret) {
3131 printk(KERN_ERR "IOMMU: dmar init failed\n");
3132 put_iova_domain(&reserved_iova_list);
3133 iommu_exit_mempool();
3134 return ret;
3135 }
3136 printk(KERN_INFO
3137 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
3138
5e0d2a6f 3139 init_timer(&unmap_timer);
ba395927 3140 force_iommu = 1;
4ed0d3e6
FY
3141
3142 if (!iommu_pass_through) {
3143 printk(KERN_INFO
3144 "Multi-level page-table translation for DMAR.\n");
3145 dma_ops = &intel_dma_ops;
3146 } else
3147 printk(KERN_INFO
3148 "DMAR: Pass through translation for DMAR.\n");
3149
f59c7b69 3150 init_iommu_sysfs();
a8bcbb0d
JR
3151
3152 register_iommu(&intel_iommu_ops);
3153
ba395927
KA
3154 return 0;
3155}
e820482c 3156
3199aa6b
HW
3157static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
3158 struct pci_dev *pdev)
3159{
3160 struct pci_dev *tmp, *parent;
3161
3162 if (!iommu || !pdev)
3163 return;
3164
3165 /* dependent device detach */
3166 tmp = pci_find_upstream_pcie_bridge(pdev);
3167 /* Secondary interface's bus number and devfn 0 */
3168 if (tmp) {
3169 parent = pdev->bus->self;
3170 while (parent != tmp) {
3171 iommu_detach_dev(iommu, parent->bus->number,
276dbf99 3172 parent->devfn);
3199aa6b
HW
3173 parent = parent->bus->self;
3174 }
3175 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
3176 iommu_detach_dev(iommu,
3177 tmp->subordinate->number, 0);
3178 else /* this is a legacy PCI bridge */
276dbf99
DW
3179 iommu_detach_dev(iommu, tmp->bus->number,
3180 tmp->devfn);
3199aa6b
HW
3181 }
3182}
3183
2c2e2c38 3184static void domain_remove_one_dev_info(struct dmar_domain *domain,
c7151a8d
WH
3185 struct pci_dev *pdev)
3186{
3187 struct device_domain_info *info;
3188 struct intel_iommu *iommu;
3189 unsigned long flags;
3190 int found = 0;
3191 struct list_head *entry, *tmp;
3192
276dbf99
DW
3193 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3194 pdev->devfn);
c7151a8d
WH
3195 if (!iommu)
3196 return;
3197
3198 spin_lock_irqsave(&device_domain_lock, flags);
3199 list_for_each_safe(entry, tmp, &domain->devices) {
3200 info = list_entry(entry, struct device_domain_info, link);
276dbf99 3201 /* No need to compare PCI domain; it has to be the same */
c7151a8d
WH
3202 if (info->bus == pdev->bus->number &&
3203 info->devfn == pdev->devfn) {
3204 list_del(&info->link);
3205 list_del(&info->global);
3206 if (info->dev)
3207 info->dev->dev.archdata.iommu = NULL;
3208 spin_unlock_irqrestore(&device_domain_lock, flags);
3209
93a23a72 3210 iommu_disable_dev_iotlb(info);
c7151a8d 3211 iommu_detach_dev(iommu, info->bus, info->devfn);
3199aa6b 3212 iommu_detach_dependent_devices(iommu, pdev);
c7151a8d
WH
3213 free_devinfo_mem(info);
3214
3215 spin_lock_irqsave(&device_domain_lock, flags);
3216
3217 if (found)
3218 break;
3219 else
3220 continue;
3221 }
3222
3223 /* if there is no other devices under the same iommu
3224 * owned by this domain, clear this iommu in iommu_bmp
3225 * update iommu count and coherency
3226 */
276dbf99
DW
3227 if (iommu == device_to_iommu(info->segment, info->bus,
3228 info->devfn))
c7151a8d
WH
3229 found = 1;
3230 }
3231
3232 if (found == 0) {
3233 unsigned long tmp_flags;
3234 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3235 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3236 domain->iommu_count--;
58c610bd 3237 domain_update_iommu_cap(domain);
c7151a8d
WH
3238 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
3239 }
3240
3241 spin_unlock_irqrestore(&device_domain_lock, flags);
3242}
3243
3244static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3245{
3246 struct device_domain_info *info;
3247 struct intel_iommu *iommu;
3248 unsigned long flags1, flags2;
3249
3250 spin_lock_irqsave(&device_domain_lock, flags1);
3251 while (!list_empty(&domain->devices)) {
3252 info = list_entry(domain->devices.next,
3253 struct device_domain_info, link);
3254 list_del(&info->link);
3255 list_del(&info->global);
3256 if (info->dev)
3257 info->dev->dev.archdata.iommu = NULL;
3258
3259 spin_unlock_irqrestore(&device_domain_lock, flags1);
3260
93a23a72 3261 iommu_disable_dev_iotlb(info);
276dbf99 3262 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
c7151a8d 3263 iommu_detach_dev(iommu, info->bus, info->devfn);
3199aa6b 3264 iommu_detach_dependent_devices(iommu, info->dev);
c7151a8d
WH
3265
3266 /* clear this iommu in iommu_bmp, update iommu count
58c610bd 3267 * and capabilities
c7151a8d
WH
3268 */
3269 spin_lock_irqsave(&domain->iommu_lock, flags2);
3270 if (test_and_clear_bit(iommu->seq_id,
3271 &domain->iommu_bmp)) {
3272 domain->iommu_count--;
58c610bd 3273 domain_update_iommu_cap(domain);
c7151a8d
WH
3274 }
3275 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3276
3277 free_devinfo_mem(info);
3278 spin_lock_irqsave(&device_domain_lock, flags1);
3279 }
3280 spin_unlock_irqrestore(&device_domain_lock, flags1);
3281}
3282
5e98c4b1
WH
3283/* domain id for virtual machine, it won't be set in context */
3284static unsigned long vm_domid;
3285
fe40f1e0
WH
3286static int vm_domain_min_agaw(struct dmar_domain *domain)
3287{
3288 int i;
3289 int min_agaw = domain->agaw;
3290
3291 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
3292 for (; i < g_num_of_iommus; ) {
3293 if (min_agaw > g_iommus[i]->agaw)
3294 min_agaw = g_iommus[i]->agaw;
3295
3296 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
3297 }
3298
3299 return min_agaw;
3300}
3301
5e98c4b1
WH
3302static struct dmar_domain *iommu_alloc_vm_domain(void)
3303{
3304 struct dmar_domain *domain;
3305
3306 domain = alloc_domain_mem();
3307 if (!domain)
3308 return NULL;
3309
3310 domain->id = vm_domid++;
3311 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3312 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3313
3314 return domain;
3315}
3316
2c2e2c38 3317static int md_domain_init(struct dmar_domain *domain, int guest_width)
5e98c4b1
WH
3318{
3319 int adjust_width;
3320
3321 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3322 spin_lock_init(&domain->mapping_lock);
3323 spin_lock_init(&domain->iommu_lock);
3324
3325 domain_reserve_special_ranges(domain);
3326
3327 /* calculate AGAW */
3328 domain->gaw = guest_width;
3329 adjust_width = guestwidth_to_adjustwidth(guest_width);
3330 domain->agaw = width_to_agaw(adjust_width);
3331
3332 INIT_LIST_HEAD(&domain->devices);
3333
3334 domain->iommu_count = 0;
3335 domain->iommu_coherency = 0;
fe40f1e0 3336 domain->max_addr = 0;
5e98c4b1
WH
3337
3338 /* always allocate the top pgd */
3339 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3340 if (!domain->pgd)
3341 return -ENOMEM;
3342 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3343 return 0;
3344}
3345
3346static void iommu_free_vm_domain(struct dmar_domain *domain)
3347{
3348 unsigned long flags;
3349 struct dmar_drhd_unit *drhd;
3350 struct intel_iommu *iommu;
3351 unsigned long i;
3352 unsigned long ndomains;
3353
3354 for_each_drhd_unit(drhd) {
3355 if (drhd->ignored)
3356 continue;
3357 iommu = drhd->iommu;
3358
3359 ndomains = cap_ndoms(iommu->cap);
3360 i = find_first_bit(iommu->domain_ids, ndomains);
3361 for (; i < ndomains; ) {
3362 if (iommu->domains[i] == domain) {
3363 spin_lock_irqsave(&iommu->lock, flags);
3364 clear_bit(i, iommu->domain_ids);
3365 iommu->domains[i] = NULL;
3366 spin_unlock_irqrestore(&iommu->lock, flags);
3367 break;
3368 }
3369 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3370 }
3371 }
3372}
3373
3374static void vm_domain_exit(struct dmar_domain *domain)
3375{
3376 u64 end;
3377
3378 /* Domain 0 is reserved, so dont process it */
3379 if (!domain)
3380 return;
3381
3382 vm_domain_remove_all_dev_info(domain);
3383 /* destroy iovas */
3384 put_iova_domain(&domain->iovad);
3385 end = DOMAIN_MAX_ADDR(domain->gaw);
3386 end = end & (~VTD_PAGE_MASK);
3387
3388 /* clear ptes */
3389 dma_pte_clear_range(domain, 0, end);
3390
3391 /* free page tables */
3392 dma_pte_free_pagetable(domain, 0, end);
3393
3394 iommu_free_vm_domain(domain);
3395 free_domain_mem(domain);
3396}
3397
5d450806 3398static int intel_iommu_domain_init(struct iommu_domain *domain)
38717946 3399{
5d450806 3400 struct dmar_domain *dmar_domain;
38717946 3401
5d450806
JR
3402 dmar_domain = iommu_alloc_vm_domain();
3403 if (!dmar_domain) {
38717946 3404 printk(KERN_ERR
5d450806
JR
3405 "intel_iommu_domain_init: dmar_domain == NULL\n");
3406 return -ENOMEM;
38717946 3407 }
2c2e2c38 3408 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
38717946 3409 printk(KERN_ERR
5d450806
JR
3410 "intel_iommu_domain_init() failed\n");
3411 vm_domain_exit(dmar_domain);
3412 return -ENOMEM;
38717946 3413 }
5d450806 3414 domain->priv = dmar_domain;
faa3d6f5 3415
5d450806 3416 return 0;
38717946 3417}
38717946 3418
5d450806 3419static void intel_iommu_domain_destroy(struct iommu_domain *domain)
38717946 3420{
5d450806
JR
3421 struct dmar_domain *dmar_domain = domain->priv;
3422
3423 domain->priv = NULL;
3424 vm_domain_exit(dmar_domain);
38717946 3425}
38717946 3426
4c5478c9
JR
3427static int intel_iommu_attach_device(struct iommu_domain *domain,
3428 struct device *dev)
38717946 3429{
4c5478c9
JR
3430 struct dmar_domain *dmar_domain = domain->priv;
3431 struct pci_dev *pdev = to_pci_dev(dev);
fe40f1e0
WH
3432 struct intel_iommu *iommu;
3433 int addr_width;
3434 u64 end;
faa3d6f5
WH
3435 int ret;
3436
3437 /* normally pdev is not mapped */
3438 if (unlikely(domain_context_mapped(pdev))) {
3439 struct dmar_domain *old_domain;
3440
3441 old_domain = find_domain(pdev);
3442 if (old_domain) {
2c2e2c38
FY
3443 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
3444 dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)
3445 domain_remove_one_dev_info(old_domain, pdev);
faa3d6f5
WH
3446 else
3447 domain_remove_dev_info(old_domain);
3448 }
3449 }
3450
276dbf99
DW
3451 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3452 pdev->devfn);
fe40f1e0
WH
3453 if (!iommu)
3454 return -ENODEV;
3455
3456 /* check if this iommu agaw is sufficient for max mapped address */
3457 addr_width = agaw_to_width(iommu->agaw);
3458 end = DOMAIN_MAX_ADDR(addr_width);
3459 end = end & VTD_PAGE_MASK;
4c5478c9 3460 if (end < dmar_domain->max_addr) {
fe40f1e0
WH
3461 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3462 "sufficient for the mapped address (%llx)\n",
4c5478c9 3463 __func__, iommu->agaw, dmar_domain->max_addr);
fe40f1e0
WH
3464 return -EFAULT;
3465 }
3466
2c2e2c38 3467 ret = domain_add_dev_info(dmar_domain, pdev);
faa3d6f5
WH
3468 if (ret)
3469 return ret;
3470
93a23a72 3471 ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
faa3d6f5 3472 return ret;
38717946 3473}
38717946 3474
4c5478c9
JR
3475static void intel_iommu_detach_device(struct iommu_domain *domain,
3476 struct device *dev)
38717946 3477{
4c5478c9
JR
3478 struct dmar_domain *dmar_domain = domain->priv;
3479 struct pci_dev *pdev = to_pci_dev(dev);
3480
2c2e2c38 3481 domain_remove_one_dev_info(dmar_domain, pdev);
faa3d6f5 3482}
c7151a8d 3483
dde57a21
JR
3484static int intel_iommu_map_range(struct iommu_domain *domain,
3485 unsigned long iova, phys_addr_t hpa,
3486 size_t size, int iommu_prot)
faa3d6f5 3487{
dde57a21 3488 struct dmar_domain *dmar_domain = domain->priv;
fe40f1e0
WH
3489 u64 max_addr;
3490 int addr_width;
dde57a21 3491 int prot = 0;
faa3d6f5 3492 int ret;
fe40f1e0 3493
dde57a21
JR
3494 if (iommu_prot & IOMMU_READ)
3495 prot |= DMA_PTE_READ;
3496 if (iommu_prot & IOMMU_WRITE)
3497 prot |= DMA_PTE_WRITE;
9cf06697
SY
3498 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3499 prot |= DMA_PTE_SNP;
dde57a21 3500
fe40f1e0 3501 max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
dde57a21 3502 if (dmar_domain->max_addr < max_addr) {
fe40f1e0
WH
3503 int min_agaw;
3504 u64 end;
3505
3506 /* check if minimum agaw is sufficient for mapped address */
dde57a21 3507 min_agaw = vm_domain_min_agaw(dmar_domain);
fe40f1e0
WH
3508 addr_width = agaw_to_width(min_agaw);
3509 end = DOMAIN_MAX_ADDR(addr_width);
3510 end = end & VTD_PAGE_MASK;
3511 if (end < max_addr) {
3512 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3513 "sufficient for the mapped address (%llx)\n",
3514 __func__, min_agaw, max_addr);
3515 return -EFAULT;
3516 }
dde57a21 3517 dmar_domain->max_addr = max_addr;
fe40f1e0
WH
3518 }
3519
dde57a21 3520 ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
faa3d6f5 3521 return ret;
38717946 3522}
38717946 3523
dde57a21
JR
3524static void intel_iommu_unmap_range(struct iommu_domain *domain,
3525 unsigned long iova, size_t size)
38717946 3526{
dde57a21 3527 struct dmar_domain *dmar_domain = domain->priv;
faa3d6f5
WH
3528 dma_addr_t base;
3529
3530 /* The address might not be aligned */
3531 base = iova & VTD_PAGE_MASK;
3532 size = VTD_PAGE_ALIGN(size);
dde57a21 3533 dma_pte_clear_range(dmar_domain, base, base + size);
fe40f1e0 3534
dde57a21
JR
3535 if (dmar_domain->max_addr == base + size)
3536 dmar_domain->max_addr = base;
38717946 3537}
38717946 3538
d14d6577
JR
3539static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3540 unsigned long iova)
38717946 3541{
d14d6577 3542 struct dmar_domain *dmar_domain = domain->priv;
38717946 3543 struct dma_pte *pte;
faa3d6f5 3544 u64 phys = 0;
38717946 3545
d14d6577 3546 pte = addr_to_dma_pte(dmar_domain, iova);
38717946 3547 if (pte)
faa3d6f5 3548 phys = dma_pte_addr(pte);
38717946 3549
faa3d6f5 3550 return phys;
38717946 3551}
a8bcbb0d 3552
dbb9fd86
SY
3553static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3554 unsigned long cap)
3555{
3556 struct dmar_domain *dmar_domain = domain->priv;
3557
3558 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3559 return dmar_domain->iommu_snooping;
3560
3561 return 0;
3562}
3563
a8bcbb0d
JR
3564static struct iommu_ops intel_iommu_ops = {
3565 .domain_init = intel_iommu_domain_init,
3566 .domain_destroy = intel_iommu_domain_destroy,
3567 .attach_dev = intel_iommu_attach_device,
3568 .detach_dev = intel_iommu_detach_device,
3569 .map = intel_iommu_map_range,
3570 .unmap = intel_iommu_unmap_range,
3571 .iova_to_phys = intel_iommu_iova_to_phys,
dbb9fd86 3572 .domain_has_cap = intel_iommu_domain_has_cap,
a8bcbb0d 3573};
9af88143
DW
3574
3575static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3576{
3577 /*
3578 * Mobile 4 Series Chipset neglects to set RWBF capability,
3579 * but needs it:
3580 */
3581 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3582 rwbf_quirk = 1;
3583}
3584
3585DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);