intel-iommu.c 99.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Copyright (c) 2006, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
17
18
19
20
 * Copyright (C) 2006-2008 Intel Corporation
 * Author: Ashok Raj <ashok.raj@intel.com>
 * Author: Shaohua Li <shaohua.li@intel.com>
 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Fenghua Yu's avatar
Fenghua Yu committed
21
 * Author: Fenghua Yu <fenghua.yu@intel.com>
22
23
24
25
 */

#include <linux/init.h>
#include <linux/bitmap.h>
mark gross's avatar
mark gross committed
26
#include <linux/debugfs.h>
27
28
29
30
31
32
33
34
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/pci.h>
#include <linux/dmar.h>
#include <linux/dma-mapping.h>
#include <linux/mempool.h>
mark gross's avatar
mark gross committed
35
#include <linux/timer.h>
Kay, Allen M's avatar
Kay, Allen M committed
36
#include <linux/iova.h>
37
#include <linux/iommu.h>
Kay, Allen M's avatar
Kay, Allen M committed
38
#include <linux/intel-iommu.h>
39
#include <linux/syscore_ops.h>
40
#include <linux/tboot.h>
41
#include <linux/dmi.h>
42
#include <linux/pci-ats.h>
43
#include <asm/cacheflush.h>
44
#include <asm/iommu.h>
45

Fenghua Yu's avatar
Fenghua Yu committed
46
47
48
#define ROOT_SIZE		VTD_PAGE_SIZE
#define CONTEXT_SIZE		VTD_PAGE_SIZE

49
50
#define IS_BRIDGE_HOST_DEVICE(pdev) \
			    ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
51
52
#define IS_GFX_DEVICE(pdev) ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY)
#define IS_ISA_DEVICE(pdev) ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
53
#define IS_AZALIA(pdev) ((pdev)->vendor == 0x8086 && (pdev)->device == 0x3a3e)
54
55
56
57
58
59
60

#define IOAPIC_RANGE_START	(0xfee00000)
#define IOAPIC_RANGE_END	(0xfeefffff)
#define IOVA_START_ADDR		(0x1000)

#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48

Fenghua Yu's avatar
Fenghua Yu committed
61
62
#define MAX_AGAW_WIDTH 64

63
64
65
66
67
68
69
70
#define __DOMAIN_MAX_PFN(gaw)  ((((uint64_t)1) << (gaw-VTD_PAGE_SHIFT)) - 1)
#define __DOMAIN_MAX_ADDR(gaw) ((((uint64_t)1) << gaw) - 1)

/* We limit DOMAIN_MAX_PFN to fit in an unsigned long, and DOMAIN_MAX_ADDR
   to match. That way, we can use 'unsigned long' for PFNs with impunity. */
#define DOMAIN_MAX_PFN(gaw)	((unsigned long) min_t(uint64_t, \
				__DOMAIN_MAX_PFN(gaw), (unsigned long)-1))
#define DOMAIN_MAX_ADDR(gaw)	(((uint64_t)__DOMAIN_MAX_PFN(gaw)) << VTD_PAGE_SHIFT)
71

72
#define IOVA_PFN(addr)		((addr) >> PAGE_SHIFT)
73
#define DMA_32BIT_PFN		IOVA_PFN(DMA_BIT_MASK(32))
74
#define DMA_64BIT_PFN		IOVA_PFN(DMA_BIT_MASK(64))
mark gross's avatar
mark gross committed
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* page table handling */
#define LEVEL_STRIDE		(9)
#define LEVEL_MASK		(((u64)1 << LEVEL_STRIDE) - 1)

static inline int agaw_to_level(int agaw)
{
	return agaw + 2;
}

static inline int agaw_to_width(int agaw)
{
	return 30 + agaw * LEVEL_STRIDE;
}

static inline int width_to_agaw(int width)
{
	return (width - 30) / LEVEL_STRIDE;
}

static inline unsigned int level_to_offset_bits(int level)
{
	return (level - 1) * LEVEL_STRIDE;
}

static inline int pfn_level_offset(unsigned long pfn, int level)
{
	return (pfn >> level_to_offset_bits(level)) & LEVEL_MASK;
}

static inline unsigned long level_mask(int level)
{
	return -1UL << level_to_offset_bits(level);
}

static inline unsigned long level_size(int level)
{
	return 1UL << level_to_offset_bits(level);
}

static inline unsigned long align_to_level(unsigned long pfn, int level)
{
	return (pfn + level_size(level) - 1) & level_mask(level);
}
119

120
121
122
123
124
static inline unsigned long lvl_to_nr_pages(unsigned int lvl)
{
	return  1 << ((lvl - 1) * LEVEL_STRIDE);
}

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* VT-d pages must always be _smaller_ than MM pages. Otherwise things
   are never going to work. */
static inline unsigned long dma_to_mm_pfn(unsigned long dma_pfn)
{
	return dma_pfn >> (PAGE_SHIFT - VTD_PAGE_SHIFT);
}

static inline unsigned long mm_to_dma_pfn(unsigned long mm_pfn)
{
	return mm_pfn << (PAGE_SHIFT - VTD_PAGE_SHIFT);
}
static inline unsigned long page_to_dma_pfn(struct page *pg)
{
	return mm_to_dma_pfn(page_to_pfn(pg));
}
static inline unsigned long virt_to_dma_pfn(void *p)
{
	return page_to_dma_pfn(virt_to_page(p));
}

Weidong Han's avatar
Weidong Han committed
145
146
147
/* global iommu list, set NULL for ignored DMAR units */
static struct intel_iommu **g_iommus;

148
static void __init check_tylersburg_isoch(void);
149
150
static int rwbf_quirk;

151
152
153
154
155
156
/*
 * set to 1 to panic kernel if can't successfully enable VT-d
 * (used when kernel is launched w/ TXT)
 */
static int force_on = 0;

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * 0: Present
 * 1-11: Reserved
 * 12-63: Context Ptr (12 - (haw-1))
 * 64-127: Reserved
 */
