blob: eea1006c860a18300c5a511e61a8ce2f8cc70e17 [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)
David Woodhouse595badf2009-06-27 22:09:11 +010059#define DOMAIN_MAX_PFN(gaw) ((((u64)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070060
Mark McLoughlinf27be032008-11-20 15:49:43 +000061#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070062#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070063#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080064
David Woodhousefd18de52009-05-10 23:57:41 +010065
David Woodhousedd4e8312009-06-27 16:21:20 +010066/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
67 are never going to work. */
68static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
69{
70 return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
71}
72
73static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
74{
75 return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
76}
77static inline unsigned long page_to_dma_pfn(struct page *pg)
78{
79 return mm_to_dma_pfn(page_to_pfn(pg));
80}
81static inline unsigned long virt_to_dma_pfn(void *p)
82{
83 return page_to_dma_pfn(virt_to_page(p));
84}
85
Weidong Hand9630fe2008-12-08 11:06:32 +080086/* global iommu list, set NULL for ignored DMAR units */
87static struct intel_iommu **g_iommus;
88
David Woodhouse9af88142009-02-13 23:18:03 +000089static int rwbf_quirk;
90
Mark McLoughlin46b08e12008-11-20 15:49:44 +000091/*
92 * 0: Present
93 * 1-11: Reserved
94 * 12-63: Context Ptr (12 - (haw-1))
95 * 64-127: Reserved
96 */
97struct root_entry {
98 u64 val;
99 u64 rsvd1;
100};
101#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
102static inline bool root_present(struct root_entry *root)
103{
104 return (root->val & 1);
105}
106static inline void set_root_present(struct root_entry *root)
107{
108 root->val |= 1;
109}
110static inline void set_root_value(struct root_entry *root, unsigned long value)
111{
112 root->val |= value & VTD_PAGE_MASK;
113}
114
115static inline struct context_entry *
116get_context_addr_from_root(struct root_entry *root)
117{
118 return (struct context_entry *)
119 (root_present(root)?phys_to_virt(
120 root->val & VTD_PAGE_MASK) :
121 NULL);
122}
123
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000124/*
125 * low 64 bits:
126 * 0: present
127 * 1: fault processing disable
128 * 2-3: translation type
129 * 12-63: address space root
130 * high 64 bits:
131 * 0-2: address width
132 * 3-6: aval
133 * 8-23: domain id
134 */
135struct context_entry {
136 u64 lo;
137 u64 hi;
138};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000139
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000140static inline bool context_present(struct context_entry *context)
141{
142 return (context->lo & 1);
143}
144static inline void context_set_present(struct context_entry *context)
145{
146 context->lo |= 1;
147}
148
149static inline void context_set_fault_enable(struct context_entry *context)
150{
151 context->lo &= (((u64)-1) << 2) | 1;
152}
153
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000154static inline void context_set_translation_type(struct context_entry *context,
155 unsigned long value)
156{
157 context->lo &= (((u64)-1) << 4) | 3;
158 context->lo |= (value & 3) << 2;
159}
160
161static inline void context_set_address_root(struct context_entry *context,
162 unsigned long value)
163{
164 context->lo |= value & VTD_PAGE_MASK;
165}
166
167static inline void context_set_address_width(struct context_entry *context,
168 unsigned long value)
169{
170 context->hi |= value & 7;
171}
172
173static inline void context_set_domain_id(struct context_entry *context,
174 unsigned long value)
175{
176 context->hi |= (value & ((1 << 16) - 1)) << 8;
177}
178
179static inline void context_clear_entry(struct context_entry *context)
180{
181 context->lo = 0;
182 context->hi = 0;
183}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000184
Mark McLoughlin622ba122008-11-20 15:49:46 +0000185/*
186 * 0: readable
187 * 1: writable
188 * 2-6: reserved
189 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800190 * 8-10: available
191 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000192 * 12-63: Host physcial address
193 */
194struct dma_pte {
195 u64 val;
196};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000197
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000198static inline void dma_clear_pte(struct dma_pte *pte)
199{
200 pte->val = 0;
201}
202
203static inline void dma_set_pte_readable(struct dma_pte *pte)
204{
205 pte->val |= DMA_PTE_READ;
206}
207
208static inline void dma_set_pte_writable(struct dma_pte *pte)
209{
210 pte->val |= DMA_PTE_WRITE;
211}
212
Sheng Yang9cf06692009-03-18 15:33:07 +0800213static inline void dma_set_pte_snp(struct dma_pte *pte)
214{
215 pte->val |= DMA_PTE_SNP;
216}
217
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000218static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
219{
220 pte->val = (pte->val & ~3) | (prot & 3);
221}
222
223static inline u64 dma_pte_addr(struct dma_pte *pte)
224{
David Woodhousec85994e2009-07-01 19:21:24 +0100225#ifdef CONFIG_64BIT
226 return pte->val & VTD_PAGE_MASK;
227#else
228 /* Must have a full atomic 64-bit read */
229 return __cmpxchg64(pte, 0ULL, 0ULL) & VTD_PAGE_MASK;
230#endif
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000231}
232
David Woodhousedd4e8312009-06-27 16:21:20 +0100233static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn)
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000234{
David Woodhousedd4e8312009-06-27 16:21:20 +0100235 pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000236}
237
238static inline bool dma_pte_present(struct dma_pte *pte)
239{
240 return (pte->val & 3) != 0;
241}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000242
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700243/*
244 * This domain is a statically identity mapping domain.
245 * 1. This domain creats a static 1:1 mapping to all usable memory.
246 * 2. It maps to each iommu if successful.
247 * 3. Each iommu mapps to this domain if successful.
248 */
249struct dmar_domain *si_domain;
250
Weidong Han3b5410e2008-12-08 09:17:15 +0800251/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100252#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800253
Weidong Han1ce28fe2008-12-08 16:35:39 +0800254/* domain represents a virtual machine, more than one devices
255 * across iommus may be owned in one domain, e.g. kvm guest.
256 */
257#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
258
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700259/* si_domain contains mulitple devices */
260#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2)
261
Mark McLoughlin99126f72008-11-20 15:49:47 +0000262struct dmar_domain {
263 int id; /* domain id */
Weidong Han8c11e792008-12-08 15:29:22 +0800264 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000265
266 struct list_head devices; /* all devices' list */
267 struct iova_domain iovad; /* iova's that belong to this domain */
268
269 struct dma_pte *pgd; /* virtual address */
270 spinlock_t mapping_lock; /* page table lock */
271 int gaw; /* max guest address width */
272
273 /* adjusted guest address width, 0 is level 2 30-bit */
274 int agaw;
275
Weidong Han3b5410e2008-12-08 09:17:15 +0800276 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800277
278 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800279 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800280 int iommu_count; /* reference count of iommu */
281 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800282 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000283};
284
Mark McLoughlina647dac2008-11-20 15:49:48 +0000285/* PCI domain-device relationship */
286struct device_domain_info {
287 struct list_head link; /* link to domain siblings */
288 struct list_head global; /* link to global list */
David Woodhouse276dbf92009-04-04 01:45:37 +0100289 int segment; /* PCI domain */
290 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000291 u8 devfn; /* PCI devfn number */
292 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800293 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000294 struct dmar_domain *domain; /* pointer to domain */
295};
296
mark gross5e0d2a62008-03-04 15:22:08 -0800297static void flush_unmaps_timeout(unsigned long data);
298
299DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
300
mark gross80b20dd2008-04-18 13:53:58 -0700301#define HIGH_WATER_MARK 250
302struct deferred_flush_tables {
303 int next;
304 struct iova *iova[HIGH_WATER_MARK];
305 struct dmar_domain *domain[HIGH_WATER_MARK];
306};
307
308static struct deferred_flush_tables *deferred_flush;
309
mark gross5e0d2a62008-03-04 15:22:08 -0800310/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800311static int g_num_of_iommus;
312
313static DEFINE_SPINLOCK(async_umap_flush_lock);
314static LIST_HEAD(unmaps_to_do);
315
316static int timer_on;
317static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800318
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700319static void domain_remove_dev_info(struct dmar_domain *domain);
320
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800321#ifdef CONFIG_DMAR_DEFAULT_ON
322int dmar_disabled = 0;
323#else
324int dmar_disabled = 1;
325#endif /*CONFIG_DMAR_DEFAULT_ON*/
326
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700327static int __initdata dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700328static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800329static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700330
331#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
332static DEFINE_SPINLOCK(device_domain_lock);
333static LIST_HEAD(device_domain_list);
334
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100335static struct iommu_ops intel_iommu_ops;
336
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700337static int __init intel_iommu_setup(char *str)
338{
339 if (!str)
340 return -EINVAL;
341 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800342 if (!strncmp(str, "on", 2)) {
343 dmar_disabled = 0;
344 printk(KERN_INFO "Intel-IOMMU: enabled\n");
345 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700346 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800347 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700348 } else if (!strncmp(str, "igfx_off", 8)) {
349 dmar_map_gfx = 0;
350 printk(KERN_INFO
351 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700352 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800353 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700354 "Intel-IOMMU: Forcing DAC for PCI devices\n");
355 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800356 } else if (!strncmp(str, "strict", 6)) {
357 printk(KERN_INFO
358 "Intel-IOMMU: disable batched IOTLB flush\n");
359 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700360 }
361
362 str += strcspn(str, ",");
363 while (*str == ',')
364 str++;
365 }
366 return 0;
367}
368__setup("intel_iommu=", intel_iommu_setup);
369
370static struct kmem_cache *iommu_domain_cache;
371static struct kmem_cache *iommu_devinfo_cache;
372static struct kmem_cache *iommu_iova_cache;
373
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700374static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
375{
376 unsigned int flags;
377 void *vaddr;
378
379 /* trying to avoid low memory issues */
380 flags = current->flags & PF_MEMALLOC;
381 current->flags |= PF_MEMALLOC;
382 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
383 current->flags &= (~PF_MEMALLOC | flags);
384 return vaddr;
385}
386
387
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700388static inline void *alloc_pgtable_page(void)
389{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700390 unsigned int flags;
391 void *vaddr;
392
393 /* trying to avoid low memory issues */
394 flags = current->flags & PF_MEMALLOC;
395 current->flags |= PF_MEMALLOC;
396 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
397 current->flags &= (~PF_MEMALLOC | flags);
398 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700399}
400
401static inline void free_pgtable_page(void *vaddr)
402{
403 free_page((unsigned long)vaddr);
404}
405
406static inline void *alloc_domain_mem(void)
407{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700408 return iommu_kmem_cache_alloc(iommu_domain_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700409}
410
Kay, Allen M38717942008-09-09 18:37:29 +0300411static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700412{
413 kmem_cache_free(iommu_domain_cache, vaddr);
414}
415
416static inline void * alloc_devinfo_mem(void)
417{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700418 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700419}
420
421static inline void free_devinfo_mem(void *vaddr)
422{
423 kmem_cache_free(iommu_devinfo_cache, vaddr);
424}
425
426struct iova *alloc_iova_mem(void)
427{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700428 return iommu_kmem_cache_alloc(iommu_iova_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700429}
430
431void free_iova_mem(struct iova *iova)
432{
433 kmem_cache_free(iommu_iova_cache, iova);
434}
435
Weidong Han1b573682008-12-08 15:34:06 +0800436
437static inline int width_to_agaw(int width);
438
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700439static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800440{
441 unsigned long sagaw;
442 int agaw = -1;
443
444 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700445 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800446 agaw >= 0; agaw--) {
447 if (test_bit(agaw, &sagaw))
448 break;
449 }
450
451 return agaw;
452}
453
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700454/*
455 * Calculate max SAGAW for each iommu.
456 */
457int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
458{
459 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
460}
461
462/*
463 * calculate agaw for each iommu.
464 * "SAGAW" may be different across iommus, use a default agaw, and
465 * get a supported less agaw for iommus that don't support the default agaw.
466 */
467int iommu_calculate_agaw(struct intel_iommu *iommu)
468{
469 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
470}
471
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700472/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800473static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
474{
475 int iommu_id;
476
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700477 /* si_domain and vm domain should not get here. */
Weidong Han1ce28fe2008-12-08 16:35:39 +0800478 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700479 BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
Weidong Han1ce28fe2008-12-08 16:35:39 +0800480
Weidong Han8c11e792008-12-08 15:29:22 +0800481 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
482 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
483 return NULL;
484
485 return g_iommus[iommu_id];
486}
487
Weidong Han8e6040972008-12-08 15:49:06 +0800488static void domain_update_iommu_coherency(struct dmar_domain *domain)
489{
490 int i;
491
492 domain->iommu_coherency = 1;
493
494 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
495 for (; i < g_num_of_iommus; ) {
496 if (!ecap_coherent(g_iommus[i]->ecap)) {
497 domain->iommu_coherency = 0;
498 break;
499 }
500 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
501 }
502}
503
Sheng Yang58c610b2009-03-18 15:33:05 +0800504static void domain_update_iommu_snooping(struct dmar_domain *domain)
505{
506 int i;
507
508 domain->iommu_snooping = 1;
509
510 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
511 for (; i < g_num_of_iommus; ) {
512 if (!ecap_sc_support(g_iommus[i]->ecap)) {
513 domain->iommu_snooping = 0;
514 break;
515 }
516 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
517 }
518}
519
520/* Some capabilities may be different across iommus */
521static void domain_update_iommu_cap(struct dmar_domain *domain)
522{
523 domain_update_iommu_coherency(domain);
524 domain_update_iommu_snooping(domain);
525}
526
David Woodhouse276dbf92009-04-04 01:45:37 +0100527static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800528{
529 struct dmar_drhd_unit *drhd = NULL;
530 int i;
531
532 for_each_drhd_unit(drhd) {
533 if (drhd->ignored)
534 continue;
David Woodhouse276dbf92009-04-04 01:45:37 +0100535 if (segment != drhd->segment)
536 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800537
David Woodhouse924b6232009-04-04 00:39:25 +0100538 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000539 if (drhd->devices[i] &&
540 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800541 drhd->devices[i]->devfn == devfn)
542 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700543 if (drhd->devices[i] &&
544 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100545 drhd->devices[i]->subordinate->number <= bus &&
546 drhd->devices[i]->subordinate->subordinate >= bus)
547 return drhd->iommu;
548 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800549
550 if (drhd->include_all)
551 return drhd->iommu;
552 }
553
554 return NULL;
555}
556
Weidong Han5331fe62008-12-08 23:00:00 +0800557static void domain_flush_cache(struct dmar_domain *domain,
558 void *addr, int size)
559{
560 if (!domain->iommu_coherency)
561 clflush_cache_range(addr, size);
562}
563
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700564/* Gets context entry for a given bus and devfn */
565static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
566 u8 bus, u8 devfn)
567{
568 struct root_entry *root;
569 struct context_entry *context;
570 unsigned long phy_addr;
571 unsigned long flags;
572
573 spin_lock_irqsave(&iommu->lock, flags);
574 root = &iommu->root_entry[bus];
575 context = get_context_addr_from_root(root);
576 if (!context) {
577 context = (struct context_entry *)alloc_pgtable_page();
578 if (!context) {
579 spin_unlock_irqrestore(&iommu->lock, flags);
580 return NULL;
581 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700582 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700583 phy_addr = virt_to_phys((void *)context);
584 set_root_value(root, phy_addr);
585 set_root_present(root);
586 __iommu_flush_cache(iommu, root, sizeof(*root));
587 }
588 spin_unlock_irqrestore(&iommu->lock, flags);
589 return &context[devfn];
590}
591
592static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
593{
594 struct root_entry *root;
595 struct context_entry *context;
596 int ret;
597 unsigned long flags;
598
599 spin_lock_irqsave(&iommu->lock, flags);
600 root = &iommu->root_entry[bus];
601 context = get_context_addr_from_root(root);
602 if (!context) {
603 ret = 0;
604 goto out;
605 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000606 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700607out:
608 spin_unlock_irqrestore(&iommu->lock, flags);
609 return ret;
610}
611
612static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
613{
614 struct root_entry *root;
615 struct context_entry *context;
616 unsigned long flags;
617
618 spin_lock_irqsave(&iommu->lock, flags);
619 root = &iommu->root_entry[bus];
620 context = get_context_addr_from_root(root);
621 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000622 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700623 __iommu_flush_cache(iommu, &context[devfn], \
624 sizeof(*context));
625 }
626 spin_unlock_irqrestore(&iommu->lock, flags);
627}
628
629static void free_context_table(struct intel_iommu *iommu)
630{
631 struct root_entry *root;
632 int i;
633 unsigned long flags;
634 struct context_entry *context;
635
636 spin_lock_irqsave(&iommu->lock, flags);
637 if (!iommu->root_entry) {
638 goto out;
639 }
640 for (i = 0; i < ROOT_ENTRY_NR; i++) {
641 root = &iommu->root_entry[i];
642 context = get_context_addr_from_root(root);
643 if (context)
644 free_pgtable_page(context);
645 }
646 free_pgtable_page(iommu->root_entry);
647 iommu->root_entry = NULL;
648out:
649 spin_unlock_irqrestore(&iommu->lock, flags);
650}
651
652/* page table handling */
653#define LEVEL_STRIDE (9)
654#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
655
656static inline int agaw_to_level(int agaw)
657{
658 return agaw + 2;
659}
660
661static inline int agaw_to_width(int agaw)
662{
663 return 30 + agaw * LEVEL_STRIDE;
664
665}
666
667static inline int width_to_agaw(int width)
668{
669 return (width - 30) / LEVEL_STRIDE;
670}
671
672static inline unsigned int level_to_offset_bits(int level)
673{
David Woodhouse6660c632009-06-27 22:41:00 +0100674 return (level - 1) * LEVEL_STRIDE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700675}
676
David Woodhouse77dfa562009-06-27 16:40:08 +0100677static inline int pfn_level_offset(unsigned long pfn, int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700678{
David Woodhouse6660c632009-06-27 22:41:00 +0100679 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700680}
681
David Woodhouse6660c632009-06-27 22:41:00 +0100682static inline unsigned long level_mask(int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700683{
David Woodhouse6660c632009-06-27 22:41:00 +0100684 return -1UL << level_to_offset_bits(level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700685}
686
David Woodhouse6660c632009-06-27 22:41:00 +0100687static inline unsigned long level_size(int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700688{
David Woodhouse6660c632009-06-27 22:41:00 +0100689 return 1UL << level_to_offset_bits(level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700690}
691
David Woodhouse6660c632009-06-27 22:41:00 +0100692static inline unsigned long align_to_level(unsigned long pfn, int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700693{
David Woodhouse6660c632009-06-27 22:41:00 +0100694 return (pfn + level_size(level) - 1) & level_mask(level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700695}
696
David Woodhouseb026fd22009-06-28 10:37:25 +0100697static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
698 unsigned long pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700699{
David Woodhouseb026fd22009-06-28 10:37:25 +0100700 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700701 struct dma_pte *parent, *pte = NULL;
702 int level = agaw_to_level(domain->agaw);
703 int offset;
704 unsigned long flags;
705
706 BUG_ON(!domain->pgd);
David Woodhouseb026fd22009-06-28 10:37:25 +0100707 BUG_ON(addr_width < BITS_PER_LONG && pfn >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700708 parent = domain->pgd;
709
710 spin_lock_irqsave(&domain->mapping_lock, flags);
711 while (level > 0) {
712 void *tmp_page;
713
David Woodhouseb026fd22009-06-28 10:37:25 +0100714 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700715 pte = &parent[offset];
716 if (level == 1)
717 break;
718
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000719 if (!dma_pte_present(pte)) {
David Woodhousec85994e2009-07-01 19:21:24 +0100720 uint64_t pteval;
721
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700722 tmp_page = alloc_pgtable_page();
723
724 if (!tmp_page) {
725 spin_unlock_irqrestore(&domain->mapping_lock,
726 flags);
727 return NULL;
728 }
David Woodhousec85994e2009-07-01 19:21:24 +0100729 domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
730 pteval = (virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE;
731 if (cmpxchg64(&pte->val, 0ULL, pteval)) {
732 /* Someone else set it while we were thinking; use theirs. */
733 free_pgtable_page(tmp_page);
734 } else {
735 dma_pte_addr(pte);
736 domain_flush_cache(domain, pte, sizeof(*pte));
737 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700738 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000739 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700740 level--;
741 }
742
743 spin_unlock_irqrestore(&domain->mapping_lock, flags);
744 return pte;
745}
746
747/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100748static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
749 unsigned long pfn,
750 int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700751{
752 struct dma_pte *parent, *pte = NULL;
753 int total = agaw_to_level(domain->agaw);
754 int offset;
755
756 parent = domain->pgd;
757 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100758 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700759 pte = &parent[offset];
760 if (level == total)
761 return pte;
762
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000763 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700764 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000765 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700766 total--;
767 }
768 return NULL;
769}
770
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700771/* clear last level pte, a tlb flush should be followed */
David Woodhouse595badf2009-06-27 22:09:11 +0100772static void dma_pte_clear_range(struct dmar_domain *domain,
773 unsigned long start_pfn,
774 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700775{
David Woodhouse04b18e62009-06-27 19:15:01 +0100776 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhouse310a5ab2009-06-28 18:52:20 +0100777 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700778
David Woodhouse04b18e62009-06-27 19:15:01 +0100779 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
David Woodhouse595badf2009-06-27 22:09:11 +0100780 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse66eae842009-06-27 19:00:32 +0100781
David Woodhouse04b18e62009-06-27 19:15:01 +0100782 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse595badf2009-06-27 22:09:11 +0100783 while (start_pfn <= last_pfn) {
David Woodhouse310a5ab2009-06-28 18:52:20 +0100784 first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1);
785 if (!pte) {
786 start_pfn = align_to_level(start_pfn + 1, 2);
787 continue;
788 }
789 while (start_pfn <= last_pfn &&
790 (unsigned long)pte >> VTD_PAGE_SHIFT ==
791 (unsigned long)first_pte >> VTD_PAGE_SHIFT) {
792 dma_clear_pte(pte);
793 start_pfn++;
794 pte++;
795 }
796 domain_flush_cache(domain, first_pte,
797 (void *)pte - (void *)first_pte);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700798 }
799}
800
801/* free page table pages. last level pte should already be cleared */
802static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +0100803 unsigned long start_pfn,
804 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700805{
David Woodhouse6660c632009-06-27 22:41:00 +0100806 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhousef3a0a522009-06-30 03:40:07 +0100807 struct dma_pte *first_pte, *pte;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700808 int total = agaw_to_level(domain->agaw);
809 int level;
David Woodhouse6660c632009-06-27 22:41:00 +0100810 unsigned long tmp;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700811
David Woodhouse6660c632009-06-27 22:41:00 +0100812 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
813 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700814
David Woodhousef3a0a522009-06-30 03:40:07 +0100815 /* We don't need lock here; nobody else touches the iova range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700816 level = 2;
817 while (level <= total) {
David Woodhouse6660c632009-06-27 22:41:00 +0100818 tmp = align_to_level(start_pfn, level);
819
David Woodhousef3a0a522009-06-30 03:40:07 +0100820 /* If we can't even clear one PTE at this level, we're done */
David Woodhouse6660c632009-06-27 22:41:00 +0100821 if (tmp + level_size(level) - 1 > last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700822 return;
823
David Woodhouse3d7b0e42009-06-30 03:38:09 +0100824 while (tmp + level_size(level) - 1 <= last_pfn) {
David Woodhousef3a0a522009-06-30 03:40:07 +0100825 first_pte = pte = dma_pfn_level_pte(domain, tmp, level);
826 if (!pte) {
827 tmp = align_to_level(tmp + 1, level + 1);
828 continue;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700829 }
David Woodhousef3a0a522009-06-30 03:40:07 +0100830 while (tmp + level_size(level) - 1 <= last_pfn &&
831 (unsigned long)pte >> VTD_PAGE_SHIFT ==
832 (unsigned long)first_pte >> VTD_PAGE_SHIFT) {
833 free_pgtable_page(phys_to_virt(dma_pte_addr(pte)));
834 dma_clear_pte(pte);
835 pte++;
836 tmp += level_size(level);
837 }
838 domain_flush_cache(domain, first_pte,
839 (void *)pte - (void *)first_pte);
840
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700841 }
842 level++;
843 }
844 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +0100845 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700846 free_pgtable_page(domain->pgd);
847 domain->pgd = NULL;
848 }
849}
850
851/* iommu handling */
852static int iommu_alloc_root_entry(struct intel_iommu *iommu)
853{
854 struct root_entry *root;
855 unsigned long flags;
856
857 root = (struct root_entry *)alloc_pgtable_page();
858 if (!root)
859 return -ENOMEM;
860
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700861 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700862
863 spin_lock_irqsave(&iommu->lock, flags);
864 iommu->root_entry = root;
865 spin_unlock_irqrestore(&iommu->lock, flags);
866
867 return 0;
868}
869
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700870static void iommu_set_root_entry(struct intel_iommu *iommu)
871{
872 void *addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100873 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700874 unsigned long flag;
875
876 addr = iommu->root_entry;
877
878 spin_lock_irqsave(&iommu->register_lock, flag);
879 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
880
David Woodhousec416daa2009-05-10 20:30:58 +0100881 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700882
883 /* Make sure hardware complete it */
884 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100885 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700886
887 spin_unlock_irqrestore(&iommu->register_lock, flag);
888}
889
890static void iommu_flush_write_buffer(struct intel_iommu *iommu)
891{
892 u32 val;
893 unsigned long flag;
894
David Woodhouse9af88142009-02-13 23:18:03 +0000895 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700896 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700897
898 spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +0100899 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700900
901 /* Make sure hardware complete it */
902 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100903 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700904
905 spin_unlock_irqrestore(&iommu->register_lock, flag);
906}
907
908/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +0100909static void __iommu_flush_context(struct intel_iommu *iommu,
910 u16 did, u16 source_id, u8 function_mask,
911 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700912{
913 u64 val = 0;
914 unsigned long flag;
915
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700916 switch (type) {
917 case DMA_CCMD_GLOBAL_INVL:
918 val = DMA_CCMD_GLOBAL_INVL;
919 break;
920 case DMA_CCMD_DOMAIN_INVL:
921 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
922 break;
923 case DMA_CCMD_DEVICE_INVL:
924 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
925 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
926 break;
927 default:
928 BUG();
929 }
930 val |= DMA_CCMD_ICC;
931
932 spin_lock_irqsave(&iommu->register_lock, flag);
933 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
934
935 /* Make sure hardware complete it */
936 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
937 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
938
939 spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700940}
941
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700942/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100943static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
944 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700945{
946 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
947 u64 val = 0, val_iva = 0;
948 unsigned long flag;
949
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700950 switch (type) {
951 case DMA_TLB_GLOBAL_FLUSH:
952 /* global flush doesn't need set IVA_REG */
953 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
954 break;
955 case DMA_TLB_DSI_FLUSH:
956 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
957 break;
958 case DMA_TLB_PSI_FLUSH:
959 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
960 /* Note: always flush non-leaf currently */
961 val_iva = size_order | addr;
962 break;
963 default:
964 BUG();
965 }
966 /* Note: set drain read/write */
967#if 0
968 /*
969 * This is probably to be super secure.. Looks like we can
970 * ignore it without any impact.
971 */
972 if (cap_read_drain(iommu->cap))
973 val |= DMA_TLB_READ_DRAIN;
974#endif
975 if (cap_write_drain(iommu->cap))
976 val |= DMA_TLB_WRITE_DRAIN;
977
978 spin_lock_irqsave(&iommu->register_lock, flag);
979 /* Note: Only uses first TLB reg currently */
980 if (val_iva)
981 dmar_writeq(iommu->reg + tlb_offset, val_iva);
982 dmar_writeq(iommu->reg + tlb_offset + 8, val);
983
984 /* Make sure hardware complete it */
985 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
986 dmar_readq, (!(val & DMA_TLB_IVT)), val);
987
988 spin_unlock_irqrestore(&iommu->register_lock, flag);
989
990 /* check IOTLB invalidation granularity */
991 if (DMA_TLB_IAIG(val) == 0)
992 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
993 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
994 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700995 (unsigned long long)DMA_TLB_IIRG(type),
996 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700997}
998
Yu Zhao93a23a72009-05-18 13:51:37 +0800999static struct device_domain_info *iommu_support_dev_iotlb(
1000 struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001001{
Yu Zhao93a23a72009-05-18 13:51:37 +08001002 int found = 0;
1003 unsigned long flags;
1004 struct device_domain_info *info;
1005 struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);
1006
1007 if (!ecap_dev_iotlb_support(iommu->ecap))
1008 return NULL;
1009
1010 if (!iommu->qi)
1011 return NULL;
1012
1013 spin_lock_irqsave(&device_domain_lock, flags);
1014 list_for_each_entry(info, &domain->devices, link)
1015 if (info->bus == bus && info->devfn == devfn) {
1016 found = 1;
1017 break;
1018 }
1019 spin_unlock_irqrestore(&device_domain_lock, flags);
1020
1021 if (!found || !info->dev)
1022 return NULL;
1023
1024 if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
1025 return NULL;
1026
1027 if (!dmar_find_matched_atsr_unit(info->dev))
1028 return NULL;
1029
1030 info->iommu = iommu;
1031
1032 return info;
1033}
1034
1035static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1036{
1037 if (!info)
1038 return;
1039
1040 pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
1041}
1042
1043static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1044{
1045 if (!info->dev || !pci_ats_enabled(info->dev))
1046 return;
1047
1048 pci_disable_ats(info->dev);
1049}
1050
1051static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1052 u64 addr, unsigned mask)
1053{
1054 u16 sid, qdep;
1055 unsigned long flags;
1056 struct device_domain_info *info;
1057
1058 spin_lock_irqsave(&device_domain_lock, flags);
1059 list_for_each_entry(info, &domain->devices, link) {
1060 if (!info->dev || !pci_ats_enabled(info->dev))
1061 continue;
1062
1063 sid = info->bus << 8 | info->devfn;
1064 qdep = pci_ats_queue_depth(info->dev);
1065 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1066 }
1067 spin_unlock_irqrestore(&device_domain_lock, flags);
1068}
1069
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001070static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
David Woodhouse03d6a242009-06-28 15:33:46 +01001071 unsigned long pfn, unsigned int pages)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001072{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001073 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001074 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001075
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001076 BUG_ON(pages == 0);
1077
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001078 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001079 * Fallback to domain selective flush if no PSI support or the size is
1080 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001081 * PSI requires page size to be 2 ^ x, and the base address is naturally
1082 * aligned to the size
1083 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001084 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1085 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001086 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001087 else
1088 iommu->flush.flush_iotlb(iommu, did, addr, mask,
1089 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001090
1091 /*
1092 * In caching mode, domain ID 0 is reserved for non-present to present
1093 * mapping flush. Device IOTLB doesn't need to be flushed in this case.
1094 */
1095 if (!cap_caching_mode(iommu->cap) || did)
Yu Zhao93a23a72009-05-18 13:51:37 +08001096 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001097}
1098
mark grossf8bab732008-02-08 04:18:38 -08001099static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1100{
1101 u32 pmen;
1102 unsigned long flags;
1103
1104 spin_lock_irqsave(&iommu->register_lock, flags);
1105 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1106 pmen &= ~DMA_PMEN_EPM;
1107 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1108
1109 /* wait for the protected region status bit to clear */
1110 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1111 readl, !(pmen & DMA_PMEN_PRS), pmen);
1112
1113 spin_unlock_irqrestore(&iommu->register_lock, flags);
1114}
1115
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001116static int iommu_enable_translation(struct intel_iommu *iommu)
1117{
1118 u32 sts;
1119 unsigned long flags;
1120
1121 spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001122 iommu->gcmd |= DMA_GCMD_TE;
1123 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001124
1125 /* Make sure hardware complete it */
1126 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001127 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001128
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001129 spin_unlock_irqrestore(&iommu->register_lock, flags);
1130 return 0;
1131}
1132
1133static int iommu_disable_translation(struct intel_iommu *iommu)
1134{
1135 u32 sts;
1136 unsigned long flag;
1137
1138 spin_lock_irqsave(&iommu->register_lock, flag);
1139 iommu->gcmd &= ~DMA_GCMD_TE;
1140 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1141
1142 /* Make sure hardware complete it */
1143 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001144 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001145
1146 spin_unlock_irqrestore(&iommu->register_lock, flag);
1147 return 0;
1148}
1149
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001150
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001151static int iommu_init_domains(struct intel_iommu *iommu)
1152{
1153 unsigned long ndomains;
1154 unsigned long nlongs;
1155
1156 ndomains = cap_ndoms(iommu->cap);
1157 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1158 nlongs = BITS_TO_LONGS(ndomains);
1159
1160 /* TBD: there might be 64K domains,
1161 * consider other allocation for future chip
1162 */
1163 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1164 if (!iommu->domain_ids) {
1165 printk(KERN_ERR "Allocating domain id array failed\n");
1166 return -ENOMEM;
1167 }
1168 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1169 GFP_KERNEL);
1170 if (!iommu->domains) {
1171 printk(KERN_ERR "Allocating domain array failed\n");
1172 kfree(iommu->domain_ids);
1173 return -ENOMEM;
1174 }
1175
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001176 spin_lock_init(&iommu->lock);
1177
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001178 /*
1179 * if Caching mode is set, then invalid translations are tagged
1180 * with domainid 0. Hence we need to pre-allocate it.
1181 */
1182 if (cap_caching_mode(iommu->cap))
1183 set_bit(0, iommu->domain_ids);
1184 return 0;
1185}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001186
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001187
1188static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001189static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001190
1191void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001192{
1193 struct dmar_domain *domain;
1194 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001195 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001196
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001197 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1198 for (; i < cap_ndoms(iommu->cap); ) {
1199 domain = iommu->domains[i];
1200 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001201
1202 spin_lock_irqsave(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001203 if (--domain->iommu_count == 0) {
1204 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1205 vm_domain_exit(domain);
1206 else
1207 domain_exit(domain);
1208 }
Weidong Hanc7151a82008-12-08 22:51:37 +08001209 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1210
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001211 i = find_next_bit(iommu->domain_ids,
1212 cap_ndoms(iommu->cap), i+1);
1213 }
1214
1215 if (iommu->gcmd & DMA_GCMD_TE)
1216 iommu_disable_translation(iommu);
1217
1218 if (iommu->irq) {
1219 set_irq_data(iommu->irq, NULL);
1220 /* This will mask the irq */
1221 free_irq(iommu->irq, iommu);
1222 destroy_irq(iommu->irq);
1223 }
1224
1225 kfree(iommu->domains);
1226 kfree(iommu->domain_ids);
1227
Weidong Hand9630fe2008-12-08 11:06:32 +08001228 g_iommus[iommu->seq_id] = NULL;
1229
1230 /* if all iommus are freed, free g_iommus */
1231 for (i = 0; i < g_num_of_iommus; i++) {
1232 if (g_iommus[i])
1233 break;
1234 }
1235
1236 if (i == g_num_of_iommus)
1237 kfree(g_iommus);
1238
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001239 /* free context mapping */
1240 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001241}
1242
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001243static struct dmar_domain *alloc_domain(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001244{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001245 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001246
1247 domain = alloc_domain_mem();
1248 if (!domain)
1249 return NULL;
1250
Weidong Han8c11e792008-12-08 15:29:22 +08001251 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
Weidong Hand71a2f32008-12-07 21:13:41 +08001252 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001253
1254 return domain;
1255}
1256
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001257static int iommu_attach_domain(struct dmar_domain *domain,
1258 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001259{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001260 int num;
1261 unsigned long ndomains;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001262 unsigned long flags;
1263
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001264 ndomains = cap_ndoms(iommu->cap);
Weidong Han8c11e792008-12-08 15:29:22 +08001265
1266 spin_lock_irqsave(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001267
1268 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1269 if (num >= ndomains) {
1270 spin_unlock_irqrestore(&iommu->lock, flags);
1271 printk(KERN_ERR "IOMMU: no free domain ids\n");
1272 return -ENOMEM;
1273 }
1274
1275 domain->id = num;
1276 set_bit(num, iommu->domain_ids);
1277 set_bit(iommu->seq_id, &domain->iommu_bmp);
1278 iommu->domains[num] = domain;
1279 spin_unlock_irqrestore(&iommu->lock, flags);
1280
1281 return 0;
1282}
1283
1284static void iommu_detach_domain(struct dmar_domain *domain,
1285 struct intel_iommu *iommu)
1286{
1287 unsigned long flags;
1288 int num, ndomains;
1289 int found = 0;
1290
1291 spin_lock_irqsave(&iommu->lock, flags);
1292 ndomains = cap_ndoms(iommu->cap);
1293 num = find_first_bit(iommu->domain_ids, ndomains);
1294 for (; num < ndomains; ) {
1295 if (iommu->domains[num] == domain) {
1296 found = 1;
1297 break;
1298 }
1299 num = find_next_bit(iommu->domain_ids,
1300 cap_ndoms(iommu->cap), num+1);
1301 }
1302
1303 if (found) {
1304 clear_bit(num, iommu->domain_ids);
1305 clear_bit(iommu->seq_id, &domain->iommu_bmp);
1306 iommu->domains[num] = NULL;
1307 }
Weidong Han8c11e792008-12-08 15:29:22 +08001308 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001309}
1310
1311static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001312static struct lock_class_key reserved_alloc_key;
1313static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001314
1315static void dmar_init_reserved_ranges(void)
1316{
1317 struct pci_dev *pdev = NULL;
1318 struct iova *iova;
1319 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001320
David Millerf6611972008-02-06 01:36:23 -08001321 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001322
Mark Gross8a443df2008-03-04 14:59:31 -08001323 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1324 &reserved_alloc_key);
1325 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1326 &reserved_rbtree_key);
1327
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001328 /* IOAPIC ranges shouldn't be accessed by DMA */
1329 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1330 IOVA_PFN(IOAPIC_RANGE_END));
1331 if (!iova)
1332 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1333
1334 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1335 for_each_pci_dev(pdev) {
1336 struct resource *r;
1337
1338 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1339 r = &pdev->resource[i];
1340 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1341 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001342 iova = reserve_iova(&reserved_iova_list,
1343 IOVA_PFN(r->start),
1344 IOVA_PFN(r->end));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001345 if (!iova)
1346 printk(KERN_ERR "Reserve iova failed\n");
1347 }
1348 }
1349
1350}
1351
1352static void domain_reserve_special_ranges(struct dmar_domain *domain)
1353{
1354 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1355}
1356
1357static inline int guestwidth_to_adjustwidth(int gaw)
1358{
1359 int agaw;
1360 int r = (gaw - 12) % 9;
1361
1362 if (r == 0)
1363 agaw = gaw;
1364 else
1365 agaw = gaw + 9 - r;
1366 if (agaw > 64)
1367 agaw = 64;
1368 return agaw;
1369}
1370
1371static int domain_init(struct dmar_domain *domain, int guest_width)
1372{
1373 struct intel_iommu *iommu;
1374 int adjust_width, agaw;
1375 unsigned long sagaw;
1376
David Millerf6611972008-02-06 01:36:23 -08001377 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001378 spin_lock_init(&domain->mapping_lock);
Weidong Hanc7151a82008-12-08 22:51:37 +08001379 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001380
1381 domain_reserve_special_ranges(domain);
1382
1383 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001384 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001385 if (guest_width > cap_mgaw(iommu->cap))
1386 guest_width = cap_mgaw(iommu->cap);
1387 domain->gaw = guest_width;
1388 adjust_width = guestwidth_to_adjustwidth(guest_width);
1389 agaw = width_to_agaw(adjust_width);
1390 sagaw = cap_sagaw(iommu->cap);
1391 if (!test_bit(agaw, &sagaw)) {
1392 /* hardware doesn't support it, choose a bigger one */
1393 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1394 agaw = find_next_bit(&sagaw, 5, agaw);
1395 if (agaw >= 5)
1396 return -ENODEV;
1397 }
1398 domain->agaw = agaw;
1399 INIT_LIST_HEAD(&domain->devices);
1400
Weidong Han8e6040972008-12-08 15:49:06 +08001401 if (ecap_coherent(iommu->ecap))
1402 domain->iommu_coherency = 1;
1403 else
1404 domain->iommu_coherency = 0;
1405
Sheng Yang58c610b2009-03-18 15:33:05 +08001406 if (ecap_sc_support(iommu->ecap))
1407 domain->iommu_snooping = 1;
1408 else
1409 domain->iommu_snooping = 0;
1410
Weidong Hanc7151a82008-12-08 22:51:37 +08001411 domain->iommu_count = 1;
1412
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001413 /* always allocate the top pgd */
1414 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1415 if (!domain->pgd)
1416 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001417 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001418 return 0;
1419}
1420
1421static void domain_exit(struct dmar_domain *domain)
1422{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001423 struct dmar_drhd_unit *drhd;
1424 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001425
1426 /* Domain 0 is reserved, so dont process it */
1427 if (!domain)
1428 return;
1429
1430 domain_remove_dev_info(domain);
1431 /* destroy iovas */
1432 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001433
1434 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01001435 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001436
1437 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01001438 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001439
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001440 for_each_active_iommu(iommu, drhd)
1441 if (test_bit(iommu->seq_id, &domain->iommu_bmp))
1442 iommu_detach_domain(domain, iommu);
1443
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001444 free_domain_mem(domain);
1445}
1446
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001447static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1448 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001449{
1450 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001451 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001452 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001453 struct dma_pte *pgd;
1454 unsigned long num;
1455 unsigned long ndomains;
1456 int id;
1457 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001458 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001459
1460 pr_debug("Set context mapping for %02x:%02x.%d\n",
1461 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001462
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001463 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001464 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1465 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001466
David Woodhouse276dbf92009-04-04 01:45:37 +01001467 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001468 if (!iommu)
1469 return -ENODEV;
1470
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001471 context = device_to_context_entry(iommu, bus, devfn);
1472 if (!context)
1473 return -ENOMEM;
1474 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001475 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001476 spin_unlock_irqrestore(&iommu->lock, flags);
1477 return 0;
1478 }
1479
Weidong Hanea6606b2008-12-08 23:08:15 +08001480 id = domain->id;
1481 pgd = domain->pgd;
1482
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001483 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
1484 domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001485 int found = 0;
1486
1487 /* find an available domain id for this device in iommu */
1488 ndomains = cap_ndoms(iommu->cap);
1489 num = find_first_bit(iommu->domain_ids, ndomains);
1490 for (; num < ndomains; ) {
1491 if (iommu->domains[num] == domain) {
1492 id = num;
1493 found = 1;
1494 break;
1495 }
1496 num = find_next_bit(iommu->domain_ids,
1497 cap_ndoms(iommu->cap), num+1);
1498 }
1499
1500 if (found == 0) {
1501 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1502 if (num >= ndomains) {
1503 spin_unlock_irqrestore(&iommu->lock, flags);
1504 printk(KERN_ERR "IOMMU: no free domain ids\n");
1505 return -EFAULT;
1506 }
1507
1508 set_bit(num, iommu->domain_ids);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001509 set_bit(iommu->seq_id, &domain->iommu_bmp);
Weidong Hanea6606b2008-12-08 23:08:15 +08001510 iommu->domains[num] = domain;
1511 id = num;
1512 }
1513
1514 /* Skip top levels of page tables for
1515 * iommu which has less agaw than default.
1516 */
1517 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1518 pgd = phys_to_virt(dma_pte_addr(pgd));
1519 if (!dma_pte_present(pgd)) {
1520 spin_unlock_irqrestore(&iommu->lock, flags);
1521 return -ENOMEM;
1522 }
1523 }
1524 }
1525
1526 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001527
Yu Zhao93a23a72009-05-18 13:51:37 +08001528 if (translation != CONTEXT_TT_PASS_THROUGH) {
1529 info = iommu_support_dev_iotlb(domain, segment, bus, devfn);
1530 translation = info ? CONTEXT_TT_DEV_IOTLB :
1531 CONTEXT_TT_MULTI_LEVEL;
1532 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001533 /*
1534 * In pass through mode, AW must be programmed to indicate the largest
1535 * AGAW value supported by hardware. And ASR is ignored by hardware.
1536 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001537 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001538 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001539 else {
1540 context_set_address_root(context, virt_to_phys(pgd));
1541 context_set_address_width(context, iommu->agaw);
1542 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001543
1544 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001545 context_set_fault_enable(context);
1546 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001547 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001548
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001549 /*
1550 * It's a non-present to present mapping. If hardware doesn't cache
1551 * non-present entry we only need to flush the write-buffer. If the
1552 * _does_ cache non-present entries, then it does so in the special
1553 * domain #0, which we have to flush:
1554 */
1555 if (cap_caching_mode(iommu->cap)) {
1556 iommu->flush.flush_context(iommu, 0,
1557 (((u16)bus) << 8) | devfn,
1558 DMA_CCMD_MASK_NOBIT,
1559 DMA_CCMD_DEVICE_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001560 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001561 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001562 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001563 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001564 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001565 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001566
1567 spin_lock_irqsave(&domain->iommu_lock, flags);
1568 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1569 domain->iommu_count++;
Sheng Yang58c610b2009-03-18 15:33:05 +08001570 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001571 }
1572 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001573 return 0;
1574}
1575
1576static int
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001577domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1578 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001579{
1580 int ret;
1581 struct pci_dev *tmp, *parent;
1582
David Woodhouse276dbf92009-04-04 01:45:37 +01001583 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001584 pdev->bus->number, pdev->devfn,
1585 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001586 if (ret)
1587 return ret;
1588
1589 /* dependent device mapping */
1590 tmp = pci_find_upstream_pcie_bridge(pdev);
1591 if (!tmp)
1592 return 0;
1593 /* Secondary interface's bus number and devfn 0 */
1594 parent = pdev->bus->self;
1595 while (parent != tmp) {
David Woodhouse276dbf92009-04-04 01:45:37 +01001596 ret = domain_context_mapping_one(domain,
1597 pci_domain_nr(parent->bus),
1598 parent->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001599 parent->devfn, translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001600 if (ret)
1601 return ret;
1602 parent = parent->bus->self;
1603 }
1604 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1605 return domain_context_mapping_one(domain,
David Woodhouse276dbf92009-04-04 01:45:37 +01001606 pci_domain_nr(tmp->subordinate),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001607 tmp->subordinate->number, 0,
1608 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001609 else /* this is a legacy PCI bridge */
1610 return domain_context_mapping_one(domain,
David Woodhouse276dbf92009-04-04 01:45:37 +01001611 pci_domain_nr(tmp->bus),
1612 tmp->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001613 tmp->devfn,
1614 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001615}
1616
Weidong Han5331fe62008-12-08 23:00:00 +08001617static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001618{
1619 int ret;
1620 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001621 struct intel_iommu *iommu;
1622
David Woodhouse276dbf92009-04-04 01:45:37 +01001623 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1624 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001625 if (!iommu)
1626 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001627
David Woodhouse276dbf92009-04-04 01:45:37 +01001628 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001629 if (!ret)
1630 return ret;
1631 /* dependent device mapping */
1632 tmp = pci_find_upstream_pcie_bridge(pdev);
1633 if (!tmp)
1634 return ret;
1635 /* Secondary interface's bus number and devfn 0 */
1636 parent = pdev->bus->self;
1637 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001638 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf92009-04-04 01:45:37 +01001639 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001640 if (!ret)
1641 return ret;
1642 parent = parent->bus->self;
1643 }
1644 if (tmp->is_pcie)
David Woodhouse276dbf92009-04-04 01:45:37 +01001645 return device_context_mapped(iommu, tmp->subordinate->number,
1646 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001647 else
David Woodhouse276dbf92009-04-04 01:45:37 +01001648 return device_context_mapped(iommu, tmp->bus->number,
1649 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001650}
1651
David Woodhouse9051aa02009-06-29 12:30:54 +01001652static int __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1653 struct scatterlist *sg, unsigned long phys_pfn,
1654 unsigned long nr_pages, int prot)
David Woodhousee1605492009-06-29 11:17:38 +01001655{
1656 struct dma_pte *first_pte = NULL, *pte = NULL;
David Woodhouse9051aa02009-06-29 12:30:54 +01001657 phys_addr_t uninitialized_var(pteval);
David Woodhousee1605492009-06-29 11:17:38 +01001658 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
David Woodhouse9051aa02009-06-29 12:30:54 +01001659 unsigned long sg_res;
David Woodhousee1605492009-06-29 11:17:38 +01001660
1661 BUG_ON(addr_width < BITS_PER_LONG && (iov_pfn + nr_pages - 1) >> addr_width);
1662
1663 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1664 return -EINVAL;
1665
1666 prot &= DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP;
1667
David Woodhouse9051aa02009-06-29 12:30:54 +01001668 if (sg)
1669 sg_res = 0;
1670 else {
1671 sg_res = nr_pages + 1;
1672 pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | prot;
1673 }
1674
David Woodhousee1605492009-06-29 11:17:38 +01001675 while (nr_pages--) {
David Woodhousec85994e2009-07-01 19:21:24 +01001676 uint64_t tmp;
1677
David Woodhousee1605492009-06-29 11:17:38 +01001678 if (!sg_res) {
1679 sg_res = (sg->offset + sg->length + VTD_PAGE_SIZE - 1) >> VTD_PAGE_SHIFT;
1680 sg->dma_address = ((dma_addr_t)iov_pfn << VTD_PAGE_SHIFT) + sg->offset;
1681 sg->dma_length = sg->length;
1682 pteval = page_to_phys(sg_page(sg)) | prot;
1683 }
1684 if (!pte) {
1685 first_pte = pte = pfn_to_dma_pte(domain, iov_pfn);
1686 if (!pte)
1687 return -ENOMEM;
1688 }
1689 /* We don't need lock here, nobody else
1690 * touches the iova range
1691 */
David Woodhousec85994e2009-07-01 19:21:24 +01001692 tmp = cmpxchg64(&pte->val, 0ULL, pteval);
1693 if (tmp) {
David Woodhouse1bf20f02009-06-29 22:06:43 +01001694 static int dumps = 5;
David Woodhousec85994e2009-07-01 19:21:24 +01001695 printk(KERN_CRIT "ERROR: DMA PTE for vPFN 0x%lx already set (to %llx not %llx)\n",
1696 iov_pfn, tmp, (unsigned long long)pteval);
David Woodhouse1bf20f02009-06-29 22:06:43 +01001697 if (dumps) {
1698 dumps--;
1699 debug_dma_dump_mappings(NULL);
1700 }
1701 WARN_ON(1);
1702 }
David Woodhousee1605492009-06-29 11:17:38 +01001703 pte++;
1704 if (!nr_pages ||
1705 (unsigned long)pte >> VTD_PAGE_SHIFT !=
1706 (unsigned long)first_pte >> VTD_PAGE_SHIFT) {
1707 domain_flush_cache(domain, first_pte,
1708 (void *)pte - (void *)first_pte);
1709 pte = NULL;
1710 }
1711 iov_pfn++;
1712 pteval += VTD_PAGE_SIZE;
1713 sg_res--;
1714 if (!sg_res)
1715 sg = sg_next(sg);
1716 }
1717 return 0;
1718}
1719
David Woodhouse9051aa02009-06-29 12:30:54 +01001720static inline int domain_sg_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1721 struct scatterlist *sg, unsigned long nr_pages,
1722 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001723{
David Woodhouse9051aa02009-06-29 12:30:54 +01001724 return __domain_mapping(domain, iov_pfn, sg, 0, nr_pages, prot);
1725}
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001726
David Woodhouse9051aa02009-06-29 12:30:54 +01001727static inline int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1728 unsigned long phys_pfn, unsigned long nr_pages,
1729 int prot)
1730{
1731 return __domain_mapping(domain, iov_pfn, NULL, phys_pfn, nr_pages, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001732}
1733
Weidong Hanc7151a82008-12-08 22:51:37 +08001734static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001735{
Weidong Hanc7151a82008-12-08 22:51:37 +08001736 if (!iommu)
1737 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001738
1739 clear_context_table(iommu, bus, devfn);
1740 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001741 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001742 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001743}
1744
1745static void domain_remove_dev_info(struct dmar_domain *domain)
1746{
1747 struct device_domain_info *info;
1748 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001749 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001750
1751 spin_lock_irqsave(&device_domain_lock, flags);
1752 while (!list_empty(&domain->devices)) {
1753 info = list_entry(domain->devices.next,
1754 struct device_domain_info, link);
1755 list_del(&info->link);
1756 list_del(&info->global);
1757 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001758 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001759 spin_unlock_irqrestore(&device_domain_lock, flags);
1760
Yu Zhao93a23a72009-05-18 13:51:37 +08001761 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf92009-04-04 01:45:37 +01001762 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001763 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001764 free_devinfo_mem(info);
1765
1766 spin_lock_irqsave(&device_domain_lock, flags);
1767 }
1768 spin_unlock_irqrestore(&device_domain_lock, flags);
1769}
1770
1771/*
1772 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001773 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001774 */
Kay, Allen M38717942008-09-09 18:37:29 +03001775static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001776find_domain(struct pci_dev *pdev)
1777{
1778 struct device_domain_info *info;
1779
1780 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001781 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001782 if (info)
1783 return info->domain;
1784 return NULL;
1785}
1786
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001787/* domain is initialized */
1788static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1789{
1790 struct dmar_domain *domain, *found = NULL;
1791 struct intel_iommu *iommu;
1792 struct dmar_drhd_unit *drhd;
1793 struct device_domain_info *info, *tmp;
1794 struct pci_dev *dev_tmp;
1795 unsigned long flags;
1796 int bus = 0, devfn = 0;
David Woodhouse276dbf92009-04-04 01:45:37 +01001797 int segment;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001798 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001799
1800 domain = find_domain(pdev);
1801 if (domain)
1802 return domain;
1803
David Woodhouse276dbf92009-04-04 01:45:37 +01001804 segment = pci_domain_nr(pdev->bus);
1805
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001806 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1807 if (dev_tmp) {
1808 if (dev_tmp->is_pcie) {
1809 bus = dev_tmp->subordinate->number;
1810 devfn = 0;
1811 } else {
1812 bus = dev_tmp->bus->number;
1813 devfn = dev_tmp->devfn;
1814 }
1815 spin_lock_irqsave(&device_domain_lock, flags);
1816 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf92009-04-04 01:45:37 +01001817 if (info->segment == segment &&
1818 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001819 found = info->domain;
1820 break;
1821 }
1822 }
1823 spin_unlock_irqrestore(&device_domain_lock, flags);
1824 /* pcie-pci bridge already has a domain, uses it */
1825 if (found) {
1826 domain = found;
1827 goto found_domain;
1828 }
1829 }
1830
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001831 domain = alloc_domain();
1832 if (!domain)
1833 goto error;
1834
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001835 /* Allocate new domain for the device */
1836 drhd = dmar_find_matched_drhd_unit(pdev);
1837 if (!drhd) {
1838 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1839 pci_name(pdev));
1840 return NULL;
1841 }
1842 iommu = drhd->iommu;
1843
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001844 ret = iommu_attach_domain(domain, iommu);
1845 if (ret) {
1846 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001847 goto error;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001848 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001849
1850 if (domain_init(domain, gaw)) {
1851 domain_exit(domain);
1852 goto error;
1853 }
1854
1855 /* register pcie-to-pci device */
1856 if (dev_tmp) {
1857 info = alloc_devinfo_mem();
1858 if (!info) {
1859 domain_exit(domain);
1860 goto error;
1861 }
David Woodhouse276dbf92009-04-04 01:45:37 +01001862 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001863 info->bus = bus;
1864 info->devfn = devfn;
1865 info->dev = NULL;
1866 info->domain = domain;
1867 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001868 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001869
1870 /* pcie-to-pci bridge already has a domain, uses it */
1871 found = NULL;
1872 spin_lock_irqsave(&device_domain_lock, flags);
1873 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf92009-04-04 01:45:37 +01001874 if (tmp->segment == segment &&
1875 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001876 found = tmp->domain;
1877 break;
1878 }
1879 }
1880 if (found) {
1881 free_devinfo_mem(info);
1882 domain_exit(domain);
1883 domain = found;
1884 } else {
1885 list_add(&info->link, &domain->devices);
1886 list_add(&info->global, &device_domain_list);
1887 }
1888 spin_unlock_irqrestore(&device_domain_lock, flags);
1889 }
1890
1891found_domain:
1892 info = alloc_devinfo_mem();
1893 if (!info)
1894 goto error;
David Woodhouse276dbf92009-04-04 01:45:37 +01001895 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001896 info->bus = pdev->bus->number;
1897 info->devfn = pdev->devfn;
1898 info->dev = pdev;
1899 info->domain = domain;
1900 spin_lock_irqsave(&device_domain_lock, flags);
1901 /* somebody is fast */
1902 found = find_domain(pdev);
1903 if (found != NULL) {
1904 spin_unlock_irqrestore(&device_domain_lock, flags);
1905 if (found != domain) {
1906 domain_exit(domain);
1907 domain = found;
1908 }
1909 free_devinfo_mem(info);
1910 return domain;
1911 }
1912 list_add(&info->link, &domain->devices);
1913 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001914 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001915 spin_unlock_irqrestore(&device_domain_lock, flags);
1916 return domain;
1917error:
1918 /* recheck it here, maybe others set it */
1919 return find_domain(pdev);
1920}
1921
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001922static int iommu_identity_mapping;
1923
David Woodhouseb2132032009-06-26 18:50:28 +01001924static int iommu_domain_identity_map(struct dmar_domain *domain,
1925 unsigned long long start,
1926 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001927{
David Woodhousec5395d52009-06-28 16:35:56 +01001928 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
1929 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001930
David Woodhousec5395d52009-06-28 16:35:56 +01001931 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
1932 dma_to_mm_pfn(last_vpfn))) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001933 printk(KERN_ERR "IOMMU: reserve iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01001934 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001935 }
1936
David Woodhousec5395d52009-06-28 16:35:56 +01001937 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
1938 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001939 /*
1940 * RMRR range might have overlap with physical memory range,
1941 * clear it first
1942 */
David Woodhousec5395d52009-06-28 16:35:56 +01001943 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001944
David Woodhousec5395d52009-06-28 16:35:56 +01001945 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
1946 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01001947 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01001948}
1949
1950static int iommu_prepare_identity_map(struct pci_dev *pdev,
1951 unsigned long long start,
1952 unsigned long long end)
1953{
1954 struct dmar_domain *domain;
1955 int ret;
1956
1957 printk(KERN_INFO
1958 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1959 pci_name(pdev), start, end);
1960
David Woodhousec7ab48d2009-06-26 19:10:36 +01001961 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01001962 if (!domain)
1963 return -ENOMEM;
1964
1965 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001966 if (ret)
1967 goto error;
1968
1969 /* context entry init */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001970 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01001971 if (ret)
1972 goto error;
1973
1974 return 0;
1975
1976 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001977 domain_exit(domain);
1978 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001979}
1980
1981static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1982 struct pci_dev *pdev)
1983{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001984 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001985 return 0;
1986 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1987 rmrr->end_address + 1);
1988}
1989
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001990#ifdef CONFIG_DMAR_FLOPPY_WA
1991static inline void iommu_prepare_isa(void)
1992{
1993 struct pci_dev *pdev;
1994 int ret;
1995
1996 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1997 if (!pdev)
1998 return;
1999
David Woodhousec7ab48d2009-06-26 19:10:36 +01002000 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002001 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
2002
2003 if (ret)
David Woodhousec7ab48d2009-06-26 19:10:36 +01002004 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
2005 "floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002006
2007}
2008#else
2009static inline void iommu_prepare_isa(void)
2010{
2011 return;
2012}
2013#endif /* !CONFIG_DMAR_FLPY_WA */
2014
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002015/* Initialize each context entry as pass through.*/
2016static int __init init_context_pass_through(void)
2017{
2018 struct pci_dev *pdev = NULL;
2019 struct dmar_domain *domain;
2020 int ret;
2021
2022 for_each_pci_dev(pdev) {
2023 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
2024 ret = domain_context_mapping(domain, pdev,
2025 CONTEXT_TT_PASS_THROUGH);
2026 if (ret)
2027 return ret;
2028 }
2029 return 0;
2030}
2031
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002032static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01002033
2034static int __init si_domain_work_fn(unsigned long start_pfn,
2035 unsigned long end_pfn, void *datax)
2036{
2037 int *ret = datax;
2038
2039 *ret = iommu_domain_identity_map(si_domain,
2040 (uint64_t)start_pfn << PAGE_SHIFT,
2041 (uint64_t)end_pfn << PAGE_SHIFT);
2042 return *ret;
2043
2044}
2045
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002046static int si_domain_init(void)
2047{
2048 struct dmar_drhd_unit *drhd;
2049 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01002050 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002051
2052 si_domain = alloc_domain();
2053 if (!si_domain)
2054 return -EFAULT;
2055
David Woodhousec7ab48d2009-06-26 19:10:36 +01002056 pr_debug("Identity mapping domain is domain %d\n", si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002057
2058 for_each_active_iommu(iommu, drhd) {
2059 ret = iommu_attach_domain(si_domain, iommu);
2060 if (ret) {
2061 domain_exit(si_domain);
2062 return -EFAULT;
2063 }
2064 }
2065
2066 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2067 domain_exit(si_domain);
2068 return -EFAULT;
2069 }
2070
2071 si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY;
2072
David Woodhousec7ab48d2009-06-26 19:10:36 +01002073 for_each_online_node(nid) {
2074 work_with_active_regions(nid, si_domain_work_fn, &ret);
2075 if (ret)
2076 return ret;
2077 }
2078
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002079 return 0;
2080}
2081
2082static void domain_remove_one_dev_info(struct dmar_domain *domain,
2083 struct pci_dev *pdev);
2084static int identity_mapping(struct pci_dev *pdev)
2085{
2086 struct device_domain_info *info;
2087
2088 if (likely(!iommu_identity_mapping))
2089 return 0;
2090
2091
2092 list_for_each_entry(info, &si_domain->devices, link)
2093 if (info->dev == pdev)
2094 return 1;
2095 return 0;
2096}
2097
2098static int domain_add_dev_info(struct dmar_domain *domain,
2099 struct pci_dev *pdev)
2100{
2101 struct device_domain_info *info;
2102 unsigned long flags;
2103
2104 info = alloc_devinfo_mem();
2105 if (!info)
2106 return -ENOMEM;
2107
2108 info->segment = pci_domain_nr(pdev->bus);
2109 info->bus = pdev->bus->number;
2110 info->devfn = pdev->devfn;
2111 info->dev = pdev;
2112 info->domain = domain;
2113
2114 spin_lock_irqsave(&device_domain_lock, flags);
2115 list_add(&info->link, &domain->devices);
2116 list_add(&info->global, &device_domain_list);
2117 pdev->dev.archdata.iommu = info;
2118 spin_unlock_irqrestore(&device_domain_lock, flags);
2119
2120 return 0;
2121}
2122
2123static int iommu_prepare_static_identity_mapping(void)
2124{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002125 struct pci_dev *pdev = NULL;
2126 int ret;
2127
2128 ret = si_domain_init();
2129 if (ret)
2130 return -EFAULT;
2131
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002132 for_each_pci_dev(pdev) {
David Woodhousec7ab48d2009-06-26 19:10:36 +01002133 printk(KERN_INFO "IOMMU: identity mapping for device %s\n",
2134 pci_name(pdev));
2135
2136 ret = domain_context_mapping(si_domain, pdev,
2137 CONTEXT_TT_MULTI_LEVEL);
2138 if (ret)
2139 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002140 ret = domain_add_dev_info(si_domain, pdev);
2141 if (ret)
2142 return ret;
2143 }
2144
2145 return 0;
2146}
2147
2148int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002149{
2150 struct dmar_drhd_unit *drhd;
2151 struct dmar_rmrr_unit *rmrr;
2152 struct pci_dev *pdev;
2153 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002154 int i, ret;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002155 int pass_through = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002156
2157 /*
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002158 * In case pass through can not be enabled, iommu tries to use identity
2159 * mapping.
2160 */
2161 if (iommu_pass_through)
2162 iommu_identity_mapping = 1;
2163
2164 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002165 * for each drhd
2166 * allocate root
2167 * initialize and program root entry to not present
2168 * endfor
2169 */
2170 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002171 g_num_of_iommus++;
2172 /*
2173 * lock not needed as this is only incremented in the single
2174 * threaded kernel __init code path all other access are read
2175 * only
2176 */
2177 }
2178
Weidong Hand9630fe2008-12-08 11:06:32 +08002179 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2180 GFP_KERNEL);
2181 if (!g_iommus) {
2182 printk(KERN_ERR "Allocating global iommu array failed\n");
2183 ret = -ENOMEM;
2184 goto error;
2185 }
2186
mark gross80b20dd2008-04-18 13:53:58 -07002187 deferred_flush = kzalloc(g_num_of_iommus *
2188 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2189 if (!deferred_flush) {
Weidong Hand9630fe2008-12-08 11:06:32 +08002190 kfree(g_iommus);
mark gross5e0d2a62008-03-04 15:22:08 -08002191 ret = -ENOMEM;
2192 goto error;
2193 }
2194
mark gross5e0d2a62008-03-04 15:22:08 -08002195 for_each_drhd_unit(drhd) {
2196 if (drhd->ignored)
2197 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002198
2199 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08002200 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002201
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002202 ret = iommu_init_domains(iommu);
2203 if (ret)
2204 goto error;
2205
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002206 /*
2207 * TBD:
2208 * we could share the same root & context tables
2209 * amoung all IOMMU's. Need to Split it later.
2210 */
2211 ret = iommu_alloc_root_entry(iommu);
2212 if (ret) {
2213 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
2214 goto error;
2215 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002216 if (!ecap_pass_through(iommu->ecap))
2217 pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002218 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002219 if (iommu_pass_through)
2220 if (!pass_through) {
2221 printk(KERN_INFO
2222 "Pass Through is not supported by hardware.\n");
2223 iommu_pass_through = 0;
2224 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002225
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002226 /*
2227 * Start from the sane iommu hardware state.
2228 */
Youquan Songa77b67d2008-10-16 16:31:56 -07002229 for_each_drhd_unit(drhd) {
2230 if (drhd->ignored)
2231 continue;
2232
2233 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002234
2235 /*
2236 * If the queued invalidation is already initialized by us
2237 * (for example, while enabling interrupt-remapping) then
2238 * we got the things already rolling from a sane state.
2239 */
2240 if (iommu->qi)
2241 continue;
2242
2243 /*
2244 * Clear any previous faults.
2245 */
2246 dmar_fault(-1, iommu);
2247 /*
2248 * Disable queued invalidation if supported and already enabled
2249 * before OS handover.
2250 */
2251 dmar_disable_qi(iommu);
2252 }
2253
2254 for_each_drhd_unit(drhd) {
2255 if (drhd->ignored)
2256 continue;
2257
2258 iommu = drhd->iommu;
2259
Youquan Songa77b67d2008-10-16 16:31:56 -07002260 if (dmar_enable_qi(iommu)) {
2261 /*
2262 * Queued Invalidate not enabled, use Register Based
2263 * Invalidate
2264 */
2265 iommu->flush.flush_context = __iommu_flush_context;
2266 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
2267 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002268 "invalidation\n",
2269 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002270 } else {
2271 iommu->flush.flush_context = qi_flush_context;
2272 iommu->flush.flush_iotlb = qi_flush_iotlb;
2273 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002274 "invalidation\n",
2275 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002276 }
2277 }
2278
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002279 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002280 * If pass through is set and enabled, context entries of all pci
2281 * devices are intialized by pass through translation type.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002282 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002283 if (iommu_pass_through) {
2284 ret = init_context_pass_through();
2285 if (ret) {
2286 printk(KERN_ERR "IOMMU: Pass through init failed.\n");
2287 iommu_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002288 }
2289 }
2290
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002291 /*
2292 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002293 * identity mappings for rmrr, gfx, and isa and may fall back to static
2294 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002295 */
2296 if (!iommu_pass_through) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002297 if (iommu_identity_mapping)
2298 iommu_prepare_static_identity_mapping();
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002299 /*
2300 * For each rmrr
2301 * for each dev attached to rmrr
2302 * do
2303 * locate drhd for dev, alloc domain for dev
2304 * allocate free domain
2305 * allocate page table entries for rmrr
2306 * if context not allocated for bus
2307 * allocate and init context
2308 * set present in root table for this bus
2309 * init context with domain, translation etc
2310 * endfor
2311 * endfor
2312 */
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002313 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002314 for_each_rmrr_units(rmrr) {
2315 for (i = 0; i < rmrr->devices_cnt; i++) {
2316 pdev = rmrr->devices[i];
2317 /*
2318 * some BIOS lists non-exist devices in DMAR
2319 * table.
2320 */
2321 if (!pdev)
2322 continue;
2323 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2324 if (ret)
2325 printk(KERN_ERR
2326 "IOMMU: mapping reserved region failed\n");
2327 }
2328 }
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002329
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002330 iommu_prepare_isa();
2331 }
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002332
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002333 /*
2334 * for each drhd
2335 * enable fault log
2336 * global invalidate context cache
2337 * global invalidate iotlb
2338 * enable translation
2339 */
2340 for_each_drhd_unit(drhd) {
2341 if (drhd->ignored)
2342 continue;
2343 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002344
2345 iommu_flush_write_buffer(iommu);
2346
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002347 ret = dmar_set_interrupt(iommu);
2348 if (ret)
2349 goto error;
2350
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002351 iommu_set_root_entry(iommu);
2352
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002353 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002354 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002355 iommu_disable_protect_mem_regions(iommu);
2356
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002357 ret = iommu_enable_translation(iommu);
2358 if (ret)
2359 goto error;
2360 }
2361
2362 return 0;
2363error:
2364 for_each_drhd_unit(drhd) {
2365 if (drhd->ignored)
2366 continue;
2367 iommu = drhd->iommu;
2368 free_iommu(iommu);
2369 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002370 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002371 return ret;
2372}
2373
David Woodhouse88cb6a72009-06-28 15:03:06 +01002374static inline unsigned long aligned_nrpages(unsigned long host_addr,
2375 size_t size)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002376{
David Woodhouse88cb6a72009-06-28 15:03:06 +01002377 host_addr &= ~PAGE_MASK;
2378 host_addr += size + PAGE_SIZE - 1;
2379
2380 return host_addr >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002381}
2382
David Woodhouse875764d2009-06-28 21:20:51 +01002383static struct iova *intel_alloc_iova(struct device *dev,
2384 struct dmar_domain *domain,
2385 unsigned long nrpages, uint64_t dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002386{
2387 struct pci_dev *pdev = to_pci_dev(dev);
2388 struct iova *iova = NULL;
2389
David Woodhouse875764d2009-06-28 21:20:51 +01002390 /* Restrict dma_mask to the width that the iommu can handle */
2391 dma_mask = min_t(uint64_t, DOMAIN_MAX_ADDR(domain->gaw), dma_mask);
2392
2393 if (!dmar_forcedac && dma_mask > DMA_BIT_MASK(32)) {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002394 /*
2395 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002396 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002397 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002398 */
David Woodhouse875764d2009-06-28 21:20:51 +01002399 iova = alloc_iova(&domain->iovad, nrpages,
2400 IOVA_PFN(DMA_BIT_MASK(32)), 1);
2401 if (iova)
2402 return iova;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002403 }
David Woodhouse875764d2009-06-28 21:20:51 +01002404 iova = alloc_iova(&domain->iovad, nrpages, IOVA_PFN(dma_mask), 1);
2405 if (unlikely(!iova)) {
2406 printk(KERN_ERR "Allocating %ld-page iova for %s failed",
2407 nrpages, pci_name(pdev));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002408 return NULL;
2409 }
2410
2411 return iova;
2412}
2413
2414static struct dmar_domain *
2415get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002416{
2417 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002418 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002419
2420 domain = get_domain_for_dev(pdev,
2421 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2422 if (!domain) {
2423 printk(KERN_ERR
2424 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002425 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002426 }
2427
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002428 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002429 if (unlikely(!domain_context_mapped(pdev))) {
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002430 ret = domain_context_mapping(domain, pdev,
2431 CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002432 if (ret) {
2433 printk(KERN_ERR
2434 "Domain context map for %s failed",
2435 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002436 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002437 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002438 }
2439
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002440 return domain;
2441}
2442
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002443static int iommu_dummy(struct pci_dev *pdev)
2444{
2445 return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
2446}
2447
2448/* Check if the pdev needs to go through non-identity map and unmap process.*/
2449static int iommu_no_mapping(struct pci_dev *pdev)
2450{
2451 int found;
2452
2453 if (!iommu_identity_mapping)
2454 return iommu_dummy(pdev);
2455
2456 found = identity_mapping(pdev);
2457 if (found) {
2458 if (pdev->dma_mask > DMA_BIT_MASK(32))
2459 return 1;
2460 else {
2461 /*
2462 * 32 bit DMA is removed from si_domain and fall back
2463 * to non-identity mapping.
2464 */
2465 domain_remove_one_dev_info(si_domain, pdev);
2466 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
2467 pci_name(pdev));
2468 return 0;
2469 }
2470 } else {
2471 /*
2472 * In case of a detached 64 bit DMA device from vm, the device
2473 * is put into si_domain for identity mapping.
2474 */
2475 if (pdev->dma_mask > DMA_BIT_MASK(32)) {
2476 int ret;
2477 ret = domain_add_dev_info(si_domain, pdev);
2478 if (!ret) {
2479 printk(KERN_INFO "64bit %s uses identity mapping\n",
2480 pci_name(pdev));
2481 return 1;
2482 }
2483 }
2484 }
2485
2486 return iommu_dummy(pdev);
2487}
2488
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002489static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2490 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002491{
2492 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002493 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002494 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002495 struct iova *iova;
2496 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002497 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002498 struct intel_iommu *iommu;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002499
2500 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002501
2502 if (iommu_no_mapping(pdev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002503 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002504
2505 domain = get_valid_domain_for_dev(pdev);
2506 if (!domain)
2507 return 0;
2508
Weidong Han8c11e792008-12-08 15:29:22 +08002509 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01002510 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002511
David Woodhouse875764d2009-06-28 21:20:51 +01002512 iova = intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002513 if (!iova)
2514 goto error;
2515
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002516 /*
2517 * Check if DMAR supports zero-length reads on write only
2518 * mappings..
2519 */
2520 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002521 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002522 prot |= DMA_PTE_READ;
2523 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2524 prot |= DMA_PTE_WRITE;
2525 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002526 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002527 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002528 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002529 * is not a big problem
2530 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01002531 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
2532 paddr >> VTD_PAGE_SHIFT, size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002533 if (ret)
2534 goto error;
2535
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002536 /* it's a non-present to present mapping. Only flush if caching mode */
2537 if (cap_caching_mode(iommu->cap))
David Woodhouse03d6a242009-06-28 15:33:46 +01002538 iommu_flush_iotlb_psi(iommu, 0, mm_to_dma_pfn(iova->pfn_lo), size);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002539 else
Weidong Han8c11e792008-12-08 15:29:22 +08002540 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002541
David Woodhouse03d6a242009-06-28 15:33:46 +01002542 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
2543 start_paddr += paddr & ~PAGE_MASK;
2544 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002545
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002546error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002547 if (iova)
2548 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002549 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002550 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002551 return 0;
2552}
2553
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002554static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2555 unsigned long offset, size_t size,
2556 enum dma_data_direction dir,
2557 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002558{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002559 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2560 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002561}
2562
mark gross5e0d2a62008-03-04 15:22:08 -08002563static void flush_unmaps(void)
2564{
mark gross80b20dd2008-04-18 13:53:58 -07002565 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002566
mark gross5e0d2a62008-03-04 15:22:08 -08002567 timer_on = 0;
2568
2569 /* just flush them all */
2570 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002571 struct intel_iommu *iommu = g_iommus[i];
2572 if (!iommu)
2573 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002574
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002575 if (!deferred_flush[i].next)
2576 continue;
2577
2578 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08002579 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002580 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08002581 unsigned long mask;
2582 struct iova *iova = deferred_flush[i].iova[j];
2583
2584 mask = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT;
2585 mask = ilog2(mask >> VTD_PAGE_SHIFT);
2586 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
2587 iova->pfn_lo << PAGE_SHIFT, mask);
2588 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
mark gross80b20dd2008-04-18 13:53:58 -07002589 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002590 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002591 }
2592
mark gross5e0d2a62008-03-04 15:22:08 -08002593 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002594}
2595
2596static void flush_unmaps_timeout(unsigned long data)
2597{
mark gross80b20dd2008-04-18 13:53:58 -07002598 unsigned long flags;
2599
2600 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002601 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002602 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002603}
2604
2605static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2606{
2607 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002608 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002609 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002610
2611 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002612 if (list_size == HIGH_WATER_MARK)
2613 flush_unmaps();
2614
Weidong Han8c11e792008-12-08 15:29:22 +08002615 iommu = domain_get_iommu(dom);
2616 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002617
mark gross80b20dd2008-04-18 13:53:58 -07002618 next = deferred_flush[iommu_id].next;
2619 deferred_flush[iommu_id].domain[next] = dom;
2620 deferred_flush[iommu_id].iova[next] = iova;
2621 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002622
2623 if (!timer_on) {
2624 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2625 timer_on = 1;
2626 }
2627 list_size++;
2628 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2629}
2630
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002631static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2632 size_t size, enum dma_data_direction dir,
2633 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002634{
2635 struct pci_dev *pdev = to_pci_dev(dev);
2636 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002637 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002638 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002639 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002640
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002641 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002642 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002643
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002644 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002645 BUG_ON(!domain);
2646
Weidong Han8c11e792008-12-08 15:29:22 +08002647 iommu = domain_get_iommu(domain);
2648
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002649 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2650 if (!iova)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002651 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002652
David Woodhoused794dc92009-06-28 00:27:49 +01002653 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2654 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002655
David Woodhoused794dc92009-06-28 00:27:49 +01002656 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
2657 pci_name(pdev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002658
2659 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002660 dma_pte_clear_range(domain, start_pfn, last_pfn);
2661
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002662 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01002663 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2664
mark gross5e0d2a62008-03-04 15:22:08 -08002665 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01002666 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhoused794dc92009-06-28 00:27:49 +01002667 last_pfn - start_pfn + 1);
mark gross5e0d2a62008-03-04 15:22:08 -08002668 /* free iova */
2669 __free_iova(&domain->iovad, iova);
2670 } else {
2671 add_unmap(domain, iova);
2672 /*
2673 * queue up the release of the unmap to save the 1/6th of the
2674 * cpu used up by the iotlb flush operation...
2675 */
mark gross5e0d2a62008-03-04 15:22:08 -08002676 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002677}
2678
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002679static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2680 int dir)
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002681{
2682 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2683}
2684
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002685static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2686 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002687{
2688 void *vaddr;
2689 int order;
2690
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002691 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002692 order = get_order(size);
2693 flags &= ~(GFP_DMA | GFP_DMA32);
2694
2695 vaddr = (void *)__get_free_pages(flags, order);
2696 if (!vaddr)
2697 return NULL;
2698 memset(vaddr, 0, size);
2699
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002700 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2701 DMA_BIDIRECTIONAL,
2702 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002703 if (*dma_handle)
2704 return vaddr;
2705 free_pages((unsigned long)vaddr, order);
2706 return NULL;
2707}
2708
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002709static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2710 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002711{
2712 int order;
2713
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002714 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002715 order = get_order(size);
2716
2717 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2718 free_pages((unsigned long)vaddr, order);
2719}
2720
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002721static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2722 int nelems, enum dma_data_direction dir,
2723 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002724{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002725 struct pci_dev *pdev = to_pci_dev(hwdev);
2726 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002727 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002728 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002729 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002730
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002731 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002732 return;
2733
2734 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002735 BUG_ON(!domain);
2736
2737 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002738
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002739 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002740 if (!iova)
2741 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002742
David Woodhoused794dc92009-06-28 00:27:49 +01002743 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2744 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002745
2746 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002747 dma_pte_clear_range(domain, start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002748
David Woodhoused794dc92009-06-28 00:27:49 +01002749 /* free page tables */
2750 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2751
David Woodhouse03d6a242009-06-28 15:33:46 +01002752 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhoused794dc92009-06-28 00:27:49 +01002753 (last_pfn - start_pfn + 1));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002754
2755 /* free iova */
2756 __free_iova(&domain->iovad, iova);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002757}
2758
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002759static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002760 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002761{
2762 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002763 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002764
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002765 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002766 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002767 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002768 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002769 }
2770 return nelems;
2771}
2772
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002773static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2774 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002775{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002776 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002777 struct pci_dev *pdev = to_pci_dev(hwdev);
2778 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002779 size_t size = 0;
2780 int prot = 0;
David Woodhouseb536d242009-06-28 14:49:31 +01002781 size_t offset_pfn = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002782 struct iova *iova = NULL;
2783 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002784 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01002785 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08002786 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002787
2788 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002789 if (iommu_no_mapping(pdev))
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002790 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002791
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002792 domain = get_valid_domain_for_dev(pdev);
2793 if (!domain)
2794 return 0;
2795
Weidong Han8c11e792008-12-08 15:29:22 +08002796 iommu = domain_get_iommu(domain);
2797
David Woodhouseb536d242009-06-28 14:49:31 +01002798 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01002799 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002800
David Woodhouse875764d2009-06-28 21:20:51 +01002801 iova = intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002802 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002803 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002804 return 0;
2805 }
2806
2807 /*
2808 * Check if DMAR supports zero-length reads on write only
2809 * mappings..
2810 */
2811 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002812 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002813 prot |= DMA_PTE_READ;
2814 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2815 prot |= DMA_PTE_WRITE;
2816
David Woodhouseb536d242009-06-28 14:49:31 +01002817 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
David Woodhousee1605492009-06-29 11:17:38 +01002818
2819 ret = domain_sg_mapping(domain, start_vpfn, sglist, mm_to_dma_pfn(size), prot);
2820 if (unlikely(ret)) {
2821 /* clear the page */
2822 dma_pte_clear_range(domain, start_vpfn,
2823 start_vpfn + size - 1);
2824 /* free page tables */
2825 dma_pte_free_pagetable(domain, start_vpfn,
2826 start_vpfn + size - 1);
2827 /* free iova */
2828 __free_iova(&domain->iovad, iova);
2829 return 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002830 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002831
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002832 /* it's a non-present to present mapping. Only flush if caching mode */
2833 if (cap_caching_mode(iommu->cap))
David Woodhouse03d6a242009-06-28 15:33:46 +01002834 iommu_flush_iotlb_psi(iommu, 0, start_vpfn, offset_pfn);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002835 else
Weidong Han8c11e792008-12-08 15:29:22 +08002836 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002837
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002838 return nelems;
2839}
2840
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002841static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2842{
2843 return !dma_addr;
2844}
2845
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002846struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002847 .alloc_coherent = intel_alloc_coherent,
2848 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002849 .map_sg = intel_map_sg,
2850 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002851 .map_page = intel_map_page,
2852 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002853 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002854};
2855
2856static inline int iommu_domain_cache_init(void)
2857{
2858 int ret = 0;
2859
2860 iommu_domain_cache = kmem_cache_create("iommu_domain",
2861 sizeof(struct dmar_domain),
2862 0,
2863 SLAB_HWCACHE_ALIGN,
2864
2865 NULL);
2866 if (!iommu_domain_cache) {
2867 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2868 ret = -ENOMEM;
2869 }
2870
2871 return ret;
2872}
2873
2874static inline int iommu_devinfo_cache_init(void)
2875{
2876 int ret = 0;
2877
2878 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2879 sizeof(struct device_domain_info),
2880 0,
2881 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002882 NULL);
2883 if (!iommu_devinfo_cache) {
2884 printk(KERN_ERR "Couldn't create devinfo cache\n");
2885 ret = -ENOMEM;
2886 }
2887
2888 return ret;
2889}
2890
2891static inline int iommu_iova_cache_init(void)
2892{
2893 int ret = 0;
2894
2895 iommu_iova_cache = kmem_cache_create("iommu_iova",
2896 sizeof(struct iova),
2897 0,
2898 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002899 NULL);
2900 if (!iommu_iova_cache) {
2901 printk(KERN_ERR "Couldn't create iova cache\n");
2902 ret = -ENOMEM;
2903 }
2904
2905 return ret;
2906}
2907
2908static int __init iommu_init_mempool(void)
2909{
2910 int ret;
2911 ret = iommu_iova_cache_init();
2912 if (ret)
2913 return ret;
2914
2915 ret = iommu_domain_cache_init();
2916 if (ret)
2917 goto domain_error;
2918
2919 ret = iommu_devinfo_cache_init();
2920 if (!ret)
2921 return ret;
2922
2923 kmem_cache_destroy(iommu_domain_cache);
2924domain_error:
2925 kmem_cache_destroy(iommu_iova_cache);
2926
2927 return -ENOMEM;
2928}
2929
2930static void __init iommu_exit_mempool(void)
2931{
2932 kmem_cache_destroy(iommu_devinfo_cache);
2933 kmem_cache_destroy(iommu_domain_cache);
2934 kmem_cache_destroy(iommu_iova_cache);
2935
2936}
2937
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002938static void __init init_no_remapping_devices(void)
2939{
2940 struct dmar_drhd_unit *drhd;
2941
2942 for_each_drhd_unit(drhd) {
2943 if (!drhd->include_all) {
2944 int i;
2945 for (i = 0; i < drhd->devices_cnt; i++)
2946 if (drhd->devices[i] != NULL)
2947 break;
2948 /* ignore DMAR unit if no pci devices exist */
2949 if (i == drhd->devices_cnt)
2950 drhd->ignored = 1;
2951 }
2952 }
2953
2954 if (dmar_map_gfx)
2955 return;
2956
2957 for_each_drhd_unit(drhd) {
2958 int i;
2959 if (drhd->ignored || drhd->include_all)
2960 continue;
2961
2962 for (i = 0; i < drhd->devices_cnt; i++)
2963 if (drhd->devices[i] &&
2964 !IS_GFX_DEVICE(drhd->devices[i]))
2965 break;
2966
2967 if (i < drhd->devices_cnt)
2968 continue;
2969
2970 /* bypass IOMMU if it is just for gfx devices */
2971 drhd->ignored = 1;
2972 for (i = 0; i < drhd->devices_cnt; i++) {
2973 if (!drhd->devices[i])
2974 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002975 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002976 }
2977 }
2978}
2979
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002980#ifdef CONFIG_SUSPEND
2981static int init_iommu_hw(void)
2982{
2983 struct dmar_drhd_unit *drhd;
2984 struct intel_iommu *iommu = NULL;
2985
2986 for_each_active_iommu(iommu, drhd)
2987 if (iommu->qi)
2988 dmar_reenable_qi(iommu);
2989
2990 for_each_active_iommu(iommu, drhd) {
2991 iommu_flush_write_buffer(iommu);
2992
2993 iommu_set_root_entry(iommu);
2994
2995 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002996 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002997 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002998 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002999 iommu_disable_protect_mem_regions(iommu);
3000 iommu_enable_translation(iommu);
3001 }
3002
3003 return 0;
3004}
3005
3006static void iommu_flush_all(void)
3007{
3008 struct dmar_drhd_unit *drhd;
3009 struct intel_iommu *iommu;
3010
3011 for_each_active_iommu(iommu, drhd) {
3012 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003013 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003014 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01003015 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003016 }
3017}
3018
3019static int iommu_suspend(struct sys_device *dev, pm_message_t state)
3020{
3021 struct dmar_drhd_unit *drhd;
3022 struct intel_iommu *iommu = NULL;
3023 unsigned long flag;
3024
3025 for_each_active_iommu(iommu, drhd) {
3026 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
3027 GFP_ATOMIC);
3028 if (!iommu->iommu_state)
3029 goto nomem;
3030 }
3031
3032 iommu_flush_all();
3033
3034 for_each_active_iommu(iommu, drhd) {
3035 iommu_disable_translation(iommu);
3036
3037 spin_lock_irqsave(&iommu->register_lock, flag);
3038
3039 iommu->iommu_state[SR_DMAR_FECTL_REG] =
3040 readl(iommu->reg + DMAR_FECTL_REG);
3041 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3042 readl(iommu->reg + DMAR_FEDATA_REG);
3043 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3044 readl(iommu->reg + DMAR_FEADDR_REG);
3045 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3046 readl(iommu->reg + DMAR_FEUADDR_REG);
3047
3048 spin_unlock_irqrestore(&iommu->register_lock, flag);
3049 }
3050 return 0;
3051
3052nomem:
3053 for_each_active_iommu(iommu, drhd)
3054 kfree(iommu->iommu_state);
3055
3056 return -ENOMEM;
3057}
3058
3059static int iommu_resume(struct sys_device *dev)
3060{
3061 struct dmar_drhd_unit *drhd;
3062 struct intel_iommu *iommu = NULL;
3063 unsigned long flag;
3064
3065 if (init_iommu_hw()) {
3066 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
3067 return -EIO;
3068 }
3069
3070 for_each_active_iommu(iommu, drhd) {
3071
3072 spin_lock_irqsave(&iommu->register_lock, flag);
3073
3074 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3075 iommu->reg + DMAR_FECTL_REG);
3076 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3077 iommu->reg + DMAR_FEDATA_REG);
3078 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3079 iommu->reg + DMAR_FEADDR_REG);
3080 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3081 iommu->reg + DMAR_FEUADDR_REG);
3082
3083 spin_unlock_irqrestore(&iommu->register_lock, flag);
3084 }
3085
3086 for_each_active_iommu(iommu, drhd)
3087 kfree(iommu->iommu_state);
3088
3089 return 0;
3090}
3091
3092static struct sysdev_class iommu_sysclass = {
3093 .name = "iommu",
3094 .resume = iommu_resume,
3095 .suspend = iommu_suspend,
3096};
3097
3098static struct sys_device device_iommu = {
3099 .cls = &iommu_sysclass,
3100};
3101
3102static int __init init_iommu_sysfs(void)
3103{
3104 int error;
3105
3106 error = sysdev_class_register(&iommu_sysclass);
3107 if (error)
3108 return error;
3109
3110 error = sysdev_register(&device_iommu);
3111 if (error)
3112 sysdev_class_unregister(&iommu_sysclass);
3113
3114 return error;
3115}
3116
3117#else
3118static int __init init_iommu_sysfs(void)
3119{
3120 return 0;
3121}
3122#endif /* CONFIG_PM */
3123
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003124int __init intel_iommu_init(void)
3125{
3126 int ret = 0;
3127
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003128 if (dmar_table_init())
3129 return -ENODEV;
3130
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003131 if (dmar_dev_scope_init())
3132 return -ENODEV;
3133
Suresh Siddha2ae21012008-07-10 11:16:43 -07003134 /*
3135 * Check the need for DMA-remapping initialization now.
3136 * Above initialization will also be used by Interrupt-remapping.
3137 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003138 if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled)
Suresh Siddha2ae21012008-07-10 11:16:43 -07003139 return -ENODEV;
3140
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003141 iommu_init_mempool();
3142 dmar_init_reserved_ranges();
3143
3144 init_no_remapping_devices();
3145
3146 ret = init_dmars();
3147 if (ret) {
3148 printk(KERN_ERR "IOMMU: dmar init failed\n");
3149 put_iova_domain(&reserved_iova_list);
3150 iommu_exit_mempool();
3151 return ret;
3152 }
3153 printk(KERN_INFO
3154 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
3155
mark gross5e0d2a62008-03-04 15:22:08 -08003156 init_timer(&unmap_timer);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003157 force_iommu = 1;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003158
3159 if (!iommu_pass_through) {
3160 printk(KERN_INFO
3161 "Multi-level page-table translation for DMAR.\n");
3162 dma_ops = &intel_dma_ops;
3163 } else
3164 printk(KERN_INFO
3165 "DMAR: Pass through translation for DMAR.\n");
3166
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003167 init_iommu_sysfs();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003168
3169 register_iommu(&intel_iommu_ops);
3170
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003171 return 0;
3172}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07003173
Han, Weidong3199aa62009-02-26 17:31:12 +08003174static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
3175 struct pci_dev *pdev)
3176{
3177 struct pci_dev *tmp, *parent;
3178
3179 if (!iommu || !pdev)
3180 return;
3181
3182 /* dependent device detach */
3183 tmp = pci_find_upstream_pcie_bridge(pdev);
3184 /* Secondary interface's bus number and devfn 0 */
3185 if (tmp) {
3186 parent = pdev->bus->self;
3187 while (parent != tmp) {
3188 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf92009-04-04 01:45:37 +01003189 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003190 parent = parent->bus->self;
3191 }
3192 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
3193 iommu_detach_dev(iommu,
3194 tmp->subordinate->number, 0);
3195 else /* this is a legacy PCI bridge */
David Woodhouse276dbf92009-04-04 01:45:37 +01003196 iommu_detach_dev(iommu, tmp->bus->number,
3197 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003198 }
3199}
3200
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003201static void domain_remove_one_dev_info(struct dmar_domain *domain,
Weidong Hanc7151a82008-12-08 22:51:37 +08003202 struct pci_dev *pdev)
3203{
3204 struct device_domain_info *info;
3205 struct intel_iommu *iommu;
3206 unsigned long flags;
3207 int found = 0;
3208 struct list_head *entry, *tmp;
3209
David Woodhouse276dbf92009-04-04 01:45:37 +01003210 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3211 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003212 if (!iommu)
3213 return;
3214
3215 spin_lock_irqsave(&device_domain_lock, flags);
3216 list_for_each_safe(entry, tmp, &domain->devices) {
3217 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf92009-04-04 01:45:37 +01003218 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08003219 if (info->bus == pdev->bus->number &&
3220 info->devfn == pdev->devfn) {
3221 list_del(&info->link);
3222 list_del(&info->global);
3223 if (info->dev)
3224 info->dev->dev.archdata.iommu = NULL;
3225 spin_unlock_irqrestore(&device_domain_lock, flags);
3226
Yu Zhao93a23a72009-05-18 13:51:37 +08003227 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08003228 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003229 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003230 free_devinfo_mem(info);
3231
3232 spin_lock_irqsave(&device_domain_lock, flags);
3233
3234 if (found)
3235 break;
3236 else
3237 continue;
3238 }
3239
3240 /* if there is no other devices under the same iommu
3241 * owned by this domain, clear this iommu in iommu_bmp
3242 * update iommu count and coherency
3243 */
David Woodhouse276dbf92009-04-04 01:45:37 +01003244 if (iommu == device_to_iommu(info->segment, info->bus,
3245 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08003246 found = 1;
3247 }
3248
3249 if (found == 0) {
3250 unsigned long tmp_flags;
3251 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3252 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3253 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003254 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003255 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
3256 }
3257
3258 spin_unlock_irqrestore(&device_domain_lock, flags);
3259}
3260
3261static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3262{
3263 struct device_domain_info *info;
3264 struct intel_iommu *iommu;
3265 unsigned long flags1, flags2;
3266
3267 spin_lock_irqsave(&device_domain_lock, flags1);
3268 while (!list_empty(&domain->devices)) {
3269 info = list_entry(domain->devices.next,
3270 struct device_domain_info, link);
3271 list_del(&info->link);
3272 list_del(&info->global);
3273 if (info->dev)
3274 info->dev->dev.archdata.iommu = NULL;
3275
3276 spin_unlock_irqrestore(&device_domain_lock, flags1);
3277
Yu Zhao93a23a72009-05-18 13:51:37 +08003278 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf92009-04-04 01:45:37 +01003279 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003280 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003281 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003282
3283 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08003284 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08003285 */
3286 spin_lock_irqsave(&domain->iommu_lock, flags2);
3287 if (test_and_clear_bit(iommu->seq_id,
3288 &domain->iommu_bmp)) {
3289 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003290 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003291 }
3292 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3293
3294 free_devinfo_mem(info);
3295 spin_lock_irqsave(&device_domain_lock, flags1);
3296 }
3297 spin_unlock_irqrestore(&device_domain_lock, flags1);
3298}
3299
Weidong Han5e98c4b2008-12-08 23:03:27 +08003300/* domain id for virtual machine, it won't be set in context */
3301static unsigned long vm_domid;
3302
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003303static int vm_domain_min_agaw(struct dmar_domain *domain)
3304{
3305 int i;
3306 int min_agaw = domain->agaw;
3307
3308 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
3309 for (; i < g_num_of_iommus; ) {
3310 if (min_agaw > g_iommus[i]->agaw)
3311 min_agaw = g_iommus[i]->agaw;
3312
3313 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
3314 }
3315
3316 return min_agaw;
3317}
3318
Weidong Han5e98c4b2008-12-08 23:03:27 +08003319static struct dmar_domain *iommu_alloc_vm_domain(void)
3320{
3321 struct dmar_domain *domain;
3322
3323 domain = alloc_domain_mem();
3324 if (!domain)
3325 return NULL;
3326
3327 domain->id = vm_domid++;
3328 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3329 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3330
3331 return domain;
3332}
3333
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003334static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08003335{
3336 int adjust_width;
3337
3338 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3339 spin_lock_init(&domain->mapping_lock);
3340 spin_lock_init(&domain->iommu_lock);
3341
3342 domain_reserve_special_ranges(domain);
3343
3344 /* calculate AGAW */
3345 domain->gaw = guest_width;
3346 adjust_width = guestwidth_to_adjustwidth(guest_width);
3347 domain->agaw = width_to_agaw(adjust_width);
3348
3349 INIT_LIST_HEAD(&domain->devices);
3350
3351 domain->iommu_count = 0;
3352 domain->iommu_coherency = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003353 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003354
3355 /* always allocate the top pgd */
3356 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3357 if (!domain->pgd)
3358 return -ENOMEM;
3359 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3360 return 0;
3361}
3362
3363static void iommu_free_vm_domain(struct dmar_domain *domain)
3364{
3365 unsigned long flags;
3366 struct dmar_drhd_unit *drhd;
3367 struct intel_iommu *iommu;
3368 unsigned long i;
3369 unsigned long ndomains;
3370
3371 for_each_drhd_unit(drhd) {
3372 if (drhd->ignored)
3373 continue;
3374 iommu = drhd->iommu;
3375
3376 ndomains = cap_ndoms(iommu->cap);
3377 i = find_first_bit(iommu->domain_ids, ndomains);
3378 for (; i < ndomains; ) {
3379 if (iommu->domains[i] == domain) {
3380 spin_lock_irqsave(&iommu->lock, flags);
3381 clear_bit(i, iommu->domain_ids);
3382 iommu->domains[i] = NULL;
3383 spin_unlock_irqrestore(&iommu->lock, flags);
3384 break;
3385 }
3386 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3387 }
3388 }
3389}
3390
3391static void vm_domain_exit(struct dmar_domain *domain)
3392{
Weidong Han5e98c4b2008-12-08 23:03:27 +08003393 /* Domain 0 is reserved, so dont process it */
3394 if (!domain)
3395 return;
3396
3397 vm_domain_remove_all_dev_info(domain);
3398 /* destroy iovas */
3399 put_iova_domain(&domain->iovad);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003400
3401 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01003402 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003403
3404 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01003405 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003406
3407 iommu_free_vm_domain(domain);
3408 free_domain_mem(domain);
3409}
3410
Joerg Roedel5d450802008-12-03 14:52:32 +01003411static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003412{
Joerg Roedel5d450802008-12-03 14:52:32 +01003413 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003414
Joerg Roedel5d450802008-12-03 14:52:32 +01003415 dmar_domain = iommu_alloc_vm_domain();
3416 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003417 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003418 "intel_iommu_domain_init: dmar_domain == NULL\n");
3419 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003420 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003421 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003422 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003423 "intel_iommu_domain_init() failed\n");
3424 vm_domain_exit(dmar_domain);
3425 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003426 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003427 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003428
Joerg Roedel5d450802008-12-03 14:52:32 +01003429 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003430}
Kay, Allen M38717942008-09-09 18:37:29 +03003431
Joerg Roedel5d450802008-12-03 14:52:32 +01003432static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003433{
Joerg Roedel5d450802008-12-03 14:52:32 +01003434 struct dmar_domain *dmar_domain = domain->priv;
3435
3436 domain->priv = NULL;
3437 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003438}
Kay, Allen M38717942008-09-09 18:37:29 +03003439
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003440static int intel_iommu_attach_device(struct iommu_domain *domain,
3441 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003442{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003443 struct dmar_domain *dmar_domain = domain->priv;
3444 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003445 struct intel_iommu *iommu;
3446 int addr_width;
3447 u64 end;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003448 int ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003449
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003450 /* normally pdev is not mapped */
3451 if (unlikely(domain_context_mapped(pdev))) {
3452 struct dmar_domain *old_domain;
3453
3454 old_domain = find_domain(pdev);
3455 if (old_domain) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003456 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
3457 dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)
3458 domain_remove_one_dev_info(old_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003459 else
3460 domain_remove_dev_info(old_domain);
3461 }
3462 }
3463
David Woodhouse276dbf92009-04-04 01:45:37 +01003464 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3465 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003466 if (!iommu)
3467 return -ENODEV;
3468
3469 /* check if this iommu agaw is sufficient for max mapped address */
3470 addr_width = agaw_to_width(iommu->agaw);
3471 end = DOMAIN_MAX_ADDR(addr_width);
3472 end = end & VTD_PAGE_MASK;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003473 if (end < dmar_domain->max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003474 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3475 "sufficient for the mapped address (%llx)\n",
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003476 __func__, iommu->agaw, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003477 return -EFAULT;
3478 }
3479
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003480 ret = domain_add_dev_info(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003481 if (ret)
3482 return ret;
3483
Yu Zhao93a23a72009-05-18 13:51:37 +08003484 ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003485 return ret;
3486}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003487
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003488static void intel_iommu_detach_device(struct iommu_domain *domain,
3489 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003490{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003491 struct dmar_domain *dmar_domain = domain->priv;
3492 struct pci_dev *pdev = to_pci_dev(dev);
3493
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003494 domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003495}
Kay, Allen M38717942008-09-09 18:37:29 +03003496
Joerg Roedeldde57a22008-12-03 15:04:09 +01003497static int intel_iommu_map_range(struct iommu_domain *domain,
3498 unsigned long iova, phys_addr_t hpa,
3499 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003500{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003501 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003502 u64 max_addr;
3503 int addr_width;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003504 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003505 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003506
Joerg Roedeldde57a22008-12-03 15:04:09 +01003507 if (iommu_prot & IOMMU_READ)
3508 prot |= DMA_PTE_READ;
3509 if (iommu_prot & IOMMU_WRITE)
3510 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08003511 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3512 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003513
David Woodhouse163cc522009-06-28 00:51:17 +01003514 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003515 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003516 int min_agaw;
3517 u64 end;
3518
3519 /* check if minimum agaw is sufficient for mapped address */
Joerg Roedeldde57a22008-12-03 15:04:09 +01003520 min_agaw = vm_domain_min_agaw(dmar_domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003521 addr_width = agaw_to_width(min_agaw);
3522 end = DOMAIN_MAX_ADDR(addr_width);
3523 end = end & VTD_PAGE_MASK;
3524 if (end < max_addr) {
3525 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3526 "sufficient for the mapped address (%llx)\n",
3527 __func__, min_agaw, max_addr);
3528 return -EFAULT;
3529 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003530 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003531 }
David Woodhousead051222009-06-28 14:22:28 +01003532 /* Round up size to next multiple of PAGE_SIZE, if it and
3533 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01003534 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01003535 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
3536 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003537 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003538}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003539
Joerg Roedeldde57a22008-12-03 15:04:09 +01003540static void intel_iommu_unmap_range(struct iommu_domain *domain,
3541 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003542{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003543 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003544
David Woodhouse163cc522009-06-28 00:51:17 +01003545 dma_pte_clear_range(dmar_domain, iova >> VTD_PAGE_SHIFT,
3546 (iova + size - 1) >> VTD_PAGE_SHIFT);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003547
David Woodhouse163cc522009-06-28 00:51:17 +01003548 if (dmar_domain->max_addr == iova + size)
3549 dmar_domain->max_addr = iova;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003550}
Kay, Allen M38717942008-09-09 18:37:29 +03003551
Joerg Roedeld14d6572008-12-03 15:06:57 +01003552static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3553 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003554{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003555 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003556 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003557 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003558
David Woodhouseb026fd22009-06-28 10:37:25 +01003559 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT);
Kay, Allen M38717942008-09-09 18:37:29 +03003560 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003561 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003562
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003563 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003564}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003565
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003566static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3567 unsigned long cap)
3568{
3569 struct dmar_domain *dmar_domain = domain->priv;
3570
3571 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3572 return dmar_domain->iommu_snooping;
3573
3574 return 0;
3575}
3576
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003577static struct iommu_ops intel_iommu_ops = {
3578 .domain_init = intel_iommu_domain_init,
3579 .domain_destroy = intel_iommu_domain_destroy,
3580 .attach_dev = intel_iommu_attach_device,
3581 .detach_dev = intel_iommu_detach_device,
3582 .map = intel_iommu_map_range,
3583 .unmap = intel_iommu_unmap_range,
3584 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003585 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003586};
David Woodhouse9af88142009-02-13 23:18:03 +00003587
3588static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3589{
3590 /*
3591 * Mobile 4 Series Chipset neglects to set RWBF capability,
3592 * but needs it:
3593 */
3594 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3595 rwbf_quirk = 1;
3596}
3597
3598DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);