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>
36 #include <sys/eventhandler.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.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 | CTLFLAG_MPSAFE, 0,
52 "bhyve iommu parameters");
54 static int iommu_avail;
55 SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, initialized, CTLFLAG_RD, &iommu_avail,
56 0, "bhyve iommu initialized?");
58 static int iommu_enable = 1;
59 SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, enable, CTLFLAG_RDTUN, &iommu_enable, 0,
60 "Enable use of I/O MMU (required for PCI passthrough).");
62 static struct iommu_ops *ops;
63 static void *host_domain;
64 static eventhandler_tag add_tag, delete_tag;
70 return ((*ops->init)());
78 if (ops != NULL && iommu_avail)
82 static __inline void *
83 IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)
86 if (ops != NULL && iommu_avail)
87 return ((*ops->create_domain)(maxaddr));
93 IOMMU_DESTROY_DOMAIN(void *dom)
96 if (ops != NULL && iommu_avail)
97 (*ops->destroy_domain)(dom);
100 static __inline uint64_t
101 IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len)
104 if (ops != NULL && iommu_avail)
105 return ((*ops->create_mapping)(domain, gpa, hpa, len));
107 return (len); /* XXX */
110 static __inline uint64_t
111 IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len)
114 if (ops != NULL && iommu_avail)
115 return ((*ops->remove_mapping)(domain, gpa, len));
117 return (len); /* XXX */
121 IOMMU_ADD_DEVICE(void *domain, uint16_t rid)
124 if (ops != NULL && iommu_avail)
125 (*ops->add_device)(domain, rid);
129 IOMMU_REMOVE_DEVICE(void *domain, uint16_t rid)
132 if (ops != NULL && iommu_avail)
133 (*ops->remove_device)(domain, rid);
137 IOMMU_INVALIDATE_TLB(void *domain)
140 if (ops != NULL && iommu_avail)
141 (*ops->invalidate_tlb)(domain);
148 if (ops != NULL && iommu_avail)
156 if (ops != NULL && iommu_avail)
161 iommu_pci_add(void *arg, device_t dev)
164 /* Add new devices to the host domain. */
165 iommu_add_device(host_domain, pci_get_rid(dev));
169 iommu_pci_delete(void *arg, device_t dev)
172 iommu_remove_device(host_domain, pci_get_rid(dev));
178 int error, bus, slot, func;
187 ops = &iommu_ops_intel;
188 else if (vmm_is_svm())
189 ops = &iommu_ops_amd;
193 error = IOMMU_INIT();
200 * Create a domain for the devices owned by the host
202 maxaddr = vmm_mem_maxaddr();
203 host_domain = IOMMU_CREATE_DOMAIN(maxaddr);
204 if (host_domain == NULL) {
205 printf("iommu_init: unable to create a host domain");
213 * Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to
216 iommu_create_mapping(host_domain, 0, 0, maxaddr);
218 add_tag = EVENTHANDLER_REGISTER(pci_add_device, iommu_pci_add, NULL, 0);
219 delete_tag = EVENTHANDLER_REGISTER(pci_delete_device, iommu_pci_delete,
221 dc = devclass_find("ppt");
222 for (bus = 0; bus <= PCI_BUSMAX; bus++) {
223 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
224 for (func = 0; func <= PCI_FUNCMAX; func++) {
225 dev = pci_find_dbsf(0, bus, slot, func);
229 /* Skip passthrough devices. */
231 device_get_devclass(dev) == dc)
235 * Everything else belongs to the host
238 iommu_add_device(host_domain,
251 if (add_tag != NULL) {
252 EVENTHANDLER_DEREGISTER(pci_add_device, add_tag);
255 if (delete_tag != NULL) {
256 EVENTHANDLER_DEREGISTER(pci_delete_device, delete_tag);
260 IOMMU_DESTROY_DOMAIN(host_domain);
265 iommu_create_domain(vm_paddr_t maxaddr)
267 static volatile int iommu_initted;
269 if (iommu_initted < 2) {
270 if (atomic_cmpset_int(&iommu_initted, 0, 1)) {
272 atomic_store_rel_int(&iommu_initted, 2);
274 while (iommu_initted == 1)
277 return (IOMMU_CREATE_DOMAIN(maxaddr));
281 iommu_destroy_domain(void *dom)
284 IOMMU_DESTROY_DOMAIN(dom);
288 iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len)
290 uint64_t mapped, remaining;
294 while (remaining > 0) {
295 mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining);
303 iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len)
305 uint64_t unmapped, remaining;
309 while (remaining > 0) {
310 unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining);
312 remaining -= unmapped;
317 iommu_host_domain(void)
320 return (host_domain);
324 iommu_add_device(void *dom, uint16_t rid)
327 IOMMU_ADD_DEVICE(dom, rid);
331 iommu_remove_device(void *dom, uint16_t rid)
334 IOMMU_REMOVE_DEVICE(dom, rid);
338 iommu_invalidate_tlb(void *domain)
341 IOMMU_INVALIDATE_TLB(domain);