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