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 | |
| 35 | int 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; |
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; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 42 | |
| 43 | /* check if iommu exists and in use */ |
| 44 | if (!domain) |
| 45 | return 0; |
| 46 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 47 | for (i = 0; i < npages; i++) { |
| 48 | /* check if already mapped */ |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 49 | if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn))) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 50 | continue; |
| 51 | |
| 52 | pfn = gfn_to_pfn(kvm, gfn); |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 53 | r = iommu_map_range(domain, |
| 54 | gfn_to_gpa(gfn), |
| 55 | pfn_to_hpa(pfn), |
| 56 | PAGE_SIZE, |
| 57 | IOMMU_READ | IOMMU_WRITE); |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame] | 58 | if (r) { |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 59 | printk(KERN_ERR "kvm_iommu_map_address:" |
Weidong Han | e5fcfc8 | 2008-09-25 23:32:02 +0800 | [diff] [blame] | 60 | "iommu failed to map pfn=%lx\n", pfn); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 61 | goto unmap_pages; |
| 62 | } |
| 63 | gfn++; |
| 64 | } |
| 65 | return 0; |
| 66 | |
| 67 | unmap_pages: |
| 68 | kvm_iommu_put_pages(kvm, base_gfn, i); |
| 69 | return r; |
| 70 | } |
| 71 | |
| 72 | static int kvm_iommu_map_memslots(struct kvm *kvm) |
| 73 | { |
Joerg Roedel | 7398ca7 | 2009-01-03 16:37:53 +0100 | [diff] [blame] | 74 | int i, r = 0; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 75 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 76 | for (i = 0; i < kvm->nmemslots; i++) { |
| 77 | r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn, |
| 78 | kvm->memslots[i].npages); |
| 79 | if (r) |
| 80 | break; |
| 81 | } |
Mark McLoughlin | 682edb4 | 2009-02-05 18:23:46 +0000 | [diff] [blame^] | 82 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 83 | return r; |
| 84 | } |
| 85 | |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 86 | int kvm_assign_device(struct kvm *kvm, |
| 87 | struct kvm_assigned_dev_kernel *assigned_dev) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 88 | { |
| 89 | struct pci_dev *pdev = NULL; |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 90 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 91 | int r; |
| 92 | |
| 93 | /* check if iommu exists and in use */ |
| 94 | if (!domain) |
| 95 | return 0; |
| 96 | |
| 97 | pdev = assigned_dev->dev; |
| 98 | if (pdev == NULL) |
| 99 | return -ENODEV; |
| 100 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 101 | r = iommu_attach_device(domain, &pdev->dev); |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 102 | if (r) { |
| 103 | printk(KERN_ERR "assign device %x:%x.%x failed", |
| 104 | pdev->bus->number, |
| 105 | PCI_SLOT(pdev->devfn), |
| 106 | PCI_FUNC(pdev->devfn)); |
| 107 | return r; |
| 108 | } |
| 109 | |
| 110 | printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n", |
| 111 | assigned_dev->host_busnr, |
| 112 | PCI_SLOT(assigned_dev->host_devfn), |
| 113 | PCI_FUNC(assigned_dev->host_devfn)); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 118 | int kvm_deassign_device(struct kvm *kvm, |
| 119 | struct kvm_assigned_dev_kernel *assigned_dev) |
| 120 | { |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 121 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 122 | struct pci_dev *pdev = NULL; |
| 123 | |
| 124 | /* check if iommu exists and in use */ |
| 125 | if (!domain) |
| 126 | return 0; |
| 127 | |
| 128 | pdev = assigned_dev->dev; |
| 129 | if (pdev == NULL) |
| 130 | return -ENODEV; |
| 131 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 132 | iommu_detach_device(domain, &pdev->dev); |
Weidong Han | 0a92035 | 2008-12-02 21:24:23 +0800 | [diff] [blame] | 133 | |
| 134 | printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n", |
| 135 | assigned_dev->host_busnr, |
| 136 | PCI_SLOT(assigned_dev->host_devfn), |
| 137 | PCI_FUNC(assigned_dev->host_devfn)); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 142 | int kvm_iommu_map_guest(struct kvm *kvm) |
| 143 | { |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 144 | int r; |
| 145 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 146 | if (!iommu_found()) { |
| 147 | printk(KERN_ERR "%s: iommu not found\n", __func__); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 148 | return -ENODEV; |
| 149 | } |
| 150 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 151 | kvm->arch.iommu_domain = iommu_domain_alloc(); |
| 152 | if (!kvm->arch.iommu_domain) |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 153 | return -ENOMEM; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 154 | |
| 155 | r = kvm_iommu_map_memslots(kvm); |
| 156 | if (r) |
| 157 | goto out_unmap; |
| 158 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 159 | return 0; |
| 160 | |
| 161 | out_unmap: |
| 162 | kvm_iommu_unmap_memslots(kvm); |
| 163 | return r; |
| 164 | } |
| 165 | |
| 166 | static void kvm_iommu_put_pages(struct kvm *kvm, |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 167 | gfn_t base_gfn, unsigned long npages) |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 168 | { |
| 169 | gfn_t gfn = base_gfn; |
| 170 | pfn_t pfn; |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 171 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 172 | unsigned long i; |
| 173 | u64 phys; |
| 174 | |
| 175 | /* check if iommu exists and in use */ |
| 176 | if (!domain) |
| 177 | return; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 178 | |
| 179 | for (i = 0; i < npages; i++) { |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 180 | phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn)); |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 181 | pfn = phys >> PAGE_SHIFT; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 182 | kvm_release_pfn_clean(pfn); |
| 183 | gfn++; |
| 184 | } |
Weidong Han | 260782b | 2008-12-02 21:03:39 +0800 | [diff] [blame] | 185 | |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 186 | 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] | 187 | } |
| 188 | |
| 189 | static int kvm_iommu_unmap_memslots(struct kvm *kvm) |
| 190 | { |
| 191 | int i; |
Mark McLoughlin | 682edb4 | 2009-02-05 18:23:46 +0000 | [diff] [blame^] | 192 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 193 | for (i = 0; i < kvm->nmemslots; i++) { |
| 194 | kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn, |
| 195 | kvm->memslots[i].npages); |
| 196 | } |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | int kvm_iommu_unmap_guest(struct kvm *kvm) |
| 202 | { |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 203 | struct iommu_domain *domain = kvm->arch.iommu_domain; |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 204 | |
| 205 | /* check if iommu exists and in use */ |
| 206 | if (!domain) |
| 207 | return 0; |
| 208 | |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 209 | kvm_iommu_unmap_memslots(kvm); |
Joerg Roedel | 19de40a | 2008-12-03 14:43:34 +0100 | [diff] [blame] | 210 | iommu_domain_free(domain); |
Ben-Ami Yassour | 62c476c | 2008-09-14 03:48:28 +0300 | [diff] [blame] | 211 | return 0; |
| 212 | } |