blob: c5caf7d63a0f9ce988c89e06636808520876e1de [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 Yang9cf066972009-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 Yang9cf066972009-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{
225 return (pte->val & VTD_PAGE_MASK);
226}
227
David Woodhousedd4e8312009-06-27 16:21:20 +0100228static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn)
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000229{
David Woodhousedd4e8312009-06-27 16:21:20 +0100230 pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000231}
232
233static inline bool dma_pte_present(struct dma_pte *pte)
234{
235 return (pte->val & 3) != 0;
236}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000237
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700238/*
239 * This domain is a statically identity mapping domain.
240 * 1. This domain creats a static 1:1 mapping to all usable memory.
241 * 2. It maps to each iommu if successful.
242 * 3. Each iommu mapps to this domain if successful.
243 */
244struct dmar_domain *si_domain;
245
Weidong Han3b5410e2008-12-08 09:17:15 +0800246/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100247#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800248
Weidong Han1ce28fe2008-12-08 16:35:39 +0800249/* domain represents a virtual machine, more than one devices
250 * across iommus may be owned in one domain, e.g. kvm guest.
251 */
252#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
253
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700254/* si_domain contains mulitple devices */
255#define DOMAIN_FLAG_STATIC_IDENTITY (1 << 2)
256
Mark McLoughlin99126f72008-11-20 15:49:47 +0000257struct dmar_domain {
258 int id; /* domain id */
Weidong Han8c11e792008-12-08 15:29:22 +0800259 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000260
261 struct list_head devices; /* all devices' list */
262 struct iova_domain iovad; /* iova's that belong to this domain */
263
264 struct dma_pte *pgd; /* virtual address */
265 spinlock_t mapping_lock; /* page table lock */
266 int gaw; /* max guest address width */
267
268 /* adjusted guest address width, 0 is level 2 30-bit */
269 int agaw;
270
Weidong Han3b5410e2008-12-08 09:17:15 +0800271 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800272
273 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800274 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800275 int iommu_count; /* reference count of iommu */
276 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800277 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000278};
279
Mark McLoughlina647dac2008-11-20 15:49:48 +0000280/* PCI domain-device relationship */
281struct device_domain_info {
282 struct list_head link; /* link to domain siblings */
283 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100284 int segment; /* PCI domain */
285 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000286 u8 devfn; /* PCI devfn number */
287 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
Yu Zhao93a23a72009-05-18 13:51:37 +0800288 struct intel_iommu *iommu; /* IOMMU used by this device */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000289 struct dmar_domain *domain; /* pointer to domain */
290};
291
mark gross5e0d2a62008-03-04 15:22:08 -0800292static void flush_unmaps_timeout(unsigned long data);
293
294DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
295
mark gross80b20dd2008-04-18 13:53:58 -0700296#define HIGH_WATER_MARK 250
297struct deferred_flush_tables {
298 int next;
299 struct iova *iova[HIGH_WATER_MARK];
300 struct dmar_domain *domain[HIGH_WATER_MARK];
301};
302
303static struct deferred_flush_tables *deferred_flush;
304
mark gross5e0d2a62008-03-04 15:22:08 -0800305/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800306static int g_num_of_iommus;
307
308static DEFINE_SPINLOCK(async_umap_flush_lock);
309static LIST_HEAD(unmaps_to_do);
310
311static int timer_on;
312static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800313
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700314static void domain_remove_dev_info(struct dmar_domain *domain);
315
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800316#ifdef CONFIG_DMAR_DEFAULT_ON
317int dmar_disabled = 0;
318#else
319int dmar_disabled = 1;
320#endif /*CONFIG_DMAR_DEFAULT_ON*/
321
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700322static int __initdata dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700323static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800324static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700325
326#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
327static DEFINE_SPINLOCK(device_domain_lock);
328static LIST_HEAD(device_domain_list);
329
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100330static struct iommu_ops intel_iommu_ops;
331
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700332static int __init intel_iommu_setup(char *str)
333{
334 if (!str)
335 return -EINVAL;
336 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800337 if (!strncmp(str, "on", 2)) {
338 dmar_disabled = 0;
339 printk(KERN_INFO "Intel-IOMMU: enabled\n");
340 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700341 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800342 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700343 } else if (!strncmp(str, "igfx_off", 8)) {
344 dmar_map_gfx = 0;
345 printk(KERN_INFO
346 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700347 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800348 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700349 "Intel-IOMMU: Forcing DAC for PCI devices\n");
350 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800351 } else if (!strncmp(str, "strict", 6)) {
352 printk(KERN_INFO
353 "Intel-IOMMU: disable batched IOTLB flush\n");
354 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700355 }
356
357 str += strcspn(str, ",");
358 while (*str == ',')
359 str++;
360 }
361 return 0;
362}
363__setup("intel_iommu=", intel_iommu_setup);
364
365static struct kmem_cache *iommu_domain_cache;
366static struct kmem_cache *iommu_devinfo_cache;
367static struct kmem_cache *iommu_iova_cache;
368
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700369static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
370{
371 unsigned int flags;
372 void *vaddr;
373
374 /* trying to avoid low memory issues */
375 flags = current->flags & PF_MEMALLOC;
376 current->flags |= PF_MEMALLOC;
377 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
378 current->flags &= (~PF_MEMALLOC | flags);
379 return vaddr;
380}
381
382
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700383static inline void *alloc_pgtable_page(void)
384{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700385 unsigned int flags;
386 void *vaddr;
387
388 /* trying to avoid low memory issues */
389 flags = current->flags & PF_MEMALLOC;
390 current->flags |= PF_MEMALLOC;
391 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
392 current->flags &= (~PF_MEMALLOC | flags);
393 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700394}
395
396static inline void free_pgtable_page(void *vaddr)
397{
398 free_page((unsigned long)vaddr);
399}
400
401static inline void *alloc_domain_mem(void)
402{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700403 return iommu_kmem_cache_alloc(iommu_domain_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700404}
405
Kay, Allen M38717942008-09-09 18:37:29 +0300406static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700407{
408 kmem_cache_free(iommu_domain_cache, vaddr);
409}
410
411static inline void * alloc_devinfo_mem(void)
412{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700413 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700414}
415
416static inline void free_devinfo_mem(void *vaddr)
417{
418 kmem_cache_free(iommu_devinfo_cache, vaddr);
419}
420
421struct iova *alloc_iova_mem(void)
422{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700423 return iommu_kmem_cache_alloc(iommu_iova_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700424}
425
426void free_iova_mem(struct iova *iova)
427{
428 kmem_cache_free(iommu_iova_cache, iova);
429}
430
Weidong Han1b573682008-12-08 15:34:06 +0800431
432static inline int width_to_agaw(int width);
433
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700434static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han1b573682008-12-08 15:34:06 +0800435{
436 unsigned long sagaw;
437 int agaw = -1;
438
439 sagaw = cap_sagaw(iommu->cap);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700440 for (agaw = width_to_agaw(max_gaw);
Weidong Han1b573682008-12-08 15:34:06 +0800441 agaw >= 0; agaw--) {
442 if (test_bit(agaw, &sagaw))
443 break;
444 }
445
446 return agaw;
447}
448
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -0700449/*
450 * Calculate max SAGAW for each iommu.
451 */
452int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
453{
454 return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
455}
456
457/*
458 * calculate agaw for each iommu.
459 * "SAGAW" may be different across iommus, use a default agaw, and
460 * get a supported less agaw for iommus that don't support the default agaw.
461 */
462int iommu_calculate_agaw(struct intel_iommu *iommu)
463{
464 return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
465}
466
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700467/* This functionin only returns single iommu in a domain */
Weidong Han8c11e792008-12-08 15:29:22 +0800468static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
469{
470 int iommu_id;
471
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700472 /* si_domain and vm domain should not get here. */
Weidong Han1ce28fe2008-12-08 16:35:39 +0800473 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -0700474 BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
Weidong Han1ce28fe2008-12-08 16:35:39 +0800475
Weidong Han8c11e792008-12-08 15:29:22 +0800476 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
477 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
478 return NULL;
479
480 return g_iommus[iommu_id];
481}
482
Weidong Han8e6040972008-12-08 15:49:06 +0800483static void domain_update_iommu_coherency(struct dmar_domain *domain)
484{
485 int i;
486
487 domain->iommu_coherency = 1;
488
489 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
490 for (; i < g_num_of_iommus; ) {
491 if (!ecap_coherent(g_iommus[i]->ecap)) {
492 domain->iommu_coherency = 0;
493 break;
494 }
495 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
496 }
497}
498
Sheng Yang58c610b2009-03-18 15:33:05 +0800499static void domain_update_iommu_snooping(struct dmar_domain *domain)
500{
501 int i;
502
503 domain->iommu_snooping = 1;
504
505 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
506 for (; i < g_num_of_iommus; ) {
507 if (!ecap_sc_support(g_iommus[i]->ecap)) {
508 domain->iommu_snooping = 0;
509 break;
510 }
511 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
512 }
513}
514
515/* Some capabilities may be different across iommus */
516static void domain_update_iommu_cap(struct dmar_domain *domain)
517{
518 domain_update_iommu_coherency(domain);
519 domain_update_iommu_snooping(domain);
520}
521
David Woodhouse276dbf992009-04-04 01:45:37 +0100522static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800523{
524 struct dmar_drhd_unit *drhd = NULL;
525 int i;
526
527 for_each_drhd_unit(drhd) {
528 if (drhd->ignored)
529 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100530 if (segment != drhd->segment)
531 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800532
David Woodhouse924b6232009-04-04 00:39:25 +0100533 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000534 if (drhd->devices[i] &&
535 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800536 drhd->devices[i]->devfn == devfn)
537 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700538 if (drhd->devices[i] &&
539 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100540 drhd->devices[i]->subordinate->number <= bus &&
541 drhd->devices[i]->subordinate->subordinate >= bus)
542 return drhd->iommu;
543 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800544
545 if (drhd->include_all)
546 return drhd->iommu;
547 }
548
549 return NULL;
550}
551
Weidong Han5331fe62008-12-08 23:00:00 +0800552static void domain_flush_cache(struct dmar_domain *domain,
553 void *addr, int size)
554{
555 if (!domain->iommu_coherency)
556 clflush_cache_range(addr, size);
557}
558
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700559/* Gets context entry for a given bus and devfn */
560static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
561 u8 bus, u8 devfn)
562{
563 struct root_entry *root;
564 struct context_entry *context;
565 unsigned long phy_addr;
566 unsigned long flags;
567
568 spin_lock_irqsave(&iommu->lock, flags);
569 root = &iommu->root_entry[bus];
570 context = get_context_addr_from_root(root);
571 if (!context) {
572 context = (struct context_entry *)alloc_pgtable_page();
573 if (!context) {
574 spin_unlock_irqrestore(&iommu->lock, flags);
575 return NULL;
576 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700577 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700578 phy_addr = virt_to_phys((void *)context);
579 set_root_value(root, phy_addr);
580 set_root_present(root);
581 __iommu_flush_cache(iommu, root, sizeof(*root));
582 }
583 spin_unlock_irqrestore(&iommu->lock, flags);
584 return &context[devfn];
585}
586
587static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
588{
589 struct root_entry *root;
590 struct context_entry *context;
591 int ret;
592 unsigned long flags;
593
594 spin_lock_irqsave(&iommu->lock, flags);
595 root = &iommu->root_entry[bus];
596 context = get_context_addr_from_root(root);
597 if (!context) {
598 ret = 0;
599 goto out;
600 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000601 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700602out:
603 spin_unlock_irqrestore(&iommu->lock, flags);
604 return ret;
605}
606
607static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
608{
609 struct root_entry *root;
610 struct context_entry *context;
611 unsigned long flags;
612
613 spin_lock_irqsave(&iommu->lock, flags);
614 root = &iommu->root_entry[bus];
615 context = get_context_addr_from_root(root);
616 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000617 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700618 __iommu_flush_cache(iommu, &context[devfn], \
619 sizeof(*context));
620 }
621 spin_unlock_irqrestore(&iommu->lock, flags);
622}
623
624static void free_context_table(struct intel_iommu *iommu)
625{
626 struct root_entry *root;
627 int i;
628 unsigned long flags;
629 struct context_entry *context;
630
631 spin_lock_irqsave(&iommu->lock, flags);
632 if (!iommu->root_entry) {
633 goto out;
634 }
635 for (i = 0; i < ROOT_ENTRY_NR; i++) {
636 root = &iommu->root_entry[i];
637 context = get_context_addr_from_root(root);
638 if (context)
639 free_pgtable_page(context);
640 }
641 free_pgtable_page(iommu->root_entry);
642 iommu->root_entry = NULL;
643out:
644 spin_unlock_irqrestore(&iommu->lock, flags);
645}
646
647/* page table handling */
648#define LEVEL_STRIDE (9)
649#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
650
651static inline int agaw_to_level(int agaw)
652{
653 return agaw + 2;
654}
655
656static inline int agaw_to_width(int agaw)
657{
658 return 30 + agaw * LEVEL_STRIDE;
659
660}
661
662static inline int width_to_agaw(int width)
663{
664 return (width - 30) / LEVEL_STRIDE;
665}
666
667static inline unsigned int level_to_offset_bits(int level)
668{
David Woodhouse6660c632009-06-27 22:41:00 +0100669 return (level - 1) * LEVEL_STRIDE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700670}
671
David Woodhouse77dfa562009-06-27 16:40:08 +0100672static inline int pfn_level_offset(unsigned long pfn, int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700673{
David Woodhouse6660c632009-06-27 22:41:00 +0100674 return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700675}
676
David Woodhouse6660c632009-06-27 22:41:00 +0100677static inline unsigned long level_mask(int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700678{
David Woodhouse6660c632009-06-27 22:41:00 +0100679 return -1UL << level_to_offset_bits(level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700680}
681
David Woodhouse6660c632009-06-27 22:41:00 +0100682static inline unsigned long level_size(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 align_to_level(unsigned long pfn, int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700688{
David Woodhouse6660c632009-06-27 22:41:00 +0100689 return (pfn + level_size(level) - 1) & level_mask(level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700690}
691
David Woodhouseb026fd22009-06-28 10:37:25 +0100692static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
693 unsigned long pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700694{
David Woodhouseb026fd22009-06-28 10:37:25 +0100695 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700696 struct dma_pte *parent, *pte = NULL;
697 int level = agaw_to_level(domain->agaw);
698 int offset;
699 unsigned long flags;
700
701 BUG_ON(!domain->pgd);
David Woodhouseb026fd22009-06-28 10:37:25 +0100702 BUG_ON(addr_width < BITS_PER_LONG && pfn >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700703 parent = domain->pgd;
704
705 spin_lock_irqsave(&domain->mapping_lock, flags);
706 while (level > 0) {
707 void *tmp_page;
708
David Woodhouseb026fd22009-06-28 10:37:25 +0100709 offset = pfn_level_offset(pfn, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700710 pte = &parent[offset];
711 if (level == 1)
712 break;
713
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000714 if (!dma_pte_present(pte)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700715 tmp_page = alloc_pgtable_page();
716
717 if (!tmp_page) {
718 spin_unlock_irqrestore(&domain->mapping_lock,
719 flags);
720 return NULL;
721 }
Weidong Han5331fe62008-12-08 23:00:00 +0800722 domain_flush_cache(domain, tmp_page, PAGE_SIZE);
David Woodhousedd4e8312009-06-27 16:21:20 +0100723 dma_set_pte_pfn(pte, virt_to_dma_pfn(tmp_page));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700724 /*
725 * high level table always sets r/w, last level page
726 * table control read/write
727 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000728 dma_set_pte_readable(pte);
729 dma_set_pte_writable(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800730 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700731 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000732 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700733 level--;
734 }
735
736 spin_unlock_irqrestore(&domain->mapping_lock, flags);
737 return pte;
738}
739
740/* return address's pte at specific level */
David Woodhouse90dcfb52009-06-27 17:14:59 +0100741static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
742 unsigned long pfn,
743 int level)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700744{
745 struct dma_pte *parent, *pte = NULL;
746 int total = agaw_to_level(domain->agaw);
747 int offset;
748
749 parent = domain->pgd;
750 while (level <= total) {
David Woodhouse90dcfb52009-06-27 17:14:59 +0100751 offset = pfn_level_offset(pfn, total);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700752 pte = &parent[offset];
753 if (level == total)
754 return pte;
755
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000756 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700757 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000758 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700759 total--;
760 }
761 return NULL;
762}
763
764/* clear one page's page table */
David Woodhousea75f7cf2009-06-27 17:44:39 +0100765static void dma_pte_clear_one(struct dmar_domain *domain, unsigned long pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700766{
767 struct dma_pte *pte = NULL;
768
769 /* get last level pte */
David Woodhousea75f7cf2009-06-27 17:44:39 +0100770 pte = dma_pfn_level_pte(domain, pfn, 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700771
772 if (pte) {
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000773 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800774 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700775 }
776}
777
778/* clear last level pte, a tlb flush should be followed */
David Woodhouse595badf2009-06-27 22:09:11 +0100779static void dma_pte_clear_range(struct dmar_domain *domain,
780 unsigned long start_pfn,
781 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700782{
David Woodhouse04b18e62009-06-27 19:15:01 +0100783 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700784
David Woodhouse04b18e62009-06-27 19:15:01 +0100785 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
David Woodhouse595badf2009-06-27 22:09:11 +0100786 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
David Woodhouse66eae842009-06-27 19:00:32 +0100787
David Woodhouse04b18e62009-06-27 19:15:01 +0100788 /* we don't need lock here; nobody else touches the iova range */
David Woodhouse595badf2009-06-27 22:09:11 +0100789 while (start_pfn <= last_pfn) {
David Woodhouse04b18e62009-06-27 19:15:01 +0100790 dma_pte_clear_one(domain, start_pfn);
791 start_pfn++;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700792 }
793}
794
795/* free page table pages. last level pte should already be cleared */
796static void dma_pte_free_pagetable(struct dmar_domain *domain,
David Woodhoused794dc92009-06-28 00:27:49 +0100797 unsigned long start_pfn,
798 unsigned long last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700799{
David Woodhouse6660c632009-06-27 22:41:00 +0100800 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700801 struct dma_pte *pte;
802 int total = agaw_to_level(domain->agaw);
803 int level;
David Woodhouse6660c632009-06-27 22:41:00 +0100804 unsigned long tmp;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700805
David Woodhouse6660c632009-06-27 22:41:00 +0100806 BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
807 BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700808
809 /* we don't need lock here, nobody else touches the iova range */
810 level = 2;
811 while (level <= total) {
David Woodhouse6660c632009-06-27 22:41:00 +0100812 tmp = align_to_level(start_pfn, level);
813
814 /* Only clear this pte/pmd if we're asked to clear its
815 _whole_ range */
816 if (tmp + level_size(level) - 1 > last_pfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700817 return;
818
David Woodhouse6660c632009-06-27 22:41:00 +0100819 while (tmp <= last_pfn) {
820 pte = dma_pfn_level_pte(domain, tmp, level);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700821 if (pte) {
822 free_pgtable_page(
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000823 phys_to_virt(dma_pte_addr(pte)));
824 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800825 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700826 }
827 tmp += level_size(level);
828 }
829 level++;
830 }
831 /* free pgd */
David Woodhoused794dc92009-06-28 00:27:49 +0100832 if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700833 free_pgtable_page(domain->pgd);
834 domain->pgd = NULL;
835 }
836}
837
838/* iommu handling */
839static int iommu_alloc_root_entry(struct intel_iommu *iommu)
840{
841 struct root_entry *root;
842 unsigned long flags;
843
844 root = (struct root_entry *)alloc_pgtable_page();
845 if (!root)
846 return -ENOMEM;
847
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700848 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700849
850 spin_lock_irqsave(&iommu->lock, flags);
851 iommu->root_entry = root;
852 spin_unlock_irqrestore(&iommu->lock, flags);
853
854 return 0;
855}
856
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700857static void iommu_set_root_entry(struct intel_iommu *iommu)
858{
859 void *addr;
David Woodhousec416daa2009-05-10 20:30:58 +0100860 u32 sts;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700861 unsigned long flag;
862
863 addr = iommu->root_entry;
864
865 spin_lock_irqsave(&iommu->register_lock, flag);
866 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
867
David Woodhousec416daa2009-05-10 20:30:58 +0100868 writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700869
870 /* Make sure hardware complete it */
871 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100872 readl, (sts & DMA_GSTS_RTPS), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700873
874 spin_unlock_irqrestore(&iommu->register_lock, flag);
875}
876
877static void iommu_flush_write_buffer(struct intel_iommu *iommu)
878{
879 u32 val;
880 unsigned long flag;
881
David Woodhouse9af88142009-02-13 23:18:03 +0000882 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700883 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700884
885 spin_lock_irqsave(&iommu->register_lock, flag);
David Woodhouse462b60f2009-05-10 20:18:18 +0100886 writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700887
888 /* Make sure hardware complete it */
889 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +0100890 readl, (!(val & DMA_GSTS_WBFS)), val);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700891
892 spin_unlock_irqrestore(&iommu->register_lock, flag);
893}
894
895/* return value determine if we need a write buffer flush */
David Woodhouse4c25a2c2009-05-10 17:16:06 +0100896static void __iommu_flush_context(struct intel_iommu *iommu,
897 u16 did, u16 source_id, u8 function_mask,
898 u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700899{
900 u64 val = 0;
901 unsigned long flag;
902
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700903 switch (type) {
904 case DMA_CCMD_GLOBAL_INVL:
905 val = DMA_CCMD_GLOBAL_INVL;
906 break;
907 case DMA_CCMD_DOMAIN_INVL:
908 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
909 break;
910 case DMA_CCMD_DEVICE_INVL:
911 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
912 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
913 break;
914 default:
915 BUG();
916 }
917 val |= DMA_CCMD_ICC;
918
919 spin_lock_irqsave(&iommu->register_lock, flag);
920 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
921
922 /* Make sure hardware complete it */
923 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
924 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
925
926 spin_unlock_irqrestore(&iommu->register_lock, flag);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700927}
928
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700929/* return value determine if we need a write buffer flush */
David Woodhouse1f0ef2a2009-05-10 19:58:49 +0100930static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
931 u64 addr, unsigned int size_order, u64 type)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700932{
933 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
934 u64 val = 0, val_iva = 0;
935 unsigned long flag;
936
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700937 switch (type) {
938 case DMA_TLB_GLOBAL_FLUSH:
939 /* global flush doesn't need set IVA_REG */
940 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
941 break;
942 case DMA_TLB_DSI_FLUSH:
943 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
944 break;
945 case DMA_TLB_PSI_FLUSH:
946 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
947 /* Note: always flush non-leaf currently */
948 val_iva = size_order | addr;
949 break;
950 default:
951 BUG();
952 }
953 /* Note: set drain read/write */
954#if 0
955 /*
956 * This is probably to be super secure.. Looks like we can
957 * ignore it without any impact.
958 */
959 if (cap_read_drain(iommu->cap))
960 val |= DMA_TLB_READ_DRAIN;
961#endif
962 if (cap_write_drain(iommu->cap))
963 val |= DMA_TLB_WRITE_DRAIN;
964
965 spin_lock_irqsave(&iommu->register_lock, flag);
966 /* Note: Only uses first TLB reg currently */
967 if (val_iva)
968 dmar_writeq(iommu->reg + tlb_offset, val_iva);
969 dmar_writeq(iommu->reg + tlb_offset + 8, val);
970
971 /* Make sure hardware complete it */
972 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
973 dmar_readq, (!(val & DMA_TLB_IVT)), val);
974
975 spin_unlock_irqrestore(&iommu->register_lock, flag);
976
977 /* check IOTLB invalidation granularity */
978 if (DMA_TLB_IAIG(val) == 0)
979 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
980 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
981 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700982 (unsigned long long)DMA_TLB_IIRG(type),
983 (unsigned long long)DMA_TLB_IAIG(val));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700984}
985
Yu Zhao93a23a72009-05-18 13:51:37 +0800986static struct device_domain_info *iommu_support_dev_iotlb(
987 struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700988{
Yu Zhao93a23a72009-05-18 13:51:37 +0800989 int found = 0;
990 unsigned long flags;
991 struct device_domain_info *info;
992 struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);
993
994 if (!ecap_dev_iotlb_support(iommu->ecap))
995 return NULL;
996
997 if (!iommu->qi)
998 return NULL;
999
1000 spin_lock_irqsave(&device_domain_lock, flags);
1001 list_for_each_entry(info, &domain->devices, link)
1002 if (info->bus == bus && info->devfn == devfn) {
1003 found = 1;
1004 break;
1005 }
1006 spin_unlock_irqrestore(&device_domain_lock, flags);
1007
1008 if (!found || !info->dev)
1009 return NULL;
1010
1011 if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
1012 return NULL;
1013
1014 if (!dmar_find_matched_atsr_unit(info->dev))
1015 return NULL;
1016
1017 info->iommu = iommu;
1018
1019 return info;
1020}
1021
1022static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1023{
1024 if (!info)
1025 return;
1026
1027 pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
1028}
1029
1030static void iommu_disable_dev_iotlb(struct device_domain_info *info)
1031{
1032 if (!info->dev || !pci_ats_enabled(info->dev))
1033 return;
1034
1035 pci_disable_ats(info->dev);
1036}
1037
1038static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
1039 u64 addr, unsigned mask)
1040{
1041 u16 sid, qdep;
1042 unsigned long flags;
1043 struct device_domain_info *info;
1044
1045 spin_lock_irqsave(&device_domain_lock, flags);
1046 list_for_each_entry(info, &domain->devices, link) {
1047 if (!info->dev || !pci_ats_enabled(info->dev))
1048 continue;
1049
1050 sid = info->bus << 8 | info->devfn;
1051 qdep = pci_ats_queue_depth(info->dev);
1052 qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
1053 }
1054 spin_unlock_irqrestore(&device_domain_lock, flags);
1055}
1056
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001057static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
David Woodhouse03d6a242009-06-28 15:33:46 +01001058 unsigned long pfn, unsigned int pages)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001059{
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001060 unsigned int mask = ilog2(__roundup_pow_of_two(pages));
David Woodhouse03d6a242009-06-28 15:33:46 +01001061 uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001062
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001063 BUG_ON(pages == 0);
1064
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001065 /*
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001066 * Fallback to domain selective flush if no PSI support or the size is
1067 * too big.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001068 * PSI requires page size to be 2 ^ x, and the base address is naturally
1069 * aligned to the size
1070 */
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001071 if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
1072 iommu->flush.flush_iotlb(iommu, did, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001073 DMA_TLB_DSI_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08001074 else
1075 iommu->flush.flush_iotlb(iommu, did, addr, mask,
1076 DMA_TLB_PSI_FLUSH);
Yu Zhaobf92df32009-06-29 11:31:45 +08001077
1078 /*
1079 * In caching mode, domain ID 0 is reserved for non-present to present
1080 * mapping flush. Device IOTLB doesn't need to be flushed in this case.
1081 */
1082 if (!cap_caching_mode(iommu->cap) || did)
Yu Zhao93a23a72009-05-18 13:51:37 +08001083 iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001084}
1085
mark grossf8bab732008-02-08 04:18:38 -08001086static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
1087{
1088 u32 pmen;
1089 unsigned long flags;
1090
1091 spin_lock_irqsave(&iommu->register_lock, flags);
1092 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1093 pmen &= ~DMA_PMEN_EPM;
1094 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1095
1096 /* wait for the protected region status bit to clear */
1097 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1098 readl, !(pmen & DMA_PMEN_PRS), pmen);
1099
1100 spin_unlock_irqrestore(&iommu->register_lock, flags);
1101}
1102
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001103static int iommu_enable_translation(struct intel_iommu *iommu)
1104{
1105 u32 sts;
1106 unsigned long flags;
1107
1108 spin_lock_irqsave(&iommu->register_lock, flags);
David Woodhousec416daa2009-05-10 20:30:58 +01001109 iommu->gcmd |= DMA_GCMD_TE;
1110 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001111
1112 /* Make sure hardware complete it */
1113 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001114 readl, (sts & DMA_GSTS_TES), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001115
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001116 spin_unlock_irqrestore(&iommu->register_lock, flags);
1117 return 0;
1118}
1119
1120static int iommu_disable_translation(struct intel_iommu *iommu)
1121{
1122 u32 sts;
1123 unsigned long flag;
1124
1125 spin_lock_irqsave(&iommu->register_lock, flag);
1126 iommu->gcmd &= ~DMA_GCMD_TE;
1127 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1128
1129 /* Make sure hardware complete it */
1130 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
David Woodhousec416daa2009-05-10 20:30:58 +01001131 readl, (!(sts & DMA_GSTS_TES)), sts);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001132
1133 spin_unlock_irqrestore(&iommu->register_lock, flag);
1134 return 0;
1135}
1136
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001137
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001138static int iommu_init_domains(struct intel_iommu *iommu)
1139{
1140 unsigned long ndomains;
1141 unsigned long nlongs;
1142
1143 ndomains = cap_ndoms(iommu->cap);
1144 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1145 nlongs = BITS_TO_LONGS(ndomains);
1146
1147 /* TBD: there might be 64K domains,
1148 * consider other allocation for future chip
1149 */
1150 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1151 if (!iommu->domain_ids) {
1152 printk(KERN_ERR "Allocating domain id array failed\n");
1153 return -ENOMEM;
1154 }
1155 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1156 GFP_KERNEL);
1157 if (!iommu->domains) {
1158 printk(KERN_ERR "Allocating domain array failed\n");
1159 kfree(iommu->domain_ids);
1160 return -ENOMEM;
1161 }
1162
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001163 spin_lock_init(&iommu->lock);
1164
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001165 /*
1166 * if Caching mode is set, then invalid translations are tagged
1167 * with domainid 0. Hence we need to pre-allocate it.
1168 */
1169 if (cap_caching_mode(iommu->cap))
1170 set_bit(0, iommu->domain_ids);
1171 return 0;
1172}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001173
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001174
1175static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001176static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001177
1178void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001179{
1180 struct dmar_domain *domain;
1181 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001182 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001183
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001184 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1185 for (; i < cap_ndoms(iommu->cap); ) {
1186 domain = iommu->domains[i];
1187 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001188
1189 spin_lock_irqsave(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001190 if (--domain->iommu_count == 0) {
1191 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1192 vm_domain_exit(domain);
1193 else
1194 domain_exit(domain);
1195 }
Weidong Hanc7151a82008-12-08 22:51:37 +08001196 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1197
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001198 i = find_next_bit(iommu->domain_ids,
1199 cap_ndoms(iommu->cap), i+1);
1200 }
1201
1202 if (iommu->gcmd & DMA_GCMD_TE)
1203 iommu_disable_translation(iommu);
1204
1205 if (iommu->irq) {
1206 set_irq_data(iommu->irq, NULL);
1207 /* This will mask the irq */
1208 free_irq(iommu->irq, iommu);
1209 destroy_irq(iommu->irq);
1210 }
1211
1212 kfree(iommu->domains);
1213 kfree(iommu->domain_ids);
1214
Weidong Hand9630fe2008-12-08 11:06:32 +08001215 g_iommus[iommu->seq_id] = NULL;
1216
1217 /* if all iommus are freed, free g_iommus */
1218 for (i = 0; i < g_num_of_iommus; i++) {
1219 if (g_iommus[i])
1220 break;
1221 }
1222
1223 if (i == g_num_of_iommus)
1224 kfree(g_iommus);
1225
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001226 /* free context mapping */
1227 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001228}
1229
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001230static struct dmar_domain *alloc_domain(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001231{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001232 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001233
1234 domain = alloc_domain_mem();
1235 if (!domain)
1236 return NULL;
1237
Weidong Han8c11e792008-12-08 15:29:22 +08001238 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
Weidong Hand71a2f32008-12-07 21:13:41 +08001239 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001240
1241 return domain;
1242}
1243
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001244static int iommu_attach_domain(struct dmar_domain *domain,
1245 struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001246{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001247 int num;
1248 unsigned long ndomains;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001249 unsigned long flags;
1250
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001251 ndomains = cap_ndoms(iommu->cap);
Weidong Han8c11e792008-12-08 15:29:22 +08001252
1253 spin_lock_irqsave(&iommu->lock, flags);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001254
1255 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1256 if (num >= ndomains) {
1257 spin_unlock_irqrestore(&iommu->lock, flags);
1258 printk(KERN_ERR "IOMMU: no free domain ids\n");
1259 return -ENOMEM;
1260 }
1261
1262 domain->id = num;
1263 set_bit(num, iommu->domain_ids);
1264 set_bit(iommu->seq_id, &domain->iommu_bmp);
1265 iommu->domains[num] = domain;
1266 spin_unlock_irqrestore(&iommu->lock, flags);
1267
1268 return 0;
1269}
1270
1271static void iommu_detach_domain(struct dmar_domain *domain,
1272 struct intel_iommu *iommu)
1273{
1274 unsigned long flags;
1275 int num, ndomains;
1276 int found = 0;
1277
1278 spin_lock_irqsave(&iommu->lock, flags);
1279 ndomains = cap_ndoms(iommu->cap);
1280 num = find_first_bit(iommu->domain_ids, ndomains);
1281 for (; num < ndomains; ) {
1282 if (iommu->domains[num] == domain) {
1283 found = 1;
1284 break;
1285 }
1286 num = find_next_bit(iommu->domain_ids,
1287 cap_ndoms(iommu->cap), num+1);
1288 }
1289
1290 if (found) {
1291 clear_bit(num, iommu->domain_ids);
1292 clear_bit(iommu->seq_id, &domain->iommu_bmp);
1293 iommu->domains[num] = NULL;
1294 }
Weidong Han8c11e792008-12-08 15:29:22 +08001295 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001296}
1297
1298static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001299static struct lock_class_key reserved_alloc_key;
1300static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001301
1302static void dmar_init_reserved_ranges(void)
1303{
1304 struct pci_dev *pdev = NULL;
1305 struct iova *iova;
1306 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001307
David Millerf6611972008-02-06 01:36:23 -08001308 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001309
Mark Gross8a443df2008-03-04 14:59:31 -08001310 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1311 &reserved_alloc_key);
1312 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1313 &reserved_rbtree_key);
1314
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001315 /* IOAPIC ranges shouldn't be accessed by DMA */
1316 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1317 IOVA_PFN(IOAPIC_RANGE_END));
1318 if (!iova)
1319 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1320
1321 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1322 for_each_pci_dev(pdev) {
1323 struct resource *r;
1324
1325 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1326 r = &pdev->resource[i];
1327 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1328 continue;
David Woodhouse1a4a4552009-06-28 16:00:42 +01001329 iova = reserve_iova(&reserved_iova_list,
1330 IOVA_PFN(r->start),
1331 IOVA_PFN(r->end));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001332 if (!iova)
1333 printk(KERN_ERR "Reserve iova failed\n");
1334 }
1335 }
1336
1337}
1338
1339static void domain_reserve_special_ranges(struct dmar_domain *domain)
1340{
1341 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1342}
1343
1344static inline int guestwidth_to_adjustwidth(int gaw)
1345{
1346 int agaw;
1347 int r = (gaw - 12) % 9;
1348
1349 if (r == 0)
1350 agaw = gaw;
1351 else
1352 agaw = gaw + 9 - r;
1353 if (agaw > 64)
1354 agaw = 64;
1355 return agaw;
1356}
1357
1358static int domain_init(struct dmar_domain *domain, int guest_width)
1359{
1360 struct intel_iommu *iommu;
1361 int adjust_width, agaw;
1362 unsigned long sagaw;
1363
David Millerf6611972008-02-06 01:36:23 -08001364 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001365 spin_lock_init(&domain->mapping_lock);
Weidong Hanc7151a82008-12-08 22:51:37 +08001366 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001367
1368 domain_reserve_special_ranges(domain);
1369
1370 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001371 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001372 if (guest_width > cap_mgaw(iommu->cap))
1373 guest_width = cap_mgaw(iommu->cap);
1374 domain->gaw = guest_width;
1375 adjust_width = guestwidth_to_adjustwidth(guest_width);
1376 agaw = width_to_agaw(adjust_width);
1377 sagaw = cap_sagaw(iommu->cap);
1378 if (!test_bit(agaw, &sagaw)) {
1379 /* hardware doesn't support it, choose a bigger one */
1380 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1381 agaw = find_next_bit(&sagaw, 5, agaw);
1382 if (agaw >= 5)
1383 return -ENODEV;
1384 }
1385 domain->agaw = agaw;
1386 INIT_LIST_HEAD(&domain->devices);
1387
Weidong Han8e6040972008-12-08 15:49:06 +08001388 if (ecap_coherent(iommu->ecap))
1389 domain->iommu_coherency = 1;
1390 else
1391 domain->iommu_coherency = 0;
1392
Sheng Yang58c610b2009-03-18 15:33:05 +08001393 if (ecap_sc_support(iommu->ecap))
1394 domain->iommu_snooping = 1;
1395 else
1396 domain->iommu_snooping = 0;
1397
Weidong Hanc7151a82008-12-08 22:51:37 +08001398 domain->iommu_count = 1;
1399
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001400 /* always allocate the top pgd */
1401 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1402 if (!domain->pgd)
1403 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001404 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001405 return 0;
1406}
1407
1408static void domain_exit(struct dmar_domain *domain)
1409{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001410 struct dmar_drhd_unit *drhd;
1411 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001412
1413 /* Domain 0 is reserved, so dont process it */
1414 if (!domain)
1415 return;
1416
1417 domain_remove_dev_info(domain);
1418 /* destroy iovas */
1419 put_iova_domain(&domain->iovad);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001420
1421 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01001422 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001423
1424 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01001425 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001426
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001427 for_each_active_iommu(iommu, drhd)
1428 if (test_bit(iommu->seq_id, &domain->iommu_bmp))
1429 iommu_detach_domain(domain, iommu);
1430
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001431 free_domain_mem(domain);
1432}
1433
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001434static int domain_context_mapping_one(struct dmar_domain *domain, int segment,
1435 u8 bus, u8 devfn, int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001436{
1437 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001438 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001439 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001440 struct dma_pte *pgd;
1441 unsigned long num;
1442 unsigned long ndomains;
1443 int id;
1444 int agaw;
Yu Zhao93a23a72009-05-18 13:51:37 +08001445 struct device_domain_info *info = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001446
1447 pr_debug("Set context mapping for %02x:%02x.%d\n",
1448 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001449
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001450 BUG_ON(!domain->pgd);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001451 BUG_ON(translation != CONTEXT_TT_PASS_THROUGH &&
1452 translation != CONTEXT_TT_MULTI_LEVEL);
Weidong Han5331fe62008-12-08 23:00:00 +08001453
David Woodhouse276dbf992009-04-04 01:45:37 +01001454 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001455 if (!iommu)
1456 return -ENODEV;
1457
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001458 context = device_to_context_entry(iommu, bus, devfn);
1459 if (!context)
1460 return -ENOMEM;
1461 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001462 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001463 spin_unlock_irqrestore(&iommu->lock, flags);
1464 return 0;
1465 }
1466
Weidong Hanea6606b2008-12-08 23:08:15 +08001467 id = domain->id;
1468 pgd = domain->pgd;
1469
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001470 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
1471 domain->flags & DOMAIN_FLAG_STATIC_IDENTITY) {
Weidong Hanea6606b2008-12-08 23:08:15 +08001472 int found = 0;
1473
1474 /* find an available domain id for this device in iommu */
1475 ndomains = cap_ndoms(iommu->cap);
1476 num = find_first_bit(iommu->domain_ids, ndomains);
1477 for (; num < ndomains; ) {
1478 if (iommu->domains[num] == domain) {
1479 id = num;
1480 found = 1;
1481 break;
1482 }
1483 num = find_next_bit(iommu->domain_ids,
1484 cap_ndoms(iommu->cap), num+1);
1485 }
1486
1487 if (found == 0) {
1488 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1489 if (num >= ndomains) {
1490 spin_unlock_irqrestore(&iommu->lock, flags);
1491 printk(KERN_ERR "IOMMU: no free domain ids\n");
1492 return -EFAULT;
1493 }
1494
1495 set_bit(num, iommu->domain_ids);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001496 set_bit(iommu->seq_id, &domain->iommu_bmp);
Weidong Hanea6606b2008-12-08 23:08:15 +08001497 iommu->domains[num] = domain;
1498 id = num;
1499 }
1500
1501 /* Skip top levels of page tables for
1502 * iommu which has less agaw than default.
1503 */
1504 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1505 pgd = phys_to_virt(dma_pte_addr(pgd));
1506 if (!dma_pte_present(pgd)) {
1507 spin_unlock_irqrestore(&iommu->lock, flags);
1508 return -ENOMEM;
1509 }
1510 }
1511 }
1512
1513 context_set_domain_id(context, id);
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001514
Yu Zhao93a23a72009-05-18 13:51:37 +08001515 if (translation != CONTEXT_TT_PASS_THROUGH) {
1516 info = iommu_support_dev_iotlb(domain, segment, bus, devfn);
1517 translation = info ? CONTEXT_TT_DEV_IOTLB :
1518 CONTEXT_TT_MULTI_LEVEL;
1519 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001520 /*
1521 * In pass through mode, AW must be programmed to indicate the largest
1522 * AGAW value supported by hardware. And ASR is ignored by hardware.
1523 */
Yu Zhao93a23a72009-05-18 13:51:37 +08001524 if (unlikely(translation == CONTEXT_TT_PASS_THROUGH))
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001525 context_set_address_width(context, iommu->msagaw);
Yu Zhao93a23a72009-05-18 13:51:37 +08001526 else {
1527 context_set_address_root(context, virt_to_phys(pgd));
1528 context_set_address_width(context, iommu->agaw);
1529 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001530
1531 context_set_translation_type(context, translation);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001532 context_set_fault_enable(context);
1533 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001534 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001535
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001536 /*
1537 * It's a non-present to present mapping. If hardware doesn't cache
1538 * non-present entry we only need to flush the write-buffer. If the
1539 * _does_ cache non-present entries, then it does so in the special
1540 * domain #0, which we have to flush:
1541 */
1542 if (cap_caching_mode(iommu->cap)) {
1543 iommu->flush.flush_context(iommu, 0,
1544 (((u16)bus) << 8) | devfn,
1545 DMA_CCMD_MASK_NOBIT,
1546 DMA_CCMD_DEVICE_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001547 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001548 } else {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001549 iommu_flush_write_buffer(iommu);
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001550 }
Yu Zhao93a23a72009-05-18 13:51:37 +08001551 iommu_enable_dev_iotlb(info);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001552 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001553
1554 spin_lock_irqsave(&domain->iommu_lock, flags);
1555 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1556 domain->iommu_count++;
Sheng Yang58c610b2009-03-18 15:33:05 +08001557 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001558 }
1559 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001560 return 0;
1561}
1562
1563static int
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001564domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev,
1565 int translation)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001566{
1567 int ret;
1568 struct pci_dev *tmp, *parent;
1569
David Woodhouse276dbf992009-04-04 01:45:37 +01001570 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001571 pdev->bus->number, pdev->devfn,
1572 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001573 if (ret)
1574 return ret;
1575
1576 /* dependent device mapping */
1577 tmp = pci_find_upstream_pcie_bridge(pdev);
1578 if (!tmp)
1579 return 0;
1580 /* Secondary interface's bus number and devfn 0 */
1581 parent = pdev->bus->self;
1582 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001583 ret = domain_context_mapping_one(domain,
1584 pci_domain_nr(parent->bus),
1585 parent->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001586 parent->devfn, translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001587 if (ret)
1588 return ret;
1589 parent = parent->bus->self;
1590 }
1591 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1592 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001593 pci_domain_nr(tmp->subordinate),
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001594 tmp->subordinate->number, 0,
1595 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001596 else /* this is a legacy PCI bridge */
1597 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001598 pci_domain_nr(tmp->bus),
1599 tmp->bus->number,
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001600 tmp->devfn,
1601 translation);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001602}
1603
Weidong Han5331fe62008-12-08 23:00:00 +08001604static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001605{
1606 int ret;
1607 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001608 struct intel_iommu *iommu;
1609
David Woodhouse276dbf992009-04-04 01:45:37 +01001610 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1611 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001612 if (!iommu)
1613 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001614
David Woodhouse276dbf992009-04-04 01:45:37 +01001615 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001616 if (!ret)
1617 return ret;
1618 /* dependent device mapping */
1619 tmp = pci_find_upstream_pcie_bridge(pdev);
1620 if (!tmp)
1621 return ret;
1622 /* Secondary interface's bus number and devfn 0 */
1623 parent = pdev->bus->self;
1624 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001625 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001626 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001627 if (!ret)
1628 return ret;
1629 parent = parent->bus->self;
1630 }
1631 if (tmp->is_pcie)
David Woodhouse276dbf992009-04-04 01:45:37 +01001632 return device_context_mapped(iommu, tmp->subordinate->number,
1633 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001634 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001635 return device_context_mapped(iommu, tmp->bus->number,
1636 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001637}
1638
David Woodhouse61df7442009-06-28 11:55:58 +01001639static int domain_pfn_mapping(struct dmar_domain *domain, unsigned long iov_pfn,
1640 unsigned long phys_pfn, unsigned long nr_pages,
1641 int prot)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001642{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001643 struct dma_pte *pte;
David Woodhouse1c5a46e2009-06-28 10:53:37 +01001644 int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001645
David Woodhouse61df7442009-06-28 11:55:58 +01001646 BUG_ON(addr_width < BITS_PER_LONG && (iov_pfn + nr_pages - 1) >> addr_width);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001647
1648 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1649 return -EINVAL;
David Woodhouse1c5a46e2009-06-28 10:53:37 +01001650
David Woodhouse61df7442009-06-28 11:55:58 +01001651 while (nr_pages--) {
1652 pte = pfn_to_dma_pte(domain, iov_pfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001653 if (!pte)
1654 return -ENOMEM;
1655 /* We don't need lock here, nobody else
1656 * touches the iova range
1657 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001658 BUG_ON(dma_pte_addr(pte));
David Woodhouse61df7442009-06-28 11:55:58 +01001659 dma_set_pte_pfn(pte, phys_pfn);
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001660 dma_set_pte_prot(pte, prot);
Sheng Yang9cf066972009-03-18 15:33:07 +08001661 if (prot & DMA_PTE_SNP)
1662 dma_set_pte_snp(pte);
Weidong Han5331fe62008-12-08 23:00:00 +08001663 domain_flush_cache(domain, pte, sizeof(*pte));
David Woodhouse61df7442009-06-28 11:55:58 +01001664 iov_pfn++;
1665 phys_pfn++;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001666 }
1667 return 0;
1668}
1669
Weidong Hanc7151a82008-12-08 22:51:37 +08001670static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001671{
Weidong Hanc7151a82008-12-08 22:51:37 +08001672 if (!iommu)
1673 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001674
1675 clear_context_table(iommu, bus, devfn);
1676 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse4c25a2c2009-05-10 17:16:06 +01001677 DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01001678 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001679}
1680
1681static void domain_remove_dev_info(struct dmar_domain *domain)
1682{
1683 struct device_domain_info *info;
1684 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001685 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001686
1687 spin_lock_irqsave(&device_domain_lock, flags);
1688 while (!list_empty(&domain->devices)) {
1689 info = list_entry(domain->devices.next,
1690 struct device_domain_info, link);
1691 list_del(&info->link);
1692 list_del(&info->global);
1693 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001694 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001695 spin_unlock_irqrestore(&device_domain_lock, flags);
1696
Yu Zhao93a23a72009-05-18 13:51:37 +08001697 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01001698 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001699 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001700 free_devinfo_mem(info);
1701
1702 spin_lock_irqsave(&device_domain_lock, flags);
1703 }
1704 spin_unlock_irqrestore(&device_domain_lock, flags);
1705}
1706
1707/*
1708 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001709 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001710 */
Kay, Allen M38717942008-09-09 18:37:29 +03001711static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001712find_domain(struct pci_dev *pdev)
1713{
1714 struct device_domain_info *info;
1715
1716 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001717 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001718 if (info)
1719 return info->domain;
1720 return NULL;
1721}
1722
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001723/* domain is initialized */
1724static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1725{
1726 struct dmar_domain *domain, *found = NULL;
1727 struct intel_iommu *iommu;
1728 struct dmar_drhd_unit *drhd;
1729 struct device_domain_info *info, *tmp;
1730 struct pci_dev *dev_tmp;
1731 unsigned long flags;
1732 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001733 int segment;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001734 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001735
1736 domain = find_domain(pdev);
1737 if (domain)
1738 return domain;
1739
David Woodhouse276dbf992009-04-04 01:45:37 +01001740 segment = pci_domain_nr(pdev->bus);
1741
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001742 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1743 if (dev_tmp) {
1744 if (dev_tmp->is_pcie) {
1745 bus = dev_tmp->subordinate->number;
1746 devfn = 0;
1747 } else {
1748 bus = dev_tmp->bus->number;
1749 devfn = dev_tmp->devfn;
1750 }
1751 spin_lock_irqsave(&device_domain_lock, flags);
1752 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001753 if (info->segment == segment &&
1754 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001755 found = info->domain;
1756 break;
1757 }
1758 }
1759 spin_unlock_irqrestore(&device_domain_lock, flags);
1760 /* pcie-pci bridge already has a domain, uses it */
1761 if (found) {
1762 domain = found;
1763 goto found_domain;
1764 }
1765 }
1766
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001767 domain = alloc_domain();
1768 if (!domain)
1769 goto error;
1770
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001771 /* Allocate new domain for the device */
1772 drhd = dmar_find_matched_drhd_unit(pdev);
1773 if (!drhd) {
1774 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1775 pci_name(pdev));
1776 return NULL;
1777 }
1778 iommu = drhd->iommu;
1779
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001780 ret = iommu_attach_domain(domain, iommu);
1781 if (ret) {
1782 domain_exit(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001783 goto error;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001784 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001785
1786 if (domain_init(domain, gaw)) {
1787 domain_exit(domain);
1788 goto error;
1789 }
1790
1791 /* register pcie-to-pci device */
1792 if (dev_tmp) {
1793 info = alloc_devinfo_mem();
1794 if (!info) {
1795 domain_exit(domain);
1796 goto error;
1797 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001798 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001799 info->bus = bus;
1800 info->devfn = devfn;
1801 info->dev = NULL;
1802 info->domain = domain;
1803 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001804 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001805
1806 /* pcie-to-pci bridge already has a domain, uses it */
1807 found = NULL;
1808 spin_lock_irqsave(&device_domain_lock, flags);
1809 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001810 if (tmp->segment == segment &&
1811 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001812 found = tmp->domain;
1813 break;
1814 }
1815 }
1816 if (found) {
1817 free_devinfo_mem(info);
1818 domain_exit(domain);
1819 domain = found;
1820 } else {
1821 list_add(&info->link, &domain->devices);
1822 list_add(&info->global, &device_domain_list);
1823 }
1824 spin_unlock_irqrestore(&device_domain_lock, flags);
1825 }
1826
1827found_domain:
1828 info = alloc_devinfo_mem();
1829 if (!info)
1830 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01001831 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001832 info->bus = pdev->bus->number;
1833 info->devfn = pdev->devfn;
1834 info->dev = pdev;
1835 info->domain = domain;
1836 spin_lock_irqsave(&device_domain_lock, flags);
1837 /* somebody is fast */
1838 found = find_domain(pdev);
1839 if (found != NULL) {
1840 spin_unlock_irqrestore(&device_domain_lock, flags);
1841 if (found != domain) {
1842 domain_exit(domain);
1843 domain = found;
1844 }
1845 free_devinfo_mem(info);
1846 return domain;
1847 }
1848 list_add(&info->link, &domain->devices);
1849 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001850 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001851 spin_unlock_irqrestore(&device_domain_lock, flags);
1852 return domain;
1853error:
1854 /* recheck it here, maybe others set it */
1855 return find_domain(pdev);
1856}
1857
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001858static int iommu_identity_mapping;
1859
David Woodhouseb2132032009-06-26 18:50:28 +01001860static int iommu_domain_identity_map(struct dmar_domain *domain,
1861 unsigned long long start,
1862 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001863{
David Woodhousec5395d52009-06-28 16:35:56 +01001864 unsigned long first_vpfn = start >> VTD_PAGE_SHIFT;
1865 unsigned long last_vpfn = end >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001866
David Woodhousec5395d52009-06-28 16:35:56 +01001867 if (!reserve_iova(&domain->iovad, dma_to_mm_pfn(first_vpfn),
1868 dma_to_mm_pfn(last_vpfn))) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001869 printk(KERN_ERR "IOMMU: reserve iova failed\n");
David Woodhouseb2132032009-06-26 18:50:28 +01001870 return -ENOMEM;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001871 }
1872
David Woodhousec5395d52009-06-28 16:35:56 +01001873 pr_debug("Mapping reserved region %llx-%llx for domain %d\n",
1874 start, end, domain->id);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001875 /*
1876 * RMRR range might have overlap with physical memory range,
1877 * clear it first
1878 */
David Woodhousec5395d52009-06-28 16:35:56 +01001879 dma_pte_clear_range(domain, first_vpfn, last_vpfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001880
David Woodhousec5395d52009-06-28 16:35:56 +01001881 return domain_pfn_mapping(domain, first_vpfn, first_vpfn,
1882 last_vpfn - first_vpfn + 1,
David Woodhouse61df7442009-06-28 11:55:58 +01001883 DMA_PTE_READ|DMA_PTE_WRITE);
David Woodhouseb2132032009-06-26 18:50:28 +01001884}
1885
1886static int iommu_prepare_identity_map(struct pci_dev *pdev,
1887 unsigned long long start,
1888 unsigned long long end)
1889{
1890 struct dmar_domain *domain;
1891 int ret;
1892
1893 printk(KERN_INFO
1894 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1895 pci_name(pdev), start, end);
1896
David Woodhousec7ab48d2009-06-26 19:10:36 +01001897 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
David Woodhouseb2132032009-06-26 18:50:28 +01001898 if (!domain)
1899 return -ENOMEM;
1900
1901 ret = iommu_domain_identity_map(domain, start, end);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001902 if (ret)
1903 goto error;
1904
1905 /* context entry init */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001906 ret = domain_context_mapping(domain, pdev, CONTEXT_TT_MULTI_LEVEL);
David Woodhouseb2132032009-06-26 18:50:28 +01001907 if (ret)
1908 goto error;
1909
1910 return 0;
1911
1912 error:
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001913 domain_exit(domain);
1914 return ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001915}
1916
1917static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1918 struct pci_dev *pdev)
1919{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001920 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001921 return 0;
1922 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1923 rmrr->end_address + 1);
1924}
1925
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001926#ifdef CONFIG_DMAR_FLOPPY_WA
1927static inline void iommu_prepare_isa(void)
1928{
1929 struct pci_dev *pdev;
1930 int ret;
1931
1932 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1933 if (!pdev)
1934 return;
1935
David Woodhousec7ab48d2009-06-26 19:10:36 +01001936 printk(KERN_INFO "IOMMU: Prepare 0-16MiB unity mapping for LPC\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001937 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1938
1939 if (ret)
David Woodhousec7ab48d2009-06-26 19:10:36 +01001940 printk(KERN_ERR "IOMMU: Failed to create 0-16MiB identity map; "
1941 "floppy might not work\n");
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001942
1943}
1944#else
1945static inline void iommu_prepare_isa(void)
1946{
1947 return;
1948}
1949#endif /* !CONFIG_DMAR_FLPY_WA */
1950
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07001951/* Initialize each context entry as pass through.*/
1952static int __init init_context_pass_through(void)
1953{
1954 struct pci_dev *pdev = NULL;
1955 struct dmar_domain *domain;
1956 int ret;
1957
1958 for_each_pci_dev(pdev) {
1959 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1960 ret = domain_context_mapping(domain, pdev,
1961 CONTEXT_TT_PASS_THROUGH);
1962 if (ret)
1963 return ret;
1964 }
1965 return 0;
1966}
1967
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001968static int md_domain_init(struct dmar_domain *domain, int guest_width);
David Woodhousec7ab48d2009-06-26 19:10:36 +01001969
1970static int __init si_domain_work_fn(unsigned long start_pfn,
1971 unsigned long end_pfn, void *datax)
1972{
1973 int *ret = datax;
1974
1975 *ret = iommu_domain_identity_map(si_domain,
1976 (uint64_t)start_pfn << PAGE_SHIFT,
1977 (uint64_t)end_pfn << PAGE_SHIFT);
1978 return *ret;
1979
1980}
1981
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001982static int si_domain_init(void)
1983{
1984 struct dmar_drhd_unit *drhd;
1985 struct intel_iommu *iommu;
David Woodhousec7ab48d2009-06-26 19:10:36 +01001986 int nid, ret = 0;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001987
1988 si_domain = alloc_domain();
1989 if (!si_domain)
1990 return -EFAULT;
1991
David Woodhousec7ab48d2009-06-26 19:10:36 +01001992 pr_debug("Identity mapping domain is domain %d\n", si_domain->id);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07001993
1994 for_each_active_iommu(iommu, drhd) {
1995 ret = iommu_attach_domain(si_domain, iommu);
1996 if (ret) {
1997 domain_exit(si_domain);
1998 return -EFAULT;
1999 }
2000 }
2001
2002 if (md_domain_init(si_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
2003 domain_exit(si_domain);
2004 return -EFAULT;
2005 }
2006
2007 si_domain->flags = DOMAIN_FLAG_STATIC_IDENTITY;
2008
David Woodhousec7ab48d2009-06-26 19:10:36 +01002009 for_each_online_node(nid) {
2010 work_with_active_regions(nid, si_domain_work_fn, &ret);
2011 if (ret)
2012 return ret;
2013 }
2014
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002015 return 0;
2016}
2017
2018static void domain_remove_one_dev_info(struct dmar_domain *domain,
2019 struct pci_dev *pdev);
2020static int identity_mapping(struct pci_dev *pdev)
2021{
2022 struct device_domain_info *info;
2023
2024 if (likely(!iommu_identity_mapping))
2025 return 0;
2026
2027
2028 list_for_each_entry(info, &si_domain->devices, link)
2029 if (info->dev == pdev)
2030 return 1;
2031 return 0;
2032}
2033
2034static int domain_add_dev_info(struct dmar_domain *domain,
2035 struct pci_dev *pdev)
2036{
2037 struct device_domain_info *info;
2038 unsigned long flags;
2039
2040 info = alloc_devinfo_mem();
2041 if (!info)
2042 return -ENOMEM;
2043
2044 info->segment = pci_domain_nr(pdev->bus);
2045 info->bus = pdev->bus->number;
2046 info->devfn = pdev->devfn;
2047 info->dev = pdev;
2048 info->domain = domain;
2049
2050 spin_lock_irqsave(&device_domain_lock, flags);
2051 list_add(&info->link, &domain->devices);
2052 list_add(&info->global, &device_domain_list);
2053 pdev->dev.archdata.iommu = info;
2054 spin_unlock_irqrestore(&device_domain_lock, flags);
2055
2056 return 0;
2057}
2058
2059static int iommu_prepare_static_identity_mapping(void)
2060{
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002061 struct pci_dev *pdev = NULL;
2062 int ret;
2063
2064 ret = si_domain_init();
2065 if (ret)
2066 return -EFAULT;
2067
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002068 for_each_pci_dev(pdev) {
David Woodhousec7ab48d2009-06-26 19:10:36 +01002069 printk(KERN_INFO "IOMMU: identity mapping for device %s\n",
2070 pci_name(pdev));
2071
2072 ret = domain_context_mapping(si_domain, pdev,
2073 CONTEXT_TT_MULTI_LEVEL);
2074 if (ret)
2075 return ret;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002076 ret = domain_add_dev_info(si_domain, pdev);
2077 if (ret)
2078 return ret;
2079 }
2080
2081 return 0;
2082}
2083
2084int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002085{
2086 struct dmar_drhd_unit *drhd;
2087 struct dmar_rmrr_unit *rmrr;
2088 struct pci_dev *pdev;
2089 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07002090 int i, ret;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002091 int pass_through = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002092
2093 /*
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002094 * In case pass through can not be enabled, iommu tries to use identity
2095 * mapping.
2096 */
2097 if (iommu_pass_through)
2098 iommu_identity_mapping = 1;
2099
2100 /*
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002101 * for each drhd
2102 * allocate root
2103 * initialize and program root entry to not present
2104 * endfor
2105 */
2106 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08002107 g_num_of_iommus++;
2108 /*
2109 * lock not needed as this is only incremented in the single
2110 * threaded kernel __init code path all other access are read
2111 * only
2112 */
2113 }
2114
Weidong Hand9630fe2008-12-08 11:06:32 +08002115 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
2116 GFP_KERNEL);
2117 if (!g_iommus) {
2118 printk(KERN_ERR "Allocating global iommu array failed\n");
2119 ret = -ENOMEM;
2120 goto error;
2121 }
2122
mark gross80b20dd2008-04-18 13:53:58 -07002123 deferred_flush = kzalloc(g_num_of_iommus *
2124 sizeof(struct deferred_flush_tables), GFP_KERNEL);
2125 if (!deferred_flush) {
Weidong Hand9630fe2008-12-08 11:06:32 +08002126 kfree(g_iommus);
mark gross5e0d2a62008-03-04 15:22:08 -08002127 ret = -ENOMEM;
2128 goto error;
2129 }
2130
mark gross5e0d2a62008-03-04 15:22:08 -08002131 for_each_drhd_unit(drhd) {
2132 if (drhd->ignored)
2133 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002134
2135 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08002136 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002137
Suresh Siddhae61d98d2008-07-10 11:16:35 -07002138 ret = iommu_init_domains(iommu);
2139 if (ret)
2140 goto error;
2141
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002142 /*
2143 * TBD:
2144 * we could share the same root & context tables
2145 * amoung all IOMMU's. Need to Split it later.
2146 */
2147 ret = iommu_alloc_root_entry(iommu);
2148 if (ret) {
2149 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
2150 goto error;
2151 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002152 if (!ecap_pass_through(iommu->ecap))
2153 pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002154 }
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002155 if (iommu_pass_through)
2156 if (!pass_through) {
2157 printk(KERN_INFO
2158 "Pass Through is not supported by hardware.\n");
2159 iommu_pass_through = 0;
2160 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002161
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002162 /*
2163 * Start from the sane iommu hardware state.
2164 */
Youquan Songa77b67d2008-10-16 16:31:56 -07002165 for_each_drhd_unit(drhd) {
2166 if (drhd->ignored)
2167 continue;
2168
2169 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07002170
2171 /*
2172 * If the queued invalidation is already initialized by us
2173 * (for example, while enabling interrupt-remapping) then
2174 * we got the things already rolling from a sane state.
2175 */
2176 if (iommu->qi)
2177 continue;
2178
2179 /*
2180 * Clear any previous faults.
2181 */
2182 dmar_fault(-1, iommu);
2183 /*
2184 * Disable queued invalidation if supported and already enabled
2185 * before OS handover.
2186 */
2187 dmar_disable_qi(iommu);
2188 }
2189
2190 for_each_drhd_unit(drhd) {
2191 if (drhd->ignored)
2192 continue;
2193
2194 iommu = drhd->iommu;
2195
Youquan Songa77b67d2008-10-16 16:31:56 -07002196 if (dmar_enable_qi(iommu)) {
2197 /*
2198 * Queued Invalidate not enabled, use Register Based
2199 * Invalidate
2200 */
2201 iommu->flush.flush_context = __iommu_flush_context;
2202 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
2203 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002204 "invalidation\n",
2205 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002206 } else {
2207 iommu->flush.flush_context = qi_flush_context;
2208 iommu->flush.flush_iotlb = qi_flush_iotlb;
2209 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09002210 "invalidation\n",
2211 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07002212 }
2213 }
2214
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002215 /*
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002216 * If pass through is set and enabled, context entries of all pci
2217 * devices are intialized by pass through translation type.
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002218 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002219 if (iommu_pass_through) {
2220 ret = init_context_pass_through();
2221 if (ret) {
2222 printk(KERN_ERR "IOMMU: Pass through init failed.\n");
2223 iommu_pass_through = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002224 }
2225 }
2226
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002227 /*
2228 * If pass through is not set or not enabled, setup context entries for
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002229 * identity mappings for rmrr, gfx, and isa and may fall back to static
2230 * identity mapping if iommu_identity_mapping is set.
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002231 */
2232 if (!iommu_pass_through) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002233 if (iommu_identity_mapping)
2234 iommu_prepare_static_identity_mapping();
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002235 /*
2236 * For each rmrr
2237 * for each dev attached to rmrr
2238 * do
2239 * locate drhd for dev, alloc domain for dev
2240 * allocate free domain
2241 * allocate page table entries for rmrr
2242 * if context not allocated for bus
2243 * allocate and init context
2244 * set present in root table for this bus
2245 * init context with domain, translation etc
2246 * endfor
2247 * endfor
2248 */
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002249 printk(KERN_INFO "IOMMU: Setting RMRR:\n");
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002250 for_each_rmrr_units(rmrr) {
2251 for (i = 0; i < rmrr->devices_cnt; i++) {
2252 pdev = rmrr->devices[i];
2253 /*
2254 * some BIOS lists non-exist devices in DMAR
2255 * table.
2256 */
2257 if (!pdev)
2258 continue;
2259 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2260 if (ret)
2261 printk(KERN_ERR
2262 "IOMMU: mapping reserved region failed\n");
2263 }
2264 }
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002265
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002266 iommu_prepare_isa();
2267 }
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002268
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002269 /*
2270 * for each drhd
2271 * enable fault log
2272 * global invalidate context cache
2273 * global invalidate iotlb
2274 * enable translation
2275 */
2276 for_each_drhd_unit(drhd) {
2277 if (drhd->ignored)
2278 continue;
2279 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002280
2281 iommu_flush_write_buffer(iommu);
2282
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002283 ret = dmar_set_interrupt(iommu);
2284 if (ret)
2285 goto error;
2286
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002287 iommu_set_root_entry(iommu);
2288
David Woodhouse4c25a2c2009-05-10 17:16:06 +01002289 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002290 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH);
mark grossf8bab732008-02-08 04:18:38 -08002291 iommu_disable_protect_mem_regions(iommu);
2292
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002293 ret = iommu_enable_translation(iommu);
2294 if (ret)
2295 goto error;
2296 }
2297
2298 return 0;
2299error:
2300 for_each_drhd_unit(drhd) {
2301 if (drhd->ignored)
2302 continue;
2303 iommu = drhd->iommu;
2304 free_iommu(iommu);
2305 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002306 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002307 return ret;
2308}
2309
David Woodhouse88cb6a72009-06-28 15:03:06 +01002310static inline unsigned long aligned_nrpages(unsigned long host_addr,
2311 size_t size)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002312{
David Woodhouse88cb6a72009-06-28 15:03:06 +01002313 host_addr &= ~PAGE_MASK;
2314 host_addr += size + PAGE_SIZE - 1;
2315
2316 return host_addr >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002317}
2318
2319struct iova *
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002320iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002321{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002322 struct iova *piova;
2323
2324 /* Make sure it's in range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002325 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002326 if (!size || (IOVA_START_ADDR + size > end))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002327 return NULL;
2328
2329 piova = alloc_iova(&domain->iovad,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002330 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002331 return piova;
2332}
2333
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002334static struct iova *
2335__intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002336 size_t size, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002337{
2338 struct pci_dev *pdev = to_pci_dev(dev);
2339 struct iova *iova = NULL;
2340
Yang Hongyang284901a2009-04-06 19:01:15 -07002341 if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002342 iova = iommu_alloc_iova(domain, size, dma_mask);
2343 else {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002344 /*
2345 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002346 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002347 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002348 */
Yang Hongyang284901a2009-04-06 19:01:15 -07002349 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002350 if (!iova)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002351 iova = iommu_alloc_iova(domain, size, dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002352 }
2353
2354 if (!iova) {
2355 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2356 return NULL;
2357 }
2358
2359 return iova;
2360}
2361
2362static struct dmar_domain *
2363get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002364{
2365 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002366 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002367
2368 domain = get_domain_for_dev(pdev,
2369 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2370 if (!domain) {
2371 printk(KERN_ERR
2372 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002373 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002374 }
2375
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002376 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002377 if (unlikely(!domain_context_mapped(pdev))) {
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07002378 ret = domain_context_mapping(domain, pdev,
2379 CONTEXT_TT_MULTI_LEVEL);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002380 if (ret) {
2381 printk(KERN_ERR
2382 "Domain context map for %s failed",
2383 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002384 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002385 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002386 }
2387
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002388 return domain;
2389}
2390
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002391static int iommu_dummy(struct pci_dev *pdev)
2392{
2393 return pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO;
2394}
2395
2396/* Check if the pdev needs to go through non-identity map and unmap process.*/
2397static int iommu_no_mapping(struct pci_dev *pdev)
2398{
2399 int found;
2400
2401 if (!iommu_identity_mapping)
2402 return iommu_dummy(pdev);
2403
2404 found = identity_mapping(pdev);
2405 if (found) {
2406 if (pdev->dma_mask > DMA_BIT_MASK(32))
2407 return 1;
2408 else {
2409 /*
2410 * 32 bit DMA is removed from si_domain and fall back
2411 * to non-identity mapping.
2412 */
2413 domain_remove_one_dev_info(si_domain, pdev);
2414 printk(KERN_INFO "32bit %s uses non-identity mapping\n",
2415 pci_name(pdev));
2416 return 0;
2417 }
2418 } else {
2419 /*
2420 * In case of a detached 64 bit DMA device from vm, the device
2421 * is put into si_domain for identity mapping.
2422 */
2423 if (pdev->dma_mask > DMA_BIT_MASK(32)) {
2424 int ret;
2425 ret = domain_add_dev_info(si_domain, pdev);
2426 if (!ret) {
2427 printk(KERN_INFO "64bit %s uses identity mapping\n",
2428 pci_name(pdev));
2429 return 1;
2430 }
2431 }
2432 }
2433
2434 return iommu_dummy(pdev);
2435}
2436
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002437static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2438 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002439{
2440 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002441 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002442 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002443 struct iova *iova;
2444 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002445 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002446 struct intel_iommu *iommu;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002447
2448 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002449
2450 if (iommu_no_mapping(pdev))
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002451 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002452
2453 domain = get_valid_domain_for_dev(pdev);
2454 if (!domain)
2455 return 0;
2456
Weidong Han8c11e792008-12-08 15:29:22 +08002457 iommu = domain_get_iommu(domain);
David Woodhouse88cb6a72009-06-28 15:03:06 +01002458 size = aligned_nrpages(paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002459
David Woodhouse0ab36de2009-06-28 14:01:43 +01002460 iova = __intel_alloc_iova(hwdev, domain, size << VTD_PAGE_SHIFT, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002461 if (!iova)
2462 goto error;
2463
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002464 /*
2465 * Check if DMAR supports zero-length reads on write only
2466 * mappings..
2467 */
2468 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002469 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002470 prot |= DMA_PTE_READ;
2471 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2472 prot |= DMA_PTE_WRITE;
2473 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002474 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002475 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002476 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002477 * is not a big problem
2478 */
David Woodhouse0ab36de2009-06-28 14:01:43 +01002479 ret = domain_pfn_mapping(domain, mm_to_dma_pfn(iova->pfn_lo),
2480 paddr >> VTD_PAGE_SHIFT, size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002481 if (ret)
2482 goto error;
2483
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002484 /* it's a non-present to present mapping. Only flush if caching mode */
2485 if (cap_caching_mode(iommu->cap))
David Woodhouse03d6a242009-06-28 15:33:46 +01002486 iommu_flush_iotlb_psi(iommu, 0, mm_to_dma_pfn(iova->pfn_lo), size);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002487 else
Weidong Han8c11e792008-12-08 15:29:22 +08002488 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002489
David Woodhouse03d6a242009-06-28 15:33:46 +01002490 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
2491 start_paddr += paddr & ~PAGE_MASK;
2492 return start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002493
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002494error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002495 if (iova)
2496 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002497 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002498 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002499 return 0;
2500}
2501
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002502static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2503 unsigned long offset, size_t size,
2504 enum dma_data_direction dir,
2505 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002506{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002507 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2508 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002509}
2510
mark gross5e0d2a62008-03-04 15:22:08 -08002511static void flush_unmaps(void)
2512{
mark gross80b20dd2008-04-18 13:53:58 -07002513 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002514
mark gross5e0d2a62008-03-04 15:22:08 -08002515 timer_on = 0;
2516
2517 /* just flush them all */
2518 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002519 struct intel_iommu *iommu = g_iommus[i];
2520 if (!iommu)
2521 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002522
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002523 if (!deferred_flush[i].next)
2524 continue;
2525
2526 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Yu Zhao93a23a72009-05-18 13:51:37 +08002527 DMA_TLB_GLOBAL_FLUSH);
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002528 for (j = 0; j < deferred_flush[i].next; j++) {
Yu Zhao93a23a72009-05-18 13:51:37 +08002529 unsigned long mask;
2530 struct iova *iova = deferred_flush[i].iova[j];
2531
2532 mask = (iova->pfn_hi - iova->pfn_lo + 1) << PAGE_SHIFT;
2533 mask = ilog2(mask >> VTD_PAGE_SHIFT);
2534 iommu_flush_dev_iotlb(deferred_flush[i].domain[j],
2535 iova->pfn_lo << PAGE_SHIFT, mask);
2536 __free_iova(&deferred_flush[i].domain[j]->iovad, iova);
mark gross80b20dd2008-04-18 13:53:58 -07002537 }
Yu Zhao9dd2fe82009-05-18 13:51:36 +08002538 deferred_flush[i].next = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002539 }
2540
mark gross5e0d2a62008-03-04 15:22:08 -08002541 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002542}
2543
2544static void flush_unmaps_timeout(unsigned long data)
2545{
mark gross80b20dd2008-04-18 13:53:58 -07002546 unsigned long flags;
2547
2548 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002549 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002550 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002551}
2552
2553static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2554{
2555 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002556 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002557 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002558
2559 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002560 if (list_size == HIGH_WATER_MARK)
2561 flush_unmaps();
2562
Weidong Han8c11e792008-12-08 15:29:22 +08002563 iommu = domain_get_iommu(dom);
2564 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002565
mark gross80b20dd2008-04-18 13:53:58 -07002566 next = deferred_flush[iommu_id].next;
2567 deferred_flush[iommu_id].domain[next] = dom;
2568 deferred_flush[iommu_id].iova[next] = iova;
2569 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002570
2571 if (!timer_on) {
2572 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2573 timer_on = 1;
2574 }
2575 list_size++;
2576 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2577}
2578
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002579static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2580 size_t size, enum dma_data_direction dir,
2581 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002582{
2583 struct pci_dev *pdev = to_pci_dev(dev);
2584 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002585 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002586 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002587 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002588
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002589 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002590 return;
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002591
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002592 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002593 BUG_ON(!domain);
2594
Weidong Han8c11e792008-12-08 15:29:22 +08002595 iommu = domain_get_iommu(domain);
2596
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002597 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2598 if (!iova)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002599 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002600
David Woodhoused794dc92009-06-28 00:27:49 +01002601 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2602 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002603
David Woodhoused794dc92009-06-28 00:27:49 +01002604 pr_debug("Device %s unmapping: pfn %lx-%lx\n",
2605 pci_name(pdev), start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002606
2607 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002608 dma_pte_clear_range(domain, start_pfn, last_pfn);
2609
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002610 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01002611 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2612
mark gross5e0d2a62008-03-04 15:22:08 -08002613 if (intel_iommu_strict) {
David Woodhouse03d6a242009-06-28 15:33:46 +01002614 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhoused794dc92009-06-28 00:27:49 +01002615 last_pfn - start_pfn + 1);
mark gross5e0d2a62008-03-04 15:22:08 -08002616 /* free iova */
2617 __free_iova(&domain->iovad, iova);
2618 } else {
2619 add_unmap(domain, iova);
2620 /*
2621 * queue up the release of the unmap to save the 1/6th of the
2622 * cpu used up by the iotlb flush operation...
2623 */
mark gross5e0d2a62008-03-04 15:22:08 -08002624 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002625}
2626
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002627static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2628 int dir)
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002629{
2630 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2631}
2632
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002633static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2634 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002635{
2636 void *vaddr;
2637 int order;
2638
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002639 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002640 order = get_order(size);
2641 flags &= ~(GFP_DMA | GFP_DMA32);
2642
2643 vaddr = (void *)__get_free_pages(flags, order);
2644 if (!vaddr)
2645 return NULL;
2646 memset(vaddr, 0, size);
2647
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002648 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2649 DMA_BIDIRECTIONAL,
2650 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002651 if (*dma_handle)
2652 return vaddr;
2653 free_pages((unsigned long)vaddr, order);
2654 return NULL;
2655}
2656
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002657static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2658 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002659{
2660 int order;
2661
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002662 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002663 order = get_order(size);
2664
2665 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2666 free_pages((unsigned long)vaddr, order);
2667}
2668
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002669static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2670 int nelems, enum dma_data_direction dir,
2671 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002672{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002673 struct pci_dev *pdev = to_pci_dev(hwdev);
2674 struct dmar_domain *domain;
David Woodhoused794dc92009-06-28 00:27:49 +01002675 unsigned long start_pfn, last_pfn;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002676 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002677 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002678
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002679 if (iommu_no_mapping(pdev))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002680 return;
2681
2682 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002683 BUG_ON(!domain);
2684
2685 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002686
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002687 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002688 if (!iova)
2689 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002690
David Woodhoused794dc92009-06-28 00:27:49 +01002691 start_pfn = mm_to_dma_pfn(iova->pfn_lo);
2692 last_pfn = mm_to_dma_pfn(iova->pfn_hi + 1) - 1;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002693
2694 /* clear the whole page */
David Woodhoused794dc92009-06-28 00:27:49 +01002695 dma_pte_clear_range(domain, start_pfn, last_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002696
David Woodhoused794dc92009-06-28 00:27:49 +01002697 /* free page tables */
2698 dma_pte_free_pagetable(domain, start_pfn, last_pfn);
2699
David Woodhouse03d6a242009-06-28 15:33:46 +01002700 iommu_flush_iotlb_psi(iommu, domain->id, start_pfn,
David Woodhoused794dc92009-06-28 00:27:49 +01002701 (last_pfn - start_pfn + 1));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002702
2703 /* free iova */
2704 __free_iova(&domain->iovad, iova);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002705}
2706
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002707static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002708 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002709{
2710 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002711 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002712
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002713 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002714 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002715 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002716 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002717 }
2718 return nelems;
2719}
2720
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002721static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2722 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002723{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002724 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002725 struct pci_dev *pdev = to_pci_dev(hwdev);
2726 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002727 size_t size = 0;
2728 int prot = 0;
David Woodhouseb536d242009-06-28 14:49:31 +01002729 size_t offset_pfn = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002730 struct iova *iova = NULL;
2731 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002732 struct scatterlist *sg;
David Woodhouseb536d242009-06-28 14:49:31 +01002733 unsigned long start_vpfn;
Weidong Han8c11e792008-12-08 15:29:22 +08002734 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002735
2736 BUG_ON(dir == DMA_NONE);
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07002737 if (iommu_no_mapping(pdev))
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002738 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002739
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002740 domain = get_valid_domain_for_dev(pdev);
2741 if (!domain)
2742 return 0;
2743
Weidong Han8c11e792008-12-08 15:29:22 +08002744 iommu = domain_get_iommu(domain);
2745
David Woodhouseb536d242009-06-28 14:49:31 +01002746 for_each_sg(sglist, sg, nelems, i)
David Woodhouse88cb6a72009-06-28 15:03:06 +01002747 size += aligned_nrpages(sg->offset, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002748
David Woodhouse88cb6a72009-06-28 15:03:06 +01002749 iova = __intel_alloc_iova(hwdev, domain, size << VTD_PAGE_SHIFT,
2750 pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002751 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002752 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002753 return 0;
2754 }
2755
2756 /*
2757 * Check if DMAR supports zero-length reads on write only
2758 * mappings..
2759 */
2760 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002761 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002762 prot |= DMA_PTE_READ;
2763 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2764 prot |= DMA_PTE_WRITE;
2765
David Woodhouseb536d242009-06-28 14:49:31 +01002766 start_vpfn = mm_to_dma_pfn(iova->pfn_lo);
2767 offset_pfn = 0;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002768 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse88cb6a72009-06-28 15:03:06 +01002769 int nr_pages = aligned_nrpages(sg->offset, sg->length);
David Woodhouseb536d242009-06-28 14:49:31 +01002770 ret = domain_pfn_mapping(domain, start_vpfn + offset_pfn,
2771 page_to_dma_pfn(sg_page(sg)),
2772 nr_pages, prot);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002773 if (ret) {
2774 /* clear the page */
David Woodhouseb536d242009-06-28 14:49:31 +01002775 dma_pte_clear_range(domain, start_vpfn,
2776 start_vpfn + offset_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002777 /* free page tables */
David Woodhouseb536d242009-06-28 14:49:31 +01002778 dma_pte_free_pagetable(domain, start_vpfn,
2779 start_vpfn + offset_pfn);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002780 /* free iova */
2781 __free_iova(&domain->iovad, iova);
2782 return 0;
2783 }
David Woodhouseb536d242009-06-28 14:49:31 +01002784 sg->dma_address = ((dma_addr_t)(start_vpfn + offset_pfn)
2785 << VTD_PAGE_SHIFT) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002786 sg->dma_length = sg->length;
David Woodhouseb536d242009-06-28 14:49:31 +01002787 offset_pfn += nr_pages;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002788 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002789
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002790 /* it's a non-present to present mapping. Only flush if caching mode */
2791 if (cap_caching_mode(iommu->cap))
David Woodhouse03d6a242009-06-28 15:33:46 +01002792 iommu_flush_iotlb_psi(iommu, 0, start_vpfn, offset_pfn);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002793 else
Weidong Han8c11e792008-12-08 15:29:22 +08002794 iommu_flush_write_buffer(iommu);
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002795
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002796 return nelems;
2797}
2798
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002799static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2800{
2801 return !dma_addr;
2802}
2803
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002804struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002805 .alloc_coherent = intel_alloc_coherent,
2806 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002807 .map_sg = intel_map_sg,
2808 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002809 .map_page = intel_map_page,
2810 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002811 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002812};
2813
2814static inline int iommu_domain_cache_init(void)
2815{
2816 int ret = 0;
2817
2818 iommu_domain_cache = kmem_cache_create("iommu_domain",
2819 sizeof(struct dmar_domain),
2820 0,
2821 SLAB_HWCACHE_ALIGN,
2822
2823 NULL);
2824 if (!iommu_domain_cache) {
2825 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2826 ret = -ENOMEM;
2827 }
2828
2829 return ret;
2830}
2831
2832static inline int iommu_devinfo_cache_init(void)
2833{
2834 int ret = 0;
2835
2836 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2837 sizeof(struct device_domain_info),
2838 0,
2839 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002840 NULL);
2841 if (!iommu_devinfo_cache) {
2842 printk(KERN_ERR "Couldn't create devinfo cache\n");
2843 ret = -ENOMEM;
2844 }
2845
2846 return ret;
2847}
2848
2849static inline int iommu_iova_cache_init(void)
2850{
2851 int ret = 0;
2852
2853 iommu_iova_cache = kmem_cache_create("iommu_iova",
2854 sizeof(struct iova),
2855 0,
2856 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002857 NULL);
2858 if (!iommu_iova_cache) {
2859 printk(KERN_ERR "Couldn't create iova cache\n");
2860 ret = -ENOMEM;
2861 }
2862
2863 return ret;
2864}
2865
2866static int __init iommu_init_mempool(void)
2867{
2868 int ret;
2869 ret = iommu_iova_cache_init();
2870 if (ret)
2871 return ret;
2872
2873 ret = iommu_domain_cache_init();
2874 if (ret)
2875 goto domain_error;
2876
2877 ret = iommu_devinfo_cache_init();
2878 if (!ret)
2879 return ret;
2880
2881 kmem_cache_destroy(iommu_domain_cache);
2882domain_error:
2883 kmem_cache_destroy(iommu_iova_cache);
2884
2885 return -ENOMEM;
2886}
2887
2888static void __init iommu_exit_mempool(void)
2889{
2890 kmem_cache_destroy(iommu_devinfo_cache);
2891 kmem_cache_destroy(iommu_domain_cache);
2892 kmem_cache_destroy(iommu_iova_cache);
2893
2894}
2895
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002896static void __init init_no_remapping_devices(void)
2897{
2898 struct dmar_drhd_unit *drhd;
2899
2900 for_each_drhd_unit(drhd) {
2901 if (!drhd->include_all) {
2902 int i;
2903 for (i = 0; i < drhd->devices_cnt; i++)
2904 if (drhd->devices[i] != NULL)
2905 break;
2906 /* ignore DMAR unit if no pci devices exist */
2907 if (i == drhd->devices_cnt)
2908 drhd->ignored = 1;
2909 }
2910 }
2911
2912 if (dmar_map_gfx)
2913 return;
2914
2915 for_each_drhd_unit(drhd) {
2916 int i;
2917 if (drhd->ignored || drhd->include_all)
2918 continue;
2919
2920 for (i = 0; i < drhd->devices_cnt; i++)
2921 if (drhd->devices[i] &&
2922 !IS_GFX_DEVICE(drhd->devices[i]))
2923 break;
2924
2925 if (i < drhd->devices_cnt)
2926 continue;
2927
2928 /* bypass IOMMU if it is just for gfx devices */
2929 drhd->ignored = 1;
2930 for (i = 0; i < drhd->devices_cnt; i++) {
2931 if (!drhd->devices[i])
2932 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002933 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002934 }
2935 }
2936}
2937
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002938#ifdef CONFIG_SUSPEND
2939static int init_iommu_hw(void)
2940{
2941 struct dmar_drhd_unit *drhd;
2942 struct intel_iommu *iommu = NULL;
2943
2944 for_each_active_iommu(iommu, drhd)
2945 if (iommu->qi)
2946 dmar_reenable_qi(iommu);
2947
2948 for_each_active_iommu(iommu, drhd) {
2949 iommu_flush_write_buffer(iommu);
2950
2951 iommu_set_root_entry(iommu);
2952
2953 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002954 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002955 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002956 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002957 iommu_disable_protect_mem_regions(iommu);
2958 iommu_enable_translation(iommu);
2959 }
2960
2961 return 0;
2962}
2963
2964static void iommu_flush_all(void)
2965{
2966 struct dmar_drhd_unit *drhd;
2967 struct intel_iommu *iommu;
2968
2969 for_each_active_iommu(iommu, drhd) {
2970 iommu->flush.flush_context(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002971 DMA_CCMD_GLOBAL_INVL);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002972 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
David Woodhouse1f0ef2a2009-05-10 19:58:49 +01002973 DMA_TLB_GLOBAL_FLUSH);
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002974 }
2975}
2976
2977static int iommu_suspend(struct sys_device *dev, pm_message_t state)
2978{
2979 struct dmar_drhd_unit *drhd;
2980 struct intel_iommu *iommu = NULL;
2981 unsigned long flag;
2982
2983 for_each_active_iommu(iommu, drhd) {
2984 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
2985 GFP_ATOMIC);
2986 if (!iommu->iommu_state)
2987 goto nomem;
2988 }
2989
2990 iommu_flush_all();
2991
2992 for_each_active_iommu(iommu, drhd) {
2993 iommu_disable_translation(iommu);
2994
2995 spin_lock_irqsave(&iommu->register_lock, flag);
2996
2997 iommu->iommu_state[SR_DMAR_FECTL_REG] =
2998 readl(iommu->reg + DMAR_FECTL_REG);
2999 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
3000 readl(iommu->reg + DMAR_FEDATA_REG);
3001 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
3002 readl(iommu->reg + DMAR_FEADDR_REG);
3003 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
3004 readl(iommu->reg + DMAR_FEUADDR_REG);
3005
3006 spin_unlock_irqrestore(&iommu->register_lock, flag);
3007 }
3008 return 0;
3009
3010nomem:
3011 for_each_active_iommu(iommu, drhd)
3012 kfree(iommu->iommu_state);
3013
3014 return -ENOMEM;
3015}
3016
3017static int iommu_resume(struct sys_device *dev)
3018{
3019 struct dmar_drhd_unit *drhd;
3020 struct intel_iommu *iommu = NULL;
3021 unsigned long flag;
3022
3023 if (init_iommu_hw()) {
3024 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
3025 return -EIO;
3026 }
3027
3028 for_each_active_iommu(iommu, drhd) {
3029
3030 spin_lock_irqsave(&iommu->register_lock, flag);
3031
3032 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
3033 iommu->reg + DMAR_FECTL_REG);
3034 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
3035 iommu->reg + DMAR_FEDATA_REG);
3036 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
3037 iommu->reg + DMAR_FEADDR_REG);
3038 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
3039 iommu->reg + DMAR_FEUADDR_REG);
3040
3041 spin_unlock_irqrestore(&iommu->register_lock, flag);
3042 }
3043
3044 for_each_active_iommu(iommu, drhd)
3045 kfree(iommu->iommu_state);
3046
3047 return 0;
3048}
3049
3050static struct sysdev_class iommu_sysclass = {
3051 .name = "iommu",
3052 .resume = iommu_resume,
3053 .suspend = iommu_suspend,
3054};
3055
3056static struct sys_device device_iommu = {
3057 .cls = &iommu_sysclass,
3058};
3059
3060static int __init init_iommu_sysfs(void)
3061{
3062 int error;
3063
3064 error = sysdev_class_register(&iommu_sysclass);
3065 if (error)
3066 return error;
3067
3068 error = sysdev_register(&device_iommu);
3069 if (error)
3070 sysdev_class_unregister(&iommu_sysclass);
3071
3072 return error;
3073}
3074
3075#else
3076static int __init init_iommu_sysfs(void)
3077{
3078 return 0;
3079}
3080#endif /* CONFIG_PM */
3081
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003082int __init intel_iommu_init(void)
3083{
3084 int ret = 0;
3085
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003086 if (dmar_table_init())
3087 return -ENODEV;
3088
Suresh Siddha1886e8a2008-07-10 11:16:37 -07003089 if (dmar_dev_scope_init())
3090 return -ENODEV;
3091
Suresh Siddha2ae21012008-07-10 11:16:43 -07003092 /*
3093 * Check the need for DMA-remapping initialization now.
3094 * Above initialization will also be used by Interrupt-remapping.
3095 */
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003096 if (no_iommu || (swiotlb && !iommu_pass_through) || dmar_disabled)
Suresh Siddha2ae21012008-07-10 11:16:43 -07003097 return -ENODEV;
3098
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003099 iommu_init_mempool();
3100 dmar_init_reserved_ranges();
3101
3102 init_no_remapping_devices();
3103
3104 ret = init_dmars();
3105 if (ret) {
3106 printk(KERN_ERR "IOMMU: dmar init failed\n");
3107 put_iova_domain(&reserved_iova_list);
3108 iommu_exit_mempool();
3109 return ret;
3110 }
3111 printk(KERN_INFO
3112 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
3113
mark gross5e0d2a62008-03-04 15:22:08 -08003114 init_timer(&unmap_timer);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003115 force_iommu = 1;
Fenghua Yu4ed0d3e2009-04-24 17:30:20 -07003116
3117 if (!iommu_pass_through) {
3118 printk(KERN_INFO
3119 "Multi-level page-table translation for DMAR.\n");
3120 dma_ops = &intel_dma_ops;
3121 } else
3122 printk(KERN_INFO
3123 "DMAR: Pass through translation for DMAR.\n");
3124
Fenghua Yuf59c7b62009-03-27 14:22:42 -07003125 init_iommu_sysfs();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003126
3127 register_iommu(&intel_iommu_ops);
3128
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07003129 return 0;
3130}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07003131
Han, Weidong3199aa62009-02-26 17:31:12 +08003132static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
3133 struct pci_dev *pdev)
3134{
3135 struct pci_dev *tmp, *parent;
3136
3137 if (!iommu || !pdev)
3138 return;
3139
3140 /* dependent device detach */
3141 tmp = pci_find_upstream_pcie_bridge(pdev);
3142 /* Secondary interface's bus number and devfn 0 */
3143 if (tmp) {
3144 parent = pdev->bus->self;
3145 while (parent != tmp) {
3146 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01003147 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003148 parent = parent->bus->self;
3149 }
3150 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
3151 iommu_detach_dev(iommu,
3152 tmp->subordinate->number, 0);
3153 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01003154 iommu_detach_dev(iommu, tmp->bus->number,
3155 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003156 }
3157}
3158
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003159static void domain_remove_one_dev_info(struct dmar_domain *domain,
Weidong Hanc7151a82008-12-08 22:51:37 +08003160 struct pci_dev *pdev)
3161{
3162 struct device_domain_info *info;
3163 struct intel_iommu *iommu;
3164 unsigned long flags;
3165 int found = 0;
3166 struct list_head *entry, *tmp;
3167
David Woodhouse276dbf992009-04-04 01:45:37 +01003168 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3169 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003170 if (!iommu)
3171 return;
3172
3173 spin_lock_irqsave(&device_domain_lock, flags);
3174 list_for_each_safe(entry, tmp, &domain->devices) {
3175 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01003176 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08003177 if (info->bus == pdev->bus->number &&
3178 info->devfn == pdev->devfn) {
3179 list_del(&info->link);
3180 list_del(&info->global);
3181 if (info->dev)
3182 info->dev->dev.archdata.iommu = NULL;
3183 spin_unlock_irqrestore(&device_domain_lock, flags);
3184
Yu Zhao93a23a72009-05-18 13:51:37 +08003185 iommu_disable_dev_iotlb(info);
Weidong Hanc7151a82008-12-08 22:51:37 +08003186 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003187 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003188 free_devinfo_mem(info);
3189
3190 spin_lock_irqsave(&device_domain_lock, flags);
3191
3192 if (found)
3193 break;
3194 else
3195 continue;
3196 }
3197
3198 /* if there is no other devices under the same iommu
3199 * owned by this domain, clear this iommu in iommu_bmp
3200 * update iommu count and coherency
3201 */
David Woodhouse276dbf992009-04-04 01:45:37 +01003202 if (iommu == device_to_iommu(info->segment, info->bus,
3203 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08003204 found = 1;
3205 }
3206
3207 if (found == 0) {
3208 unsigned long tmp_flags;
3209 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
3210 clear_bit(iommu->seq_id, &domain->iommu_bmp);
3211 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003212 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003213 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
3214 }
3215
3216 spin_unlock_irqrestore(&device_domain_lock, flags);
3217}
3218
3219static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
3220{
3221 struct device_domain_info *info;
3222 struct intel_iommu *iommu;
3223 unsigned long flags1, flags2;
3224
3225 spin_lock_irqsave(&device_domain_lock, flags1);
3226 while (!list_empty(&domain->devices)) {
3227 info = list_entry(domain->devices.next,
3228 struct device_domain_info, link);
3229 list_del(&info->link);
3230 list_del(&info->global);
3231 if (info->dev)
3232 info->dev->dev.archdata.iommu = NULL;
3233
3234 spin_unlock_irqrestore(&device_domain_lock, flags1);
3235
Yu Zhao93a23a72009-05-18 13:51:37 +08003236 iommu_disable_dev_iotlb(info);
David Woodhouse276dbf992009-04-04 01:45:37 +01003237 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08003238 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08003239 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08003240
3241 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08003242 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08003243 */
3244 spin_lock_irqsave(&domain->iommu_lock, flags2);
3245 if (test_and_clear_bit(iommu->seq_id,
3246 &domain->iommu_bmp)) {
3247 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08003248 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08003249 }
3250 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
3251
3252 free_devinfo_mem(info);
3253 spin_lock_irqsave(&device_domain_lock, flags1);
3254 }
3255 spin_unlock_irqrestore(&device_domain_lock, flags1);
3256}
3257
Weidong Han5e98c4b2008-12-08 23:03:27 +08003258/* domain id for virtual machine, it won't be set in context */
3259static unsigned long vm_domid;
3260
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003261static int vm_domain_min_agaw(struct dmar_domain *domain)
3262{
3263 int i;
3264 int min_agaw = domain->agaw;
3265
3266 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
3267 for (; i < g_num_of_iommus; ) {
3268 if (min_agaw > g_iommus[i]->agaw)
3269 min_agaw = g_iommus[i]->agaw;
3270
3271 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
3272 }
3273
3274 return min_agaw;
3275}
3276
Weidong Han5e98c4b2008-12-08 23:03:27 +08003277static struct dmar_domain *iommu_alloc_vm_domain(void)
3278{
3279 struct dmar_domain *domain;
3280
3281 domain = alloc_domain_mem();
3282 if (!domain)
3283 return NULL;
3284
3285 domain->id = vm_domid++;
3286 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
3287 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
3288
3289 return domain;
3290}
3291
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003292static int md_domain_init(struct dmar_domain *domain, int guest_width)
Weidong Han5e98c4b2008-12-08 23:03:27 +08003293{
3294 int adjust_width;
3295
3296 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3297 spin_lock_init(&domain->mapping_lock);
3298 spin_lock_init(&domain->iommu_lock);
3299
3300 domain_reserve_special_ranges(domain);
3301
3302 /* calculate AGAW */
3303 domain->gaw = guest_width;
3304 adjust_width = guestwidth_to_adjustwidth(guest_width);
3305 domain->agaw = width_to_agaw(adjust_width);
3306
3307 INIT_LIST_HEAD(&domain->devices);
3308
3309 domain->iommu_count = 0;
3310 domain->iommu_coherency = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003311 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003312
3313 /* always allocate the top pgd */
3314 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3315 if (!domain->pgd)
3316 return -ENOMEM;
3317 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3318 return 0;
3319}
3320
3321static void iommu_free_vm_domain(struct dmar_domain *domain)
3322{
3323 unsigned long flags;
3324 struct dmar_drhd_unit *drhd;
3325 struct intel_iommu *iommu;
3326 unsigned long i;
3327 unsigned long ndomains;
3328
3329 for_each_drhd_unit(drhd) {
3330 if (drhd->ignored)
3331 continue;
3332 iommu = drhd->iommu;
3333
3334 ndomains = cap_ndoms(iommu->cap);
3335 i = find_first_bit(iommu->domain_ids, ndomains);
3336 for (; i < ndomains; ) {
3337 if (iommu->domains[i] == domain) {
3338 spin_lock_irqsave(&iommu->lock, flags);
3339 clear_bit(i, iommu->domain_ids);
3340 iommu->domains[i] = NULL;
3341 spin_unlock_irqrestore(&iommu->lock, flags);
3342 break;
3343 }
3344 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3345 }
3346 }
3347}
3348
3349static void vm_domain_exit(struct dmar_domain *domain)
3350{
Weidong Han5e98c4b2008-12-08 23:03:27 +08003351 /* Domain 0 is reserved, so dont process it */
3352 if (!domain)
3353 return;
3354
3355 vm_domain_remove_all_dev_info(domain);
3356 /* destroy iovas */
3357 put_iova_domain(&domain->iovad);
Weidong Han5e98c4b2008-12-08 23:03:27 +08003358
3359 /* clear ptes */
David Woodhouse595badf2009-06-27 22:09:11 +01003360 dma_pte_clear_range(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003361
3362 /* free page tables */
David Woodhoused794dc92009-06-28 00:27:49 +01003363 dma_pte_free_pagetable(domain, 0, DOMAIN_MAX_PFN(domain->gaw));
Weidong Han5e98c4b2008-12-08 23:03:27 +08003364
3365 iommu_free_vm_domain(domain);
3366 free_domain_mem(domain);
3367}
3368
Joerg Roedel5d450802008-12-03 14:52:32 +01003369static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003370{
Joerg Roedel5d450802008-12-03 14:52:32 +01003371 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003372
Joerg Roedel5d450802008-12-03 14:52:32 +01003373 dmar_domain = iommu_alloc_vm_domain();
3374 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003375 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003376 "intel_iommu_domain_init: dmar_domain == NULL\n");
3377 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003378 }
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003379 if (md_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003380 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003381 "intel_iommu_domain_init() failed\n");
3382 vm_domain_exit(dmar_domain);
3383 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003384 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003385 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003386
Joerg Roedel5d450802008-12-03 14:52:32 +01003387 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003388}
Kay, Allen M38717942008-09-09 18:37:29 +03003389
Joerg Roedel5d450802008-12-03 14:52:32 +01003390static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003391{
Joerg Roedel5d450802008-12-03 14:52:32 +01003392 struct dmar_domain *dmar_domain = domain->priv;
3393
3394 domain->priv = NULL;
3395 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003396}
Kay, Allen M38717942008-09-09 18:37:29 +03003397
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003398static int intel_iommu_attach_device(struct iommu_domain *domain,
3399 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003400{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003401 struct dmar_domain *dmar_domain = domain->priv;
3402 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003403 struct intel_iommu *iommu;
3404 int addr_width;
3405 u64 end;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003406 int ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003407
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003408 /* normally pdev is not mapped */
3409 if (unlikely(domain_context_mapped(pdev))) {
3410 struct dmar_domain *old_domain;
3411
3412 old_domain = find_domain(pdev);
3413 if (old_domain) {
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003414 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE ||
3415 dmar_domain->flags & DOMAIN_FLAG_STATIC_IDENTITY)
3416 domain_remove_one_dev_info(old_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003417 else
3418 domain_remove_dev_info(old_domain);
3419 }
3420 }
3421
David Woodhouse276dbf992009-04-04 01:45:37 +01003422 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3423 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003424 if (!iommu)
3425 return -ENODEV;
3426
3427 /* check if this iommu agaw is sufficient for max mapped address */
3428 addr_width = agaw_to_width(iommu->agaw);
3429 end = DOMAIN_MAX_ADDR(addr_width);
3430 end = end & VTD_PAGE_MASK;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003431 if (end < dmar_domain->max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003432 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3433 "sufficient for the mapped address (%llx)\n",
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003434 __func__, iommu->agaw, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003435 return -EFAULT;
3436 }
3437
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003438 ret = domain_add_dev_info(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003439 if (ret)
3440 return ret;
3441
Yu Zhao93a23a72009-05-18 13:51:37 +08003442 ret = domain_context_mapping(dmar_domain, pdev, CONTEXT_TT_MULTI_LEVEL);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003443 return ret;
3444}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003445
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003446static void intel_iommu_detach_device(struct iommu_domain *domain,
3447 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003448{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003449 struct dmar_domain *dmar_domain = domain->priv;
3450 struct pci_dev *pdev = to_pci_dev(dev);
3451
Fenghua Yu2c2e2c32009-06-19 13:47:29 -07003452 domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003453}
Kay, Allen M38717942008-09-09 18:37:29 +03003454
Joerg Roedeldde57a22008-12-03 15:04:09 +01003455static int intel_iommu_map_range(struct iommu_domain *domain,
3456 unsigned long iova, phys_addr_t hpa,
3457 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003458{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003459 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003460 u64 max_addr;
3461 int addr_width;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003462 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003463 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003464
Joerg Roedeldde57a22008-12-03 15:04:09 +01003465 if (iommu_prot & IOMMU_READ)
3466 prot |= DMA_PTE_READ;
3467 if (iommu_prot & IOMMU_WRITE)
3468 prot |= DMA_PTE_WRITE;
Sheng Yang9cf066972009-03-18 15:33:07 +08003469 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3470 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003471
David Woodhouse163cc522009-06-28 00:51:17 +01003472 max_addr = iova + size;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003473 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003474 int min_agaw;
3475 u64 end;
3476
3477 /* check if minimum agaw is sufficient for mapped address */
Joerg Roedeldde57a22008-12-03 15:04:09 +01003478 min_agaw = vm_domain_min_agaw(dmar_domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003479 addr_width = agaw_to_width(min_agaw);
3480 end = DOMAIN_MAX_ADDR(addr_width);
3481 end = end & VTD_PAGE_MASK;
3482 if (end < max_addr) {
3483 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3484 "sufficient for the mapped address (%llx)\n",
3485 __func__, min_agaw, max_addr);
3486 return -EFAULT;
3487 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003488 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003489 }
David Woodhousead051222009-06-28 14:22:28 +01003490 /* Round up size to next multiple of PAGE_SIZE, if it and
3491 the low bits of hpa would take us onto the next page */
David Woodhouse88cb6a72009-06-28 15:03:06 +01003492 size = aligned_nrpages(hpa, size);
David Woodhousead051222009-06-28 14:22:28 +01003493 ret = domain_pfn_mapping(dmar_domain, iova >> VTD_PAGE_SHIFT,
3494 hpa >> VTD_PAGE_SHIFT, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003495 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003496}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003497
Joerg Roedeldde57a22008-12-03 15:04:09 +01003498static void intel_iommu_unmap_range(struct iommu_domain *domain,
3499 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003500{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003501 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003502
David Woodhouse163cc522009-06-28 00:51:17 +01003503 dma_pte_clear_range(dmar_domain, iova >> VTD_PAGE_SHIFT,
3504 (iova + size - 1) >> VTD_PAGE_SHIFT);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003505
David Woodhouse163cc522009-06-28 00:51:17 +01003506 if (dmar_domain->max_addr == iova + size)
3507 dmar_domain->max_addr = iova;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003508}
Kay, Allen M38717942008-09-09 18:37:29 +03003509
Joerg Roedeld14d6572008-12-03 15:06:57 +01003510static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3511 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003512{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003513 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003514 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003515 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003516
David Woodhouseb026fd22009-06-28 10:37:25 +01003517 pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT);
Kay, Allen M38717942008-09-09 18:37:29 +03003518 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003519 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003520
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003521 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003522}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003523
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003524static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3525 unsigned long cap)
3526{
3527 struct dmar_domain *dmar_domain = domain->priv;
3528
3529 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3530 return dmar_domain->iommu_snooping;
3531
3532 return 0;
3533}
3534
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003535static struct iommu_ops intel_iommu_ops = {
3536 .domain_init = intel_iommu_domain_init,
3537 .domain_destroy = intel_iommu_domain_destroy,
3538 .attach_dev = intel_iommu_attach_device,
3539 .detach_dev = intel_iommu_detach_device,
3540 .map = intel_iommu_map_range,
3541 .unmap = intel_iommu_unmap_range,
3542 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003543 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003544};
David Woodhouse9af88142009-02-13 23:18:03 +00003545
3546static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3547{
3548 /*
3549 * Mobile 4 Series Chipset neglects to set RWBF capability,
3550 * but needs it:
3551 */
3552 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3553 rwbf_quirk = 1;
3554}
3555
3556DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);