blob: e9693a29d00e2fe5b159632e301ca4cd0d08bb4d [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
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;
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;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030042
43 /* check if iommu exists and in use */
44 if (!domain)
45 return 0;
46
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030047 for (i = 0; i < npages; i++) {
48 /* check if already mapped */
Joerg Roedel19de40a2008-12-03 14:43:34 +010049 if (iommu_iova_to_phys(domain, gfn_to_gpa(gfn)))
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030050 continue;
51
52 pfn = gfn_to_pfn(kvm, gfn);
Joerg Roedel19de40a2008-12-03 14:43:34 +010053 r = iommu_map_range(domain,
54 gfn_to_gpa(gfn),
55 pfn_to_hpa(pfn),
56 PAGE_SIZE,
57 IOMMU_READ | IOMMU_WRITE);
Weidong Hane5fcfc82008-09-25 23:32:02 +080058 if (r) {
Weidong Han260782b2008-12-02 21:03:39 +080059 printk(KERN_ERR "kvm_iommu_map_address:"
Weidong Hane5fcfc82008-09-25 23:32:02 +080060 "iommu failed to map pfn=%lx\n", pfn);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030061 goto unmap_pages;
62 }
63 gfn++;
64 }
65 return 0;
66
67unmap_pages:
68 kvm_iommu_put_pages(kvm, base_gfn, i);
69 return r;
70}
71
72static int kvm_iommu_map_memslots(struct kvm *kvm)
73{
Joerg Roedel7398ca72009-01-03 16:37:53 +010074 int i, r = 0;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030075
76 down_read(&kvm->slots_lock);
77 for (i = 0; i < kvm->nmemslots; i++) {
78 r = kvm_iommu_map_pages(kvm, kvm->memslots[i].base_gfn,
79 kvm->memslots[i].npages);
80 if (r)
81 break;
82 }
83 up_read(&kvm->slots_lock);
84 return r;
85}
86
Weidong Han260782b2008-12-02 21:03:39 +080087int kvm_assign_device(struct kvm *kvm,
88 struct kvm_assigned_dev_kernel *assigned_dev)
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +030089{
90 struct pci_dev *pdev = NULL;
Joerg Roedel19de40a2008-12-03 14:43:34 +010091 struct iommu_domain *domain = kvm->arch.iommu_domain;
Weidong Han260782b2008-12-02 21:03:39 +080092 int r;
93
94 /* check if iommu exists and in use */
95 if (!domain)
96 return 0;
97
98 pdev = assigned_dev->dev;
99 if (pdev == NULL)
100 return -ENODEV;
101
Joerg Roedel19de40a2008-12-03 14:43:34 +0100102 r = iommu_attach_device(domain, &pdev->dev);
Weidong Han260782b2008-12-02 21:03:39 +0800103 if (r) {
104 printk(KERN_ERR "assign device %x:%x.%x failed",
105 pdev->bus->number,
106 PCI_SLOT(pdev->devfn),
107 PCI_FUNC(pdev->devfn));
108 return r;
109 }
110
111 printk(KERN_DEBUG "assign device: host bdf = %x:%x:%x\n",
112 assigned_dev->host_busnr,
113 PCI_SLOT(assigned_dev->host_devfn),
114 PCI_FUNC(assigned_dev->host_devfn));
115
116 return 0;
117}
118
Weidong Han0a920352008-12-02 21:24:23 +0800119int kvm_deassign_device(struct kvm *kvm,
120 struct kvm_assigned_dev_kernel *assigned_dev)
121{
Joerg Roedel19de40a2008-12-03 14:43:34 +0100122 struct iommu_domain *domain = kvm->arch.iommu_domain;
Weidong Han0a920352008-12-02 21:24:23 +0800123 struct pci_dev *pdev = NULL;
124
125 /* check if iommu exists and in use */
126 if (!domain)
127 return 0;
128
129 pdev = assigned_dev->dev;
130 if (pdev == NULL)
131 return -ENODEV;
132
Joerg Roedel19de40a2008-12-03 14:43:34 +0100133 iommu_detach_device(domain, &pdev->dev);
Weidong Han0a920352008-12-02 21:24:23 +0800134
135 printk(KERN_DEBUG "deassign device: host bdf = %x:%x:%x\n",
136 assigned_dev->host_busnr,
137 PCI_SLOT(assigned_dev->host_devfn),
138 PCI_FUNC(assigned_dev->host_devfn));
139
140 return 0;
141}
142
Weidong Han260782b2008-12-02 21:03:39 +0800143int kvm_iommu_map_guest(struct kvm *kvm)
144{
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300145 int r;
146
Joerg Roedel19de40a2008-12-03 14:43:34 +0100147 if (!iommu_found()) {
148 printk(KERN_ERR "%s: iommu not found\n", __func__);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300149 return -ENODEV;
150 }
151
Joerg Roedel19de40a2008-12-03 14:43:34 +0100152 kvm->arch.iommu_domain = iommu_domain_alloc();
153 if (!kvm->arch.iommu_domain)
Weidong Han260782b2008-12-02 21:03:39 +0800154 return -ENOMEM;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300155
156 r = kvm_iommu_map_memslots(kvm);
157 if (r)
158 goto out_unmap;
159
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300160 return 0;
161
162out_unmap:
163 kvm_iommu_unmap_memslots(kvm);
164 return r;
165}
166
167static void kvm_iommu_put_pages(struct kvm *kvm,
Weidong Han260782b2008-12-02 21:03:39 +0800168 gfn_t base_gfn, unsigned long npages)
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300169{
170 gfn_t gfn = base_gfn;
171 pfn_t pfn;
Joerg Roedel19de40a2008-12-03 14:43:34 +0100172 struct iommu_domain *domain = kvm->arch.iommu_domain;
Weidong Han260782b2008-12-02 21:03:39 +0800173 unsigned long i;
174 u64 phys;
175
176 /* check if iommu exists and in use */
177 if (!domain)
178 return;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300179
180 for (i = 0; i < npages; i++) {
Joerg Roedel19de40a2008-12-03 14:43:34 +0100181 phys = iommu_iova_to_phys(domain, gfn_to_gpa(gfn));
Weidong Han260782b2008-12-02 21:03:39 +0800182 pfn = phys >> PAGE_SHIFT;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300183 kvm_release_pfn_clean(pfn);
184 gfn++;
185 }
Weidong Han260782b2008-12-02 21:03:39 +0800186
Joerg Roedel19de40a2008-12-03 14:43:34 +0100187 iommu_unmap_range(domain, gfn_to_gpa(base_gfn), PAGE_SIZE * npages);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300188}
189
190static int kvm_iommu_unmap_memslots(struct kvm *kvm)
191{
192 int i;
193 down_read(&kvm->slots_lock);
194 for (i = 0; i < kvm->nmemslots; i++) {
195 kvm_iommu_put_pages(kvm, kvm->memslots[i].base_gfn,
196 kvm->memslots[i].npages);
197 }
198 up_read(&kvm->slots_lock);
199
200 return 0;
201}
202
203int kvm_iommu_unmap_guest(struct kvm *kvm)
204{
Joerg Roedel19de40a2008-12-03 14:43:34 +0100205 struct iommu_domain *domain = kvm->arch.iommu_domain;
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300206
207 /* check if iommu exists and in use */
208 if (!domain)
209 return 0;
210
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300211 kvm_iommu_unmap_memslots(kvm);
Joerg Roedel19de40a2008-12-03 14:43:34 +0100212 iommu_domain_free(domain);
Ben-Ami Yassour62c476c2008-09-14 03:48:28 +0300213 return 0;
214}