struct root_entry {
	u64	val;
	u64	rsvd1;
};
#define ROOT_ENTRY_NR (VTD_PAGE_SIZE/sizeof(struct root_entry))
static inline bool root_present(struct root_entry *root)
{
	return (root->val & 1);
}
static inline void set_root_present(struct root_entry *root)
{
	root->val |= 1;
}
static inline void set_root_value(struct root_entry *root, unsigned long value)
{
	root->val |= value & VTD_PAGE_MASK;
}

static inline struct context_entry *
get_context_addr_from_root(struct root_entry *root)
{
	return (struct context_entry *)
		(root_present(root)?phys_to_virt(
		root->val & VTD_PAGE_MASK) :
		NULL);
}

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * low 64 bits:
 * 0: present
 * 1: fault processing disable
 * 2-3: translation type
 * 12-63: address space root
 * high 64 bits:
 * 0-2: address width
 * 3-6: aval
 * 8-23: domain id
 */
struct context_entry {
	u64 lo;
	u64 hi;
};
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

static inline bool context_present(struct context_entry *context)
{
	return (context->lo & 1);
}
static inline void context_set_present(struct context_entry *context)
{
	context->lo |= 1;
}

static inline void context_set_fault_enable(struct context_entry *context)
{
	context->lo &= (((u64)-1) << 2) | 1;
}

static inline void context_set_translation_type(struct context_entry *context,
						unsigned long value)
{
	context->lo &= (((u64)-1) << 4) | 3;
	context->lo |= (value & 3) << 2;
}

static inline void context_set_address_root(struct context_entry *context,
					    unsigned long value)
{
	context->lo |= value & VTD_PAGE_MASK;
}

static inline void context_set_address_width(struct context_entry *context,
					     unsigned long value)
{
	context->hi |= value & 7;
}

static inline void context_set_domain_id(struct context_entry *context,
					 unsigned long value)
{
	context->hi |= (value & ((1 << 16) - 1)) << 8;
}

static inline void context_clear_entry(struct context_entry *context)
{
	context->lo = 0;
	context->hi = 0;
}
250

251
252
253
254
255
/*
 * 0: readable
 * 1: writable
 * 2-6: reserved
 * 7: super page
256
257
 * 8-10: available
 * 11: snoop behavior
258
259
260
261
262
263
 * 12-63: Host physcial address
 */
struct dma_pte {
	u64 val;
};

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
static inline void dma_clear_pte(struct dma_pte *pte)
{
	pte->val = 0;
}

static inline void dma_set_pte_readable(struct dma_pte *pte)
{
	pte->val |= DMA_PTE_READ;
}

static inline void dma_set_pte_writable(struct dma_pte *pte)
{
	pte->val |= DMA_PTE_WRITE;
}

279
280
281
282
283
static inline void dma_set_pte_snp(struct dma_pte *pte)
{
	pte->val |= DMA_PTE_SNP;
}

284
285
286
287
288
289
290
static inline void dma_set_pte_prot(struct dma_pte *pte, unsigned long prot)
{
	pte->val = (pte->val & ~3) | (prot & 3);
}

static inline u64 dma_pte_addr(struct dma_pte *pte)
{
291
292
293
294
#ifdef CONFIG_64BIT
	return pte->val & VTD_PAGE_MASK;
#else
	/* Must have a full atomic 64-bit read */
295
	return  __cmpxchg64(&pte->val, 0ULL, 0ULL) & VTD_PAGE_MASK;
296
#endif
297
298
}

299
static inline void dma_set_pte_pfn(struct dma_pte *pte, unsigned long pfn)
300
{
301
	pte->val |= (uint64_t)pfn << VTD_PAGE_SHIFT;
302
303
304
305
306
307
}

static inline bool dma_pte_present(struct dma_pte *pte)
{
	return (pte->val & 3) != 0;
}
308

309
310
311
312
313
static inline int first_pte_in_page(struct dma_pte *pte)
{
	return !((unsigned long)pte & ~VTD_PAGE_MASK);
}

314
315
316
317
318
319
/*
 * This domain is a statically identity mapping domain.
 *	1. This domain creats a static 1:1 mapping to all usable memory.
 * 	2. It maps to each iommu if successful.
 *	3. Each iommu mapps to this domain if successful.
 */
320
321
static struct dmar_domain *si_domain;
static int hw_pass_through = 1;
322

Weidong Han's avatar
Weidong Han committed
323
/* devices under the same p2p bridge are owned in one domain */
324
#define DOMAIN_FLAG_P2P_MULTIPLE_DEVICES (1 << 0)
Weidong Han's avatar
Weidong Han committed
325

326
327
328
329
330
/* domain represents a virtual machine, more than one devices
 * across iommus may be owned in one domain, e.g. kvm guest.
 */
#define DOMAIN_FLAG_VIRTUAL_MACHINE	(1 << 1)

331
332
333
/* si_domain contains mulitple devices */
#define DOMAIN_FLAG_STATIC_IDENTITY	(1 << 2)

334
335
struct dmar_domain {
	int	id;			/* domain id */
336
	int	nid;			/* node id */
337
	unsigned long iommu_bmp;	/* bitmap of iommus this domain uses*/
338
339
340
341
342
343
344
345
346
347

	struct list_head devices; 	/* all devices' list */
	struct iova_domain iovad;	/* iova's that belong to this domain */

	struct dma_pte	*pgd;		/* virtual address */
	int		gaw;		/* max guest address width */

	/* adjusted guest address width, 0 is level 2 30-bit */
	int		agaw;

Weidong Han's avatar
Weidong Han committed
348
	int		flags;		/* flags to find out type of domain */
Weidong Han's avatar
Weidong Han committed
349
350

	int		iommu_coherency;/* indicate coherency of iommu access */
351
	int		iommu_snooping; /* indicate snooping control feature*/
352
	int		iommu_count;	/* reference count of iommu */
353
354
355
	int		iommu_superpage;/* Level of superpages supported:
					   0 == 4KiB (no superpages), 1 == 2MiB,
					   2 == 1GiB, 3 == 512GiB, 4 == 1TiB */
356
	spinlock_t	iommu_lock;	/* protect iommu set in domain */
357
	u64		max_addr;	/* maximum mapped address */
358
359
};

