2 * Copyright (c) 2006, Intel Corporation.
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.
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
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.
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>
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>
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
);
35 int kvm_iommu_map_pages(struct kvm
*kvm
,
36 gfn_t base_gfn
, unsigned long npages
)
41 struct iommu_domain
*domain
= kvm
->arch
.iommu_domain
;
43 /* check if iommu exists and in use */
47 for (i
= 0; i
< npages
; i
++) {
48 /* check if already mapped */
49 if (iommu_iova_to_phys(domain
, gfn_to_gpa(gfn
)))
52 pfn
= gfn_to_pfn(kvm
, gfn
);
53 r
= iommu_map_range(domain
,
57 IOMMU_READ
| IOMMU_WRITE
);
59 printk(KERN_ERR
"kvm_iommu_map_address:"
60 "iommu failed to map pfn=%lx\n", pfn
);
68 kvm_iommu_put_pages(kvm
, base_gfn
, i
);
72 static int kvm_iommu_map_memslots(struct kvm
*kvm
)
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
);
86 int kvm_assign_device(struct kvm
*kvm
,
87 struct kvm_assigned_dev_kernel
*assigned_dev
)
89 struct pci_dev
*pdev
= NULL
;
90 struct iommu_domain
*domain
= kvm
->arch
.iommu_domain
;
93 /* check if iommu exists and in use */
97 pdev
= assigned_dev
->dev
;
101 r
= iommu_attach_device(domain
, &pdev
->dev
);
103 printk(KERN_ERR
"assign device %x:%x.%x failed",
105 PCI_SLOT(pdev
->devfn
),
106 PCI_FUNC(pdev
->devfn
));
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
));
118 int kvm_deassign_device(struct kvm
*kvm
,
119 struct kvm_assigned_dev_kernel
*assigned_dev
)
121 struct iommu_domain
*domain
= kvm
->arch
.iommu_domain
;
122 struct pci_dev
*pdev
= NULL
;
124 /* check if iommu exists and in use */
128 pdev
= assigned_dev
->dev
;
132 iommu_detach_device(domain
, &pdev
->dev
);
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
));
142 int kvm_iommu_map_guest(struct kvm
*kvm
)
146 if (!iommu_found()) {
147 printk(KERN_ERR
"%s: iommu not found\n", __func__
);
151 kvm
->arch
.iommu_domain
= iommu_domain_alloc();
152 if (!kvm
->arch
.iommu_domain
)
155 r
= kvm_iommu_map_memslots(kvm
);
162 kvm_iommu_unmap_memslots(kvm
);
166 static void kvm_iommu_put_pages(struct kvm
*kvm
,
167 gfn_t base_gfn
, unsigned long npages
)
169 gfn_t gfn
= base_gfn
;
171 struct iommu_domain
*domain
= kvm
->arch
.iommu_domain
;
175 /* check if iommu exists and in use */
179 for (i
= 0; i
< npages
; i
++) {
180 phys
= iommu_iova_to_phys(domain
, gfn_to_gpa(gfn
));
181 pfn
= phys
>> PAGE_SHIFT
;
182 kvm_release_pfn_clean(pfn
);
186 iommu_unmap_range(domain
, gfn_to_gpa(base_gfn
), PAGE_SIZE
* npages
);
189 static int kvm_iommu_unmap_memslots(struct kvm
*kvm
)
193 for (i
= 0; i
< kvm
->nmemslots
; i
++) {
194 kvm_iommu_put_pages(kvm
, kvm
->memslots
[i
].base_gfn
,
195 kvm
->memslots
[i
].npages
);
201 int kvm_iommu_unmap_guest(struct kvm
*kvm
)
203 struct iommu_domain
*domain
= kvm
->arch
.iommu_domain
;
205 /* check if iommu exists and in use */
209 kvm_iommu_unmap_memslots(kvm
);
210 iommu_domain_free(domain
);