blob: f47d04aced87065ab5b892c690971e4f85636dc7 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
2 * Copyright (c) 2006, Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
mark gross98bcef52008-02-23 15:23:35 -080017 * Copyright (C) 2006-2008 Intel Corporation
18 * Author: Ashok Raj <ashok.raj@intel.com>
19 * Author: Shaohua Li <shaohua.li@intel.com>
20 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Fenghua Yu5b6985c2008-10-16 18:02:32 -070021 * Author: Fenghua Yu <fenghua.yu@intel.com>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070022 */
23
24#include <linux/init.h>
25#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080026#include <linux/debugfs.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
mark gross5e0d2a62008-03-04 15:22:08 -080035#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030036#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010037#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030038#include <linux/intel-iommu.h>
Fenghua Yuf59c7b62009-03-27 14:22:42 -070039#include <linux/sysdev.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070040#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090041#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070042#include "pci.h"
43
Fenghua Yu5b6985c2008-10-16 18:02:32 -070044#define ROOT_SIZE VTD_PAGE_SIZE
45#define CONTEXT_SIZE VTD_PAGE_SIZE
46
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070047#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
48#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
49
50#define IOAPIC_RANGE_START (0xfee00000)
51#define IOAPIC_RANGE_END (0xfeefffff)
52#define IOVA_START_ADDR (0x1000)
53
54#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
55
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -070056#define MAX_AGAW_WIDTH 64
57
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070058#define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
59
Mark McLoughlinf27be032008-11-20 15:49:43 +000060#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070061#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070062#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080063
Weidong Hand9630fe2008-12-08 11:06:32 +080064/* global iommu list, set NULL for ignored DMAR units */
65static struct intel_iommu **g_iommus;
66
David Woodhouse9af88142009-02-13 23:18:03 +000067static int rwbf_quirk;
68
Mark McLoughlin46b08e12008-11-20 15:49:44 +000069/*
70 * 0: Present
71 * 1-11: Reserved
72 * 12-63: Context Ptr (12 - (haw-1))
73 * 64-127: Reserved
74 */
75struct root_entry {
76 u64 val;
77 u64 rsvd1;
78};
79#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
80static inline bool root_present(struct root_entry *root)
81{
82 return (root->val & 1);
83}
84static inline void set_root_present(struct root_entry *root)
85{
86 root->val |= 1;
87}
88static inline void set_root_value(struct root_entry *root, unsigned long value)
89{
90 root->val |= value & VTD_PAGE_MASK;
91}
92
93static inline struct context_entry *
94get_context_addr_from_root(struct root_entry *root)
95{
96 return (struct context_entry *)
97 (root_present(root)?phys_to_virt(
98 root->val & VTD_PAGE_MASK) :
99 NULL);
100}
101
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000102/*
103 * low 64 bits:
104 * 0: present
105 * 1: fault processing disable
106 * 2-3: translation type
107 * 12-63: address space root
108 * high 64 bits:
109 * 0-2: address width
110 * 3-6: aval
111 * 8-23: domain id
112 */
113struct context_entry {
114 u64 lo;
115 u64 hi;
116};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000117
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000118static inline bool context_present(struct context_entry *context)
119{
120 return (context->lo & 1);
121}
122static inline void context_set_present(struct context_entry *context)
123{
124 context->lo |= 1;
125}
126
127static inline void context_set_fault_enable(struct context_entry *context)
128{
129 context->lo &= (((u64)-1) << 2) | 1;
130}
131
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000132static inline void context_set_translation_type(struct context_entry *context,
133 unsigned long value)
134{
135 context->lo &= (((u64)-1) << 4) | 3;
136 context->lo |= (value & 3) << 2;
137}
138
139static inline void context_set_address_root(struct context_entry *context,
140 unsigned long value)
141{
142 context->lo |= value & VTD_PAGE_MASK;
143}
144
145static inline void context_set_address_width(struct context_entry *context,
146 unsigned long value)
147{
148 context->hi |= value & 7;
149}
150
151static inline void context_set_domain_id(struct context_entry *context,
152 unsigned long value)
153{
154 context->hi |= (value & ((1 << 16) - 1)) << 8;
155}
156
157static inline void context_clear_entry(struct context_entry *context)
158{
159 context->lo = 0;
160 context->hi = 0;
161}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000162
Mark McLoughlin622ba122008-11-20 15:49:46 +0000163/*
164 * 0: readable
165 * 1: writable
166 * 2-6: reserved
167 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800168 * 8-10: available
169 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000170 * 12-63: Host physcial address
171 */
172struct dma_pte {
173 u64 val;
174};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000175
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000176static inline void dma_clear_pte(struct dma_pte *pte)
177{
178 pte->val = 0;
179}
180
181static inline void dma_set_pte_readable(struct dma_pte *pte)
182{
183 pte->val |= DMA_PTE_READ;
184}
185
186static inline void dma_set_pte_writable(struct dma_pte *pte)
187{
188 pte->val |= DMA_PTE_WRITE;
189}
190
Sheng Yang9cf06692009-03-18 15:33:07 +0800191static inline void dma_set_pte_snp(struct dma_pte *pte)
192{
193 pte->val |= DMA_PTE_SNP;
194}
195
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000196static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
197{
198 pte->val = (pte->val & ~3) | (prot & 3);
199}
200
201static inline u64 dma_pte_addr(struct dma_pte *pte)
202{
203 return (pte->val & VTD_PAGE_MASK);
204}
205
206static inline void dma_set_pte_addr(struct dma_pte *pte, u64 addr)
207{
208 pte->val |= (addr & VTD_PAGE_MASK);
209}
210
211static inline bool dma_pte_present(struct dma_pte *pte)
212{
213 return (pte->val & 3) != 0;
214}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000215
Weidong Han3b5410e2008-12-08 09:17:15 +0800216/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100217#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800218
Weidong Han1ce28fe2008-12-08 16:35:39 +0800219/* domain represents a virtual machine, more than one devices
220 * across iommus may be owned in one domain, e.g. kvm guest.
221 */
222#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
223
Mark McLoughlin99126f72008-11-20 15:49:47 +0000224struct dmar_domain {
225 int id; /* domain id */
Weidong Han8c11e792008-12-08 15:29:22 +0800226 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000227
228 struct list_head devices; /* all devices' list */
229 struct iova_domain iovad; /* iova's that belong to this domain */
230
231 struct dma_pte *pgd; /* virtual address */
232 spinlock_t mapping_lock; /* page table lock */
233 int gaw; /* max guest address width */
234
235 /* adjusted guest address width, 0 is level 2 30-bit */
236 int agaw;
237
Weidong Han3b5410e2008-12-08 09:17:15 +0800238 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800239
240 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800241 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800242 int iommu_count; /* reference count of iommu */
243 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800244 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000245};
246
Mark McLoughlina647dac2008-11-20 15:49:48 +0000247/* PCI domain-device relationship */
248struct device_domain_info {
249 struct list_head link; /* link to domain siblings */
250 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100251 int segment; /* PCI domain */
252 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000253 u8 devfn; /* PCI devfn number */
254 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
255 struct dmar_domain *domain; /* pointer to domain */
256};
257
mark gross5e0d2a62008-03-04 15:22:08 -0800258static void flush_unmaps_timeout(unsigned long data);
259
260DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
261
mark gross80b20dd2008-04-18 13:53:58 -0700262#define HIGH_WATER_MARK 250
263struct deferred_flush_tables {
264 int next;
265 struct iova *iova[HIGH_WATER_MARK];
266 struct dmar_domain *domain[HIGH_WATER_MARK];
267};
268
269static struct deferred_flush_tables *deferred_flush;
270
mark gross5e0d2a62008-03-04 15:22:08 -0800271/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800272static int g_num_of_iommus;
273
274static DEFINE_SPINLOCK(async_umap_flush_lock);
275static LIST_HEAD(unmaps_to_do);
276
277static int timer_on;
278static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800279
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700280static void domain_remove_dev_info(struct dmar_domain *domain);
281
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800282#ifdef CONFIG_DMAR_DEFAULT_ON
283int dmar_disabled = 0;
284#else
285int dmar_disabled = 1;
286#endif /*CONFIG_DMAR_DEFAULT_ON*/
287
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700288static int __initdata dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700289static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800290static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700291
292#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
293static DEFINE_SPINLOCK(device_domain_lock);
294static LIST_HEAD(device_domain_list);
295
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100296static struct iommu_ops intel_iommu_ops;
297
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700298static int __init intel_iommu_setup(char *str)
299{
300 if (!str)
301 return -EINVAL;
302 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800303 if (!strncmp(str, "on", 2)) {
304 dmar_disabled = 0;
305 printk(KERN_INFO "Intel-IOMMU: enabled\n");
306 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700307 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800308 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700309 } else if (!strncmp(str, "igfx_off", 8)) {
310 dmar_map_gfx = 0;
311 printk(KERN_INFO
312 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700313 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800314 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700315 "Intel-IOMMU: Forcing DAC for PCI devices\n");
316 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800317 } else if (!strncmp(str, "strict", 6)) {
318 printk(KERN_INFO
319 "Intel-IOMMU: disable batched IOTLB flush\n");
320 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700321 }
322
323 str += strcspn(str, ",");
324 while (*str == ',')
325 str++;
326 }
327 return 0;
328}
329__setup("intel_iommu=", intel_iommu_setup);
330
331static struct kmem_cache *iommu_domain_cache;
332static struct kmem_cache *iommu_devinfo_cache;
333static struct kmem_cache *iommu_iova_cache;
334
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700335static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
336{
337 unsigned int flags;
338 void *vaddr;
339
340 /* trying to avoid low memory issues */
341 flags = current->flags & PF_MEMALLOC;
342 current->flags |= PF_MEMALLOC;
343 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
344 current->flags &= (~PF_MEMALLOC | flags);
345 return vaddr;
346}
347
348
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700349static inline void *alloc_pgtable_page(void)
350{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700351 unsigned int flags;
352 void *vaddr;
353
354 /* trying to avoid low memory issues */
355 flags = current->flags & PF_MEMALLOC;
356 current->flags |= PF_MEMALLOC;
357 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
358 current->flags &= (~PF_MEMALLOC | flags);
359 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700360}
361
362static inline void free_pgtable_page(void *vaddr)
363{
364 free_page((unsigned long)vaddr);
365}
366
367static inline void *alloc_domain_mem(void)
368{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700369 return iommu_kmem_cache_alloc(iommu_domain_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700370}
371
Kay, Allen M38717942008-09-09 18:37:29 +0300372static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700373{
374 kmem_cache_free(iommu_domain_cache, vaddr);
375}
376
377static inline void * alloc_devinfo_mem(void)
378{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700379 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700380}
381
382static inline void free_devinfo_mem(void *vaddr)
383{
384 kmem_cache_free(iommu_devinfo_cache, vaddr);
385}
386
387struct iova *alloc_iova_mem(void)
388{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700389 return iommu_kmem_cache_alloc(iommu_iova_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700390}
391
392void free_iova_mem(struct iova *iova)
393{
394 kmem_cache_free(iommu_iova_cache, iova);
395}
396
Weidong Han1b573682008-12-08 15:34:06 +0800397
398static inline int width_to_agaw(int width);
399
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700400static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800401{
402 unsigned long sagaw;
403 int agaw = -1;
404
405 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700406 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800407 agaw >= 0; agaw--) {
408 if (test_bit(agaw, &sagaw))
409 break;
410 }
411
412 return agaw;
413}
414
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700415/*
416 * Calculate max SAGAW for each iommu.
417 */
418int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
419{
420 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
421}
422
423/*
424 * calculate agaw for each iommu.
425 * "SAGAW" may be different across iommus, use a default agaw, and
426 * get a supported less agaw for iommus that don't support the default agaw.
427 */
428int iommu_calculate_agaw(struct intel_iommu *iommu)
429{
430 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
431}
432
Weidong Han8c11e792008-12-08 15:29:22 +0800433/* in native case, each domain is related to only one iommu */
434static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
435{
436 int iommu_id;
437
Weidong Han1ce28fe2008-12-08 16:35:39 +0800438 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
439
Weidong Han8c11e792008-12-08 15:29:22 +0800440 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
441 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
442 return NULL;
443
444 return g_iommus[iommu_id];
445}
446
Weidong Han8e6040972008-12-08 15:49:06 +0800447static void domain_update_iommu_coherency(struct dmar_domain *domain)
448{
449 int i;
450
451 domain->iommu_coherency = 1;
452
453 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
454 for (; i < g_num_of_iommus; ) {
455 if (!ecap_coherent(g_iommus[i]->ecap)) {
456 domain->iommu_coherency = 0;
457 break;
458 }
459 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
460 }
461}
462
Sheng Yang58c610b2009-03-18 15:33:05 +0800463static void domain_update_iommu_snooping(struct dmar_domain *domain)
464{
465 int i;
466
467 domain->iommu_snooping = 1;
468
469 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
470 for (; i < g_num_of_iommus; ) {
471 if (!ecap_sc_support(g_iommus[i]->ecap)) {
472 domain->iommu_snooping = 0;
473 break;
474 }
475 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
476 }
477}
478
479/* Some capabilities may be different across iommus */
480static void domain_update_iommu_cap(struct dmar_domain *domain)
481{
482 domain_update_iommu_coherency(domain);
483 domain_update_iommu_snooping(domain);
484}
485
David Woodhouse276dbf992009-04-04 01:45:37 +0100486static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800487{
488 struct dmar_drhd_unit *drhd = NULL;
489 int i;
490
491 for_each_drhd_unit(drhd) {
492 if (drhd->ignored)
493 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100494 if (segment != drhd->segment)
495 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800496
David Woodhouse924b6232009-04-04 00:39:25 +0100497 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000498 if (drhd->devices[i] &&
499 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800500 drhd->devices[i]->devfn == devfn)
501 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700502 if (drhd->devices[i] &&
503 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100504 drhd->devices[i]->subordinate->number <= bus &&
505 drhd->devices[i]->subordinate->subordinate >= bus)
506 return drhd->iommu;
507 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800508
509 if (drhd->include_all)
510 return drhd->iommu;
511 }
512
513 return NULL;
514}
515
Weidong Han5331fe62008-12-08 23:00:00 +0800516static void domain_flush_cache(struct dmar_domain *domain,
517 void *addr, int size)
518{
519 if (!domain->iommu_coherency)
520 clflush_cache_range(addr, size);
521}
522
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700523/* Gets context entry for a given bus and devfn */
524static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
525 u8 bus, u8 devfn)
526{
527 struct root_entry *root;
528 struct context_entry *context;
529 unsigned long phy_addr;
530 unsigned long flags;
531
532 spin_lock_irqsave(&iommu->lock, flags);
533 root = &iommu->root_entry[bus];
534 context = get_context_addr_from_root(root);
535 if (!context) {
536 context = (struct context_entry *)alloc_pgtable_page();
537 if (!context) {
538 spin_unlock_irqrestore(&iommu->lock, flags);
539 return NULL;
540 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700541 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700542 phy_addr = virt_to_phys((void *)context);
543 set_root_value(root, phy_addr);
544 set_root_present(root);
545 __iommu_flush_cache(iommu, root, sizeof(*root));
546 }
547 spin_unlock_irqrestore(&iommu->lock, flags);
548 return &context[devfn];
549}
550
551static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
552{
553 struct root_entry *root;
554 struct context_entry *context;
555 int ret;
556 unsigned long flags;
557
558 spin_lock_irqsave(&iommu->lock, flags);
559 root = &iommu->root_entry[bus];
560 context = get_context_addr_from_root(root);
561 if (!context) {
562 ret = 0;
563 goto out;
564 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000565 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700566out:
567 spin_unlock_irqrestore(&iommu->lock, flags);
568 return ret;
569}
570
571static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
572{
573 struct root_entry *root;
574 struct context_entry *context;
575 unsigned long flags;
576
577 spin_lock_irqsave(&iommu->lock, flags);
578 root = &iommu->root_entry[bus];
579 context = get_context_addr_from_root(root);
580 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000581 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700582 __iommu_flush_cache(iommu, &context[devfn], \
583 sizeof(*context));
584 }
585 spin_unlock_irqrestore(&iommu->lock, flags);
586}
587
588static void free_context_table(struct intel_iommu *iommu)
589{
590 struct root_entry *root;
591 int i;
592 unsigned long flags;
593 struct context_entry *context;
594
595 spin_lock_irqsave(&iommu->lock, flags);
596 if (!iommu->root_entry) {
597 goto out;
598 }
599 for (i = 0; i < ROOT_ENTRY_NR; i++) {
600 root = &iommu->root_entry[i];
601 context = get_context_addr_from_root(root);
602 if (context)
603 free_pgtable_page(context);
604 }
605 free_pgtable_page(iommu->root_entry);
606 iommu->root_entry = NULL;
607out:
608 spin_unlock_irqrestore(&iommu->lock, flags);
609}
610
611/* page table handling */
612#define LEVEL_STRIDE (9)
613#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
614
615static inline int agaw_to_level(int agaw)
616{
617 return agaw + 2;
618}
619
620static inline int agaw_to_width(int agaw)
621{
622 return 30 + agaw * LEVEL_STRIDE;
623
624}
625
626static inline int width_to_agaw(int width)
627{
628 return (width - 30) / LEVEL_STRIDE;
629}
630
631static inline unsigned int level_to_offset_bits(int level)
632{
633 return (12 + (level - 1) * LEVEL_STRIDE);
634}
635
636static inline int address_level_offset(u64 addr, int level)
637{
638 return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
639}
640
641static inline u64 level_mask(int level)
642{
643 return ((u64)-1 << level_to_offset_bits(level));
644}
645
646static inline u64 level_size(int level)
647{
648 return ((u64)1 << level_to_offset_bits(level));
649}
650
651static inline u64 align_to_level(u64 addr, int level)
652{
653 return ((addr + level_size(level) - 1) & level_mask(level));
654}
655
656static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
657{
658 int addr_width = agaw_to_width(domain->agaw);
659 struct dma_pte *parent, *pte = NULL;
660 int level = agaw_to_level(domain->agaw);
661 int offset;
662 unsigned long flags;
663
664 BUG_ON(!domain->pgd);
665
666 addr &= (((u64)1) << addr_width) - 1;
667 parent = domain->pgd;
668
669 spin_lock_irqsave(&domain->mapping_lock, flags);
670 while (level > 0) {
671 void *tmp_page;
672
673 offset = address_level_offset(addr, level);
674 pte = &parent[offset];
675 if (level == 1)
676 break;
677
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000678 if (!dma_pte_present(pte)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700679 tmp_page = alloc_pgtable_page();
680
681 if (!tmp_page) {
682 spin_unlock_irqrestore(&domain->mapping_lock,
683 flags);
684 return NULL;
685 }
Weidong Han5331fe62008-12-08 23:00:00 +0800686 domain_flush_cache(domain, tmp_page, PAGE_SIZE);
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000687 dma_set_pte_addr(pte, virt_to_phys(tmp_page));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700688 /*
689 * high level table always sets r/w, last level page
690 * table control read/write
691 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000692 dma_set_pte_readable(pte);
693 dma_set_pte_writable(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800694 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700695 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000696 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700697 level--;
698 }
699
700 spin_unlock_irqrestore(&domain->mapping_lock, flags);
701 return pte;
702}
703
704/* return address's pte at specific level */
705static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
706 int level)
707{
708 struct dma_pte *parent, *pte = NULL;
709 int total = agaw_to_level(domain->agaw);
710 int offset;
711
712 parent = domain->pgd;
713 while (level <= total) {
714 offset = address_level_offset(addr, total);
715 pte = &parent[offset];
716 if (level == total)
717 return pte;
718
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000719 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700720 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000721 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700722 total--;
723 }
724 return NULL;
725}
726
727/* clear one page's page table */
728static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
729{
730 struct dma_pte *pte = NULL;
731
732 /* get last level pte */
733 pte = dma_addr_level_pte(domain, addr, 1);
734
735 if (pte) {
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000736 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800737 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700738 }
739}
740
741/* clear last level pte, a tlb flush should be followed */
742static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
743{
744 int addr_width = agaw_to_width(domain->agaw);
Zhao, Yuafeeb7ce2009-02-13 17:55:49 +0800745 int npages;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700746
747 start &= (((u64)1) << addr_width) - 1;
748 end &= (((u64)1) << addr_width) - 1;
749 /* in case it's partial page */
Fenghua Yu31d35682009-04-06 11:21:49 -0700750 start &= PAGE_MASK;
751 end = PAGE_ALIGN(end);
Zhao, Yuafeeb7ce2009-02-13 17:55:49 +0800752 npages = (end - start) / VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700753
754 /* we don't need lock here, nobody else touches the iova range */
Zhao, Yuafeeb7ce2009-02-13 17:55:49 +0800755 while (npages--) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700756 dma_pte_clear_one(domain, start);
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700757 start += VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700758 }
759}
760
761/* free page table pages. last level pte should already be cleared */
762static void dma_pte_free_pagetable(struct dmar_domain *domain,
763 u64 start, u64 end)
764{
765 int addr_width = agaw_to_width(domain->agaw);
766 struct dma_pte *pte;
767 int total = agaw_to_level(domain->agaw);
768 int level;
769 u64 tmp;
770
771 start &= (((u64)1) << addr_width) - 1;
772 end &= (((u64)1) << addr_width) - 1;
773
774 /* we don't need lock here, nobody else touches the iova range */
775 level = 2;
776 while (level <= total) {
777 tmp = align_to_level(start, level);
778 if (tmp >= end || (tmp + level_size(level) > end))
779 return;
780
781 while (tmp < end) {
782 pte = dma_addr_level_pte(domain, tmp, level);
783 if (pte) {
784 free_pgtable_page(
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000785 phys_to_virt(dma_pte_addr(pte)));
786 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800787 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700788 }
789 tmp += level_size(level);
790 }
791 level++;
792 }
793 /* free pgd */
794 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
795 free_pgtable_page(domain->pgd);
796 domain->pgd = NULL;
797 }
798}
799
800/* iommu handling */
801static int iommu_alloc_root_entry(struct intel_iommu *iommu)
802{
803 struct root_entry *root;
804 unsigned long flags;
805
806 root = (struct root_entry *)alloc_pgtable_page();
807 if (!root)
808 return -ENOMEM;
809
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700810 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700811
812 spin_lock_irqsave(&iommu->lock, flags);
813 iommu->root_entry = root;
814 spin_unlock_irqrestore(&iommu->lock, flags);
815
816 return 0;
817}
818
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700819static void iommu_set_root_entry(struct intel_iommu *iommu)
820{
821 void *addr;
822 u32 cmd, sts;
823 unsigned long flag;
824
825 addr = iommu->root_entry;
826
827 spin_lock_irqsave(&iommu->register_lock, flag);
828 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
829
830 cmd = iommu->gcmd | DMA_GCMD_SRTP;
831 writel(cmd, iommu->reg + DMAR_GCMD_REG);
832
833 /* Make sure hardware complete it */
834 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
835 readl, (sts & DMA_GSTS_RTPS), sts);
836
837 spin_unlock_irqrestore(&iommu->register_lock, flag);
838}
839
840static void iommu_flush_write_buffer(struct intel_iommu *iommu)
841{
842 u32 val;
843 unsigned long flag;
844
David Woodhouse9af88142009-02-13 23:18:03 +0000845 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700846 return;
847 val = iommu->gcmd | DMA_GCMD_WBF;
848
849 spin_lock_irqsave(&iommu->register_lock, flag);
850 writel(val, iommu->reg + DMAR_GCMD_REG);
851
852 /* Make sure hardware complete it */
853 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
854 readl, (!(val & DMA_GSTS_WBFS)), val);
855
856 spin_unlock_irqrestore(&iommu->register_lock, flag);
857}
858
859/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +0100860static void __iommu_flush_context(struct intel_iommu *iommu,
861 u16 did, u16 source_id, u8 function_mask,
862 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700863{
864 u64 val = 0;
865 unsigned long flag;
866
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700867 switch (type) {
868 case DMA_CCMD_GLOBAL_INVL:
869 val = DMA_CCMD_GLOBAL_INVL;
870 break;
871 case DMA_CCMD_DOMAIN_INVL:
872 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
873 break;
874 case DMA_CCMD_DEVICE_INVL:
875 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
876 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
877 break;
878 default:
879 BUG();
880 }
881 val |= DMA_CCMD_ICC;
882
883 spin_lock_irqsave(&iommu->register_lock, flag);
884 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
885
886 /* Make sure hardware complete it */
887 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
888 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
889
890 spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700891}
892
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700893/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100894static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
895 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700896{
897 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
898 u64 val = 0, val_iva = 0;
899 unsigned long flag;
900
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700901 switch (type) {
902 case DMA_TLB_GLOBAL_FLUSH:
903 /* global flush doesn't need set IVA_REG */
904 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
905 break;
906 case DMA_TLB_DSI_FLUSH:
907 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
908 break;
909 case DMA_TLB_PSI_FLUSH:
910 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
911 /* Note: always flush non-leaf currently */
912 val_iva = size_order | addr;
913 break;
914 default:
915 BUG();
916 }
917 /* Note: set drain read/write */
918#if 0
919 /*
920 * This is probably to be super secure.. Looks like we can
921 * ignore it without any impact.
922 */
923 if (cap_read_drain(iommu->cap))
924 val |= DMA_TLB_READ_DRAIN;
925#endif
926 if (cap_write_drain(iommu->cap))
927 val |= DMA_TLB_WRITE_DRAIN;
928
929 spin_lock_irqsave(&iommu->register_lock, flag);
930 /* Note: Only uses first TLB reg currently */
931 if (val_iva)
932 dmar_writeq(iommu->reg + tlb_offset, val_iva);
933 dmar_writeq(iommu->reg + tlb_offset + 8, val);
934
935 /* Make sure hardware complete it */
936 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
937 dmar_readq, (!(val & DMA_TLB_IVT)), val);
938
939 spin_unlock_irqrestore(&iommu->register_lock, flag);
940
941 /* check IOTLB invalidation granularity */
942 if (DMA_TLB_IAIG(val) == 0)
943 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
944 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
945 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700946 (unsigned long long)DMA_TLB_IIRG(type),
947 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700948}
949
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100950static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
951 u64 addr, unsigned int pages)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700952{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700953 unsigned int mask;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700954
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700955 BUG_ON(addr & (~VTD_PAGE_MASK));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700956 BUG_ON(pages == 0);
957
958 /* Fallback to domain selective flush if no PSI support */
959 if (!cap_pgsel_inv(iommu->cap))
Youquan Songa77b67d2008-10-16 16:31:56 -0700960 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100961 DMA_TLB_DSI_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700962
963 /*
964 * PSI requires page size to be 2 ^ x, and the base address is naturally
965 * aligned to the size
966 */
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700967 mask = ilog2(__roundup_pow_of_two(pages));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700968 /* Fallback to domain selective flush if size is too big */
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700969 if (mask > cap_max_amask_val(iommu->cap))
Youquan Songa77b67d2008-10-16 16:31:56 -0700970 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100971 DMA_TLB_DSI_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700972
Youquan Songa77b67d2008-10-16 16:31:56 -0700973 return iommu->flush.flush_iotlb(iommu, did, addr, mask,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100974 DMA_TLB_PSI_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700975}
976
mark grossf8bab732008-02-08 04:18:38 -0800977static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
978{
979 u32 pmen;
980 unsigned long flags;
981
982 spin_lock_irqsave(&iommu->register_lock, flags);
983 pmen = readl(iommu->reg + DMAR_PMEN_REG);
984 pmen &= ~DMA_PMEN_EPM;
985 writel(pmen, iommu->reg + DMAR_PMEN_REG);
986
987 /* wait for the protected region status bit to clear */
988 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
989 readl, !(pmen & DMA_PMEN_PRS), pmen);
990
991 spin_unlock_irqrestore(&iommu->register_lock, flags);
992}
993
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700994static int iommu_enable_translation(struct intel_iommu *iommu)
995{
996 u32 sts;
997 unsigned long flags;
998
999 spin_lock_irqsave(&iommu->register_lock, flags);
1000 writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
1001
1002 /* Make sure hardware complete it */
1003 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1004 readl, (sts & DMA_GSTS_TES), sts);
1005
1006 iommu->gcmd |= DMA_GCMD_TE;
1007 spin_unlock_irqrestore(&iommu->register_lock, flags);
1008 return 0;
1009}
1010
1011static int iommu_disable_translation(struct intel_iommu *iommu)
1012{
1013 u32 sts;
1014 unsigned long flag;
1015
1016 spin_lock_irqsave(&iommu->register_lock, flag);
1017 iommu->gcmd &= ~DMA_GCMD_TE;
1018 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1019
1020 /* Make sure hardware complete it */
1021 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1022 readl, (!(sts & DMA_GSTS_TES)), sts);
1023
1024 spin_unlock_irqrestore(&iommu->register_lock, flag);
1025 return 0;
1026}
1027
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001028
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001029static int iommu_init_domains(struct intel_iommu *iommu)
1030{
1031 unsigned long ndomains;
1032 unsigned long nlongs;
1033
1034 ndomains = cap_ndoms(iommu->cap);
1035 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1036 nlongs = BITS_TO_LONGS(ndomains);
1037
1038 /* TBD: there might be 64K domains,
1039 * consider other allocation for future chip
1040 */
1041 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1042 if (!iommu->domain_ids) {
1043 printk(KERN_ERR "Allocating domain id array failed\n");
1044 return -ENOMEM;
1045 }
1046 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1047 GFP_KERNEL);
1048 if (!iommu->domains) {
1049 printk(KERN_ERR "Allocating domain array failed\n");
1050 kfree(iommu->domain_ids);
1051 return -ENOMEM;
1052 }
1053
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001054 spin_lock_init(&iommu->lock);
1055
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001056 /*
1057 * if Caching mode is set, then invalid translations are tagged
1058 * with domainid 0. Hence we need to pre-allocate it.
1059 */
1060 if (cap_caching_mode(iommu->cap))
1061 set_bit(0, iommu->domain_ids);
1062 return 0;
1063}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001064
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001065
1066static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001067static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001068
1069void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001070{
1071 struct dmar_domain *domain;
1072 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001073 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001074
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001075 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1076 for (; i < cap_ndoms(iommu->cap); ) {
1077 domain = iommu->domains[i];
1078 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001079
1080 spin_lock_irqsave(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001081 if (--domain->iommu_count == 0) {
1082 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1083 vm_domain_exit(domain);
1084 else
1085 domain_exit(domain);
1086 }
Weidong Hanc7151a82008-12-08 22:51:37 +08001087 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1088
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001089 i = find_next_bit(iommu->domain_ids,
1090 cap_ndoms(iommu->cap), i+1);
1091 }
1092
1093 if (iommu->gcmd & DMA_GCMD_TE)
1094 iommu_disable_translation(iommu);
1095
1096 if (iommu->irq) {
1097 set_irq_data(iommu->irq, NULL);
1098 /* This will mask the irq */
1099 free_irq(iommu->irq, iommu);
1100 destroy_irq(iommu->irq);
1101 }
1102
1103 kfree(iommu->domains);
1104 kfree(iommu->domain_ids);
1105
Weidong Hand9630fe2008-12-08 11:06:32 +08001106 g_iommus[iommu->seq_id] = NULL;
1107
1108 /* if all iommus are freed, free g_iommus */
1109 for (i = 0; i < g_num_of_iommus; i++) {
1110 if (g_iommus[i])
1111 break;
1112 }
1113
1114 if (i == g_num_of_iommus)
1115 kfree(g_iommus);
1116
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001117 /* free context mapping */
1118 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001119}
1120
1121static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
1122{
1123 unsigned long num;
1124 unsigned long ndomains;
1125 struct dmar_domain *domain;
1126 unsigned long flags;
1127
1128 domain = alloc_domain_mem();
1129 if (!domain)
1130 return NULL;
1131
1132 ndomains = cap_ndoms(iommu->cap);
1133
1134 spin_lock_irqsave(&iommu->lock, flags);
1135 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1136 if (num >= ndomains) {
1137 spin_unlock_irqrestore(&iommu->lock, flags);
1138 free_domain_mem(domain);
1139 printk(KERN_ERR "IOMMU: no free domain ids\n");
1140 return NULL;
1141 }
1142
1143 set_bit(num, iommu->domain_ids);
1144 domain->id = num;
Weidong Han8c11e792008-12-08 15:29:22 +08001145 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
1146 set_bit(iommu->seq_id, &domain->iommu_bmp);
Weidong Hand71a2f32008-12-07 21:13:41 +08001147 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001148 iommu->domains[num] = domain;
1149 spin_unlock_irqrestore(&iommu->lock, flags);
1150
1151 return domain;
1152}
1153
1154static void iommu_free_domain(struct dmar_domain *domain)
1155{
1156 unsigned long flags;
Weidong Han8c11e792008-12-08 15:29:22 +08001157 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001158
Weidong Han8c11e792008-12-08 15:29:22 +08001159 iommu = domain_get_iommu(domain);
1160
1161 spin_lock_irqsave(&iommu->lock, flags);
1162 clear_bit(domain->id, iommu->domain_ids);
1163 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001164}
1165
1166static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001167static struct lock_class_key reserved_alloc_key;
1168static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001169
1170static void dmar_init_reserved_ranges(void)
1171{
1172 struct pci_dev *pdev = NULL;
1173 struct iova *iova;
1174 int i;
1175 u64 addr, size;
1176
David Millerf6611972008-02-06 01:36:23 -08001177 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001178
Mark Gross8a443df2008-03-04 14:59:31 -08001179 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1180 &reserved_alloc_key);
1181 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1182 &reserved_rbtree_key);
1183
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001184 /* IOAPIC ranges shouldn't be accessed by DMA */
1185 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1186 IOVA_PFN(IOAPIC_RANGE_END));
1187 if (!iova)
1188 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1189
1190 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1191 for_each_pci_dev(pdev) {
1192 struct resource *r;
1193
1194 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1195 r = &pdev->resource[i];
1196 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1197 continue;
1198 addr = r->start;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001199 addr &= PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001200 size = r->end - addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001201 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001202 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1203 IOVA_PFN(size + addr) - 1);
1204 if (!iova)
1205 printk(KERN_ERR "Reserve iova failed\n");
1206 }
1207 }
1208
1209}
1210
1211static void domain_reserve_special_ranges(struct dmar_domain *domain)
1212{
1213 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1214}
1215
1216static inline int guestwidth_to_adjustwidth(int gaw)
1217{
1218 int agaw;
1219 int r = (gaw - 12) % 9;
1220
1221 if (r == 0)
1222 agaw = gaw;
1223 else
1224 agaw = gaw + 9 - r;
1225 if (agaw > 64)
1226 agaw = 64;
1227 return agaw;
1228}
1229
1230static int domain_init(struct dmar_domain *domain, int guest_width)
1231{
1232 struct intel_iommu *iommu;
1233 int adjust_width, agaw;
1234 unsigned long sagaw;
1235
David Millerf6611972008-02-06 01:36:23 -08001236 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001237 spin_lock_init(&domain->mapping_lock);
Weidong Hanc7151a82008-12-08 22:51:37 +08001238 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001239
1240 domain_reserve_special_ranges(domain);
1241
1242 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001243 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001244 if (guest_width > cap_mgaw(iommu->cap))
1245 guest_width = cap_mgaw(iommu->cap);
1246 domain->gaw = guest_width;
1247 adjust_width = guestwidth_to_adjustwidth(guest_width);
1248 agaw = width_to_agaw(adjust_width);
1249 sagaw = cap_sagaw(iommu->cap);
1250 if (!test_bit(agaw, &sagaw)) {
1251 /* hardware doesn't support it, choose a bigger one */
1252 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1253 agaw = find_next_bit(&sagaw, 5, agaw);
1254 if (agaw >= 5)
1255 return -ENODEV;
1256 }
1257 domain->agaw = agaw;
1258 INIT_LIST_HEAD(&domain->devices);
1259
Weidong Han8e6040972008-12-08 15:49:06 +08001260 if (ecap_coherent(iommu->ecap))
1261 domain->iommu_coherency = 1;
1262 else
1263 domain->iommu_coherency = 0;
1264
Sheng Yang58c610b2009-03-18 15:33:05 +08001265 if (ecap_sc_support(iommu->ecap))
1266 domain->iommu_snooping = 1;
1267 else
1268 domain->iommu_snooping = 0;
1269
Weidong Hanc7151a82008-12-08 22:51:37 +08001270 domain->iommu_count = 1;
1271
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001272 /* always allocate the top pgd */
1273 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1274 if (!domain->pgd)
1275 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001276 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001277 return 0;
1278}
1279
1280static void domain_exit(struct dmar_domain *domain)
1281{
1282 u64 end;
1283
1284 /* Domain 0 is reserved, so dont process it */
1285 if (!domain)
1286 return;
1287
1288 domain_remove_dev_info(domain);
1289 /* destroy iovas */
1290 put_iova_domain(&domain->iovad);
1291 end = DOMAIN_MAX_ADDR(domain->gaw);
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001292 end = end & (~PAGE_MASK);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001293
1294 /* clear ptes */
1295 dma_pte_clear_range(domain, 0, end);
1296
1297 /* free page tables */
1298 dma_pte_free_pagetable(domain, 0, end);
1299
1300 iommu_free_domain(domain);
1301 free_domain_mem(domain);
1302}
1303
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001304static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1305 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001306{
1307 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001308 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001309 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001310 struct dma_pte *pgd;
1311 unsigned long num;
1312 unsigned long ndomains;
1313 int id;
1314 int agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001315
1316 pr_debug("Set context mapping for %02x:%02x.%d\n",
1317 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001318
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001319 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001320 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1321 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001322
David Woodhouse276dbf992009-04-04 01:45:37 +01001323 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001324 if (!iommu)
1325 return -ENODEV;
1326
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001327 context = device_to_context_entry(iommu, bus, devfn);
1328 if (!context)
1329 return -ENOMEM;
1330 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001331 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001332 spin_unlock_irqrestore(&iommu->lock, flags);
1333 return 0;
1334 }
1335
Weidong Hanea6606b2008-12-08 23:08:15 +08001336 id = domain->id;
1337 pgd = domain->pgd;
1338
1339 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) {
1340 int found = 0;
1341
1342 /* find an available domain id for this device in iommu */
1343 ndomains = cap_ndoms(iommu->cap);
1344 num = find_first_bit(iommu->domain_ids, ndomains);
1345 for (; num < ndomains; ) {
1346 if (iommu->domains[num] == domain) {
1347 id = num;
1348 found = 1;
1349 break;
1350 }
1351 num = find_next_bit(iommu->domain_ids,
1352 cap_ndoms(iommu->cap), num+1);
1353 }
1354
1355 if (found == 0) {
1356 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1357 if (num >= ndomains) {
1358 spin_unlock_irqrestore(&iommu->lock, flags);
1359 printk(KERN_ERR "IOMMU: no free domain ids\n");
1360 return -EFAULT;
1361 }
1362
1363 set_bit(num, iommu->domain_ids);
1364 iommu->domains[num] = domain;
1365 id = num;
1366 }
1367
1368 /* Skip top levels of page tables for
1369 * iommu which has less agaw than default.
1370 */
1371 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1372 pgd = phys_to_virt(dma_pte_addr(pgd));
1373 if (!dma_pte_present(pgd)) {
1374 spin_unlock_irqrestore(&iommu->lock, flags);
1375 return -ENOMEM;
1376 }
1377 }
1378 }
1379
1380 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001381
1382 /*
1383 * In pass through mode, AW must be programmed to indicate the largest
1384 * AGAW value supported by hardware. And ASR is ignored by hardware.
1385 */
1386 if (likely(translation == CONTEXT_TT_MULTI_LEVEL)) {
1387 context_set_address_width(context, iommu->agaw);
1388 context_set_address_root(context, virt_to_phys(pgd));
1389 } else
1390 context_set_address_width(context, iommu->msagaw);
1391
1392 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001393 context_set_fault_enable(context);
1394 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001395 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001396
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001397 /*
1398 * It's a non-present to present mapping. If hardware doesn't cache
1399 * non-present entry we only need to flush the write-buffer. If the
1400 * _does_ cache non-present entries, then it does so in the special
1401 * domain #0, which we have to flush:
1402 */
1403 if (cap_caching_mode(iommu->cap)) {
1404 iommu->flush.flush_context(iommu, 0,
1405 (((u16)bus) << 8) | devfn,
1406 DMA_CCMD_MASK_NOBIT,
1407 DMA_CCMD_DEVICE_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001408 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001409 } else {
1410 iommu_flush_write_buffer(iommu);
1411 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001412 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001413
1414 spin_lock_irqsave(&domain->iommu_lock, flags);
1415 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1416 domain->iommu_count++;
Sheng Yang58c610b2009-03-18 15:33:05 +08001417 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001418 }
1419 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001420 return 0;
1421}
1422
1423static int
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001424domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1425 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001426{
1427 int ret;
1428 struct pci_dev *tmp, *parent;
1429
David Woodhouse276dbf992009-04-04 01:45:37 +01001430 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001431 pdev->bus->number, pdev->devfn,
1432 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001433 if (ret)
1434 return ret;
1435
1436 /* dependent device mapping */
1437 tmp = pci_find_upstream_pcie_bridge(pdev);
1438 if (!tmp)
1439 return 0;
1440 /* Secondary interface's bus number and devfn 0 */
1441 parent = pdev->bus->self;
1442 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001443 ret = domain_context_mapping_one(domain,
1444 pci_domain_nr(parent->bus),
1445 parent->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001446 parent->devfn, translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001447 if (ret)
1448 return ret;
1449 parent = parent->bus->self;
1450 }
1451 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1452 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001453 pci_domain_nr(tmp->subordinate),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001454 tmp->subordinate->number, 0,
1455 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001456 else /* this is a legacy PCI bridge */
1457 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001458 pci_domain_nr(tmp->bus),
1459 tmp->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001460 tmp->devfn,
1461 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001462}
1463
Weidong Han5331fe62008-12-08 23:00:00 +08001464static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001465{
1466 int ret;
1467 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001468 struct intel_iommu *iommu;
1469
David Woodhouse276dbf992009-04-04 01:45:37 +01001470 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1471 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001472 if (!iommu)
1473 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001474
David Woodhouse276dbf992009-04-04 01:45:37 +01001475 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001476 if (!ret)
1477 return ret;
1478 /* dependent device mapping */
1479 tmp = pci_find_upstream_pcie_bridge(pdev);
1480 if (!tmp)
1481 return ret;
1482 /* Secondary interface's bus number and devfn 0 */
1483 parent = pdev->bus->self;
1484 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001485 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001486 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001487 if (!ret)
1488 return ret;
1489 parent = parent->bus->self;
1490 }
1491 if (tmp->is_pcie)
David Woodhouse276dbf992009-04-04 01:45:37 +01001492 return device_context_mapped(iommu, tmp->subordinate->number,
1493 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001494 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001495 return device_context_mapped(iommu, tmp->bus->number,
1496 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001497}
1498
1499static int
1500domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1501 u64 hpa, size_t size, int prot)
1502{
1503 u64 start_pfn, end_pfn;
1504 struct dma_pte *pte;
1505 int index;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001506 int addr_width = agaw_to_width(domain->agaw);
1507
1508 hpa &= (((u64)1) << addr_width) - 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001509
1510 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1511 return -EINVAL;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001512 iova &= PAGE_MASK;
1513 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1514 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001515 index = 0;
1516 while (start_pfn < end_pfn) {
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001517 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001518 if (!pte)
1519 return -ENOMEM;
1520 /* We don't need lock here, nobody else
1521 * touches the iova range
1522 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001523 BUG_ON(dma_pte_addr(pte));
1524 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
1525 dma_set_pte_prot(pte, prot);
Sheng Yang9cf06692009-03-18 15:33:07 +08001526 if (prot & DMA_PTE_SNP)
1527 dma_set_pte_snp(pte);
Weidong Han5331fe62008-12-08 23:00:00 +08001528 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001529 start_pfn++;
1530 index++;
1531 }
1532 return 0;
1533}
1534
Weidong Hanc7151a82008-12-08 22:51:37 +08001535static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001536{
Weidong Hanc7151a82008-12-08 22:51:37 +08001537 if (!iommu)
1538 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001539
1540 clear_context_table(iommu, bus, devfn);
1541 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001542 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001543 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001544}
1545
1546static void domain_remove_dev_info(struct dmar_domain *domain)
1547{
1548 struct device_domain_info *info;
1549 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001550 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001551
1552 spin_lock_irqsave(&device_domain_lock, flags);
1553 while (!list_empty(&domain->devices)) {
1554 info = list_entry(domain->devices.next,
1555 struct device_domain_info, link);
1556 list_del(&info->link);
1557 list_del(&info->global);
1558 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001559 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001560 spin_unlock_irqrestore(&device_domain_lock, flags);
1561
David Woodhouse276dbf992009-04-04 01:45:37 +01001562 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001563 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001564 free_devinfo_mem(info);
1565
1566 spin_lock_irqsave(&device_domain_lock, flags);
1567 }
1568 spin_unlock_irqrestore(&device_domain_lock, flags);
1569}
1570
1571/*
1572 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001573 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001574 */
Kay, Allen M38717942008-09-09 18:37:29 +03001575static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001576find_domain(struct pci_dev *pdev)
1577{
1578 struct device_domain_info *info;
1579
1580 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001581 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001582 if (info)
1583 return info->domain;
1584 return NULL;
1585}
1586
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001587/* domain is initialized */
1588static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1589{
1590 struct dmar_domain *domain, *found = NULL;
1591 struct intel_iommu *iommu;
1592 struct dmar_drhd_unit *drhd;
1593 struct device_domain_info *info, *tmp;
1594 struct pci_dev *dev_tmp;
1595 unsigned long flags;
1596 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001597 int segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001598
1599 domain = find_domain(pdev);
1600 if (domain)
1601 return domain;
1602
David Woodhouse276dbf992009-04-04 01:45:37 +01001603 segment = pci_domain_nr(pdev->bus);
1604
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001605 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1606 if (dev_tmp) {
1607 if (dev_tmp->is_pcie) {
1608 bus = dev_tmp->subordinate->number;
1609 devfn = 0;
1610 } else {
1611 bus = dev_tmp->bus->number;
1612 devfn = dev_tmp->devfn;
1613 }
1614 spin_lock_irqsave(&device_domain_lock, flags);
1615 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001616 if (info->segment == segment &&
1617 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001618 found = info->domain;
1619 break;
1620 }
1621 }
1622 spin_unlock_irqrestore(&device_domain_lock, flags);
1623 /* pcie-pci bridge already has a domain, uses it */
1624 if (found) {
1625 domain = found;
1626 goto found_domain;
1627 }
1628 }
1629
1630 /* Allocate new domain for the device */
1631 drhd = dmar_find_matched_drhd_unit(pdev);
1632 if (!drhd) {
1633 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1634 pci_name(pdev));
1635 return NULL;
1636 }
1637 iommu = drhd->iommu;
1638
1639 domain = iommu_alloc_domain(iommu);
1640 if (!domain)
1641 goto error;
1642
1643 if (domain_init(domain, gaw)) {
1644 domain_exit(domain);
1645 goto error;
1646 }
1647
1648 /* register pcie-to-pci device */
1649 if (dev_tmp) {
1650 info = alloc_devinfo_mem();
1651 if (!info) {
1652 domain_exit(domain);
1653 goto error;
1654 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001655 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001656 info->bus = bus;
1657 info->devfn = devfn;
1658 info->dev = NULL;
1659 info->domain = domain;
1660 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001661 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001662
1663 /* pcie-to-pci bridge already has a domain, uses it */
1664 found = NULL;
1665 spin_lock_irqsave(&device_domain_lock, flags);
1666 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001667 if (tmp->segment == segment &&
1668 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001669 found = tmp->domain;
1670 break;
1671 }
1672 }
1673 if (found) {
1674 free_devinfo_mem(info);
1675 domain_exit(domain);
1676 domain = found;
1677 } else {
1678 list_add(&info->link, &domain->devices);
1679 list_add(&info->global, &device_domain_list);
1680 }
1681 spin_unlock_irqrestore(&device_domain_lock, flags);
1682 }
1683
1684found_domain:
1685 info = alloc_devinfo_mem();
1686 if (!info)
1687 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01001688 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001689 info->bus = pdev->bus->number;
1690 info->devfn = pdev->devfn;
1691 info->dev = pdev;
1692 info->domain = domain;
1693 spin_lock_irqsave(&device_domain_lock, flags);
1694 /* somebody is fast */
1695 found = find_domain(pdev);
1696 if (found != NULL) {
1697 spin_unlock_irqrestore(&device_domain_lock, flags);
1698 if (found != domain) {
1699 domain_exit(domain);
1700 domain = found;
1701 }
1702 free_devinfo_mem(info);
1703 return domain;
1704 }
1705 list_add(&info->link, &domain->devices);
1706 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001707 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001708 spin_unlock_irqrestore(&device_domain_lock, flags);
1709 return domain;
1710error:
1711 /* recheck it here, maybe others set it */
1712 return find_domain(pdev);
1713}
1714
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001715static int iommu_prepare_identity_map(struct pci_dev *pdev,
1716 unsigned long long start,
1717 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001718{
1719 struct dmar_domain *domain;
1720 unsigned long size;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001721 unsigned long long base;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001722 int ret;
1723
1724 printk(KERN_INFO
1725 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1726 pci_name(pdev), start, end);
1727 /* page table init */
1728 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1729 if (!domain)
1730 return -ENOMEM;
1731
1732 /* The address might not be aligned */
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001733 base = start & PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001734 size = end - base;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001735 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001736 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1737 IOVA_PFN(base + size) - 1)) {
1738 printk(KERN_ERR "IOMMU: reserve iova failed\n");
1739 ret = -ENOMEM;
1740 goto error;
1741 }
1742
1743 pr_debug("Mapping reserved region %lx@%llx for %s\n",
1744 size, base, pci_name(pdev));
1745 /*
1746 * RMRR range might have overlap with physical memory range,
1747 * clear it first
1748 */
1749 dma_pte_clear_range(domain, base, base + size);
1750
1751 ret = domain_page_mapping(domain, base, base, size,
1752 DMA_PTE_READ|DMA_PTE_WRITE);
1753 if (ret)
1754 goto error;
1755
1756 /* context entry init */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001757 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001758 if (!ret)
1759 return 0;
1760error:
1761 domain_exit(domain);
1762 return ret;
1763
1764}
1765
1766static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1767 struct pci_dev *pdev)
1768{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001769 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001770 return 0;
1771 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1772 rmrr->end_address + 1);
1773}
1774
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001775#ifdef CONFIG_DMAR_GFX_WA
Yinghai Lud52d53b2008-06-16 20:10:55 -07001776struct iommu_prepare_data {
1777 struct pci_dev *pdev;
1778 int ret;
1779};
1780
1781static int __init iommu_prepare_work_fn(unsigned long start_pfn,
1782 unsigned long end_pfn, void *datax)
1783{
1784 struct iommu_prepare_data *data;
1785
1786 data = (struct iommu_prepare_data *)datax;
1787
1788 data->ret = iommu_prepare_identity_map(data->pdev,
1789 start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
1790 return data->ret;
1791
1792}
1793
1794static int __init iommu_prepare_with_active_regions(struct pci_dev *pdev)
1795{
1796 int nid;
1797 struct iommu_prepare_data data;
1798
1799 data.pdev = pdev;
1800 data.ret = 0;
1801
1802 for_each_online_node(nid) {
1803 work_with_active_regions(nid, iommu_prepare_work_fn, &data);
1804 if (data.ret)
1805 return data.ret;
1806 }
1807 return data.ret;
1808}
1809
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001810static void __init iommu_prepare_gfx_mapping(void)
1811{
1812 struct pci_dev *pdev = NULL;
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001813 int ret;
1814
1815 for_each_pci_dev(pdev) {
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001816 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO ||
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001817 !IS_GFX_DEVICE(pdev))
1818 continue;
1819 printk(KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n",
1820 pci_name(pdev));
Yinghai Lud52d53b2008-06-16 20:10:55 -07001821 ret = iommu_prepare_with_active_regions(pdev);
1822 if (ret)
1823 printk(KERN_ERR "IOMMU: mapping reserved region failed\n");
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001824 }
1825}
Mark McLoughlin2abd7e12008-11-20 15:49:50 +00001826#else /* !CONFIG_DMAR_GFX_WA */
1827static inline void iommu_prepare_gfx_mapping(void)
1828{
1829 return;
1830}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001831#endif
1832
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001833#ifdef CONFIG_DMAR_FLOPPY_WA
1834static inline void iommu_prepare_isa(void)
1835{
1836 struct pci_dev *pdev;
1837 int ret;
1838
1839 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1840 if (!pdev)
1841 return;
1842
1843 printk(KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n");
1844 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1845
1846 if (ret)
Frank Seidel1c35b8e2009-02-06 10:23:36 +01001847 printk(KERN_ERR "IOMMU: Failed to create 0-64M identity map, "
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001848 "floppy might not work\n");
1849
1850}
1851#else
1852static inline void iommu_prepare_isa(void)
1853{
1854 return;
1855}
1856#endif /* !CONFIG_DMAR_FLPY_WA */
1857
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001858/* Initialize each context entry as pass through.*/
1859static int __init init_context_pass_through(void)
1860{
1861 struct pci_dev *pdev = NULL;
1862 struct dmar_domain *domain;
1863 int ret;
1864
1865 for_each_pci_dev(pdev) {
1866 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1867 ret = domain_context_mapping(domain, pdev,
1868 CONTEXT_TT_PASS_THROUGH);
1869 if (ret)
1870 return ret;
1871 }
1872 return 0;
1873}
1874
Mark McLoughlin519a0542008-11-20 14:21:13 +00001875static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001876{
1877 struct dmar_drhd_unit *drhd;
1878 struct dmar_rmrr_unit *rmrr;
1879 struct pci_dev *pdev;
1880 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001881 int i, ret;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001882 int pass_through = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001883
1884 /*
1885 * for each drhd
1886 * allocate root
1887 * initialize and program root entry to not present
1888 * endfor
1889 */
1890 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08001891 g_num_of_iommus++;
1892 /*
1893 * lock not needed as this is only incremented in the single
1894 * threaded kernel __init code path all other access are read
1895 * only
1896 */
1897 }
1898
Weidong Hand9630fe2008-12-08 11:06:32 +08001899 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
1900 GFP_KERNEL);
1901 if (!g_iommus) {
1902 printk(KERN_ERR "Allocating global iommu array failed\n");
1903 ret = -ENOMEM;
1904 goto error;
1905 }
1906
mark gross80b20dd2008-04-18 13:53:58 -07001907 deferred_flush = kzalloc(g_num_of_iommus *
1908 sizeof(struct deferred_flush_tables), GFP_KERNEL);
1909 if (!deferred_flush) {
Weidong Hand9630fe2008-12-08 11:06:32 +08001910 kfree(g_iommus);
mark gross5e0d2a62008-03-04 15:22:08 -08001911 ret = -ENOMEM;
1912 goto error;
1913 }
1914
mark gross5e0d2a62008-03-04 15:22:08 -08001915 for_each_drhd_unit(drhd) {
1916 if (drhd->ignored)
1917 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07001918
1919 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08001920 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001921
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001922 ret = iommu_init_domains(iommu);
1923 if (ret)
1924 goto error;
1925
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001926 /*
1927 * TBD:
1928 * we could share the same root & context tables
1929 * amoung all IOMMU's. Need to Split it later.
1930 */
1931 ret = iommu_alloc_root_entry(iommu);
1932 if (ret) {
1933 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
1934 goto error;
1935 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001936 if (!ecap_pass_through(iommu->ecap))
1937 pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001938 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001939 if (iommu_pass_through)
1940 if (!pass_through) {
1941 printk(KERN_INFO
1942 "Pass Through is not supported by hardware.\n");
1943 iommu_pass_through = 0;
1944 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001945
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001946 /*
1947 * Start from the sane iommu hardware state.
1948 */
Youquan Songa77b67d2008-10-16 16:31:56 -07001949 for_each_drhd_unit(drhd) {
1950 if (drhd->ignored)
1951 continue;
1952
1953 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001954
1955 /*
1956 * If the queued invalidation is already initialized by us
1957 * (for example, while enabling interrupt-remapping) then
1958 * we got the things already rolling from a sane state.
1959 */
1960 if (iommu->qi)
1961 continue;
1962
1963 /*
1964 * Clear any previous faults.
1965 */
1966 dmar_fault(-1, iommu);
1967 /*
1968 * Disable queued invalidation if supported and already enabled
1969 * before OS handover.
1970 */
1971 dmar_disable_qi(iommu);
1972 }
1973
1974 for_each_drhd_unit(drhd) {
1975 if (drhd->ignored)
1976 continue;
1977
1978 iommu = drhd->iommu;
1979
Youquan Songa77b67d2008-10-16 16:31:56 -07001980 if (dmar_enable_qi(iommu)) {
1981 /*
1982 * Queued Invalidate not enabled, use Register Based
1983 * Invalidate
1984 */
1985 iommu->flush.flush_context = __iommu_flush_context;
1986 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
1987 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09001988 "invalidation\n",
1989 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07001990 } else {
1991 iommu->flush.flush_context = qi_flush_context;
1992 iommu->flush.flush_iotlb = qi_flush_iotlb;
1993 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09001994 "invalidation\n",
1995 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07001996 }
1997 }
1998
Han, Weidongd0b03bd2009-04-03 17:15:50 +08001999#ifdef CONFIG_INTR_REMAP
2000 if (!intr_remapping_enabled) {
2001 ret = enable_intr_remapping(0);
2002 if (ret)
2003 printk(KERN_ERR
2004 "IOMMU: enable interrupt remapping failed\n");
2005 }
2006#endif
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002007 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002008 * If pass through is set and enabled, context entries of all pci
2009 * devices are intialized by pass through translation type.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002010 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002011 if (iommu_pass_through) {
2012 ret = init_context_pass_through();
2013 if (ret) {
2014 printk(KERN_ERR "IOMMU: Pass through init failed.\n");
2015 iommu_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002016 }
2017 }
2018
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002019 /*
2020 * If pass through is not set or not enabled, setup context entries for
2021 * identity mappings for rmrr, gfx, and isa.
2022 */
2023 if (!iommu_pass_through) {
2024 /*
2025 * For each rmrr
2026 * for each dev attached to rmrr
2027 * do
2028 * locate drhd for dev, alloc domain for dev
2029 * allocate free domain
2030 * allocate page table entries for rmrr
2031 * if context not allocated for bus
2032 * allocate and init context
2033 * set present in root table for this bus
2034 * init context with domain, translation etc
2035 * endfor
2036 * endfor
2037 */
2038 for_each_rmrr_units(rmrr) {
2039 for (i = 0; i < rmrr->devices_cnt; i++) {
2040 pdev = rmrr->devices[i];
2041 /*
2042 * some BIOS lists non-exist devices in DMAR
2043 * table.
2044 */
2045 if (!pdev)
2046 continue;
2047 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2048 if (ret)
2049 printk(KERN_ERR
2050 "IOMMU: mapping reserved region failed\n");
2051 }
2052 }
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002053
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002054 iommu_prepare_gfx_mapping();
2055
2056 iommu_prepare_isa();
2057 }
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002058
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002059 /*
2060 * for each drhd
2061 * enable fault log
2062 * global invalidate context cache
2063 * global invalidate iotlb
2064 * enable translation
2065 */
2066 for_each_drhd_unit(drhd) {
2067 if (drhd->ignored)
2068 continue;
2069 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002070
2071 iommu_flush_write_buffer(iommu);
2072
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002073 ret = dmar_set_interrupt(iommu);
2074 if (ret)
2075 goto error;
2076
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002077 iommu_set_root_entry(iommu);
2078
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002079 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002080 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002081 iommu_disable_protect_mem_regions(iommu);
2082
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002083 ret = iommu_enable_translation(iommu);
2084 if (ret)
2085 goto error;
2086 }
2087
2088 return 0;
2089error:
2090 for_each_drhd_unit(drhd) {
2091 if (drhd->ignored)
2092 continue;
2093 iommu = drhd->iommu;
2094 free_iommu(iommu);
2095 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002096 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002097 return ret;
2098}
2099
2100static inline u64 aligned_size(u64 host_addr, size_t size)
2101{
2102 u64 addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002103 addr = (host_addr & (~PAGE_MASK)) + size;
2104 return PAGE_ALIGN(addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002105}
2106
2107struct iova *
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002108iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002109{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002110 struct iova *piova;
2111
2112 /* Make sure it's in range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002113 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002114 if (!size || (IOVA_START_ADDR + size > end))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002115 return NULL;
2116
2117 piova = alloc_iova(&domain->iovad,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002118 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002119 return piova;
2120}
2121
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002122static struct iova *
2123__intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002124 size_t size, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002125{
2126 struct pci_dev *pdev = to_pci_dev(dev);
2127 struct iova *iova = NULL;
2128
Yang Hongyang284901a2009-04-06 19:01:15 -07002129 if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002130 iova = iommu_alloc_iova(domain, size, dma_mask);
2131 else {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002132 /*
2133 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002134 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002135 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002136 */
Yang Hongyang284901a2009-04-06 19:01:15 -07002137 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002138 if (!iova)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002139 iova = iommu_alloc_iova(domain, size, dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002140 }
2141
2142 if (!iova) {
2143 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2144 return NULL;
2145 }
2146
2147 return iova;
2148}
2149
2150static struct dmar_domain *
2151get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002152{
2153 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002154 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002155
2156 domain = get_domain_for_dev(pdev,
2157 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2158 if (!domain) {
2159 printk(KERN_ERR
2160 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002161 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002162 }
2163
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002164 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002165 if (unlikely(!domain_context_mapped(pdev))) {
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002166 ret = domain_context_mapping(domain, pdev,
2167 CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002168 if (ret) {
2169 printk(KERN_ERR
2170 "Domain context map for %s failed",
2171 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002172 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002173 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002174 }
2175
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002176 return domain;
2177}
2178
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002179static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2180 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002181{
2182 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002183 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002184 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002185 struct iova *iova;
2186 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002187 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002188 struct intel_iommu *iommu;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002189
2190 BUG_ON(dir == DMA_NONE);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002191 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002192 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002193
2194 domain = get_valid_domain_for_dev(pdev);
2195 if (!domain)
2196 return 0;
2197
Weidong Han8c11e792008-12-08 15:29:22 +08002198 iommu = domain_get_iommu(domain);
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002199 size = aligned_size((u64)paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002200
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002201 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002202 if (!iova)
2203 goto error;
2204
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002205 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002206
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002207 /*
2208 * Check if DMAR supports zero-length reads on write only
2209 * mappings..
2210 */
2211 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002212 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002213 prot |= DMA_PTE_READ;
2214 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2215 prot |= DMA_PTE_WRITE;
2216 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002217 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002218 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002219 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002220 * is not a big problem
2221 */
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002222 ret = domain_page_mapping(domain, start_paddr,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002223 ((u64)paddr) & PAGE_MASK, size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002224 if (ret)
2225 goto error;
2226
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002227 /* it's a non-present to present mapping. Only flush if caching mode */
2228 if (cap_caching_mode(iommu->cap))
2229 iommu_flush_iotlb_psi(iommu, 0, start_paddr,
2230 size >> VTD_PAGE_SHIFT);
2231 else
Weidong Han8c11e792008-12-08 15:29:22 +08002232 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002233
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002234 return start_paddr + ((u64)paddr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002235
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002236error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002237 if (iova)
2238 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002239 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002240 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002241 return 0;
2242}
2243
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002244static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2245 unsigned long offset, size_t size,
2246 enum dma_data_direction dir,
2247 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002248{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002249 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2250 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002251}
2252
mark gross5e0d2a62008-03-04 15:22:08 -08002253static void flush_unmaps(void)
2254{
mark gross80b20dd2008-04-18 13:53:58 -07002255 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002256
mark gross5e0d2a62008-03-04 15:22:08 -08002257 timer_on = 0;
2258
2259 /* just flush them all */
2260 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002261 struct intel_iommu *iommu = g_iommus[i];
2262 if (!iommu)
2263 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002264
Weidong Hana2bb8452008-12-08 11:24:12 +08002265 if (deferred_flush[i].next) {
Youquan Songa77b67d2008-10-16 16:31:56 -07002266 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002267 DMA_TLB_GLOBAL_FLUSH);
mark gross80b20dd2008-04-18 13:53:58 -07002268 for (j = 0; j < deferred_flush[i].next; j++) {
2269 __free_iova(&deferred_flush[i].domain[j]->iovad,
2270 deferred_flush[i].iova[j]);
2271 }
2272 deferred_flush[i].next = 0;
2273 }
mark gross5e0d2a62008-03-04 15:22:08 -08002274 }
2275
mark gross5e0d2a62008-03-04 15:22:08 -08002276 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002277}
2278
2279static void flush_unmaps_timeout(unsigned long data)
2280{
mark gross80b20dd2008-04-18 13:53:58 -07002281 unsigned long flags;
2282
2283 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002284 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002285 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002286}
2287
2288static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2289{
2290 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002291 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002292 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002293
2294 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002295 if (list_size == HIGH_WATER_MARK)
2296 flush_unmaps();
2297
Weidong Han8c11e792008-12-08 15:29:22 +08002298 iommu = domain_get_iommu(dom);
2299 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002300
mark gross80b20dd2008-04-18 13:53:58 -07002301 next = deferred_flush[iommu_id].next;
2302 deferred_flush[iommu_id].domain[next] = dom;
2303 deferred_flush[iommu_id].iova[next] = iova;
2304 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002305
2306 if (!timer_on) {
2307 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2308 timer_on = 1;
2309 }
2310 list_size++;
2311 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2312}
2313
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002314static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2315 size_t size, enum dma_data_direction dir,
2316 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002317{
2318 struct pci_dev *pdev = to_pci_dev(dev);
2319 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002320 unsigned long start_addr;
2321 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002322 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002323
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002324 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002325 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002326 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002327 BUG_ON(!domain);
2328
Weidong Han8c11e792008-12-08 15:29:22 +08002329 iommu = domain_get_iommu(domain);
2330
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002331 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2332 if (!iova)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002333 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002334
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002335 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002336 size = aligned_size((u64)dev_addr, size);
2337
David Woodhouse4cf2e752009-02-11 17:23:43 +00002338 pr_debug("Device %s unmapping: %zx@%llx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002339 pci_name(pdev), size, (unsigned long long)start_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002340
2341 /* clear the whole page */
2342 dma_pte_clear_range(domain, start_addr, start_addr + size);
2343 /* free page tables */
2344 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
mark gross5e0d2a62008-03-04 15:22:08 -08002345 if (intel_iommu_strict) {
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002346 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2347 size >> VTD_PAGE_SHIFT);
mark gross5e0d2a62008-03-04 15:22:08 -08002348 /* free iova */
2349 __free_iova(&domain->iovad, iova);
2350 } else {
2351 add_unmap(domain, iova);
2352 /*
2353 * queue up the release of the unmap to save the 1/6th of the
2354 * cpu used up by the iotlb flush operation...
2355 */
mark gross5e0d2a62008-03-04 15:22:08 -08002356 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002357}
2358
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002359static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2360 int dir)
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002361{
2362 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2363}
2364
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002365static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2366 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002367{
2368 void *vaddr;
2369 int order;
2370
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002371 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002372 order = get_order(size);
2373 flags &= ~(GFP_DMA | GFP_DMA32);
2374
2375 vaddr = (void *)__get_free_pages(flags, order);
2376 if (!vaddr)
2377 return NULL;
2378 memset(vaddr, 0, size);
2379
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002380 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2381 DMA_BIDIRECTIONAL,
2382 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002383 if (*dma_handle)
2384 return vaddr;
2385 free_pages((unsigned long)vaddr, order);
2386 return NULL;
2387}
2388
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002389static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2390 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002391{
2392 int order;
2393
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002394 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002395 order = get_order(size);
2396
2397 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2398 free_pages((unsigned long)vaddr, order);
2399}
2400
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002401static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2402 int nelems, enum dma_data_direction dir,
2403 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002404{
2405 int i;
2406 struct pci_dev *pdev = to_pci_dev(hwdev);
2407 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002408 unsigned long start_addr;
2409 struct iova *iova;
2410 size_t size = 0;
David Woodhouse4cf2e752009-02-11 17:23:43 +00002411 phys_addr_t addr;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002412 struct scatterlist *sg;
Weidong Han8c11e792008-12-08 15:29:22 +08002413 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002414
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002415 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002416 return;
2417
2418 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002419 BUG_ON(!domain);
2420
2421 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002422
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002423 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002424 if (!iova)
2425 return;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002426 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002427 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002428 size += aligned_size((u64)addr, sg->length);
2429 }
2430
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002431 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002432
2433 /* clear the whole page */
2434 dma_pte_clear_range(domain, start_addr, start_addr + size);
2435 /* free page tables */
2436 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2437
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002438 iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
2439 size >> VTD_PAGE_SHIFT);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002440
2441 /* free iova */
2442 __free_iova(&domain->iovad, iova);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002443}
2444
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002445static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002446 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002447{
2448 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002449 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002450
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002451 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002452 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002453 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002454 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002455 }
2456 return nelems;
2457}
2458
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002459static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2460 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002461{
David Woodhouse4cf2e752009-02-11 17:23:43 +00002462 phys_addr_t addr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002463 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002464 struct pci_dev *pdev = to_pci_dev(hwdev);
2465 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002466 size_t size = 0;
2467 int prot = 0;
2468 size_t offset = 0;
2469 struct iova *iova = NULL;
2470 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002471 struct scatterlist *sg;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002472 unsigned long start_addr;
Weidong Han8c11e792008-12-08 15:29:22 +08002473 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002474
2475 BUG_ON(dir == DMA_NONE);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002476 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002477 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002478
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002479 domain = get_valid_domain_for_dev(pdev);
2480 if (!domain)
2481 return 0;
2482
Weidong Han8c11e792008-12-08 15:29:22 +08002483 iommu = domain_get_iommu(domain);
2484
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002485 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002486 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002487 size += aligned_size((u64)addr, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002488 }
2489
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002490 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002491 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002492 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002493 return 0;
2494 }
2495
2496 /*
2497 * Check if DMAR supports zero-length reads on write only
2498 * mappings..
2499 */
2500 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002501 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002502 prot |= DMA_PTE_READ;
2503 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2504 prot |= DMA_PTE_WRITE;
2505
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002506 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002507 offset = 0;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002508 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002509 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002510 size = aligned_size((u64)addr, sg->length);
2511 ret = domain_page_mapping(domain, start_addr + offset,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002512 ((u64)addr) & PAGE_MASK,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002513 size, prot);
2514 if (ret) {
2515 /* clear the page */
2516 dma_pte_clear_range(domain, start_addr,
2517 start_addr + offset);
2518 /* free page tables */
2519 dma_pte_free_pagetable(domain, start_addr,
2520 start_addr + offset);
2521 /* free iova */
2522 __free_iova(&domain->iovad, iova);
2523 return 0;
2524 }
2525 sg->dma_address = start_addr + offset +
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002526 ((u64)addr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002527 sg->dma_length = sg->length;
2528 offset += size;
2529 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002530
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002531 /* it's a non-present to present mapping. Only flush if caching mode */
2532 if (cap_caching_mode(iommu->cap))
2533 iommu_flush_iotlb_psi(iommu, 0, start_addr,
2534 offset >> VTD_PAGE_SHIFT);
2535 else
Weidong Han8c11e792008-12-08 15:29:22 +08002536 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002537
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002538 return nelems;
2539}
2540
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002541static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2542{
2543 return !dma_addr;
2544}
2545
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002546struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002547 .alloc_coherent = intel_alloc_coherent,
2548 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002549 .map_sg = intel_map_sg,
2550 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002551 .map_page = intel_map_page,
2552 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002553 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002554};
2555
2556static inline int iommu_domain_cache_init(void)
2557{
2558 int ret = 0;
2559
2560 iommu_domain_cache = kmem_cache_create("iommu_domain",
2561 sizeof(struct dmar_domain),
2562 0,
2563 SLAB_HWCACHE_ALIGN,
2564
2565 NULL);
2566 if (!iommu_domain_cache) {
2567 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2568 ret = -ENOMEM;
2569 }
2570
2571 return ret;
2572}
2573
2574static inline int iommu_devinfo_cache_init(void)
2575{
2576 int ret = 0;
2577
2578 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2579 sizeof(struct device_domain_info),
2580 0,
2581 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002582 NULL);
2583 if (!iommu_devinfo_cache) {
2584 printk(KERN_ERR "Couldn't create devinfo cache\n");
2585 ret = -ENOMEM;
2586 }
2587
2588 return ret;
2589}
2590
2591static inline int iommu_iova_cache_init(void)
2592{
2593 int ret = 0;
2594
2595 iommu_iova_cache = kmem_cache_create("iommu_iova",
2596 sizeof(struct iova),
2597 0,
2598 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002599 NULL);
2600 if (!iommu_iova_cache) {
2601 printk(KERN_ERR "Couldn't create iova cache\n");
2602 ret = -ENOMEM;
2603 }
2604
2605 return ret;
2606}
2607
2608static int __init iommu_init_mempool(void)
2609{
2610 int ret;
2611 ret = iommu_iova_cache_init();
2612 if (ret)
2613 return ret;
2614
2615 ret = iommu_domain_cache_init();
2616 if (ret)
2617 goto domain_error;
2618
2619 ret = iommu_devinfo_cache_init();
2620 if (!ret)
2621 return ret;
2622
2623 kmem_cache_destroy(iommu_domain_cache);
2624domain_error:
2625 kmem_cache_destroy(iommu_iova_cache);
2626
2627 return -ENOMEM;
2628}
2629
2630static void __init iommu_exit_mempool(void)
2631{
2632 kmem_cache_destroy(iommu_devinfo_cache);
2633 kmem_cache_destroy(iommu_domain_cache);
2634 kmem_cache_destroy(iommu_iova_cache);
2635
2636}
2637
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002638static void __init init_no_remapping_devices(void)
2639{
2640 struct dmar_drhd_unit *drhd;
2641
2642 for_each_drhd_unit(drhd) {
2643 if (!drhd->include_all) {
2644 int i;
2645 for (i = 0; i < drhd->devices_cnt; i++)
2646 if (drhd->devices[i] != NULL)
2647 break;
2648 /* ignore DMAR unit if no pci devices exist */
2649 if (i == drhd->devices_cnt)
2650 drhd->ignored = 1;
2651 }
2652 }
2653
2654 if (dmar_map_gfx)
2655 return;
2656
2657 for_each_drhd_unit(drhd) {
2658 int i;
2659 if (drhd->ignored || drhd->include_all)
2660 continue;
2661
2662 for (i = 0; i < drhd->devices_cnt; i++)
2663 if (drhd->devices[i] &&
2664 !IS_GFX_DEVICE(drhd->devices[i]))
2665 break;
2666
2667 if (i < drhd->devices_cnt)
2668 continue;
2669
2670 /* bypass IOMMU if it is just for gfx devices */
2671 drhd->ignored = 1;
2672 for (i = 0; i < drhd->devices_cnt; i++) {
2673 if (!drhd->devices[i])
2674 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002675 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002676 }
2677 }
2678}
2679
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002680#ifdef CONFIG_SUSPEND
2681static int init_iommu_hw(void)
2682{
2683 struct dmar_drhd_unit *drhd;
2684 struct intel_iommu *iommu = NULL;
2685
2686 for_each_active_iommu(iommu, drhd)
2687 if (iommu->qi)
2688 dmar_reenable_qi(iommu);
2689
2690 for_each_active_iommu(iommu, drhd) {
2691 iommu_flush_write_buffer(iommu);
2692
2693 iommu_set_root_entry(iommu);
2694
2695 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002696 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002697 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002698 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002699 iommu_disable_protect_mem_regions(iommu);
2700 iommu_enable_translation(iommu);
2701 }
2702
2703 return 0;
2704}
2705
2706static void iommu_flush_all(void)
2707{
2708 struct dmar_drhd_unit *drhd;
2709 struct intel_iommu *iommu;
2710
2711 for_each_active_iommu(iommu, drhd) {
2712 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002713 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002714 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002715 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002716 }
2717}
2718
2719static int iommu_suspend(struct sys_device *dev, pm_message_t state)
2720{
2721 struct dmar_drhd_unit *drhd;
2722 struct intel_iommu *iommu = NULL;
2723 unsigned long flag;
2724
2725 for_each_active_iommu(iommu, drhd) {
2726 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
2727 GFP_ATOMIC);
2728 if (!iommu->iommu_state)
2729 goto nomem;
2730 }
2731
2732 iommu_flush_all();
2733
2734 for_each_active_iommu(iommu, drhd) {
2735 iommu_disable_translation(iommu);
2736
2737 spin_lock_irqsave(&iommu->register_lock, flag);
2738
2739 iommu->iommu_state[SR_DMAR_FECTL_REG] =
2740 readl(iommu->reg + DMAR_FECTL_REG);
2741 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
2742 readl(iommu->reg + DMAR_FEDATA_REG);
2743 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
2744 readl(iommu->reg + DMAR_FEADDR_REG);
2745 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
2746 readl(iommu->reg + DMAR_FEUADDR_REG);
2747
2748 spin_unlock_irqrestore(&iommu->register_lock, flag);
2749 }
2750 return 0;
2751
2752nomem:
2753 for_each_active_iommu(iommu, drhd)
2754 kfree(iommu->iommu_state);
2755
2756 return -ENOMEM;
2757}
2758
2759static int iommu_resume(struct sys_device *dev)
2760{
2761 struct dmar_drhd_unit *drhd;
2762 struct intel_iommu *iommu = NULL;
2763 unsigned long flag;
2764
2765 if (init_iommu_hw()) {
2766 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
2767 return -EIO;
2768 }
2769
2770 for_each_active_iommu(iommu, drhd) {
2771
2772 spin_lock_irqsave(&iommu->register_lock, flag);
2773
2774 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
2775 iommu->reg + DMAR_FECTL_REG);
2776 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
2777 iommu->reg + DMAR_FEDATA_REG);
2778 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
2779 iommu->reg + DMAR_FEADDR_REG);
2780 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
2781 iommu->reg + DMAR_FEUADDR_REG);
2782
2783 spin_unlock_irqrestore(&iommu->register_lock, flag);
2784 }
2785
2786 for_each_active_iommu(iommu, drhd)
2787 kfree(iommu->iommu_state);
2788
2789 return 0;
2790}
2791
2792static struct sysdev_class iommu_sysclass = {
2793 .name = "iommu",
2794 .resume = iommu_resume,
2795 .suspend = iommu_suspend,
2796};
2797
2798static struct sys_device device_iommu = {
2799 .cls = &iommu_sysclass,
2800};
2801
2802static int __init init_iommu_sysfs(void)
2803{
2804 int error;
2805
2806 error = sysdev_class_register(&iommu_sysclass);
2807 if (error)
2808 return error;
2809
2810 error = sysdev_register(&device_iommu);
2811 if (error)
2812 sysdev_class_unregister(&iommu_sysclass);
2813
2814 return error;
2815}
2816
2817#else
2818static int __init init_iommu_sysfs(void)
2819{
2820 return 0;
2821}
2822#endif /* CONFIG_PM */
2823
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002824int __init intel_iommu_init(void)
2825{
2826 int ret = 0;
2827
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002828 if (dmar_table_init())
2829 return -ENODEV;
2830
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002831 if (dmar_dev_scope_init())
2832 return -ENODEV;
2833
Suresh Siddha2ae21012008-07-10 11:16:43 -07002834 /*
2835 * Check the need for DMA-remapping initialization now.
2836 * Above initialization will also be used by Interrupt-remapping.
2837 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002838 if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled)
Suresh Siddha2ae21012008-07-10 11:16:43 -07002839 return -ENODEV;
2840
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002841 iommu_init_mempool();
2842 dmar_init_reserved_ranges();
2843
2844 init_no_remapping_devices();
2845
2846 ret = init_dmars();
2847 if (ret) {
2848 printk(KERN_ERR "IOMMU: dmar init failed\n");
2849 put_iova_domain(&reserved_iova_list);
2850 iommu_exit_mempool();
2851 return ret;
2852 }
2853 printk(KERN_INFO
2854 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
2855
mark gross5e0d2a62008-03-04 15:22:08 -08002856 init_timer(&unmap_timer);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002857 force_iommu = 1;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002858
2859 if (!iommu_pass_through) {
2860 printk(KERN_INFO
2861 "Multi-level page-table translation for DMAR.\n");
2862 dma_ops = &intel_dma_ops;
2863 } else
2864 printk(KERN_INFO
2865 "DMAR: Pass through translation for DMAR.\n");
2866
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002867 init_iommu_sysfs();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01002868
2869 register_iommu(&intel_iommu_ops);
2870
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002871 return 0;
2872}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002873
Weidong Hanc7151a82008-12-08 22:51:37 +08002874static int vm_domain_add_dev_info(struct dmar_domain *domain,
2875 struct pci_dev *pdev)
2876{
2877 struct device_domain_info *info;
2878 unsigned long flags;
2879
2880 info = alloc_devinfo_mem();
2881 if (!info)
2882 return -ENOMEM;
2883
David Woodhouse276dbf992009-04-04 01:45:37 +01002884 info->segment = pci_domain_nr(pdev->bus);
Weidong Hanc7151a82008-12-08 22:51:37 +08002885 info->bus = pdev->bus->number;
2886 info->devfn = pdev->devfn;
2887 info->dev = pdev;
2888 info->domain = domain;
2889
2890 spin_lock_irqsave(&device_domain_lock, flags);
2891 list_add(&info->link, &domain->devices);
2892 list_add(&info->global, &device_domain_list);
2893 pdev->dev.archdata.iommu = info;
2894 spin_unlock_irqrestore(&device_domain_lock, flags);
2895
2896 return 0;
2897}
2898
Han, Weidong3199aa62009-02-26 17:31:12 +08002899static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
2900 struct pci_dev *pdev)
2901{
2902 struct pci_dev *tmp, *parent;
2903
2904 if (!iommu || !pdev)
2905 return;
2906
2907 /* dependent device detach */
2908 tmp = pci_find_upstream_pcie_bridge(pdev);
2909 /* Secondary interface's bus number and devfn 0 */
2910 if (tmp) {
2911 parent = pdev->bus->self;
2912 while (parent != tmp) {
2913 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01002914 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002915 parent = parent->bus->self;
2916 }
2917 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
2918 iommu_detach_dev(iommu,
2919 tmp->subordinate->number, 0);
2920 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01002921 iommu_detach_dev(iommu, tmp->bus->number,
2922 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002923 }
2924}
2925
Weidong Hanc7151a82008-12-08 22:51:37 +08002926static void vm_domain_remove_one_dev_info(struct dmar_domain *domain,
2927 struct pci_dev *pdev)
2928{
2929 struct device_domain_info *info;
2930 struct intel_iommu *iommu;
2931 unsigned long flags;
2932 int found = 0;
2933 struct list_head *entry, *tmp;
2934
David Woodhouse276dbf992009-04-04 01:45:37 +01002935 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
2936 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08002937 if (!iommu)
2938 return;
2939
2940 spin_lock_irqsave(&device_domain_lock, flags);
2941 list_for_each_safe(entry, tmp, &domain->devices) {
2942 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01002943 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08002944 if (info->bus == pdev->bus->number &&
2945 info->devfn == pdev->devfn) {
2946 list_del(&info->link);
2947 list_del(&info->global);
2948 if (info->dev)
2949 info->dev->dev.archdata.iommu = NULL;
2950 spin_unlock_irqrestore(&device_domain_lock, flags);
2951
2952 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002953 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08002954 free_devinfo_mem(info);
2955
2956 spin_lock_irqsave(&device_domain_lock, flags);
2957
2958 if (found)
2959 break;
2960 else
2961 continue;
2962 }
2963
2964 /* if there is no other devices under the same iommu
2965 * owned by this domain, clear this iommu in iommu_bmp
2966 * update iommu count and coherency
2967 */
David Woodhouse276dbf992009-04-04 01:45:37 +01002968 if (iommu == device_to_iommu(info->segment, info->bus,
2969 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08002970 found = 1;
2971 }
2972
2973 if (found == 0) {
2974 unsigned long tmp_flags;
2975 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
2976 clear_bit(iommu->seq_id, &domain->iommu_bmp);
2977 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08002978 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08002979 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
2980 }
2981
2982 spin_unlock_irqrestore(&device_domain_lock, flags);
2983}
2984
2985static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
2986{
2987 struct device_domain_info *info;
2988 struct intel_iommu *iommu;
2989 unsigned long flags1, flags2;
2990
2991 spin_lock_irqsave(&device_domain_lock, flags1);
2992 while (!list_empty(&domain->devices)) {
2993 info = list_entry(domain->devices.next,
2994 struct device_domain_info, link);
2995 list_del(&info->link);
2996 list_del(&info->global);
2997 if (info->dev)
2998 info->dev->dev.archdata.iommu = NULL;
2999
3000 spin_unlock_irqrestore(&device_domain_lock, flags1);
3001
David Woodhouse276dbf992009-04-04 01:45:37 +01003002 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003003 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003004 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003005
3006 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08003007 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08003008 */
3009 spin_lock_irqsave(&domain->iommu_lock, flags2);
3010 if (test_and_clear_bit(iommu->seq_id,
3011 &domain->iommu_bmp)) {
3012 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003013 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003014 }
3015 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3016
3017 free_devinfo_mem(info);
3018 spin_lock_irqsave(&device_domain_lock, flags1);
3019 }
3020 spin_unlock_irqrestore(&device_domain_lock, flags1);
3021}
3022
Weidong Han5e98c4b2008-12-08 23:03:27 +08003023/* domain id for virtual machine, it won't be set in context */
3024static unsigned long vm_domid;
3025
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003026static int vm_domain_min_agaw(struct dmar_domain *domain)
3027{
3028 int i;
3029 int min_agaw = domain->agaw;
3030
3031 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
3032 for (; i < g_num_of_iommus; ) {
3033 if (min_agaw > g_iommus[i]->agaw)
3034 min_agaw = g_iommus[i]->agaw;
3035
3036 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
3037 }
3038
3039 return min_agaw;
3040}
3041
Weidong Han5e98c4b2008-12-08 23:03:27 +08003042static struct dmar_domain *iommu_alloc_vm_domain(void)
3043{
3044 struct dmar_domain *domain;
3045
3046 domain = alloc_domain_mem();
3047 if (!domain)
3048 return NULL;
3049
3050 domain->id = vm_domid++;
3051 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3052 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3053
3054 return domain;
3055}
3056
3057static int vm_domain_init(struct dmar_domain *domain, int guest_width)
3058{
3059 int adjust_width;
3060
3061 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3062 spin_lock_init(&domain->mapping_lock);
3063 spin_lock_init(&domain->iommu_lock);
3064
3065 domain_reserve_special_ranges(domain);
3066
3067 /* calculate AGAW */
3068 domain->gaw = guest_width;
3069 adjust_width = guestwidth_to_adjustwidth(guest_width);
3070 domain->agaw = width_to_agaw(adjust_width);
3071
3072 INIT_LIST_HEAD(&domain->devices);
3073
3074 domain->iommu_count = 0;
3075 domain->iommu_coherency = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003076 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003077
3078 /* always allocate the top pgd */
3079 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3080 if (!domain->pgd)
3081 return -ENOMEM;
3082 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3083 return 0;
3084}
3085
3086static void iommu_free_vm_domain(struct dmar_domain *domain)
3087{
3088 unsigned long flags;
3089 struct dmar_drhd_unit *drhd;
3090 struct intel_iommu *iommu;
3091 unsigned long i;
3092 unsigned long ndomains;
3093
3094 for_each_drhd_unit(drhd) {
3095 if (drhd->ignored)
3096 continue;
3097 iommu = drhd->iommu;
3098
3099 ndomains = cap_ndoms(iommu->cap);
3100 i = find_first_bit(iommu->domain_ids, ndomains);
3101 for (; i < ndomains; ) {
3102 if (iommu->domains[i] == domain) {
3103 spin_lock_irqsave(&iommu->lock, flags);
3104 clear_bit(i, iommu->domain_ids);
3105 iommu->domains[i] = NULL;
3106 spin_unlock_irqrestore(&iommu->lock, flags);
3107 break;
3108 }
3109 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3110 }
3111 }
3112}
3113
3114static void vm_domain_exit(struct dmar_domain *domain)
3115{
3116 u64 end;
3117
3118 /* Domain 0 is reserved, so dont process it */
3119 if (!domain)
3120 return;
3121
3122 vm_domain_remove_all_dev_info(domain);
3123 /* destroy iovas */
3124 put_iova_domain(&domain->iovad);
3125 end = DOMAIN_MAX_ADDR(domain->gaw);
3126 end = end & (~VTD_PAGE_MASK);
3127
3128 /* clear ptes */
3129 dma_pte_clear_range(domain, 0, end);
3130
3131 /* free page tables */
3132 dma_pte_free_pagetable(domain, 0, end);
3133
3134 iommu_free_vm_domain(domain);
3135 free_domain_mem(domain);
3136}
3137
Joerg Roedel5d450802008-12-03 14:52:32 +01003138static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003139{
Joerg Roedel5d450802008-12-03 14:52:32 +01003140 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003141
Joerg Roedel5d450802008-12-03 14:52:32 +01003142 dmar_domain = iommu_alloc_vm_domain();
3143 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003144 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003145 "intel_iommu_domain_init: dmar_domain == NULL\n");
3146 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003147 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003148 if (vm_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003149 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003150 "intel_iommu_domain_init() failed\n");
3151 vm_domain_exit(dmar_domain);
3152 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003153 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003154 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003155
Joerg Roedel5d450802008-12-03 14:52:32 +01003156 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003157}
Kay, Allen M38717942008-09-09 18:37:29 +03003158
Joerg Roedel5d450802008-12-03 14:52:32 +01003159static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003160{
Joerg Roedel5d450802008-12-03 14:52:32 +01003161 struct dmar_domain *dmar_domain = domain->priv;
3162
3163 domain->priv = NULL;
3164 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003165}
Kay, Allen M38717942008-09-09 18:37:29 +03003166
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003167static int intel_iommu_attach_device(struct iommu_domain *domain,
3168 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003169{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003170 struct dmar_domain *dmar_domain = domain->priv;
3171 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003172 struct intel_iommu *iommu;
3173 int addr_width;
3174 u64 end;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003175 int ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003176
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003177 /* normally pdev is not mapped */
3178 if (unlikely(domain_context_mapped(pdev))) {
3179 struct dmar_domain *old_domain;
3180
3181 old_domain = find_domain(pdev);
3182 if (old_domain) {
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003183 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003184 vm_domain_remove_one_dev_info(old_domain, pdev);
3185 else
3186 domain_remove_dev_info(old_domain);
3187 }
3188 }
3189
David Woodhouse276dbf992009-04-04 01:45:37 +01003190 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3191 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003192 if (!iommu)
3193 return -ENODEV;
3194
3195 /* check if this iommu agaw is sufficient for max mapped address */
3196 addr_width = agaw_to_width(iommu->agaw);
3197 end = DOMAIN_MAX_ADDR(addr_width);
3198 end = end & VTD_PAGE_MASK;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003199 if (end < dmar_domain->max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003200 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3201 "sufficient for the mapped address (%llx)\n",
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003202 __func__, iommu->agaw, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003203 return -EFAULT;
3204 }
3205
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003206 ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003207 if (ret)
3208 return ret;
3209
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003210 ret = vm_domain_add_dev_info(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003211 return ret;
3212}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003213
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003214static void intel_iommu_detach_device(struct iommu_domain *domain,
3215 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003216{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003217 struct dmar_domain *dmar_domain = domain->priv;
3218 struct pci_dev *pdev = to_pci_dev(dev);
3219
3220 vm_domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003221}
Kay, Allen M38717942008-09-09 18:37:29 +03003222
Joerg Roedeldde57a22008-12-03 15:04:09 +01003223static int intel_iommu_map_range(struct iommu_domain *domain,
3224 unsigned long iova, phys_addr_t hpa,
3225 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003226{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003227 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003228 u64 max_addr;
3229 int addr_width;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003230 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003231 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003232
Joerg Roedeldde57a22008-12-03 15:04:09 +01003233 if (iommu_prot & IOMMU_READ)
3234 prot |= DMA_PTE_READ;
3235 if (iommu_prot & IOMMU_WRITE)
3236 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08003237 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3238 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003239
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003240 max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003241 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003242 int min_agaw;
3243 u64 end;
3244
3245 /* check if minimum agaw is sufficient for mapped address */
Joerg Roedeldde57a22008-12-03 15:04:09 +01003246 min_agaw = vm_domain_min_agaw(dmar_domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003247 addr_width = agaw_to_width(min_agaw);
3248 end = DOMAIN_MAX_ADDR(addr_width);
3249 end = end & VTD_PAGE_MASK;
3250 if (end < max_addr) {
3251 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3252 "sufficient for the mapped address (%llx)\n",
3253 __func__, min_agaw, max_addr);
3254 return -EFAULT;
3255 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003256 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003257 }
3258
Joerg Roedeldde57a22008-12-03 15:04:09 +01003259 ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003260 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003261}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003262
Joerg Roedeldde57a22008-12-03 15:04:09 +01003263static void intel_iommu_unmap_range(struct iommu_domain *domain,
3264 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003265{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003266 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003267 dma_addr_t base;
3268
3269 /* The address might not be aligned */
3270 base = iova & VTD_PAGE_MASK;
3271 size = VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003272 dma_pte_clear_range(dmar_domain, base, base + size);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003273
Joerg Roedeldde57a22008-12-03 15:04:09 +01003274 if (dmar_domain->max_addr == base + size)
3275 dmar_domain->max_addr = base;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003276}
Kay, Allen M38717942008-09-09 18:37:29 +03003277
Joerg Roedeld14d6572008-12-03 15:06:57 +01003278static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3279 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003280{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003281 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003282 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003283 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003284
Joerg Roedeld14d6572008-12-03 15:06:57 +01003285 pte = addr_to_dma_pte(dmar_domain, iova);
Kay, Allen M38717942008-09-09 18:37:29 +03003286 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003287 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003288
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003289 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003290}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003291
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003292static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3293 unsigned long cap)
3294{
3295 struct dmar_domain *dmar_domain = domain->priv;
3296
3297 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3298 return dmar_domain->iommu_snooping;
3299
3300 return 0;
3301}
3302
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003303static struct iommu_ops intel_iommu_ops = {
3304 .domain_init = intel_iommu_domain_init,
3305 .domain_destroy = intel_iommu_domain_destroy,
3306 .attach_dev = intel_iommu_attach_device,
3307 .detach_dev = intel_iommu_detach_device,
3308 .map = intel_iommu_map_range,
3309 .unmap = intel_iommu_unmap_range,
3310 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003311 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003312};
David Woodhouse9af88142009-02-13 23:18:03 +00003313
3314static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3315{
3316 /*
3317 * Mobile 4 Series Chipset neglects to set RWBF capability,
3318 * but needs it:
3319 */
3320 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3321 rwbf_quirk = 1;
3322}
3323
3324DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);