2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
38 #include <sys/sysctl.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
43 #include <machine/cpu.h>
44 #include <machine/md_var.h>
51 SYSCTL_NODE(_hw_vmm, OID_AUTO, iommu, CTLFLAG_RW, 0, "bhyve iommu parameters");
53 static int iommu_avail;
54 SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, initialized, CTLFLAG_RD, &iommu_avail,
55 0, "bhyve iommu initialized?");
57 static int iommu_enable = 1;
58 SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, enable, CTLFLAG_RDTUN, &iommu_enable, 0,
59 "Enable use of I/O MMU (required for PCI passthrough).");
61 static struct iommu_ops *ops;
62 static void *host_domain;
63 static eventhandler_tag add_tag, delete_tag;
69 return ((*ops->init)());
77 if (ops != NULL && iommu_avail)
81 static __inline void *
82 IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)
85 if (ops != NULL && iommu_avail)
86 return ((*ops->create_domain)(maxaddr));
92 IOMMU_DESTROY_DOMAIN(void *dom)
95 if (ops != NULL && iommu_avail)
96 (*ops->destroy_domain)(dom);
99 static __inline uint64_t
100 IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len)
103 if (ops != NULL && iommu_avail)
104 return ((*ops->create_mapping)(domain, gpa, hpa, len));
106 return (len); /* XXX */
109 static __inline uint64_t
110 IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len)
113 if (ops != NULL && iommu_avail)
114 return ((*ops->remove_mapping)(domain, gpa, len));
116 return (len); /* XXX */
120 IOMMU_ADD_DEVICE(void *domain, uint16_t rid)
123 if (ops != NULL && iommu_avail)
124 (*ops->add_device)(domain, rid);
128 IOMMU_REMOVE_DEVICE(void *domain, uint16_t rid)
131 if (ops != NULL && iommu_avail)
132 (*ops->remove_device)(domain, rid);
136 IOMMU_INVALIDATE_TLB(void *domain)
139 if (ops != NULL && iommu_avail)
140 (*ops->invalidate_tlb)(domain);
147 if (ops != NULL && iommu_avail)
155 if (ops != NULL && iommu_avail)
160 iommu_pci_add(void *arg, device_t dev)
163 /* Add new devices to the host domain. */
164 iommu_add_device(host_domain, pci_get_rid(dev));
168 iommu_pci_delete(void *arg, device_t dev)
171 iommu_remove_device(host_domain, pci_get_rid(dev));
177 int error, bus, slot, func;
186 ops = &iommu_ops_intel;
187 else if (vmm_is_amd())
188 ops = &iommu_ops_amd;
192 error = IOMMU_INIT();
199 * Create a domain for the devices owned by the host
201 maxaddr = vmm_mem_maxaddr();
202 host_domain = IOMMU_CREATE_DOMAIN(maxaddr);
203 if (host_domain == NULL) {
204 printf("iommu_init: unable to create a host domain");
212 * Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to
215 iommu_create_mapping(host_domain, 0, 0, maxaddr);
217 add_tag = EVENTHANDLER_REGISTER(pci_add_device, iommu_pci_add, NULL, 0);
218 delete_tag = EVENTHANDLER_REGISTER(pci_delete_device, iommu_pci_delete,
220 dc = devclass_find("ppt");
221 for (bus = 0; bus <= PCI_BUSMAX; bus++) {
222 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
223 for (func = 0; func <= PCI_FUNCMAX; func++) {
224 dev = pci_find_dbsf(0, bus, slot, func);
228 /* Skip passthrough devices. */
230 device_get_devclass(dev) == dc)
234 * Everything else belongs to the host
237 iommu_add_device(host_domain,
250 if (add_tag != NULL) {
251 EVENTHANDLER_DEREGISTER(pci_add_device, add_tag);
254 if (delete_tag != NULL) {
255 EVENTHANDLER_DEREGISTER(pci_delete_device, delete_tag);
259 IOMMU_DESTROY_DOMAIN(host_domain);
264 iommu_create_domain(vm_paddr_t maxaddr)
266 static volatile int iommu_initted;
268 if (iommu_initted < 2) {
269 if (atomic_cmpset_int(&iommu_initted, 0, 1)) {
271 atomic_store_rel_int(&iommu_initted, 2);
273 while (iommu_initted == 1)
276 return (IOMMU_CREATE_DOMAIN(maxaddr));
280 iommu_destroy_domain(void *dom)
283 IOMMU_DESTROY_DOMAIN(dom);
287 iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len)
289 uint64_t mapped, remaining;
293 while (remaining > 0) {
294 mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining);
302 iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len)
304 uint64_t unmapped, remaining;
308 while (remaining > 0) {
309 unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining);
311 remaining -= unmapped;
316 iommu_host_domain(void)
319 return (host_domain);
323 iommu_add_device(void *dom, uint16_t rid)
326 IOMMU_ADD_DEVICE(dom, rid);
330 iommu_remove_device(void *dom, uint16_t rid)
333 IOMMU_REMOVE_DEVICE(dom, rid);
337 iommu_invalidate_tlb(void *domain)
340 IOMMU_INVALIDATE_TLB(domain);