aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen-Ami Yassour <benami@il.ibm.com>2008-09-13 20:48:28 -0400
committerAvi Kivity <avi@redhat.com>2008-10-15 08:25:04 -0400
commit62c476c7c7f25a5b245b9902a935636e6316e58c (patch)
tree6584591c7c345fcbb3c6d437203dc7f4a628573a
parent387179464257921eb9aa3d15cc3ff194f6945a7c (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>
-rw-r--r--arch/x86/kvm/Makefile3
-rw-r--r--arch/x86/kvm/vtd.c198
-rw-r--r--arch/x86/kvm/x86.c14
-rw-r--r--include/asm-x86/kvm_host.h23
-rw-r--r--include/linux/kvm.h3
-rw-r--r--include/linux/kvm_host.h52
-rw-r--r--virt/kvm/kvm_main.c9
7 files changed, 281 insertions, 21 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
13kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \ 13kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \
14 i8254.o 14 i8254.o
15ifeq ($(CONFIG_DMAR),y)
16kvm-objs += vtd.o
17endif
15obj-$(CONFIG_KVM) += kvm.o 18obj-$(CONFIG_KVM) += kvm.o
16kvm-intel-objs = vmx.o 19kvm-intel-objs = vmx.o
17obj-$(CONFIG_KVM_INTEL) += kvm-intel.o 20obj-$(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
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}
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
280out: 287out:
281 mutex_unlock(&kvm->lock); 288 mutex_unlock(&kvm->lock);
282 return r; 289 return r;
290out_list_del:
291 list_del(&match->list);
292 pci_release_regions(dev);
283out_disable: 293out_disable:
284 pci_disable_device(dev); 294 pci_disable_device(dev);
285out_put: 295out_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
4283void kvm_arch_destroy_vm(struct kvm *kvm) 4296void 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);
diff --git a/include/asm-x86/kvm_host.h b/include/asm-x86/kvm_host.h
index 68a3ac13afce..805629c0f15f 100644
--- a/include/asm-x86/kvm_host.h
+++ b/include/asm-x86/kvm_host.h
@@ -331,26 +331,6 @@ struct kvm_mem_alias {
331 gfn_t target_gfn; 331 gfn_t target_gfn;
332}; 332};
333 333
334struct kvm_irq_ack_notifier {
335 struct hlist_node link;
336 unsigned gsi;
337 void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
338};
339
340struct kvm_assigned_dev_kernel {
341 struct kvm_irq_ack_notifier ack_notifier;
342 struct work_struct interrupt_work;
343 struct list_head list;
344 int assigned_dev_id;
345 int host_busnr;
346 int host_devfn;
347 int host_irq;
348 int guest_irq;
349 int irq_requested;
350 struct pci_dev *dev;
351 struct kvm *kvm;
352};
353
354struct kvm_arch{ 334struct kvm_arch{
355 int naliases; 335 int naliases;
356 struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS]; 336 struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
@@ -364,6 +344,7 @@ struct kvm_arch{
364 */ 344 */
365 struct list_head active_mmu_pages; 345 struct list_head active_mmu_pages;
366 struct list_head assigned_dev_head; 346 struct list_head assigned_dev_head;
347 struct dmar_domain *intel_iommu_domain;
367 struct kvm_pic *vpic; 348 struct kvm_pic *vpic;
368 struct kvm_ioapic *vioapic; 349 struct kvm_ioapic *vioapic;
369 struct kvm_pit *vpit; 350 struct kvm_pit *vpit;
@@ -514,6 +495,8 @@ int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
514int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes, 495int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
515 gpa_t addr, unsigned long *ret); 496 gpa_t addr, unsigned long *ret);
516 497
498int is_mmio_pfn(pfn_t pfn);
499
517extern bool tdp_enabled; 500extern bool tdp_enabled;
518 501
519enum emulation_result { 502enum emulation_result {
diff --git a/include/linux/kvm.h b/include/linux/kvm.h
index ef4bc6f89778..4269be171faf 100644
--- a/include/linux/kvm.h
+++ b/include/linux/kvm.h
@@ -384,6 +384,7 @@ struct kvm_trace_rec {
384#define KVM_CAP_COALESCED_MMIO 15 384#define KVM_CAP_COALESCED_MMIO 15
385#define KVM_CAP_SYNC_MMU 16 /* Changes to host mmap are reflected in guest */ 385#define KVM_CAP_SYNC_MMU 16 /* Changes to host mmap are reflected in guest */
386#define KVM_CAP_DEVICE_ASSIGNMENT 17 386#define KVM_CAP_DEVICE_ASSIGNMENT 17
387#define KVM_CAP_IOMMU 18
387 388
388/* 389/*
389 * ioctls for VM fds 390 * ioctls for VM fds
@@ -495,4 +496,6 @@ struct kvm_assigned_irq {
495 __u32 flags; 496 __u32 flags;
496}; 497};
497 498
499#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)
500
498#endif 501#endif
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 4b036430ea23..6252802c3cc0 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -286,6 +286,53 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v);
286int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); 286int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
287void kvm_vcpu_kick(struct kvm_vcpu *vcpu); 287void kvm_vcpu_kick(struct kvm_vcpu *vcpu);
288 288
289struct kvm_irq_ack_notifier {
290 struct hlist_node link;
291 unsigned gsi;
292 void (*irq_acked)(struct kvm_irq_ack_notifier *kian);
293};
294
295struct kvm_assigned_dev_kernel {
296 struct kvm_irq_ack_notifier ack_notifier;
297 struct work_struct interrupt_work;
298 struct list_head list;
299 int assigned_dev_id;
300 int host_busnr;
301 int host_devfn;
302 int host_irq;
303 int guest_irq;
304 int irq_requested;
305 struct pci_dev *dev;
306 struct kvm *kvm;
307};
308
309#ifdef CONFIG_DMAR
310int kvm_iommu_map_pages(struct kvm *kvm, gfn_t base_gfn,
311 unsigned long npages);
312int kvm_iommu_map_guest(struct kvm *kvm,
313 struct kvm_assigned_dev_kernel *assigned_dev);
314int kvm_iommu_unmap_guest(struct kvm *kvm);
315#else /* CONFIG_DMAR */
316static inline int kvm_iommu_map_pages(struct kvm *kvm,
317 gfn_t base_gfn,
318 unsigned long npages)
319{
320 return 0;
321}
322
323static inline int kvm_iommu_map_guest(struct kvm *kvm,
324 struct kvm_assigned_dev_kernel
325 *assigned_dev)
326{
327 return -ENODEV;
328}
329
330static inline int kvm_iommu_unmap_guest(struct kvm *kvm)
331{
332 return 0;
333}
334#endif /* CONFIG_DMAR */
335
289static inline void kvm_guest_enter(void) 336static inline void kvm_guest_enter(void)
290{ 337{
291 account_system_vtime(current); 338 account_system_vtime(current);
@@ -308,6 +355,11 @@ static inline gpa_t gfn_to_gpa(gfn_t gfn)
308 return (gpa_t)gfn << PAGE_SHIFT; 355 return (gpa_t)gfn << PAGE_SHIFT;
309} 356}
310 357
358static inline hpa_t pfn_to_hpa(pfn_t pfn)
359{
360 return (hpa_t)pfn << PAGE_SHIFT;
361}
362
311static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu) 363static inline void kvm_migrate_timers(struct kvm_vcpu *vcpu)
312{ 364{
313 set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests); 365 set_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 63e661be040a..f42d5c2a396d 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -41,6 +41,7 @@
41#include <linux/pagemap.h> 41#include <linux/pagemap.h>
42#include <linux/mman.h> 42#include <linux/mman.h>
43#include <linux/swap.h> 43#include <linux/swap.h>
44#include <linux/intel-iommu.h>
44 45
45#include <asm/processor.h> 46#include <asm/processor.h>
46#include <asm/io.h> 47#include <asm/io.h>
@@ -76,7 +77,7 @@ static inline int valid_vcpu(int n)
76 return likely(n >= 0 && n < KVM_MAX_VCPUS); 77 return likely(n >= 0 && n < KVM_MAX_VCPUS);
77} 78}
78 79
79static inline int is_mmio_pfn(pfn_t pfn) 80inline int is_mmio_pfn(pfn_t pfn)
80{ 81{
81 if (pfn_valid(pfn)) 82 if (pfn_valid(pfn))
82 return PageReserved(pfn_to_page(pfn)); 83 return PageReserved(pfn_to_page(pfn));
@@ -578,6 +579,12 @@ int __kvm_set_memory_region(struct kvm *kvm,
578 } 579 }
579 580
580 kvm_free_physmem_slot(&old, &new); 581 kvm_free_physmem_slot(&old, &new);
582
583 /* map the pages in iommu page table */
584 r = kvm_iommu_map_pages(kvm, base_gfn, npages);
585 if (r)
586 goto out;
587
581 return 0; 588 return 0;
582 589
583out_free: 590out_free: