xref: /freebsd/sys/amd64/vmm/io/iommu.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/eventhandler.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
42 
43 #include <machine/cpu.h>
44 #include <machine/md_var.h>
45 
46 #include "vmm_util.h"
47 #include "vmm_mem.h"
48 #include "iommu.h"
49 
50 SYSCTL_DECL(_hw_vmm);
51 SYSCTL_NODE(_hw_vmm, OID_AUTO, iommu, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
52     "bhyve iommu parameters");
53 
54 static int iommu_avail;
55 SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, initialized, CTLFLAG_RD, &iommu_avail,
56     0, "bhyve iommu initialized?");
57 
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).");
61 
62 static struct iommu_ops *ops;
63 static void *host_domain;
64 static eventhandler_tag add_tag, delete_tag;
65 
66 static __inline int
67 IOMMU_INIT(void)
68 {
69 	if (ops != NULL)
70 		return ((*ops->init)());
71 	else
72 		return (ENXIO);
73 }
74 
75 static __inline void
76 IOMMU_CLEANUP(void)
77 {
78 	if (ops != NULL && iommu_avail)
79 		(*ops->cleanup)();
80 }
81 
82 static __inline void *
83 IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)
84 {
85 
86 	if (ops != NULL && iommu_avail)
87 		return ((*ops->create_domain)(maxaddr));
88 	else
89 		return (NULL);
90 }
91 
92 static __inline void
93 IOMMU_DESTROY_DOMAIN(void *dom)
94 {
95 
96 	if (ops != NULL && iommu_avail)
97 		(*ops->destroy_domain)(dom);
98 }
99 
100 static __inline uint64_t
101 IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len)
102 {
103 
104 	if (ops != NULL && iommu_avail)
105 		return ((*ops->create_mapping)(domain, gpa, hpa, len));
106 	else
107 		return (len);		/* XXX */
108 }
109 
110 static __inline uint64_t
111 IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len)
112 {
113 
114 	if (ops != NULL && iommu_avail)
115 		return ((*ops->remove_mapping)(domain, gpa, len));
116 	else
117 		return (len);		/* XXX */
118 }
119 
120 static __inline void
121 IOMMU_ADD_DEVICE(void *domain, uint16_t rid)
122 {
123 
124 	if (ops != NULL && iommu_avail)
125 		(*ops->add_device)(domain, rid);
126 }
127 
128 static __inline void
129 IOMMU_REMOVE_DEVICE(void *domain, uint16_t rid)
130 {
131 
132 	if (ops != NULL && iommu_avail)
133 		(*ops->remove_device)(domain, rid);
134 }
135 
136 static __inline void
137 IOMMU_INVALIDATE_TLB(void *domain)
138 {
139 
140 	if (ops != NULL && iommu_avail)
141 		(*ops->invalidate_tlb)(domain);
142 }
143 
144 static __inline void
145 IOMMU_ENABLE(void)
146 {
147 
148 	if (ops != NULL && iommu_avail)
149 		(*ops->enable)();
150 }
151 
152 static __inline void
153 IOMMU_DISABLE(void)
154 {
155 
156 	if (ops != NULL && iommu_avail)
157 		(*ops->disable)();
158 }
159 
160 static void
161 iommu_pci_add(void *arg, device_t dev)
162 {
163 
164 	/* Add new devices to the host domain. */
165 	iommu_add_device(host_domain, pci_get_rid(dev));
166 }
167 
168 static void
169 iommu_pci_delete(void *arg, device_t dev)
170 {
171 
172 	iommu_remove_device(host_domain, pci_get_rid(dev));
173 }
174 
175 static void
176 iommu_init(void)
177 {
178 	int error, bus, slot, func;
179 	vm_paddr_t maxaddr;
180 	devclass_t dc;
181 	device_t dev;
182 
183 	if (!iommu_enable)
184 		return;
185 
186 	if (vmm_is_intel())
187 		ops = &iommu_ops_intel;
188 	else if (vmm_is_svm())
189 		ops = &iommu_ops_amd;
190 	else
191 		ops = NULL;
192 
193 	error = IOMMU_INIT();
194 	if (error)
195 		return;
196 
197 	iommu_avail = 1;
198 
199 	/*
200 	 * Create a domain for the devices owned by the host
201 	 */
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");
206 		IOMMU_CLEANUP();
207 		ops = NULL;
208 		iommu_avail = 0;
209 		return;
210 	}
211 
212 	/*
213 	 * Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to
214 	 * the host
215 	 */
216 	iommu_create_mapping(host_domain, 0, 0, maxaddr);
217 
218 	add_tag = EVENTHANDLER_REGISTER(pci_add_device, iommu_pci_add, NULL, 0);
219 	delete_tag = EVENTHANDLER_REGISTER(pci_delete_device, iommu_pci_delete,
220 	    NULL, 0);
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);
226 				if (dev == NULL)
227 					continue;
228 
229 				/* Skip passthrough devices. */
230 				if (dc != NULL &&
231 				    device_get_devclass(dev) == dc)
232 					continue;
233 
234 				/*
235 				 * Everything else belongs to the host
236 				 * domain.
237 				 */
238 				iommu_add_device(host_domain,
239 				    pci_get_rid(dev));
240 			}
241 		}
242 	}
243 	IOMMU_ENABLE();
244 
245 }
246 
247 void
248 iommu_cleanup(void)
249 {
250 
251 	if (add_tag != NULL) {
252 		EVENTHANDLER_DEREGISTER(pci_add_device, add_tag);
253 		add_tag = NULL;
254 	}
255 	if (delete_tag != NULL) {
256 		EVENTHANDLER_DEREGISTER(pci_delete_device, delete_tag);
257 		delete_tag = NULL;
258 	}
259 	IOMMU_DISABLE();
260 	IOMMU_DESTROY_DOMAIN(host_domain);
261 	IOMMU_CLEANUP();
262 }
263 
264 void *
265 iommu_create_domain(vm_paddr_t maxaddr)
266 {
267 	static volatile int iommu_initted;
268 
269 	if (iommu_initted < 2) {
270 		if (atomic_cmpset_int(&iommu_initted, 0, 1)) {
271 			iommu_init();
272 			atomic_store_rel_int(&iommu_initted, 2);
273 		} else
274 			while (iommu_initted == 1)
275 				cpu_spinwait();
276 	}
277 	return (IOMMU_CREATE_DOMAIN(maxaddr));
278 }
279 
280 void
281 iommu_destroy_domain(void *dom)
282 {
283 
284 	IOMMU_DESTROY_DOMAIN(dom);
285 }
286 
287 void
288 iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len)
289 {
290 	uint64_t mapped, remaining;
291 
292 	remaining = len;
293 
294 	while (remaining > 0) {
295 		mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining);
296 		gpa += mapped;
297 		hpa += mapped;
298 		remaining -= mapped;
299 	}
300 }
301 
302 void
303 iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len)
304 {
305 	uint64_t unmapped, remaining;
306 
307 	remaining = len;
308 
309 	while (remaining > 0) {
310 		unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining);
311 		gpa += unmapped;
312 		remaining -= unmapped;
313 	}
314 }
315 
316 void *
317 iommu_host_domain(void)
318 {
319 
320 	return (host_domain);
321 }
322 
323 void
324 iommu_add_device(void *dom, uint16_t rid)
325 {
326 
327 	IOMMU_ADD_DEVICE(dom, rid);
328 }
329 
330 void
331 iommu_remove_device(void *dom, uint16_t rid)
332 {
333 
334 	IOMMU_REMOVE_DEVICE(dom, rid);
335 }
336 
337 void
338 iommu_invalidate_tlb(void *domain)
339 {
340 
341 	IOMMU_INVALIDATE_TLB(domain);
342 }
343