1
2
3
4
5
6
7
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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39static 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
65void 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
83static 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
92static 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
103void 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
118static 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
126static void remote_iommu_register_types(void)
127{
128 type_register_static(&remote_iommu_info);
129}
130
131type_init(remote_iommu_register_types)
132