]>
Commit | Line | Data |
---|---|---|
318f67ce AK |
1 | /* |
2 | * DMA memory preregistration | |
3 | * | |
4 | * Authors: | |
5 | * Alexey Kardashevskiy <[email protected]> | |
6 | * | |
7 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
8 | * the COPYING file in the top-level directory. | |
9 | */ | |
10 | ||
11 | #include "qemu/osdep.h" | |
12 | #include "cpu.h" | |
13 | #include <sys/ioctl.h> | |
14 | #include <linux/vfio.h> | |
15 | ||
16 | #include "hw/vfio/vfio-common.h" | |
17 | #include "hw/hw.h" | |
c26bc185 | 18 | #include "exec/ram_addr.h" |
318f67ce | 19 | #include "qemu/error-report.h" |
d7d87836 | 20 | #include "qapi/error.h" |
318f67ce AK |
21 | #include "trace.h" |
22 | ||
23 | static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section) | |
24 | { | |
25 | if (memory_region_is_iommu(section->mr)) { | |
26 | hw_error("Cannot possibly preregister IOMMU memory"); | |
27 | } | |
28 | ||
29 | return !memory_region_is_ram(section->mr) || | |
21e00fa5 | 30 | memory_region_is_ram_device(section->mr); |
318f67ce AK |
31 | } |
32 | ||
33 | static void *vfio_prereg_gpa_to_vaddr(MemoryRegionSection *section, hwaddr gpa) | |
34 | { | |
35 | return memory_region_get_ram_ptr(section->mr) + | |
36 | section->offset_within_region + | |
37 | (gpa - section->offset_within_address_space); | |
38 | } | |
39 | ||
40 | static void vfio_prereg_listener_region_add(MemoryListener *listener, | |
41 | MemoryRegionSection *section) | |
42 | { | |
43 | VFIOContainer *container = container_of(listener, VFIOContainer, | |
44 | prereg_listener); | |
45 | const hwaddr gpa = section->offset_within_address_space; | |
46 | hwaddr end; | |
47 | int ret; | |
48 | hwaddr page_mask = qemu_real_host_page_mask; | |
49 | struct vfio_iommu_spapr_register_memory reg = { | |
50 | .argsz = sizeof(reg), | |
51 | .flags = 0, | |
52 | }; | |
53 | ||
54 | if (vfio_prereg_listener_skipped_section(section)) { | |
55 | trace_vfio_prereg_listener_region_add_skip( | |
56 | section->offset_within_address_space, | |
57 | section->offset_within_address_space + | |
58 | int128_get64(int128_sub(section->size, int128_one()))); | |
59 | return; | |
60 | } | |
61 | ||
62 | if (unlikely((section->offset_within_address_space & ~page_mask) || | |
63 | (section->offset_within_region & ~page_mask) || | |
64 | (int128_get64(section->size) & ~page_mask))) { | |
65 | error_report("%s received unaligned region", __func__); | |
66 | return; | |
67 | } | |
68 | ||
69 | end = section->offset_within_address_space + int128_get64(section->size); | |
70 | if (gpa >= end) { | |
71 | return; | |
72 | } | |
73 | ||
74 | memory_region_ref(section->mr); | |
75 | ||
76 | reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa); | |
77 | reg.size = end - gpa; | |
78 | ||
79 | ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®); | |
80 | trace_vfio_prereg_register(reg.vaddr, reg.size, ret ? -errno : 0); | |
81 | if (ret) { | |
82 | /* | |
83 | * On the initfn path, store the first error in the container so we | |
84 | * can gracefully fail. Runtime, there's not much we can do other | |
85 | * than throw a hardware error. | |
86 | */ | |
87 | if (!container->initialized) { | |
88 | if (!container->error) { | |
d7d87836 EA |
89 | error_setg_errno(&container->error, -ret, |
90 | "Memory registering failed"); | |
318f67ce AK |
91 | } |
92 | } else { | |
93 | hw_error("vfio: Memory registering failed, unable to continue"); | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | static void vfio_prereg_listener_region_del(MemoryListener *listener, | |
99 | MemoryRegionSection *section) | |
100 | { | |
101 | VFIOContainer *container = container_of(listener, VFIOContainer, | |
102 | prereg_listener); | |
103 | const hwaddr gpa = section->offset_within_address_space; | |
104 | hwaddr end; | |
105 | int ret; | |
106 | hwaddr page_mask = qemu_real_host_page_mask; | |
107 | struct vfio_iommu_spapr_register_memory reg = { | |
108 | .argsz = sizeof(reg), | |
109 | .flags = 0, | |
110 | }; | |
111 | ||
112 | if (vfio_prereg_listener_skipped_section(section)) { | |
113 | trace_vfio_prereg_listener_region_del_skip( | |
114 | section->offset_within_address_space, | |
115 | section->offset_within_address_space + | |
116 | int128_get64(int128_sub(section->size, int128_one()))); | |
117 | return; | |
118 | } | |
119 | ||
120 | if (unlikely((section->offset_within_address_space & ~page_mask) || | |
121 | (section->offset_within_region & ~page_mask) || | |
122 | (int128_get64(section->size) & ~page_mask))) { | |
123 | error_report("%s received unaligned region", __func__); | |
124 | return; | |
125 | } | |
126 | ||
127 | end = section->offset_within_address_space + int128_get64(section->size); | |
128 | if (gpa >= end) { | |
129 | return; | |
130 | } | |
131 | ||
132 | reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa); | |
133 | reg.size = end - gpa; | |
134 | ||
135 | ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®); | |
136 | trace_vfio_prereg_unregister(reg.vaddr, reg.size, ret ? -errno : 0); | |
137 | } | |
138 | ||
139 | const MemoryListener vfio_prereg_listener = { | |
140 | .region_add = vfio_prereg_listener_region_add, | |
141 | .region_del = vfio_prereg_listener_region_del, | |
142 | }; | |
2e4109de AK |
143 | |
144 | int vfio_spapr_create_window(VFIOContainer *container, | |
145 | MemoryRegionSection *section, | |
146 | hwaddr *pgsize) | |
147 | { | |
16107998 | 148 | int ret = 0; |
3df9d748 | 149 | IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); |
c26bc185 | 150 | uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr); |
16107998 | 151 | unsigned entries, bits_total, bits_per_level, max_levels; |
2e4109de | 152 | struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) }; |
905b7ee4 | 153 | long rampagesize = qemu_minrampagesize(); |
c26bc185 AK |
154 | |
155 | /* | |
156 | * The host might not support the guest supported IOMMU page size, | |
157 | * so we will use smaller physical IOMMU pages to back them. | |
158 | */ | |
3cdd801b AK |
159 | if (pagesize > rampagesize) { |
160 | pagesize = rampagesize; | |
c26bc185 AK |
161 | } |
162 | pagesize = 1ULL << (63 - clz64(container->pgsizes & | |
163 | (pagesize | (pagesize - 1)))); | |
164 | if (!pagesize) { | |
165 | error_report("Host doesn't support page size 0x%"PRIx64 | |
166 | ", the supported mask is 0x%lx", | |
167 | memory_region_iommu_get_min_page_size(iommu_mr), | |
168 | container->pgsizes); | |
169 | return -EINVAL; | |
170 | } | |
2e4109de AK |
171 | |
172 | /* | |
173 | * FIXME: For VFIO iommu types which have KVM acceleration to | |
174 | * avoid bouncing all map/unmaps through qemu this way, this | |
175 | * would be the right place to wire that up (tell the KVM | |
176 | * device emulation the VFIO iommu handles to use). | |
177 | */ | |
178 | create.window_size = int128_get64(section->size); | |
179 | create.page_shift = ctz64(pagesize); | |
180 | /* | |
16107998 AK |
181 | * SPAPR host supports multilevel TCE tables. We try to guess optimal |
182 | * levels number and if this fails (for example due to the host memory | |
183 | * fragmentation), we increase levels. The DMA address structure is: | |
184 | * rrrrrrrr rxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx iiiiiiii | |
185 | * where: | |
186 | * r = reserved (bits >= 55 are reserved in the existing hardware) | |
187 | * i = IOMMU page offset (64K in this example) | |
188 | * x = bits to index a TCE which can be split to equal chunks to index | |
189 | * within the level. | |
190 | * The aim is to split "x" to smaller possible number of levels. | |
2e4109de AK |
191 | */ |
192 | entries = create.window_size >> create.page_shift; | |
16107998 AK |
193 | /* bits_total is number of "x" needed */ |
194 | bits_total = ctz64(entries * sizeof(uint64_t)); | |
195 | /* | |
196 | * bits_per_level is a safe guess of how much we can allocate per level: | |
197 | * 8 is the current minimum for CONFIG_FORCE_MAX_ZONEORDER and MAX_ORDER | |
198 | * is usually bigger than that. | |
199 | * Below we look at getpagesize() as TCEs are allocated from system pages. | |
200 | */ | |
201 | bits_per_level = ctz64(getpagesize()) + 8; | |
202 | create.levels = bits_total / bits_per_level; | |
203 | if (bits_total % bits_per_level) { | |
204 | ++create.levels; | |
205 | } | |
206 | max_levels = (64 - create.page_shift) / ctz64(getpagesize()); | |
207 | for ( ; create.levels <= max_levels; ++create.levels) { | |
208 | ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create); | |
209 | if (!ret) { | |
210 | break; | |
211 | } | |
212 | } | |
2e4109de AK |
213 | if (ret) { |
214 | error_report("Failed to create a window, ret = %d (%m)", ret); | |
215 | return -errno; | |
216 | } | |
217 | ||
218 | if (create.start_addr != section->offset_within_address_space) { | |
219 | vfio_spapr_remove_window(container, create.start_addr); | |
220 | ||
221 | error_report("Host doesn't support DMA window at %"HWADDR_PRIx", must be %"PRIx64, | |
222 | section->offset_within_address_space, | |
223 | (uint64_t)create.start_addr); | |
2e4109de AK |
224 | return -EINVAL; |
225 | } | |
226 | trace_vfio_spapr_create_window(create.page_shift, | |
16107998 | 227 | create.levels, |
2e4109de AK |
228 | create.window_size, |
229 | create.start_addr); | |
230 | *pgsize = pagesize; | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | int vfio_spapr_remove_window(VFIOContainer *container, | |
236 | hwaddr offset_within_address_space) | |
237 | { | |
238 | struct vfio_iommu_spapr_tce_remove remove = { | |
239 | .argsz = sizeof(remove), | |
240 | .start_addr = offset_within_address_space, | |
241 | }; | |
242 | int ret; | |
243 | ||
244 | ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove); | |
245 | if (ret) { | |
246 | error_report("Failed to remove window at %"PRIx64, | |
247 | (uint64_t)remove.start_addr); | |
248 | return -errno; | |
249 | } | |
250 | ||
251 | trace_vfio_spapr_remove_window(offset_within_address_space); | |
252 | ||
253 | return 0; | |
254 | } |