blob: a563fbe559d0505b90d1f736beb3a8e8bf197717 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
2 * Copyright (c) 2006, Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
mark gross98bcef52008-02-23 15:23:35 -080017 * Copyright (C) 2006-2008 Intel Corporation
18 * Author: Ashok Raj <ashok.raj@intel.com>
19 * Author: Shaohua Li <shaohua.li@intel.com>
20 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Fenghua Yu5b6985c2008-10-16 18:02:32 -070021 * Author: Fenghua Yu <fenghua.yu@intel.com>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070022 */
23
24#include <linux/init.h>
25#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080026#include <linux/debugfs.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
mark gross5e0d2a62008-03-04 15:22:08 -080035#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030036#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010037#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030038#include <linux/intel-iommu.h>
Fenghua Yuf59c7b62009-03-27 14:22:42 -070039#include <linux/sysdev.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070040#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090041#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070042#include "pci.h"
43
Fenghua Yu5b6985c2008-10-16 18:02:32 -070044#define ROOT_SIZE VTD_PAGE_SIZE
45#define CONTEXT_SIZE VTD_PAGE_SIZE
46
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070047#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
48#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
49
50#define IOAPIC_RANGE_START (0xfee00000)
51#define IOAPIC_RANGE_END (0xfeefffff)
52#define IOVA_START_ADDR (0x1000)
53
54#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
55
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070056#define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
57
Mark McLoughlinf27be032008-11-20 15:49:43 +000058#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070059#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070060#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080061
David Woodhousefd18de52009-05-10 23:57:41 +010062#ifndef PHYSICAL_PAGE_MASK
63#define PHYSICAL_PAGE_MASK PAGE_MASK
64#endif
65
Weidong Hand9630fe2008-12-08 11:06:32 +080066/* global iommu list, set NULL for ignored DMAR units */
67static struct intel_iommu **g_iommus;
68
David Woodhouse9af88142009-02-13 23:18:03 +000069static int rwbf_quirk;
70
Mark McLoughlin46b08e12008-11-20 15:49:44 +000071/*
72 * 0: Present
73 * 1-11: Reserved
74 * 12-63: Context Ptr (12 - (haw-1))
75 * 64-127: Reserved
76 */
77struct root_entry {
78 u64 val;
79 u64 rsvd1;
80};
81#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
82static inline bool root_present(struct root_entry *root)
83{
84 return (root->val & 1);
85}
86static inline void set_root_present(struct root_entry *root)
87{
88 root->val |= 1;
89}
90static inline void set_root_value(struct root_entry *root, unsigned long value)
91{
92 root->val |= value & VTD_PAGE_MASK;
93}
94
95static inline struct context_entry *
96get_context_addr_from_root(struct root_entry *root)
97{
98 return (struct context_entry *)
99 (root_present(root)?phys_to_virt(
100 root->val & VTD_PAGE_MASK) :
101 NULL);
102}
103
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000104/*
105 * low 64 bits:
106 * 0: present
107 * 1: fault processing disable
108 * 2-3: translation type
109 * 12-63: address space root
110 * high 64 bits:
111 * 0-2: address width
112 * 3-6: aval
113 * 8-23: domain id
114 */
115struct context_entry {
116 u64 lo;
117 u64 hi;
118};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000119
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000120static inline bool context_present(struct context_entry *context)
121{
122 return (context->lo & 1);
123}
124static inline void context_set_present(struct context_entry *context)
125{
126 context->lo |= 1;
127}
128
129static inline void context_set_fault_enable(struct context_entry *context)
130{
131 context->lo &= (((u64)-1) << 2) | 1;
132}
133
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000134#define CONTEXT_TT_MULTI_LEVEL 0
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000135
136static inline void context_set_translation_type(struct context_entry *context,
137 unsigned long value)
138{
139 context->lo &= (((u64)-1) << 4) | 3;
140 context->lo |= (value & 3) << 2;
141}
142
143static inline void context_set_address_root(struct context_entry *context,
144 unsigned long value)
145{
146 context->lo |= value & VTD_PAGE_MASK;
147}
148
149static inline void context_set_address_width(struct context_entry *context,
150 unsigned long value)
151{
152 context->hi |= value & 7;
153}
154
155static inline void context_set_domain_id(struct context_entry *context,
156 unsigned long value)
157{
158 context->hi |= (value & ((1 << 16) - 1)) << 8;
159}
160
161static inline void context_clear_entry(struct context_entry *context)
162{
163 context->lo = 0;
164 context->hi = 0;
165}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000166
Mark McLoughlin622ba122008-11-20 15:49:46 +0000167/*
168 * 0: readable
169 * 1: writable
170 * 2-6: reserved
171 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800172 * 8-10: available
173 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000174 * 12-63: Host physcial address
175 */
176struct dma_pte {
177 u64 val;
178};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000179
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000180static inline void dma_clear_pte(struct dma_pte *pte)
181{
182 pte->val = 0;
183}
184
185static inline void dma_set_pte_readable(struct dma_pte *pte)
186{
187 pte->val |= DMA_PTE_READ;
188}
189
190static inline void dma_set_pte_writable(struct dma_pte *pte)
191{
192 pte->val |= DMA_PTE_WRITE;
193}
194
Sheng Yang9cf06692009-03-18 15:33:07 +0800195static inline void dma_set_pte_snp(struct dma_pte *pte)
196{
197 pte->val |= DMA_PTE_SNP;
198}
199
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000200static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
201{
202 pte->val = (pte->val & ~3) | (prot & 3);
203}
204
205static inline u64 dma_pte_addr(struct dma_pte *pte)
206{
207 return (pte->val & VTD_PAGE_MASK);
208}
209
210static inline void dma_set_pte_addr(struct dma_pte *pte, u64 addr)
211{
212 pte->val |= (addr & VTD_PAGE_MASK);
213}
214
215static inline bool dma_pte_present(struct dma_pte *pte)
216{
217 return (pte->val & 3) != 0;
218}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000219
Weidong Han3b5410e2008-12-08 09:17:15 +0800220/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100221#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800222
Weidong Han1ce28fe2008-12-08 16:35:39 +0800223/* domain represents a virtual machine, more than one devices
224 * across iommus may be owned in one domain, e.g. kvm guest.
225 */
226#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
227
Mark McLoughlin99126f72008-11-20 15:49:47 +0000228struct dmar_domain {
229 int id; /* domain id */
Weidong Han8c11e792008-12-08 15:29:22 +0800230 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000231
232 struct list_head devices; /* all devices' list */
233 struct iova_domain iovad; /* iova's that belong to this domain */
234
235 struct dma_pte *pgd; /* virtual address */
236 spinlock_t mapping_lock; /* page table lock */
237 int gaw; /* max guest address width */
238
239 /* adjusted guest address width, 0 is level 2 30-bit */
240 int agaw;
241
Weidong Han3b5410e2008-12-08 09:17:15 +0800242 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800243
244 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800245 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800246 int iommu_count; /* reference count of iommu */
247 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800248 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000249};
250
Mark McLoughlina647dacb2008-11-20 15:49:48 +0000251/* PCI domain-device relationship */
252struct device_domain_info {
253 struct list_head link; /* link to domain siblings */
254 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100255 int segment; /* PCI domain */
256 u8 bus; /* PCI bus number */
Mark McLoughlina647dacb2008-11-20 15:49:48 +0000257 u8 devfn; /* PCI devfn number */
258 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
259 struct dmar_domain *domain; /* pointer to domain */
260};
261
mark gross5e0d2a62008-03-04 15:22:08 -0800262static void flush_unmaps_timeout(unsigned long data);
263
264DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
265
mark gross80b20dd2008-04-18 13:53:58 -0700266#define HIGH_WATER_MARK 250
267struct deferred_flush_tables {
268 int next;
269 struct iova *iova[HIGH_WATER_MARK];
270 struct dmar_domain *domain[HIGH_WATER_MARK];
271};
272
273static struct deferred_flush_tables *deferred_flush;
274
mark gross5e0d2a62008-03-04 15:22:08 -0800275/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800276static int g_num_of_iommus;
277
278static DEFINE_SPINLOCK(async_umap_flush_lock);
279static LIST_HEAD(unmaps_to_do);
280
281static int timer_on;
282static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800283
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700284static void domain_remove_dev_info(struct dmar_domain *domain);
285
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800286#ifdef CONFIG_DMAR_DEFAULT_ON
287int dmar_disabled = 0;
288#else
289int dmar_disabled = 1;
290#endif /*CONFIG_DMAR_DEFAULT_ON*/
291
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700292static int __initdata dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700293static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800294static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700295
296#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
297static DEFINE_SPINLOCK(device_domain_lock);
298static LIST_HEAD(device_domain_list);
299
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100300static struct iommu_ops intel_iommu_ops;
301
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700302static int __init intel_iommu_setup(char *str)
303{
304 if (!str)
305 return -EINVAL;
306 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800307 if (!strncmp(str, "on", 2)) {
308 dmar_disabled = 0;
309 printk(KERN_INFO "Intel-IOMMU: enabled\n");
310 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700311 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800312 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700313 } else if (!strncmp(str, "igfx_off", 8)) {
314 dmar_map_gfx = 0;
315 printk(KERN_INFO
316 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700317 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800318 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700319 "Intel-IOMMU: Forcing DAC for PCI devices\n");
320 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800321 } else if (!strncmp(str, "strict", 6)) {
322 printk(KERN_INFO
323 "Intel-IOMMU: disable batched IOTLB flush\n");
324 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700325 }
326
327 str += strcspn(str, ",");
328 while (*str == ',')
329 str++;
330 }
331 return 0;
332}
333__setup("intel_iommu=", intel_iommu_setup);
334
335static struct kmem_cache *iommu_domain_cache;
336static struct kmem_cache *iommu_devinfo_cache;
337static struct kmem_cache *iommu_iova_cache;
338
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700339static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
340{
341 unsigned int flags;
342 void *vaddr;
343
344 /* trying to avoid low memory issues */
345 flags = current->flags & PF_MEMALLOC;
346 current->flags |= PF_MEMALLOC;
347 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
348 current->flags &= (~PF_MEMALLOC | flags);
349 return vaddr;
350}
351
352
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700353static inline void *alloc_pgtable_page(void)
354{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700355 unsigned int flags;
356 void *vaddr;
357
358 /* trying to avoid low memory issues */
359 flags = current->flags & PF_MEMALLOC;
360 current->flags |= PF_MEMALLOC;
361 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
362 current->flags &= (~PF_MEMALLOC | flags);
363 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700364}
365
366static inline void free_pgtable_page(void *vaddr)
367{
368 free_page((unsigned long)vaddr);
369}
370
371static inline void *alloc_domain_mem(void)
372{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700373 return iommu_kmem_cache_alloc(iommu_domain_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700374}
375
Kay, Allen M38717942008-09-09 18:37:29 +0300376static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700377{
378 kmem_cache_free(iommu_domain_cache, vaddr);
379}
380
381static inline void * alloc_devinfo_mem(void)
382{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700383 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700384}
385
386static inline void free_devinfo_mem(void *vaddr)
387{
388 kmem_cache_free(iommu_devinfo_cache, vaddr);
389}
390
391struct iova *alloc_iova_mem(void)
392{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700393 return iommu_kmem_cache_alloc(iommu_iova_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700394}
395
396void free_iova_mem(struct iova *iova)
397{
398 kmem_cache_free(iommu_iova_cache, iova);
399}
400
Weidong Han1b573682008-12-08 15:34:06 +0800401
402static inline int width_to_agaw(int width);
403
404/* calculate agaw for each iommu.
405 * "SAGAW" may be different across iommus, use a default agaw, and
406 * get a supported less agaw for iommus that don't support the default agaw.
407 */
408int iommu_calculate_agaw(struct intel_iommu *iommu)
409{
410 unsigned long sagaw;
411 int agaw = -1;
412
413 sagaw = cap_sagaw(iommu->cap);
414 for (agaw = width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH);
415 agaw >= 0; agaw--) {
416 if (test_bit(agaw, &sagaw))
417 break;
418 }
419
420 return agaw;
421}
422
Weidong Han8c11e792008-12-08 15:29:22 +0800423/* in native case, each domain is related to only one iommu */
424static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
425{
426 int iommu_id;
427
Weidong Han1ce28fe2008-12-08 16:35:39 +0800428 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
429
Weidong Han8c11e792008-12-08 15:29:22 +0800430 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
431 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
432 return NULL;
433
434 return g_iommus[iommu_id];
435}
436
Weidong Han8e6040972008-12-08 15:49:06 +0800437static void domain_update_iommu_coherency(struct dmar_domain *domain)
438{
439 int i;
440
441 domain->iommu_coherency = 1;
442
443 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
444 for (; i < g_num_of_iommus; ) {
445 if (!ecap_coherent(g_iommus[i]->ecap)) {
446 domain->iommu_coherency = 0;
447 break;
448 }
449 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
450 }
451}
452
Sheng Yang58c610b2009-03-18 15:33:05 +0800453static void domain_update_iommu_snooping(struct dmar_domain *domain)
454{
455 int i;
456
457 domain->iommu_snooping = 1;
458
459 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
460 for (; i < g_num_of_iommus; ) {
461 if (!ecap_sc_support(g_iommus[i]->ecap)) {
462 domain->iommu_snooping = 0;
463 break;
464 }
465 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
466 }
467}
468
469/* Some capabilities may be different across iommus */
470static void domain_update_iommu_cap(struct dmar_domain *domain)
471{
472 domain_update_iommu_coherency(domain);
473 domain_update_iommu_snooping(domain);
474}
475
David Woodhouse276dbf992009-04-04 01:45:37 +0100476static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800477{
478 struct dmar_drhd_unit *drhd = NULL;
479 int i;
480
481 for_each_drhd_unit(drhd) {
482 if (drhd->ignored)
483 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100484 if (segment != drhd->segment)
485 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800486
David Woodhouse924b6232009-04-04 00:39:25 +0100487 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000488 if (drhd->devices[i] &&
489 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800490 drhd->devices[i]->devfn == devfn)
491 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700492 if (drhd->devices[i] &&
493 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100494 drhd->devices[i]->subordinate->number <= bus &&
495 drhd->devices[i]->subordinate->subordinate >= bus)
496 return drhd->iommu;
497 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800498
499 if (drhd->include_all)
500 return drhd->iommu;
501 }
502
503 return NULL;
504}
505
Weidong Han5331fe62008-12-08 23:00:00 +0800506static void domain_flush_cache(struct dmar_domain *domain,
507 void *addr, int size)
508{
509 if (!domain->iommu_coherency)
510 clflush_cache_range(addr, size);
511}
512
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700513/* Gets context entry for a given bus and devfn */
514static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
515 u8 bus, u8 devfn)
516{
517 struct root_entry *root;
518 struct context_entry *context;
519 unsigned long phy_addr;
520 unsigned long flags;
521
522 spin_lock_irqsave(&iommu->lock, flags);
523 root = &iommu->root_entry[bus];
524 context = get_context_addr_from_root(root);
525 if (!context) {
526 context = (struct context_entry *)alloc_pgtable_page();
527 if (!context) {
528 spin_unlock_irqrestore(&iommu->lock, flags);
529 return NULL;
530 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700531 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700532 phy_addr = virt_to_phys((void *)context);
533 set_root_value(root, phy_addr);
534 set_root_present(root);
535 __iommu_flush_cache(iommu, root, sizeof(*root));
536 }
537 spin_unlock_irqrestore(&iommu->lock, flags);
538 return &context[devfn];
539}
540
541static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
542{
543 struct root_entry *root;
544 struct context_entry *context;
545 int ret;
546 unsigned long flags;
547
548 spin_lock_irqsave(&iommu->lock, flags);
549 root = &iommu->root_entry[bus];
550 context = get_context_addr_from_root(root);
551 if (!context) {
552 ret = 0;
553 goto out;
554 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000555 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700556out:
557 spin_unlock_irqrestore(&iommu->lock, flags);
558 return ret;
559}
560
561static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
562{
563 struct root_entry *root;
564 struct context_entry *context;
565 unsigned long flags;
566
567 spin_lock_irqsave(&iommu->lock, flags);
568 root = &iommu->root_entry[bus];
569 context = get_context_addr_from_root(root);
570 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000571 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700572 __iommu_flush_cache(iommu, &context[devfn], \
573 sizeof(*context));
574 }
575 spin_unlock_irqrestore(&iommu->lock, flags);
576}
577
578static void free_context_table(struct intel_iommu *iommu)
579{
580 struct root_entry *root;
581 int i;
582 unsigned long flags;
583 struct context_entry *context;
584
585 spin_lock_irqsave(&iommu->lock, flags);
586 if (!iommu->root_entry) {
587 goto out;
588 }
589 for (i = 0; i < ROOT_ENTRY_NR; i++) {
590 root = &iommu->root_entry[i];
591 context = get_context_addr_from_root(root);
592 if (context)
593 free_pgtable_page(context);
594 }
595 free_pgtable_page(iommu->root_entry);
596 iommu->root_entry = NULL;
597out:
598 spin_unlock_irqrestore(&iommu->lock, flags);
599}
600
601/* page table handling */
602#define LEVEL_STRIDE (9)
603#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
604
605static inline int agaw_to_level(int agaw)
606{
607 return agaw + 2;
608}
609
610static inline int agaw_to_width(int agaw)
611{
612 return 30 + agaw * LEVEL_STRIDE;
613
614}
615
616static inline int width_to_agaw(int width)
617{
618 return (width - 30) / LEVEL_STRIDE;
619}
620
621static inline unsigned int level_to_offset_bits(int level)
622{
623 return (12 + (level - 1) * LEVEL_STRIDE);
624}
625
626static inline int address_level_offset(u64 addr, int level)
627{
628 return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
629}
630
631static inline u64 level_mask(int level)
632{
633 return ((u64)-1 << level_to_offset_bits(level));
634}
635
636static inline u64 level_size(int level)
637{
638 return ((u64)1 << level_to_offset_bits(level));
639}
640
641static inline u64 align_to_level(u64 addr, int level)
642{
643 return ((addr + level_size(level) - 1) & level_mask(level));
644}
645
646static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
647{
648 int addr_width = agaw_to_width(domain->agaw);
649 struct dma_pte *parent, *pte = NULL;
650 int level = agaw_to_level(domain->agaw);
651 int offset;
652 unsigned long flags;
653
654 BUG_ON(!domain->pgd);
655
656 addr &= (((u64)1) << addr_width) - 1;
657 parent = domain->pgd;
658
659 spin_lock_irqsave(&domain->mapping_lock, flags);
660 while (level > 0) {
661 void *tmp_page;
662
663 offset = address_level_offset(addr, level);
664 pte = &parent[offset];
665 if (level == 1)
666 break;
667
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000668 if (!dma_pte_present(pte)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700669 tmp_page = alloc_pgtable_page();
670
671 if (!tmp_page) {
672 spin_unlock_irqrestore(&domain->mapping_lock,
673 flags);
674 return NULL;
675 }
Weidong Han5331fe62008-12-08 23:00:00 +0800676 domain_flush_cache(domain, tmp_page, PAGE_SIZE);
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000677 dma_set_pte_addr(pte, virt_to_phys(tmp_page));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700678 /*
679 * high level table always sets r/w, last level page
680 * table control read/write
681 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000682 dma_set_pte_readable(pte);
683 dma_set_pte_writable(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800684 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700685 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000686 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700687 level--;
688 }
689
690 spin_unlock_irqrestore(&domain->mapping_lock, flags);
691 return pte;
692}
693
694/* return address's pte at specific level */
695static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
696 int level)
697{
698 struct dma_pte *parent, *pte = NULL;
699 int total = agaw_to_level(domain->agaw);
700 int offset;
701
702 parent = domain->pgd;
703 while (level <= total) {
704 offset = address_level_offset(addr, total);
705 pte = &parent[offset];
706 if (level == total)
707 return pte;
708
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000709 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700710 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000711 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700712 total--;
713 }
714 return NULL;
715}
716
717/* clear one page's page table */
718static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
719{
720 struct dma_pte *pte = NULL;
721
722 /* get last level pte */
723 pte = dma_addr_level_pte(domain, addr, 1);
724
725 if (pte) {
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000726 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800727 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700728 }
729}
730
731/* clear last level pte, a tlb flush should be followed */
732static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
733{
734 int addr_width = agaw_to_width(domain->agaw);
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800735 int npages;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700736
737 start &= (((u64)1) << addr_width) - 1;
738 end &= (((u64)1) << addr_width) - 1;
739 /* in case it's partial page */
Fenghua Yu31d35682009-04-06 11:21:49 -0700740 start &= PAGE_MASK;
741 end = PAGE_ALIGN(end);
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800742 npages = (end - start) / VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700743
744 /* we don't need lock here, nobody else touches the iova range */
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800745 while (npages--) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700746 dma_pte_clear_one(domain, start);
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700747 start += VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700748 }
749}
750
751/* free page table pages. last level pte should already be cleared */
752static void dma_pte_free_pagetable(struct dmar_domain *domain,
753 u64 start, u64 end)
754{
755 int addr_width = agaw_to_width(domain->agaw);
756 struct dma_pte *pte;
757 int total = agaw_to_level(domain->agaw);
758 int level;
759 u64 tmp;
760
761 start &= (((u64)1) << addr_width) - 1;
762 end &= (((u64)1) << addr_width) - 1;
763
764 /* we don't need lock here, nobody else touches the iova range */
765 level = 2;
766 while (level <= total) {
767 tmp = align_to_level(start, level);
768 if (tmp >= end || (tmp + level_size(level) > end))
769 return;
770
771 while (tmp < end) {
772 pte = dma_addr_level_pte(domain, tmp, level);
773 if (pte) {
774 free_pgtable_page(
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000775 phys_to_virt(dma_pte_addr(pte)));
776 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800777 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700778 }
779 tmp += level_size(level);
780 }
781 level++;
782 }
783 /* free pgd */
784 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
785 free_pgtable_page(domain->pgd);
786 domain->pgd = NULL;
787 }
788}
789
790/* iommu handling */
791static int iommu_alloc_root_entry(struct intel_iommu *iommu)
792{
793 struct root_entry *root;
794 unsigned long flags;
795
796 root = (struct root_entry *)alloc_pgtable_page();
797 if (!root)
798 return -ENOMEM;
799
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700800 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700801
802 spin_lock_irqsave(&iommu->lock, flags);
803 iommu->root_entry = root;
804 spin_unlock_irqrestore(&iommu->lock, flags);
805
806 return 0;
807}
808
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700809static void iommu_set_root_entry(struct intel_iommu *iommu)
810{
811 void *addr;
812 u32 cmd, sts;
813 unsigned long flag;
814
815 addr = iommu->root_entry;
816
817 spin_lock_irqsave(&iommu->register_lock, flag);
818 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
819
820 cmd = iommu->gcmd | DMA_GCMD_SRTP;
821 writel(cmd, iommu->reg + DMAR_GCMD_REG);
822
823 /* Make sure hardware complete it */
824 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
825 readl, (sts & DMA_GSTS_RTPS), sts);
826
827 spin_unlock_irqrestore(&iommu->register_lock, flag);
828}
829
830static void iommu_flush_write_buffer(struct intel_iommu *iommu)
831{
832 u32 val;
833 unsigned long flag;
834
David Woodhouse9af88142009-02-13 23:18:03 +0000835 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700836 return;
837 val = iommu->gcmd | DMA_GCMD_WBF;
838
839 spin_lock_irqsave(&iommu->register_lock, flag);
840 writel(val, iommu->reg + DMAR_GCMD_REG);
841
842 /* Make sure hardware complete it */
843 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
844 readl, (!(val & DMA_GSTS_WBFS)), val);
845
846 spin_unlock_irqrestore(&iommu->register_lock, flag);
847}
848
849/* return value determine if we need a write buffer flush */
850static int __iommu_flush_context(struct intel_iommu *iommu,
851 u16 did, u16 source_id, u8 function_mask, u64 type,
852 int non_present_entry_flush)
853{
854 u64 val = 0;
855 unsigned long flag;
856
857 /*
858 * In the non-present entry flush case, if hardware doesn't cache
859 * non-present entry we do nothing and if hardware cache non-present
860 * entry, we flush entries of domain 0 (the domain id is used to cache
861 * any non-present entries)
862 */
863 if (non_present_entry_flush) {
864 if (!cap_caching_mode(iommu->cap))
865 return 1;
866 else
867 did = 0;
868 }
869
870 switch (type) {
871 case DMA_CCMD_GLOBAL_INVL:
872 val = DMA_CCMD_GLOBAL_INVL;
873 break;
874 case DMA_CCMD_DOMAIN_INVL:
875 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
876 break;
877 case DMA_CCMD_DEVICE_INVL:
878 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
879 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
880 break;
881 default:
882 BUG();
883 }
884 val |= DMA_CCMD_ICC;
885
886 spin_lock_irqsave(&iommu->register_lock, flag);
887 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
888
889 /* Make sure hardware complete it */
890 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
891 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
892
893 spin_unlock_irqrestore(&iommu->register_lock, flag);
894
Ameya Palande4d235ba2008-10-18 20:27:30 -0700895 /* flush context entry will implicitly flush write buffer */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700896 return 0;
897}
898
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700899/* return value determine if we need a write buffer flush */
900static int __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
901 u64 addr, unsigned int size_order, u64 type,
902 int non_present_entry_flush)
903{
904 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
905 u64 val = 0, val_iva = 0;
906 unsigned long flag;
907
908 /*
909 * In the non-present entry flush case, if hardware doesn't cache
910 * non-present entry we do nothing and if hardware cache non-present
911 * entry, we flush entries of domain 0 (the domain id is used to cache
912 * any non-present entries)
913 */
914 if (non_present_entry_flush) {
915 if (!cap_caching_mode(iommu->cap))
916 return 1;
917 else
918 did = 0;
919 }
920
921 switch (type) {
922 case DMA_TLB_GLOBAL_FLUSH:
923 /* global flush doesn't need set IVA_REG */
924 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
925 break;
926 case DMA_TLB_DSI_FLUSH:
927 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
928 break;
929 case DMA_TLB_PSI_FLUSH:
930 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
931 /* Note: always flush non-leaf currently */
932 val_iva = size_order | addr;
933 break;
934 default:
935 BUG();
936 }
937 /* Note: set drain read/write */
938#if 0
939 /*
940 * This is probably to be super secure.. Looks like we can
941 * ignore it without any impact.
942 */
943 if (cap_read_drain(iommu->cap))
944 val |= DMA_TLB_READ_DRAIN;
945#endif
946 if (cap_write_drain(iommu->cap))
947 val |= DMA_TLB_WRITE_DRAIN;
948
949 spin_lock_irqsave(&iommu->register_lock, flag);
950 /* Note: Only uses first TLB reg currently */
951 if (val_iva)
952 dmar_writeq(iommu->reg + tlb_offset, val_iva);
953 dmar_writeq(iommu->reg + tlb_offset + 8, val);
954
955 /* Make sure hardware complete it */
956 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
957 dmar_readq, (!(val & DMA_TLB_IVT)), val);
958
959 spin_unlock_irqrestore(&iommu->register_lock, flag);
960
961 /* check IOTLB invalidation granularity */
962 if (DMA_TLB_IAIG(val) == 0)
963 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
964 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
965 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700966 (unsigned long long)DMA_TLB_IIRG(type),
967 (unsigned long long)DMA_TLB_IAIG(val));
Ameya Palande4d235ba2008-10-18 20:27:30 -0700968 /* flush iotlb entry will implicitly flush write buffer */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700969 return 0;
970}
971
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700972static int iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
973 u64 addr, unsigned int pages, int non_present_entry_flush)
974{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700975 unsigned int mask;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700976
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700977 BUG_ON(addr & (~VTD_PAGE_MASK));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700978 BUG_ON(pages == 0);
979
980 /* Fallback to domain selective flush if no PSI support */
981 if (!cap_pgsel_inv(iommu->cap))
Youquan Songa77b67d2008-10-16 16:31:56 -0700982 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
983 DMA_TLB_DSI_FLUSH,
984 non_present_entry_flush);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700985
986 /*
987 * PSI requires page size to be 2 ^ x, and the base address is naturally
988 * aligned to the size
989 */
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700990 mask = ilog2(__roundup_pow_of_two(pages));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700991 /* Fallback to domain selective flush if size is too big */
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700992 if (mask > cap_max_amask_val(iommu->cap))
Youquan Songa77b67d2008-10-16 16:31:56 -0700993 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
994 DMA_TLB_DSI_FLUSH, non_present_entry_flush);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700995
Youquan Songa77b67d2008-10-16 16:31:56 -0700996 return iommu->flush.flush_iotlb(iommu, did, addr, mask,
997 DMA_TLB_PSI_FLUSH,
998 non_present_entry_flush);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700999}
1000
mark grossf8bab732008-02-08 04:18:38 -08001001static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1002{
1003 u32 pmen;
1004 unsigned long flags;
1005
1006 spin_lock_irqsave(&iommu->register_lock, flags);
1007 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1008 pmen &= ~DMA_PMEN_EPM;
1009 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1010
1011 /* wait for the protected region status bit to clear */
1012 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1013 readl, !(pmen & DMA_PMEN_PRS), pmen);
1014
1015 spin_unlock_irqrestore(&iommu->register_lock, flags);
1016}
1017
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001018static int iommu_enable_translation(struct intel_iommu *iommu)
1019{
1020 u32 sts;
1021 unsigned long flags;
1022
1023 spin_lock_irqsave(&iommu->register_lock, flags);
1024 writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
1025
1026 /* Make sure hardware complete it */
1027 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1028 readl, (sts & DMA_GSTS_TES), sts);
1029
1030 iommu->gcmd |= DMA_GCMD_TE;
1031 spin_unlock_irqrestore(&iommu->register_lock, flags);
1032 return 0;
1033}
1034
1035static int iommu_disable_translation(struct intel_iommu *iommu)
1036{
1037 u32 sts;
1038 unsigned long flag;
1039
1040 spin_lock_irqsave(&iommu->register_lock, flag);
1041 iommu->gcmd &= ~DMA_GCMD_TE;
1042 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1043
1044 /* Make sure hardware complete it */
1045 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1046 readl, (!(sts & DMA_GSTS_TES)), sts);
1047
1048 spin_unlock_irqrestore(&iommu->register_lock, flag);
1049 return 0;
1050}
1051
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001052
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001053static int iommu_init_domains(struct intel_iommu *iommu)
1054{
1055 unsigned long ndomains;
1056 unsigned long nlongs;
1057
1058 ndomains = cap_ndoms(iommu->cap);
1059 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1060 nlongs = BITS_TO_LONGS(ndomains);
1061
1062 /* TBD: there might be 64K domains,
1063 * consider other allocation for future chip
1064 */
1065 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1066 if (!iommu->domain_ids) {
1067 printk(KERN_ERR "Allocating domain id array failed\n");
1068 return -ENOMEM;
1069 }
1070 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1071 GFP_KERNEL);
1072 if (!iommu->domains) {
1073 printk(KERN_ERR "Allocating domain array failed\n");
1074 kfree(iommu->domain_ids);
1075 return -ENOMEM;
1076 }
1077
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001078 spin_lock_init(&iommu->lock);
1079
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001080 /*
1081 * if Caching mode is set, then invalid translations are tagged
1082 * with domainid 0. Hence we need to pre-allocate it.
1083 */
1084 if (cap_caching_mode(iommu->cap))
1085 set_bit(0, iommu->domain_ids);
1086 return 0;
1087}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001088
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001089
1090static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001091static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001092
1093void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001094{
1095 struct dmar_domain *domain;
1096 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001097 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001098
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001099 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1100 for (; i < cap_ndoms(iommu->cap); ) {
1101 domain = iommu->domains[i];
1102 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001103
1104 spin_lock_irqsave(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001105 if (--domain->iommu_count == 0) {
1106 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1107 vm_domain_exit(domain);
1108 else
1109 domain_exit(domain);
1110 }
Weidong Hanc7151a82008-12-08 22:51:37 +08001111 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1112
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001113 i = find_next_bit(iommu->domain_ids,
1114 cap_ndoms(iommu->cap), i+1);
1115 }
1116
1117 if (iommu->gcmd & DMA_GCMD_TE)
1118 iommu_disable_translation(iommu);
1119
1120 if (iommu->irq) {
1121 set_irq_data(iommu->irq, NULL);
1122 /* This will mask the irq */
1123 free_irq(iommu->irq, iommu);
1124 destroy_irq(iommu->irq);
1125 }
1126
1127 kfree(iommu->domains);
1128 kfree(iommu->domain_ids);
1129
Weidong Hand9630fe2008-12-08 11:06:32 +08001130 g_iommus[iommu->seq_id] = NULL;
1131
1132 /* if all iommus are freed, free g_iommus */
1133 for (i = 0; i < g_num_of_iommus; i++) {
1134 if (g_iommus[i])
1135 break;
1136 }
1137
1138 if (i == g_num_of_iommus)
1139 kfree(g_iommus);
1140
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001141 /* free context mapping */
1142 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001143}
1144
1145static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
1146{
1147 unsigned long num;
1148 unsigned long ndomains;
1149 struct dmar_domain *domain;
1150 unsigned long flags;
1151
1152 domain = alloc_domain_mem();
1153 if (!domain)
1154 return NULL;
1155
1156 ndomains = cap_ndoms(iommu->cap);
1157
1158 spin_lock_irqsave(&iommu->lock, flags);
1159 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1160 if (num >= ndomains) {
1161 spin_unlock_irqrestore(&iommu->lock, flags);
1162 free_domain_mem(domain);
1163 printk(KERN_ERR "IOMMU: no free domain ids\n");
1164 return NULL;
1165 }
1166
1167 set_bit(num, iommu->domain_ids);
1168 domain->id = num;
Weidong Han8c11e792008-12-08 15:29:22 +08001169 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
1170 set_bit(iommu->seq_id, &domain->iommu_bmp);
Weidong Hand71a2f32008-12-07 21:13:41 +08001171 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001172 iommu->domains[num] = domain;
1173 spin_unlock_irqrestore(&iommu->lock, flags);
1174
1175 return domain;
1176}
1177
1178static void iommu_free_domain(struct dmar_domain *domain)
1179{
1180 unsigned long flags;
Weidong Han8c11e792008-12-08 15:29:22 +08001181 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001182
Weidong Han8c11e792008-12-08 15:29:22 +08001183 iommu = domain_get_iommu(domain);
1184
1185 spin_lock_irqsave(&iommu->lock, flags);
1186 clear_bit(domain->id, iommu->domain_ids);
1187 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001188}
1189
1190static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001191static struct lock_class_key reserved_alloc_key;
1192static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001193
1194static void dmar_init_reserved_ranges(void)
1195{
1196 struct pci_dev *pdev = NULL;
1197 struct iova *iova;
1198 int i;
1199 u64 addr, size;
1200
David Millerf6611972008-02-06 01:36:23 -08001201 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001202
Mark Gross8a443df2008-03-04 14:59:31 -08001203 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1204 &reserved_alloc_key);
1205 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1206 &reserved_rbtree_key);
1207
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001208 /* IOAPIC ranges shouldn't be accessed by DMA */
1209 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1210 IOVA_PFN(IOAPIC_RANGE_END));
1211 if (!iova)
1212 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1213
1214 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1215 for_each_pci_dev(pdev) {
1216 struct resource *r;
1217
1218 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1219 r = &pdev->resource[i];
1220 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1221 continue;
1222 addr = r->start;
David Woodhousefd18de52009-05-10 23:57:41 +01001223 addr &= PHYSICAL_PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001224 size = r->end - addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001225 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001226 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1227 IOVA_PFN(size + addr) - 1);
1228 if (!iova)
1229 printk(KERN_ERR "Reserve iova failed\n");
1230 }
1231 }
1232
1233}
1234
1235static void domain_reserve_special_ranges(struct dmar_domain *domain)
1236{
1237 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1238}
1239
1240static inline int guestwidth_to_adjustwidth(int gaw)
1241{
1242 int agaw;
1243 int r = (gaw - 12) % 9;
1244
1245 if (r == 0)
1246 agaw = gaw;
1247 else
1248 agaw = gaw + 9 - r;
1249 if (agaw > 64)
1250 agaw = 64;
1251 return agaw;
1252}
1253
1254static int domain_init(struct dmar_domain *domain, int guest_width)
1255{
1256 struct intel_iommu *iommu;
1257 int adjust_width, agaw;
1258 unsigned long sagaw;
1259
David Millerf6611972008-02-06 01:36:23 -08001260 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001261 spin_lock_init(&domain->mapping_lock);
Weidong Hanc7151a82008-12-08 22:51:37 +08001262 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001263
1264 domain_reserve_special_ranges(domain);
1265
1266 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001267 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001268 if (guest_width > cap_mgaw(iommu->cap))
1269 guest_width = cap_mgaw(iommu->cap);
1270 domain->gaw = guest_width;
1271 adjust_width = guestwidth_to_adjustwidth(guest_width);
1272 agaw = width_to_agaw(adjust_width);
1273 sagaw = cap_sagaw(iommu->cap);
1274 if (!test_bit(agaw, &sagaw)) {
1275 /* hardware doesn't support it, choose a bigger one */
1276 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1277 agaw = find_next_bit(&sagaw, 5, agaw);
1278 if (agaw >= 5)
1279 return -ENODEV;
1280 }
1281 domain->agaw = agaw;
1282 INIT_LIST_HEAD(&domain->devices);
1283
Weidong Han8e6040972008-12-08 15:49:06 +08001284 if (ecap_coherent(iommu->ecap))
1285 domain->iommu_coherency = 1;
1286 else
1287 domain->iommu_coherency = 0;
1288
Sheng Yang58c610b2009-03-18 15:33:05 +08001289 if (ecap_sc_support(iommu->ecap))
1290 domain->iommu_snooping = 1;
1291 else
1292 domain->iommu_snooping = 0;
1293
Weidong Hanc7151a82008-12-08 22:51:37 +08001294 domain->iommu_count = 1;
1295
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001296 /* always allocate the top pgd */
1297 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1298 if (!domain->pgd)
1299 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001300 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001301 return 0;
1302}
1303
1304static void domain_exit(struct dmar_domain *domain)
1305{
1306 u64 end;
1307
1308 /* Domain 0 is reserved, so dont process it */
1309 if (!domain)
1310 return;
1311
1312 domain_remove_dev_info(domain);
1313 /* destroy iovas */
1314 put_iova_domain(&domain->iovad);
1315 end = DOMAIN_MAX_ADDR(domain->gaw);
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001316 end = end & (~PAGE_MASK);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001317
1318 /* clear ptes */
1319 dma_pte_clear_range(domain, 0, end);
1320
1321 /* free page tables */
1322 dma_pte_free_pagetable(domain, 0, end);
1323
1324 iommu_free_domain(domain);
1325 free_domain_mem(domain);
1326}
1327
1328static int domain_context_mapping_one(struct dmar_domain *domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001329 int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001330{
1331 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001332 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001333 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001334 struct dma_pte *pgd;
1335 unsigned long num;
1336 unsigned long ndomains;
1337 int id;
1338 int agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001339
1340 pr_debug("Set context mapping for %02x:%02x.%d\n",
1341 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1342 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001343
David Woodhouse276dbf992009-04-04 01:45:37 +01001344 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001345 if (!iommu)
1346 return -ENODEV;
1347
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001348 context = device_to_context_entry(iommu, bus, devfn);
1349 if (!context)
1350 return -ENOMEM;
1351 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001352 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001353 spin_unlock_irqrestore(&iommu->lock, flags);
1354 return 0;
1355 }
1356
Weidong Hanea6606b2008-12-08 23:08:15 +08001357 id = domain->id;
1358 pgd = domain->pgd;
1359
1360 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) {
1361 int found = 0;
1362
1363 /* find an available domain id for this device in iommu */
1364 ndomains = cap_ndoms(iommu->cap);
1365 num = find_first_bit(iommu->domain_ids, ndomains);
1366 for (; num < ndomains; ) {
1367 if (iommu->domains[num] == domain) {
1368 id = num;
1369 found = 1;
1370 break;
1371 }
1372 num = find_next_bit(iommu->domain_ids,
1373 cap_ndoms(iommu->cap), num+1);
1374 }
1375
1376 if (found == 0) {
1377 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1378 if (num >= ndomains) {
1379 spin_unlock_irqrestore(&iommu->lock, flags);
1380 printk(KERN_ERR "IOMMU: no free domain ids\n");
1381 return -EFAULT;
1382 }
1383
1384 set_bit(num, iommu->domain_ids);
1385 iommu->domains[num] = domain;
1386 id = num;
1387 }
1388
1389 /* Skip top levels of page tables for
1390 * iommu which has less agaw than default.
1391 */
1392 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1393 pgd = phys_to_virt(dma_pte_addr(pgd));
1394 if (!dma_pte_present(pgd)) {
1395 spin_unlock_irqrestore(&iommu->lock, flags);
1396 return -ENOMEM;
1397 }
1398 }
1399 }
1400
1401 context_set_domain_id(context, id);
1402 context_set_address_width(context, iommu->agaw);
1403 context_set_address_root(context, virt_to_phys(pgd));
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001404 context_set_translation_type(context, CONTEXT_TT_MULTI_LEVEL);
1405 context_set_fault_enable(context);
1406 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001407 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001408
1409 /* it's a non-present to present mapping */
Youquan Songa77b67d2008-10-16 16:31:56 -07001410 if (iommu->flush.flush_context(iommu, domain->id,
1411 (((u16)bus) << 8) | devfn, DMA_CCMD_MASK_NOBIT,
1412 DMA_CCMD_DEVICE_INVL, 1))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001413 iommu_flush_write_buffer(iommu);
1414 else
Youquan Songa77b67d2008-10-16 16:31:56 -07001415 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH, 0);
1416
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001417 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001418
1419 spin_lock_irqsave(&domain->iommu_lock, flags);
1420 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1421 domain->iommu_count++;
Sheng Yang58c610b2009-03-18 15:33:05 +08001422 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001423 }
1424 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001425 return 0;
1426}
1427
1428static int
1429domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev)
1430{
1431 int ret;
1432 struct pci_dev *tmp, *parent;
1433
David Woodhouse276dbf992009-04-04 01:45:37 +01001434 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
1435 pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001436 if (ret)
1437 return ret;
1438
1439 /* dependent device mapping */
1440 tmp = pci_find_upstream_pcie_bridge(pdev);
1441 if (!tmp)
1442 return 0;
1443 /* Secondary interface's bus number and devfn 0 */
1444 parent = pdev->bus->self;
1445 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001446 ret = domain_context_mapping_one(domain,
1447 pci_domain_nr(parent->bus),
1448 parent->bus->number,
1449 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001450 if (ret)
1451 return ret;
1452 parent = parent->bus->self;
1453 }
1454 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1455 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001456 pci_domain_nr(tmp->subordinate),
1457 tmp->subordinate->number, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001458 else /* this is a legacy PCI bridge */
1459 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001460 pci_domain_nr(tmp->bus),
1461 tmp->bus->number,
1462 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001463}
1464
Weidong Han5331fe62008-12-08 23:00:00 +08001465static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001466{
1467 int ret;
1468 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001469 struct intel_iommu *iommu;
1470
David Woodhouse276dbf992009-04-04 01:45:37 +01001471 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1472 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001473 if (!iommu)
1474 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001475
David Woodhouse276dbf992009-04-04 01:45:37 +01001476 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001477 if (!ret)
1478 return ret;
1479 /* dependent device mapping */
1480 tmp = pci_find_upstream_pcie_bridge(pdev);
1481 if (!tmp)
1482 return ret;
1483 /* Secondary interface's bus number and devfn 0 */
1484 parent = pdev->bus->self;
1485 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001486 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001487 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001488 if (!ret)
1489 return ret;
1490 parent = parent->bus->self;
1491 }
1492 if (tmp->is_pcie)
David Woodhouse276dbf992009-04-04 01:45:37 +01001493 return device_context_mapped(iommu, tmp->subordinate->number,
1494 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001495 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001496 return device_context_mapped(iommu, tmp->bus->number,
1497 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001498}
1499
1500static int
1501domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1502 u64 hpa, size_t size, int prot)
1503{
1504 u64 start_pfn, end_pfn;
1505 struct dma_pte *pte;
1506 int index;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001507 int addr_width = agaw_to_width(domain->agaw);
1508
1509 hpa &= (((u64)1) << addr_width) - 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001510
1511 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1512 return -EINVAL;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001513 iova &= PAGE_MASK;
1514 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1515 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001516 index = 0;
1517 while (start_pfn < end_pfn) {
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001518 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001519 if (!pte)
1520 return -ENOMEM;
1521 /* We don't need lock here, nobody else
1522 * touches the iova range
1523 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001524 BUG_ON(dma_pte_addr(pte));
1525 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
1526 dma_set_pte_prot(pte, prot);
Sheng Yang9cf06692009-03-18 15:33:07 +08001527 if (prot & DMA_PTE_SNP)
1528 dma_set_pte_snp(pte);
Weidong Han5331fe62008-12-08 23:00:00 +08001529 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001530 start_pfn++;
1531 index++;
1532 }
1533 return 0;
1534}
1535
Weidong Hanc7151a82008-12-08 22:51:37 +08001536static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001537{
Weidong Hanc7151a82008-12-08 22:51:37 +08001538 if (!iommu)
1539 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001540
1541 clear_context_table(iommu, bus, devfn);
1542 iommu->flush.flush_context(iommu, 0, 0, 0,
Youquan Songa77b67d2008-10-16 16:31:56 -07001543 DMA_CCMD_GLOBAL_INVL, 0);
Weidong Han8c11e792008-12-08 15:29:22 +08001544 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Youquan Songa77b67d2008-10-16 16:31:56 -07001545 DMA_TLB_GLOBAL_FLUSH, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001546}
1547
1548static void domain_remove_dev_info(struct dmar_domain *domain)
1549{
1550 struct device_domain_info *info;
1551 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001552 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001553
1554 spin_lock_irqsave(&device_domain_lock, flags);
1555 while (!list_empty(&domain->devices)) {
1556 info = list_entry(domain->devices.next,
1557 struct device_domain_info, link);
1558 list_del(&info->link);
1559 list_del(&info->global);
1560 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001561 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001562 spin_unlock_irqrestore(&device_domain_lock, flags);
1563
David Woodhouse276dbf992009-04-04 01:45:37 +01001564 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001565 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001566 free_devinfo_mem(info);
1567
1568 spin_lock_irqsave(&device_domain_lock, flags);
1569 }
1570 spin_unlock_irqrestore(&device_domain_lock, flags);
1571}
1572
1573/*
1574 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001575 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001576 */
Kay, Allen M38717942008-09-09 18:37:29 +03001577static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001578find_domain(struct pci_dev *pdev)
1579{
1580 struct device_domain_info *info;
1581
1582 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001583 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001584 if (info)
1585 return info->domain;
1586 return NULL;
1587}
1588
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001589/* domain is initialized */
1590static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1591{
1592 struct dmar_domain *domain, *found = NULL;
1593 struct intel_iommu *iommu;
1594 struct dmar_drhd_unit *drhd;
1595 struct device_domain_info *info, *tmp;
1596 struct pci_dev *dev_tmp;
1597 unsigned long flags;
1598 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001599 int segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001600
1601 domain = find_domain(pdev);
1602 if (domain)
1603 return domain;
1604
David Woodhouse276dbf992009-04-04 01:45:37 +01001605 segment = pci_domain_nr(pdev->bus);
1606
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001607 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1608 if (dev_tmp) {
1609 if (dev_tmp->is_pcie) {
1610 bus = dev_tmp->subordinate->number;
1611 devfn = 0;
1612 } else {
1613 bus = dev_tmp->bus->number;
1614 devfn = dev_tmp->devfn;
1615 }
1616 spin_lock_irqsave(&device_domain_lock, flags);
1617 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001618 if (info->segment == segment &&
1619 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001620 found = info->domain;
1621 break;
1622 }
1623 }
1624 spin_unlock_irqrestore(&device_domain_lock, flags);
1625 /* pcie-pci bridge already has a domain, uses it */
1626 if (found) {
1627 domain = found;
1628 goto found_domain;
1629 }
1630 }
1631
1632 /* Allocate new domain for the device */
1633 drhd = dmar_find_matched_drhd_unit(pdev);
1634 if (!drhd) {
1635 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1636 pci_name(pdev));
1637 return NULL;
1638 }
1639 iommu = drhd->iommu;
1640
1641 domain = iommu_alloc_domain(iommu);
1642 if (!domain)
1643 goto error;
1644
1645 if (domain_init(domain, gaw)) {
1646 domain_exit(domain);
1647 goto error;
1648 }
1649
1650 /* register pcie-to-pci device */
1651 if (dev_tmp) {
1652 info = alloc_devinfo_mem();
1653 if (!info) {
1654 domain_exit(domain);
1655 goto error;
1656 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001657 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001658 info->bus = bus;
1659 info->devfn = devfn;
1660 info->dev = NULL;
1661 info->domain = domain;
1662 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001663 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001664
1665 /* pcie-to-pci bridge already has a domain, uses it */
1666 found = NULL;
1667 spin_lock_irqsave(&device_domain_lock, flags);
1668 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001669 if (tmp->segment == segment &&
1670 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001671 found = tmp->domain;
1672 break;
1673 }
1674 }
1675 if (found) {
1676 free_devinfo_mem(info);
1677 domain_exit(domain);
1678 domain = found;
1679 } else {
1680 list_add(&info->link, &domain->devices);
1681 list_add(&info->global, &device_domain_list);
1682 }
1683 spin_unlock_irqrestore(&device_domain_lock, flags);
1684 }
1685
1686found_domain:
1687 info = alloc_devinfo_mem();
1688 if (!info)
1689 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01001690 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001691 info->bus = pdev->bus->number;
1692 info->devfn = pdev->devfn;
1693 info->dev = pdev;
1694 info->domain = domain;
1695 spin_lock_irqsave(&device_domain_lock, flags);
1696 /* somebody is fast */
1697 found = find_domain(pdev);
1698 if (found != NULL) {
1699 spin_unlock_irqrestore(&device_domain_lock, flags);
1700 if (found != domain) {
1701 domain_exit(domain);
1702 domain = found;
1703 }
1704 free_devinfo_mem(info);
1705 return domain;
1706 }
1707 list_add(&info->link, &domain->devices);
1708 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001709 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001710 spin_unlock_irqrestore(&device_domain_lock, flags);
1711 return domain;
1712error:
1713 /* recheck it here, maybe others set it */
1714 return find_domain(pdev);
1715}
1716
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001717static int iommu_prepare_identity_map(struct pci_dev *pdev,
1718 unsigned long long start,
1719 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001720{
1721 struct dmar_domain *domain;
1722 unsigned long size;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001723 unsigned long long base;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001724 int ret;
1725
1726 printk(KERN_INFO
1727 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1728 pci_name(pdev), start, end);
1729 /* page table init */
1730 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1731 if (!domain)
1732 return -ENOMEM;
1733
1734 /* The address might not be aligned */
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001735 base = start & PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001736 size = end - base;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001737 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001738 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1739 IOVA_PFN(base + size) - 1)) {
1740 printk(KERN_ERR "IOMMU: reserve iova failed\n");
1741 ret = -ENOMEM;
1742 goto error;
1743 }
1744
1745 pr_debug("Mapping reserved region %lx@%llx for %s\n",
1746 size, base, pci_name(pdev));
1747 /*
1748 * RMRR range might have overlap with physical memory range,
1749 * clear it first
1750 */
1751 dma_pte_clear_range(domain, base, base + size);
1752
1753 ret = domain_page_mapping(domain, base, base, size,
1754 DMA_PTE_READ|DMA_PTE_WRITE);
1755 if (ret)
1756 goto error;
1757
1758 /* context entry init */
1759 ret = domain_context_mapping(domain, pdev);
1760 if (!ret)
1761 return 0;
1762error:
1763 domain_exit(domain);
1764 return ret;
1765
1766}
1767
1768static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1769 struct pci_dev *pdev)
1770{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001771 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001772 return 0;
1773 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1774 rmrr->end_address + 1);
1775}
1776
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001777#ifdef CONFIG_DMAR_GFX_WA
Yinghai Lud52d53b2008-06-16 20:10:55 -07001778struct iommu_prepare_data {
1779 struct pci_dev *pdev;
1780 int ret;
1781};
1782
1783static int __init iommu_prepare_work_fn(unsigned long start_pfn,
1784 unsigned long end_pfn, void *datax)
1785{
1786 struct iommu_prepare_data *data;
1787
1788 data = (struct iommu_prepare_data *)datax;
1789
1790 data->ret = iommu_prepare_identity_map(data->pdev,
1791 start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
1792 return data->ret;
1793
1794}
1795
1796static int __init iommu_prepare_with_active_regions(struct pci_dev *pdev)
1797{
1798 int nid;
1799 struct iommu_prepare_data data;
1800
1801 data.pdev = pdev;
1802 data.ret = 0;
1803
1804 for_each_online_node(nid) {
1805 work_with_active_regions(nid, iommu_prepare_work_fn, &data);
1806 if (data.ret)
1807 return data.ret;
1808 }
1809 return data.ret;
1810}
1811
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001812static void __init iommu_prepare_gfx_mapping(void)
1813{
1814 struct pci_dev *pdev = NULL;
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001815 int ret;
1816
1817 for_each_pci_dev(pdev) {
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001818 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO ||
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001819 !IS_GFX_DEVICE(pdev))
1820 continue;
1821 printk(KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n",
1822 pci_name(pdev));
Yinghai Lud52d53b2008-06-16 20:10:55 -07001823 ret = iommu_prepare_with_active_regions(pdev);
1824 if (ret)
1825 printk(KERN_ERR "IOMMU: mapping reserved region failed\n");
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001826 }
1827}
Mark McLoughlin2abd7e12008-11-20 15:49:50 +00001828#else /* !CONFIG_DMAR_GFX_WA */
1829static inline void iommu_prepare_gfx_mapping(void)
1830{
1831 return;
1832}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001833#endif
1834
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001835#ifdef CONFIG_DMAR_FLOPPY_WA
1836static inline void iommu_prepare_isa(void)
1837{
1838 struct pci_dev *pdev;
1839 int ret;
1840
1841 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1842 if (!pdev)
1843 return;
1844
1845 printk(KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n");
1846 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1847
1848 if (ret)
Frank Seidel1c35b8e2009-02-06 10:23:36 +01001849 printk(KERN_ERR "IOMMU: Failed to create 0-64M identity map, "
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001850 "floppy might not work\n");
1851
1852}
1853#else
1854static inline void iommu_prepare_isa(void)
1855{
1856 return;
1857}
1858#endif /* !CONFIG_DMAR_FLPY_WA */
1859
Mark McLoughlin519a0542008-11-20 14:21:13 +00001860static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001861{
1862 struct dmar_drhd_unit *drhd;
1863 struct dmar_rmrr_unit *rmrr;
1864 struct pci_dev *pdev;
1865 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001866 int i, ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001867
1868 /*
1869 * for each drhd
1870 * allocate root
1871 * initialize and program root entry to not present
1872 * endfor
1873 */
1874 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08001875 g_num_of_iommus++;
1876 /*
1877 * lock not needed as this is only incremented in the single
1878 * threaded kernel __init code path all other access are read
1879 * only
1880 */
1881 }
1882
Weidong Hand9630fe2008-12-08 11:06:32 +08001883 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
1884 GFP_KERNEL);
1885 if (!g_iommus) {
1886 printk(KERN_ERR "Allocating global iommu array failed\n");
1887 ret = -ENOMEM;
1888 goto error;
1889 }
1890
mark gross80b20dd2008-04-18 13:53:58 -07001891 deferred_flush = kzalloc(g_num_of_iommus *
1892 sizeof(struct deferred_flush_tables), GFP_KERNEL);
1893 if (!deferred_flush) {
Weidong Hand9630fe2008-12-08 11:06:32 +08001894 kfree(g_iommus);
mark gross5e0d2a62008-03-04 15:22:08 -08001895 ret = -ENOMEM;
1896 goto error;
1897 }
1898
mark gross5e0d2a62008-03-04 15:22:08 -08001899 for_each_drhd_unit(drhd) {
1900 if (drhd->ignored)
1901 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07001902
1903 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08001904 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001905
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001906 ret = iommu_init_domains(iommu);
1907 if (ret)
1908 goto error;
1909
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001910 /*
1911 * TBD:
1912 * we could share the same root & context tables
1913 * amoung all IOMMU's. Need to Split it later.
1914 */
1915 ret = iommu_alloc_root_entry(iommu);
1916 if (ret) {
1917 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
1918 goto error;
1919 }
1920 }
1921
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001922 /*
1923 * Start from the sane iommu hardware state.
1924 */
Youquan Songa77b67d2008-10-16 16:31:56 -07001925 for_each_drhd_unit(drhd) {
1926 if (drhd->ignored)
1927 continue;
1928
1929 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001930
1931 /*
1932 * If the queued invalidation is already initialized by us
1933 * (for example, while enabling interrupt-remapping) then
1934 * we got the things already rolling from a sane state.
1935 */
1936 if (iommu->qi)
1937 continue;
1938
1939 /*
1940 * Clear any previous faults.
1941 */
1942 dmar_fault(-1, iommu);
1943 /*
1944 * Disable queued invalidation if supported and already enabled
1945 * before OS handover.
1946 */
1947 dmar_disable_qi(iommu);
1948 }
1949
1950 for_each_drhd_unit(drhd) {
1951 if (drhd->ignored)
1952 continue;
1953
1954 iommu = drhd->iommu;
1955
Youquan Songa77b67d2008-10-16 16:31:56 -07001956 if (dmar_enable_qi(iommu)) {
1957 /*
1958 * Queued Invalidate not enabled, use Register Based
1959 * Invalidate
1960 */
1961 iommu->flush.flush_context = __iommu_flush_context;
1962 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
1963 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09001964 "invalidation\n",
1965 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07001966 } else {
1967 iommu->flush.flush_context = qi_flush_context;
1968 iommu->flush.flush_iotlb = qi_flush_iotlb;
1969 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09001970 "invalidation\n",
1971 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07001972 }
1973 }
1974
Han, Weidongd0b03bd2009-04-03 17:15:50 +08001975#ifdef CONFIG_INTR_REMAP
1976 if (!intr_remapping_enabled) {
1977 ret = enable_intr_remapping(0);
1978 if (ret)
1979 printk(KERN_ERR
1980 "IOMMU: enable interrupt remapping failed\n");
1981 }
1982#endif
1983
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001984 /*
1985 * For each rmrr
1986 * for each dev attached to rmrr
1987 * do
1988 * locate drhd for dev, alloc domain for dev
1989 * allocate free domain
1990 * allocate page table entries for rmrr
1991 * if context not allocated for bus
1992 * allocate and init context
1993 * set present in root table for this bus
1994 * init context with domain, translation etc
1995 * endfor
1996 * endfor
1997 */
1998 for_each_rmrr_units(rmrr) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001999 for (i = 0; i < rmrr->devices_cnt; i++) {
2000 pdev = rmrr->devices[i];
2001 /* some BIOS lists non-exist devices in DMAR table */
2002 if (!pdev)
2003 continue;
2004 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2005 if (ret)
2006 printk(KERN_ERR
2007 "IOMMU: mapping reserved region failed\n");
2008 }
2009 }
2010
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002011 iommu_prepare_gfx_mapping();
2012
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002013 iommu_prepare_isa();
2014
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002015 /*
2016 * for each drhd
2017 * enable fault log
2018 * global invalidate context cache
2019 * global invalidate iotlb
2020 * enable translation
2021 */
2022 for_each_drhd_unit(drhd) {
2023 if (drhd->ignored)
2024 continue;
2025 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002026
2027 iommu_flush_write_buffer(iommu);
2028
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002029 ret = dmar_set_interrupt(iommu);
2030 if (ret)
2031 goto error;
2032
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002033 iommu_set_root_entry(iommu);
2034
Youquan Songa77b67d2008-10-16 16:31:56 -07002035 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL,
2036 0);
2037 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH,
2038 0);
mark grossf8bab732008-02-08 04:18:38 -08002039 iommu_disable_protect_mem_regions(iommu);
2040
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002041 ret = iommu_enable_translation(iommu);
2042 if (ret)
2043 goto error;
2044 }
2045
2046 return 0;
2047error:
2048 for_each_drhd_unit(drhd) {
2049 if (drhd->ignored)
2050 continue;
2051 iommu = drhd->iommu;
2052 free_iommu(iommu);
2053 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002054 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002055 return ret;
2056}
2057
2058static inline u64 aligned_size(u64 host_addr, size_t size)
2059{
2060 u64 addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002061 addr = (host_addr & (~PAGE_MASK)) + size;
2062 return PAGE_ALIGN(addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002063}
2064
2065struct iova *
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002066iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002067{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002068 struct iova *piova;
2069
2070 /* Make sure it's in range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002071 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002072 if (!size || (IOVA_START_ADDR + size > end))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002073 return NULL;
2074
2075 piova = alloc_iova(&domain->iovad,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002076 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002077 return piova;
2078}
2079
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002080static struct iova *
2081__intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002082 size_t size, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002083{
2084 struct pci_dev *pdev = to_pci_dev(dev);
2085 struct iova *iova = NULL;
2086
Yang Hongyang284901a2009-04-06 19:01:15 -07002087 if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002088 iova = iommu_alloc_iova(domain, size, dma_mask);
2089 else {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002090 /*
2091 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002092 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002093 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002094 */
Yang Hongyang284901a2009-04-06 19:01:15 -07002095 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002096 if (!iova)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002097 iova = iommu_alloc_iova(domain, size, dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002098 }
2099
2100 if (!iova) {
2101 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2102 return NULL;
2103 }
2104
2105 return iova;
2106}
2107
2108static struct dmar_domain *
2109get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002110{
2111 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002112 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002113
2114 domain = get_domain_for_dev(pdev,
2115 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2116 if (!domain) {
2117 printk(KERN_ERR
2118 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002119 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002120 }
2121
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002122 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002123 if (unlikely(!domain_context_mapped(pdev))) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002124 ret = domain_context_mapping(domain, pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002125 if (ret) {
2126 printk(KERN_ERR
2127 "Domain context map for %s failed",
2128 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002129 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002130 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002131 }
2132
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002133 return domain;
2134}
2135
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002136static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2137 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002138{
2139 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002140 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002141 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002142 struct iova *iova;
2143 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002144 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002145 struct intel_iommu *iommu;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002146
2147 BUG_ON(dir == DMA_NONE);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002148 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002149 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002150
2151 domain = get_valid_domain_for_dev(pdev);
2152 if (!domain)
2153 return 0;
2154
Weidong Han8c11e792008-12-08 15:29:22 +08002155 iommu = domain_get_iommu(domain);
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002156 size = aligned_size((u64)paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002157
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002158 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002159 if (!iova)
2160 goto error;
2161
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002162 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002163
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002164 /*
2165 * Check if DMAR supports zero-length reads on write only
2166 * mappings..
2167 */
2168 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002169 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002170 prot |= DMA_PTE_READ;
2171 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2172 prot |= DMA_PTE_WRITE;
2173 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002174 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002175 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002176 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002177 * is not a big problem
2178 */
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002179 ret = domain_page_mapping(domain, start_paddr,
David Woodhousefd18de52009-05-10 23:57:41 +01002180 ((u64)paddr) & PHYSICAL_PAGE_MASK,
2181 size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002182 if (ret)
2183 goto error;
2184
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002185 /* it's a non-present to present mapping */
Weidong Han8c11e792008-12-08 15:29:22 +08002186 ret = iommu_flush_iotlb_psi(iommu, domain->id,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002187 start_paddr, size >> VTD_PAGE_SHIFT, 1);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002188 if (ret)
Weidong Han8c11e792008-12-08 15:29:22 +08002189 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002190
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002191 return start_paddr + ((u64)paddr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002192
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002193error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002194 if (iova)
2195 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002196 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002197 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002198 return 0;
2199}
2200
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002201static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2202 unsigned long offset, size_t size,
2203 enum dma_data_direction dir,
2204 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002205{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002206 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2207 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002208}
2209
mark gross5e0d2a62008-03-04 15:22:08 -08002210static void flush_unmaps(void)
2211{
mark gross80b20dd2008-04-18 13:53:58 -07002212 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002213
mark gross5e0d2a62008-03-04 15:22:08 -08002214 timer_on = 0;
2215
2216 /* just flush them all */
2217 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002218 struct intel_iommu *iommu = g_iommus[i];
2219 if (!iommu)
2220 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002221
Weidong Hana2bb8452008-12-08 11:24:12 +08002222 if (deferred_flush[i].next) {
Youquan Songa77b67d2008-10-16 16:31:56 -07002223 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2224 DMA_TLB_GLOBAL_FLUSH, 0);
mark gross80b20dd2008-04-18 13:53:58 -07002225 for (j = 0; j < deferred_flush[i].next; j++) {
2226 __free_iova(&deferred_flush[i].domain[j]->iovad,
2227 deferred_flush[i].iova[j]);
2228 }
2229 deferred_flush[i].next = 0;
2230 }
mark gross5e0d2a62008-03-04 15:22:08 -08002231 }
2232
mark gross5e0d2a62008-03-04 15:22:08 -08002233 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002234}
2235
2236static void flush_unmaps_timeout(unsigned long data)
2237{
mark gross80b20dd2008-04-18 13:53:58 -07002238 unsigned long flags;
2239
2240 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002241 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002242 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002243}
2244
2245static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2246{
2247 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002248 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002249 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002250
2251 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002252 if (list_size == HIGH_WATER_MARK)
2253 flush_unmaps();
2254
Weidong Han8c11e792008-12-08 15:29:22 +08002255 iommu = domain_get_iommu(dom);
2256 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002257
mark gross80b20dd2008-04-18 13:53:58 -07002258 next = deferred_flush[iommu_id].next;
2259 deferred_flush[iommu_id].domain[next] = dom;
2260 deferred_flush[iommu_id].iova[next] = iova;
2261 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002262
2263 if (!timer_on) {
2264 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2265 timer_on = 1;
2266 }
2267 list_size++;
2268 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2269}
2270
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002271static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2272 size_t size, enum dma_data_direction dir,
2273 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002274{
2275 struct pci_dev *pdev = to_pci_dev(dev);
2276 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002277 unsigned long start_addr;
2278 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002279 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002280
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002281 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002282 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002283 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002284 BUG_ON(!domain);
2285
Weidong Han8c11e792008-12-08 15:29:22 +08002286 iommu = domain_get_iommu(domain);
2287
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002288 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2289 if (!iova)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002290 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002291
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002292 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002293 size = aligned_size((u64)dev_addr, size);
2294
David Woodhouse4cf2e752009-02-11 17:23:43 +00002295 pr_debug("Device %s unmapping: %zx@%llx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002296 pci_name(pdev), size, (unsigned long long)start_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002297
2298 /* clear the whole page */
2299 dma_pte_clear_range(domain, start_addr, start_addr + size);
2300 /* free page tables */
2301 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
mark gross5e0d2a62008-03-04 15:22:08 -08002302 if (intel_iommu_strict) {
Weidong Han8c11e792008-12-08 15:29:22 +08002303 if (iommu_flush_iotlb_psi(iommu,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002304 domain->id, start_addr, size >> VTD_PAGE_SHIFT, 0))
Weidong Han8c11e792008-12-08 15:29:22 +08002305 iommu_flush_write_buffer(iommu);
mark gross5e0d2a62008-03-04 15:22:08 -08002306 /* free iova */
2307 __free_iova(&domain->iovad, iova);
2308 } else {
2309 add_unmap(domain, iova);
2310 /*
2311 * queue up the release of the unmap to save the 1/6th of the
2312 * cpu used up by the iotlb flush operation...
2313 */
mark gross5e0d2a62008-03-04 15:22:08 -08002314 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002315}
2316
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002317static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2318 int dir)
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002319{
2320 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2321}
2322
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002323static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2324 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002325{
2326 void *vaddr;
2327 int order;
2328
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002329 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002330 order = get_order(size);
2331 flags &= ~(GFP_DMA | GFP_DMA32);
2332
2333 vaddr = (void *)__get_free_pages(flags, order);
2334 if (!vaddr)
2335 return NULL;
2336 memset(vaddr, 0, size);
2337
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002338 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2339 DMA_BIDIRECTIONAL,
2340 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002341 if (*dma_handle)
2342 return vaddr;
2343 free_pages((unsigned long)vaddr, order);
2344 return NULL;
2345}
2346
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002347static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2348 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002349{
2350 int order;
2351
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002352 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002353 order = get_order(size);
2354
2355 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2356 free_pages((unsigned long)vaddr, order);
2357}
2358
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002359static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2360 int nelems, enum dma_data_direction dir,
2361 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002362{
2363 int i;
2364 struct pci_dev *pdev = to_pci_dev(hwdev);
2365 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002366 unsigned long start_addr;
2367 struct iova *iova;
2368 size_t size = 0;
David Woodhouse4cf2e752009-02-11 17:23:43 +00002369 phys_addr_t addr;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002370 struct scatterlist *sg;
Weidong Han8c11e792008-12-08 15:29:22 +08002371 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002372
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002373 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002374 return;
2375
2376 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002377 BUG_ON(!domain);
2378
2379 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002380
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002381 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002382 if (!iova)
2383 return;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002384 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002385 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002386 size += aligned_size((u64)addr, sg->length);
2387 }
2388
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002389 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002390
2391 /* clear the whole page */
2392 dma_pte_clear_range(domain, start_addr, start_addr + size);
2393 /* free page tables */
2394 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2395
Weidong Han8c11e792008-12-08 15:29:22 +08002396 if (iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002397 size >> VTD_PAGE_SHIFT, 0))
Weidong Han8c11e792008-12-08 15:29:22 +08002398 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002399
2400 /* free iova */
2401 __free_iova(&domain->iovad, iova);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002402}
2403
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002404static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002405 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002406{
2407 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002408 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002409
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002410 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002411 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002412 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002413 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002414 }
2415 return nelems;
2416}
2417
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002418static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2419 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002420{
David Woodhouse4cf2e752009-02-11 17:23:43 +00002421 phys_addr_t addr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002422 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002423 struct pci_dev *pdev = to_pci_dev(hwdev);
2424 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002425 size_t size = 0;
2426 int prot = 0;
2427 size_t offset = 0;
2428 struct iova *iova = NULL;
2429 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002430 struct scatterlist *sg;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002431 unsigned long start_addr;
Weidong Han8c11e792008-12-08 15:29:22 +08002432 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002433
2434 BUG_ON(dir == DMA_NONE);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002435 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002436 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002437
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002438 domain = get_valid_domain_for_dev(pdev);
2439 if (!domain)
2440 return 0;
2441
Weidong Han8c11e792008-12-08 15:29:22 +08002442 iommu = domain_get_iommu(domain);
2443
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002444 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002445 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002446 size += aligned_size((u64)addr, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002447 }
2448
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002449 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002450 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002451 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002452 return 0;
2453 }
2454
2455 /*
2456 * Check if DMAR supports zero-length reads on write only
2457 * mappings..
2458 */
2459 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002460 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002461 prot |= DMA_PTE_READ;
2462 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2463 prot |= DMA_PTE_WRITE;
2464
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002465 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002466 offset = 0;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002467 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002468 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002469 size = aligned_size((u64)addr, sg->length);
2470 ret = domain_page_mapping(domain, start_addr + offset,
David Woodhousefd18de52009-05-10 23:57:41 +01002471 ((u64)addr) & PHYSICAL_PAGE_MASK,
2472 size, prot);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002473 if (ret) {
2474 /* clear the page */
2475 dma_pte_clear_range(domain, start_addr,
2476 start_addr + offset);
2477 /* free page tables */
2478 dma_pte_free_pagetable(domain, start_addr,
2479 start_addr + offset);
2480 /* free iova */
2481 __free_iova(&domain->iovad, iova);
2482 return 0;
2483 }
2484 sg->dma_address = start_addr + offset +
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002485 ((u64)addr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002486 sg->dma_length = sg->length;
2487 offset += size;
2488 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002489
2490 /* it's a non-present to present mapping */
Weidong Han8c11e792008-12-08 15:29:22 +08002491 if (iommu_flush_iotlb_psi(iommu, domain->id,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002492 start_addr, offset >> VTD_PAGE_SHIFT, 1))
Weidong Han8c11e792008-12-08 15:29:22 +08002493 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002494 return nelems;
2495}
2496
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002497static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2498{
2499 return !dma_addr;
2500}
2501
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002502struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002503 .alloc_coherent = intel_alloc_coherent,
2504 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002505 .map_sg = intel_map_sg,
2506 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002507 .map_page = intel_map_page,
2508 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002509 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002510};
2511
2512static inline int iommu_domain_cache_init(void)
2513{
2514 int ret = 0;
2515
2516 iommu_domain_cache = kmem_cache_create("iommu_domain",
2517 sizeof(struct dmar_domain),
2518 0,
2519 SLAB_HWCACHE_ALIGN,
2520
2521 NULL);
2522 if (!iommu_domain_cache) {
2523 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2524 ret = -ENOMEM;
2525 }
2526
2527 return ret;
2528}
2529
2530static inline int iommu_devinfo_cache_init(void)
2531{
2532 int ret = 0;
2533
2534 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2535 sizeof(struct device_domain_info),
2536 0,
2537 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002538 NULL);
2539 if (!iommu_devinfo_cache) {
2540 printk(KERN_ERR "Couldn't create devinfo cache\n");
2541 ret = -ENOMEM;
2542 }
2543
2544 return ret;
2545}
2546
2547static inline int iommu_iova_cache_init(void)
2548{
2549 int ret = 0;
2550
2551 iommu_iova_cache = kmem_cache_create("iommu_iova",
2552 sizeof(struct iova),
2553 0,
2554 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002555 NULL);
2556 if (!iommu_iova_cache) {
2557 printk(KERN_ERR "Couldn't create iova cache\n");
2558 ret = -ENOMEM;
2559 }
2560
2561 return ret;
2562}
2563
2564static int __init iommu_init_mempool(void)
2565{
2566 int ret;
2567 ret = iommu_iova_cache_init();
2568 if (ret)
2569 return ret;
2570
2571 ret = iommu_domain_cache_init();
2572 if (ret)
2573 goto domain_error;
2574
2575 ret = iommu_devinfo_cache_init();
2576 if (!ret)
2577 return ret;
2578
2579 kmem_cache_destroy(iommu_domain_cache);
2580domain_error:
2581 kmem_cache_destroy(iommu_iova_cache);
2582
2583 return -ENOMEM;
2584}
2585
2586static void __init iommu_exit_mempool(void)
2587{
2588 kmem_cache_destroy(iommu_devinfo_cache);
2589 kmem_cache_destroy(iommu_domain_cache);
2590 kmem_cache_destroy(iommu_iova_cache);
2591
2592}
2593
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002594static void __init init_no_remapping_devices(void)
2595{
2596 struct dmar_drhd_unit *drhd;
2597
2598 for_each_drhd_unit(drhd) {
2599 if (!drhd->include_all) {
2600 int i;
2601 for (i = 0; i < drhd->devices_cnt; i++)
2602 if (drhd->devices[i] != NULL)
2603 break;
2604 /* ignore DMAR unit if no pci devices exist */
2605 if (i == drhd->devices_cnt)
2606 drhd->ignored = 1;
2607 }
2608 }
2609
2610 if (dmar_map_gfx)
2611 return;
2612
2613 for_each_drhd_unit(drhd) {
2614 int i;
2615 if (drhd->ignored || drhd->include_all)
2616 continue;
2617
2618 for (i = 0; i < drhd->devices_cnt; i++)
2619 if (drhd->devices[i] &&
2620 !IS_GFX_DEVICE(drhd->devices[i]))
2621 break;
2622
2623 if (i < drhd->devices_cnt)
2624 continue;
2625
2626 /* bypass IOMMU if it is just for gfx devices */
2627 drhd->ignored = 1;
2628 for (i = 0; i < drhd->devices_cnt; i++) {
2629 if (!drhd->devices[i])
2630 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002631 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002632 }
2633 }
2634}
2635
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002636#ifdef CONFIG_SUSPEND
2637static int init_iommu_hw(void)
2638{
2639 struct dmar_drhd_unit *drhd;
2640 struct intel_iommu *iommu = NULL;
2641
2642 for_each_active_iommu(iommu, drhd)
2643 if (iommu->qi)
2644 dmar_reenable_qi(iommu);
2645
2646 for_each_active_iommu(iommu, drhd) {
2647 iommu_flush_write_buffer(iommu);
2648
2649 iommu_set_root_entry(iommu);
2650
2651 iommu->flush.flush_context(iommu, 0, 0, 0,
2652 DMA_CCMD_GLOBAL_INVL, 0);
2653 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2654 DMA_TLB_GLOBAL_FLUSH, 0);
2655 iommu_disable_protect_mem_regions(iommu);
2656 iommu_enable_translation(iommu);
2657 }
2658
2659 return 0;
2660}
2661
2662static void iommu_flush_all(void)
2663{
2664 struct dmar_drhd_unit *drhd;
2665 struct intel_iommu *iommu;
2666
2667 for_each_active_iommu(iommu, drhd) {
2668 iommu->flush.flush_context(iommu, 0, 0, 0,
2669 DMA_CCMD_GLOBAL_INVL, 0);
2670 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2671 DMA_TLB_GLOBAL_FLUSH, 0);
2672 }
2673}
2674
2675static int iommu_suspend(struct sys_device *dev, pm_message_t state)
2676{
2677 struct dmar_drhd_unit *drhd;
2678 struct intel_iommu *iommu = NULL;
2679 unsigned long flag;
2680
2681 for_each_active_iommu(iommu, drhd) {
2682 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
2683 GFP_ATOMIC);
2684 if (!iommu->iommu_state)
2685 goto nomem;
2686 }
2687
2688 iommu_flush_all();
2689
2690 for_each_active_iommu(iommu, drhd) {
2691 iommu_disable_translation(iommu);
2692
2693 spin_lock_irqsave(&iommu->register_lock, flag);
2694
2695 iommu->iommu_state[SR_DMAR_FECTL_REG] =
2696 readl(iommu->reg + DMAR_FECTL_REG);
2697 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
2698 readl(iommu->reg + DMAR_FEDATA_REG);
2699 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
2700 readl(iommu->reg + DMAR_FEADDR_REG);
2701 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
2702 readl(iommu->reg + DMAR_FEUADDR_REG);
2703
2704 spin_unlock_irqrestore(&iommu->register_lock, flag);
2705 }
2706 return 0;
2707
2708nomem:
2709 for_each_active_iommu(iommu, drhd)
2710 kfree(iommu->iommu_state);
2711
2712 return -ENOMEM;
2713}
2714
2715static int iommu_resume(struct sys_device *dev)
2716{
2717 struct dmar_drhd_unit *drhd;
2718 struct intel_iommu *iommu = NULL;
2719 unsigned long flag;
2720
2721 if (init_iommu_hw()) {
2722 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
2723 return -EIO;
2724 }
2725
2726 for_each_active_iommu(iommu, drhd) {
2727
2728 spin_lock_irqsave(&iommu->register_lock, flag);
2729
2730 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
2731 iommu->reg + DMAR_FECTL_REG);
2732 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
2733 iommu->reg + DMAR_FEDATA_REG);
2734 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
2735 iommu->reg + DMAR_FEADDR_REG);
2736 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
2737 iommu->reg + DMAR_FEUADDR_REG);
2738
2739 spin_unlock_irqrestore(&iommu->register_lock, flag);
2740 }
2741
2742 for_each_active_iommu(iommu, drhd)
2743 kfree(iommu->iommu_state);
2744
2745 return 0;
2746}
2747
2748static struct sysdev_class iommu_sysclass = {
2749 .name = "iommu",
2750 .resume = iommu_resume,
2751 .suspend = iommu_suspend,
2752};
2753
2754static struct sys_device device_iommu = {
2755 .cls = &iommu_sysclass,
2756};
2757
2758static int __init init_iommu_sysfs(void)
2759{
2760 int error;
2761
2762 error = sysdev_class_register(&iommu_sysclass);
2763 if (error)
2764 return error;
2765
2766 error = sysdev_register(&device_iommu);
2767 if (error)
2768 sysdev_class_unregister(&iommu_sysclass);
2769
2770 return error;
2771}
2772
2773#else
2774static int __init init_iommu_sysfs(void)
2775{
2776 return 0;
2777}
2778#endif /* CONFIG_PM */
2779
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002780int __init intel_iommu_init(void)
2781{
2782 int ret = 0;
2783
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002784 if (dmar_table_init())
2785 return -ENODEV;
2786
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002787 if (dmar_dev_scope_init())
2788 return -ENODEV;
2789
Suresh Siddha2ae21012008-07-10 11:16:43 -07002790 /*
2791 * Check the need for DMA-remapping initialization now.
2792 * Above initialization will also be used by Interrupt-remapping.
2793 */
2794 if (no_iommu || swiotlb || dmar_disabled)
2795 return -ENODEV;
2796
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002797 iommu_init_mempool();
2798 dmar_init_reserved_ranges();
2799
2800 init_no_remapping_devices();
2801
2802 ret = init_dmars();
2803 if (ret) {
2804 printk(KERN_ERR "IOMMU: dmar init failed\n");
2805 put_iova_domain(&reserved_iova_list);
2806 iommu_exit_mempool();
2807 return ret;
2808 }
2809 printk(KERN_INFO
2810 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
2811
mark gross5e0d2a62008-03-04 15:22:08 -08002812 init_timer(&unmap_timer);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002813 force_iommu = 1;
2814 dma_ops = &intel_dma_ops;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002815 init_iommu_sysfs();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01002816
2817 register_iommu(&intel_iommu_ops);
2818
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002819 return 0;
2820}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002821
Weidong Hanc7151a82008-12-08 22:51:37 +08002822static int vm_domain_add_dev_info(struct dmar_domain *domain,
2823 struct pci_dev *pdev)
2824{
2825 struct device_domain_info *info;
2826 unsigned long flags;
2827
2828 info = alloc_devinfo_mem();
2829 if (!info)
2830 return -ENOMEM;
2831
David Woodhouse276dbf992009-04-04 01:45:37 +01002832 info->segment = pci_domain_nr(pdev->bus);
Weidong Hanc7151a82008-12-08 22:51:37 +08002833 info->bus = pdev->bus->number;
2834 info->devfn = pdev->devfn;
2835 info->dev = pdev;
2836 info->domain = domain;
2837
2838 spin_lock_irqsave(&device_domain_lock, flags);
2839 list_add(&info->link, &domain->devices);
2840 list_add(&info->global, &device_domain_list);
2841 pdev->dev.archdata.iommu = info;
2842 spin_unlock_irqrestore(&device_domain_lock, flags);
2843
2844 return 0;
2845}
2846
Han, Weidong3199aa62009-02-26 17:31:12 +08002847static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
2848 struct pci_dev *pdev)
2849{
2850 struct pci_dev *tmp, *parent;
2851
2852 if (!iommu || !pdev)
2853 return;
2854
2855 /* dependent device detach */
2856 tmp = pci_find_upstream_pcie_bridge(pdev);
2857 /* Secondary interface's bus number and devfn 0 */
2858 if (tmp) {
2859 parent = pdev->bus->self;
2860 while (parent != tmp) {
2861 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01002862 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002863 parent = parent->bus->self;
2864 }
2865 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
2866 iommu_detach_dev(iommu,
2867 tmp->subordinate->number, 0);
2868 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01002869 iommu_detach_dev(iommu, tmp->bus->number,
2870 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002871 }
2872}
2873
Weidong Hanc7151a82008-12-08 22:51:37 +08002874static void vm_domain_remove_one_dev_info(struct dmar_domain *domain,
2875 struct pci_dev *pdev)
2876{
2877 struct device_domain_info *info;
2878 struct intel_iommu *iommu;
2879 unsigned long flags;
2880 int found = 0;
2881 struct list_head *entry, *tmp;
2882
David Woodhouse276dbf992009-04-04 01:45:37 +01002883 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
2884 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08002885 if (!iommu)
2886 return;
2887
2888 spin_lock_irqsave(&device_domain_lock, flags);
2889 list_for_each_safe(entry, tmp, &domain->devices) {
2890 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01002891 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08002892 if (info->bus == pdev->bus->number &&
2893 info->devfn == pdev->devfn) {
2894 list_del(&info->link);
2895 list_del(&info->global);
2896 if (info->dev)
2897 info->dev->dev.archdata.iommu = NULL;
2898 spin_unlock_irqrestore(&device_domain_lock, flags);
2899
2900 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002901 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08002902 free_devinfo_mem(info);
2903
2904 spin_lock_irqsave(&device_domain_lock, flags);
2905
2906 if (found)
2907 break;
2908 else
2909 continue;
2910 }
2911
2912 /* if there is no other devices under the same iommu
2913 * owned by this domain, clear this iommu in iommu_bmp
2914 * update iommu count and coherency
2915 */
David Woodhouse276dbf992009-04-04 01:45:37 +01002916 if (iommu == device_to_iommu(info->segment, info->bus,
2917 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08002918 found = 1;
2919 }
2920
2921 if (found == 0) {
2922 unsigned long tmp_flags;
2923 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
2924 clear_bit(iommu->seq_id, &domain->iommu_bmp);
2925 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08002926 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08002927 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
2928 }
2929
2930 spin_unlock_irqrestore(&device_domain_lock, flags);
2931}
2932
2933static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
2934{
2935 struct device_domain_info *info;
2936 struct intel_iommu *iommu;
2937 unsigned long flags1, flags2;
2938
2939 spin_lock_irqsave(&device_domain_lock, flags1);
2940 while (!list_empty(&domain->devices)) {
2941 info = list_entry(domain->devices.next,
2942 struct device_domain_info, link);
2943 list_del(&info->link);
2944 list_del(&info->global);
2945 if (info->dev)
2946 info->dev->dev.archdata.iommu = NULL;
2947
2948 spin_unlock_irqrestore(&device_domain_lock, flags1);
2949
David Woodhouse276dbf992009-04-04 01:45:37 +01002950 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08002951 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002952 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08002953
2954 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08002955 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08002956 */
2957 spin_lock_irqsave(&domain->iommu_lock, flags2);
2958 if (test_and_clear_bit(iommu->seq_id,
2959 &domain->iommu_bmp)) {
2960 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08002961 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08002962 }
2963 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
2964
2965 free_devinfo_mem(info);
2966 spin_lock_irqsave(&device_domain_lock, flags1);
2967 }
2968 spin_unlock_irqrestore(&device_domain_lock, flags1);
2969}
2970
Weidong Han5e98c4b2008-12-08 23:03:27 +08002971/* domain id for virtual machine, it won't be set in context */
2972static unsigned long vm_domid;
2973
Weidong Hanfe40f1e2008-12-08 23:10:23 +08002974static int vm_domain_min_agaw(struct dmar_domain *domain)
2975{
2976 int i;
2977 int min_agaw = domain->agaw;
2978
2979 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
2980 for (; i < g_num_of_iommus; ) {
2981 if (min_agaw > g_iommus[i]->agaw)
2982 min_agaw = g_iommus[i]->agaw;
2983
2984 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
2985 }
2986
2987 return min_agaw;
2988}
2989
Weidong Han5e98c4b2008-12-08 23:03:27 +08002990static struct dmar_domain *iommu_alloc_vm_domain(void)
2991{
2992 struct dmar_domain *domain;
2993
2994 domain = alloc_domain_mem();
2995 if (!domain)
2996 return NULL;
2997
2998 domain->id = vm_domid++;
2999 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3000 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3001
3002 return domain;
3003}
3004
3005static int vm_domain_init(struct dmar_domain *domain, int guest_width)
3006{
3007 int adjust_width;
3008
3009 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3010 spin_lock_init(&domain->mapping_lock);
3011 spin_lock_init(&domain->iommu_lock);
3012
3013 domain_reserve_special_ranges(domain);
3014
3015 /* calculate AGAW */
3016 domain->gaw = guest_width;
3017 adjust_width = guestwidth_to_adjustwidth(guest_width);
3018 domain->agaw = width_to_agaw(adjust_width);
3019
3020 INIT_LIST_HEAD(&domain->devices);
3021
3022 domain->iommu_count = 0;
3023 domain->iommu_coherency = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003024 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003025
3026 /* always allocate the top pgd */
3027 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3028 if (!domain->pgd)
3029 return -ENOMEM;
3030 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3031 return 0;
3032}
3033
3034static void iommu_free_vm_domain(struct dmar_domain *domain)
3035{
3036 unsigned long flags;
3037 struct dmar_drhd_unit *drhd;
3038 struct intel_iommu *iommu;
3039 unsigned long i;
3040 unsigned long ndomains;
3041
3042 for_each_drhd_unit(drhd) {
3043 if (drhd->ignored)
3044 continue;
3045 iommu = drhd->iommu;
3046
3047 ndomains = cap_ndoms(iommu->cap);
3048 i = find_first_bit(iommu->domain_ids, ndomains);
3049 for (; i < ndomains; ) {
3050 if (iommu->domains[i] == domain) {
3051 spin_lock_irqsave(&iommu->lock, flags);
3052 clear_bit(i, iommu->domain_ids);
3053 iommu->domains[i] = NULL;
3054 spin_unlock_irqrestore(&iommu->lock, flags);
3055 break;
3056 }
3057 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3058 }
3059 }
3060}
3061
3062static void vm_domain_exit(struct dmar_domain *domain)
3063{
3064 u64 end;
3065
3066 /* Domain 0 is reserved, so dont process it */
3067 if (!domain)
3068 return;
3069
3070 vm_domain_remove_all_dev_info(domain);
3071 /* destroy iovas */
3072 put_iova_domain(&domain->iovad);
3073 end = DOMAIN_MAX_ADDR(domain->gaw);
3074 end = end & (~VTD_PAGE_MASK);
3075
3076 /* clear ptes */
3077 dma_pte_clear_range(domain, 0, end);
3078
3079 /* free page tables */
3080 dma_pte_free_pagetable(domain, 0, end);
3081
3082 iommu_free_vm_domain(domain);
3083 free_domain_mem(domain);
3084}
3085
Joerg Roedel5d450802008-12-03 14:52:32 +01003086static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003087{
Joerg Roedel5d450802008-12-03 14:52:32 +01003088 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003089
Joerg Roedel5d450802008-12-03 14:52:32 +01003090 dmar_domain = iommu_alloc_vm_domain();
3091 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003092 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003093 "intel_iommu_domain_init: dmar_domain == NULL\n");
3094 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003095 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003096 if (vm_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003097 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003098 "intel_iommu_domain_init() failed\n");
3099 vm_domain_exit(dmar_domain);
3100 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003101 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003102 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003103
Joerg Roedel5d450802008-12-03 14:52:32 +01003104 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003105}
Kay, Allen M38717942008-09-09 18:37:29 +03003106
Joerg Roedel5d450802008-12-03 14:52:32 +01003107static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003108{
Joerg Roedel5d450802008-12-03 14:52:32 +01003109 struct dmar_domain *dmar_domain = domain->priv;
3110
3111 domain->priv = NULL;
3112 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003113}
Kay, Allen M38717942008-09-09 18:37:29 +03003114
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003115static int intel_iommu_attach_device(struct iommu_domain *domain,
3116 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003117{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003118 struct dmar_domain *dmar_domain = domain->priv;
3119 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003120 struct intel_iommu *iommu;
3121 int addr_width;
3122 u64 end;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003123 int ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003124
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003125 /* normally pdev is not mapped */
3126 if (unlikely(domain_context_mapped(pdev))) {
3127 struct dmar_domain *old_domain;
3128
3129 old_domain = find_domain(pdev);
3130 if (old_domain) {
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003131 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003132 vm_domain_remove_one_dev_info(old_domain, pdev);
3133 else
3134 domain_remove_dev_info(old_domain);
3135 }
3136 }
3137
David Woodhouse276dbf992009-04-04 01:45:37 +01003138 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3139 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003140 if (!iommu)
3141 return -ENODEV;
3142
3143 /* check if this iommu agaw is sufficient for max mapped address */
3144 addr_width = agaw_to_width(iommu->agaw);
3145 end = DOMAIN_MAX_ADDR(addr_width);
3146 end = end & VTD_PAGE_MASK;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003147 if (end < dmar_domain->max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003148 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3149 "sufficient for the mapped address (%llx)\n",
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003150 __func__, iommu->agaw, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003151 return -EFAULT;
3152 }
3153
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003154 ret = domain_context_mapping(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003155 if (ret)
3156 return ret;
3157
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003158 ret = vm_domain_add_dev_info(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003159 return ret;
3160}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003161
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003162static void intel_iommu_detach_device(struct iommu_domain *domain,
3163 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003164{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003165 struct dmar_domain *dmar_domain = domain->priv;
3166 struct pci_dev *pdev = to_pci_dev(dev);
3167
3168 vm_domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003169}
Kay, Allen M38717942008-09-09 18:37:29 +03003170
Joerg Roedeldde57a22008-12-03 15:04:09 +01003171static int intel_iommu_map_range(struct iommu_domain *domain,
3172 unsigned long iova, phys_addr_t hpa,
3173 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003174{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003175 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003176 u64 max_addr;
3177 int addr_width;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003178 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003179 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003180
Joerg Roedeldde57a22008-12-03 15:04:09 +01003181 if (iommu_prot & IOMMU_READ)
3182 prot |= DMA_PTE_READ;
3183 if (iommu_prot & IOMMU_WRITE)
3184 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08003185 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3186 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003187
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003188 max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003189 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003190 int min_agaw;
3191 u64 end;
3192
3193 /* check if minimum agaw is sufficient for mapped address */
Joerg Roedeldde57a22008-12-03 15:04:09 +01003194 min_agaw = vm_domain_min_agaw(dmar_domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003195 addr_width = agaw_to_width(min_agaw);
3196 end = DOMAIN_MAX_ADDR(addr_width);
3197 end = end & VTD_PAGE_MASK;
3198 if (end < max_addr) {
3199 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3200 "sufficient for the mapped address (%llx)\n",
3201 __func__, min_agaw, max_addr);
3202 return -EFAULT;
3203 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003204 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003205 }
3206
Joerg Roedeldde57a22008-12-03 15:04:09 +01003207 ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003208 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003209}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003210
Joerg Roedeldde57a22008-12-03 15:04:09 +01003211static void intel_iommu_unmap_range(struct iommu_domain *domain,
3212 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003213{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003214 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003215 dma_addr_t base;
3216
3217 /* The address might not be aligned */
3218 base = iova & VTD_PAGE_MASK;
3219 size = VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003220 dma_pte_clear_range(dmar_domain, base, base + size);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003221
Joerg Roedeldde57a22008-12-03 15:04:09 +01003222 if (dmar_domain->max_addr == base + size)
3223 dmar_domain->max_addr = base;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003224}
Kay, Allen M38717942008-09-09 18:37:29 +03003225
Joerg Roedeld14d6572008-12-03 15:06:57 +01003226static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3227 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003228{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003229 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003230 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003231 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003232
Joerg Roedeld14d6572008-12-03 15:06:57 +01003233 pte = addr_to_dma_pte(dmar_domain, iova);
Kay, Allen M38717942008-09-09 18:37:29 +03003234 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003235 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003236
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003237 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003238}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003239
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003240static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3241 unsigned long cap)
3242{
3243 struct dmar_domain *dmar_domain = domain->priv;
3244
3245 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3246 return dmar_domain->iommu_snooping;
3247
3248 return 0;
3249}
3250
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003251static struct iommu_ops intel_iommu_ops = {
3252 .domain_init = intel_iommu_domain_init,
3253 .domain_destroy = intel_iommu_domain_destroy,
3254 .attach_dev = intel_iommu_attach_device,
3255 .detach_dev = intel_iommu_detach_device,
3256 .map = intel_iommu_map_range,
3257 .unmap = intel_iommu_unmap_range,
3258 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003259 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003260};
David Woodhouse9af88142009-02-13 23:18:03 +00003261
3262static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3263{
3264 /*
3265 * Mobile 4 Series Chipset neglects to set RWBF capability,
3266 * but needs it:
3267 */
3268 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3269 rwbf_quirk = 1;
3270}
3271
3272DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);