2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
36 #include <sys/sysctl.h>
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
41 #include <machine/md_var.h>
48 SYSCTL_NODE(_hw_vmm, OID_AUTO, iommu, CTLFLAG_RW, 0, "bhyve iommu parameters");
50 static int iommu_avail;
51 SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, initialized, CTLFLAG_RD, &iommu_avail,
52 0, "bhyve iommu initialized?");
54 static struct iommu_ops *ops;
55 static void *host_domain;
61 return ((*ops->init)());
69 if (ops != NULL && iommu_avail)
73 static __inline void *
74 IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)
77 if (ops != NULL && iommu_avail)
78 return ((*ops->create_domain)(maxaddr));
84 IOMMU_DESTROY_DOMAIN(void *dom)
87 if (ops != NULL && iommu_avail)
88 (*ops->destroy_domain)(dom);
91 static __inline uint64_t
92 IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len)
95 if (ops != NULL && iommu_avail)
96 return ((*ops->create_mapping)(domain, gpa, hpa, len));
98 return (len); /* XXX */
101 static __inline uint64_t
102 IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len)
105 if (ops != NULL && iommu_avail)
106 return ((*ops->remove_mapping)(domain, gpa, len));
108 return (len); /* XXX */
112 IOMMU_ADD_DEVICE(void *domain, uint16_t rid)
115 if (ops != NULL && iommu_avail)
116 (*ops->add_device)(domain, rid);
120 IOMMU_REMOVE_DEVICE(void *domain, uint16_t rid)
123 if (ops != NULL && iommu_avail)
124 (*ops->remove_device)(domain, rid);
128 IOMMU_INVALIDATE_TLB(void *domain)
131 if (ops != NULL && iommu_avail)
132 (*ops->invalidate_tlb)(domain);
139 if (ops != NULL && iommu_avail)
147 if (ops != NULL && iommu_avail)
154 int error, bus, slot, func;
160 ops = &iommu_ops_intel;
161 else if (vmm_is_amd())
162 ops = &iommu_ops_amd;
166 error = IOMMU_INIT();
173 * Create a domain for the devices owned by the host
175 maxaddr = vmm_mem_maxaddr();
176 host_domain = IOMMU_CREATE_DOMAIN(maxaddr);
177 if (host_domain == NULL)
178 panic("iommu_init: unable to create a host domain");
181 * Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to
184 iommu_create_mapping(host_domain, 0, 0, maxaddr);
186 for (bus = 0; bus <= PCI_BUSMAX; bus++) {
187 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
188 for (func = 0; func <= PCI_FUNCMAX; func++) {
189 dev = pci_find_dbsf(0, bus, slot, func);
193 /* skip passthrough devices */
194 name = device_get_name(dev);
195 if (name != NULL && strcmp(name, "ppt") == 0)
198 /* everything else belongs to the host domain */
199 iommu_add_device(host_domain,
212 IOMMU_DESTROY_DOMAIN(host_domain);
217 iommu_create_domain(vm_paddr_t maxaddr)
220 return (IOMMU_CREATE_DOMAIN(maxaddr));
224 iommu_destroy_domain(void *dom)
227 IOMMU_DESTROY_DOMAIN(dom);
231 iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len)
233 uint64_t mapped, remaining;
237 while (remaining > 0) {
238 mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining);
246 iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len)
248 uint64_t unmapped, remaining;
252 while (remaining > 0) {
253 unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining);
255 remaining -= unmapped;
260 iommu_host_domain(void)
263 return (host_domain);
267 iommu_add_device(void *dom, uint16_t rid)
270 IOMMU_ADD_DEVICE(dom, rid);
274 iommu_remove_device(void *dom, uint16_t rid)
277 IOMMU_REMOVE_DEVICE(dom, rid);
281 iommu_invalidate_tlb(void *domain)
284 IOMMU_INVALIDATE_TLB(domain);