aboutsummaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorZhang xiantao <xiantao.zhang@intel.com>2008-09-11 01:19:32 -0400
committerAvi Kivity <avi@redhat.com>2008-10-15 08:25:32 -0400
commit371c01b28e4049d1fbf60a9631cdad98f7cae030 (patch)
tree84286c5292b404923d8cf730e06d52a332468da6 /arch
parent83dbc83a0d7c88c919d769177bd1924a46c9c034 (diff)
KVM: Device Assignment: Move vtd.c from arch/x86/kvm/ to virt/kvm/
Preparation for kvm/ia64 VT-d support. Signed-off-by: Zhang xiantao <xiantao.zhang@intel.com> Signed-off-by: Avi Kivity <avi@qumranet.com>
Diffstat (limited to 'arch')
-rw-r--r--arch/x86/kvm/Makefile6
-rw-r--r--arch/x86/kvm/vtd.c191
2 files changed, 3 insertions, 194 deletions
diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
index 3072b17447ab..7dce593b9c66 100644
--- a/arch/x86/kvm/Makefile
+++ b/arch/x86/kvm/Makefile
@@ -7,14 +7,14 @@ common-objs = $(addprefix ../../../virt/kvm/, kvm_main.o ioapic.o \
7ifeq ($(CONFIG_KVM_TRACE),y) 7ifeq ($(CONFIG_KVM_TRACE),y)
8common-objs += $(addprefix ../../../virt/kvm/, kvm_trace.o) 8common-objs += $(addprefix ../../../virt/kvm/, kvm_trace.o)
9endif 9endif
10ifeq ($(CONFIG_DMAR),y)
11common-objs += $(addprefix ../../../virt/kvm/, vtd.o)
12endif
10 13
11EXTRA_CFLAGS += -Ivirt/kvm -Iarch/x86/kvm 14EXTRA_CFLAGS += -Ivirt/kvm -Iarch/x86/kvm
12 15
13kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \ 16kvm-objs := $(common-objs) x86.o mmu.o x86_emulate.o i8259.o irq.o lapic.o \
14 i8254.o 17 i8254.o
15ifeq ($(CONFIG_DMAR),y)
16kvm-objs += vtd.o
17endif
18obj-$(CONFIG_KVM) += kvm.o 18obj-$(CONFIG_KVM) += kvm.o
19kvm-intel-objs = vmx.o 19kvm-intel-objs = vmx.o
20obj-$(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
deleted file mode 100644
index a770874f3a3a..000000000000
--- a/arch/x86/kvm/vtd.c
+++ /dev/null
@@ -1,191 +0,0 @@
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 = 0;
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 for (i = 0; i < npages; i++) {
47 /* check if already mapped */
48 pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
49 gfn_to_gpa(gfn));
50 if (pfn)
51 continue;
52
53 pfn = gfn_to_pfn(kvm, gfn);
54 r = intel_iommu_page_mapping(domain,
55 gfn_to_gpa(gfn),
56 pfn_to_hpa(pfn),
57 PAGE_SIZE,
58 DMA_PTE_READ |
59 DMA_PTE_WRITE);
60 if (r) {
61 printk(KERN_ERR "kvm_iommu_map_pages:"
62 "iommu failed to map pfn=%lx\n", pfn);
63 goto unmap_pages;
64 }
65 gfn++;
66 }
67 return 0;
68
69unmap_pages:
70 kvm_iommu_put_pages(kvm, base_gfn, i);
71 return r;
72}
73
74static int kvm_iommu_map_memslots(struct kvm *kvm)
75{
76 int i, r;
77
78 down_read(&kvm->slots_lock);
79 for (i = 0; i < kvm->nmemslots; i++) {
80 r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
81 kvm->memslots[i].npages);
82 if (r)
83 break;
84 }
85 up_read(&kvm->slots_lock);
86 return r;
87}
88
89int kvm_iommu_map_guest(struct kvm *kvm,
90 struct kvm_assigned_dev_kernel *assigned_dev)
91{
92 struct pci_dev *pdev = NULL;
93 int r;
94
95 if (!intel_iommu_found()) {
96 printk(KERN_ERR "%s: intel iommu not found\n", __func__);
97 return -ENODEV;
98 }
99
100 printk(KERN_DEBUG "VT-d direct map: host bdf = %x:%x:%x\n",
101 assigned_dev->host_busnr,
102 PCI_SLOT(assigned_dev->host_devfn),
103 PCI_FUNC(assigned_dev->host_devfn));
104
105 pdev = assigned_dev->dev;
106
107 if (pdev == NULL) {
108 if (kvm->arch.intel_iommu_domain) {
109 intel_iommu_domain_exit(kvm->arch.intel_iommu_domain);
110 kvm->arch.intel_iommu_domain = NULL;
111 }
112 return -ENODEV;
113 }
114
115 kvm->arch.intel_iommu_domain = intel_iommu_domain_alloc(pdev);
116 if (!kvm->arch.intel_iommu_domain)
117 return -ENODEV;
118
119 r = kvm_iommu_map_memslots(kvm);
120 if (r)
121 goto out_unmap;
122
123 intel_iommu_detach_dev(kvm->arch.intel_iommu_domain,
124 pdev->bus->number, pdev->devfn);
125
126 r = intel_iommu_context_mapping(kvm->arch.intel_iommu_domain,
127 pdev);
128 if (r) {
129 printk(KERN_ERR "Domain context map for %s failed",
130 pci_name(pdev));
131 goto out_unmap;
132 }
133 return 0;
134
135out_unmap:
136 kvm_iommu_unmap_memslots(kvm);
137 return r;
138}
139
140static void kvm_iommu_put_pages(struct kvm *kvm,
141 gfn_t base_gfn, unsigned long npages)
142{
143 gfn_t gfn = base_gfn;
144 pfn_t pfn;
145 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
146 int i;
147
148 for (i = 0; i < npages; i++) {
149 pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
150 gfn_to_gpa(gfn));
151 kvm_release_pfn_clean(pfn);
152 gfn++;
153 }
154}
155
156static int kvm_iommu_unmap_memslots(struct kvm *kvm)
157{
158 int i;
159 down_read(&kvm->slots_lock);
160 for (i = 0; i < kvm->nmemslots; i++) {
161 kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
162 kvm->memslots[i].npages);
163 }
164 up_read(&kvm->slots_lock);
165
166 return 0;
167}
168
169int kvm_iommu_unmap_guest(struct kvm *kvm)
170{
171 struct kvm_assigned_dev_kernel *entry;
172 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
173
174 /* check if iommu exists and in use */
175 if (!domain)
176 return 0;
177
178 list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) {
179 printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n",
180 entry->host_busnr,
181 PCI_SLOT(entry->host_devfn),
182 PCI_FUNC(entry->host_devfn));
183
184 /* detach kvm dmar domain */
185 intel_iommu_detach_dev(domain, entry->host_busnr,
186 entry->host_devfn);
187 }
188 kvm_iommu_unmap_memslots(kvm);
189 intel_iommu_domain_exit(domain);
190 return 0;
191}