aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kvm/vtd.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/kvm/vtd.c')
-rw-r--r--arch/x86/kvm/vtd.c198
1 files changed, 198 insertions, 0 deletions
diff --git a/arch/x86/kvm/vtd.c b/arch/x86/kvm/vtd.c
new file mode 100644
index 000000000000..667bf3fb64bf
--- /dev/null
+++ b/arch/x86/kvm/vtd.c
@@ -0,0 +1,198 @@
1/*
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 *
17 * Copyright (C) 2006-2008 Intel Corporation
18 * Copyright IBM Corporation, 2008
19 * Author: Allen M. Kay <allen.m.kay@intel.com>
20 * Author: Weidong Han <weidong.han@intel.com>
21 * Author: Ben-Ami Yassour <benami@il.ibm.com>
22 */
23
24#include <linux/list.h>
25#include <linux/kvm_host.h>
26#include <linux/pci.h>
27#include <linux/dmar.h>
28#include <linux/intel-iommu.h>
29
30static int kvm_iommu_unmap_memslots(struct kvm *kvm);
31static void kvm_iommu_put_pages(struct kvm *kvm,
32 gfn_t base_gfn, unsigned long npages);
33
34int kvm_iommu_map_pages(struct kvm *kvm,
35 gfn_t base_gfn, unsigned long npages)
36{
37 gfn_t gfn = base_gfn;
38 pfn_t pfn;
39 int i, r;
40 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
41
42 /* check if iommu exists and in use */
43 if (!domain)
44 return 0;
45
46 r = -EINVAL;
47 for (i = 0; i < npages; i++) {
48 /* check if already mapped */
49 pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
50 gfn_to_gpa(gfn));
51 if (pfn && !is_mmio_pfn(pfn))
52 continue;
53
54 pfn = gfn_to_pfn(kvm, gfn);
55 if (!is_mmio_pfn(pfn)) {
56 r = intel_iommu_page_mapping(domain,
57 gfn_to_gpa(gfn),
58 pfn_to_hpa(pfn),
59 PAGE_SIZE,
60 DMA_PTE_READ |
61 DMA_PTE_WRITE);
62 if (r) {
63 printk(KERN_DEBUG "kvm_iommu_map_pages:"
64 "iommu failed to map pfn=%lx\n", pfn);
65 goto unmap_pages;
66 }
67 } else {
68 printk(KERN_DEBUG "kvm_iommu_map_page:"
69 "invalid pfn=%lx\n", pfn);
70 goto unmap_pages;
71 }
72 gfn++;
73 }
74 return 0;
75
76unmap_pages:
77 kvm_iommu_put_pages(kvm, base_gfn, i);
78 return r;
79}
80
81static int kvm_iommu_map_memslots(struct kvm *kvm)
82{
83 int i, r;
84
85 down_read(&kvm->slots_lock);
86 for (i = 0; i < kvm->nmemslots; i++) {
87 r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
88 kvm->memslots[i].npages);
89 if (r)
90 break;
91 }
92 up_read(&kvm->slots_lock);
93 return r;
94}
95
96int kvm_iommu_map_guest(struct kvm *kvm,
97 struct kvm_assigned_dev_kernel *assigned_dev)
98{
99 struct pci_dev *pdev = NULL;
100 int r;
101
102 if (!intel_iommu_found()) {
103 printk(KERN_ERR "%s: intel iommu not found\n", __func__);
104 return -ENODEV;
105 }
106
107 printk(KERN_DEBUG "VT-d direct map: host bdf = %x:%x:%x\n",
108 assigned_dev->host_busnr,
109 PCI_SLOT(assigned_dev->host_devfn),
110 PCI_FUNC(assigned_dev->host_devfn));
111
112 pdev = assigned_dev->dev;
113
114 if (pdev == NULL) {
115 if (kvm->arch.intel_iommu_domain) {
116 intel_iommu_domain_exit(kvm->arch.intel_iommu_domain);
117 kvm->arch.intel_iommu_domain = NULL;
118 }
119 return -ENODEV;
120 }
121
122 kvm->arch.intel_iommu_domain = intel_iommu_domain_alloc(pdev);
123 if (!kvm->arch.intel_iommu_domain)
124 return -ENODEV;
125
126 r = kvm_iommu_map_memslots(kvm);
127 if (r)
128 goto out_unmap;
129
130 intel_iommu_detach_dev(kvm->arch.intel_iommu_domain,
131 pdev->bus->number, pdev->devfn);
132
133 r = intel_iommu_context_mapping(kvm->arch.intel_iommu_domain,
134 pdev);
135 if (r) {
136 printk(KERN_ERR "Domain context map for %s failed",
137 pci_name(pdev));
138 goto out_unmap;
139 }
140 return 0;
141
142out_unmap:
143 kvm_iommu_unmap_memslots(kvm);
144 return r;
145}
146
147static void kvm_iommu_put_pages(struct kvm *kvm,
148 gfn_t base_gfn, unsigned long npages)
149{
150 gfn_t gfn = base_gfn;
151 pfn_t pfn;
152 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
153 int i;
154
155 for (i = 0; i < npages; i++) {
156 pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
157 gfn_to_gpa(gfn));
158 kvm_release_pfn_clean(pfn);
159 gfn++;
160 }
161}
162
163static int kvm_iommu_unmap_memslots(struct kvm *kvm)
164{
165 int i;
166 down_read(&kvm->slots_lock);
167 for (i = 0; i < kvm->nmemslots; i++) {
168 kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
169 kvm->memslots[i].npages);
170 }
171 up_read(&kvm->slots_lock);
172
173 return 0;
174}
175
176int kvm_iommu_unmap_guest(struct kvm *kvm)
177{
178 struct kvm_assigned_dev_kernel *entry;
179 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
180
181 /* check if iommu exists and in use */
182 if (!domain)
183 return 0;
184
185 list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) {
186 printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n",
187 entry->host_busnr,
188 PCI_SLOT(entry->host_devfn),
189 PCI_FUNC(entry->host_devfn));
190
191 /* detach kvm dmar domain */
192 intel_iommu_detach_dev(domain, entry->host_busnr,
193 entry->host_devfn);
194 }
195 kvm_iommu_unmap_memslots(kvm);
196 intel_iommu_domain_exit(domain);
197 return 0;
198}