blob: 37ca71ebdba88645ad3a79eec1197998fcbc7dd5 [file] [log] [blame]
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +03001/*
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 Roedel19de40a2008-12-03 14:43:34 +010028#include <linux/iommu.h>
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030029#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
Marcelo Tosatti3ad26d82009-12-23 14:35:20 -020035int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030036{
Marcelo Tosatti3ad26d82009-12-23 14:35:20 -020037 gfn_t gfn = slot->base_gfn;
38 unsigned long npages = slot->npages;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030039 pfn_t pfn;
Weidong Hane5fcfc82008-09-25 23:32:02 +080040 int i, r = 0;
Joerg Roedel19de40a2008-12-03 14:43:34 +010041 struct iommu_domain *domain = kvm->arch.iommu_domain;
Sheng Yang522c68c2009-04-27 20:35:43 +080042 int flags;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030043
44 /* check if iommu exists and in use */
45 if (!domain)
46 return 0;
47
Sheng Yang522c68c2009-04-27 20:35:43 +080048 flags = IOMMU_READ | IOMMU_WRITE;
49 if (kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)
50 flags |= IOMMU_CACHE;
51
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030052 for (i = 0; i < npages; i++) {
53 /* check if already mapped */
Joerg Roedel19de40a2008-12-03 14:43:34 +010054 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn)))
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030055 continue;
56
Marcelo Tosatti3ad26d82009-12-23 14:35:20 -020057 pfn = gfn_to_pfn_memslot(kvm, slot, gfn);
Joerg Roedel19de40a2008-12-03 14:43:34 +010058 r = iommu_map_range(domain,
59 gfn_to_gpa(gfn),
60 pfn_to_hpa(pfn),
Sheng Yang522c68c2009-04-27 20:35:43 +080061 PAGE_SIZE, flags);
Weidong Hane5fcfc82008-09-25 23:32:02 +080062 if (r) {
Weidong Han260782b2008-12-02 21:03:39 +080063 printk(KERN_ERR "kvm_iommu_map_address:"
Weidong Hane5fcfc82008-09-25 23:32:02 +080064 "iommu failed to map pfn=%lx\n", pfn);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030065 goto unmap_pages;
66 }
67 gfn++;
68 }
69 return 0;
70
71unmap_pages:
Marcelo Tosatti3ad26d82009-12-23 14:35:20 -020072 kvm_iommu_put_pages(kvm, slot->base_gfn, i);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030073 return r;
74}
75
76static int kvm_iommu_map_memslots(struct kvm *kvm)
77{
Joerg Roedel7398ca72009-01-03 16:37:53 +010078 int i, r = 0;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -020079 struct kvm_memslots *slots;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030080
Lai Jiangshan90d83dc2010-04-19 17:41:23 +080081 slots = kvm_memslots(kvm);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -020082
83 for (i = 0; i < slots->nmemslots; i++) {
Marcelo Tosatti3ad26d82009-12-23 14:35:20 -020084 r = kvm_iommu_map_pages(kvm, &slots->memslots[i]);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030085 if (r)
86 break;
87 }
Mark McLoughlin682edb42009-02-05 18:23:46 +000088
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030089 return r;
90}
91
Weidong Han260782b2008-12-02 21:03:39 +080092int kvm_assign_device(struct kvm *kvm,
93 struct kvm_assigned_dev_kernel *assigned_dev)
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030094{
95 struct pci_dev *pdev = NULL;
Joerg Roedel19de40a2008-12-03 14:43:34 +010096 struct iommu_domain *domain = kvm->arch.iommu_domain;
Sheng Yang522c68c2009-04-27 20:35:43 +080097 int r, last_flags;
Weidong Han260782b2008-12-02 21:03:39 +080098
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 Roedel19de40a2008-12-03 14:43:34 +0100107 r = iommu_attach_device(domain, &pdev->dev);
Weidong Han260782b2008-12-02 21:03:39 +0800108 if (r) {
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +0800109 printk(KERN_ERR "assign device %x:%x:%x.%x failed",
110 pci_domain_nr(pdev->bus),
Weidong Han260782b2008-12-02 21:03:39 +0800111 pdev->bus->number,
112 PCI_SLOT(pdev->devfn),
113 PCI_FUNC(pdev->devfn));
114 return r;
115 }
116
Sheng Yang522c68c2009-04-27 20:35:43 +0800117 last_flags = kvm->arch.iommu_flags;
118 if (iommu_domain_has_cap(kvm->arch.iommu_domain,
119 IOMMU_CAP_CACHE_COHERENCY))
120 kvm->arch.iommu_flags |= KVM_IOMMU_CACHE_COHERENCY;
121
122 /* Check if need to update IOMMU page table for guest memory */
123 if ((last_flags ^ kvm->arch.iommu_flags) ==
124 KVM_IOMMU_CACHE_COHERENCY) {
125 kvm_iommu_unmap_memslots(kvm);
126 r = kvm_iommu_map_memslots(kvm);
127 if (r)
128 goto out_unmap;
129 }
130
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +0800131 printk(KERN_DEBUG "assign device %x:%x:%x.%x\n",
132 assigned_dev->host_segnr,
Weidong Han260782b2008-12-02 21:03:39 +0800133 assigned_dev->host_busnr,
134 PCI_SLOT(assigned_dev->host_devfn),
135 PCI_FUNC(assigned_dev->host_devfn));
136
137 return 0;
Sheng Yang522c68c2009-04-27 20:35:43 +0800138out_unmap:
139 kvm_iommu_unmap_memslots(kvm);
140 return r;
Weidong Han260782b2008-12-02 21:03:39 +0800141}
142
Weidong Han0a920352008-12-02 21:24:23 +0800143int kvm_deassign_device(struct kvm *kvm,
144 struct kvm_assigned_dev_kernel *assigned_dev)
145{
Joerg Roedel19de40a2008-12-03 14:43:34 +0100146 struct iommu_domain *domain = kvm->arch.iommu_domain;
Weidong Han0a920352008-12-02 21:24:23 +0800147 struct pci_dev *pdev = NULL;
148
149 /* check if iommu exists and in use */
150 if (!domain)
151 return 0;
152
153 pdev = assigned_dev->dev;
154 if (pdev == NULL)
155 return -ENODEV;
156
Joerg Roedel19de40a2008-12-03 14:43:34 +0100157 iommu_detach_device(domain, &pdev->dev);
Weidong Han0a920352008-12-02 21:24:23 +0800158
Zhai, Edwinab9f4ec2010-01-29 14:38:44 +0800159 printk(KERN_DEBUG "deassign device %x:%x:%x.%x\n",
160 assigned_dev->host_segnr,
Weidong Han0a920352008-12-02 21:24:23 +0800161 assigned_dev->host_busnr,
162 PCI_SLOT(assigned_dev->host_devfn),
163 PCI_FUNC(assigned_dev->host_devfn));
164
165 return 0;
166}
167
Weidong Han260782b2008-12-02 21:03:39 +0800168int kvm_iommu_map_guest(struct kvm *kvm)
169{
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300170 int r;
171
Joerg Roedel19de40a2008-12-03 14:43:34 +0100172 if (!iommu_found()) {
173 printk(KERN_ERR "%s: iommu not found\n", __func__);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300174 return -ENODEV;
175 }
176
Joerg Roedel19de40a2008-12-03 14:43:34 +0100177 kvm->arch.iommu_domain = iommu_domain_alloc();
178 if (!kvm->arch.iommu_domain)
Weidong Han260782b2008-12-02 21:03:39 +0800179 return -ENOMEM;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300180
181 r = kvm_iommu_map_memslots(kvm);
182 if (r)
183 goto out_unmap;
184
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300185 return 0;
186
187out_unmap:
188 kvm_iommu_unmap_memslots(kvm);
189 return r;
190}
191
192static void kvm_iommu_put_pages(struct kvm *kvm,
Weidong Han260782b2008-12-02 21:03:39 +0800193 gfn_t base_gfn, unsigned long npages)
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300194{
195 gfn_t gfn = base_gfn;
196 pfn_t pfn;
Joerg Roedel19de40a2008-12-03 14:43:34 +0100197 struct iommu_domain *domain = kvm->arch.iommu_domain;
Weidong Han260782b2008-12-02 21:03:39 +0800198 unsigned long i;
199 u64 phys;
200
201 /* check if iommu exists and in use */
202 if (!domain)
203 return;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300204
205 for (i = 0; i < npages; i++) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100206 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
Weidong Han260782b2008-12-02 21:03:39 +0800207 pfn = phys >> PAGE_SHIFT;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300208 kvm_release_pfn_clean(pfn);
209 gfn++;
210 }
Weidong Han260782b2008-12-02 21:03:39 +0800211
Joerg Roedel19de40a2008-12-03 14:43:34 +0100212 iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300213}
214
215static int kvm_iommu_unmap_memslots(struct kvm *kvm)
216{
217 int i;
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200218 struct kvm_memslots *slots;
Mark McLoughlin682edb42009-02-05 18:23:46 +0000219
Lai Jiangshan90d83dc2010-04-19 17:41:23 +0800220 slots = kvm_memslots(kvm);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -0200221
222 for (i = 0; i < slots->nmemslots; i++) {
223 kvm_iommu_put_pages(kvm, slots->memslots[i].base_gfn,
224 slots->memslots[i].npages);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300225 }
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300226
227 return 0;
228}
229
230int kvm_iommu_unmap_guest(struct kvm *kvm)
231{
Joerg Roedel19de40a2008-12-03 14:43:34 +0100232 struct iommu_domain *domain = kvm->arch.iommu_domain;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300233
234 /* check if iommu exists and in use */
235 if (!domain)
236 return 0;
237
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300238 kvm_iommu_unmap_memslots(kvm);
Joerg Roedel19de40a2008-12-03 14:43:34 +0100239 iommu_domain_free(domain);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300240 return 0;
241}