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