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> |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 28 | #include <linux/iommu.h> |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 29 | #include <linux/intel-iommu.h> |
| 30 | |
| 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); |
| 34 | |
Marcelo Tosatti | 3ad26d8 | 2009-12-23 14:35:20 -0200 | [diff] [blame] | 35 | int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 36 | { |
Marcelo Tosatti | 3ad26d8 | 2009-12-23 14:35:20 -0200 | [diff] [blame] | 37 | gfn_t gfn = slot->base_gfn; |
| 38 | unsigned long npages = slot->npages; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 39 | pfn_t pfn; |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame] | 40 | int i, r = 0; |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 41 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 42 | int flags; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 43 | |
| 44 | /* check if iommu exists and in use */ |
| 45 | if (!domain) |
| 46 | return 0; |
| 47 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 48 | flags = IOMMU_READ | IOMMU_WRITE; |
| 49 | if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY) |
| 50 | flags |= IOMMU_CACHE; |
| 51 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 52 | for (i = 0; i < npages; i++) { |
| 53 | /* check if already mapped */ |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 54 | if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 55 | continue; |
| 56 | |
Marcelo Tosatti | 3ad26d8 | 2009-12-23 14:35:20 -0200 | [diff] [blame] | 57 | pfn = gfn_to_pfn_memslot(kvm, slot, gfn); |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 58 | r = iommu_map_range(domain, |
| 59 | gfn_to_gpa(gfn), |
| 60 | pfn_to_hpa(pfn), |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 61 | PAGE_SIZE, flags); |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame] | 62 | if (r) { |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 63 | printk(KERN_ERR "kvm_iommu_map_address:" |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame] | 64 | "iommu failed to map pfn=%lx\n", pfn); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 65 | goto unmap_pages; |
| 66 | } |
| 67 | gfn++; |
| 68 | } |
| 69 | return 0; |
| 70 | |
| 71 | unmap_pages: |
Marcelo Tosatti | 3ad26d8 | 2009-12-23 14:35:20 -0200 | [diff] [blame] | 72 | kvm_iommu_put_pages(kvm, slot->base_gfn, i); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 73 | return r; |
| 74 | } |
| 75 | |
| 76 | static int kvm_iommu_map_memslots(struct kvm *kvm) |
| 77 | { |
Joerg Roedel | 7398ca7 | 2009-01-03 16:37:53 +0100 | [diff] [blame] | 78 | int i, r = 0; |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 79 | struct kvm_memslots *slots; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 80 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 81 | slots = rcu_dereference(kvm->memslots); |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 82 | |
| 83 | for (i = 0; i < slots->nmemslots; i++) { |
Marcelo Tosatti | 3ad26d8 | 2009-12-23 14:35:20 -0200 | [diff] [blame] | 84 | r = kvm_iommu_map_pages(kvm, &slots->memslots[i]); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 85 | if (r) |
| 86 | break; |
| 87 | } |
Mark McLoughlin | 682edb4 | 2009-02-05 18:23:46 +0000 | [diff] [blame] | 88 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 89 | return r; |
| 90 | } |
| 91 | |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 92 | int kvm_assign_device(struct kvm *kvm, |
| 93 | struct kvm_assigned_dev_kernel *assigned_dev) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 94 | { |
| 95 | struct pci_dev *pdev = NULL; |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 96 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 97 | int r, last_flags; |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 98 | |
| 99 | /* check if iommu exists and in use */ |
| 100 | if (!domain) |
| 101 | return 0; |
| 102 | |
| 103 | pdev = assigned_dev->dev; |
| 104 | if (pdev == NULL) |
| 105 | return -ENODEV; |
| 106 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 107 | r = iommu_attach_device(domain, &pdev->dev); |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 108 | if (r) { |
| 109 | printk(KERN_ERR "assign device %x:%x.%x failed", |
| 110 | pdev->bus->number, |
| 111 | PCI_SLOT(pdev->devfn), |
| 112 | PCI_FUNC(pdev->devfn)); |
| 113 | return r; |
| 114 | } |
| 115 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 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; |
| 120 | |
| 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); |
| 126 | if (r) |
| 127 | goto out_unmap; |
| 128 | } |
| 129 | |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 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)); |
| 134 | |
| 135 | return 0; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 136 | out_unmap: |
| 137 | kvm_iommu_unmap_memslots(kvm); |
| 138 | return r; |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 141 | int kvm_deassign_device(struct kvm *kvm, |
| 142 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 143 | { |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 144 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 145 | struct pci_dev *pdev = NULL; |
| 146 | |
| 147 | /* check if iommu exists and in use */ |
| 148 | if (!domain) |
| 149 | return 0; |
| 150 | |
| 151 | pdev = assigned_dev->dev; |
| 152 | if (pdev == NULL) |
| 153 | return -ENODEV; |
| 154 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 155 | iommu_detach_device(domain, &pdev->dev); |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 156 | |
| 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)); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 165 | int kvm_iommu_map_guest(struct kvm *kvm) |
| 166 | { |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 167 | int r; |
| 168 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 169 | if (!iommu_found()) { |
| 170 | printk(KERN_ERR "%s: iommu not found\n", __func__); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 171 | return -ENODEV; |
| 172 | } |
| 173 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 174 | kvm->arch.iommu_domain = iommu_domain_alloc(); |
| 175 | if (!kvm->arch.iommu_domain) |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 176 | return -ENOMEM; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 177 | |
| 178 | r = kvm_iommu_map_memslots(kvm); |
| 179 | if (r) |
| 180 | goto out_unmap; |
| 181 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 182 | return 0; |
| 183 | |
| 184 | out_unmap: |
| 185 | kvm_iommu_unmap_memslots(kvm); |
| 186 | return r; |
| 187 | } |
| 188 | |
| 189 | static void kvm_iommu_put_pages(struct kvm *kvm, |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 190 | gfn_t base_gfn, unsigned long npages) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 191 | { |
| 192 | gfn_t gfn = base_gfn; |
| 193 | pfn_t pfn; |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 194 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 195 | unsigned long i; |
| 196 | u64 phys; |
| 197 | |
| 198 | /* check if iommu exists and in use */ |
| 199 | if (!domain) |
| 200 | return; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 201 | |
| 202 | for (i = 0; i < npages; i++) { |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 203 | phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn)); |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 204 | pfn = phys >> PAGE_SHIFT; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 205 | kvm_release_pfn_clean(pfn); |
| 206 | gfn++; |
| 207 | } |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 208 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 209 | iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static int kvm_iommu_unmap_memslots(struct kvm *kvm) |
| 213 | { |
| 214 | int i; |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 215 | struct kvm_memslots *slots; |
Mark McLoughlin | 682edb4 | 2009-02-05 18:23:46 +0000 | [diff] [blame] | 216 | |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 217 | slots = rcu_dereference(kvm->memslots); |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 218 | |
| 219 | for (i = 0; i < slots->nmemslots; i++) { |
| 220 | kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn, |
| 221 | slots->memslots[i].npages); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 222 | } |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int kvm_iommu_unmap_guest(struct kvm *kvm) |
| 228 | { |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 229 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 230 | |
| 231 | /* check if iommu exists and in use */ |
| 232 | if (!domain) |
| 233 | return 0; |
| 234 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 235 | kvm_iommu_unmap_memslots(kvm); |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 236 | iommu_domain_free(domain); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 237 | return 0; |
| 238 | } |