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 */
47 for (i = 0; i < npages; i++) {
48 /* check if already mapped */
49 pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
51 if (pfn && !is_mmio_pfn(pfn))
54 pfn = gfn_to_pfn(kvm, gfn);
55 if (!is_mmio_pfn(pfn)) {
56 r = intel_iommu_page_mapping(domain,
63 printk(KERN_DEBUG "kvm_iommu_map_pages:"
64 "iommu failed to map pfn=%lx\n", pfn);
68 printk(KERN_DEBUG "kvm_iommu_map_page:"
69 "invalid pfn=%lx\n", pfn);
77 kvm_iommu_put_pages(kvm, base_gfn, i);
81 static int kvm_iommu_map_memslots(struct kvm *kvm)
85 down_read(&kvm->slots_lock);
86 for (i = 0; i < kvm->nmemslots; i++) {
87 r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
88 kvm->memslots[i].npages);
92 up_read(&kvm->slots_lock);
96 int kvm_iommu_map_guest(struct kvm *kvm,
97 struct kvm_assigned_dev_kernel *assigned_dev)
99 struct pci_dev *pdev = NULL;
102 if (!intel_iommu_found()) {
103 printk(KERN_ERR "%s: intel iommu not found\n", __func__);
107 printk(KERN_DEBUG "VT-d direct map: host bdf = %x:%x:%x\n",
108 assigned_dev->host_busnr,
109 PCI_SLOT(assigned_dev->host_devfn),
110 PCI_FUNC(assigned_dev->host_devfn));
112 pdev = assigned_dev->dev;
115 if (kvm->arch.intel_iommu_domain) {
116 intel_iommu_domain_exit(kvm->arch.intel_iommu_domain);
117 kvm->arch.intel_iommu_domain = NULL;
122 kvm->arch.intel_iommu_domain = intel_iommu_domain_alloc(pdev);
123 if (!kvm->arch.intel_iommu_domain)
126 r = kvm_iommu_map_memslots(kvm);
130 intel_iommu_detach_dev(kvm->arch.intel_iommu_domain,
131 pdev->bus->number, pdev->devfn);
133 r = intel_iommu_context_mapping(kvm->arch.intel_iommu_domain,
136 printk(KERN_ERR "Domain context map for %s failed",
143 kvm_iommu_unmap_memslots(kvm);
147 static void kvm_iommu_put_pages(struct kvm *kvm,
148 gfn_t base_gfn, unsigned long npages)
150 gfn_t gfn = base_gfn;
152 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
155 for (i = 0; i < npages; i++) {
156 pfn = (pfn_t)intel_iommu_iova_to_pfn(domain,
158 kvm_release_pfn_clean(pfn);
163 static int kvm_iommu_unmap_memslots(struct kvm *kvm)
166 down_read(&kvm->slots_lock);
167 for (i = 0; i < kvm->nmemslots; i++) {
168 kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
169 kvm->memslots[i].npages);
171 up_read(&kvm->slots_lock);
176 int kvm_iommu_unmap_guest(struct kvm *kvm)
178 struct kvm_assigned_dev_kernel *entry;
179 struct dmar_domain *domain = kvm->arch.intel_iommu_domain;
181 /* check if iommu exists and in use */
185 list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) {
186 printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n",
188 PCI_SLOT(entry->host_devfn),
189 PCI_FUNC(entry->host_devfn));
191 /* detach kvm dmar domain */
192 intel_iommu_detach_dev(domain, entry->host_busnr,
195 kvm_iommu_unmap_memslots(kvm);
196 intel_iommu_domain_exit(domain);