2 * Copyright (c) 2006, Intel Corporation.
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.
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
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.
17 * Copyright (C) 2006-2008 Intel Corporation
18 * Copyright IBM Corporation, 2008
19 * Author: Allen M. Kay <allen.m.kay@intel.com>
20 * Author: Weidong Han <weidong.han@intel.com>
21 * Author: Ben-Ami Yassour <benami@il.ibm.com>
24 #include <linux/list.h>
25 #include <linux/kvm_host.h>
26 #include <linux/pci.h>
27 #include <linux/dmar.h>
28 #include <linux/iommu.h>
29 #include <linux/intel-iommu.h>
31 static int kvm_iommu_unmap_memslots(struct kvm *kvm);
32 static void kvm_iommu_put_pages(struct kvm *kvm,
33 gfn_t base_gfn, unsigned long npages);
35 int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
37 gfn_t gfn = slot->base_gfn;
38 unsigned long npages = slot->npages;
41 struct iommu_domain *domain = kvm->arch.iommu_domain;
44 /* check if iommu exists and in use */
48 flags = IOMMU_READ | IOMMU_WRITE;
49 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
52 for (i = 0; i < npages; i++) {
53 /* check if already mapped */
54 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn)))
57 pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
58 r = iommu_map_range(domain,
63 printk(KERN_ERR "kvm_iommu_map_address:"
64 "iommu failed to map pfn=%lx\n", pfn);
72 kvm_iommu_put_pages(kvm, slot->base_gfn, i);
76 static int kvm_iommu_map_memslots(struct kvm *kvm)
79 struct kvm_memslots *slots;
81 slots = rcu_dereference(kvm->memslots);
83 for (i = 0; i < slots->nmemslots; i++) {
84 r = kvm_iommu_map_pages(kvm, &slots->memslots[i]);
92 int kvm_assign_device(struct kvm *kvm,
93 struct kvm_assigned_dev_kernel *assigned_dev)
95 struct pci_dev *pdev = NULL;
96 struct iommu_domain *domain = kvm->arch.iommu_domain;
99 /* check if iommu exists and in use */
103 pdev = assigned_dev->dev;
107 r = iommu_attach_device(domain, &pdev->dev);
109 printk(KERN_ERR "assign device %x:%x.%x failed",
111 PCI_SLOT(pdev->devfn),
112 PCI_FUNC(pdev->devfn));
116 last_flags = kvm->arch.iommu_flags;
117 if (iommu_domain_has_cap(kvm->arch.iommu_domain,
118 IOMMU_CAP_CACHE_COHERENCY))
119 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
121 /* Check if need to update IOMMU page table for guest memory */
122 if ((last_flags ^ kvm->arch.iommu_flags) ==
123 KVM_IOMMU_CACHE_COHERENCY) {
124 kvm_iommu_unmap_memslots(kvm);
125 r = kvm_iommu_map_memslots(kvm);
130 printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n",
131 assigned_dev->host_busnr,
132 PCI_SLOT(assigned_dev->host_devfn),
133 PCI_FUNC(assigned_dev->host_devfn));
137 kvm_iommu_unmap_memslots(kvm);
141 int kvm_deassign_device(struct kvm *kvm,
142 struct kvm_assigned_dev_kernel *assigned_dev)
144 struct iommu_domain *domain = kvm->arch.iommu_domain;
145 struct pci_dev *pdev = NULL;
147 /* check if iommu exists and in use */
151 pdev = assigned_dev->dev;
155 iommu_detach_device(domain, &pdev->dev);
157 printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n",
158 assigned_dev->host_busnr,
159 PCI_SLOT(assigned_dev->host_devfn),
160 PCI_FUNC(assigned_dev->host_devfn));
165 int kvm_iommu_map_guest(struct kvm *kvm)
169 if (!iommu_found()) {
170 printk(KERN_ERR "%s: iommu not found\n", __func__);
174 kvm->arch.iommu_domain = iommu_domain_alloc();
175 if (!kvm->arch.iommu_domain)
178 r = kvm_iommu_map_memslots(kvm);
185 kvm_iommu_unmap_memslots(kvm);
189 static void kvm_iommu_put_pages(struct kvm *kvm,
190 gfn_t base_gfn, unsigned long npages)
192 gfn_t gfn = base_gfn;
194 struct iommu_domain *domain = kvm->arch.iommu_domain;
198 /* check if iommu exists and in use */
202 for (i = 0; i < npages; i++) {
203 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
204 pfn = phys >> PAGE_SHIFT;
205 kvm_release_pfn_clean(pfn);
209 iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
212 static int kvm_iommu_unmap_memslots(struct kvm *kvm)
215 struct kvm_memslots *slots;
217 slots = rcu_dereference(kvm->memslots);
219 for (i = 0; i < slots->nmemslots; i++) {
220 kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
221 slots->memslots[i].npages);
227 int kvm_iommu_unmap_guest(struct kvm *kvm)
229 struct iommu_domain *domain = kvm->arch.iommu_domain;
231 /* check if iommu exists and in use */
235 kvm_iommu_unmap_memslots(kvm);
236 iommu_domain_free(domain);