Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.31-rc3 233 lines 5.4 kB view raw
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/iommu.h> 29#include <linux/intel-iommu.h> 30 31static int kvm_iommu_unmap_memslots(struct kvm *kvm); 32static void kvm_iommu_put_pages(struct kvm *kvm, 33 gfn_t base_gfn, unsigned long npages); 34 35int kvm_iommu_map_pages(struct kvm *kvm, 36 gfn_t base_gfn, unsigned long npages) 37{ 38 gfn_t gfn = base_gfn; 39 pfn_t pfn; 40 int i, r = 0; 41 struct iommu_domain *domain = kvm->arch.iommu_domain; 42 int flags; 43 44 /* check if iommu exists and in use */ 45 if (!domain) 46 return 0; 47 48 flags = IOMMU_READ | IOMMU_WRITE; 49 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY) 50 flags |= IOMMU_CACHE; 51 52 for (i = 0; i < npages; i++) { 53 /* check if already mapped */ 54 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) 55 continue; 56 57 pfn = gfn_to_pfn(kvm, gfn); 58 r = iommu_map_range(domain, 59 gfn_to_gpa(gfn), 60 pfn_to_hpa(pfn), 61 PAGE_SIZE, flags); 62 if (r) { 63 printk(KERN_ERR "kvm_iommu_map_address:" 64 "iommu failed to map pfn=%lx\n", pfn); 65 goto unmap_pages; 66 } 67 gfn++; 68 } 69 return 0; 70 71unmap_pages: 72 kvm_iommu_put_pages(kvm, base_gfn, i); 73 return r; 74} 75 76static int kvm_iommu_map_memslots(struct kvm *kvm) 77{ 78 int i, r = 0; 79 80 for (i = 0; i < kvm->nmemslots; i++) { 81 r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn, 82 kvm->memslots[i].npages); 83 if (r) 84 break; 85 } 86 87 return r; 88} 89 90int kvm_assign_device(struct kvm *kvm, 91 struct kvm_assigned_dev_kernel *assigned_dev) 92{ 93 struct pci_dev *pdev = NULL; 94 struct iommu_domain *domain = kvm->arch.iommu_domain; 95 int r, last_flags; 96 97 /* check if iommu exists and in use */ 98 if (!domain) 99 return 0; 100 101 pdev = assigned_dev->dev; 102 if (pdev == NULL) 103 return -ENODEV; 104 105 r = iommu_attach_device(domain, &pdev->dev); 106 if (r) { 107 printk(KERN_ERR "assign device %x:%x.%x failed", 108 pdev->bus->number, 109 PCI_SLOT(pdev->devfn), 110 PCI_FUNC(pdev->devfn)); 111 return r; 112 } 113 114 last_flags = kvm->arch.iommu_flags; 115 if (iommu_domain_has_cap(kvm->arch.iommu_domain, 116 IOMMU_CAP_CACHE_COHERENCY)) 117 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY; 118 119 /* Check if need to update IOMMU page table for guest memory */ 120 if ((last_flags ^ kvm->arch.iommu_flags) == 121 KVM_IOMMU_CACHE_COHERENCY) { 122 kvm_iommu_unmap_memslots(kvm); 123 r = kvm_iommu_map_memslots(kvm); 124 if (r) 125 goto out_unmap; 126 } 127 128 printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n", 129 assigned_dev->host_busnr, 130 PCI_SLOT(assigned_dev->host_devfn), 131 PCI_FUNC(assigned_dev->host_devfn)); 132 133 return 0; 134out_unmap: 135 kvm_iommu_unmap_memslots(kvm); 136 return r; 137} 138 139int kvm_deassign_device(struct kvm *kvm, 140 struct kvm_assigned_dev_kernel *assigned_dev) 141{ 142 struct iommu_domain *domain = kvm->arch.iommu_domain; 143 struct pci_dev *pdev = NULL; 144 145 /* check if iommu exists and in use */ 146 if (!domain) 147 return 0; 148 149 pdev = assigned_dev->dev; 150 if (pdev == NULL) 151 return -ENODEV; 152 153 iommu_detach_device(domain, &pdev->dev); 154 155 printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n", 156 assigned_dev->host_busnr, 157 PCI_SLOT(assigned_dev->host_devfn), 158 PCI_FUNC(assigned_dev->host_devfn)); 159 160 return 0; 161} 162 163int kvm_iommu_map_guest(struct kvm *kvm) 164{ 165 int r; 166 167 if (!iommu_found()) { 168 printk(KERN_ERR "%s: iommu not found\n", __func__); 169 return -ENODEV; 170 } 171 172 kvm->arch.iommu_domain = iommu_domain_alloc(); 173 if (!kvm->arch.iommu_domain) 174 return -ENOMEM; 175 176 r = kvm_iommu_map_memslots(kvm); 177 if (r) 178 goto out_unmap; 179 180 return 0; 181 182out_unmap: 183 kvm_iommu_unmap_memslots(kvm); 184 return r; 185} 186 187static void kvm_iommu_put_pages(struct kvm *kvm, 188 gfn_t base_gfn, unsigned long npages) 189{ 190 gfn_t gfn = base_gfn; 191 pfn_t pfn; 192 struct iommu_domain *domain = kvm->arch.iommu_domain; 193 unsigned long i; 194 u64 phys; 195 196 /* check if iommu exists and in use */ 197 if (!domain) 198 return; 199 200 for (i = 0; i < npages; i++) { 201 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn)); 202 pfn = phys >> PAGE_SHIFT; 203 kvm_release_pfn_clean(pfn); 204 gfn++; 205 } 206 207 iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages); 208} 209 210static int kvm_iommu_unmap_memslots(struct kvm *kvm) 211{ 212 int i; 213 214 for (i = 0; i < kvm->nmemslots; i++) { 215 kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn, 216 kvm->memslots[i].npages); 217 } 218 219 return 0; 220} 221 222int kvm_iommu_unmap_guest(struct kvm *kvm) 223{ 224 struct iommu_domain *domain = kvm->arch.iommu_domain; 225 226 /* check if iommu exists and in use */ 227 if (!domain) 228 return 0; 229 230 kvm_iommu_unmap_memslots(kvm); 231 iommu_domain_free(domain); 232 return 0; 233}