360
361
362
363
/* PCI domain-device relationship */
struct device_domain_info {
	struct list_head link;	/* link to domain siblings */
	struct list_head global; /* link to global list */
364
365
	int segment;		/* PCI domain */
	u8 bus;			/* PCI bus number */
366
	u8 devfn;		/* PCI devfn number */
367
	struct pci_dev *dev; /* it's NULL for PCIe-to-PCI bridge */
Yu Zhao's avatar
Yu Zhao committed
368
	struct intel_iommu *iommu; /* IOMMU used by this device */
369
370
371
	struct dmar_domain *domain; /* pointer to domain */
};

mark gross's avatar
mark gross committed
372
373
374
375
static void flush_unmaps_timeout(unsigned long data);

DEFINE_TIMER(unmap_timer,  flush_unmaps_timeout, 0, 0);

376
377
378
379
380
381
382
383
384
#define HIGH_WATER_MARK 250
struct deferred_flush_tables {
	int next;
	struct iova *iova[HIGH_WATER_MARK];
	struct dmar_domain *domain[HIGH_WATER_MARK];
};

static struct deferred_flush_tables *deferred_flush;

mark gross's avatar
mark gross committed
385
386
387
388
389
390
391
392
393
/* bitmap for indexing intel_iommus */
static int g_num_of_iommus;

static DEFINE_SPINLOCK(async_umap_flush_lock);
static LIST_HEAD(unmaps_to_do);

static int timer_on;
static long list_size;

394
395
static void domain_remove_dev_info(struct dmar_domain *domain);

396
397
398
399
400
401
#ifdef CONFIG_DMAR_DEFAULT_ON
int dmar_disabled = 0;
#else
int dmar_disabled = 1;
#endif /*CONFIG_DMAR_DEFAULT_ON*/

402
static int dmar_map_gfx = 1;
403
static int dmar_forcedac;
mark gross's avatar
mark gross committed
404
static int intel_iommu_strict;
405
static int intel_iommu_superpage = 1;
406

407
408
409
int intel_iommu_gfx_mapped;
EXPORT_SYMBOL_GPL(intel_iommu_gfx_mapped);

410
411
412
413
#define DUMMY_DEVICE_DOMAIN_INFO ((struct device_domain_info *)(-1))
static DEFINE_SPINLOCK(device_domain_lock);
static LIST_HEAD(device_domain_list);

414
415
static struct iommu_ops intel_iommu_ops;

416
417
418
419
420
static int __init intel_iommu_setup(char *str)
{
	if (!str)
		return -EINVAL;
	while (*str) {
421
422
423
424
		if (!strncmp(str, "on", 2)) {
			dmar_disabled = 0;
			printk(KERN_INFO "Intel-IOMMU: enabled\n");
		} else if (!strncmp(str, "off", 3)) {
425
			dmar_disabled = 1;
426
			printk(KERN_INFO "Intel-IOMMU: disabled\n");
427
428
429
430
		} else if (!strncmp(str, "igfx_off", 8)) {
			dmar_map_gfx = 0;
			printk(KERN_INFO
				"Intel-IOMMU: disable GFX device mapping\n");
431
		} else if (!strncmp(str, "forcedac", 8)) {
mark gross's avatar
mark gross committed
432
			printk(KERN_INFO
433
434
				"Intel-IOMMU: Forcing DAC for PCI devices\n");
			dmar_forcedac = 1;
mark gross's avatar
mark gross committed
435
436
437
438
		} else if (!strncmp(str, "strict", 6)) {
			printk(KERN_INFO
				"Intel-IOMMU: disable batched IOTLB flush\n");
			intel_iommu_strict = 1;
439
440
441
442
		} else if (!strncmp(str, "sp_off", 6)) {
			printk(KERN_INFO
				"Intel-IOMMU: disable supported super page\n");
			intel_iommu_superpage = 0;
443
444
445
446
447
448
449
450
451
452
453
454
455
456
		}

		str += strcspn(str, ",");
		while (*str == ',')
			str++;
	}
	return 0;
}
__setup("intel_iommu=", intel_iommu_setup);

static struct kmem_cache *iommu_domain_cache;
static struct kmem_cache *iommu_devinfo_cache;
static struct kmem_cache *iommu_iova_cache;

457
static inline void *alloc_pgtable_page(int node)
458
{
459
460
	struct page *page;
	void *vaddr = NULL;
461

462
463
464
	page = alloc_pages_node(node, GFP_ATOMIC | __GFP_ZERO, 0);
	if (page)
		vaddr = page_address(page);
465
	return vaddr;
466
467
468
469
470
471
472
473
474
}

static inline void free_pgtable_page(void *vaddr)
{
	free_page((unsigned long)vaddr);
}

static inline void *alloc_domain_mem(void)
{
475
	return kmem_cache_alloc(iommu_domain_cache, GFP_ATOMIC);
476
477
}

Kay, Allen M's avatar
Kay, Allen M committed
478
static void free_domain_mem(void *vaddr)
479
480
481
482
483
484
{
	kmem_cache_free(iommu_domain_cache, vaddr);
}

static inline void * alloc_devinfo_mem(void)
{
485
	return kmem_cache_alloc(iommu_devinfo_cache, GFP_ATOMIC);
486
487
488
489
490
491
492
493
494
}

static inline void free_devinfo_mem(void *vaddr)
{
	kmem_cache_free(iommu_devinfo_cache, vaddr);
}

struct iova *alloc_iova_mem(void)
{
495
	return kmem_cache_alloc(iommu_iova_cache, GFP_ATOMIC);
496
497
498
499
500
501
502
}

void free_iova_mem(struct iova *iova)
{
	kmem_cache_free(iommu_iova_cache, iova);
}

Weidong Han's avatar
Weidong Han committed
503

Fenghua Yu's avatar
Fenghua Yu committed
504
static int __iommu_calculate_agaw(struct intel_iommu *iommu, int max_gaw)
Weidong Han's avatar
Weidong Han committed
505
506
507
508
509
{
	unsigned long sagaw;
	int agaw = -1;

	sagaw = cap_sagaw(iommu->cap);
Fenghua Yu's avatar
Fenghua Yu committed
510
	for (agaw = width_to_agaw(max_gaw);
Weidong Han's avatar
Weidong Han committed
511
512
513
514
515
516
517
518
	     agaw >= 0; agaw--) {
		if (test_bit(agaw, &sagaw))
			break;
	}

	return agaw;
}

Fenghua Yu's avatar
Fenghua Yu committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * Calculate max SAGAW for each iommu.
 */
int iommu_calculate_max_sagaw(struct intel_iommu *iommu)
{
	return __iommu_calculate_agaw(iommu, MAX_AGAW_WIDTH);
}

/*
 * calculate agaw for each iommu.
 * "SAGAW" may be different across iommus, use a default agaw, and
 * get a supported less agaw for iommus that don't support the default agaw.
 */
int iommu_calculate_agaw(struct intel_iommu *iommu)
{
	return __iommu_calculate_agaw(iommu, DEFAULT_DOMAIN_ADDRESS_WIDTH);
}

537
/* This functionin only returns single iommu in a domain */
538
539
540
541
static struct intel_iommu *domain_get_iommu(struct dmar_domain *domain)
{
	int iommu_id;

542
	/* si_domain and vm domain should not get here. */
543
	BUG_ON(domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE);
544
	BUG_ON(domain->flags & DOMAIN_FLAG_STATIC_IDENTITY);
545

546
547
548
549
550
551
552
	iommu_id = find_first_bit(&domain->iommu_bmp, g_num_of_iommus);
	if (iommu_id < 0 || iommu_id >= g_num_of_iommus)
		return NULL;

	return g_iommus[iommu_id];
}

Weidong Han's avatar
Weidong Han committed
553
554
555
556
557
558
static void domain_update_iommu_coherency(struct dmar_domain *domain)
{
	int i;

	domain->iommu_coherency = 1;

559
	for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) {
Weidong Han's avatar
Weidong Han committed
560
561
562
563
564
565
566
		if (!ecap_coherent(g_iommus[i]->ecap)) {
			domain->iommu_coherency = 0;
			break;
		}
	}
}

567
568
569
570
571
572
static void domain_update_iommu_snooping(struct dmar_domain *domain)
{
	int i;

	domain->iommu_snooping = 1;

573
	for_each_set_bit(i, &domain->iommu_bmp, g_num_of_iommus) {
574
575
576
577
578
579
580
		if (!ecap_sc_support(g_iommus[i]->ecap)) {
			domain->iommu_snooping = 0;
			break;
		}
	}
}

581
582
static void domain_update_iommu_superpage(struct dmar_domain *domain)
{
583
584
585
	struct dmar_drhd_unit *drhd;
	struct intel_iommu *iommu = NULL;
	int mask = 0xf;
586
587
588
589
590
591

	if (!intel_iommu_superpage) {
		domain->iommu_superpage = 0;
		return;
	}

592
593
594
	/* set iommu_superpage to the smallest common denominator */
	for_each_active_iommu(iommu, drhd) {
		mask &= cap_super_page_val(iommu->cap);
595
596
597
598
599
600
601
		if (!mask) {
			break;
		}
	}
	domain->iommu_superpage = fls(mask);
}

602
603
604
605
606
/* Some capabilities may be different across iommus */
static void domain_update_iommu_cap(struct dmar_domain *domain)
{
	domain_update_iommu_coherency(domain);
	domain_update_iommu_snooping(domain);
607
	domain_update_iommu_superpage(domain);
608
609
}

610
static struct intel_iommu *device_to_iommu(int segment, u8 bus, u8 devfn)
611
612
613
614
615
616
617
{
	struct dmar_drhd_unit *drhd = NULL;
	int i;

	for_each_drhd_unit(drhd) {
		if (drhd->ignored)
			continue;
618
619
		if (segment != drhd->segment)
			continue;
620

621
		for (i = 0; i < drhd->devices_cnt; i++) {
Dirk Hohndel's avatar
Dirk Hohndel committed
622
623
			if (drhd->devices[i] &&
			    drhd->devices[i]->bus->number == bus &&
624
625
			    drhd->devices[i]->devfn == devfn)
				return drhd->iommu;
626
627
			if (drhd->devices[i] &&
			    drhd->devices[i]->subordinate &&
628
629
630
631
			    drhd->devices[i]->subordinate->number <= bus &&
			    drhd->devices[i]->subordinate->subordinate >= bus)
				return drhd->iommu;
		}
632
633
634
635
636
637
638
639

		if (drhd->include_all)
			return drhd->iommu;
	}

	return NULL;
}

Weidong Han's avatar
Weidong Han committed
640
641
642
643
644
645
646
static void domain_flush_cache(struct dmar_domain *domain,
			       void *addr, int size)
{
	if (!domain->iommu_coherency)
		clflush_cache_range(addr, size);
}

647
648
649
650
651
652
653
654
655
656
657
658
659
/* Gets context entry for a given bus and devfn */
static struct context_entry * device_to_context_entry(struct intel_iommu *iommu,
		u8 bus, u8 devfn)
{
	struct root_entry *root;
	struct context_entry *context;
	unsigned long phy_addr;
	unsigned long flags;

	spin_lock_irqsave(&iommu->lock, flags);
	root = &iommu->root_entry[bus];
	context = get_context_addr_from_root(root);
	if (!context) {
660
661
		context = (struct context_entry *)
				alloc_pgtable_page(iommu->node);
662
663
664
665
		if (!context) {
			spin_unlock_irqrestore(&iommu->lock, flags);
			return NULL;
		}
Fenghua Yu's avatar
Fenghua Yu committed
666
		__iommu_flush_cache(iommu, (void *)context, CONTEXT_SIZE);
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
		phy_addr = virt_to_phys((void *)context);
		set_root_value(root, phy_addr);
		set_root_present(root);
		__iommu_flush_cache(iommu, root, sizeof(*root));
	}
	spin_unlock_irqrestore(&iommu->lock, flags);
	return &context[devfn];
}

