Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 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 | * 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> |
| 22 | */ |
| 23 | |
| 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> |
| 29 | |
| 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); |
| 33 | |
| 34 | int kvm_iommu_map_pages(struct kvm *kvm, |
| 35 | gfn_t base_gfn, unsigned long npages) |
| 36 | { |
| 37 | gfn_t gfn = base_gfn; |
| 38 | pfn_t pfn; |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame^] | 39 | int i, r = 0; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 40 | struct dmar_domain *domain = kvm->arch.intel_iommu_domain; |
| 41 | |
| 42 | /* check if iommu exists and in use */ |
| 43 | if (!domain) |
| 44 | return 0; |
| 45 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 46 | for (i = 0; i < npages; i++) { |
| 47 | /* check if already mapped */ |
| 48 | pfn = (pfn_t)intel_iommu_iova_to_pfn(domain, |
| 49 | gfn_to_gpa(gfn)); |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame^] | 50 | if (pfn) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 51 | continue; |
| 52 | |
| 53 | pfn = gfn_to_pfn(kvm, gfn); |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame^] | 54 | r = intel_iommu_page_mapping(domain, |
| 55 | gfn_to_gpa(gfn), |
| 56 | pfn_to_hpa(pfn), |
| 57 | PAGE_SIZE, |
| 58 | DMA_PTE_READ | |
| 59 | DMA_PTE_WRITE); |
| 60 | if (r) { |
| 61 | printk(KERN_ERR "kvm_iommu_map_pages:" |
| 62 | "iommu failed to map pfn=%lx\n", pfn); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 63 | goto unmap_pages; |
| 64 | } |
| 65 | gfn++; |
| 66 | } |
| 67 | return 0; |
| 68 | |
| 69 | unmap_pages: |
| 70 | kvm_iommu_put_pages(kvm, base_gfn, i); |
| 71 | return r; |
| 72 | } |
| 73 | |
| 74 | static int kvm_iommu_map_memslots(struct kvm *kvm) |
| 75 | { |
| 76 | int i, r; |
| 77 | |
| 78 | down_read(&kvm->slots_lock); |
| 79 | for (i = 0; i < kvm->nmemslots; i++) { |
| 80 | r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn, |
| 81 | kvm->memslots[i].npages); |
| 82 | if (r) |
| 83 | break; |
| 84 | } |
| 85 | up_read(&kvm->slots_lock); |
| 86 | return r; |
| 87 | } |
| 88 | |
| 89 | int kvm_iommu_map_guest(struct kvm *kvm, |
| 90 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 91 | { |
| 92 | struct pci_dev *pdev = NULL; |
| 93 | int r; |
| 94 | |
| 95 | if (!intel_iommu_found()) { |
| 96 | printk(KERN_ERR "%s: intel iommu not found\n", __func__); |
| 97 | return -ENODEV; |
| 98 | } |
| 99 | |
| 100 | printk(KERN_DEBUG "VT-d direct map: host bdf = %x:%x:%x\n", |
| 101 | assigned_dev->host_busnr, |
| 102 | PCI_SLOT(assigned_dev->host_devfn), |
| 103 | PCI_FUNC(assigned_dev->host_devfn)); |
| 104 | |
| 105 | pdev = assigned_dev->dev; |
| 106 | |
| 107 | if (pdev == NULL) { |
| 108 | if (kvm->arch.intel_iommu_domain) { |
| 109 | intel_iommu_domain_exit(kvm->arch.intel_iommu_domain); |
| 110 | kvm->arch.intel_iommu_domain = NULL; |
| 111 | } |
| 112 | return -ENODEV; |
| 113 | } |
| 114 | |
| 115 | kvm->arch.intel_iommu_domain = intel_iommu_domain_alloc(pdev); |
| 116 | if (!kvm->arch.intel_iommu_domain) |
| 117 | return -ENODEV; |
| 118 | |
| 119 | r = kvm_iommu_map_memslots(kvm); |
| 120 | if (r) |
| 121 | goto out_unmap; |
| 122 | |
| 123 | intel_iommu_detach_dev(kvm->arch.intel_iommu_domain, |
| 124 | pdev->bus->number, pdev->devfn); |
| 125 | |
| 126 | r = intel_iommu_context_mapping(kvm->arch.intel_iommu_domain, |
| 127 | pdev); |
| 128 | if (r) { |
| 129 | printk(KERN_ERR "Domain context map for %s failed", |
| 130 | pci_name(pdev)); |
| 131 | goto out_unmap; |
| 132 | } |
| 133 | return 0; |
| 134 | |
| 135 | out_unmap: |
| 136 | kvm_iommu_unmap_memslots(kvm); |
| 137 | return r; |
| 138 | } |
| 139 | |
| 140 | static void kvm_iommu_put_pages(struct kvm *kvm, |
| 141 | gfn_t base_gfn, unsigned long npages) |
| 142 | { |
| 143 | gfn_t gfn = base_gfn; |
| 144 | pfn_t pfn; |
| 145 | struct dmar_domain *domain = kvm->arch.intel_iommu_domain; |
| 146 | int i; |
| 147 | |
| 148 | for (i = 0; i < npages; i++) { |
| 149 | pfn = (pfn_t)intel_iommu_iova_to_pfn(domain, |
| 150 | gfn_to_gpa(gfn)); |
| 151 | kvm_release_pfn_clean(pfn); |
| 152 | gfn++; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static int kvm_iommu_unmap_memslots(struct kvm *kvm) |
| 157 | { |
| 158 | int i; |
| 159 | down_read(&kvm->slots_lock); |
| 160 | for (i = 0; i < kvm->nmemslots; i++) { |
| 161 | kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn, |
| 162 | kvm->memslots[i].npages); |
| 163 | } |
| 164 | up_read(&kvm->slots_lock); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | int kvm_iommu_unmap_guest(struct kvm *kvm) |
| 170 | { |
| 171 | struct kvm_assigned_dev_kernel *entry; |
| 172 | struct dmar_domain *domain = kvm->arch.intel_iommu_domain; |
| 173 | |
| 174 | /* check if iommu exists and in use */ |
| 175 | if (!domain) |
| 176 | return 0; |
| 177 | |
| 178 | list_for_each_entry(entry, &kvm->arch.assigned_dev_head, list) { |
| 179 | printk(KERN_DEBUG "VT-d unmap: host bdf = %x:%x:%x\n", |
| 180 | entry->host_busnr, |
| 181 | PCI_SLOT(entry->host_devfn), |
| 182 | PCI_FUNC(entry->host_devfn)); |
| 183 | |
| 184 | /* detach kvm dmar domain */ |
| 185 | intel_iommu_detach_dev(domain, entry->host_busnr, |
| 186 | entry->host_devfn); |
| 187 | } |
| 188 | kvm_iommu_unmap_memslots(kvm); |
| 189 | intel_iommu_domain_exit(domain); |
| 190 | return 0; |
| 191 | } |