Commit | Line | Data |
---|---|---|
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> | |
19de40a8 | 28 | #include <linux/iommu.h> |
62c476c7 BAY |
29 | #include <linux/intel-iommu.h> |
30 | ||
31 | static int kvm_iommu_unmap_memslots(struct kvm *kvm); | |
32 | static void kvm_iommu_put_pages(struct kvm *kvm, | |
33 | gfn_t base_gfn, unsigned long npages); | |
34 | ||
35 | int kvm_iommu_map_pages(struct kvm *kvm, | |
36 | gfn_t base_gfn, unsigned long npages) | |
37 | { | |
38 | gfn_t gfn = base_gfn; | |
39 | pfn_t pfn; | |
e5fcfc82 | 40 | int i, r = 0; |
19de40a8 | 41 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
522c68c4 | 42 | int flags; |
62c476c7 BAY |
43 | |
44 | /* check if iommu exists and in use */ | |
45 | if (!domain) | |
46 | return 0; | |
47 | ||
522c68c4 SY |
48 | flags = IOMMU_READ | IOMMU_WRITE; |
49 | if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY) | |
50 | flags |= IOMMU_CACHE; | |
51 | ||
62c476c7 BAY |
52 | for (i = 0; i < npages; i++) { |
53 | /* check if already mapped */ | |
19de40a8 | 54 | if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) |
62c476c7 BAY |
55 | continue; |
56 | ||
57 | pfn = gfn_to_pfn(kvm, gfn); | |
19de40a8 JR |
58 | r = iommu_map_range(domain, |
59 | gfn_to_gpa(gfn), | |
60 | pfn_to_hpa(pfn), | |
522c68c4 | 61 | PAGE_SIZE, flags); |
e5fcfc82 | 62 | if (r) { |
260782bc | 63 | printk(KERN_ERR "kvm_iommu_map_address:" |
e5fcfc82 | 64 | "iommu failed to map pfn=%lx\n", pfn); |
62c476c7 BAY |
65 | goto unmap_pages; |
66 | } | |
67 | gfn++; | |
68 | } | |
69 | return 0; | |
70 | ||
71 | unmap_pages: | |
72 | kvm_iommu_put_pages(kvm, base_gfn, i); | |
73 | return r; | |
74 | } | |
75 | ||
76 | static int kvm_iommu_map_memslots(struct kvm *kvm) | |
77 | { | |
7398ca79 | 78 | int i, r = 0; |
46a26bf5 | 79 | struct kvm_memslots *slots; |
62c476c7 | 80 | |
46a26bf5 MT |
81 | slots = kvm->memslots; |
82 | ||
83 | for (i = 0; i < slots->nmemslots; i++) { | |
84 | r = kvm_iommu_map_pages(kvm, slots->memslots[i].base_gfn, | |
85 | slots->memslots[i].npages); | |
62c476c7 BAY |
86 | if (r) |
87 | break; | |
88 | } | |
682edb4c | 89 | |
62c476c7 BAY |
90 | return r; |
91 | } | |
92 | ||
260782bc WH |
93 | int kvm_assign_device(struct kvm *kvm, |
94 | struct kvm_assigned_dev_kernel *assigned_dev) | |
62c476c7 BAY |
95 | { |
96 | struct pci_dev *pdev = NULL; | |
19de40a8 | 97 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
522c68c4 | 98 | int r, last_flags; |
62c476c7 | 99 | |
260782bc WH |
100 | /* check if iommu exists and in use */ |
101 | if (!domain) | |
102 | return 0; | |
103 | ||
104 | pdev = assigned_dev->dev; | |
105 | if (pdev == NULL) | |
62c476c7 | 106 | return -ENODEV; |
260782bc | 107 | |
19de40a8 | 108 | r = iommu_attach_device(domain, &pdev->dev); |
260782bc WH |
109 | if (r) { |
110 | printk(KERN_ERR "assign device %x:%x.%x failed", | |
111 | pdev->bus->number, | |
112 | PCI_SLOT(pdev->devfn), | |
113 | PCI_FUNC(pdev->devfn)); | |
114 | return r; | |
62c476c7 BAY |
115 | } |
116 | ||
522c68c4 SY |
117 | last_flags = kvm->arch.iommu_flags; |
118 | if (iommu_domain_has_cap(kvm->arch.iommu_domain, | |
119 | IOMMU_CAP_CACHE_COHERENCY)) | |
120 | kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY; | |
121 | ||
122 | /* Check if need to update IOMMU page table for guest memory */ | |
123 | if ((last_flags ^ kvm->arch.iommu_flags) == | |
124 | KVM_IOMMU_CACHE_COHERENCY) { | |
125 | kvm_iommu_unmap_memslots(kvm); | |
126 | r = kvm_iommu_map_memslots(kvm); | |
127 | if (r) | |
128 | goto out_unmap; | |
129 | } | |
130 | ||
260782bc WH |
131 | printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n", |
132 | assigned_dev->host_busnr, | |
133 | PCI_SLOT(assigned_dev->host_devfn), | |
134 | PCI_FUNC(assigned_dev->host_devfn)); | |
62c476c7 | 135 | |
260782bc | 136 | return 0; |
522c68c4 SY |
137 | out_unmap: |
138 | kvm_iommu_unmap_memslots(kvm); | |
139 | return r; | |
260782bc | 140 | } |
62c476c7 | 141 | |
0a920356 WH |
142 | int kvm_deassign_device(struct kvm *kvm, |
143 | struct kvm_assigned_dev_kernel *assigned_dev) | |
144 | { | |
19de40a8 | 145 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
0a920356 WH |
146 | struct pci_dev *pdev = NULL; |
147 | ||
148 | /* check if iommu exists and in use */ | |
149 | if (!domain) | |
150 | return 0; | |
151 | ||
152 | pdev = assigned_dev->dev; | |
153 | if (pdev == NULL) | |
154 | return -ENODEV; | |
155 | ||
19de40a8 | 156 | iommu_detach_device(domain, &pdev->dev); |
0a920356 WH |
157 | |
158 | printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n", | |
159 | assigned_dev->host_busnr, | |
160 | PCI_SLOT(assigned_dev->host_devfn), | |
161 | PCI_FUNC(assigned_dev->host_devfn)); | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
260782bc WH |
166 | int kvm_iommu_map_guest(struct kvm *kvm) |
167 | { | |
168 | int r; | |
169 | ||
19de40a8 JR |
170 | if (!iommu_found()) { |
171 | printk(KERN_ERR "%s: iommu not found\n", __func__); | |
62c476c7 BAY |
172 | return -ENODEV; |
173 | } | |
174 | ||
19de40a8 JR |
175 | kvm->arch.iommu_domain = iommu_domain_alloc(); |
176 | if (!kvm->arch.iommu_domain) | |
260782bc | 177 | return -ENOMEM; |
62c476c7 BAY |
178 | |
179 | r = kvm_iommu_map_memslots(kvm); | |
180 | if (r) | |
181 | goto out_unmap; | |
182 | ||
62c476c7 BAY |
183 | return 0; |
184 | ||
185 | out_unmap: | |
186 | kvm_iommu_unmap_memslots(kvm); | |
187 | return r; | |
188 | } | |
189 | ||
190 | static void kvm_iommu_put_pages(struct kvm *kvm, | |
260782bc | 191 | gfn_t base_gfn, unsigned long npages) |
62c476c7 BAY |
192 | { |
193 | gfn_t gfn = base_gfn; | |
194 | pfn_t pfn; | |
19de40a8 | 195 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
260782bc WH |
196 | unsigned long i; |
197 | u64 phys; | |
198 | ||
199 | /* check if iommu exists and in use */ | |
200 | if (!domain) | |
201 | return; | |
62c476c7 BAY |
202 | |
203 | for (i = 0; i < npages; i++) { | |
19de40a8 | 204 | phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn)); |
260782bc | 205 | pfn = phys >> PAGE_SHIFT; |
62c476c7 BAY |
206 | kvm_release_pfn_clean(pfn); |
207 | gfn++; | |
208 | } | |
260782bc | 209 | |
19de40a8 | 210 | iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages); |
62c476c7 BAY |
211 | } |
212 | ||
213 | static int kvm_iommu_unmap_memslots(struct kvm *kvm) | |
214 | { | |
215 | int i; | |
46a26bf5 MT |
216 | struct kvm_memslots *slots; |
217 | ||
218 | slots = kvm->memslots; | |
682edb4c | 219 | |
46a26bf5 MT |
220 | for (i = 0; i < slots->nmemslots; i++) { |
221 | kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn, | |
222 | slots->memslots[i].npages); | |
62c476c7 | 223 | } |
62c476c7 BAY |
224 | |
225 | return 0; | |
226 | } | |
227 | ||
228 | int kvm_iommu_unmap_guest(struct kvm *kvm) | |
229 | { | |
19de40a8 | 230 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
62c476c7 BAY |
231 | |
232 | /* check if iommu exists and in use */ | |
233 | if (!domain) | |
234 | return 0; | |
235 | ||
62c476c7 | 236 | kvm_iommu_unmap_memslots(kvm); |
19de40a8 | 237 | iommu_domain_free(domain); |
62c476c7 BAY |
238 | return 0; |
239 | } |