static int device_context_mapped(struct intel_iommu *iommu, u8 bus, u8 devfn)
{
	struct root_entry *root;
	struct context_entry *context;
	int ret;
	unsigned long flags;

	spin_lock_irqsave(&iommu->lock, flags);
	root = &iommu->root_entry[bus];
	context = get_context_addr_from_root(root);
	if (!context) {
		ret = 0;
		goto out;
	}
690
	ret = context_present(&context[devfn]);
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
out:
	spin_unlock_irqrestore(&iommu->lock, flags);
	return ret;
}

static void clear_context_table(struct intel_iommu *iommu, u8 bus, u8 devfn)
{
	struct root_entry *root;
	struct context_entry *context;
	unsigned long flags;

	spin_lock_irqsave(&iommu->lock, flags);
	root = &iommu->root_entry[bus];
	context = get_context_addr_from_root(root);
	if (context) {
706
		context_clear_entry(&context[devfn]);
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
		__iommu_flush_cache(iommu, &context[devfn], \
			sizeof(*context));
	}
	spin_unlock_irqrestore(&iommu->lock, flags);
}

static void free_context_table(struct intel_iommu *iommu)
{
	struct root_entry *root;
	int i;
	unsigned long flags;
	struct context_entry *context;

	spin_lock_irqsave(&iommu->lock, flags);
	if (!iommu->root_entry) {
		goto out;
	}
	for (i = 0; i < ROOT_ENTRY_NR; i++) {
		root = &iommu->root_entry[i];
		context = get_context_addr_from_root(root);
		if (context)
			free_pgtable_page(context);
	}
	free_pgtable_page(iommu->root_entry);
	iommu->root_entry = NULL;
out:
	spin_unlock_irqrestore(&iommu->lock, flags);
}

736
static struct dma_pte *pfn_to_dma_pte(struct dmar_domain *domain,
737
				      unsigned long pfn, int large_level)
738
{
739
	int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
740
741
	struct dma_pte *parent, *pte = NULL;
	int level = agaw_to_level(domain->agaw);
742
	int offset, target_level;
743
744

	BUG_ON(!domain->pgd);
745
	BUG_ON(addr_width < BITS_PER_LONG && pfn >> addr_width);
746
747
	parent = domain->pgd;

748
749
750
751
752
753
	/* Search pte */
	if (!large_level)
		target_level = 1;
	else
		target_level = large_level;

754
755
756
	while (level > 0) {
		void *tmp_page;

757
		offset = pfn_level_offset(pfn, level);
758
		pte = &parent[offset];
759
760
761
		if (!large_level && (pte->val & DMA_PTE_LARGE_PAGE))
			break;
		if (level == target_level)
762
763
			break;

764
		if (!dma_pte_present(pte)) {
765
766
			uint64_t pteval;

767
			tmp_page = alloc_pgtable_page(domain->nid);
768

769
			if (!tmp_page)
770
				return NULL;
771

772
			domain_flush_cache(domain, tmp_page, VTD_PAGE_SIZE);
773
			pteval = ((uint64_t)virt_to_dma_pfn(tmp_page) << VTD_PAGE_SHIFT) | DMA_PTE_READ | DMA_PTE_WRITE;
774
775
776
777
778
779
780
			if (cmpxchg64(&pte->val, 0ULL, pteval)) {
				/* Someone else set it while we were thinking; use theirs. */
				free_pgtable_page(tmp_page);
			} else {
				dma_pte_addr(pte);
				domain_flush_cache(domain, pte, sizeof(*pte));
			}
781
		}
782
		parent = phys_to_virt(dma_pte_addr(pte));
783
784
785
786
787
788
		level--;
	}

	return pte;
}

789

790
/* return address's pte at specific level */
791
792
static struct dma_pte *dma_pfn_level_pte(struct dmar_domain *domain,
					 unsigned long pfn,
793
					 int level, int *large_page)
794
795
796
797
798
799
800
{
	struct dma_pte *parent, *pte = NULL;
	int total = agaw_to_level(domain->agaw);
	int offset;

	parent = domain->pgd;
	while (level <= total) {
801
		offset = pfn_level_offset(pfn, total);
802
803
804
805
		pte = &parent[offset];
		if (level == total)
			return pte;

806
807
		if (!dma_pte_present(pte)) {
			*large_page = total;
808
			break;
809
810
811
812
813
814
815
		}

		if (pte->val & DMA_PTE_LARGE_PAGE) {
			*large_page = total;
			return pte;
		}

816
		parent = phys_to_virt(dma_pte_addr(pte));
817
818
819
820
821
822
		total--;
	}
	return NULL;
}

/* clear last level pte, a tlb flush should be followed */
823
static int dma_pte_clear_range(struct dmar_domain *domain,
824
825
				unsigned long start_pfn,
				unsigned long last_pfn)
826
{
827
	int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
828
	unsigned int large_page = 1;
829
	struct dma_pte *first_pte, *pte;
830
	int order;
831

832
	BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
833
	BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
834
	BUG_ON(start_pfn > last_pfn);
835

836
	/* we don't need lock here; nobody else touches the iova range */
837
	do {
838
839
		large_page = 1;
		first_pte = pte = dma_pfn_level_pte(domain, start_pfn, 1, &large_page);
840
		if (!pte) {
841
			start_pfn = align_to_level(start_pfn + 1, large_page + 1);
842
843
			continue;
		}
844
		do {
845
			dma_clear_pte(pte);
846
			start_pfn += lvl_to_nr_pages(large_page);
847
			pte++;
848
849
		} while (start_pfn <= last_pfn && !first_pte_in_page(pte));

850
851
		domain_flush_cache(domain, first_pte,
				   (void *)pte - (void *)first_pte);
852
853

	} while (start_pfn && start_pfn <= last_pfn);
854
855
856

	order = (large_page - 1) * 9;
	return order;
857
858
859
860
}

/* free page table pages. last level pte should already be cleared */
static void dma_pte_free_pagetable(struct dmar_domain *domain,
861
862
				   unsigned long start_pfn,
				   unsigned long last_pfn)
