c946700927fa7c605e5227d216da7a3ba05a80ed
[pandora-kernel.git] / virt / kvm / iommu.c
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  * 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
35 static int allow_unsafe_assigned_interrupts;
36 module_param_named(allow_unsafe_assigned_interrupts,
37                    allow_unsafe_assigned_interrupts, bool, S_IRUGO | S_IWUSR);
38 MODULE_PARM_DESC(allow_unsafe_assigned_interrupts,
39  "Enable device assignment on platforms without interrupt remapping support.");
40
41 static int kvm_iommu_unmap_memslots(struct kvm *kvm);
42 static void kvm_iommu_put_pages(struct kvm *kvm,
43                                 gfn_t base_gfn, unsigned long npages);
44
45 static pfn_t kvm_pin_pages(struct kvm *kvm, struct kvm_memory_slot *slot,
46                            gfn_t gfn, unsigned long size)
47 {
48         gfn_t end_gfn;
49         pfn_t pfn;
50
51         pfn     = gfn_to_pfn_memslot(kvm, slot, gfn);
52         end_gfn = gfn + (size >> PAGE_SHIFT);
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
64 static 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
72 int 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);
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                               get_order(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);
133                         goto unmap_pages;
134                 }
135
136                 gfn += page_size >> PAGE_SHIFT;
137
138
139         }
140
141         return 0;
142
143 unmap_pages:
144         kvm_iommu_put_pages(kvm, slot->base_gfn, gfn - slot->base_gfn);
145         return r;
146 }
147
148 static int kvm_iommu_map_memslots(struct kvm *kvm)
149 {
150         int i, idx, r = 0;
151         struct kvm_memslots *slots;
152
153         idx = srcu_read_lock(&kvm->srcu);
154         slots = kvm_memslots(kvm);
155
156         for (i = 0; i < slots->nmemslots; i++) {
157                 r = kvm_iommu_map_pages(kvm, &slots->memslots[i]);
158                 if (r)
159                         break;
160         }
161         srcu_read_unlock(&kvm->srcu, idx);
162
163         return r;
164 }
165
166 int kvm_assign_device(struct kvm *kvm,
167                       struct kvm_assigned_dev_kernel *assigned_dev)
168 {
169         struct pci_dev *pdev = NULL;
170         struct iommu_domain *domain = kvm->arch.iommu_domain;
171         int r, last_flags;
172
173         /* check if iommu exists and in use */
174         if (!domain)
175                 return 0;
176
177         pdev = assigned_dev->dev;
178         if (pdev == NULL)
179                 return -ENODEV;
180
181         r = iommu_attach_device(domain, &pdev->dev);
182         if (r) {
183                 printk(KERN_ERR "assign device %x:%x:%x.%x failed",
184                         pci_domain_nr(pdev->bus),
185                         pdev->bus->number,
186                         PCI_SLOT(pdev->devfn),
187                         PCI_FUNC(pdev->devfn));
188                 return r;
189         }
190
191         last_flags = kvm->arch.iommu_flags;
192         if (iommu_domain_has_cap(kvm->arch.iommu_domain,
193                                  IOMMU_CAP_CACHE_COHERENCY))
194                 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
195
196         /* Check if need to update IOMMU page table for guest memory */
197         if ((last_flags ^ kvm->arch.iommu_flags) ==
198                         KVM_IOMMU_CACHE_COHERENCY) {
199                 kvm_iommu_unmap_memslots(kvm);
200                 r = kvm_iommu_map_memslots(kvm);
201                 if (r)
202                         goto out_unmap;
203         }
204
205         pdev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
206
207         printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
208                 assigned_dev->host_segnr,
209                 assigned_dev->host_busnr,
210                 PCI_SLOT(assigned_dev->host_devfn),
211                 PCI_FUNC(assigned_dev->host_devfn));
212
213         return 0;
214 out_unmap:
215         kvm_iommu_unmap_memslots(kvm);
216         return r;
217 }
218
219 int kvm_deassign_device(struct kvm *kvm,
220                         struct kvm_assigned_dev_kernel *assigned_dev)
221 {
222         struct iommu_domain *domain = kvm->arch.iommu_domain;
223         struct pci_dev *pdev = NULL;
224
225         /* check if iommu exists and in use */
226         if (!domain)
227                 return 0;
228
229         pdev = assigned_dev->dev;
230         if (pdev == NULL)
231                 return -ENODEV;
232
233         iommu_detach_device(domain, &pdev->dev);
234
235         pdev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
236
237         printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
238                 assigned_dev->host_segnr,
239                 assigned_dev->host_busnr,
240                 PCI_SLOT(assigned_dev->host_devfn),
241                 PCI_FUNC(assigned_dev->host_devfn));
242
243         return 0;
244 }
245
246 int kvm_iommu_map_guest(struct kvm *kvm)
247 {
248         int r;
249
250         if (!iommu_present(&pci_bus_type)) {
251                 printk(KERN_ERR "%s: iommu not found\n", __func__);
252                 return -ENODEV;
253         }
254
255         mutex_lock(&kvm->slots_lock);
256
257         kvm->arch.iommu_domain = iommu_domain_alloc(&pci_bus_type);
258         if (!kvm->arch.iommu_domain) {
259                 r = -ENOMEM;
260                 goto out_unlock;
261         }
262
263         if (!allow_unsafe_assigned_interrupts &&
264             !iommu_domain_has_cap(kvm->arch.iommu_domain,
265                                   IOMMU_CAP_INTR_REMAP)) {
266                 printk(KERN_WARNING "%s: No interrupt remapping support,"
267                        " disallowing device assignment."
268                        " Re-enble with \"allow_unsafe_assigned_interrupts=1\""
269                        " module option.\n", __func__);
270                 iommu_domain_free(kvm->arch.iommu_domain);
271                 kvm->arch.iommu_domain = NULL;
272                 r = -EPERM;
273                 goto out_unlock;
274         }
275
276         r = kvm_iommu_map_memslots(kvm);
277         if (r)
278                 kvm_iommu_unmap_memslots(kvm);
279
280 out_unlock:
281         mutex_unlock(&kvm->slots_lock);
282         return r;
283 }
284
285 static void kvm_iommu_put_pages(struct kvm *kvm,
286                                 gfn_t base_gfn, unsigned long npages)
287 {
288         struct iommu_domain *domain;
289         gfn_t end_gfn, gfn;
290         pfn_t pfn;
291         u64 phys;
292
293         domain  = kvm->arch.iommu_domain;
294         end_gfn = base_gfn + npages;
295         gfn     = base_gfn;
296
297         /* check if iommu exists and in use */
298         if (!domain)
299                 return;
300
301         while (gfn < end_gfn) {
302                 unsigned long unmap_pages;
303                 int order;
304
305                 /* Get physical address */
306                 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
307                 pfn  = phys >> PAGE_SHIFT;
308
309                 /* Unmap address from IO address space */
310                 order       = iommu_unmap(domain, gfn_to_gpa(gfn), 0);
311                 unmap_pages = 1ULL << order;
312
313                 /* Unpin all pages we just unmapped to not leak any memory */
314                 kvm_unpin_pages(kvm, pfn, unmap_pages);
315
316                 gfn += unmap_pages;
317         }
318 }
319
320 void kvm_iommu_unmap_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
321 {
322         kvm_iommu_put_pages(kvm, slot->base_gfn, slot->npages);
323 }
324
325 static int kvm_iommu_unmap_memslots(struct kvm *kvm)
326 {
327         int i, idx;
328         struct kvm_memslots *slots;
329
330         idx = srcu_read_lock(&kvm->srcu);
331         slots = kvm_memslots(kvm);
332
333         for (i = 0; i < slots->nmemslots; i++)
334                 kvm_iommu_unmap_pages(kvm, &slots->memslots[i]);
335
336         srcu_read_unlock(&kvm->srcu, idx);
337
338         return 0;
339 }
340
341 int kvm_iommu_unmap_guest(struct kvm *kvm)
342 {
343         struct iommu_domain *domain = kvm->arch.iommu_domain;
344
345         /* check if iommu exists and in use */
346         if (!domain)
347                 return 0;
348
349         mutex_lock(&kvm->slots_lock);
350         kvm_iommu_unmap_memslots(kvm);
351         kvm->arch.iommu_domain = NULL;
352         mutex_unlock(&kvm->slots_lock);
353
354         iommu_domain_free(domain);
355         return 0;
356 }