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/intel-iommu.h>
30 static int kvm_iommu_unmap_memslots(struct kvm *kvm);
31 static void kvm_iommu_put_pages(struct kvm *kvm,
32 gfn_t base_gfn, unsigned long npages);
34 int kvm_iommu_map_pages(struct kvm *kvm,
35 gfn_t base_gfn, unsigned long npages)
40 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
42 /* check if iommu exists and in use */
46 for (i = 0; i < npages; i++) {
47 /* check if already mapped */
48 if (intel_iommu_iova_to_phys(domain,
52 pfn = gfn_to_pfn(kvm, gfn);
53 r = intel_iommu_map_address(domain,
57 DMA_PTE_READ | DMA_PTE_WRITE);
59 printk(KERN_ERR "kvm_iommu_map_address:"
60 "iommu failed to map pfn=%lx\n", pfn);
68 kvm_iommu_put_pages(kvm, base_gfn, i);
72 static int kvm_iommu_map_memslots(struct kvm *kvm)
76 down_read(&kvm->slots_lock);
77 for (i = 0; i < kvm->nmemslots; i++) {
78 r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
79 kvm->memslots[i].npages);
83 up_read(&kvm->slots_lock);
87 int kvm_assign_device(struct kvm *kvm,
88 struct kvm_assigned_dev_kernel *assigned_dev)
90 struct pci_dev *pdev = NULL;
91 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
94 /* check if iommu exists and in use */
98 pdev = assigned_dev->dev;
102 r = intel_iommu_attach_device(domain, pdev);
104 printk(KERN_ERR "assign device %x:%x.%x failed",
106 PCI_SLOT(pdev->devfn),
107 PCI_FUNC(pdev->devfn));
111 printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n",
112 assigned_dev->host_busnr,
113 PCI_SLOT(assigned_dev->host_devfn),
114 PCI_FUNC(assigned_dev->host_devfn));
119 int kvm_deassign_device(struct kvm *kvm,
120 struct kvm_assigned_dev_kernel *assigned_dev)
122 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
123 struct pci_dev *pdev = NULL;
125 /* check if iommu exists and in use */
129 pdev = assigned_dev->dev;
133 intel_iommu_detach_device(domain, pdev);
135 printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n",
136 assigned_dev->host_busnr,
137 PCI_SLOT(assigned_dev->host_devfn),
138 PCI_FUNC(assigned_dev->host_devfn));
143 int kvm_iommu_map_guest(struct kvm *kvm)
147 if (!intel_iommu_found()) {
148 printk(KERN_ERR "%s: intel iommu not found\n", __func__);
152 kvm->arch.intel_iommu_domain = intel_iommu_alloc_domain();
153 if (!kvm->arch.intel_iommu_domain)
156 r = kvm_iommu_map_memslots(kvm);
163 kvm_iommu_unmap_memslots(kvm);
167 static void kvm_iommu_put_pages(struct kvm *kvm,
168 gfn_t base_gfn, unsigned long npages)
170 gfn_t gfn = base_gfn;
172 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
176 /* check if iommu exists and in use */
180 for (i = 0; i < npages; i++) {
181 phys = intel_iommu_iova_to_phys(domain,
183 pfn = phys >> PAGE_SHIFT;
184 kvm_release_pfn_clean(pfn);
188 intel_iommu_unmap_address(domain,
189 gfn_to_gpa(base_gfn),
193 static int kvm_iommu_unmap_memslots(struct kvm *kvm)
196 down_read(&kvm->slots_lock);
197 for (i = 0; i < kvm->nmemslots; i++) {
198 kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
199 kvm->memslots[i].npages);
201 up_read(&kvm->slots_lock);
206 int kvm_iommu_unmap_guest(struct kvm *kvm)
208 struct kvm_assigned_dev_kernel *entry;
209 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
211 /* check if iommu exists and in use */
215 list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) {
216 printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n",
218 PCI_SLOT(entry->host_devfn),
219 PCI_FUNC(entry->host_devfn));
221 /* detach kvm dmar domain */
222 intel_iommu_detach_device(domain, entry->dev);
224 kvm_iommu_unmap_memslots(kvm);
225 intel_iommu_free_domain(domain);