863
{
864
	int addr_width = agaw_to_width(domain->agaw) - VTD_PAGE_SHIFT;
865
	struct dma_pte *first_pte, *pte;
866
867
	int total = agaw_to_level(domain->agaw);
	int level;
868
	unsigned long tmp;
869
	int large_page = 2;
870

871
872
	BUG_ON(addr_width < BITS_PER_LONG && start_pfn >> addr_width);
	BUG_ON(addr_width < BITS_PER_LONG && last_pfn >> addr_width);
873
	BUG_ON(start_pfn > last_pfn);
874

875
	/* We don't need lock here; nobody else touches the iova range */
876
877
	level = 2;
	while (level <= total) {
878
879
		tmp = align_to_level(start_pfn, level);

880
		/* If we can't even clear one PTE at this level, we're done */
881
		if (tmp + level_size(level) - 1 > last_pfn)
882
883
			return;

884
		do {
885
886
887
888
			large_page = level;
			first_pte = pte = dma_pfn_level_pte(domain, tmp, level, &large_page);
			if (large_page > level)
				level = large_page + 1;
889
890
891
892
			if (!pte) {
				tmp = align_to_level(tmp + 1, level + 1);
				continue;
			}
893
			do {
894
895
896
897
				if (dma_pte_present(pte)) {
					free_pgtable_page(phys_to_virt(dma_pte_addr(pte)));
					dma_clear_pte(pte);
				}
898
899
				pte++;
				tmp += level_size(level);
900
901
902
			} while (!first_pte_in_page(pte) &&
				 tmp + level_size(level) - 1 <= last_pfn);

903
904
905
			domain_flush_cache(domain, first_pte,
					   (void *)pte - (void *)first_pte);
			
906
		} while (tmp && tmp + level_size(level) - 1 <= last_pfn);
907
908
909
		level++;
	}
	/* free pgd */
910
	if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) {
911
912
913
914
915
916
917
918
919
920
921
		free_pgtable_page(domain->pgd);
		domain->pgd = NULL;
	}
}

/* iommu handling */
static int iommu_alloc_root_entry(struct intel_iommu *iommu)
{
	struct root_entry *root;
	unsigned long flags;

922
	root = (struct root_entry *)alloc_pgtable_page(iommu->node);
923
924
925
	if (!root)
		return -ENOMEM;

Fenghua Yu's avatar
Fenghua Yu committed
926
	__iommu_flush_cache(iommu, root, ROOT_SIZE);
927
928
929
930
931
932
933
934
935
936
937

	spin_lock_irqsave(&iommu->lock, flags);
	iommu->root_entry = root;
	spin_unlock_irqrestore(&iommu->lock, flags);

	return 0;
}

static void iommu_set_root_entry(struct intel_iommu *iommu)
{
	void *addr;
938
	u32 sts;
939
940
941
942
943
944
945
	unsigned long flag;

	addr = iommu->root_entry;

	spin_lock_irqsave(&iommu->register_lock, flag);
	dmar_writeq(iommu->reg + DMAR_RTADDR_REG, virt_to_phys(addr));

946
	writel(iommu->gcmd | DMA_GCMD_SRTP, iommu->reg + DMAR_GCMD_REG);
947
948
949

	/* Make sure hardware complete it */
	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
950
		      readl, (sts & DMA_GSTS_RTPS), sts);
951
952
953
954
955
956
957
958
959

	spin_unlock_irqrestore(&iommu->register_lock, flag);
}

static void iommu_flush_write_buffer(struct intel_iommu *iommu)
{
	u32 val;
	unsigned long flag;

960
	if (!rwbf_quirk && !cap_rwbf(iommu->cap))
961
962
963
		return;

	spin_lock_irqsave(&iommu->register_lock, flag);
964
	writel(iommu->gcmd | DMA_GCMD_WBF, iommu->reg + DMAR_GCMD_REG);
965
966
967

	/* Make sure hardware complete it */
	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
968
		      readl, (!(val & DMA_GSTS_WBFS)), val);
969
970
971
972
973

	spin_unlock_irqrestore(&iommu->register_lock, flag);
}

/* return value determine if we need a write buffer flush */
974
975
976
static void __iommu_flush_context(struct intel_iommu *iommu,
				  u16 did, u16 source_id, u8 function_mask,
				  u64 type)
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
{
	u64 val = 0;
	unsigned long flag;

	switch (type) {
	case DMA_CCMD_GLOBAL_INVL:
		val = DMA_CCMD_GLOBAL_INVL;
		break;
	case DMA_CCMD_DOMAIN_INVL:
		val = DMA_CCMD_DOMAIN_INVL|DMA_CCMD_DID(did);
		break;
	case DMA_CCMD_DEVICE_INVL:
		val = DMA_CCMD_DEVICE_INVL|DMA_CCMD_DID(did)
			| DMA_CCMD_SID(source_id) | DMA_CCMD_FM(function_mask);
		break;
	default:
		BUG();
	}
	val |= DMA_CCMD_ICC;

	spin_lock_irqsave(&iommu->register_lock, flag);
	dmar_writeq(iommu->reg + DMAR_CCMD_REG, val);

	/* Make sure hardware complete it */
	IOMMU_WAIT_OP(iommu, DMAR_CCMD_REG,
		dmar_readq, (!(val & DMA_CCMD_ICC)), val);

	spin_unlock_irqrestore(&iommu->register_lock, flag);
}

