]> bbs.cooldavid.org Git - net-next-2.6.git/blame - virt/kvm/vtd.c
KVM: use the new intel iommu APIs
[net-next-2.6.git] / virt / kvm / vtd.c
CommitLineData
62c476c7
BAY
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;
e5fcfc82 39 int i, r = 0;
62c476c7
BAY
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
62c476c7
BAY
46 for (i = 0; i < npages; i++) {
47 /* check if already mapped */
260782bc
WH
48 if (intel_iommu_iova_to_phys(domain,
49 gfn_to_gpa(gfn)))
62c476c7
BAY
50 continue;
51
52 pfn = gfn_to_pfn(kvm, gfn);
260782bc
WH
53 r = intel_iommu_map_address(domain,
54 gfn_to_gpa(gfn),
55 pfn_to_hpa(pfn),
56 PAGE_SIZE,
57 DMA_PTE_READ | DMA_PTE_WRITE);
e5fcfc82 58 if (r) {
260782bc 59 printk(KERN_ERR "kvm_iommu_map_address:"
e5fcfc82 60 "iommu failed to map pfn=%lx\n", pfn);
62c476c7
BAY
61 goto unmap_pages;
62 }
63 gfn++;
64 }
65 return 0;
66
67unmap_pages:
68 kvm_iommu_put_pages(kvm, base_gfn, i);
69 return r;
70}
71
72static int kvm_iommu_map_memslots(struct kvm *kvm)
73{
74 int i, r;
75
76 down_read(&kvm->slots_lock);
77 for (i = 0; i < kvm->nmemslots; i++) {
78 r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
79 kvm->memslots[i].npages);
80 if (r)
81 break;
82 }
83 up_read(&kvm->slots_lock);
84 return r;
85}
86
260782bc
WH
87int kvm_assign_device(struct kvm *kvm,
88 struct kvm_assigned_dev_kernel *assigned_dev)
62c476c7
BAY
89{
90 struct pci_dev *pdev = NULL;
260782bc 91 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
62c476c7
BAY
92 int r;
93
260782bc
WH
94 /* check if iommu exists and in use */
95 if (!domain)
96 return 0;
97
98 pdev = assigned_dev->dev;
99 if (pdev == NULL)
62c476c7 100 return -ENODEV;
260782bc
WH
101
102 r = intel_iommu_attach_device(domain, pdev);
103 if (r) {
104 printk(KERN_ERR "assign device %x:%x.%x failed",
105 pdev->bus->number,
106 PCI_SLOT(pdev->devfn),
107 PCI_FUNC(pdev->devfn));
108 return r;
62c476c7
BAY
109 }
110
260782bc
WH
111 printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n",
112 assigned_dev->host_busnr,
113 PCI_SLOT(assigned_dev->host_devfn),
114 PCI_FUNC(assigned_dev->host_devfn));
62c476c7 115
260782bc
WH
116 return 0;
117}
62c476c7 118
260782bc
WH
119int kvm_iommu_map_guest(struct kvm *kvm)
120{
121 int r;
122
123 if (!intel_iommu_found()) {
124 printk(KERN_ERR "%s: intel iommu not found\n", __func__);
62c476c7
BAY
125 return -ENODEV;
126 }
127
260782bc 128 kvm->arch.intel_iommu_domain = intel_iommu_alloc_domain();
62c476c7 129 if (!kvm->arch.intel_iommu_domain)
260782bc 130 return -ENOMEM;
62c476c7
BAY
131
132 r = kvm_iommu_map_memslots(kvm);
133 if (r)
134 goto out_unmap;
135
62c476c7
BAY
136 return 0;
137
138out_unmap:
139 kvm_iommu_unmap_memslots(kvm);
140 return r;
141}
142
143static void kvm_iommu_put_pages(struct kvm *kvm,
260782bc 144 gfn_t base_gfn, unsigned long npages)
62c476c7
BAY
145{
146 gfn_t gfn = base_gfn;
147 pfn_t pfn;
148 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
260782bc
WH
149 unsigned long i;
150 u64 phys;
151
152 /* check if iommu exists and in use */
153 if (!domain)
154 return;
62c476c7
BAY
155
156 for (i = 0; i < npages; i++) {
260782bc
WH
157 phys = intel_iommu_iova_to_phys(domain,
158 gfn_to_gpa(gfn));
159 pfn = phys >> PAGE_SHIFT;
62c476c7
BAY
160 kvm_release_pfn_clean(pfn);
161 gfn++;
162 }
260782bc
WH
163
164 intel_iommu_unmap_address(domain,
165 gfn_to_gpa(base_gfn),
166 PAGE_SIZE * npages);
62c476c7
BAY
167}
168
169static int kvm_iommu_unmap_memslots(struct kvm *kvm)
170{
171 int i;
172 down_read(&kvm->slots_lock);
173 for (i = 0; i < kvm->nmemslots; i++) {
174 kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
175 kvm->memslots[i].npages);
176 }
177 up_read(&kvm->slots_lock);
178
179 return 0;
180}
181
182int kvm_iommu_unmap_guest(struct kvm *kvm)
183{
184 struct kvm_assigned_dev_kernel *entry;
185 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
186
187 /* check if iommu exists and in use */
188 if (!domain)
189 return 0;
190
191 list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) {
192 printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n",
193 entry->host_busnr,
194 PCI_SLOT(entry->host_devfn),
195 PCI_FUNC(entry->host_devfn));
196
197 /* detach kvm dmar domain */
260782bc 198 intel_iommu_detach_device(domain, entry->dev);
62c476c7
BAY
199 }
200 kvm_iommu_unmap_memslots(kvm);
260782bc 201 intel_iommu_free_domain(domain);
62c476c7
BAY
202 return 0;
203}