diff options
author | Ben-Ami Yassour <benami@il.ibm.com> | 2008-09-13 20:48:28 -0400 |
---|---|---|
committer | Avi Kivity <avi@redhat.com> | 2008-10-15 08:25:04 -0400 |
commit | 62c476c7c7f25a5b245b9902a935636e6316e58c (patch) | |
tree | 6584591c7c345fcbb3c6d437203dc7f4a628573a /arch/x86 | |
parent | 387179464257921eb9aa3d15cc3ff194f6945a7c (diff) |
KVM: Device Assignment with VT-d
Based on a patch by: Kay, Allen M <allen.m.kay@intel.com>
This patch enables PCI device assignment based on VT-d support.
When a device is assigned to the guest, the guest memory is pinned and
the mapping is updated in the VT-d IOMMU.
[Amit: Expose KVM_CAP_IOMMU so we can check if an IOMMU is present
and also control enable/disable from userspace]
Signed-off-by: Kay, Allen M <allen.m.kay@intel.com>
Signed-off-by: Weidong Han <weidong.han@intel.com>
Signed-off-by: Ben-Ami Yassour <benami@il.ibm.com>
Signed-off-by: Amit Shah <amit.shah@qumranet.com>
Acked-by: Mark Gross <mgross@linux.intel.com>
Signed-off-by: Avi Kivity <avi@qumranet.com>
Diffstat (limited to 'arch/x86')
-rw-r--r-- | arch/x86/kvm/Makefile | 3 | ||||
-rw-r--r-- | arch/x86/kvm/vtd.c | 198 | ||||
-rw-r--r-- | arch/x86/kvm/x86.c | 14 |
3 files changed, 215 insertions, 0 deletions
diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile index d0e940bb6f40..3072b17447ab 100644 --- a/arch/x86/kvm/Makefile +++ b/arch/x86/kvm/Makefile | |||
@@ -12,6 +12,9 @@ EXTRA_CFLAGS += -Ivirt/kvm -Iarch/x86/kvm | |||
12 | 12 | ||
13 | kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \ | 13 | kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \ |
14 | i8254.o | 14 | i8254.o |
15 | ifeq ($(CONFIG_DMAR),y) | ||
16 | kvm-objs += vtd.o | ||
17 | endif | ||
15 | obj-$(CONFIG_KVM) += kvm.o | 18 | obj-$(CONFIG_KVM) += kvm.o |
16 | kvm-intel-objs = vmx.o | 19 | kvm-intel-objs = vmx.o |
17 | obj-$(CONFIG_KVM_INTEL) += kvm-intel.o | 20 | obj-$(CONFIG_KVM_INTEL) += kvm-intel.o |
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 | |||
30 | static int kvm_iommu_unmap_memslots(struct kvm *kvm); | ||
31 | static void kvm_iommu_put_pages(struct kvm *kvm, | ||
32 | gfn_t base_gfn, unsigned long npages); | ||
33 | |||
34 | int 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 | |||
76 | unmap_pages: | ||
77 | kvm_iommu_put_pages(kvm, base_gfn, i); | ||
78 | return r; | ||
79 | } | ||
80 | |||
81 | static 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 | |||
96 | int 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 | |||
142 | out_unmap: | ||
143 | kvm_iommu_unmap_memslots(kvm); | ||
144 | return r; | ||
145 | } | ||
146 | |||
147 | static 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 | |||
163 | static 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 | |||
176 | int 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 | } | ||
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2134f3e0a516..c8a2793626ec 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c | |||
@@ -35,6 +35,7 @@ | |||
35 | #include <linux/module.h> | 35 | #include <linux/module.h> |
36 | #include <linux/mman.h> | 36 | #include <linux/mman.h> |
37 | #include <linux/highmem.h> | 37 | #include <linux/highmem.h> |
38 | #include <linux/intel-iommu.h> | ||
38 | 39 | ||
39 | #include <asm/uaccess.h> | 40 | #include <asm/uaccess.h> |
40 | #include <asm/msr.h> | 41 | #include <asm/msr.h> |
@@ -277,9 +278,18 @@ static int kvm_vm_ioctl_assign_device(struct kvm *kvm, | |||
277 | 278 | ||
278 | list_add(&match->list, &kvm->arch.assigned_dev_head); | 279 | list_add(&match->list, &kvm->arch.assigned_dev_head); |
279 | 280 | ||
281 | if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) { | ||
282 | r = kvm_iommu_map_guest(kvm, match); | ||
283 | if (r) | ||
284 | goto out_list_del; | ||
285 | } | ||
286 | |||
280 | out: | 287 | out: |
281 | mutex_unlock(&kvm->lock); | 288 | mutex_unlock(&kvm->lock); |
282 | return r; | 289 | return r; |
290 | out_list_del: | ||
291 | list_del(&match->list); | ||
292 | pci_release_regions(dev); | ||
283 | out_disable: | 293 | out_disable: |
284 | pci_disable_device(dev); | 294 | pci_disable_device(dev); |
285 | out_put: | 295 | out_put: |
@@ -1147,6 +1157,9 @@ int kvm_dev_ioctl_check_extension(long ext) | |||
1147 | case KVM_CAP_PV_MMU: | 1157 | case KVM_CAP_PV_MMU: |
1148 | r = !tdp_enabled; | 1158 | r = !tdp_enabled; |
1149 | break; | 1159 | break; |
1160 | case KVM_CAP_IOMMU: | ||
1161 | r = intel_iommu_found(); | ||
1162 | break; | ||
1150 | default: | 1163 | default: |
1151 | r = 0; | 1164 | r = 0; |
1152 | break; | 1165 | break; |
@@ -4282,6 +4295,7 @@ static void kvm_free_vcpus(struct kvm *kvm) | |||
4282 | 4295 | ||
4283 | void kvm_arch_destroy_vm(struct kvm *kvm) | 4296 | void kvm_arch_destroy_vm(struct kvm *kvm) |
4284 | { | 4297 | { |
4298 | kvm_iommu_unmap_guest(kvm); | ||
4285 | kvm_free_assigned_devices(kvm); | 4299 | kvm_free_assigned_devices(kvm); |
4286 | kvm_free_pit(kvm); | 4300 | kvm_free_pit(kvm); |
4287 | kfree(kvm->arch.vpic); | 4301 | kfree(kvm->arch.vpic); |