/* return value determine if we need a write buffer flush */
1008
1009
static void __iommu_flush_iotlb(struct intel_iommu *iommu, u16 did,
				u64 addr, unsigned int size_order, u64 type)
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
{
	int tlb_offset = ecap_iotlb_offset(iommu->ecap);
	u64 val = 0, val_iva = 0;
	unsigned long flag;

	switch (type) {
	case DMA_TLB_GLOBAL_FLUSH:
		/* global flush doesn't need set IVA_REG */
		val = DMA_TLB_GLOBAL_FLUSH|DMA_TLB_IVT;
		break;
	case DMA_TLB_DSI_FLUSH:
		val = DMA_TLB_DSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
		break;
	case DMA_TLB_PSI_FLUSH:
		val = DMA_TLB_PSI_FLUSH|DMA_TLB_IVT|DMA_TLB_DID(did);
		/* Note: always flush non-leaf currently */
		val_iva = size_order | addr;
		break;
	default:
		BUG();
	}
	/* Note: set drain read/write */
#if 0
	/*
	 * This is probably to be super secure.. Looks like we can
	 * ignore it without any impact.
	 */
	if (cap_read_drain(iommu->cap))
		val |= DMA_TLB_READ_DRAIN;
#endif
	if (cap_write_drain(iommu->cap))
		val |= DMA_TLB_WRITE_DRAIN;

	spin_lock_irqsave(&iommu->register_lock, flag);
	/* Note: Only uses first TLB reg currently */
	if (val_iva)
		dmar_writeq(iommu->reg + tlb_offset, val_iva);
	dmar_writeq(iommu->reg + tlb_offset + 8, val);

	/* Make sure hardware complete it */
	IOMMU_WAIT_OP(iommu, tlb_offset + 8,
		dmar_readq, (!(val & DMA_TLB_IVT)), val);

	spin_unlock_irqrestore(&iommu->register_lock, flag);

	/* check IOTLB invalidation granularity */
	if (DMA_TLB_IAIG(val) == 0)
		printk(KERN_ERR"IOMMU: flush IOTLB failed\n");
	if (DMA_TLB_IAIG(val) != DMA_TLB_IIRG(type))
		pr_debug("IOMMU: tlb flush request %Lx, actual %Lx\n",
Fenghua Yu's avatar
Fenghua Yu committed
1060
1061
			(unsigned long long)DMA_TLB_IIRG(type),
			(unsigned long long)DMA_TLB_IAIG(val));
1062
1063
}

Yu Zhao's avatar
Yu Zhao committed
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
static struct device_domain_info *iommu_support_dev_iotlb(
	struct dmar_domain *domain, int segment, u8 bus, u8 devfn)
{
	int found = 0;
	unsigned long flags;
	struct device_domain_info *info;
	struct intel_iommu *iommu = device_to_iommu(segment, bus, devfn);

	if (!ecap_dev_iotlb_support(iommu->ecap))
		return NULL;

	if (!iommu->qi)
		return NULL;

	spin_lock_irqsave(&device_domain_lock, flags);
	list_for_each_entry(info, &domain->devices, link)
		if (info->bus == bus && info->devfn == devfn) {
			found = 1;
			break;
		}
	spin_unlock_irqrestore(&device_domain_lock, flags);

	if (!found || !info->dev)
		return NULL;

	if (!pci_find_ext_capability(info->dev, PCI_EXT_CAP_ID_ATS))
		return NULL;

	if (!dmar_find_matched_atsr_unit(info->dev))
		return NULL;

	info->iommu = iommu;

	return info;
}

static void iommu_enable_dev_iotlb(struct device_domain_info *info)
1101
{
Yu Zhao's avatar
Yu Zhao committed
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
	if (!info)
		return;

	pci_enable_ats(info->dev, VTD_PAGE_SHIFT);
}

static void iommu_disable_dev_iotlb(struct device_domain_info *info)
{
	if (!info->dev || !pci_ats_enabled(info->dev))
		return;

	pci_disable_ats(info->dev);
}

static void iommu_flush_dev_iotlb(struct dmar_domain *domain,
				  u64 addr, unsigned mask)
{
	u16 sid, qdep;
	unsigned long flags;
	struct device_domain_info *info;

	spin_lock_irqsave(&device_domain_lock, flags);
	list_for_each_entry(info, &domain->devices, link) {
		if (!info->dev || !pci_ats_enabled(info->dev))
			continue;

		sid = info->bus << 8 | info->devfn;
		qdep = pci_ats_queue_depth(info->dev);
		qi_flush_dev_iotlb(info->iommu, sid, qdep, addr, mask);
	}
	spin_unlock_irqrestore(&device_domain_lock, flags);
}

1135
static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, u16 did,
1136
				  unsigned long pfn, unsigned int pages, int map)
1137
{
1138
	unsigned int mask = ilog2(__roundup_pow_of_two(pages));
1139
	uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT;
1140
1141
1142
1143

	BUG_ON(pages == 0);

	/*
1144
1145
	 * Fallback to domain selective flush if no PSI support or the size is
	 * too big.
1146
1147
1148
	 * PSI requires page size to be 2 ^ x, and the base address is naturally
	 * aligned to the size
	 */
1149
1150
	if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))
		iommu->flush.flush_iotlb(iommu, did, 0, 0,
1151
						DMA_TLB_DSI_FLUSH);
1152
1153
1154
	else
		iommu->flush.flush_iotlb(iommu, did, addr, mask,
						DMA_TLB_PSI_FLUSH);
1155
1156

	/*
1157
1158
	 * In caching mode, changes of pages from non-present to present require
	 * flush. However, device IOTLB doesn't need to be flushed in this case.
1159
	 */
1160
	if (!cap_caching_mode(iommu->cap) || !map)
Yu Zhao's avatar
Yu Zhao committed
1161
		iommu_flush_dev_iotlb(iommu->domains[did], addr, mask);
1162
1163
}

mark gross's avatar
mark gross committed
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
static void iommu_disable_protect_mem_regions(struct intel_iommu *iommu)
{
	u32 pmen;
	unsigned long flags;

	spin_lock_irqsave(&iommu->register_lock, flags);
	pmen = readl(iommu->reg + DMAR_PMEN_REG);
	pmen &= ~DMA_PMEN_EPM;
	writel(pmen, iommu->reg + DMAR_PMEN_REG);

	/* wait for the protected region status bit to clear */
	IOMMU_WAIT_OP(iommu, DMAR_PMEN_REG,
		readl, !(pmen & DMA_PMEN_PRS), pmen);

	spin_unlock_irqrestore(&iommu->register_lock, flags);
}

1181
1182
1183
1184
1185
1186
static int iommu_enable_translation(struct intel_iommu *iommu)
{
	u32 sts;
	unsigned long flags;

	spin_lock_irqsave(&iommu->register_lock, flags);
1187
1188
	iommu->gcmd |= DMA_GCMD_TE;
	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);
1189
1190
1191

	/* Make sure hardware complete it */
	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1192
		      readl, (sts & DMA_GSTS_TES), sts);
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208

	spin_unlock_irqrestore(&iommu->register_lock, flags);
	return 0;
}

