blob: 3225903ec91be1f732f723262f377e296e60d3f5 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
20 *
21 * Author: Allen M. Kay <allen.m.kay@intel.com>
22 * Author: Weidong Han <weidong.han@intel.com>
23 * Author: Ben-Ami Yassour <benami@il.ibm.com>
24 */
25
26#include <linux/list.h>
27#include <linux/kvm_host.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30#include <linux/stat.h>
31#include <linux/dmar.h>
32#include <linux/iommu.h>
33#include <linux/intel-iommu.h>
34
35static bool allow_unsafe_assigned_interrupts;
36module_param_named(allow_unsafe_assigned_interrupts,
37 allow_unsafe_assigned_interrupts, bool, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(allow_unsafe_assigned_interrupts,
39 "Enable device assignment on platforms without interrupt remapping support.");
40
41static int kvm_iommu_unmap_memslots(struct kvm *kvm);
42static void kvm_iommu_put_pages(struct kvm *kvm,
43 gfn_t base_gfn, unsigned long npages);
44
45static pfn_t kvm_pin_pages(struct kvm *kvm, struct kvm_memory_slot *slot,
46 gfn_t gfn, unsigned long npages)
47{
48 gfn_t end_gfn;
49 pfn_t pfn;
50
51 pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
52 end_gfn = gfn + npages;
53 gfn += 1;
54
55 if (is_error_pfn(pfn))
56 return pfn;
57
58 while (gfn < end_gfn)
59 gfn_to_pfn_memslot(kvm, slot, gfn++);
60
61 return pfn;
62}
63
64static void kvm_unpin_pages(struct kvm *kvm, pfn_t pfn, unsigned long npages)
65{
66 unsigned long i;
67
68 for (i = 0; i < npages; ++i)
69 kvm_release_pfn_clean(pfn + i);
70}
71
72int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
73{
74 gfn_t gfn, end_gfn;
75 pfn_t pfn;
76 int r = 0;
77 struct iommu_domain *domain = kvm->arch.iommu_domain;
78 int flags;
79
80 /* check if iommu exists and in use */
81 if (!domain)
82 return 0;
83
84 gfn = slot->base_gfn;
85 end_gfn = gfn + slot->npages;
86
87 flags = IOMMU_READ | IOMMU_WRITE;
88 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
89 flags |= IOMMU_CACHE;
90
91
92 while (gfn < end_gfn) {
93 unsigned long page_size;
94
95 /* Check if already mapped */
96 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) {
97 gfn += 1;
98 continue;
99 }
100
101 /* Get the page size we could use to map */
102 page_size = kvm_host_page_size(kvm, gfn);
103
104 /* Make sure the page_size does not exceed the memslot */
105 while ((gfn + (page_size >> PAGE_SHIFT)) > end_gfn)
106 page_size >>= 1;
107
108 /* Make sure gfn is aligned to the page size we want to map */
109 while ((gfn << PAGE_SHIFT) & (page_size - 1))
110 page_size >>= 1;
111
112 /* Make sure hva is aligned to the page size we want to map */
113 while (gfn_to_hva_memslot(slot, gfn) & (page_size - 1))
114 page_size >>= 1;
115
116 /*
117 * Pin all pages we are about to map in memory. This is
118 * important because we unmap and unpin in 4kb steps later.
119 */
120 pfn = kvm_pin_pages(kvm, slot, gfn, page_size >> PAGE_SHIFT);
121 if (is_error_pfn(pfn)) {
122 gfn += 1;
123 continue;
124 }
125
126 /* Map into IO address space */
127 r = iommu_map(domain, gfn_to_gpa(gfn), pfn_to_hpa(pfn),
128 page_size, flags);
129 if (r) {
130 printk(KERN_ERR "kvm_iommu_map_address:"
131 "iommu failed to map pfn=%llx\n", pfn);
132 kvm_unpin_pages(kvm, pfn, page_size >> PAGE_SHIFT);
133 goto unmap_pages;
134 }
135
136 gfn += page_size >> PAGE_SHIFT;
137
138
139 }
140
141 return 0;
142
143unmap_pages:
144 kvm_iommu_put_pages(kvm, slot->base_gfn, gfn - slot->base_gfn);
145 return r;
146}
147
148static int kvm_iommu_map_memslots(struct kvm *kvm)
149{
150 int idx, r = 0;
151 struct kvm_memslots *slots;
152 struct kvm_memory_slot *memslot;
153
154 idx = srcu_read_lock(&kvm->srcu);
155 slots = kvm_memslots(kvm);
156
157 kvm_for_each_memslot(memslot, slots) {
158 r = kvm_iommu_map_pages(kvm, memslot);
159 if (r)
160 break;
161 }
162 srcu_read_unlock(&kvm->srcu, idx);
163
164 return r;
165}
166
167int kvm_assign_device(struct kvm *kvm,
168 struct kvm_assigned_dev_kernel *assigned_dev)
169{
170 struct pci_dev *pdev = NULL;
171 struct iommu_domain *domain = kvm->arch.iommu_domain;
172 int r, last_flags;
173
174 /* check if iommu exists and in use */
175 if (!domain)
176 return 0;
177
178 pdev = assigned_dev->dev;
179 if (pdev == NULL)
180 return -ENODEV;
181
182 r = iommu_attach_device(domain, &pdev->dev);
183 if (r) {
184 printk(KERN_ERR "assign device %x:%x:%x.%x failed",
185 pci_domain_nr(pdev->bus),
186 pdev->bus->number,
187 PCI_SLOT(pdev->devfn),
188 PCI_FUNC(pdev->devfn));
189 return r;
190 }
191
192 last_flags = kvm->arch.iommu_flags;
193 if (iommu_domain_has_cap(kvm->arch.iommu_domain,
194 IOMMU_CAP_CACHE_COHERENCY))
195 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
196
197 /* Check if need to update IOMMU page table for guest memory */
198 if ((last_flags ^ kvm->arch.iommu_flags) ==
199 KVM_IOMMU_CACHE_COHERENCY) {
200 kvm_iommu_unmap_memslots(kvm);
201 r = kvm_iommu_map_memslots(kvm);
202 if (r)
203 goto out_unmap;
204 }
205
206 pdev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
207
208 printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
209 assigned_dev->host_segnr,
210 assigned_dev->host_busnr,
211 PCI_SLOT(assigned_dev->host_devfn),
212 PCI_FUNC(assigned_dev->host_devfn));
213
214 return 0;
215out_unmap:
216 kvm_iommu_unmap_memslots(kvm);
217 return r;
218}
219
220int kvm_deassign_device(struct kvm *kvm,
221 struct kvm_assigned_dev_kernel *assigned_dev)
222{
223 struct iommu_domain *domain = kvm->arch.iommu_domain;
224 struct pci_dev *pdev = NULL;
225
226 /* check if iommu exists and in use */
227 if (!domain)
228 return 0;
229
230 pdev = assigned_dev->dev;
231 if (pdev == NULL)
232 return -ENODEV;
233
234 iommu_detach_device(domain, &pdev->dev);
235
236 pdev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
237
238 printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
239 assigned_dev->host_segnr,
240 assigned_dev->host_busnr,
241 PCI_SLOT(assigned_dev->host_devfn),
242 PCI_FUNC(assigned_dev->host_devfn));
243
244 return 0;
245}
246
247int kvm_iommu_map_guest(struct kvm *kvm)
248{
249 int r;
250
251 if (!iommu_present(&pci_bus_type)) {
252 printk(KERN_ERR "%s: iommu not found\n", __func__);
253 return -ENODEV;
254 }
255
256 mutex_lock(&kvm->slots_lock);
257
258 kvm->arch.iommu_domain = iommu_domain_alloc(&pci_bus_type);
259 if (!kvm->arch.iommu_domain) {
260 r = -ENOMEM;
261 goto out_unlock;
262 }
263
264 if (!allow_unsafe_assigned_interrupts &&
265 !iommu_domain_has_cap(kvm->arch.iommu_domain,
266 IOMMU_CAP_INTR_REMAP)) {
267 printk(KERN_WARNING "%s: No interrupt remapping support,"
268 " disallowing device assignment."
269 " Re-enble with \"allow_unsafe_assigned_interrupts=1\""
270 " module option.\n", __func__);
271 iommu_domain_free(kvm->arch.iommu_domain);
272 kvm->arch.iommu_domain = NULL;
273 r = -EPERM;
274 goto out_unlock;
275 }
276
277 r = kvm_iommu_map_memslots(kvm);
278 if (r)
279 kvm_iommu_unmap_memslots(kvm);
280
281out_unlock:
282 mutex_unlock(&kvm->slots_lock);
283 return r;
284}
285
286static void kvm_iommu_put_pages(struct kvm *kvm,
287 gfn_t base_gfn, unsigned long npages)
288{
289 struct iommu_domain *domain;
290 gfn_t end_gfn, gfn;
291 pfn_t pfn;
292 u64 phys;
293
294 domain = kvm->arch.iommu_domain;
295 end_gfn = base_gfn + npages;
296 gfn = base_gfn;
297
298 /* check if iommu exists and in use */
299 if (!domain)
300 return;
301
302 while (gfn < end_gfn) {
303 unsigned long unmap_pages;
304 size_t size;
305
306 /* Get physical address */
307 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
308 pfn = phys >> PAGE_SHIFT;
309
310 /* Unmap address from IO address space */
311 size = iommu_unmap(domain, gfn_to_gpa(gfn), PAGE_SIZE);
312 unmap_pages = 1ULL << get_order(size);
313
314 /* Unpin all pages we just unmapped to not leak any memory */
315 kvm_unpin_pages(kvm, pfn, unmap_pages);
316
317 gfn += unmap_pages;
318 }
319}
320
321void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
322{
323 kvm_iommu_put_pages(kvm, slot->base_gfn, slot->npages);
324}
325
326static int kvm_iommu_unmap_memslots(struct kvm *kvm)
327{
328 int idx;
329 struct kvm_memslots *slots;
330 struct kvm_memory_slot *memslot;
331
332 idx = srcu_read_lock(&kvm->srcu);
333 slots = kvm_memslots(kvm);
334
335 kvm_for_each_memslot(memslot, slots)
336 kvm_iommu_unmap_pages(kvm, memslot);
337
338 srcu_read_unlock(&kvm->srcu, idx);
339
340 return 0;
341}
342
343int kvm_iommu_unmap_guest(struct kvm *kvm)
344{
345 struct iommu_domain *domain = kvm->arch.iommu_domain;
346
347 /* check if iommu exists and in use */
348 if (!domain)
349 return 0;
350
351 mutex_lock(&kvm->slots_lock);
352 kvm_iommu_unmap_memslots(kvm);
353 kvm->arch.iommu_domain = NULL;
354 mutex_unlock(&kvm->slots_lock);
355
356 iommu_domain_free(domain);
357 return 0;
358}