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>
37 #include <dev/pci/pcivar.h>
38 #include <dev/pci/pcireg.h>
40 #include <machine/md_var.h>
46 static boolean_t iommu_avail;
47 static struct iommu_ops *ops;
48 static void *host_domain;
54 return ((*ops->init)());
62 if (ops != NULL && iommu_avail)
66 static __inline void *
67 IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)
70 if (ops != NULL && iommu_avail)
71 return ((*ops->create_domain)(maxaddr));
77 IOMMU_DESTROY_DOMAIN(void *dom)
80 if (ops != NULL && iommu_avail)
81 (*ops->destroy_domain)(dom);
84 static __inline uint64_t
85 IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len)
88 if (ops != NULL && iommu_avail)
89 return ((*ops->create_mapping)(domain, gpa, hpa, len));
91 return (len); /* XXX */
94 static __inline uint64_t
95 IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len)
98 if (ops != NULL && iommu_avail)
99 return ((*ops->remove_mapping)(domain, gpa, len));
101 return (len); /* XXX */
105 IOMMU_ADD_DEVICE(void *domain, int bus, int slot, int func)
108 if (ops != NULL && iommu_avail)
109 (*ops->add_device)(domain, bus, slot, func);
113 IOMMU_REMOVE_DEVICE(void *domain, int bus, int slot, int func)
116 if (ops != NULL && iommu_avail)
117 (*ops->remove_device)(domain, bus, slot, func);
121 IOMMU_INVALIDATE_TLB(void *domain)
124 if (ops != NULL && iommu_avail)
125 (*ops->invalidate_tlb)(domain);
132 if (ops != NULL && iommu_avail)
140 if (ops != NULL && iommu_avail)
147 int error, bus, slot, func;
153 ops = &iommu_ops_intel;
154 else if (vmm_is_amd())
155 ops = &iommu_ops_amd;
159 error = IOMMU_INIT();
166 * Create a domain for the devices owned by the host
168 maxaddr = vmm_mem_maxaddr();
169 host_domain = IOMMU_CREATE_DOMAIN(maxaddr);
170 if (host_domain == NULL)
171 panic("iommu_init: unable to create a host domain");
174 * Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to
177 iommu_create_mapping(host_domain, 0, 0, maxaddr);
179 for (bus = 0; bus <= PCI_BUSMAX; bus++) {
180 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
181 for (func = 0; func <= PCI_FUNCMAX; func++) {
182 dev = pci_find_dbsf(0, bus, slot, func);
186 /* skip passthrough devices */
187 name = device_get_name(dev);
188 if (name != NULL && strcmp(name, "ppt") == 0)
191 /* everything else belongs to the host domain */
192 iommu_add_device(host_domain, bus, slot, func);
204 IOMMU_DESTROY_DOMAIN(host_domain);
209 iommu_create_domain(vm_paddr_t maxaddr)
212 return (IOMMU_CREATE_DOMAIN(maxaddr));
216 iommu_destroy_domain(void *dom)
219 IOMMU_DESTROY_DOMAIN(dom);
223 iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len)
225 uint64_t mapped, remaining;
229 while (remaining > 0) {
230 mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining);
238 iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len)
240 uint64_t unmapped, remaining;
244 while (remaining > 0) {
245 unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining);
247 remaining -= unmapped;
252 iommu_host_domain(void)
255 return (host_domain);
259 iommu_add_device(void *dom, int bus, int slot, int func)
262 IOMMU_ADD_DEVICE(dom, bus, slot, func);
266 iommu_remove_device(void *dom, int bus, int slot, int func)
269 IOMMU_REMOVE_DEVICE(dom, bus, slot, func);
273 iommu_invalidate_tlb(void *domain)
276 IOMMU_INVALIDATE_TLB(domain);