static int iommu_disable_translation(struct intel_iommu *iommu)
{
	u32 sts;
	unsigned long flag;

	spin_lock_irqsave(&iommu->register_lock, flag);
	iommu->gcmd &= ~DMA_GCMD_TE;
	writel(iommu->gcmd, iommu->reg + DMAR_GCMD_REG);

	/* Make sure hardware complete it */
	IOMMU_WAIT_OP(iommu, DMAR_GSTS_REG,
1209
		      readl, (!(sts & DMA_GSTS_TES)), sts);
1210
1211
1212
1213
1214

	spin_unlock_irqrestore(&iommu->register_lock, flag);
	return 0;
}

1215

1216
1217
1218
1219
1220
1221
static int iommu_init_domains(struct intel_iommu *iommu)
{
	unsigned long ndomains;
	unsigned long nlongs;

	ndomains = cap_ndoms(iommu->cap);
1222
1223
	pr_debug("IOMMU %d: Number of Domains supportd <%ld>\n", iommu->seq_id,
			ndomains);
1224
1225
	nlongs = BITS_TO_LONGS(ndomains);

1226
1227
	spin_lock_init(&iommu->lock);

1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
	/* TBD: there might be 64K domains,
	 * consider other allocation for future chip
	 */
	iommu->domain_ids = kcalloc(nlongs, sizeof(unsigned long), GFP_KERNEL);
	if (!iommu->domain_ids) {
		printk(KERN_ERR "Allocating domain id array failed\n");
		return -ENOMEM;
	}
	iommu->domains = kcalloc(ndomains, sizeof(struct dmar_domain *),
			GFP_KERNEL);
	if (!iommu->domains) {
		printk(KERN_ERR "Allocating domain array failed\n");
		return -ENOMEM;
	}

	/*
	 * if Caching mode is set, then invalid translations are tagged
	 * with domainid 0. Hence we need to pre-allocate it.
	 */
	if (cap_caching_mode(iommu->cap))
		set_bit(0, iommu->domain_ids);
	return 0;
}


static void domain_exit(struct dmar_domain *domain);
1254
static void vm_domain_exit(struct dmar_domain *domain);
1255
1256

void free_dmar_iommu(struct intel_iommu *iommu)
1257
1258
1259
{
	struct dmar_domain *domain;
	int i;
1260
	unsigned long flags;
1261

1262
	if ((iommu->domains) && (iommu->domain_ids)) {
1263
		for_each_set_bit(i, iommu->domain_ids, cap_ndoms(iommu->cap)) {
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
			domain = iommu->domains[i];
			clear_bit(i, iommu->domain_ids);

			spin_lock_irqsave(&domain->iommu_lock, flags);
			if (--domain->iommu_count == 0) {
				if (domain->flags & DOMAIN_FLAG_VIRTUAL_MACHINE)
					vm_domain_exit(domain);
				else
					domain_exit(domain);
			}
			spin_unlock_irqrestore(&domain->iommu_lock, flags);
1275
		}
1276
1277
1278
1279
1280
1281
	}

	if (iommu->gcmd & DMA_GCMD_TE)
		iommu_disable_translation(iommu);

	if (iommu->irq) {
1282
		irq_set_handler_data(iommu->irq, NULL);
1283
1284
1285
1286
1287
1288
1289
1290
		/* This will mask the irq */
		free_irq(iommu->irq, iommu);
		destroy_irq(iommu->irq);
	}

	kfree(iommu->domains);
	kfree(iommu->domain_ids);

Weidong Han's avatar
Weidong Han committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
	g_iommus[iommu->seq_id] = NULL;

	/* if all iommus are freed, free g_iommus */
	for (i = 0; i < g_num_of_iommus; i++) {
		if (g_iommus[i])
			break;
	}

	if (i == g_num_of_iommus)
		kfree(g_iommus);

1302
1303
1304
1305
	/* free context mapping */
	free_context_table(iommu);
}

1306
static struct dmar_domain *alloc_domain(void)
1307
1308
1309
1310
1311
1312
1313
{
	struct dmar_domain *domain;

	domain = alloc_domain_mem();
	if (!domain)
		return NULL;

1314
	domain->nid = -1;
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
	memset(&domain->iommu_bmp, 0, sizeof(unsigned long));
	domain->flags = 0;

	return domain;
}

static int iommu_attach_domain(struct dmar_domain *domain,
			       struct intel_iommu *iommu)
{
	int num;
	unsigned long ndomains;
	unsigned long flags;

1328
1329
1330
	ndomains = cap_ndoms(iommu->cap);

	spin_lock_irqsave(&iommu->lock, flags);
1331

1332
1333
1334
1335
	num = find_first_zero_bit(iommu->domain_ids, ndomains);
	if (num >= ndomains) {
		spin_unlock_irqrestore(&iommu->lock, flags);
		printk(KERN_ERR "IOMMU: no free domain ids\n");
1336
		return -ENOMEM;
1337
1338
1339
	}

	domain->id = num;
1340
	set_bit(num, iommu->domain_ids);
1341
	set_bit(iommu->seq_id, &domain->iommu_bmp);
1342
1343
1344
	iommu->domains[num] = domain;
	spin_unlock_irqrestore(&iommu->lock, flags);

1345
	return 0;
1346
1347
}

1348
1349
static void iommu_detach_domain(struct dmar_domain *domain,
				struct intel_iommu *iommu)
1350
1351
{
	unsigned long flags;
1352
1353
	int num, ndomains;
	int found = 0;
1354

1355
	spin_lock_irqsave(&iommu->lock, flags);
1356
	ndomains = cap_ndoms(iommu->cap);
1357
	for_each_set_bit(num, iommu->domain_ids, ndomains) {
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
		if (iommu->domains[num] == domain) {
			found = 1;
			break;
		}
	}

	if (found) {
		clear_bit(num, iommu->domain_ids);
		clear_bit(iommu->seq_id, &domain->iommu_bmp);
		iommu->domains[num] = NULL;
	}
1369
	spin_unlock_irqrestore(&iommu->lock, flags);
1370
1371
1372
}

static struct iova_domain reserved_iova_list;
1373
static struct lock_class_key reserved_rbtree_key;