1366f6083SPeter Grehan /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3c49761ddSPedro F. Giffuni *
4366f6083SPeter Grehan * Copyright (c) 2011 NetApp, Inc.
5366f6083SPeter Grehan * All rights reserved.
6366f6083SPeter Grehan *
7366f6083SPeter Grehan * Redistribution and use in source and binary forms, with or without
8366f6083SPeter Grehan * modification, are permitted provided that the following conditions
9366f6083SPeter Grehan * are met:
10366f6083SPeter Grehan * 1. Redistributions of source code must retain the above copyright
11366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer.
12366f6083SPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright
13366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer in the
14366f6083SPeter Grehan * documentation and/or other materials provided with the distribution.
15366f6083SPeter Grehan *
16366f6083SPeter Grehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17366f6083SPeter Grehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18366f6083SPeter Grehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19366f6083SPeter Grehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20366f6083SPeter Grehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21366f6083SPeter Grehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22366f6083SPeter Grehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23366f6083SPeter Grehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24366f6083SPeter Grehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25366f6083SPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26366f6083SPeter Grehan * SUCH DAMAGE.
27366f6083SPeter Grehan */
28366f6083SPeter Grehan
29366f6083SPeter Grehan #include <sys/param.h>
30366f6083SPeter Grehan #include <sys/bus.h>
31e2e050c8SConrad Meyer #include <sys/eventhandler.h>
3251f45d01SNeel Natu #include <sys/sysctl.h>
33e2e050c8SConrad Meyer #include <sys/systm.h>
34366f6083SPeter Grehan
35366f6083SPeter Grehan #include <dev/pci/pcivar.h>
36366f6083SPeter Grehan #include <dev/pci/pcireg.h>
37366f6083SPeter Grehan
38ffe1b10dSJohn Baldwin #include <machine/cpu.h>
39366f6083SPeter Grehan #include <machine/md_var.h>
40366f6083SPeter Grehan
41366f6083SPeter Grehan #include "vmm_util.h"
427ce04d0aSNeel Natu #include "vmm_mem.h"
43366f6083SPeter Grehan #include "iommu.h"
44366f6083SPeter Grehan
4551f45d01SNeel Natu SYSCTL_DECL(_hw_vmm);
46b40598c5SPawel Biernacki SYSCTL_NODE(_hw_vmm, OID_AUTO, iommu, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
47b40598c5SPawel Biernacki "bhyve iommu parameters");
4851f45d01SNeel Natu
4951f45d01SNeel Natu static int iommu_avail;
5051f45d01SNeel Natu SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, initialized, CTLFLAG_RD, &iommu_avail,
5151f45d01SNeel Natu 0, "bhyve iommu initialized?");
5251f45d01SNeel Natu
53ffe1b10dSJohn Baldwin static int iommu_enable = 1;
54ffe1b10dSJohn Baldwin SYSCTL_INT(_hw_vmm_iommu, OID_AUTO, enable, CTLFLAG_RDTUN, &iommu_enable, 0,
55ffe1b10dSJohn Baldwin "Enable use of I/O MMU (required for PCI passthrough).");
56ffe1b10dSJohn Baldwin
5741335c6bSMark Johnston static const struct iommu_ops *ops;
58366f6083SPeter Grehan static void *host_domain;
5964414cc0SJohn Baldwin static eventhandler_tag add_tag, delete_tag;
60366f6083SPeter Grehan
61366f6083SPeter Grehan static __inline int
IOMMU_INIT(void)62366f6083SPeter Grehan IOMMU_INIT(void)
63366f6083SPeter Grehan {
64366f6083SPeter Grehan if (ops != NULL)
65366f6083SPeter Grehan return ((*ops->init)());
66366f6083SPeter Grehan else
67366f6083SPeter Grehan return (ENXIO);
68366f6083SPeter Grehan }
69366f6083SPeter Grehan
70366f6083SPeter Grehan static __inline void
IOMMU_CLEANUP(void)71366f6083SPeter Grehan IOMMU_CLEANUP(void)
72366f6083SPeter Grehan {
73366f6083SPeter Grehan if (ops != NULL && iommu_avail)
74366f6083SPeter Grehan (*ops->cleanup)();
75366f6083SPeter Grehan }
76366f6083SPeter Grehan
77366f6083SPeter Grehan static __inline void *
IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)78366f6083SPeter Grehan IOMMU_CREATE_DOMAIN(vm_paddr_t maxaddr)
79366f6083SPeter Grehan {
80366f6083SPeter Grehan
81366f6083SPeter Grehan if (ops != NULL && iommu_avail)
82366f6083SPeter Grehan return ((*ops->create_domain)(maxaddr));
83366f6083SPeter Grehan else
84366f6083SPeter Grehan return (NULL);
85366f6083SPeter Grehan }
86366f6083SPeter Grehan
87366f6083SPeter Grehan static __inline void
IOMMU_DESTROY_DOMAIN(void * dom)88366f6083SPeter Grehan IOMMU_DESTROY_DOMAIN(void *dom)
89366f6083SPeter Grehan {
90366f6083SPeter Grehan
91366f6083SPeter Grehan if (ops != NULL && iommu_avail)
92366f6083SPeter Grehan (*ops->destroy_domain)(dom);
93366f6083SPeter Grehan }
94366f6083SPeter Grehan
95366f6083SPeter Grehan static __inline uint64_t
IOMMU_CREATE_MAPPING(void * domain,vm_paddr_t gpa,vm_paddr_t hpa,uint64_t len)96366f6083SPeter Grehan IOMMU_CREATE_MAPPING(void *domain, vm_paddr_t gpa, vm_paddr_t hpa, uint64_t len)
97366f6083SPeter Grehan {
98366f6083SPeter Grehan
99366f6083SPeter Grehan if (ops != NULL && iommu_avail)
100366f6083SPeter Grehan return ((*ops->create_mapping)(domain, gpa, hpa, len));
101366f6083SPeter Grehan else
102366f6083SPeter Grehan return (len); /* XXX */
103366f6083SPeter Grehan }
104366f6083SPeter Grehan
1057ce04d0aSNeel Natu static __inline uint64_t
IOMMU_REMOVE_MAPPING(void * domain,vm_paddr_t gpa,uint64_t len)1067ce04d0aSNeel Natu IOMMU_REMOVE_MAPPING(void *domain, vm_paddr_t gpa, uint64_t len)
1077ce04d0aSNeel Natu {
1087ce04d0aSNeel Natu
1097ce04d0aSNeel Natu if (ops != NULL && iommu_avail)
1107ce04d0aSNeel Natu return ((*ops->remove_mapping)(domain, gpa, len));
1117ce04d0aSNeel Natu else
1127ce04d0aSNeel Natu return (len); /* XXX */
1137ce04d0aSNeel Natu }
1147ce04d0aSNeel Natu
115366f6083SPeter Grehan static __inline void
IOMMU_ADD_DEVICE(void * domain,uint16_t rid)116a8667250SRyan Stone IOMMU_ADD_DEVICE(void *domain, uint16_t rid)
117366f6083SPeter Grehan {
118366f6083SPeter Grehan
119366f6083SPeter Grehan if (ops != NULL && iommu_avail)
120a8667250SRyan Stone (*ops->add_device)(domain, rid);
121366f6083SPeter Grehan }
122366f6083SPeter Grehan
123366f6083SPeter Grehan static __inline void
IOMMU_REMOVE_DEVICE(void * domain,uint16_t rid)124a8667250SRyan Stone IOMMU_REMOVE_DEVICE(void *domain, uint16_t rid)
125366f6083SPeter Grehan {
126366f6083SPeter Grehan
127366f6083SPeter Grehan if (ops != NULL && iommu_avail)
128a8667250SRyan Stone (*ops->remove_device)(domain, rid);
129366f6083SPeter Grehan }
130366f6083SPeter Grehan
131366f6083SPeter Grehan static __inline void
IOMMU_INVALIDATE_TLB(void * domain)1327ce04d0aSNeel Natu IOMMU_INVALIDATE_TLB(void *domain)
1337ce04d0aSNeel Natu {
1347ce04d0aSNeel Natu
1357ce04d0aSNeel Natu if (ops != NULL && iommu_avail)
1367ce04d0aSNeel Natu (*ops->invalidate_tlb)(domain);
1377ce04d0aSNeel Natu }
1387ce04d0aSNeel Natu
1397ce04d0aSNeel Natu static __inline void
IOMMU_ENABLE(void)140366f6083SPeter Grehan IOMMU_ENABLE(void)
141366f6083SPeter Grehan {
142366f6083SPeter Grehan
143366f6083SPeter Grehan if (ops != NULL && iommu_avail)
144366f6083SPeter Grehan (*ops->enable)();
145366f6083SPeter Grehan }
146366f6083SPeter Grehan
147366f6083SPeter Grehan static __inline void
IOMMU_DISABLE(void)148366f6083SPeter Grehan IOMMU_DISABLE(void)
149366f6083SPeter Grehan {
150366f6083SPeter Grehan
151366f6083SPeter Grehan if (ops != NULL && iommu_avail)
152366f6083SPeter Grehan (*ops->disable)();
153366f6083SPeter Grehan }
154366f6083SPeter Grehan
155ffe1b10dSJohn Baldwin static void
iommu_pci_add(void * arg,device_t dev)15664414cc0SJohn Baldwin iommu_pci_add(void *arg, device_t dev)
15764414cc0SJohn Baldwin {
15864414cc0SJohn Baldwin
15964414cc0SJohn Baldwin /* Add new devices to the host domain. */
16064414cc0SJohn Baldwin iommu_add_device(host_domain, pci_get_rid(dev));
16164414cc0SJohn Baldwin }
16264414cc0SJohn Baldwin
16364414cc0SJohn Baldwin static void
iommu_pci_delete(void * arg,device_t dev)16464414cc0SJohn Baldwin iommu_pci_delete(void *arg, device_t dev)
16564414cc0SJohn Baldwin {
16664414cc0SJohn Baldwin
16764414cc0SJohn Baldwin iommu_remove_device(host_domain, pci_get_rid(dev));
16864414cc0SJohn Baldwin }
16964414cc0SJohn Baldwin
17064414cc0SJohn Baldwin static void
iommu_init(void)171366f6083SPeter Grehan iommu_init(void)
172366f6083SPeter Grehan {
173366f6083SPeter Grehan int error, bus, slot, func;
174366f6083SPeter Grehan vm_paddr_t maxaddr;
1756db55a0fSJohn Baldwin devclass_t dc;
176366f6083SPeter Grehan device_t dev;
177366f6083SPeter Grehan
178ffe1b10dSJohn Baldwin if (!iommu_enable)
179ffe1b10dSJohn Baldwin return;
180ffe1b10dSJohn Baldwin
181366f6083SPeter Grehan if (vmm_is_intel())
182366f6083SPeter Grehan ops = &iommu_ops_intel;
183caab5042SKonstantin Belousov else if (vmm_is_svm())
184366f6083SPeter Grehan ops = &iommu_ops_amd;
185366f6083SPeter Grehan else
186366f6083SPeter Grehan ops = NULL;
187366f6083SPeter Grehan
188366f6083SPeter Grehan error = IOMMU_INIT();
189366f6083SPeter Grehan if (error)
190366f6083SPeter Grehan return;
191366f6083SPeter Grehan
19251f45d01SNeel Natu iommu_avail = 1;
193366f6083SPeter Grehan
194366f6083SPeter Grehan /*
195366f6083SPeter Grehan * Create a domain for the devices owned by the host
196366f6083SPeter Grehan */
1977ce04d0aSNeel Natu maxaddr = vmm_mem_maxaddr();
198366f6083SPeter Grehan host_domain = IOMMU_CREATE_DOMAIN(maxaddr);
199ffe1b10dSJohn Baldwin if (host_domain == NULL) {
200ffe1b10dSJohn Baldwin printf("iommu_init: unable to create a host domain");
201ffe1b10dSJohn Baldwin IOMMU_CLEANUP();
202ffe1b10dSJohn Baldwin ops = NULL;
203ffe1b10dSJohn Baldwin iommu_avail = 0;
204ffe1b10dSJohn Baldwin return;
205ffe1b10dSJohn Baldwin }
206366f6083SPeter Grehan
207366f6083SPeter Grehan /*
2087ce04d0aSNeel Natu * Create 1:1 mappings from '0' to 'maxaddr' for devices assigned to
209366f6083SPeter Grehan * the host
210366f6083SPeter Grehan */
211366f6083SPeter Grehan iommu_create_mapping(host_domain, 0, 0, maxaddr);
212366f6083SPeter Grehan
21364414cc0SJohn Baldwin add_tag = EVENTHANDLER_REGISTER(pci_add_device, iommu_pci_add, NULL, 0);
21464414cc0SJohn Baldwin delete_tag = EVENTHANDLER_REGISTER(pci_delete_device, iommu_pci_delete,
21564414cc0SJohn Baldwin NULL, 0);
2166db55a0fSJohn Baldwin dc = devclass_find("ppt");
217366f6083SPeter Grehan for (bus = 0; bus <= PCI_BUSMAX; bus++) {
218366f6083SPeter Grehan for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
219366f6083SPeter Grehan for (func = 0; func <= PCI_FUNCMAX; func++) {
220366f6083SPeter Grehan dev = pci_find_dbsf(0, bus, slot, func);
221366f6083SPeter Grehan if (dev == NULL)
222366f6083SPeter Grehan continue;
223366f6083SPeter Grehan
2246db55a0fSJohn Baldwin /* Skip passthrough devices. */
2256db55a0fSJohn Baldwin if (dc != NULL &&
2266db55a0fSJohn Baldwin device_get_devclass(dev) == dc)
2276db55a0fSJohn Baldwin continue;
2286db55a0fSJohn Baldwin
2296db55a0fSJohn Baldwin /*
2306db55a0fSJohn Baldwin * Everything else belongs to the host
2316db55a0fSJohn Baldwin * domain.
2326db55a0fSJohn Baldwin */
233a8667250SRyan Stone iommu_add_device(host_domain,
234a8667250SRyan Stone pci_get_rid(dev));
235366f6083SPeter Grehan }
236366f6083SPeter Grehan }
237366f6083SPeter Grehan }
238366f6083SPeter Grehan IOMMU_ENABLE();
239366f6083SPeter Grehan
240366f6083SPeter Grehan }
241366f6083SPeter Grehan
242366f6083SPeter Grehan void
iommu_cleanup(void)243366f6083SPeter Grehan iommu_cleanup(void)
244366f6083SPeter Grehan {
24564414cc0SJohn Baldwin
24664414cc0SJohn Baldwin if (add_tag != NULL) {
24764414cc0SJohn Baldwin EVENTHANDLER_DEREGISTER(pci_add_device, add_tag);
24864414cc0SJohn Baldwin add_tag = NULL;
24964414cc0SJohn Baldwin }
25064414cc0SJohn Baldwin if (delete_tag != NULL) {
25164414cc0SJohn Baldwin EVENTHANDLER_DEREGISTER(pci_delete_device, delete_tag);
25264414cc0SJohn Baldwin delete_tag = NULL;
25364414cc0SJohn Baldwin }
254366f6083SPeter Grehan IOMMU_DISABLE();
255366f6083SPeter Grehan IOMMU_DESTROY_DOMAIN(host_domain);
25627029bc0SRobert Wing host_domain = NULL;
257366f6083SPeter Grehan IOMMU_CLEANUP();
258366f6083SPeter Grehan }
259366f6083SPeter Grehan
260366f6083SPeter Grehan void *
iommu_create_domain(vm_paddr_t maxaddr)261366f6083SPeter Grehan iommu_create_domain(vm_paddr_t maxaddr)
262366f6083SPeter Grehan {
263ffe1b10dSJohn Baldwin static volatile int iommu_initted;
264366f6083SPeter Grehan
265ffe1b10dSJohn Baldwin if (iommu_initted < 2) {
266ffe1b10dSJohn Baldwin if (atomic_cmpset_int(&iommu_initted, 0, 1)) {
267ffe1b10dSJohn Baldwin iommu_init();
268ffe1b10dSJohn Baldwin atomic_store_rel_int(&iommu_initted, 2);
269ffe1b10dSJohn Baldwin } else
270ffe1b10dSJohn Baldwin while (iommu_initted == 1)
271ffe1b10dSJohn Baldwin cpu_spinwait();
272ffe1b10dSJohn Baldwin }
273366f6083SPeter Grehan return (IOMMU_CREATE_DOMAIN(maxaddr));
274366f6083SPeter Grehan }
275366f6083SPeter Grehan
276366f6083SPeter Grehan void
iommu_destroy_domain(void * dom)277366f6083SPeter Grehan iommu_destroy_domain(void *dom)
278366f6083SPeter Grehan {
279366f6083SPeter Grehan
280366f6083SPeter Grehan IOMMU_DESTROY_DOMAIN(dom);
281366f6083SPeter Grehan }
282366f6083SPeter Grehan
283366f6083SPeter Grehan void
iommu_create_mapping(void * dom,vm_paddr_t gpa,vm_paddr_t hpa,size_t len)284366f6083SPeter Grehan iommu_create_mapping(void *dom, vm_paddr_t gpa, vm_paddr_t hpa, size_t len)
285366f6083SPeter Grehan {
286366f6083SPeter Grehan uint64_t mapped, remaining;
287366f6083SPeter Grehan
288366f6083SPeter Grehan remaining = len;
289366f6083SPeter Grehan
290366f6083SPeter Grehan while (remaining > 0) {
291366f6083SPeter Grehan mapped = IOMMU_CREATE_MAPPING(dom, gpa, hpa, remaining);
292366f6083SPeter Grehan gpa += mapped;
293366f6083SPeter Grehan hpa += mapped;
294366f6083SPeter Grehan remaining -= mapped;
295366f6083SPeter Grehan }
296366f6083SPeter Grehan }
297366f6083SPeter Grehan
298366f6083SPeter Grehan void
iommu_remove_mapping(void * dom,vm_paddr_t gpa,size_t len)2997ce04d0aSNeel Natu iommu_remove_mapping(void *dom, vm_paddr_t gpa, size_t len)
3007ce04d0aSNeel Natu {
3017ce04d0aSNeel Natu uint64_t unmapped, remaining;
3027ce04d0aSNeel Natu
3037ce04d0aSNeel Natu remaining = len;
3047ce04d0aSNeel Natu
3057ce04d0aSNeel Natu while (remaining > 0) {
3067ce04d0aSNeel Natu unmapped = IOMMU_REMOVE_MAPPING(dom, gpa, remaining);
3077ce04d0aSNeel Natu gpa += unmapped;
3087ce04d0aSNeel Natu remaining -= unmapped;
3097ce04d0aSNeel Natu }
3107ce04d0aSNeel Natu }
3117ce04d0aSNeel Natu
3127ce04d0aSNeel Natu void *
iommu_host_domain(void)3137ce04d0aSNeel Natu iommu_host_domain(void)
3147ce04d0aSNeel Natu {
3157ce04d0aSNeel Natu
3167ce04d0aSNeel Natu return (host_domain);
3177ce04d0aSNeel Natu }
3187ce04d0aSNeel Natu
3197ce04d0aSNeel Natu void
iommu_add_device(void * dom,uint16_t rid)320a8667250SRyan Stone iommu_add_device(void *dom, uint16_t rid)
321366f6083SPeter Grehan {
322366f6083SPeter Grehan
323a8667250SRyan Stone IOMMU_ADD_DEVICE(dom, rid);
324366f6083SPeter Grehan }
325366f6083SPeter Grehan
326366f6083SPeter Grehan void
iommu_remove_device(void * dom,uint16_t rid)327a8667250SRyan Stone iommu_remove_device(void *dom, uint16_t rid)
328366f6083SPeter Grehan {
329366f6083SPeter Grehan
330a8667250SRyan Stone IOMMU_REMOVE_DEVICE(dom, rid);
331366f6083SPeter Grehan }
3327ce04d0aSNeel Natu
3337ce04d0aSNeel Natu void
iommu_invalidate_tlb(void * domain)3347ce04d0aSNeel Natu iommu_invalidate_tlb(void *domain)
3357ce04d0aSNeel Natu {
3367ce04d0aSNeel Natu
3377ce04d0aSNeel Natu IOMMU_INVALIDATE_TLB(domain);
3387ce04d0aSNeel Natu }
339