blob: fb3a3f3fca7a4835aab7fef7bb45edd84a2df182 [file] [log] [blame]
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001/*
2 * Copyright (c) 2006, Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
16 *
mark gross98bcef52008-02-23 15:23:35 -080017 * Copyright (C) 2006-2008 Intel Corporation
18 * Author: Ashok Raj <ashok.raj@intel.com>
19 * Author: Shaohua Li <shaohua.li@intel.com>
20 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Fenghua Yu5b6985c2008-10-16 18:02:32 -070021 * Author: Fenghua Yu <fenghua.yu@intel.com>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070022 */
23
24#include <linux/init.h>
25#include <linux/bitmap.h>
mark gross5e0d2a62008-03-04 15:22:08 -080026#include <linux/debugfs.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070027#include <linux/slab.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070030#include <linux/spinlock.h>
31#include <linux/pci.h>
32#include <linux/dmar.h>
33#include <linux/dma-mapping.h>
34#include <linux/mempool.h>
mark gross5e0d2a62008-03-04 15:22:08 -080035#include <linux/timer.h>
Kay, Allen M38717942008-09-09 18:37:29 +030036#include <linux/iova.h>
Joerg Roedel5d450802008-12-03 14:52:32 +010037#include <linux/iommu.h>
Kay, Allen M38717942008-09-09 18:37:29 +030038#include <linux/intel-iommu.h>
Fenghua Yuf59c7b62009-03-27 14:22:42 -070039#include <linux/sysdev.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070040#include <asm/cacheflush.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090041#include <asm/iommu.h>
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070042#include "pci.h"
43
Fenghua Yu5b6985c2008-10-16 18:02:32 -070044#define ROOT_SIZE VTD_PAGE_SIZE
45#define CONTEXT_SIZE VTD_PAGE_SIZE
46
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070047#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
48#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
49
50#define IOAPIC_RANGE_START (0xfee00000)
51#define IOAPIC_RANGE_END (0xfeefffff)
52#define IOVA_START_ADDR (0x1000)
53
54#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
55
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -070056#define DOMAIN_MAX_ADDR(gaw) ((((u64)1) << gaw) - 1)
57
Mark McLoughlinf27be032008-11-20 15:49:43 +000058#define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT)
Yang Hongyang284901a2009-04-06 19:01:15 -070059#define DMA_32BIT_PFN IOVA_PFN(DMA_BIT_MASK(32))
Yang Hongyang6a355282009-04-06 19:01:13 -070060#define DMA_64BIT_PFN IOVA_PFN(DMA_BIT_MASK(64))
mark gross5e0d2a62008-03-04 15:22:08 -080061
Weidong Hand9630fe2008-12-08 11:06:32 +080062/* global iommu list, set NULL for ignored DMAR units */
63static struct intel_iommu **g_iommus;
64
David Woodhouse9af88142009-02-13 23:18:03 +000065static int rwbf_quirk;
66
Mark McLoughlin46b08e12008-11-20 15:49:44 +000067/*
68 * 0: Present
69 * 1-11: Reserved
70 * 12-63: Context Ptr (12 - (haw-1))
71 * 64-127: Reserved
72 */
73struct root_entry {
74 u64 val;
75 u64 rsvd1;
76};
77#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
78static inline bool root_present(struct root_entry *root)
79{
80 return (root->val & 1);
81}
82static inline void set_root_present(struct root_entry *root)
83{
84 root->val |= 1;
85}
86static inline void set_root_value(struct root_entry *root, unsigned long value)
87{
88 root->val |= value & VTD_PAGE_MASK;
89}
90
91static inline struct context_entry *
92get_context_addr_from_root(struct root_entry *root)
93{
94 return (struct context_entry *)
95 (root_present(root)?phys_to_virt(
96 root->val & VTD_PAGE_MASK) :
97 NULL);
98}
99
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000100/*
101 * low 64 bits:
102 * 0: present
103 * 1: fault processing disable
104 * 2-3: translation type
105 * 12-63: address space root
106 * high 64 bits:
107 * 0-2: address width
108 * 3-6: aval
109 * 8-23: domain id
110 */
111struct context_entry {
112 u64 lo;
113 u64 hi;
114};
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000115
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000116static inline bool context_present(struct context_entry *context)
117{
118 return (context->lo & 1);
119}
120static inline void context_set_present(struct context_entry *context)
121{
122 context->lo |= 1;
123}
124
125static inline void context_set_fault_enable(struct context_entry *context)
126{
127 context->lo &= (((u64)-1) << 2) | 1;
128}
129
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000130#define CONTEXT_TT_MULTI_LEVEL 0
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000131
132static inline void context_set_translation_type(struct context_entry *context,
133 unsigned long value)
134{
135 context->lo &= (((u64)-1) << 4) | 3;
136 context->lo |= (value & 3) << 2;
137}
138
139static inline void context_set_address_root(struct context_entry *context,
140 unsigned long value)
141{
142 context->lo |= value & VTD_PAGE_MASK;
143}
144
145static inline void context_set_address_width(struct context_entry *context,
146 unsigned long value)
147{
148 context->hi |= value & 7;
149}
150
151static inline void context_set_domain_id(struct context_entry *context,
152 unsigned long value)
153{
154 context->hi |= (value & ((1 << 16) - 1)) << 8;
155}
156
157static inline void context_clear_entry(struct context_entry *context)
158{
159 context->lo = 0;
160 context->hi = 0;
161}
Mark McLoughlin7a8fc252008-11-20 15:49:45 +0000162
Mark McLoughlin622ba122008-11-20 15:49:46 +0000163/*
164 * 0: readable
165 * 1: writable
166 * 2-6: reserved
167 * 7: super page
Sheng Yang9cf06692009-03-18 15:33:07 +0800168 * 8-10: available
169 * 11: snoop behavior
Mark McLoughlin622ba122008-11-20 15:49:46 +0000170 * 12-63: Host physcial address
171 */
172struct dma_pte {
173 u64 val;
174};
Mark McLoughlin622ba122008-11-20 15:49:46 +0000175
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000176static inline void dma_clear_pte(struct dma_pte *pte)
177{
178 pte->val = 0;
179}
180
181static inline void dma_set_pte_readable(struct dma_pte *pte)
182{
183 pte->val |= DMA_PTE_READ;
184}
185
186static inline void dma_set_pte_writable(struct dma_pte *pte)
187{
188 pte->val |= DMA_PTE_WRITE;
189}
190
Sheng Yang9cf06692009-03-18 15:33:07 +0800191static inline void dma_set_pte_snp(struct dma_pte *pte)
192{
193 pte->val |= DMA_PTE_SNP;
194}
195
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000196static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
197{
198 pte->val = (pte->val & ~3) | (prot & 3);
199}
200
201static inline u64 dma_pte_addr(struct dma_pte *pte)
202{
203 return (pte->val & VTD_PAGE_MASK);
204}
205
206static inline void dma_set_pte_addr(struct dma_pte *pte, u64 addr)
207{
208 pte->val |= (addr & VTD_PAGE_MASK);
209}
210
211static inline bool dma_pte_present(struct dma_pte *pte)
212{
213 return (pte->val & 3) != 0;
214}
Mark McLoughlin622ba122008-11-20 15:49:46 +0000215
Weidong Han3b5410e2008-12-08 09:17:15 +0800216/* devices under the same p2p bridge are owned in one domain */
Mike Daycdc7b832008-12-12 17:16:30 +0100217#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han3b5410e2008-12-08 09:17:15 +0800218
Weidong Han1ce28fe2008-12-08 16:35:39 +0800219/* domain represents a virtual machine, more than one devices
220 * across iommus may be owned in one domain, e.g. kvm guest.
221 */
222#define DOMAIN_FLAG_VIRTUAL_MACHINE (1 << 1)
223
Mark McLoughlin99126f72008-11-20 15:49:47 +0000224struct dmar_domain {
225 int id; /* domain id */
Weidong Han8c11e792008-12-08 15:29:22 +0800226 unsigned long iommu_bmp; /* bitmap of iommus this domain uses*/
Mark McLoughlin99126f72008-11-20 15:49:47 +0000227
228 struct list_head devices; /* all devices' list */
229 struct iova_domain iovad; /* iova's that belong to this domain */
230
231 struct dma_pte *pgd; /* virtual address */
232 spinlock_t mapping_lock; /* page table lock */
233 int gaw; /* max guest address width */
234
235 /* adjusted guest address width, 0 is level 2 30-bit */
236 int agaw;
237
Weidong Han3b5410e2008-12-08 09:17:15 +0800238 int flags; /* flags to find out type of domain */
Weidong Han8e6040972008-12-08 15:49:06 +0800239
240 int iommu_coherency;/* indicate coherency of iommu access */
Sheng Yang58c610b2009-03-18 15:33:05 +0800241 int iommu_snooping; /* indicate snooping control feature*/
Weidong Hanc7151a82008-12-08 22:51:37 +0800242 int iommu_count; /* reference count of iommu */
243 spinlock_t iommu_lock; /* protect iommu set in domain */
Weidong Hanfe40f1e2008-12-08 23:10:23 +0800244 u64 max_addr; /* maximum mapped address */
Mark McLoughlin99126f72008-11-20 15:49:47 +0000245};
246
Mark McLoughlina647dac2008-11-20 15:49:48 +0000247/* PCI domain-device relationship */
248struct device_domain_info {
249 struct list_head link; /* link to domain siblings */
250 struct list_head global; /* link to global list */
David Woodhouse276dbf992009-04-04 01:45:37 +0100251 int segment; /* PCI domain */
252 u8 bus; /* PCI bus number */
Mark McLoughlina647dac2008-11-20 15:49:48 +0000253 u8 devfn; /* PCI devfn number */
254 struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
255 struct dmar_domain *domain; /* pointer to domain */
256};
257
mark gross5e0d2a62008-03-04 15:22:08 -0800258static void flush_unmaps_timeout(unsigned long data);
259
260DEFINE_TIMER(unmap_timer, flush_unmaps_timeout, 0, 0);
261
mark gross80b20dd2008-04-18 13:53:58 -0700262#define HIGH_WATER_MARK 250
263struct deferred_flush_tables {
264 int next;
265 struct iova *iova[HIGH_WATER_MARK];
266 struct dmar_domain *domain[HIGH_WATER_MARK];
267};
268
269static struct deferred_flush_tables *deferred_flush;
270
mark gross5e0d2a62008-03-04 15:22:08 -0800271/* bitmap for indexing intel_iommus */
mark gross5e0d2a62008-03-04 15:22:08 -0800272static int g_num_of_iommus;
273
274static DEFINE_SPINLOCK(async_umap_flush_lock);
275static LIST_HEAD(unmaps_to_do);
276
277static int timer_on;
278static long list_size;
mark gross5e0d2a62008-03-04 15:22:08 -0800279
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700280static void domain_remove_dev_info(struct dmar_domain *domain);
281
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800282#ifdef CONFIG_DMAR_DEFAULT_ON
283int dmar_disabled = 0;
284#else
285int dmar_disabled = 1;
286#endif /*CONFIG_DMAR_DEFAULT_ON*/
287
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700288static int __initdata dmar_map_gfx = 1;
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700289static int dmar_forcedac;
mark gross5e0d2a62008-03-04 15:22:08 -0800290static int intel_iommu_strict;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700291
292#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
293static DEFINE_SPINLOCK(device_domain_lock);
294static LIST_HEAD(device_domain_list);
295
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +0100296static struct iommu_ops intel_iommu_ops;
297
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700298static int __init intel_iommu_setup(char *str)
299{
300 if (!str)
301 return -EINVAL;
302 while (*str) {
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800303 if (!strncmp(str, "on", 2)) {
304 dmar_disabled = 0;
305 printk(KERN_INFO "Intel-IOMMU: enabled\n");
306 } else if (!strncmp(str, "off", 3)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700307 dmar_disabled = 1;
Kyle McMartin0cd5c3c2009-02-04 14:29:19 -0800308 printk(KERN_INFO "Intel-IOMMU: disabled\n");
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700309 } else if (!strncmp(str, "igfx_off", 8)) {
310 dmar_map_gfx = 0;
311 printk(KERN_INFO
312 "Intel-IOMMU: disable GFX device mapping\n");
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700313 } else if (!strncmp(str, "forcedac", 8)) {
mark gross5e0d2a62008-03-04 15:22:08 -0800314 printk(KERN_INFO
Keshavamurthy, Anil S7d3b03c2007-10-21 16:41:53 -0700315 "Intel-IOMMU: Forcing DAC for PCI devices\n");
316 dmar_forcedac = 1;
mark gross5e0d2a62008-03-04 15:22:08 -0800317 } else if (!strncmp(str, "strict", 6)) {
318 printk(KERN_INFO
319 "Intel-IOMMU: disable batched IOTLB flush\n");
320 intel_iommu_strict = 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700321 }
322
323 str += strcspn(str, ",");
324 while (*str == ',')
325 str++;
326 }
327 return 0;
328}
329__setup("intel_iommu=", intel_iommu_setup);
330
331static struct kmem_cache *iommu_domain_cache;
332static struct kmem_cache *iommu_devinfo_cache;
333static struct kmem_cache *iommu_iova_cache;
334
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700335static inline void *iommu_kmem_cache_alloc(struct kmem_cache *cachep)
336{
337 unsigned int flags;
338 void *vaddr;
339
340 /* trying to avoid low memory issues */
341 flags = current->flags & PF_MEMALLOC;
342 current->flags |= PF_MEMALLOC;
343 vaddr = kmem_cache_alloc(cachep, GFP_ATOMIC);
344 current->flags &= (~PF_MEMALLOC | flags);
345 return vaddr;
346}
347
348
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700349static inline void *alloc_pgtable_page(void)
350{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700351 unsigned int flags;
352 void *vaddr;
353
354 /* trying to avoid low memory issues */
355 flags = current->flags & PF_MEMALLOC;
356 current->flags |= PF_MEMALLOC;
357 vaddr = (void *)get_zeroed_page(GFP_ATOMIC);
358 current->flags &= (~PF_MEMALLOC | flags);
359 return vaddr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700360}
361
362static inline void free_pgtable_page(void *vaddr)
363{
364 free_page((unsigned long)vaddr);
365}
366
367static inline void *alloc_domain_mem(void)
368{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700369 return iommu_kmem_cache_alloc(iommu_domain_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700370}
371
Kay, Allen M38717942008-09-09 18:37:29 +0300372static void free_domain_mem(void *vaddr)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700373{
374 kmem_cache_free(iommu_domain_cache, vaddr);
375}
376
377static inline void * alloc_devinfo_mem(void)
378{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700379 return iommu_kmem_cache_alloc(iommu_devinfo_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700380}
381
382static inline void free_devinfo_mem(void *vaddr)
383{
384 kmem_cache_free(iommu_devinfo_cache, vaddr);
385}
386
387struct iova *alloc_iova_mem(void)
388{
Keshavamurthy, Anil Seb3fa7c2007-10-21 16:41:52 -0700389 return iommu_kmem_cache_alloc(iommu_iova_cache);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700390}
391
392void free_iova_mem(struct iova *iova)
393{
394 kmem_cache_free(iommu_iova_cache, iova);
395}
396
Weidong Han1b573682008-12-08 15:34:06 +0800397
398static inline int width_to_agaw(int width);
399
400/* calculate agaw for each iommu.
401 * "SAGAW" may be different across iommus, use a default agaw, and
402 * get a supported less agaw for iommus that don't support the default agaw.
403 */
404int iommu_calculate_agaw(struct intel_iommu *iommu)
405{
406 unsigned long sagaw;
407 int agaw = -1;
408
409 sagaw = cap_sagaw(iommu->cap);
410 for (agaw = width_to_agaw(DEFAULT_DOMAIN_ADDRESS_WIDTH);
411 agaw >= 0; agaw--) {
412 if (test_bit(agaw, &sagaw))
413 break;
414 }
415
416 return agaw;
417}
418
Weidong Han8c11e792008-12-08 15:29:22 +0800419/* in native case, each domain is related to only one iommu */
420static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
421{
422 int iommu_id;
423
Weidong Han1ce28fe2008-12-08 16:35:39 +0800424 BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
425
Weidong Han8c11e792008-12-08 15:29:22 +0800426 iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
427 if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
428 return NULL;
429
430 return g_iommus[iommu_id];
431}
432
Weidong Han8e6040972008-12-08 15:49:06 +0800433static void domain_update_iommu_coherency(struct dmar_domain *domain)
434{
435 int i;
436
437 domain->iommu_coherency = 1;
438
439 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
440 for (; i < g_num_of_iommus; ) {
441 if (!ecap_coherent(g_iommus[i]->ecap)) {
442 domain->iommu_coherency = 0;
443 break;
444 }
445 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
446 }
447}
448
Sheng Yang58c610b2009-03-18 15:33:05 +0800449static void domain_update_iommu_snooping(struct dmar_domain *domain)
450{
451 int i;
452
453 domain->iommu_snooping = 1;
454
455 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
456 for (; i < g_num_of_iommus; ) {
457 if (!ecap_sc_support(g_iommus[i]->ecap)) {
458 domain->iommu_snooping = 0;
459 break;
460 }
461 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
462 }
463}
464
465/* Some capabilities may be different across iommus */
466static void domain_update_iommu_cap(struct dmar_domain *domain)
467{
468 domain_update_iommu_coherency(domain);
469 domain_update_iommu_snooping(domain);
470}
471
David Woodhouse276dbf992009-04-04 01:45:37 +0100472static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
Weidong Hanc7151a82008-12-08 22:51:37 +0800473{
474 struct dmar_drhd_unit *drhd = NULL;
475 int i;
476
477 for_each_drhd_unit(drhd) {
478 if (drhd->ignored)
479 continue;
David Woodhouse276dbf992009-04-04 01:45:37 +0100480 if (segment != drhd->segment)
481 continue;
Weidong Hanc7151a82008-12-08 22:51:37 +0800482
David Woodhouse924b6232009-04-04 00:39:25 +0100483 for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel288e4872009-01-11 15:33:51 +0000484 if (drhd->devices[i] &&
485 drhd->devices[i]->bus->number == bus &&
Weidong Hanc7151a82008-12-08 22:51:37 +0800486 drhd->devices[i]->devfn == devfn)
487 return drhd->iommu;
David Woodhouse4958c5d2009-04-06 13:30:01 -0700488 if (drhd->devices[i] &&
489 drhd->devices[i]->subordinate &&
David Woodhouse924b6232009-04-04 00:39:25 +0100490 drhd->devices[i]->subordinate->number <= bus &&
491 drhd->devices[i]->subordinate->subordinate >= bus)
492 return drhd->iommu;
493 }
Weidong Hanc7151a82008-12-08 22:51:37 +0800494
495 if (drhd->include_all)
496 return drhd->iommu;
497 }
498
499 return NULL;
500}
501
Weidong Han5331fe62008-12-08 23:00:00 +0800502static void domain_flush_cache(struct dmar_domain *domain,
503 void *addr, int size)
504{
505 if (!domain->iommu_coherency)
506 clflush_cache_range(addr, size);
507}
508
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700509/* Gets context entry for a given bus and devfn */
510static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
511 u8 bus, u8 devfn)
512{
513 struct root_entry *root;
514 struct context_entry *context;
515 unsigned long phy_addr;
516 unsigned long flags;
517
518 spin_lock_irqsave(&iommu->lock, flags);
519 root = &iommu->root_entry[bus];
520 context = get_context_addr_from_root(root);
521 if (!context) {
522 context = (struct context_entry *)alloc_pgtable_page();
523 if (!context) {
524 spin_unlock_irqrestore(&iommu->lock, flags);
525 return NULL;
526 }
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700527 __iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700528 phy_addr = virt_to_phys((void *)context);
529 set_root_value(root, phy_addr);
530 set_root_present(root);
531 __iommu_flush_cache(iommu, root, sizeof(*root));
532 }
533 spin_unlock_irqrestore(&iommu->lock, flags);
534 return &context[devfn];
535}
536
537static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
538{
539 struct root_entry *root;
540 struct context_entry *context;
541 int ret;
542 unsigned long flags;
543
544 spin_lock_irqsave(&iommu->lock, flags);
545 root = &iommu->root_entry[bus];
546 context = get_context_addr_from_root(root);
547 if (!context) {
548 ret = 0;
549 goto out;
550 }
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000551 ret = context_present(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700552out:
553 spin_unlock_irqrestore(&iommu->lock, flags);
554 return ret;
555}
556
557static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
558{
559 struct root_entry *root;
560 struct context_entry *context;
561 unsigned long flags;
562
563 spin_lock_irqsave(&iommu->lock, flags);
564 root = &iommu->root_entry[bus];
565 context = get_context_addr_from_root(root);
566 if (context) {
Mark McLoughlinc07e7d22008-11-21 16:54:46 +0000567 context_clear_entry(&context[devfn]);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700568 __iommu_flush_cache(iommu, &context[devfn], \
569 sizeof(*context));
570 }
571 spin_unlock_irqrestore(&iommu->lock, flags);
572}
573
574static void free_context_table(struct intel_iommu *iommu)
575{
576 struct root_entry *root;
577 int i;
578 unsigned long flags;
579 struct context_entry *context;
580
581 spin_lock_irqsave(&iommu->lock, flags);
582 if (!iommu->root_entry) {
583 goto out;
584 }
585 for (i = 0; i < ROOT_ENTRY_NR; i++) {
586 root = &iommu->root_entry[i];
587 context = get_context_addr_from_root(root);
588 if (context)
589 free_pgtable_page(context);
590 }
591 free_pgtable_page(iommu->root_entry);
592 iommu->root_entry = NULL;
593out:
594 spin_unlock_irqrestore(&iommu->lock, flags);
595}
596
597/* page table handling */
598#define LEVEL_STRIDE (9)
599#define LEVEL_MASK (((u64)1 << LEVEL_STRIDE) - 1)
600
601static inline int agaw_to_level(int agaw)
602{
603 return agaw + 2;
604}
605
606static inline int agaw_to_width(int agaw)
607{
608 return 30 + agaw * LEVEL_STRIDE;
609
610}
611
612static inline int width_to_agaw(int width)
613{
614 return (width - 30) / LEVEL_STRIDE;
615}
616
617static inline unsigned int level_to_offset_bits(int level)
618{
619 return (12 + (level - 1) * LEVEL_STRIDE);
620}
621
622static inline int address_level_offset(u64 addr, int level)
623{
624 return ((addr >> level_to_offset_bits(level)) & LEVEL_MASK);
625}
626
627static inline u64 level_mask(int level)
628{
629 return ((u64)-1 << level_to_offset_bits(level));
630}
631
632static inline u64 level_size(int level)
633{
634 return ((u64)1 << level_to_offset_bits(level));
635}
636
637static inline u64 align_to_level(u64 addr, int level)
638{
639 return ((addr + level_size(level) - 1) & level_mask(level));
640}
641
642static struct dma_pte * addr_to_dma_pte(struct dmar_domain *domain, u64 addr)
643{
644 int addr_width = agaw_to_width(domain->agaw);
645 struct dma_pte *parent, *pte = NULL;
646 int level = agaw_to_level(domain->agaw);
647 int offset;
648 unsigned long flags;
649
650 BUG_ON(!domain->pgd);
651
652 addr &= (((u64)1) << addr_width) - 1;
653 parent = domain->pgd;
654
655 spin_lock_irqsave(&domain->mapping_lock, flags);
656 while (level > 0) {
657 void *tmp_page;
658
659 offset = address_level_offset(addr, level);
660 pte = &parent[offset];
661 if (level == 1)
662 break;
663
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000664 if (!dma_pte_present(pte)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700665 tmp_page = alloc_pgtable_page();
666
667 if (!tmp_page) {
668 spin_unlock_irqrestore(&domain->mapping_lock,
669 flags);
670 return NULL;
671 }
Weidong Han5331fe62008-12-08 23:00:00 +0800672 domain_flush_cache(domain, tmp_page, PAGE_SIZE);
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000673 dma_set_pte_addr(pte, virt_to_phys(tmp_page));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700674 /*
675 * high level table always sets r/w, last level page
676 * table control read/write
677 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000678 dma_set_pte_readable(pte);
679 dma_set_pte_writable(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800680 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700681 }
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000682 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700683 level--;
684 }
685
686 spin_unlock_irqrestore(&domain->mapping_lock, flags);
687 return pte;
688}
689
690/* return address's pte at specific level */
691static struct dma_pte *dma_addr_level_pte(struct dmar_domain *domain, u64 addr,
692 int level)
693{
694 struct dma_pte *parent, *pte = NULL;
695 int total = agaw_to_level(domain->agaw);
696 int offset;
697
698 parent = domain->pgd;
699 while (level <= total) {
700 offset = address_level_offset(addr, total);
701 pte = &parent[offset];
702 if (level == total)
703 return pte;
704
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000705 if (!dma_pte_present(pte))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700706 break;
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000707 parent = phys_to_virt(dma_pte_addr(pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700708 total--;
709 }
710 return NULL;
711}
712
713/* clear one page's page table */
714static void dma_pte_clear_one(struct dmar_domain *domain, u64 addr)
715{
716 struct dma_pte *pte = NULL;
717
718 /* get last level pte */
719 pte = dma_addr_level_pte(domain, addr, 1);
720
721 if (pte) {
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000722 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800723 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700724 }
725}
726
727/* clear last level pte, a tlb flush should be followed */
728static void dma_pte_clear_range(struct dmar_domain *domain, u64 start, u64 end)
729{
730 int addr_width = agaw_to_width(domain->agaw);
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800731 int npages;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700732
733 start &= (((u64)1) << addr_width) - 1;
734 end &= (((u64)1) << addr_width) - 1;
735 /* in case it's partial page */
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700736 start = PAGE_ALIGN(start);
737 end &= PAGE_MASK;
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800738 npages = (end - start) / VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700739
740 /* we don't need lock here, nobody else touches the iova range */
Zhao, Yuafeeb7c2009-02-13 17:55:49 +0800741 while (npages--) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700742 dma_pte_clear_one(domain, start);
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700743 start += VTD_PAGE_SIZE;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700744 }
745}
746
747/* free page table pages. last level pte should already be cleared */
748static void dma_pte_free_pagetable(struct dmar_domain *domain,
749 u64 start, u64 end)
750{
751 int addr_width = agaw_to_width(domain->agaw);
752 struct dma_pte *pte;
753 int total = agaw_to_level(domain->agaw);
754 int level;
755 u64 tmp;
756
757 start &= (((u64)1) << addr_width) - 1;
758 end &= (((u64)1) << addr_width) - 1;
759
760 /* we don't need lock here, nobody else touches the iova range */
761 level = 2;
762 while (level <= total) {
763 tmp = align_to_level(start, level);
764 if (tmp >= end || (tmp + level_size(level) > end))
765 return;
766
767 while (tmp < end) {
768 pte = dma_addr_level_pte(domain, tmp, level);
769 if (pte) {
770 free_pgtable_page(
Mark McLoughlin19c239c2008-11-21 16:56:53 +0000771 phys_to_virt(dma_pte_addr(pte)));
772 dma_clear_pte(pte);
Weidong Han5331fe62008-12-08 23:00:00 +0800773 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700774 }
775 tmp += level_size(level);
776 }
777 level++;
778 }
779 /* free pgd */
780 if (start == 0 && end >= ((((u64)1) << addr_width) - 1)) {
781 free_pgtable_page(domain->pgd);
782 domain->pgd = NULL;
783 }
784}
785
786/* iommu handling */
787static int iommu_alloc_root_entry(struct intel_iommu *iommu)
788{
789 struct root_entry *root;
790 unsigned long flags;
791
792 root = (struct root_entry *)alloc_pgtable_page();
793 if (!root)
794 return -ENOMEM;
795
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700796 __iommu_flush_cache(iommu, root, ROOT_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700797
798 spin_lock_irqsave(&iommu->lock, flags);
799 iommu->root_entry = root;
800 spin_unlock_irqrestore(&iommu->lock, flags);
801
802 return 0;
803}
804
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700805static void iommu_set_root_entry(struct intel_iommu *iommu)
806{
807 void *addr;
808 u32 cmd, sts;
809 unsigned long flag;
810
811 addr = iommu->root_entry;
812
813 spin_lock_irqsave(&iommu->register_lock, flag);
814 dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));
815
816 cmd = iommu->gcmd | DMA_GCMD_SRTP;
817 writel(cmd, iommu->reg + DMAR_GCMD_REG);
818
819 /* Make sure hardware complete it */
820 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
821 readl, (sts & DMA_GSTS_RTPS), sts);
822
823 spin_unlock_irqrestore(&iommu->register_lock, flag);
824}
825
826static void iommu_flush_write_buffer(struct intel_iommu *iommu)
827{
828 u32 val;
829 unsigned long flag;
830
David Woodhouse9af88142009-02-13 23:18:03 +0000831 if (!rwbf_quirk && !cap_rwbf(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700832 return;
833 val = iommu->gcmd | DMA_GCMD_WBF;
834
835 spin_lock_irqsave(&iommu->register_lock, flag);
836 writel(val, iommu->reg + DMAR_GCMD_REG);
837
838 /* Make sure hardware complete it */
839 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
840 readl, (!(val & DMA_GSTS_WBFS)), val);
841
842 spin_unlock_irqrestore(&iommu->register_lock, flag);
843}
844
845/* return value determine if we need a write buffer flush */
846static int __iommu_flush_context(struct intel_iommu *iommu,
847 u16 did, u16 source_id, u8 function_mask, u64 type,
848 int non_present_entry_flush)
849{
850 u64 val = 0;
851 unsigned long flag;
852
853 /*
854 * In the non-present entry flush case, if hardware doesn't cache
855 * non-present entry we do nothing and if hardware cache non-present
856 * entry, we flush entries of domain 0 (the domain id is used to cache
857 * any non-present entries)
858 */
859 if (non_present_entry_flush) {
860 if (!cap_caching_mode(iommu->cap))
861 return 1;
862 else
863 did = 0;
864 }
865
866 switch (type) {
867 case DMA_CCMD_GLOBAL_INVL:
868 val = DMA_CCMD_GLOBAL_INVL;
869 break;
870 case DMA_CCMD_DOMAIN_INVL:
871 val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
872 break;
873 case DMA_CCMD_DEVICE_INVL:
874 val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
875 | DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
876 break;
877 default:
878 BUG();
879 }
880 val |= DMA_CCMD_ICC;
881
882 spin_lock_irqsave(&iommu->register_lock, flag);
883 dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);
884
885 /* Make sure hardware complete it */
886 IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
887 dmar_readq, (!(val & DMA_CCMD_ICC)), val);
888
889 spin_unlock_irqrestore(&iommu->register_lock, flag);
890
Ameya Palande4d235ba2008-10-18 20:27:30 -0700891 /* flush context entry will implicitly flush write buffer */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700892 return 0;
893}
894
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700895/* return value determine if we need a write buffer flush */
896static int __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
897 u64 addr, unsigned int size_order, u64 type,
898 int non_present_entry_flush)
899{
900 int tlb_offset = ecap_iotlb_offset(iommu->ecap);
901 u64 val = 0, val_iva = 0;
902 unsigned long flag;
903
904 /*
905 * In the non-present entry flush case, if hardware doesn't cache
906 * non-present entry we do nothing and if hardware cache non-present
907 * entry, we flush entries of domain 0 (the domain id is used to cache
908 * any non-present entries)
909 */
910 if (non_present_entry_flush) {
911 if (!cap_caching_mode(iommu->cap))
912 return 1;
913 else
914 did = 0;
915 }
916
917 switch (type) {
918 case DMA_TLB_GLOBAL_FLUSH:
919 /* global flush doesn't need set IVA_REG */
920 val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
921 break;
922 case DMA_TLB_DSI_FLUSH:
923 val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
924 break;
925 case DMA_TLB_PSI_FLUSH:
926 val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
927 /* Note: always flush non-leaf currently */
928 val_iva = size_order | addr;
929 break;
930 default:
931 BUG();
932 }
933 /* Note: set drain read/write */
934#if 0
935 /*
936 * This is probably to be super secure.. Looks like we can
937 * ignore it without any impact.
938 */
939 if (cap_read_drain(iommu->cap))
940 val |= DMA_TLB_READ_DRAIN;
941#endif
942 if (cap_write_drain(iommu->cap))
943 val |= DMA_TLB_WRITE_DRAIN;
944
945 spin_lock_irqsave(&iommu->register_lock, flag);
946 /* Note: Only uses first TLB reg currently */
947 if (val_iva)
948 dmar_writeq(iommu->reg + tlb_offset, val_iva);
949 dmar_writeq(iommu->reg + tlb_offset + 8, val);
950
951 /* Make sure hardware complete it */
952 IOMMU_WAIT_OP(iommu, tlb_offset + 8,
953 dmar_readq, (!(val & DMA_TLB_IVT)), val);
954
955 spin_unlock_irqrestore(&iommu->register_lock, flag);
956
957 /* check IOTLB invalidation granularity */
958 if (DMA_TLB_IAIG(val) == 0)
959 printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
960 if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
961 pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700962 (unsigned long long)DMA_TLB_IIRG(type),
963 (unsigned long long)DMA_TLB_IAIG(val));
Ameya Palande4d235ba2008-10-18 20:27:30 -0700964 /* flush iotlb entry will implicitly flush write buffer */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700965 return 0;
966}
967
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700968static int iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
969 u64 addr, unsigned int pages, int non_present_entry_flush)
970{
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700971 unsigned int mask;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700972
Fenghua Yu5b6985c2008-10-16 18:02:32 -0700973 BUG_ON(addr & (~VTD_PAGE_MASK));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700974 BUG_ON(pages == 0);
975
976 /* Fallback to domain selective flush if no PSI support */
977 if (!cap_pgsel_inv(iommu->cap))
Youquan Songa77b67d2008-10-16 16:31:56 -0700978 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
979 DMA_TLB_DSI_FLUSH,
980 non_present_entry_flush);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700981
982 /*
983 * PSI requires page size to be 2 ^ x, and the base address is naturally
984 * aligned to the size
985 */
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700986 mask = ilog2(__roundup_pow_of_two(pages));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700987 /* Fallback to domain selective flush if size is too big */
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -0700988 if (mask > cap_max_amask_val(iommu->cap))
Youquan Songa77b67d2008-10-16 16:31:56 -0700989 return iommu->flush.flush_iotlb(iommu, did, 0, 0,
990 DMA_TLB_DSI_FLUSH, non_present_entry_flush);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700991
Youquan Songa77b67d2008-10-16 16:31:56 -0700992 return iommu->flush.flush_iotlb(iommu, did, addr, mask,
993 DMA_TLB_PSI_FLUSH,
994 non_present_entry_flush);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -0700995}
996
mark grossf8bab732008-02-08 04:18:38 -0800997static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
998{
999 u32 pmen;
1000 unsigned long flags;
1001
1002 spin_lock_irqsave(&iommu->register_lock, flags);
1003 pmen = readl(iommu->reg + DMAR_PMEN_REG);
1004 pmen &= ~DMA_PMEN_EPM;
1005 writel(pmen, iommu->reg + DMAR_PMEN_REG);
1006
1007 /* wait for the protected region status bit to clear */
1008 IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
1009 readl, !(pmen & DMA_PMEN_PRS), pmen);
1010
1011 spin_unlock_irqrestore(&iommu->register_lock, flags);
1012}
1013
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001014static int iommu_enable_translation(struct intel_iommu *iommu)
1015{
1016 u32 sts;
1017 unsigned long flags;
1018
1019 spin_lock_irqsave(&iommu->register_lock, flags);
1020 writel(iommu->gcmd|DMA_GCMD_TE, iommu->reg + DMAR_GCMD_REG);
1021
1022 /* Make sure hardware complete it */
1023 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1024 readl, (sts & DMA_GSTS_TES), sts);
1025
1026 iommu->gcmd |= DMA_GCMD_TE;
1027 spin_unlock_irqrestore(&iommu->register_lock, flags);
1028 return 0;
1029}
1030
1031static int iommu_disable_translation(struct intel_iommu *iommu)
1032{
1033 u32 sts;
1034 unsigned long flag;
1035
1036 spin_lock_irqsave(&iommu->register_lock, flag);
1037 iommu->gcmd &= ~DMA_GCMD_TE;
1038 writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1039
1040 /* Make sure hardware complete it */
1041 IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1042 readl, (!(sts & DMA_GSTS_TES)), sts);
1043
1044 spin_unlock_irqrestore(&iommu->register_lock, flag);
1045 return 0;
1046}
1047
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07001048
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001049static int iommu_init_domains(struct intel_iommu *iommu)
1050{
1051 unsigned long ndomains;
1052 unsigned long nlongs;
1053
1054 ndomains = cap_ndoms(iommu->cap);
1055 pr_debug("Number of Domains supportd <%ld>\n", ndomains);
1056 nlongs = BITS_TO_LONGS(ndomains);
1057
1058 /* TBD: there might be 64K domains,
1059 * consider other allocation for future chip
1060 */
1061 iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
1062 if (!iommu->domain_ids) {
1063 printk(KERN_ERR "Allocating domain id array failed\n");
1064 return -ENOMEM;
1065 }
1066 iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
1067 GFP_KERNEL);
1068 if (!iommu->domains) {
1069 printk(KERN_ERR "Allocating domain array failed\n");
1070 kfree(iommu->domain_ids);
1071 return -ENOMEM;
1072 }
1073
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001074 spin_lock_init(&iommu->lock);
1075
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001076 /*
1077 * if Caching mode is set, then invalid translations are tagged
1078 * with domainid 0. Hence we need to pre-allocate it.
1079 */
1080 if (cap_caching_mode(iommu->cap))
1081 set_bit(0, iommu->domain_ids);
1082 return 0;
1083}
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001084
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001085
1086static void domain_exit(struct dmar_domain *domain);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001087static void vm_domain_exit(struct dmar_domain *domain);
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001088
1089void free_dmar_iommu(struct intel_iommu *iommu)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001090{
1091 struct dmar_domain *domain;
1092 int i;
Weidong Hanc7151a82008-12-08 22:51:37 +08001093 unsigned long flags;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001094
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001095 i = find_first_bit(iommu->domain_ids, cap_ndoms(iommu->cap));
1096 for (; i < cap_ndoms(iommu->cap); ) {
1097 domain = iommu->domains[i];
1098 clear_bit(i, iommu->domain_ids);
Weidong Hanc7151a82008-12-08 22:51:37 +08001099
1100 spin_lock_irqsave(&domain->iommu_lock, flags);
Weidong Han5e98c4b2008-12-08 23:03:27 +08001101 if (--domain->iommu_count == 0) {
1102 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
1103 vm_domain_exit(domain);
1104 else
1105 domain_exit(domain);
1106 }
Weidong Hanc7151a82008-12-08 22:51:37 +08001107 spin_unlock_irqrestore(&domain->iommu_lock, flags);
1108
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001109 i = find_next_bit(iommu->domain_ids,
1110 cap_ndoms(iommu->cap), i+1);
1111 }
1112
1113 if (iommu->gcmd & DMA_GCMD_TE)
1114 iommu_disable_translation(iommu);
1115
1116 if (iommu->irq) {
1117 set_irq_data(iommu->irq, NULL);
1118 /* This will mask the irq */
1119 free_irq(iommu->irq, iommu);
1120 destroy_irq(iommu->irq);
1121 }
1122
1123 kfree(iommu->domains);
1124 kfree(iommu->domain_ids);
1125
Weidong Hand9630fe2008-12-08 11:06:32 +08001126 g_iommus[iommu->seq_id] = NULL;
1127
1128 /* if all iommus are freed, free g_iommus */
1129 for (i = 0; i < g_num_of_iommus; i++) {
1130 if (g_iommus[i])
1131 break;
1132 }
1133
1134 if (i == g_num_of_iommus)
1135 kfree(g_iommus);
1136
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001137 /* free context mapping */
1138 free_context_table(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001139}
1140
1141static struct dmar_domain * iommu_alloc_domain(struct intel_iommu *iommu)
1142{
1143 unsigned long num;
1144 unsigned long ndomains;
1145 struct dmar_domain *domain;
1146 unsigned long flags;
1147
1148 domain = alloc_domain_mem();
1149 if (!domain)
1150 return NULL;
1151
1152 ndomains = cap_ndoms(iommu->cap);
1153
1154 spin_lock_irqsave(&iommu->lock, flags);
1155 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1156 if (num >= ndomains) {
1157 spin_unlock_irqrestore(&iommu->lock, flags);
1158 free_domain_mem(domain);
1159 printk(KERN_ERR "IOMMU: no free domain ids\n");
1160 return NULL;
1161 }
1162
1163 set_bit(num, iommu->domain_ids);
1164 domain->id = num;
Weidong Han8c11e792008-12-08 15:29:22 +08001165 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
1166 set_bit(iommu->seq_id, &domain->iommu_bmp);
Weidong Hand71a2f32008-12-07 21:13:41 +08001167 domain->flags = 0;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001168 iommu->domains[num] = domain;
1169 spin_unlock_irqrestore(&iommu->lock, flags);
1170
1171 return domain;
1172}
1173
1174static void iommu_free_domain(struct dmar_domain *domain)
1175{
1176 unsigned long flags;
Weidong Han8c11e792008-12-08 15:29:22 +08001177 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001178
Weidong Han8c11e792008-12-08 15:29:22 +08001179 iommu = domain_get_iommu(domain);
1180
1181 spin_lock_irqsave(&iommu->lock, flags);
1182 clear_bit(domain->id, iommu->domain_ids);
1183 spin_unlock_irqrestore(&iommu->lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001184}
1185
1186static struct iova_domain reserved_iova_list;
Mark Gross8a443df2008-03-04 14:59:31 -08001187static struct lock_class_key reserved_alloc_key;
1188static struct lock_class_key reserved_rbtree_key;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001189
1190static void dmar_init_reserved_ranges(void)
1191{
1192 struct pci_dev *pdev = NULL;
1193 struct iova *iova;
1194 int i;
1195 u64 addr, size;
1196
David Millerf6611972008-02-06 01:36:23 -08001197 init_iova_domain(&reserved_iova_list, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001198
Mark Gross8a443df2008-03-04 14:59:31 -08001199 lockdep_set_class(&reserved_iova_list.iova_alloc_lock,
1200 &reserved_alloc_key);
1201 lockdep_set_class(&reserved_iova_list.iova_rbtree_lock,
1202 &reserved_rbtree_key);
1203
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001204 /* IOAPIC ranges shouldn't be accessed by DMA */
1205 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(IOAPIC_RANGE_START),
1206 IOVA_PFN(IOAPIC_RANGE_END));
1207 if (!iova)
1208 printk(KERN_ERR "Reserve IOAPIC range failed\n");
1209
1210 /* Reserve all PCI MMIO to avoid peer-to-peer access */
1211 for_each_pci_dev(pdev) {
1212 struct resource *r;
1213
1214 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
1215 r = &pdev->resource[i];
1216 if (!r->flags || !(r->flags & IORESOURCE_MEM))
1217 continue;
1218 addr = r->start;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001219 addr &= PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001220 size = r->end - addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001221 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001222 iova = reserve_iova(&reserved_iova_list, IOVA_PFN(addr),
1223 IOVA_PFN(size + addr) - 1);
1224 if (!iova)
1225 printk(KERN_ERR "Reserve iova failed\n");
1226 }
1227 }
1228
1229}
1230
1231static void domain_reserve_special_ranges(struct dmar_domain *domain)
1232{
1233 copy_reserved_iova(&reserved_iova_list, &domain->iovad);
1234}
1235
1236static inline int guestwidth_to_adjustwidth(int gaw)
1237{
1238 int agaw;
1239 int r = (gaw - 12) % 9;
1240
1241 if (r == 0)
1242 agaw = gaw;
1243 else
1244 agaw = gaw + 9 - r;
1245 if (agaw > 64)
1246 agaw = 64;
1247 return agaw;
1248}
1249
1250static int domain_init(struct dmar_domain *domain, int guest_width)
1251{
1252 struct intel_iommu *iommu;
1253 int adjust_width, agaw;
1254 unsigned long sagaw;
1255
David Millerf6611972008-02-06 01:36:23 -08001256 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001257 spin_lock_init(&domain->mapping_lock);
Weidong Hanc7151a82008-12-08 22:51:37 +08001258 spin_lock_init(&domain->iommu_lock);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001259
1260 domain_reserve_special_ranges(domain);
1261
1262 /* calculate AGAW */
Weidong Han8c11e792008-12-08 15:29:22 +08001263 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001264 if (guest_width > cap_mgaw(iommu->cap))
1265 guest_width = cap_mgaw(iommu->cap);
1266 domain->gaw = guest_width;
1267 adjust_width = guestwidth_to_adjustwidth(guest_width);
1268 agaw = width_to_agaw(adjust_width);
1269 sagaw = cap_sagaw(iommu->cap);
1270 if (!test_bit(agaw, &sagaw)) {
1271 /* hardware doesn't support it, choose a bigger one */
1272 pr_debug("IOMMU: hardware doesn't support agaw %d\n", agaw);
1273 agaw = find_next_bit(&sagaw, 5, agaw);
1274 if (agaw >= 5)
1275 return -ENODEV;
1276 }
1277 domain->agaw = agaw;
1278 INIT_LIST_HEAD(&domain->devices);
1279
Weidong Han8e6040972008-12-08 15:49:06 +08001280 if (ecap_coherent(iommu->ecap))
1281 domain->iommu_coherency = 1;
1282 else
1283 domain->iommu_coherency = 0;
1284
Sheng Yang58c610b2009-03-18 15:33:05 +08001285 if (ecap_sc_support(iommu->ecap))
1286 domain->iommu_snooping = 1;
1287 else
1288 domain->iommu_snooping = 0;
1289
Weidong Hanc7151a82008-12-08 22:51:37 +08001290 domain->iommu_count = 1;
1291
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001292 /* always allocate the top pgd */
1293 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
1294 if (!domain->pgd)
1295 return -ENOMEM;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001296 __iommu_flush_cache(iommu, domain->pgd, PAGE_SIZE);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001297 return 0;
1298}
1299
1300static void domain_exit(struct dmar_domain *domain)
1301{
1302 u64 end;
1303
1304 /* Domain 0 is reserved, so dont process it */
1305 if (!domain)
1306 return;
1307
1308 domain_remove_dev_info(domain);
1309 /* destroy iovas */
1310 put_iova_domain(&domain->iovad);
1311 end = DOMAIN_MAX_ADDR(domain->gaw);
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001312 end = end & (~PAGE_MASK);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001313
1314 /* clear ptes */
1315 dma_pte_clear_range(domain, 0, end);
1316
1317 /* free page tables */
1318 dma_pte_free_pagetable(domain, 0, end);
1319
1320 iommu_free_domain(domain);
1321 free_domain_mem(domain);
1322}
1323
1324static int domain_context_mapping_one(struct dmar_domain *domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001325 int segment, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001326{
1327 struct context_entry *context;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001328 unsigned long flags;
Weidong Han5331fe62008-12-08 23:00:00 +08001329 struct intel_iommu *iommu;
Weidong Hanea6606b2008-12-08 23:08:15 +08001330 struct dma_pte *pgd;
1331 unsigned long num;
1332 unsigned long ndomains;
1333 int id;
1334 int agaw;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001335
1336 pr_debug("Set context mapping for %02x:%02x.%d\n",
1337 bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
1338 BUG_ON(!domain->pgd);
Weidong Han5331fe62008-12-08 23:00:00 +08001339
David Woodhouse276dbf992009-04-04 01:45:37 +01001340 iommu = device_to_iommu(segment, bus, devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001341 if (!iommu)
1342 return -ENODEV;
1343
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001344 context = device_to_context_entry(iommu, bus, devfn);
1345 if (!context)
1346 return -ENOMEM;
1347 spin_lock_irqsave(&iommu->lock, flags);
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001348 if (context_present(context)) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001349 spin_unlock_irqrestore(&iommu->lock, flags);
1350 return 0;
1351 }
1352
Weidong Hanea6606b2008-12-08 23:08:15 +08001353 id = domain->id;
1354 pgd = domain->pgd;
1355
1356 if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE) {
1357 int found = 0;
1358
1359 /* find an available domain id for this device in iommu */
1360 ndomains = cap_ndoms(iommu->cap);
1361 num = find_first_bit(iommu->domain_ids, ndomains);
1362 for (; num < ndomains; ) {
1363 if (iommu->domains[num] == domain) {
1364 id = num;
1365 found = 1;
1366 break;
1367 }
1368 num = find_next_bit(iommu->domain_ids,
1369 cap_ndoms(iommu->cap), num+1);
1370 }
1371
1372 if (found == 0) {
1373 num = find_first_zero_bit(iommu->domain_ids, ndomains);
1374 if (num >= ndomains) {
1375 spin_unlock_irqrestore(&iommu->lock, flags);
1376 printk(KERN_ERR "IOMMU: no free domain ids\n");
1377 return -EFAULT;
1378 }
1379
1380 set_bit(num, iommu->domain_ids);
1381 iommu->domains[num] = domain;
1382 id = num;
1383 }
1384
1385 /* Skip top levels of page tables for
1386 * iommu which has less agaw than default.
1387 */
1388 for (agaw = domain->agaw; agaw != iommu->agaw; agaw--) {
1389 pgd = phys_to_virt(dma_pte_addr(pgd));
1390 if (!dma_pte_present(pgd)) {
1391 spin_unlock_irqrestore(&iommu->lock, flags);
1392 return -ENOMEM;
1393 }
1394 }
1395 }
1396
1397 context_set_domain_id(context, id);
1398 context_set_address_width(context, iommu->agaw);
1399 context_set_address_root(context, virt_to_phys(pgd));
Mark McLoughlinc07e7d22008-11-21 16:54:46 +00001400 context_set_translation_type(context, CONTEXT_TT_MULTI_LEVEL);
1401 context_set_fault_enable(context);
1402 context_set_present(context);
Weidong Han5331fe62008-12-08 23:00:00 +08001403 domain_flush_cache(domain, context, sizeof(*context));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001404
1405 /* it's a non-present to present mapping */
Youquan Songa77b67d2008-10-16 16:31:56 -07001406 if (iommu->flush.flush_context(iommu, domain->id,
1407 (((u16)bus) << 8) | devfn, DMA_CCMD_MASK_NOBIT,
1408 DMA_CCMD_DEVICE_INVL, 1))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001409 iommu_flush_write_buffer(iommu);
1410 else
Youquan Songa77b67d2008-10-16 16:31:56 -07001411 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH, 0);
1412
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001413 spin_unlock_irqrestore(&iommu->lock, flags);
Weidong Hanc7151a82008-12-08 22:51:37 +08001414
1415 spin_lock_irqsave(&domain->iommu_lock, flags);
1416 if (!test_and_set_bit(iommu->seq_id, &domain->iommu_bmp)) {
1417 domain->iommu_count++;
Sheng Yang58c610b2009-03-18 15:33:05 +08001418 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08001419 }
1420 spin_unlock_irqrestore(&domain->iommu_lock, flags);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001421 return 0;
1422}
1423
1424static int
1425domain_context_mapping(struct dmar_domain *domain, struct pci_dev *pdev)
1426{
1427 int ret;
1428 struct pci_dev *tmp, *parent;
1429
David Woodhouse276dbf992009-04-04 01:45:37 +01001430 ret = domain_context_mapping_one(domain, pci_domain_nr(pdev->bus),
1431 pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001432 if (ret)
1433 return ret;
1434
1435 /* dependent device mapping */
1436 tmp = pci_find_upstream_pcie_bridge(pdev);
1437 if (!tmp)
1438 return 0;
1439 /* Secondary interface's bus number and devfn 0 */
1440 parent = pdev->bus->self;
1441 while (parent != tmp) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001442 ret = domain_context_mapping_one(domain,
1443 pci_domain_nr(parent->bus),
1444 parent->bus->number,
1445 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001446 if (ret)
1447 return ret;
1448 parent = parent->bus->self;
1449 }
1450 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
1451 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001452 pci_domain_nr(tmp->subordinate),
1453 tmp->subordinate->number, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001454 else /* this is a legacy PCI bridge */
1455 return domain_context_mapping_one(domain,
David Woodhouse276dbf992009-04-04 01:45:37 +01001456 pci_domain_nr(tmp->bus),
1457 tmp->bus->number,
1458 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001459}
1460
Weidong Han5331fe62008-12-08 23:00:00 +08001461static int domain_context_mapped(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001462{
1463 int ret;
1464 struct pci_dev *tmp, *parent;
Weidong Han5331fe62008-12-08 23:00:00 +08001465 struct intel_iommu *iommu;
1466
David Woodhouse276dbf992009-04-04 01:45:37 +01001467 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
1468 pdev->devfn);
Weidong Han5331fe62008-12-08 23:00:00 +08001469 if (!iommu)
1470 return -ENODEV;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001471
David Woodhouse276dbf992009-04-04 01:45:37 +01001472 ret = device_context_mapped(iommu, pdev->bus->number, pdev->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001473 if (!ret)
1474 return ret;
1475 /* dependent device mapping */
1476 tmp = pci_find_upstream_pcie_bridge(pdev);
1477 if (!tmp)
1478 return ret;
1479 /* Secondary interface's bus number and devfn 0 */
1480 parent = pdev->bus->self;
1481 while (parent != tmp) {
Weidong Han8c11e792008-12-08 15:29:22 +08001482 ret = device_context_mapped(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01001483 parent->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001484 if (!ret)
1485 return ret;
1486 parent = parent->bus->self;
1487 }
1488 if (tmp->is_pcie)
David Woodhouse276dbf992009-04-04 01:45:37 +01001489 return device_context_mapped(iommu, tmp->subordinate->number,
1490 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001491 else
David Woodhouse276dbf992009-04-04 01:45:37 +01001492 return device_context_mapped(iommu, tmp->bus->number,
1493 tmp->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001494}
1495
1496static int
1497domain_page_mapping(struct dmar_domain *domain, dma_addr_t iova,
1498 u64 hpa, size_t size, int prot)
1499{
1500 u64 start_pfn, end_pfn;
1501 struct dma_pte *pte;
1502 int index;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001503 int addr_width = agaw_to_width(domain->agaw);
1504
1505 hpa &= (((u64)1) << addr_width) - 1;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001506
1507 if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0)
1508 return -EINVAL;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001509 iova &= PAGE_MASK;
1510 start_pfn = ((u64)hpa) >> VTD_PAGE_SHIFT;
1511 end_pfn = (VTD_PAGE_ALIGN(((u64)hpa) + size)) >> VTD_PAGE_SHIFT;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001512 index = 0;
1513 while (start_pfn < end_pfn) {
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001514 pte = addr_to_dma_pte(domain, iova + VTD_PAGE_SIZE * index);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001515 if (!pte)
1516 return -ENOMEM;
1517 /* We don't need lock here, nobody else
1518 * touches the iova range
1519 */
Mark McLoughlin19c239c2008-11-21 16:56:53 +00001520 BUG_ON(dma_pte_addr(pte));
1521 dma_set_pte_addr(pte, start_pfn << VTD_PAGE_SHIFT);
1522 dma_set_pte_prot(pte, prot);
Sheng Yang9cf06692009-03-18 15:33:07 +08001523 if (prot & DMA_PTE_SNP)
1524 dma_set_pte_snp(pte);
Weidong Han5331fe62008-12-08 23:00:00 +08001525 domain_flush_cache(domain, pte, sizeof(*pte));
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001526 start_pfn++;
1527 index++;
1528 }
1529 return 0;
1530}
1531
Weidong Hanc7151a82008-12-08 22:51:37 +08001532static void iommu_detach_dev(struct intel_iommu *iommu, u8 bus, u8 devfn)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001533{
Weidong Hanc7151a82008-12-08 22:51:37 +08001534 if (!iommu)
1535 return;
Weidong Han8c11e792008-12-08 15:29:22 +08001536
1537 clear_context_table(iommu, bus, devfn);
1538 iommu->flush.flush_context(iommu, 0, 0, 0,
Youquan Songa77b67d2008-10-16 16:31:56 -07001539 DMA_CCMD_GLOBAL_INVL, 0);
Weidong Han8c11e792008-12-08 15:29:22 +08001540 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
Youquan Songa77b67d2008-10-16 16:31:56 -07001541 DMA_TLB_GLOBAL_FLUSH, 0);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001542}
1543
1544static void domain_remove_dev_info(struct dmar_domain *domain)
1545{
1546 struct device_domain_info *info;
1547 unsigned long flags;
Weidong Hanc7151a82008-12-08 22:51:37 +08001548 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001549
1550 spin_lock_irqsave(&device_domain_lock, flags);
1551 while (!list_empty(&domain->devices)) {
1552 info = list_entry(domain->devices.next,
1553 struct device_domain_info, link);
1554 list_del(&info->link);
1555 list_del(&info->global);
1556 if (info->dev)
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001557 info->dev->dev.archdata.iommu = NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001558 spin_unlock_irqrestore(&device_domain_lock, flags);
1559
David Woodhouse276dbf992009-04-04 01:45:37 +01001560 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08001561 iommu_detach_dev(iommu, info->bus, info->devfn);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001562 free_devinfo_mem(info);
1563
1564 spin_lock_irqsave(&device_domain_lock, flags);
1565 }
1566 spin_unlock_irqrestore(&device_domain_lock, flags);
1567}
1568
1569/*
1570 * find_domain
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001571 * Note: we use struct pci_dev->dev.archdata.iommu stores the info
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001572 */
Kay, Allen M38717942008-09-09 18:37:29 +03001573static struct dmar_domain *
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001574find_domain(struct pci_dev *pdev)
1575{
1576 struct device_domain_info *info;
1577
1578 /* No lock here, assumes no domain exit in normal case */
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001579 info = pdev->dev.archdata.iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001580 if (info)
1581 return info->domain;
1582 return NULL;
1583}
1584
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001585/* domain is initialized */
1586static struct dmar_domain *get_domain_for_dev(struct pci_dev *pdev, int gaw)
1587{
1588 struct dmar_domain *domain, *found = NULL;
1589 struct intel_iommu *iommu;
1590 struct dmar_drhd_unit *drhd;
1591 struct device_domain_info *info, *tmp;
1592 struct pci_dev *dev_tmp;
1593 unsigned long flags;
1594 int bus = 0, devfn = 0;
David Woodhouse276dbf992009-04-04 01:45:37 +01001595 int segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001596
1597 domain = find_domain(pdev);
1598 if (domain)
1599 return domain;
1600
David Woodhouse276dbf992009-04-04 01:45:37 +01001601 segment = pci_domain_nr(pdev->bus);
1602
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001603 dev_tmp = pci_find_upstream_pcie_bridge(pdev);
1604 if (dev_tmp) {
1605 if (dev_tmp->is_pcie) {
1606 bus = dev_tmp->subordinate->number;
1607 devfn = 0;
1608 } else {
1609 bus = dev_tmp->bus->number;
1610 devfn = dev_tmp->devfn;
1611 }
1612 spin_lock_irqsave(&device_domain_lock, flags);
1613 list_for_each_entry(info, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001614 if (info->segment == segment &&
1615 info->bus == bus && info->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001616 found = info->domain;
1617 break;
1618 }
1619 }
1620 spin_unlock_irqrestore(&device_domain_lock, flags);
1621 /* pcie-pci bridge already has a domain, uses it */
1622 if (found) {
1623 domain = found;
1624 goto found_domain;
1625 }
1626 }
1627
1628 /* Allocate new domain for the device */
1629 drhd = dmar_find_matched_drhd_unit(pdev);
1630 if (!drhd) {
1631 printk(KERN_ERR "IOMMU: can't find DMAR for device %s\n",
1632 pci_name(pdev));
1633 return NULL;
1634 }
1635 iommu = drhd->iommu;
1636
1637 domain = iommu_alloc_domain(iommu);
1638 if (!domain)
1639 goto error;
1640
1641 if (domain_init(domain, gaw)) {
1642 domain_exit(domain);
1643 goto error;
1644 }
1645
1646 /* register pcie-to-pci device */
1647 if (dev_tmp) {
1648 info = alloc_devinfo_mem();
1649 if (!info) {
1650 domain_exit(domain);
1651 goto error;
1652 }
David Woodhouse276dbf992009-04-04 01:45:37 +01001653 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001654 info->bus = bus;
1655 info->devfn = devfn;
1656 info->dev = NULL;
1657 info->domain = domain;
1658 /* This domain is shared by devices under p2p bridge */
Weidong Han3b5410e2008-12-08 09:17:15 +08001659 domain->flags |= DOMAIN_FLAG_P2P_MULTIPLE_DEVICES;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001660
1661 /* pcie-to-pci bridge already has a domain, uses it */
1662 found = NULL;
1663 spin_lock_irqsave(&device_domain_lock, flags);
1664 list_for_each_entry(tmp, &device_domain_list, global) {
David Woodhouse276dbf992009-04-04 01:45:37 +01001665 if (tmp->segment == segment &&
1666 tmp->bus == bus && tmp->devfn == devfn) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001667 found = tmp->domain;
1668 break;
1669 }
1670 }
1671 if (found) {
1672 free_devinfo_mem(info);
1673 domain_exit(domain);
1674 domain = found;
1675 } else {
1676 list_add(&info->link, &domain->devices);
1677 list_add(&info->global, &device_domain_list);
1678 }
1679 spin_unlock_irqrestore(&device_domain_lock, flags);
1680 }
1681
1682found_domain:
1683 info = alloc_devinfo_mem();
1684 if (!info)
1685 goto error;
David Woodhouse276dbf992009-04-04 01:45:37 +01001686 info->segment = segment;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001687 info->bus = pdev->bus->number;
1688 info->devfn = pdev->devfn;
1689 info->dev = pdev;
1690 info->domain = domain;
1691 spin_lock_irqsave(&device_domain_lock, flags);
1692 /* somebody is fast */
1693 found = find_domain(pdev);
1694 if (found != NULL) {
1695 spin_unlock_irqrestore(&device_domain_lock, flags);
1696 if (found != domain) {
1697 domain_exit(domain);
1698 domain = found;
1699 }
1700 free_devinfo_mem(info);
1701 return domain;
1702 }
1703 list_add(&info->link, &domain->devices);
1704 list_add(&info->global, &device_domain_list);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001705 pdev->dev.archdata.iommu = info;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001706 spin_unlock_irqrestore(&device_domain_lock, flags);
1707 return domain;
1708error:
1709 /* recheck it here, maybe others set it */
1710 return find_domain(pdev);
1711}
1712
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001713static int iommu_prepare_identity_map(struct pci_dev *pdev,
1714 unsigned long long start,
1715 unsigned long long end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001716{
1717 struct dmar_domain *domain;
1718 unsigned long size;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001719 unsigned long long base;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001720 int ret;
1721
1722 printk(KERN_INFO
1723 "IOMMU: Setting identity map for device %s [0x%Lx - 0x%Lx]\n",
1724 pci_name(pdev), start, end);
1725 /* page table init */
1726 domain = get_domain_for_dev(pdev, DEFAULT_DOMAIN_ADDRESS_WIDTH);
1727 if (!domain)
1728 return -ENOMEM;
1729
1730 /* The address might not be aligned */
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001731 base = start & PAGE_MASK;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001732 size = end - base;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07001733 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001734 if (!reserve_iova(&domain->iovad, IOVA_PFN(base),
1735 IOVA_PFN(base + size) - 1)) {
1736 printk(KERN_ERR "IOMMU: reserve iova failed\n");
1737 ret = -ENOMEM;
1738 goto error;
1739 }
1740
1741 pr_debug("Mapping reserved region %lx@%llx for %s\n",
1742 size, base, pci_name(pdev));
1743 /*
1744 * RMRR range might have overlap with physical memory range,
1745 * clear it first
1746 */
1747 dma_pte_clear_range(domain, base, base + size);
1748
1749 ret = domain_page_mapping(domain, base, base, size,
1750 DMA_PTE_READ|DMA_PTE_WRITE);
1751 if (ret)
1752 goto error;
1753
1754 /* context entry init */
1755 ret = domain_context_mapping(domain, pdev);
1756 if (!ret)
1757 return 0;
1758error:
1759 domain_exit(domain);
1760 return ret;
1761
1762}
1763
1764static inline int iommu_prepare_rmrr_dev(struct dmar_rmrr_unit *rmrr,
1765 struct pci_dev *pdev)
1766{
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001767 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001768 return 0;
1769 return iommu_prepare_identity_map(pdev, rmrr->base_address,
1770 rmrr->end_address + 1);
1771}
1772
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001773#ifdef CONFIG_DMAR_GFX_WA
Yinghai Lud52d53b2008-06-16 20:10:55 -07001774struct iommu_prepare_data {
1775 struct pci_dev *pdev;
1776 int ret;
1777};
1778
1779static int __init iommu_prepare_work_fn(unsigned long start_pfn,
1780 unsigned long end_pfn, void *datax)
1781{
1782 struct iommu_prepare_data *data;
1783
1784 data = (struct iommu_prepare_data *)datax;
1785
1786 data->ret = iommu_prepare_identity_map(data->pdev,
1787 start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT);
1788 return data->ret;
1789
1790}
1791
1792static int __init iommu_prepare_with_active_regions(struct pci_dev *pdev)
1793{
1794 int nid;
1795 struct iommu_prepare_data data;
1796
1797 data.pdev = pdev;
1798 data.ret = 0;
1799
1800 for_each_online_node(nid) {
1801 work_with_active_regions(nid, iommu_prepare_work_fn, &data);
1802 if (data.ret)
1803 return data.ret;
1804 }
1805 return data.ret;
1806}
1807
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001808static void __init iommu_prepare_gfx_mapping(void)
1809{
1810 struct pci_dev *pdev = NULL;
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001811 int ret;
1812
1813 for_each_pci_dev(pdev) {
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07001814 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO ||
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001815 !IS_GFX_DEVICE(pdev))
1816 continue;
1817 printk(KERN_INFO "IOMMU: gfx device %s 1-1 mapping\n",
1818 pci_name(pdev));
Yinghai Lud52d53b2008-06-16 20:10:55 -07001819 ret = iommu_prepare_with_active_regions(pdev);
1820 if (ret)
1821 printk(KERN_ERR "IOMMU: mapping reserved region failed\n");
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001822 }
1823}
Mark McLoughlin2abd7e12008-11-20 15:49:50 +00001824#else /* !CONFIG_DMAR_GFX_WA */
1825static inline void iommu_prepare_gfx_mapping(void)
1826{
1827 return;
1828}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07001829#endif
1830
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001831#ifdef CONFIG_DMAR_FLOPPY_WA
1832static inline void iommu_prepare_isa(void)
1833{
1834 struct pci_dev *pdev;
1835 int ret;
1836
1837 pdev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
1838 if (!pdev)
1839 return;
1840
1841 printk(KERN_INFO "IOMMU: Prepare 0-16M unity mapping for LPC\n");
1842 ret = iommu_prepare_identity_map(pdev, 0, 16*1024*1024);
1843
1844 if (ret)
Frank Seidel1c35b8e2009-02-06 10:23:36 +01001845 printk(KERN_ERR "IOMMU: Failed to create 0-64M identity map, "
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07001846 "floppy might not work\n");
1847
1848}
1849#else
1850static inline void iommu_prepare_isa(void)
1851{
1852 return;
1853}
1854#endif /* !CONFIG_DMAR_FLPY_WA */
1855
Mark McLoughlin519a0542008-11-20 14:21:13 +00001856static int __init init_dmars(void)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001857{
1858 struct dmar_drhd_unit *drhd;
1859 struct dmar_rmrr_unit *rmrr;
1860 struct pci_dev *pdev;
1861 struct intel_iommu *iommu;
Suresh Siddha9d783ba2009-03-16 17:04:55 -07001862 int i, ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001863
1864 /*
1865 * for each drhd
1866 * allocate root
1867 * initialize and program root entry to not present
1868 * endfor
1869 */
1870 for_each_drhd_unit(drhd) {
mark gross5e0d2a62008-03-04 15:22:08 -08001871 g_num_of_iommus++;
1872 /*
1873 * lock not needed as this is only incremented in the single
1874 * threaded kernel __init code path all other access are read
1875 * only
1876 */
1877 }
1878
Weidong Hand9630fe2008-12-08 11:06:32 +08001879 g_iommus = kcalloc(g_num_of_iommus, sizeof(struct intel_iommu *),
1880 GFP_KERNEL);
1881 if (!g_iommus) {
1882 printk(KERN_ERR "Allocating global iommu array failed\n");
1883 ret = -ENOMEM;
1884 goto error;
1885 }
1886
mark gross80b20dd2008-04-18 13:53:58 -07001887 deferred_flush = kzalloc(g_num_of_iommus *
1888 sizeof(struct deferred_flush_tables), GFP_KERNEL);
1889 if (!deferred_flush) {
Weidong Hand9630fe2008-12-08 11:06:32 +08001890 kfree(g_iommus);
mark gross5e0d2a62008-03-04 15:22:08 -08001891 ret = -ENOMEM;
1892 goto error;
1893 }
1894
mark gross5e0d2a62008-03-04 15:22:08 -08001895 for_each_drhd_unit(drhd) {
1896 if (drhd->ignored)
1897 continue;
Suresh Siddha1886e8a2008-07-10 11:16:37 -07001898
1899 iommu = drhd->iommu;
Weidong Hand9630fe2008-12-08 11:06:32 +08001900 g_iommus[iommu->seq_id] = iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001901
Suresh Siddhae61d98d2008-07-10 11:16:35 -07001902 ret = iommu_init_domains(iommu);
1903 if (ret)
1904 goto error;
1905
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001906 /*
1907 * TBD:
1908 * we could share the same root & context tables
1909 * amoung all IOMMU's. Need to Split it later.
1910 */
1911 ret = iommu_alloc_root_entry(iommu);
1912 if (ret) {
1913 printk(KERN_ERR "IOMMU: allocate root entry failed\n");
1914 goto error;
1915 }
1916 }
1917
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001918 /*
1919 * Start from the sane iommu hardware state.
1920 */
Youquan Songa77b67d2008-10-16 16:31:56 -07001921 for_each_drhd_unit(drhd) {
1922 if (drhd->ignored)
1923 continue;
1924
1925 iommu = drhd->iommu;
Suresh Siddha1531a6a2009-03-16 17:04:57 -07001926
1927 /*
1928 * If the queued invalidation is already initialized by us
1929 * (for example, while enabling interrupt-remapping) then
1930 * we got the things already rolling from a sane state.
1931 */
1932 if (iommu->qi)
1933 continue;
1934
1935 /*
1936 * Clear any previous faults.
1937 */
1938 dmar_fault(-1, iommu);
1939 /*
1940 * Disable queued invalidation if supported and already enabled
1941 * before OS handover.
1942 */
1943 dmar_disable_qi(iommu);
1944 }
1945
1946 for_each_drhd_unit(drhd) {
1947 if (drhd->ignored)
1948 continue;
1949
1950 iommu = drhd->iommu;
1951
Youquan Songa77b67d2008-10-16 16:31:56 -07001952 if (dmar_enable_qi(iommu)) {
1953 /*
1954 * Queued Invalidate not enabled, use Register Based
1955 * Invalidate
1956 */
1957 iommu->flush.flush_context = __iommu_flush_context;
1958 iommu->flush.flush_iotlb = __iommu_flush_iotlb;
1959 printk(KERN_INFO "IOMMU 0x%Lx: using Register based "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09001960 "invalidation\n",
1961 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07001962 } else {
1963 iommu->flush.flush_context = qi_flush_context;
1964 iommu->flush.flush_iotlb = qi_flush_iotlb;
1965 printk(KERN_INFO "IOMMU 0x%Lx: using Queued "
FUJITA Tomonorib4e0f9e2008-11-19 13:53:42 +09001966 "invalidation\n",
1967 (unsigned long long)drhd->reg_base_addr);
Youquan Songa77b67d2008-10-16 16:31:56 -07001968 }
1969 }
1970
Han, Weidongd0b03bd2009-04-03 17:15:50 +08001971#ifdef CONFIG_INTR_REMAP
1972 if (!intr_remapping_enabled) {
1973 ret = enable_intr_remapping(0);
1974 if (ret)
1975 printk(KERN_ERR
1976 "IOMMU: enable interrupt remapping failed\n");
1977 }
1978#endif
1979
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001980 /*
1981 * For each rmrr
1982 * for each dev attached to rmrr
1983 * do
1984 * locate drhd for dev, alloc domain for dev
1985 * allocate free domain
1986 * allocate page table entries for rmrr
1987 * if context not allocated for bus
1988 * allocate and init context
1989 * set present in root table for this bus
1990 * init context with domain, translation etc
1991 * endfor
1992 * endfor
1993 */
1994 for_each_rmrr_units(rmrr) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07001995 for (i = 0; i < rmrr->devices_cnt; i++) {
1996 pdev = rmrr->devices[i];
1997 /* some BIOS lists non-exist devices in DMAR table */
1998 if (!pdev)
1999 continue;
2000 ret = iommu_prepare_rmrr_dev(rmrr, pdev);
2001 if (ret)
2002 printk(KERN_ERR
2003 "IOMMU: mapping reserved region failed\n");
2004 }
2005 }
2006
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002007 iommu_prepare_gfx_mapping();
2008
Keshavamurthy, Anil S49a04292007-10-21 16:41:57 -07002009 iommu_prepare_isa();
2010
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002011 /*
2012 * for each drhd
2013 * enable fault log
2014 * global invalidate context cache
2015 * global invalidate iotlb
2016 * enable translation
2017 */
2018 for_each_drhd_unit(drhd) {
2019 if (drhd->ignored)
2020 continue;
2021 iommu = drhd->iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002022
2023 iommu_flush_write_buffer(iommu);
2024
Keshavamurthy, Anil S3460a6d2007-10-21 16:41:54 -07002025 ret = dmar_set_interrupt(iommu);
2026 if (ret)
2027 goto error;
2028
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002029 iommu_set_root_entry(iommu);
2030
Youquan Songa77b67d2008-10-16 16:31:56 -07002031 iommu->flush.flush_context(iommu, 0, 0, 0, DMA_CCMD_GLOBAL_INVL,
2032 0);
2033 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH,
2034 0);
mark grossf8bab732008-02-08 04:18:38 -08002035 iommu_disable_protect_mem_regions(iommu);
2036
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002037 ret = iommu_enable_translation(iommu);
2038 if (ret)
2039 goto error;
2040 }
2041
2042 return 0;
2043error:
2044 for_each_drhd_unit(drhd) {
2045 if (drhd->ignored)
2046 continue;
2047 iommu = drhd->iommu;
2048 free_iommu(iommu);
2049 }
Weidong Hand9630fe2008-12-08 11:06:32 +08002050 kfree(g_iommus);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002051 return ret;
2052}
2053
2054static inline u64 aligned_size(u64 host_addr, size_t size)
2055{
2056 u64 addr;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002057 addr = (host_addr & (~PAGE_MASK)) + size;
2058 return PAGE_ALIGN(addr);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002059}
2060
2061struct iova *
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002062iommu_alloc_iova(struct dmar_domain *domain, size_t size, u64 end)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002063{
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002064 struct iova *piova;
2065
2066 /* Make sure it's in range */
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002067 end = min_t(u64, DOMAIN_MAX_ADDR(domain->gaw), end);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002068 if (!size || (IOVA_START_ADDR + size > end))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002069 return NULL;
2070
2071 piova = alloc_iova(&domain->iovad,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002072 size >> PAGE_SHIFT, IOVA_PFN(end), 1);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002073 return piova;
2074}
2075
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002076static struct iova *
2077__intel_alloc_iova(struct device *dev, struct dmar_domain *domain,
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002078 size_t size, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002079{
2080 struct pci_dev *pdev = to_pci_dev(dev);
2081 struct iova *iova = NULL;
2082
Yang Hongyang284901a2009-04-06 19:01:15 -07002083 if (dma_mask <= DMA_BIT_MASK(32) || dmar_forcedac)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002084 iova = iommu_alloc_iova(domain, size, dma_mask);
2085 else {
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002086 /*
2087 * First try to allocate an io virtual address in
Yang Hongyang284901a2009-04-06 19:01:15 -07002088 * DMA_BIT_MASK(32) and if that fails then try allocating
Joe Perches36098012007-12-17 11:40:11 -08002089 * from higher range
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002090 */
Yang Hongyang284901a2009-04-06 19:01:15 -07002091 iova = iommu_alloc_iova(domain, size, DMA_BIT_MASK(32));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002092 if (!iova)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002093 iova = iommu_alloc_iova(domain, size, dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002094 }
2095
2096 if (!iova) {
2097 printk(KERN_ERR"Allocating iova for %s failed", pci_name(pdev));
2098 return NULL;
2099 }
2100
2101 return iova;
2102}
2103
2104static struct dmar_domain *
2105get_valid_domain_for_dev(struct pci_dev *pdev)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002106{
2107 struct dmar_domain *domain;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002108 int ret;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002109
2110 domain = get_domain_for_dev(pdev,
2111 DEFAULT_DOMAIN_ADDRESS_WIDTH);
2112 if (!domain) {
2113 printk(KERN_ERR
2114 "Allocating domain for %s failed", pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002115 return NULL;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002116 }
2117
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002118 /* make sure context mapping is ok */
Weidong Han5331fe62008-12-08 23:00:00 +08002119 if (unlikely(!domain_context_mapped(pdev))) {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002120 ret = domain_context_mapping(domain, pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002121 if (ret) {
2122 printk(KERN_ERR
2123 "Domain context map for %s failed",
2124 pci_name(pdev));
Al Viro4fe05bb2007-10-29 04:51:16 +00002125 return NULL;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002126 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002127 }
2128
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002129 return domain;
2130}
2131
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002132static dma_addr_t __intel_map_single(struct device *hwdev, phys_addr_t paddr,
2133 size_t size, int dir, u64 dma_mask)
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002134{
2135 struct pci_dev *pdev = to_pci_dev(hwdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002136 struct dmar_domain *domain;
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002137 phys_addr_t start_paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002138 struct iova *iova;
2139 int prot = 0;
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002140 int ret;
Weidong Han8c11e792008-12-08 15:29:22 +08002141 struct intel_iommu *iommu;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002142
2143 BUG_ON(dir == DMA_NONE);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002144 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002145 return paddr;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002146
2147 domain = get_valid_domain_for_dev(pdev);
2148 if (!domain)
2149 return 0;
2150
Weidong Han8c11e792008-12-08 15:29:22 +08002151 iommu = domain_get_iommu(domain);
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002152 size = aligned_size((u64)paddr, size);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002153
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002154 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002155 if (!iova)
2156 goto error;
2157
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002158 start_paddr = (phys_addr_t)iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002159
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002160 /*
2161 * Check if DMAR supports zero-length reads on write only
2162 * mappings..
2163 */
2164 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002165 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002166 prot |= DMA_PTE_READ;
2167 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2168 prot |= DMA_PTE_WRITE;
2169 /*
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002170 * paddr - (paddr + size) might be partial page, we should map the whole
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002171 * page. Note: if two part of one page are separately mapped, we
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002172 * might have two guest_addr mapping to the same host paddr, but this
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002173 * is not a big problem
2174 */
Ingo Molnar6865f0d2008-04-22 11:09:04 +02002175 ret = domain_page_mapping(domain, start_paddr,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002176 ((u64)paddr) & PAGE_MASK, size, prot);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002177 if (ret)
2178 goto error;
2179
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002180 /* it's a non-present to present mapping */
Weidong Han8c11e792008-12-08 15:29:22 +08002181 ret = iommu_flush_iotlb_psi(iommu, domain->id,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002182 start_paddr, size >> VTD_PAGE_SHIFT, 1);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002183 if (ret)
Weidong Han8c11e792008-12-08 15:29:22 +08002184 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002185
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002186 return start_paddr + ((u64)paddr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002187
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002188error:
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002189 if (iova)
2190 __free_iova(&domain->iovad, iova);
David Woodhouse4cf2e752009-02-11 17:23:43 +00002191 printk(KERN_ERR"Device %s request: %zx@%llx dir %d --- failed\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002192 pci_name(pdev), size, (unsigned long long)paddr, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002193 return 0;
2194}
2195
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002196static dma_addr_t intel_map_page(struct device *dev, struct page *page,
2197 unsigned long offset, size_t size,
2198 enum dma_data_direction dir,
2199 struct dma_attrs *attrs)
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002200{
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002201 return __intel_map_single(dev, page_to_phys(page) + offset, size,
2202 dir, to_pci_dev(dev)->dma_mask);
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002203}
2204
mark gross5e0d2a62008-03-04 15:22:08 -08002205static void flush_unmaps(void)
2206{
mark gross80b20dd2008-04-18 13:53:58 -07002207 int i, j;
mark gross5e0d2a62008-03-04 15:22:08 -08002208
mark gross5e0d2a62008-03-04 15:22:08 -08002209 timer_on = 0;
2210
2211 /* just flush them all */
2212 for (i = 0; i < g_num_of_iommus; i++) {
Weidong Hana2bb8452008-12-08 11:24:12 +08002213 struct intel_iommu *iommu = g_iommus[i];
2214 if (!iommu)
2215 continue;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002216
Weidong Hana2bb8452008-12-08 11:24:12 +08002217 if (deferred_flush[i].next) {
Youquan Songa77b67d2008-10-16 16:31:56 -07002218 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2219 DMA_TLB_GLOBAL_FLUSH, 0);
mark gross80b20dd2008-04-18 13:53:58 -07002220 for (j = 0; j < deferred_flush[i].next; j++) {
2221 __free_iova(&deferred_flush[i].domain[j]->iovad,
2222 deferred_flush[i].iova[j]);
2223 }
2224 deferred_flush[i].next = 0;
2225 }
mark gross5e0d2a62008-03-04 15:22:08 -08002226 }
2227
mark gross5e0d2a62008-03-04 15:22:08 -08002228 list_size = 0;
mark gross5e0d2a62008-03-04 15:22:08 -08002229}
2230
2231static void flush_unmaps_timeout(unsigned long data)
2232{
mark gross80b20dd2008-04-18 13:53:58 -07002233 unsigned long flags;
2234
2235 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002236 flush_unmaps();
mark gross80b20dd2008-04-18 13:53:58 -07002237 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
mark gross5e0d2a62008-03-04 15:22:08 -08002238}
2239
2240static void add_unmap(struct dmar_domain *dom, struct iova *iova)
2241{
2242 unsigned long flags;
mark gross80b20dd2008-04-18 13:53:58 -07002243 int next, iommu_id;
Weidong Han8c11e792008-12-08 15:29:22 +08002244 struct intel_iommu *iommu;
mark gross5e0d2a62008-03-04 15:22:08 -08002245
2246 spin_lock_irqsave(&async_umap_flush_lock, flags);
mark gross80b20dd2008-04-18 13:53:58 -07002247 if (list_size == HIGH_WATER_MARK)
2248 flush_unmaps();
2249
Weidong Han8c11e792008-12-08 15:29:22 +08002250 iommu = domain_get_iommu(dom);
2251 iommu_id = iommu->seq_id;
Suresh Siddhac42d9f32008-07-10 11:16:36 -07002252
mark gross80b20dd2008-04-18 13:53:58 -07002253 next = deferred_flush[iommu_id].next;
2254 deferred_flush[iommu_id].domain[next] = dom;
2255 deferred_flush[iommu_id].iova[next] = iova;
2256 deferred_flush[iommu_id].next++;
mark gross5e0d2a62008-03-04 15:22:08 -08002257
2258 if (!timer_on) {
2259 mod_timer(&unmap_timer, jiffies + msecs_to_jiffies(10));
2260 timer_on = 1;
2261 }
2262 list_size++;
2263 spin_unlock_irqrestore(&async_umap_flush_lock, flags);
2264}
2265
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002266static void intel_unmap_page(struct device *dev, dma_addr_t dev_addr,
2267 size_t size, enum dma_data_direction dir,
2268 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002269{
2270 struct pci_dev *pdev = to_pci_dev(dev);
2271 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002272 unsigned long start_addr;
2273 struct iova *iova;
Weidong Han8c11e792008-12-08 15:29:22 +08002274 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002275
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002276 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002277 return;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002278 domain = find_domain(pdev);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002279 BUG_ON(!domain);
2280
Weidong Han8c11e792008-12-08 15:29:22 +08002281 iommu = domain_get_iommu(domain);
2282
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002283 iova = find_iova(&domain->iovad, IOVA_PFN(dev_addr));
2284 if (!iova)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002285 return;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002286
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002287 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002288 size = aligned_size((u64)dev_addr, size);
2289
David Woodhouse4cf2e752009-02-11 17:23:43 +00002290 pr_debug("Device %s unmapping: %zx@%llx\n",
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002291 pci_name(pdev), size, (unsigned long long)start_addr);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002292
2293 /* clear the whole page */
2294 dma_pte_clear_range(domain, start_addr, start_addr + size);
2295 /* free page tables */
2296 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
mark gross5e0d2a62008-03-04 15:22:08 -08002297 if (intel_iommu_strict) {
Weidong Han8c11e792008-12-08 15:29:22 +08002298 if (iommu_flush_iotlb_psi(iommu,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002299 domain->id, start_addr, size >> VTD_PAGE_SHIFT, 0))
Weidong Han8c11e792008-12-08 15:29:22 +08002300 iommu_flush_write_buffer(iommu);
mark gross5e0d2a62008-03-04 15:22:08 -08002301 /* free iova */
2302 __free_iova(&domain->iovad, iova);
2303 } else {
2304 add_unmap(domain, iova);
2305 /*
2306 * queue up the release of the unmap to save the 1/6th of the
2307 * cpu used up by the iotlb flush operation...
2308 */
mark gross5e0d2a62008-03-04 15:22:08 -08002309 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002310}
2311
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002312static void intel_unmap_single(struct device *dev, dma_addr_t dev_addr, size_t size,
2313 int dir)
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002314{
2315 intel_unmap_page(dev, dev_addr, size, dir, NULL);
2316}
2317
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002318static void *intel_alloc_coherent(struct device *hwdev, size_t size,
2319 dma_addr_t *dma_handle, gfp_t flags)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002320{
2321 void *vaddr;
2322 int order;
2323
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002324 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002325 order = get_order(size);
2326 flags &= ~(GFP_DMA | GFP_DMA32);
2327
2328 vaddr = (void *)__get_free_pages(flags, order);
2329 if (!vaddr)
2330 return NULL;
2331 memset(vaddr, 0, size);
2332
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002333 *dma_handle = __intel_map_single(hwdev, virt_to_bus(vaddr), size,
2334 DMA_BIDIRECTIONAL,
2335 hwdev->coherent_dma_mask);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002336 if (*dma_handle)
2337 return vaddr;
2338 free_pages((unsigned long)vaddr, order);
2339 return NULL;
2340}
2341
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002342static void intel_free_coherent(struct device *hwdev, size_t size, void *vaddr,
2343 dma_addr_t dma_handle)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002344{
2345 int order;
2346
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002347 size = PAGE_ALIGN(size);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002348 order = get_order(size);
2349
2350 intel_unmap_single(hwdev, dma_handle, size, DMA_BIDIRECTIONAL);
2351 free_pages((unsigned long)vaddr, order);
2352}
2353
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002354static void intel_unmap_sg(struct device *hwdev, struct scatterlist *sglist,
2355 int nelems, enum dma_data_direction dir,
2356 struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002357{
2358 int i;
2359 struct pci_dev *pdev = to_pci_dev(hwdev);
2360 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002361 unsigned long start_addr;
2362 struct iova *iova;
2363 size_t size = 0;
David Woodhouse4cf2e752009-02-11 17:23:43 +00002364 phys_addr_t addr;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002365 struct scatterlist *sg;
Weidong Han8c11e792008-12-08 15:29:22 +08002366 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002367
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002368 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002369 return;
2370
2371 domain = find_domain(pdev);
Weidong Han8c11e792008-12-08 15:29:22 +08002372 BUG_ON(!domain);
2373
2374 iommu = domain_get_iommu(domain);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002375
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002376 iova = find_iova(&domain->iovad, IOVA_PFN(sglist[0].dma_address));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002377 if (!iova)
2378 return;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002379 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002380 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002381 size += aligned_size((u64)addr, sg->length);
2382 }
2383
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002384 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002385
2386 /* clear the whole page */
2387 dma_pte_clear_range(domain, start_addr, start_addr + size);
2388 /* free page tables */
2389 dma_pte_free_pagetable(domain, start_addr, start_addr + size);
2390
Weidong Han8c11e792008-12-08 15:29:22 +08002391 if (iommu_flush_iotlb_psi(iommu, domain->id, start_addr,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002392 size >> VTD_PAGE_SHIFT, 0))
Weidong Han8c11e792008-12-08 15:29:22 +08002393 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002394
2395 /* free iova */
2396 __free_iova(&domain->iovad, iova);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002397}
2398
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002399static int intel_nontranslate_map_sg(struct device *hddev,
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002400 struct scatterlist *sglist, int nelems, int dir)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002401{
2402 int i;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002403 struct scatterlist *sg;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002404
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002405 for_each_sg(sglist, sg, nelems, i) {
FUJITA Tomonori12d4d402007-10-23 09:32:25 +02002406 BUG_ON(!sg_page(sg));
David Woodhouse4cf2e752009-02-11 17:23:43 +00002407 sg->dma_address = page_to_phys(sg_page(sg)) + sg->offset;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002408 sg->dma_length = sg->length;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002409 }
2410 return nelems;
2411}
2412
FUJITA Tomonorid7ab5c42009-01-28 21:53:18 +09002413static int intel_map_sg(struct device *hwdev, struct scatterlist *sglist, int nelems,
2414 enum dma_data_direction dir, struct dma_attrs *attrs)
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002415{
David Woodhouse4cf2e752009-02-11 17:23:43 +00002416 phys_addr_t addr;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002417 int i;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002418 struct pci_dev *pdev = to_pci_dev(hwdev);
2419 struct dmar_domain *domain;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002420 size_t size = 0;
2421 int prot = 0;
2422 size_t offset = 0;
2423 struct iova *iova = NULL;
2424 int ret;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002425 struct scatterlist *sg;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002426 unsigned long start_addr;
Weidong Han8c11e792008-12-08 15:29:22 +08002427 struct intel_iommu *iommu;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002428
2429 BUG_ON(dir == DMA_NONE);
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002430 if (pdev->dev.archdata.iommu == DUMMY_DEVICE_DOMAIN_INFO)
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002431 return intel_nontranslate_map_sg(hwdev, sglist, nelems, dir);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002432
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002433 domain = get_valid_domain_for_dev(pdev);
2434 if (!domain)
2435 return 0;
2436
Weidong Han8c11e792008-12-08 15:29:22 +08002437 iommu = domain_get_iommu(domain);
2438
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002439 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002440 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002441 size += aligned_size((u64)addr, sg->length);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002442 }
2443
FUJITA Tomonoribb9e6d62008-10-15 16:08:28 +09002444 iova = __intel_alloc_iova(hwdev, domain, size, pdev->dma_mask);
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002445 if (!iova) {
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002446 sglist->dma_length = 0;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002447 return 0;
2448 }
2449
2450 /*
2451 * Check if DMAR supports zero-length reads on write only
2452 * mappings..
2453 */
2454 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL || \
Weidong Han8c11e792008-12-08 15:29:22 +08002455 !cap_zlr(iommu->cap))
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002456 prot |= DMA_PTE_READ;
2457 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
2458 prot |= DMA_PTE_WRITE;
2459
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002460 start_addr = iova->pfn_lo << PAGE_SHIFT;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002461 offset = 0;
FUJITA Tomonoric03ab372007-10-21 16:42:00 -07002462 for_each_sg(sglist, sg, nelems, i) {
David Woodhouse4cf2e752009-02-11 17:23:43 +00002463 addr = page_to_phys(sg_page(sg)) + sg->offset;
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002464 size = aligned_size((u64)addr, sg->length);
2465 ret = domain_page_mapping(domain, start_addr + offset,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002466 ((u64)addr) & PAGE_MASK,
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002467 size, prot);
2468 if (ret) {
2469 /* clear the page */
2470 dma_pte_clear_range(domain, start_addr,
2471 start_addr + offset);
2472 /* free page tables */
2473 dma_pte_free_pagetable(domain, start_addr,
2474 start_addr + offset);
2475 /* free iova */
2476 __free_iova(&domain->iovad, iova);
2477 return 0;
2478 }
2479 sg->dma_address = start_addr + offset +
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002480 ((u64)addr & (~PAGE_MASK));
Keshavamurthy, Anil Sf76aec72007-10-21 16:41:58 -07002481 sg->dma_length = sg->length;
2482 offset += size;
2483 }
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002484
2485 /* it's a non-present to present mapping */
Weidong Han8c11e792008-12-08 15:29:22 +08002486 if (iommu_flush_iotlb_psi(iommu, domain->id,
Fenghua Yu5b6985c2008-10-16 18:02:32 -07002487 start_addr, offset >> VTD_PAGE_SHIFT, 1))
Weidong Han8c11e792008-12-08 15:29:22 +08002488 iommu_flush_write_buffer(iommu);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002489 return nelems;
2490}
2491
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002492static int intel_mapping_error(struct device *dev, dma_addr_t dma_addr)
2493{
2494 return !dma_addr;
2495}
2496
FUJITA Tomonori160c1d82009-01-05 23:59:02 +09002497struct dma_map_ops intel_dma_ops = {
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002498 .alloc_coherent = intel_alloc_coherent,
2499 .free_coherent = intel_free_coherent,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002500 .map_sg = intel_map_sg,
2501 .unmap_sg = intel_unmap_sg,
FUJITA Tomonoriffbbef52009-01-05 23:47:26 +09002502 .map_page = intel_map_page,
2503 .unmap_page = intel_unmap_page,
FUJITA Tomonoridfb805e2009-01-28 21:53:17 +09002504 .mapping_error = intel_mapping_error,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002505};
2506
2507static inline int iommu_domain_cache_init(void)
2508{
2509 int ret = 0;
2510
2511 iommu_domain_cache = kmem_cache_create("iommu_domain",
2512 sizeof(struct dmar_domain),
2513 0,
2514 SLAB_HWCACHE_ALIGN,
2515
2516 NULL);
2517 if (!iommu_domain_cache) {
2518 printk(KERN_ERR "Couldn't create iommu_domain cache\n");
2519 ret = -ENOMEM;
2520 }
2521
2522 return ret;
2523}
2524
2525static inline int iommu_devinfo_cache_init(void)
2526{
2527 int ret = 0;
2528
2529 iommu_devinfo_cache = kmem_cache_create("iommu_devinfo",
2530 sizeof(struct device_domain_info),
2531 0,
2532 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002533 NULL);
2534 if (!iommu_devinfo_cache) {
2535 printk(KERN_ERR "Couldn't create devinfo cache\n");
2536 ret = -ENOMEM;
2537 }
2538
2539 return ret;
2540}
2541
2542static inline int iommu_iova_cache_init(void)
2543{
2544 int ret = 0;
2545
2546 iommu_iova_cache = kmem_cache_create("iommu_iova",
2547 sizeof(struct iova),
2548 0,
2549 SLAB_HWCACHE_ALIGN,
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002550 NULL);
2551 if (!iommu_iova_cache) {
2552 printk(KERN_ERR "Couldn't create iova cache\n");
2553 ret = -ENOMEM;
2554 }
2555
2556 return ret;
2557}
2558
2559static int __init iommu_init_mempool(void)
2560{
2561 int ret;
2562 ret = iommu_iova_cache_init();
2563 if (ret)
2564 return ret;
2565
2566 ret = iommu_domain_cache_init();
2567 if (ret)
2568 goto domain_error;
2569
2570 ret = iommu_devinfo_cache_init();
2571 if (!ret)
2572 return ret;
2573
2574 kmem_cache_destroy(iommu_domain_cache);
2575domain_error:
2576 kmem_cache_destroy(iommu_iova_cache);
2577
2578 return -ENOMEM;
2579}
2580
2581static void __init iommu_exit_mempool(void)
2582{
2583 kmem_cache_destroy(iommu_devinfo_cache);
2584 kmem_cache_destroy(iommu_domain_cache);
2585 kmem_cache_destroy(iommu_iova_cache);
2586
2587}
2588
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002589static void __init init_no_remapping_devices(void)
2590{
2591 struct dmar_drhd_unit *drhd;
2592
2593 for_each_drhd_unit(drhd) {
2594 if (!drhd->include_all) {
2595 int i;
2596 for (i = 0; i < drhd->devices_cnt; i++)
2597 if (drhd->devices[i] != NULL)
2598 break;
2599 /* ignore DMAR unit if no pci devices exist */
2600 if (i == drhd->devices_cnt)
2601 drhd->ignored = 1;
2602 }
2603 }
2604
2605 if (dmar_map_gfx)
2606 return;
2607
2608 for_each_drhd_unit(drhd) {
2609 int i;
2610 if (drhd->ignored || drhd->include_all)
2611 continue;
2612
2613 for (i = 0; i < drhd->devices_cnt; i++)
2614 if (drhd->devices[i] &&
2615 !IS_GFX_DEVICE(drhd->devices[i]))
2616 break;
2617
2618 if (i < drhd->devices_cnt)
2619 continue;
2620
2621 /* bypass IOMMU if it is just for gfx devices */
2622 drhd->ignored = 1;
2623 for (i = 0; i < drhd->devices_cnt; i++) {
2624 if (!drhd->devices[i])
2625 continue;
Keshavamurthy, Anil S358dd8a2007-10-21 16:41:59 -07002626 drhd->devices[i]->dev.archdata.iommu = DUMMY_DEVICE_DOMAIN_INFO;
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002627 }
2628 }
2629}
2630
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002631#ifdef CONFIG_SUSPEND
2632static int init_iommu_hw(void)
2633{
2634 struct dmar_drhd_unit *drhd;
2635 struct intel_iommu *iommu = NULL;
2636
2637 for_each_active_iommu(iommu, drhd)
2638 if (iommu->qi)
2639 dmar_reenable_qi(iommu);
2640
2641 for_each_active_iommu(iommu, drhd) {
2642 iommu_flush_write_buffer(iommu);
2643
2644 iommu_set_root_entry(iommu);
2645
2646 iommu->flush.flush_context(iommu, 0, 0, 0,
2647 DMA_CCMD_GLOBAL_INVL, 0);
2648 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2649 DMA_TLB_GLOBAL_FLUSH, 0);
2650 iommu_disable_protect_mem_regions(iommu);
2651 iommu_enable_translation(iommu);
2652 }
2653
2654 return 0;
2655}
2656
2657static void iommu_flush_all(void)
2658{
2659 struct dmar_drhd_unit *drhd;
2660 struct intel_iommu *iommu;
2661
2662 for_each_active_iommu(iommu, drhd) {
2663 iommu->flush.flush_context(iommu, 0, 0, 0,
2664 DMA_CCMD_GLOBAL_INVL, 0);
2665 iommu->flush.flush_iotlb(iommu, 0, 0, 0,
2666 DMA_TLB_GLOBAL_FLUSH, 0);
2667 }
2668}
2669
2670static int iommu_suspend(struct sys_device *dev, pm_message_t state)
2671{
2672 struct dmar_drhd_unit *drhd;
2673 struct intel_iommu *iommu = NULL;
2674 unsigned long flag;
2675
2676 for_each_active_iommu(iommu, drhd) {
2677 iommu->iommu_state = kzalloc(sizeof(u32) * MAX_SR_DMAR_REGS,
2678 GFP_ATOMIC);
2679 if (!iommu->iommu_state)
2680 goto nomem;
2681 }
2682
2683 iommu_flush_all();
2684
2685 for_each_active_iommu(iommu, drhd) {
2686 iommu_disable_translation(iommu);
2687
2688 spin_lock_irqsave(&iommu->register_lock, flag);
2689
2690 iommu->iommu_state[SR_DMAR_FECTL_REG] =
2691 readl(iommu->reg + DMAR_FECTL_REG);
2692 iommu->iommu_state[SR_DMAR_FEDATA_REG] =
2693 readl(iommu->reg + DMAR_FEDATA_REG);
2694 iommu->iommu_state[SR_DMAR_FEADDR_REG] =
2695 readl(iommu->reg + DMAR_FEADDR_REG);
2696 iommu->iommu_state[SR_DMAR_FEUADDR_REG] =
2697 readl(iommu->reg + DMAR_FEUADDR_REG);
2698
2699 spin_unlock_irqrestore(&iommu->register_lock, flag);
2700 }
2701 return 0;
2702
2703nomem:
2704 for_each_active_iommu(iommu, drhd)
2705 kfree(iommu->iommu_state);
2706
2707 return -ENOMEM;
2708}
2709
2710static int iommu_resume(struct sys_device *dev)
2711{
2712 struct dmar_drhd_unit *drhd;
2713 struct intel_iommu *iommu = NULL;
2714 unsigned long flag;
2715
2716 if (init_iommu_hw()) {
2717 WARN(1, "IOMMU setup failed, DMAR can not resume!\n");
2718 return -EIO;
2719 }
2720
2721 for_each_active_iommu(iommu, drhd) {
2722
2723 spin_lock_irqsave(&iommu->register_lock, flag);
2724
2725 writel(iommu->iommu_state[SR_DMAR_FECTL_REG],
2726 iommu->reg + DMAR_FECTL_REG);
2727 writel(iommu->iommu_state[SR_DMAR_FEDATA_REG],
2728 iommu->reg + DMAR_FEDATA_REG);
2729 writel(iommu->iommu_state[SR_DMAR_FEADDR_REG],
2730 iommu->reg + DMAR_FEADDR_REG);
2731 writel(iommu->iommu_state[SR_DMAR_FEUADDR_REG],
2732 iommu->reg + DMAR_FEUADDR_REG);
2733
2734 spin_unlock_irqrestore(&iommu->register_lock, flag);
2735 }
2736
2737 for_each_active_iommu(iommu, drhd)
2738 kfree(iommu->iommu_state);
2739
2740 return 0;
2741}
2742
2743static struct sysdev_class iommu_sysclass = {
2744 .name = "iommu",
2745 .resume = iommu_resume,
2746 .suspend = iommu_suspend,
2747};
2748
2749static struct sys_device device_iommu = {
2750 .cls = &iommu_sysclass,
2751};
2752
2753static int __init init_iommu_sysfs(void)
2754{
2755 int error;
2756
2757 error = sysdev_class_register(&iommu_sysclass);
2758 if (error)
2759 return error;
2760
2761 error = sysdev_register(&device_iommu);
2762 if (error)
2763 sysdev_class_unregister(&iommu_sysclass);
2764
2765 return error;
2766}
2767
2768#else
2769static int __init init_iommu_sysfs(void)
2770{
2771 return 0;
2772}
2773#endif /* CONFIG_PM */
2774
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002775int __init intel_iommu_init(void)
2776{
2777 int ret = 0;
2778
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002779 if (dmar_table_init())
2780 return -ENODEV;
2781
Suresh Siddha1886e8a2008-07-10 11:16:37 -07002782 if (dmar_dev_scope_init())
2783 return -ENODEV;
2784
Suresh Siddha2ae21012008-07-10 11:16:43 -07002785 /*
2786 * Check the need for DMA-remapping initialization now.
2787 * Above initialization will also be used by Interrupt-remapping.
2788 */
2789 if (no_iommu || swiotlb || dmar_disabled)
2790 return -ENODEV;
2791
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002792 iommu_init_mempool();
2793 dmar_init_reserved_ranges();
2794
2795 init_no_remapping_devices();
2796
2797 ret = init_dmars();
2798 if (ret) {
2799 printk(KERN_ERR "IOMMU: dmar init failed\n");
2800 put_iova_domain(&reserved_iova_list);
2801 iommu_exit_mempool();
2802 return ret;
2803 }
2804 printk(KERN_INFO
2805 "PCI-DMA: Intel(R) Virtualization Technology for Directed I/O\n");
2806
mark gross5e0d2a62008-03-04 15:22:08 -08002807 init_timer(&unmap_timer);
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002808 force_iommu = 1;
2809 dma_ops = &intel_dma_ops;
Fenghua Yuf59c7b62009-03-27 14:22:42 -07002810 init_iommu_sysfs();
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01002811
2812 register_iommu(&intel_iommu_ops);
2813
Keshavamurthy, Anil Sba395922007-10-21 16:41:49 -07002814 return 0;
2815}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -07002816
Weidong Hanc7151a82008-12-08 22:51:37 +08002817static int vm_domain_add_dev_info(struct dmar_domain *domain,
2818 struct pci_dev *pdev)
2819{
2820 struct device_domain_info *info;
2821 unsigned long flags;
2822
2823 info = alloc_devinfo_mem();
2824 if (!info)
2825 return -ENOMEM;
2826
David Woodhouse276dbf992009-04-04 01:45:37 +01002827 info->segment = pci_domain_nr(pdev->bus);
Weidong Hanc7151a82008-12-08 22:51:37 +08002828 info->bus = pdev->bus->number;
2829 info->devfn = pdev->devfn;
2830 info->dev = pdev;
2831 info->domain = domain;
2832
2833 spin_lock_irqsave(&device_domain_lock, flags);
2834 list_add(&info->link, &domain->devices);
2835 list_add(&info->global, &device_domain_list);
2836 pdev->dev.archdata.iommu = info;
2837 spin_unlock_irqrestore(&device_domain_lock, flags);
2838
2839 return 0;
2840}
2841
Han, Weidong3199aa62009-02-26 17:31:12 +08002842static void iommu_detach_dependent_devices(struct intel_iommu *iommu,
2843 struct pci_dev *pdev)
2844{
2845 struct pci_dev *tmp, *parent;
2846
2847 if (!iommu || !pdev)
2848 return;
2849
2850 /* dependent device detach */
2851 tmp = pci_find_upstream_pcie_bridge(pdev);
2852 /* Secondary interface's bus number and devfn 0 */
2853 if (tmp) {
2854 parent = pdev->bus->self;
2855 while (parent != tmp) {
2856 iommu_detach_dev(iommu, parent->bus->number,
David Woodhouse276dbf992009-04-04 01:45:37 +01002857 parent->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002858 parent = parent->bus->self;
2859 }
2860 if (tmp->is_pcie) /* this is a PCIE-to-PCI bridge */
2861 iommu_detach_dev(iommu,
2862 tmp->subordinate->number, 0);
2863 else /* this is a legacy PCI bridge */
David Woodhouse276dbf992009-04-04 01:45:37 +01002864 iommu_detach_dev(iommu, tmp->bus->number,
2865 tmp->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002866 }
2867}
2868
Weidong Hanc7151a82008-12-08 22:51:37 +08002869static void vm_domain_remove_one_dev_info(struct dmar_domain *domain,
2870 struct pci_dev *pdev)
2871{
2872 struct device_domain_info *info;
2873 struct intel_iommu *iommu;
2874 unsigned long flags;
2875 int found = 0;
2876 struct list_head *entry, *tmp;
2877
David Woodhouse276dbf992009-04-04 01:45:37 +01002878 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
2879 pdev->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08002880 if (!iommu)
2881 return;
2882
2883 spin_lock_irqsave(&device_domain_lock, flags);
2884 list_for_each_safe(entry, tmp, &domain->devices) {
2885 info = list_entry(entry, struct device_domain_info, link);
David Woodhouse276dbf992009-04-04 01:45:37 +01002886 /* No need to compare PCI domain; it has to be the same */
Weidong Hanc7151a82008-12-08 22:51:37 +08002887 if (info->bus == pdev->bus->number &&
2888 info->devfn == pdev->devfn) {
2889 list_del(&info->link);
2890 list_del(&info->global);
2891 if (info->dev)
2892 info->dev->dev.archdata.iommu = NULL;
2893 spin_unlock_irqrestore(&device_domain_lock, flags);
2894
2895 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002896 iommu_detach_dependent_devices(iommu, pdev);
Weidong Hanc7151a82008-12-08 22:51:37 +08002897 free_devinfo_mem(info);
2898
2899 spin_lock_irqsave(&device_domain_lock, flags);
2900
2901 if (found)
2902 break;
2903 else
2904 continue;
2905 }
2906
2907 /* if there is no other devices under the same iommu
2908 * owned by this domain, clear this iommu in iommu_bmp
2909 * update iommu count and coherency
2910 */
David Woodhouse276dbf992009-04-04 01:45:37 +01002911 if (iommu == device_to_iommu(info->segment, info->bus,
2912 info->devfn))
Weidong Hanc7151a82008-12-08 22:51:37 +08002913 found = 1;
2914 }
2915
2916 if (found == 0) {
2917 unsigned long tmp_flags;
2918 spin_lock_irqsave(&domain->iommu_lock, tmp_flags);
2919 clear_bit(iommu->seq_id, &domain->iommu_bmp);
2920 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08002921 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08002922 spin_unlock_irqrestore(&domain->iommu_lock, tmp_flags);
2923 }
2924
2925 spin_unlock_irqrestore(&device_domain_lock, flags);
2926}
2927
2928static void vm_domain_remove_all_dev_info(struct dmar_domain *domain)
2929{
2930 struct device_domain_info *info;
2931 struct intel_iommu *iommu;
2932 unsigned long flags1, flags2;
2933
2934 spin_lock_irqsave(&device_domain_lock, flags1);
2935 while (!list_empty(&domain->devices)) {
2936 info = list_entry(domain->devices.next,
2937 struct device_domain_info, link);
2938 list_del(&info->link);
2939 list_del(&info->global);
2940 if (info->dev)
2941 info->dev->dev.archdata.iommu = NULL;
2942
2943 spin_unlock_irqrestore(&device_domain_lock, flags1);
2944
David Woodhouse276dbf992009-04-04 01:45:37 +01002945 iommu = device_to_iommu(info->segment, info->bus, info->devfn);
Weidong Hanc7151a82008-12-08 22:51:37 +08002946 iommu_detach_dev(iommu, info->bus, info->devfn);
Han, Weidong3199aa62009-02-26 17:31:12 +08002947 iommu_detach_dependent_devices(iommu, info->dev);
Weidong Hanc7151a82008-12-08 22:51:37 +08002948
2949 /* clear this iommu in iommu_bmp, update iommu count
Sheng Yang58c610b2009-03-18 15:33:05 +08002950 * and capabilities
Weidong Hanc7151a82008-12-08 22:51:37 +08002951 */
2952 spin_lock_irqsave(&domain->iommu_lock, flags2);
2953 if (test_and_clear_bit(iommu->seq_id,
2954 &domain->iommu_bmp)) {
2955 domain->iommu_count--;
Sheng Yang58c610b2009-03-18 15:33:05 +08002956 domain_update_iommu_cap(domain);
Weidong Hanc7151a82008-12-08 22:51:37 +08002957 }
2958 spin_unlock_irqrestore(&domain->iommu_lock, flags2);
2959
2960 free_devinfo_mem(info);
2961 spin_lock_irqsave(&device_domain_lock, flags1);
2962 }
2963 spin_unlock_irqrestore(&device_domain_lock, flags1);
2964}
2965
Weidong Han5e98c4b2008-12-08 23:03:27 +08002966/* domain id for virtual machine, it won't be set in context */
2967static unsigned long vm_domid;
2968
Weidong Hanfe40f1e2008-12-08 23:10:23 +08002969static int vm_domain_min_agaw(struct dmar_domain *domain)
2970{
2971 int i;
2972 int min_agaw = domain->agaw;
2973
2974 i = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
2975 for (; i < g_num_of_iommus; ) {
2976 if (min_agaw > g_iommus[i]->agaw)
2977 min_agaw = g_iommus[i]->agaw;
2978
2979 i = find_next_bit(&domain->iommu_bmp, g_num_of_iommus, i+1);
2980 }
2981
2982 return min_agaw;
2983}
2984
Weidong Han5e98c4b2008-12-08 23:03:27 +08002985static struct dmar_domain *iommu_alloc_vm_domain(void)
2986{
2987 struct dmar_domain *domain;
2988
2989 domain = alloc_domain_mem();
2990 if (!domain)
2991 return NULL;
2992
2993 domain->id = vm_domid++;
2994 memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
2995 domain->flags = DOMAIN_FLAG_VIRTUAL_MACHINE;
2996
2997 return domain;
2998}
2999
3000static int vm_domain_init(struct dmar_domain *domain, int guest_width)
3001{
3002 int adjust_width;
3003
3004 init_iova_domain(&domain->iovad, DMA_32BIT_PFN);
3005 spin_lock_init(&domain->mapping_lock);
3006 spin_lock_init(&domain->iommu_lock);
3007
3008 domain_reserve_special_ranges(domain);
3009
3010 /* calculate AGAW */
3011 domain->gaw = guest_width;
3012 adjust_width = guestwidth_to_adjustwidth(guest_width);
3013 domain->agaw = width_to_agaw(adjust_width);
3014
3015 INIT_LIST_HEAD(&domain->devices);
3016
3017 domain->iommu_count = 0;
3018 domain->iommu_coherency = 0;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003019 domain->max_addr = 0;
Weidong Han5e98c4b2008-12-08 23:03:27 +08003020
3021 /* always allocate the top pgd */
3022 domain->pgd = (struct dma_pte *)alloc_pgtable_page();
3023 if (!domain->pgd)
3024 return -ENOMEM;
3025 domain_flush_cache(domain, domain->pgd, PAGE_SIZE);
3026 return 0;
3027}
3028
3029static void iommu_free_vm_domain(struct dmar_domain *domain)
3030{
3031 unsigned long flags;
3032 struct dmar_drhd_unit *drhd;
3033 struct intel_iommu *iommu;
3034 unsigned long i;
3035 unsigned long ndomains;
3036
3037 for_each_drhd_unit(drhd) {
3038 if (drhd->ignored)
3039 continue;
3040 iommu = drhd->iommu;
3041
3042 ndomains = cap_ndoms(iommu->cap);
3043 i = find_first_bit(iommu->domain_ids, ndomains);
3044 for (; i < ndomains; ) {
3045 if (iommu->domains[i] == domain) {
3046 spin_lock_irqsave(&iommu->lock, flags);
3047 clear_bit(i, iommu->domain_ids);
3048 iommu->domains[i] = NULL;
3049 spin_unlock_irqrestore(&iommu->lock, flags);
3050 break;
3051 }
3052 i = find_next_bit(iommu->domain_ids, ndomains, i+1);
3053 }
3054 }
3055}
3056
3057static void vm_domain_exit(struct dmar_domain *domain)
3058{
3059 u64 end;
3060
3061 /* Domain 0 is reserved, so dont process it */
3062 if (!domain)
3063 return;
3064
3065 vm_domain_remove_all_dev_info(domain);
3066 /* destroy iovas */
3067 put_iova_domain(&domain->iovad);
3068 end = DOMAIN_MAX_ADDR(domain->gaw);
3069 end = end & (~VTD_PAGE_MASK);
3070
3071 /* clear ptes */
3072 dma_pte_clear_range(domain, 0, end);
3073
3074 /* free page tables */
3075 dma_pte_free_pagetable(domain, 0, end);
3076
3077 iommu_free_vm_domain(domain);
3078 free_domain_mem(domain);
3079}
3080
Joerg Roedel5d450802008-12-03 14:52:32 +01003081static int intel_iommu_domain_init(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003082{
Joerg Roedel5d450802008-12-03 14:52:32 +01003083 struct dmar_domain *dmar_domain;
Kay, Allen M38717942008-09-09 18:37:29 +03003084
Joerg Roedel5d450802008-12-03 14:52:32 +01003085 dmar_domain = iommu_alloc_vm_domain();
3086 if (!dmar_domain) {
Kay, Allen M38717942008-09-09 18:37:29 +03003087 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003088 "intel_iommu_domain_init: dmar_domain == NULL\n");
3089 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003090 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003091 if (vm_domain_init(dmar_domain, DEFAULT_DOMAIN_ADDRESS_WIDTH)) {
Kay, Allen M38717942008-09-09 18:37:29 +03003092 printk(KERN_ERR
Joerg Roedel5d450802008-12-03 14:52:32 +01003093 "intel_iommu_domain_init() failed\n");
3094 vm_domain_exit(dmar_domain);
3095 return -ENOMEM;
Kay, Allen M38717942008-09-09 18:37:29 +03003096 }
Joerg Roedel5d450802008-12-03 14:52:32 +01003097 domain->priv = dmar_domain;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003098
Joerg Roedel5d450802008-12-03 14:52:32 +01003099 return 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003100}
Kay, Allen M38717942008-09-09 18:37:29 +03003101
Joerg Roedel5d450802008-12-03 14:52:32 +01003102static void intel_iommu_domain_destroy(struct iommu_domain *domain)
Kay, Allen M38717942008-09-09 18:37:29 +03003103{
Joerg Roedel5d450802008-12-03 14:52:32 +01003104 struct dmar_domain *dmar_domain = domain->priv;
3105
3106 domain->priv = NULL;
3107 vm_domain_exit(dmar_domain);
Kay, Allen M38717942008-09-09 18:37:29 +03003108}
Kay, Allen M38717942008-09-09 18:37:29 +03003109
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003110static int intel_iommu_attach_device(struct iommu_domain *domain,
3111 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003112{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003113 struct dmar_domain *dmar_domain = domain->priv;
3114 struct pci_dev *pdev = to_pci_dev(dev);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003115 struct intel_iommu *iommu;
3116 int addr_width;
3117 u64 end;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003118 int ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003119
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003120 /* normally pdev is not mapped */
3121 if (unlikely(domain_context_mapped(pdev))) {
3122 struct dmar_domain *old_domain;
3123
3124 old_domain = find_domain(pdev);
3125 if (old_domain) {
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003126 if (dmar_domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003127 vm_domain_remove_one_dev_info(old_domain, pdev);
3128 else
3129 domain_remove_dev_info(old_domain);
3130 }
3131 }
3132
David Woodhouse276dbf992009-04-04 01:45:37 +01003133 iommu = device_to_iommu(pci_domain_nr(pdev->bus), pdev->bus->number,
3134 pdev->devfn);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003135 if (!iommu)
3136 return -ENODEV;
3137
3138 /* check if this iommu agaw is sufficient for max mapped address */
3139 addr_width = agaw_to_width(iommu->agaw);
3140 end = DOMAIN_MAX_ADDR(addr_width);
3141 end = end & VTD_PAGE_MASK;
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003142 if (end < dmar_domain->max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003143 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3144 "sufficient for the mapped address (%llx)\n",
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003145 __func__, iommu->agaw, dmar_domain->max_addr);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003146 return -EFAULT;
3147 }
3148
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003149 ret = domain_context_mapping(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003150 if (ret)
3151 return ret;
3152
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003153 ret = vm_domain_add_dev_info(dmar_domain, pdev);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003154 return ret;
3155}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003156
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003157static void intel_iommu_detach_device(struct iommu_domain *domain,
3158 struct device *dev)
Kay, Allen M38717942008-09-09 18:37:29 +03003159{
Joerg Roedel4c5478c2008-12-03 14:58:24 +01003160 struct dmar_domain *dmar_domain = domain->priv;
3161 struct pci_dev *pdev = to_pci_dev(dev);
3162
3163 vm_domain_remove_one_dev_info(dmar_domain, pdev);
Kay, Allen M38717942008-09-09 18:37:29 +03003164}
Kay, Allen M38717942008-09-09 18:37:29 +03003165
Joerg Roedeldde57a22008-12-03 15:04:09 +01003166static int intel_iommu_map_range(struct iommu_domain *domain,
3167 unsigned long iova, phys_addr_t hpa,
3168 size_t size, int iommu_prot)
Kay, Allen M38717942008-09-09 18:37:29 +03003169{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003170 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003171 u64 max_addr;
3172 int addr_width;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003173 int prot = 0;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003174 int ret;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003175
Joerg Roedeldde57a22008-12-03 15:04:09 +01003176 if (iommu_prot & IOMMU_READ)
3177 prot |= DMA_PTE_READ;
3178 if (iommu_prot & IOMMU_WRITE)
3179 prot |= DMA_PTE_WRITE;
Sheng Yang9cf06692009-03-18 15:33:07 +08003180 if ((iommu_prot & IOMMU_CACHE) && dmar_domain->iommu_snooping)
3181 prot |= DMA_PTE_SNP;
Joerg Roedeldde57a22008-12-03 15:04:09 +01003182
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003183 max_addr = (iova & VTD_PAGE_MASK) + VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003184 if (dmar_domain->max_addr < max_addr) {
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003185 int min_agaw;
3186 u64 end;
3187
3188 /* check if minimum agaw is sufficient for mapped address */
Joerg Roedeldde57a22008-12-03 15:04:09 +01003189 min_agaw = vm_domain_min_agaw(dmar_domain);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003190 addr_width = agaw_to_width(min_agaw);
3191 end = DOMAIN_MAX_ADDR(addr_width);
3192 end = end & VTD_PAGE_MASK;
3193 if (end < max_addr) {
3194 printk(KERN_ERR "%s: iommu agaw (%d) is not "
3195 "sufficient for the mapped address (%llx)\n",
3196 __func__, min_agaw, max_addr);
3197 return -EFAULT;
3198 }
Joerg Roedeldde57a22008-12-03 15:04:09 +01003199 dmar_domain->max_addr = max_addr;
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003200 }
3201
Joerg Roedeldde57a22008-12-03 15:04:09 +01003202 ret = domain_page_mapping(dmar_domain, iova, hpa, size, prot);
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003203 return ret;
Kay, Allen M38717942008-09-09 18:37:29 +03003204}
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003205
Joerg Roedeldde57a22008-12-03 15:04:09 +01003206static void intel_iommu_unmap_range(struct iommu_domain *domain,
3207 unsigned long iova, size_t size)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003208{
Joerg Roedeldde57a22008-12-03 15:04:09 +01003209 struct dmar_domain *dmar_domain = domain->priv;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003210 dma_addr_t base;
3211
3212 /* The address might not be aligned */
3213 base = iova & VTD_PAGE_MASK;
3214 size = VTD_PAGE_ALIGN(size);
Joerg Roedeldde57a22008-12-03 15:04:09 +01003215 dma_pte_clear_range(dmar_domain, base, base + size);
Weidong Hanfe40f1e2008-12-08 23:10:23 +08003216
Joerg Roedeldde57a22008-12-03 15:04:09 +01003217 if (dmar_domain->max_addr == base + size)
3218 dmar_domain->max_addr = base;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003219}
Kay, Allen M38717942008-09-09 18:37:29 +03003220
Joerg Roedeld14d6572008-12-03 15:06:57 +01003221static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,
3222 unsigned long iova)
Kay, Allen M38717942008-09-09 18:37:29 +03003223{
Joerg Roedeld14d6572008-12-03 15:06:57 +01003224 struct dmar_domain *dmar_domain = domain->priv;
Kay, Allen M38717942008-09-09 18:37:29 +03003225 struct dma_pte *pte;
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003226 u64 phys = 0;
Kay, Allen M38717942008-09-09 18:37:29 +03003227
Joerg Roedeld14d6572008-12-03 15:06:57 +01003228 pte = addr_to_dma_pte(dmar_domain, iova);
Kay, Allen M38717942008-09-09 18:37:29 +03003229 if (pte)
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003230 phys = dma_pte_addr(pte);
Kay, Allen M38717942008-09-09 18:37:29 +03003231
Weidong Hanfaa3d6f2008-12-08 23:09:29 +08003232 return phys;
Kay, Allen M38717942008-09-09 18:37:29 +03003233}
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003234
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003235static int intel_iommu_domain_has_cap(struct iommu_domain *domain,
3236 unsigned long cap)
3237{
3238 struct dmar_domain *dmar_domain = domain->priv;
3239
3240 if (cap == IOMMU_CAP_CACHE_COHERENCY)
3241 return dmar_domain->iommu_snooping;
3242
3243 return 0;
3244}
3245
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003246static struct iommu_ops intel_iommu_ops = {
3247 .domain_init = intel_iommu_domain_init,
3248 .domain_destroy = intel_iommu_domain_destroy,
3249 .attach_dev = intel_iommu_attach_device,
3250 .detach_dev = intel_iommu_detach_device,
3251 .map = intel_iommu_map_range,
3252 .unmap = intel_iommu_unmap_range,
3253 .iova_to_phys = intel_iommu_iova_to_phys,
Sheng Yangdbb9fd82009-03-18 15:33:06 +08003254 .domain_has_cap = intel_iommu_domain_has_cap,
Joerg Roedela8bcbb0d2008-12-03 15:14:02 +01003255};
David Woodhouse9af88142009-02-13 23:18:03 +00003256
3257static void __devinit quirk_iommu_rwbf(struct pci_dev *dev)
3258{
3259 /*
3260 * Mobile 4 Series Chipset neglects to set RWBF capability,
3261 * but needs it:
3262 */
3263 printk(KERN_INFO "DMAR: Forcing write-buffer flush capability\n");
3264 rwbf_quirk = 1;
3265}
3266
3267DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x2a40, quirk_iommu_rwbf);