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