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