xref: /openbmc/qemu/hw/remote/iommu.c (revision 954a6c4f)
1 /**
2  * IOMMU for remote device
3  *
4  * Copyright © 2022 Oracle and/or its affiliates.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  */
10 
11 #include "qemu/osdep.h"
12 
13 #include "hw/remote/iommu.h"
14 #include "hw/pci/pci_bus.h"
15 #include "hw/pci/pci.h"
16 #include "exec/memory.h"
17 #include "exec/address-spaces.h"
18 #include "trace.h"
19 
20 /**
21  * IOMMU for TYPE_REMOTE_MACHINE - manages DMA address space isolation
22  *     for remote machine. It is used by TYPE_VFIO_USER_SERVER.
23  *
24  * - Each TYPE_VFIO_USER_SERVER instance handles one PCIDevice on a PCIBus.
25  *   There is one RemoteIommu per PCIBus, so the RemoteIommu tracks multiple
26  *   PCIDevices by maintaining a ->elem_by_devfn mapping.
27  *
28  * - memory_region_init_iommu() is not used because vfio-user MemoryRegions
29  *   will be added to the elem->mr container instead. This is more natural
30  *   than implementing the IOMMUMemoryRegionClass APIs since vfio-user
31  *   provides something that is close to a full-fledged MemoryRegion and
32  *   not like an IOMMU mapping.
33  *
34  * - When a device is hot unplugged, the elem->mr reference is dropped so
35  *   all vfio-user MemoryRegions associated with this vfio-user server are
36  *   destroyed.
37  */
38 
39 static AddressSpace *remote_iommu_find_add_as(PCIBus *pci_bus,
40                                               void *opaque, int devfn)
41 {
42     RemoteIommu *iommu = opaque;
43     RemoteIommuElem *elem = NULL;
44 
45     qemu_mutex_lock(&iommu->lock);
46 
47     elem = g_hash_table_lookup(iommu->elem_by_devfn, INT2VOIDP(devfn));
48 
49     if (!elem) {
50         elem = g_new0(RemoteIommuElem, 1);
51         g_hash_table_insert(iommu->elem_by_devfn, INT2VOIDP(devfn), elem);
52     }
53 
54     if (!elem->mr) {
55         elem->mr = MEMORY_REGION(object_new(TYPE_MEMORY_REGION));
56         memory_region_set_size(elem->mr, UINT64_MAX);
57         address_space_init(&elem->as, elem->mr, NULL);
58     }
59 
60     qemu_mutex_unlock(&iommu->lock);
61 
62     return &elem->as;
63 }
64 
65 void remote_iommu_unplug_dev(PCIDevice *pci_dev)
66 {
67     AddressSpace *as = pci_device_iommu_address_space(pci_dev);
68     RemoteIommuElem *elem = NULL;
69 
70     if (as == &address_space_memory) {
71         return;
72     }
73 
74     elem = container_of(as, RemoteIommuElem, as);
75 
76     address_space_destroy(&elem->as);
77 
78     object_unref(elem->mr);
79 
80     elem->mr = NULL;
81 }
82 
83 static void remote_iommu_init(Object *obj)
84 {
85     RemoteIommu *iommu = REMOTE_IOMMU(obj);
86 
87     iommu->elem_by_devfn = g_hash_table_new_full(NULL, NULL, NULL, g_free);
88 
89     qemu_mutex_init(&iommu->lock);
90 }
91 
92 static void remote_iommu_finalize(Object *obj)
93 {
94     RemoteIommu *iommu = REMOTE_IOMMU(obj);
95 
96     qemu_mutex_destroy(&iommu->lock);
97 
98     g_hash_table_destroy(iommu->elem_by_devfn);
99 
100     iommu->elem_by_devfn = NULL;
101 }
102 
103 void remote_iommu_setup(PCIBus *pci_bus)
104 {
105     RemoteIommu *iommu = NULL;
106 
107     g_assert(pci_bus);
108 
109     iommu = REMOTE_IOMMU(object_new(TYPE_REMOTE_IOMMU));
110 
111     pci_setup_iommu(pci_bus, remote_iommu_find_add_as, iommu);
112 
113     object_property_add_child(OBJECT(pci_bus), "remote-iommu", OBJECT(iommu));
114 
115     object_unref(OBJECT(iommu));
116 }
117 
118 static const TypeInfo remote_iommu_info = {
119     .name = TYPE_REMOTE_IOMMU,
120     .parent = TYPE_OBJECT,
121     .instance_size = sizeof(RemoteIommu),
122     .instance_init = remote_iommu_init,
123     .instance_finalize = remote_iommu_finalize,
124 };
125 
126 static void remote_iommu_register_types(void)
127 {
128     type_register_static(&remote_iommu_info);
129 }
130 
131 type_init(remote_iommu_register_types)
132