]>
Commit | Line | Data |
---|---|---|
e2c7d025 EA |
1 | /* |
2 | * generic functions used by VFIO devices | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Alex Williamson <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | * Based on qemu-kvm device-assignment: | |
13 | * Adapted for KVM by Qumranet. | |
14 | * Copyright (c) 2007, Neocleus, Alex Novik ([email protected]) | |
15 | * Copyright (c) 2007, Neocleus, Guy Zana ([email protected]) | |
16 | * Copyright (C) 2008, Qumranet, Amit Shah ([email protected]) | |
17 | * Copyright (C) 2008, Red Hat, Amit Shah ([email protected]) | |
18 | * Copyright (C) 2008, IBM, Muli Ben-Yehuda ([email protected]) | |
19 | */ | |
20 | ||
c6eacb1a | 21 | #include "qemu/osdep.h" |
e2c7d025 | 22 | #include <sys/ioctl.h> |
a9c94277 MA |
23 | #ifdef CONFIG_KVM |
24 | #include <linux/kvm.h> | |
25 | #endif | |
e2c7d025 EA |
26 | #include <linux/vfio.h> |
27 | ||
28 | #include "hw/vfio/vfio-common.h" | |
29 | #include "hw/vfio/vfio.h" | |
30 | #include "exec/address-spaces.h" | |
31 | #include "exec/memory.h" | |
32 | #include "hw/hw.h" | |
33 | #include "qemu/error-report.h" | |
f4ec5e26 | 34 | #include "qemu/range.h" |
e2c7d025 EA |
35 | #include "sysemu/kvm.h" |
36 | #include "trace.h" | |
01905f58 | 37 | #include "qapi/error.h" |
e2c7d025 EA |
38 | |
39 | struct vfio_group_head vfio_group_list = | |
39cb514f | 40 | QLIST_HEAD_INITIALIZER(vfio_group_list); |
e2c7d025 EA |
41 | struct vfio_as_head vfio_address_spaces = |
42 | QLIST_HEAD_INITIALIZER(vfio_address_spaces); | |
43 | ||
44 | #ifdef CONFIG_KVM | |
45 | /* | |
46 | * We have a single VFIO pseudo device per KVM VM. Once created it lives | |
47 | * for the life of the VM. Closing the file descriptor only drops our | |
48 | * reference to it and the device's reference to kvm. Therefore once | |
49 | * initialized, this file descriptor is only released on QEMU exit and | |
50 | * we'll re-use it should another vfio device be attached before then. | |
51 | */ | |
52 | static int vfio_kvm_device_fd = -1; | |
53 | #endif | |
54 | ||
55 | /* | |
56 | * Common VFIO interrupt disable | |
57 | */ | |
58 | void vfio_disable_irqindex(VFIODevice *vbasedev, int index) | |
59 | { | |
60 | struct vfio_irq_set irq_set = { | |
61 | .argsz = sizeof(irq_set), | |
62 | .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER, | |
63 | .index = index, | |
64 | .start = 0, | |
65 | .count = 0, | |
66 | }; | |
67 | ||
68 | ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); | |
69 | } | |
70 | ||
71 | void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index) | |
72 | { | |
73 | struct vfio_irq_set irq_set = { | |
74 | .argsz = sizeof(irq_set), | |
75 | .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK, | |
76 | .index = index, | |
77 | .start = 0, | |
78 | .count = 1, | |
79 | }; | |
80 | ||
81 | ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); | |
82 | } | |
83 | ||
84 | void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index) | |
85 | { | |
86 | struct vfio_irq_set irq_set = { | |
87 | .argsz = sizeof(irq_set), | |
88 | .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK, | |
89 | .index = index, | |
90 | .start = 0, | |
91 | .count = 1, | |
92 | }; | |
93 | ||
94 | ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); | |
95 | } | |
96 | ||
97 | /* | |
98 | * IO Port/MMIO - Beware of the endians, VFIO is always little endian | |
99 | */ | |
100 | void vfio_region_write(void *opaque, hwaddr addr, | |
101 | uint64_t data, unsigned size) | |
102 | { | |
103 | VFIORegion *region = opaque; | |
104 | VFIODevice *vbasedev = region->vbasedev; | |
105 | union { | |
106 | uint8_t byte; | |
107 | uint16_t word; | |
108 | uint32_t dword; | |
109 | uint64_t qword; | |
110 | } buf; | |
111 | ||
112 | switch (size) { | |
113 | case 1: | |
114 | buf.byte = data; | |
115 | break; | |
116 | case 2: | |
117 | buf.word = cpu_to_le16(data); | |
118 | break; | |
119 | case 4: | |
120 | buf.dword = cpu_to_le32(data); | |
121 | break; | |
122 | default: | |
123 | hw_error("vfio: unsupported write size, %d bytes", size); | |
124 | break; | |
125 | } | |
126 | ||
127 | if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) { | |
128 | error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64 | |
129 | ",%d) failed: %m", | |
130 | __func__, vbasedev->name, region->nr, | |
131 | addr, data, size); | |
132 | } | |
133 | ||
134 | trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size); | |
135 | ||
136 | /* | |
137 | * A read or write to a BAR always signals an INTx EOI. This will | |
138 | * do nothing if not pending (including not in INTx mode). We assume | |
139 | * that a BAR access is in response to an interrupt and that BAR | |
140 | * accesses will service the interrupt. Unfortunately, we don't know | |
141 | * which access will service the interrupt, so we're potentially | |
142 | * getting quite a few host interrupts per guest interrupt. | |
143 | */ | |
144 | vbasedev->ops->vfio_eoi(vbasedev); | |
145 | } | |
146 | ||
147 | uint64_t vfio_region_read(void *opaque, | |
148 | hwaddr addr, unsigned size) | |
149 | { | |
150 | VFIORegion *region = opaque; | |
151 | VFIODevice *vbasedev = region->vbasedev; | |
152 | union { | |
153 | uint8_t byte; | |
154 | uint16_t word; | |
155 | uint32_t dword; | |
156 | uint64_t qword; | |
157 | } buf; | |
158 | uint64_t data = 0; | |
159 | ||
160 | if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) { | |
161 | error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m", | |
162 | __func__, vbasedev->name, region->nr, | |
163 | addr, size); | |
164 | return (uint64_t)-1; | |
165 | } | |
166 | switch (size) { | |
167 | case 1: | |
168 | data = buf.byte; | |
169 | break; | |
170 | case 2: | |
171 | data = le16_to_cpu(buf.word); | |
172 | break; | |
173 | case 4: | |
174 | data = le32_to_cpu(buf.dword); | |
175 | break; | |
176 | default: | |
177 | hw_error("vfio: unsupported read size, %d bytes", size); | |
178 | break; | |
179 | } | |
180 | ||
181 | trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data); | |
182 | ||
183 | /* Same as write above */ | |
184 | vbasedev->ops->vfio_eoi(vbasedev); | |
185 | ||
186 | return data; | |
187 | } | |
188 | ||
189 | const MemoryRegionOps vfio_region_ops = { | |
190 | .read = vfio_region_read, | |
191 | .write = vfio_region_write, | |
192 | .endianness = DEVICE_LITTLE_ENDIAN, | |
193 | }; | |
194 | ||
195 | /* | |
196 | * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86 | |
197 | */ | |
198 | static int vfio_dma_unmap(VFIOContainer *container, | |
199 | hwaddr iova, ram_addr_t size) | |
200 | { | |
201 | struct vfio_iommu_type1_dma_unmap unmap = { | |
202 | .argsz = sizeof(unmap), | |
203 | .flags = 0, | |
204 | .iova = iova, | |
205 | .size = size, | |
206 | }; | |
207 | ||
208 | if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) { | |
78e5b17f | 209 | error_report("VFIO_UNMAP_DMA: %d", -errno); |
e2c7d025 EA |
210 | return -errno; |
211 | } | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
216 | static int vfio_dma_map(VFIOContainer *container, hwaddr iova, | |
217 | ram_addr_t size, void *vaddr, bool readonly) | |
218 | { | |
219 | struct vfio_iommu_type1_dma_map map = { | |
220 | .argsz = sizeof(map), | |
221 | .flags = VFIO_DMA_MAP_FLAG_READ, | |
222 | .vaddr = (__u64)(uintptr_t)vaddr, | |
223 | .iova = iova, | |
224 | .size = size, | |
225 | }; | |
226 | ||
227 | if (!readonly) { | |
228 | map.flags |= VFIO_DMA_MAP_FLAG_WRITE; | |
229 | } | |
230 | ||
231 | /* | |
232 | * Try the mapping, if it fails with EBUSY, unmap the region and try | |
233 | * again. This shouldn't be necessary, but we sometimes see it in | |
b6af0975 | 234 | * the VGA ROM space. |
e2c7d025 EA |
235 | */ |
236 | if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 || | |
237 | (errno == EBUSY && vfio_dma_unmap(container, iova, size) == 0 && | |
238 | ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) { | |
239 | return 0; | |
240 | } | |
241 | ||
78e5b17f | 242 | error_report("VFIO_MAP_DMA: %d", -errno); |
e2c7d025 EA |
243 | return -errno; |
244 | } | |
245 | ||
f4ec5e26 AK |
246 | static void vfio_host_win_add(VFIOContainer *container, |
247 | hwaddr min_iova, hwaddr max_iova, | |
248 | uint64_t iova_pgsizes) | |
249 | { | |
250 | VFIOHostDMAWindow *hostwin; | |
251 | ||
252 | QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { | |
253 | if (ranges_overlap(hostwin->min_iova, | |
254 | hostwin->max_iova - hostwin->min_iova + 1, | |
255 | min_iova, | |
256 | max_iova - min_iova + 1)) { | |
257 | hw_error("%s: Overlapped IOMMU are not enabled", __func__); | |
258 | } | |
259 | } | |
260 | ||
261 | hostwin = g_malloc0(sizeof(*hostwin)); | |
262 | ||
263 | hostwin->min_iova = min_iova; | |
264 | hostwin->max_iova = max_iova; | |
265 | hostwin->iova_pgsizes = iova_pgsizes; | |
266 | QLIST_INSERT_HEAD(&container->hostwin_list, hostwin, hostwin_next); | |
267 | } | |
268 | ||
2e4109de AK |
269 | static int vfio_host_win_del(VFIOContainer *container, hwaddr min_iova, |
270 | hwaddr max_iova) | |
271 | { | |
272 | VFIOHostDMAWindow *hostwin; | |
273 | ||
274 | QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { | |
275 | if (hostwin->min_iova == min_iova && hostwin->max_iova == max_iova) { | |
276 | QLIST_REMOVE(hostwin, hostwin_next); | |
277 | return 0; | |
278 | } | |
279 | } | |
280 | ||
281 | return -1; | |
282 | } | |
283 | ||
e2c7d025 EA |
284 | static bool vfio_listener_skipped_section(MemoryRegionSection *section) |
285 | { | |
286 | return (!memory_region_is_ram(section->mr) && | |
287 | !memory_region_is_iommu(section->mr)) || | |
288 | /* | |
289 | * Sizing an enabled 64-bit BAR can cause spurious mappings to | |
290 | * addresses in the upper part of the 64-bit address space. These | |
291 | * are never accessed by the CPU and beyond the address width of | |
292 | * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width. | |
293 | */ | |
294 | section->offset_within_address_space & (1ULL << 63); | |
295 | } | |
296 | ||
4a4b88fb PX |
297 | /* Called with rcu_read_lock held. */ |
298 | static bool vfio_get_vaddr(IOMMUTLBEntry *iotlb, void **vaddr, | |
299 | bool *read_only) | |
e2c7d025 | 300 | { |
e2c7d025 EA |
301 | MemoryRegion *mr; |
302 | hwaddr xlat; | |
303 | hwaddr len = iotlb->addr_mask + 1; | |
4a4b88fb | 304 | bool writable = iotlb->perm & IOMMU_WO; |
f1f93650 | 305 | |
e2c7d025 EA |
306 | /* |
307 | * The IOMMU TLB entry we have just covers translation through | |
308 | * this IOMMU to its immediate target. We need to translate | |
309 | * it the rest of the way through to memory. | |
310 | */ | |
311 | mr = address_space_translate(&address_space_memory, | |
312 | iotlb->translated_addr, | |
4a4b88fb | 313 | &xlat, &len, writable); |
e2c7d025 | 314 | if (!memory_region_is_ram(mr)) { |
78e5b17f | 315 | error_report("iommu map to non memory area %"HWADDR_PRIx"", |
e2c7d025 | 316 | xlat); |
4a4b88fb | 317 | return false; |
e2c7d025 | 318 | } |
4a4b88fb | 319 | |
e2c7d025 EA |
320 | /* |
321 | * Translation truncates length to the IOMMU page size, | |
322 | * check that it did not truncate too much. | |
323 | */ | |
324 | if (len & iotlb->addr_mask) { | |
78e5b17f | 325 | error_report("iommu has granularity incompatible with target AS"); |
4a4b88fb PX |
326 | return false; |
327 | } | |
328 | ||
329 | *vaddr = memory_region_get_ram_ptr(mr) + xlat; | |
330 | *read_only = !writable || mr->readonly; | |
331 | ||
332 | return true; | |
333 | } | |
334 | ||
335 | static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) | |
336 | { | |
337 | VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); | |
338 | VFIOContainer *container = giommu->container; | |
339 | hwaddr iova = iotlb->iova + giommu->iommu_offset; | |
340 | bool read_only; | |
341 | void *vaddr; | |
342 | int ret; | |
343 | ||
344 | trace_vfio_iommu_map_notify(iotlb->perm == IOMMU_NONE ? "UNMAP" : "MAP", | |
345 | iova, iova + iotlb->addr_mask); | |
346 | ||
347 | if (iotlb->target_as != &address_space_memory) { | |
348 | error_report("Wrong target AS \"%s\", only system memory is allowed", | |
349 | iotlb->target_as->name ? iotlb->target_as->name : "none"); | |
350 | return; | |
351 | } | |
352 | ||
353 | rcu_read_lock(); | |
354 | ||
355 | if (!vfio_get_vaddr(iotlb, &vaddr, &read_only)) { | |
41063e1e | 356 | goto out; |
e2c7d025 EA |
357 | } |
358 | ||
359 | if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) { | |
4a4b88fb PX |
360 | /* |
361 | * vaddr is only valid until rcu_read_unlock(). But after | |
362 | * vfio_dma_map has set up the mapping the pages will be | |
363 | * pinned by the kernel. This makes sure that the RAM backend | |
364 | * of vaddr will always be there, even if the memory object is | |
365 | * destroyed and its backing memory munmap-ed. | |
366 | */ | |
d78c19b5 | 367 | ret = vfio_dma_map(container, iova, |
e2c7d025 | 368 | iotlb->addr_mask + 1, vaddr, |
4a4b88fb | 369 | read_only); |
e2c7d025 EA |
370 | if (ret) { |
371 | error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", " | |
372 | "0x%"HWADDR_PRIx", %p) = %d (%m)", | |
d78c19b5 | 373 | container, iova, |
e2c7d025 EA |
374 | iotlb->addr_mask + 1, vaddr, ret); |
375 | } | |
376 | } else { | |
d78c19b5 | 377 | ret = vfio_dma_unmap(container, iova, iotlb->addr_mask + 1); |
e2c7d025 EA |
378 | if (ret) { |
379 | error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " | |
380 | "0x%"HWADDR_PRIx") = %d (%m)", | |
d78c19b5 | 381 | container, iova, |
e2c7d025 EA |
382 | iotlb->addr_mask + 1, ret); |
383 | } | |
384 | } | |
41063e1e PB |
385 | out: |
386 | rcu_read_unlock(); | |
e2c7d025 EA |
387 | } |
388 | ||
389 | static void vfio_listener_region_add(MemoryListener *listener, | |
390 | MemoryRegionSection *section) | |
391 | { | |
ee0bf0e5 | 392 | VFIOContainer *container = container_of(listener, VFIOContainer, listener); |
e2c7d025 | 393 | hwaddr iova, end; |
55efcc53 | 394 | Int128 llend, llsize; |
e2c7d025 EA |
395 | void *vaddr; |
396 | int ret; | |
f4ec5e26 AK |
397 | VFIOHostDMAWindow *hostwin; |
398 | bool hostwin_found; | |
e2c7d025 EA |
399 | |
400 | if (vfio_listener_skipped_section(section)) { | |
401 | trace_vfio_listener_region_add_skip( | |
402 | section->offset_within_address_space, | |
403 | section->offset_within_address_space + | |
404 | int128_get64(int128_sub(section->size, int128_one()))); | |
405 | return; | |
406 | } | |
407 | ||
408 | if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) != | |
409 | (section->offset_within_region & ~TARGET_PAGE_MASK))) { | |
410 | error_report("%s received unaligned region", __func__); | |
411 | return; | |
412 | } | |
413 | ||
414 | iova = TARGET_PAGE_ALIGN(section->offset_within_address_space); | |
415 | llend = int128_make64(section->offset_within_address_space); | |
416 | llend = int128_add(llend, section->size); | |
417 | llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK)); | |
418 | ||
419 | if (int128_ge(int128_make64(iova), llend)) { | |
420 | return; | |
421 | } | |
55efcc53 | 422 | end = int128_get64(int128_sub(llend, int128_one())); |
3898aad3 | 423 | |
2e4109de AK |
424 | if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { |
425 | VFIOHostDMAWindow *hostwin; | |
426 | hwaddr pgsize = 0; | |
427 | ||
428 | /* For now intersections are not allowed, we may relax this later */ | |
429 | QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { | |
430 | if (ranges_overlap(hostwin->min_iova, | |
431 | hostwin->max_iova - hostwin->min_iova + 1, | |
432 | section->offset_within_address_space, | |
433 | int128_get64(section->size))) { | |
434 | ret = -1; | |
435 | goto fail; | |
436 | } | |
437 | } | |
438 | ||
439 | ret = vfio_spapr_create_window(container, section, &pgsize); | |
440 | if (ret) { | |
441 | goto fail; | |
442 | } | |
443 | ||
444 | vfio_host_win_add(container, section->offset_within_address_space, | |
445 | section->offset_within_address_space + | |
446 | int128_get64(section->size) - 1, pgsize); | |
447 | } | |
448 | ||
f4ec5e26 AK |
449 | hostwin_found = false; |
450 | QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { | |
451 | if (hostwin->min_iova <= iova && end <= hostwin->max_iova) { | |
452 | hostwin_found = true; | |
453 | break; | |
454 | } | |
455 | } | |
456 | ||
457 | if (!hostwin_found) { | |
3898aad3 DG |
458 | error_report("vfio: IOMMU container %p can't map guest IOVA region" |
459 | " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx, | |
55efcc53 | 460 | container, iova, end); |
3898aad3 DG |
461 | ret = -EFAULT; |
462 | goto fail; | |
463 | } | |
e2c7d025 EA |
464 | |
465 | memory_region_ref(section->mr); | |
466 | ||
467 | if (memory_region_is_iommu(section->mr)) { | |
468 | VFIOGuestIOMMU *giommu; | |
469 | ||
55efcc53 | 470 | trace_vfio_listener_region_add_iommu(iova, end); |
e2c7d025 | 471 | /* |
e2c7d025 EA |
472 | * FIXME: For VFIO iommu types which have KVM acceleration to |
473 | * avoid bouncing all map/unmaps through qemu this way, this | |
474 | * would be the right place to wire that up (tell the KVM | |
475 | * device emulation the VFIO iommu handles to use). | |
476 | */ | |
e2c7d025 EA |
477 | giommu = g_malloc0(sizeof(*giommu)); |
478 | giommu->iommu = section->mr; | |
d78c19b5 AK |
479 | giommu->iommu_offset = section->offset_within_address_space - |
480 | section->offset_within_region; | |
e2c7d025 EA |
481 | giommu->container = container; |
482 | giommu->n.notify = vfio_iommu_map_notify; | |
cdb30812 | 483 | giommu->n.notifier_flags = IOMMU_NOTIFIER_ALL; |
e2c7d025 | 484 | QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); |
508ce5eb | 485 | |
e2c7d025 | 486 | memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); |
f682e9c2 | 487 | memory_region_iommu_replay(giommu->iommu, &giommu->n, false); |
e2c7d025 EA |
488 | |
489 | return; | |
490 | } | |
491 | ||
492 | /* Here we assume that memory_region_is_ram(section->mr)==true */ | |
493 | ||
e2c7d025 EA |
494 | vaddr = memory_region_get_ram_ptr(section->mr) + |
495 | section->offset_within_region + | |
496 | (iova - section->offset_within_address_space); | |
497 | ||
55efcc53 | 498 | trace_vfio_listener_region_add_ram(iova, end, vaddr); |
e2c7d025 | 499 | |
55efcc53 BD |
500 | llsize = int128_sub(llend, int128_make64(iova)); |
501 | ||
502 | ret = vfio_dma_map(container, iova, int128_get64(llsize), | |
503 | vaddr, section->readonly); | |
e2c7d025 EA |
504 | if (ret) { |
505 | error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", " | |
506 | "0x%"HWADDR_PRIx", %p) = %d (%m)", | |
55efcc53 | 507 | container, iova, int128_get64(llsize), vaddr, ret); |
ac6dc389 DG |
508 | goto fail; |
509 | } | |
e2c7d025 | 510 | |
ac6dc389 DG |
511 | return; |
512 | ||
513 | fail: | |
514 | /* | |
515 | * On the initfn path, store the first error in the container so we | |
516 | * can gracefully fail. Runtime, there's not much we can do other | |
517 | * than throw a hardware error. | |
518 | */ | |
519 | if (!container->initialized) { | |
520 | if (!container->error) { | |
521 | container->error = ret; | |
e2c7d025 | 522 | } |
ac6dc389 DG |
523 | } else { |
524 | hw_error("vfio: DMA mapping failed, unable to continue"); | |
e2c7d025 EA |
525 | } |
526 | } | |
527 | ||
528 | static void vfio_listener_region_del(MemoryListener *listener, | |
529 | MemoryRegionSection *section) | |
530 | { | |
ee0bf0e5 | 531 | VFIOContainer *container = container_of(listener, VFIOContainer, listener); |
e2c7d025 | 532 | hwaddr iova, end; |
7a057b4f | 533 | Int128 llend, llsize; |
e2c7d025 EA |
534 | int ret; |
535 | ||
536 | if (vfio_listener_skipped_section(section)) { | |
537 | trace_vfio_listener_region_del_skip( | |
538 | section->offset_within_address_space, | |
539 | section->offset_within_address_space + | |
540 | int128_get64(int128_sub(section->size, int128_one()))); | |
541 | return; | |
542 | } | |
543 | ||
544 | if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) != | |
545 | (section->offset_within_region & ~TARGET_PAGE_MASK))) { | |
546 | error_report("%s received unaligned region", __func__); | |
547 | return; | |
548 | } | |
549 | ||
550 | if (memory_region_is_iommu(section->mr)) { | |
551 | VFIOGuestIOMMU *giommu; | |
552 | ||
553 | QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) { | |
554 | if (giommu->iommu == section->mr) { | |
d22d8956 AK |
555 | memory_region_unregister_iommu_notifier(giommu->iommu, |
556 | &giommu->n); | |
e2c7d025 EA |
557 | QLIST_REMOVE(giommu, giommu_next); |
558 | g_free(giommu); | |
559 | break; | |
560 | } | |
561 | } | |
562 | ||
563 | /* | |
564 | * FIXME: We assume the one big unmap below is adequate to | |
565 | * remove any individual page mappings in the IOMMU which | |
566 | * might have been copied into VFIO. This works for a page table | |
567 | * based IOMMU where a big unmap flattens a large range of IO-PTEs. | |
568 | * That may not be true for all IOMMU types. | |
569 | */ | |
570 | } | |
571 | ||
572 | iova = TARGET_PAGE_ALIGN(section->offset_within_address_space); | |
7a057b4f AK |
573 | llend = int128_make64(section->offset_within_address_space); |
574 | llend = int128_add(llend, section->size); | |
575 | llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK)); | |
e2c7d025 | 576 | |
7a057b4f | 577 | if (int128_ge(int128_make64(iova), llend)) { |
e2c7d025 EA |
578 | return; |
579 | } | |
7a057b4f AK |
580 | end = int128_get64(int128_sub(llend, int128_one())); |
581 | ||
582 | llsize = int128_sub(llend, int128_make64(iova)); | |
e2c7d025 | 583 | |
7a057b4f | 584 | trace_vfio_listener_region_del(iova, end); |
e2c7d025 | 585 | |
7a057b4f | 586 | ret = vfio_dma_unmap(container, iova, int128_get64(llsize)); |
e2c7d025 EA |
587 | memory_region_unref(section->mr); |
588 | if (ret) { | |
589 | error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " | |
590 | "0x%"HWADDR_PRIx") = %d (%m)", | |
7a057b4f | 591 | container, iova, int128_get64(llsize), ret); |
e2c7d025 | 592 | } |
2e4109de AK |
593 | |
594 | if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { | |
595 | vfio_spapr_remove_window(container, | |
596 | section->offset_within_address_space); | |
597 | if (vfio_host_win_del(container, | |
598 | section->offset_within_address_space, | |
599 | section->offset_within_address_space + | |
600 | int128_get64(section->size) - 1) < 0) { | |
601 | hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx, | |
602 | __func__, section->offset_within_address_space); | |
603 | } | |
604 | } | |
e2c7d025 EA |
605 | } |
606 | ||
51b833f4 | 607 | static const MemoryListener vfio_memory_listener = { |
e2c7d025 EA |
608 | .region_add = vfio_listener_region_add, |
609 | .region_del = vfio_listener_region_del, | |
610 | }; | |
611 | ||
51b833f4 | 612 | static void vfio_listener_release(VFIOContainer *container) |
e2c7d025 | 613 | { |
ee0bf0e5 | 614 | memory_listener_unregister(&container->listener); |
318f67ce AK |
615 | if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { |
616 | memory_listener_unregister(&container->prereg_listener); | |
617 | } | |
e2c7d025 EA |
618 | } |
619 | ||
b53b0f69 AW |
620 | static struct vfio_info_cap_header * |
621 | vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id) | |
622 | { | |
623 | struct vfio_info_cap_header *hdr; | |
624 | void *ptr = info; | |
625 | ||
626 | if (!(info->flags & VFIO_REGION_INFO_FLAG_CAPS)) { | |
627 | return NULL; | |
628 | } | |
629 | ||
630 | for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) { | |
631 | if (hdr->id == id) { | |
632 | return hdr; | |
633 | } | |
634 | } | |
635 | ||
636 | return NULL; | |
637 | } | |
638 | ||
24acf72b AW |
639 | static int vfio_setup_region_sparse_mmaps(VFIORegion *region, |
640 | struct vfio_region_info *info) | |
b53b0f69 AW |
641 | { |
642 | struct vfio_info_cap_header *hdr; | |
643 | struct vfio_region_info_cap_sparse_mmap *sparse; | |
24acf72b | 644 | int i, j; |
b53b0f69 AW |
645 | |
646 | hdr = vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMAP); | |
647 | if (!hdr) { | |
24acf72b | 648 | return -ENODEV; |
b53b0f69 AW |
649 | } |
650 | ||
651 | sparse = container_of(hdr, struct vfio_region_info_cap_sparse_mmap, header); | |
652 | ||
653 | trace_vfio_region_sparse_mmap_header(region->vbasedev->name, | |
654 | region->nr, sparse->nr_areas); | |
655 | ||
24acf72b AW |
656 | region->mmaps = g_new0(VFIOMmap, sparse->nr_areas); |
657 | ||
658 | for (i = 0, j = 0; i < sparse->nr_areas; i++) { | |
659 | trace_vfio_region_sparse_mmap_entry(i, sparse->areas[i].offset, | |
660 | sparse->areas[i].offset + | |
661 | sparse->areas[i].size); | |
b53b0f69 | 662 | |
24acf72b AW |
663 | if (sparse->areas[i].size) { |
664 | region->mmaps[j].offset = sparse->areas[i].offset; | |
665 | region->mmaps[j].size = sparse->areas[i].size; | |
666 | j++; | |
667 | } | |
b53b0f69 | 668 | } |
24acf72b AW |
669 | |
670 | region->nr_mmaps = j; | |
671 | region->mmaps = g_realloc(region->mmaps, j * sizeof(VFIOMmap)); | |
672 | ||
673 | return 0; | |
b53b0f69 AW |
674 | } |
675 | ||
db0da029 AW |
676 | int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, |
677 | int index, const char *name) | |
e2c7d025 | 678 | { |
db0da029 AW |
679 | struct vfio_region_info *info; |
680 | int ret; | |
681 | ||
682 | ret = vfio_get_region_info(vbasedev, index, &info); | |
683 | if (ret) { | |
684 | return ret; | |
685 | } | |
686 | ||
687 | region->vbasedev = vbasedev; | |
688 | region->flags = info->flags; | |
689 | region->size = info->size; | |
690 | region->fd_offset = info->offset; | |
691 | region->nr = index; | |
692 | ||
693 | if (region->size) { | |
694 | region->mem = g_new0(MemoryRegion, 1); | |
695 | memory_region_init_io(region->mem, obj, &vfio_region_ops, | |
696 | region, name, region->size); | |
e2c7d025 | 697 | |
db0da029 | 698 | if (!vbasedev->no_mmap && |
95251725 | 699 | region->flags & VFIO_REGION_INFO_FLAG_MMAP) { |
e2c7d025 | 700 | |
24acf72b | 701 | ret = vfio_setup_region_sparse_mmaps(region, info); |
db0da029 | 702 | |
24acf72b | 703 | if (ret) { |
b53b0f69 AW |
704 | region->nr_mmaps = 1; |
705 | region->mmaps = g_new0(VFIOMmap, region->nr_mmaps); | |
706 | region->mmaps[0].offset = 0; | |
707 | region->mmaps[0].size = region->size; | |
708 | } | |
e2c7d025 | 709 | } |
db0da029 AW |
710 | } |
711 | ||
712 | g_free(info); | |
713 | ||
714 | trace_vfio_region_setup(vbasedev->name, index, name, | |
715 | region->flags, region->fd_offset, region->size); | |
716 | return 0; | |
717 | } | |
e2c7d025 | 718 | |
db0da029 AW |
719 | int vfio_region_mmap(VFIORegion *region) |
720 | { | |
721 | int i, prot = 0; | |
722 | char *name; | |
723 | ||
724 | if (!region->mem) { | |
725 | return 0; | |
726 | } | |
727 | ||
728 | prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0; | |
729 | prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0; | |
730 | ||
731 | for (i = 0; i < region->nr_mmaps; i++) { | |
732 | region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot, | |
733 | MAP_SHARED, region->vbasedev->fd, | |
734 | region->fd_offset + | |
735 | region->mmaps[i].offset); | |
736 | if (region->mmaps[i].mmap == MAP_FAILED) { | |
737 | int ret = -errno; | |
738 | ||
739 | trace_vfio_region_mmap_fault(memory_region_name(region->mem), i, | |
740 | region->fd_offset + | |
741 | region->mmaps[i].offset, | |
742 | region->fd_offset + | |
743 | region->mmaps[i].offset + | |
744 | region->mmaps[i].size - 1, ret); | |
745 | ||
746 | region->mmaps[i].mmap = NULL; | |
747 | ||
748 | for (i--; i >= 0; i--) { | |
749 | memory_region_del_subregion(region->mem, ®ion->mmaps[i].mem); | |
750 | munmap(region->mmaps[i].mmap, region->mmaps[i].size); | |
751 | object_unparent(OBJECT(®ion->mmaps[i].mem)); | |
752 | region->mmaps[i].mmap = NULL; | |
753 | } | |
754 | ||
755 | return ret; | |
e2c7d025 EA |
756 | } |
757 | ||
db0da029 AW |
758 | name = g_strdup_printf("%s mmaps[%d]", |
759 | memory_region_name(region->mem), i); | |
21e00fa5 AW |
760 | memory_region_init_ram_device_ptr(®ion->mmaps[i].mem, |
761 | memory_region_owner(region->mem), | |
762 | name, region->mmaps[i].size, | |
763 | region->mmaps[i].mmap); | |
db0da029 | 764 | g_free(name); |
db0da029 AW |
765 | memory_region_add_subregion(region->mem, region->mmaps[i].offset, |
766 | ®ion->mmaps[i].mem); | |
767 | ||
768 | trace_vfio_region_mmap(memory_region_name(®ion->mmaps[i].mem), | |
769 | region->mmaps[i].offset, | |
770 | region->mmaps[i].offset + | |
771 | region->mmaps[i].size - 1); | |
772 | } | |
773 | ||
774 | return 0; | |
775 | } | |
776 | ||
777 | void vfio_region_exit(VFIORegion *region) | |
778 | { | |
779 | int i; | |
780 | ||
781 | if (!region->mem) { | |
782 | return; | |
783 | } | |
784 | ||
785 | for (i = 0; i < region->nr_mmaps; i++) { | |
786 | if (region->mmaps[i].mmap) { | |
787 | memory_region_del_subregion(region->mem, ®ion->mmaps[i].mem); | |
e2c7d025 | 788 | } |
db0da029 | 789 | } |
e2c7d025 | 790 | |
db0da029 AW |
791 | trace_vfio_region_exit(region->vbasedev->name, region->nr); |
792 | } | |
793 | ||
794 | void vfio_region_finalize(VFIORegion *region) | |
795 | { | |
796 | int i; | |
797 | ||
798 | if (!region->mem) { | |
799 | return; | |
e2c7d025 EA |
800 | } |
801 | ||
db0da029 AW |
802 | for (i = 0; i < region->nr_mmaps; i++) { |
803 | if (region->mmaps[i].mmap) { | |
804 | munmap(region->mmaps[i].mmap, region->mmaps[i].size); | |
805 | object_unparent(OBJECT(®ion->mmaps[i].mem)); | |
806 | } | |
807 | } | |
808 | ||
809 | object_unparent(OBJECT(region->mem)); | |
810 | ||
811 | g_free(region->mem); | |
812 | g_free(region->mmaps); | |
813 | ||
814 | trace_vfio_region_finalize(region->vbasedev->name, region->nr); | |
815 | } | |
816 | ||
817 | void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled) | |
818 | { | |
819 | int i; | |
820 | ||
821 | if (!region->mem) { | |
822 | return; | |
823 | } | |
824 | ||
825 | for (i = 0; i < region->nr_mmaps; i++) { | |
826 | if (region->mmaps[i].mmap) { | |
827 | memory_region_set_enabled(®ion->mmaps[i].mem, enabled); | |
828 | } | |
829 | } | |
e2c7d025 | 830 | |
db0da029 AW |
831 | trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem), |
832 | enabled); | |
e2c7d025 EA |
833 | } |
834 | ||
835 | void vfio_reset_handler(void *opaque) | |
836 | { | |
837 | VFIOGroup *group; | |
838 | VFIODevice *vbasedev; | |
839 | ||
840 | QLIST_FOREACH(group, &vfio_group_list, next) { | |
841 | QLIST_FOREACH(vbasedev, &group->device_list, next) { | |
842 | vbasedev->ops->vfio_compute_needs_reset(vbasedev); | |
843 | } | |
844 | } | |
845 | ||
846 | QLIST_FOREACH(group, &vfio_group_list, next) { | |
847 | QLIST_FOREACH(vbasedev, &group->device_list, next) { | |
848 | if (vbasedev->needs_reset) { | |
849 | vbasedev->ops->vfio_hot_reset_multi(vbasedev); | |
850 | } | |
851 | } | |
852 | } | |
853 | } | |
854 | ||
855 | static void vfio_kvm_device_add_group(VFIOGroup *group) | |
856 | { | |
857 | #ifdef CONFIG_KVM | |
858 | struct kvm_device_attr attr = { | |
859 | .group = KVM_DEV_VFIO_GROUP, | |
860 | .attr = KVM_DEV_VFIO_GROUP_ADD, | |
861 | .addr = (uint64_t)(unsigned long)&group->fd, | |
862 | }; | |
863 | ||
864 | if (!kvm_enabled()) { | |
865 | return; | |
866 | } | |
867 | ||
868 | if (vfio_kvm_device_fd < 0) { | |
869 | struct kvm_create_device cd = { | |
870 | .type = KVM_DEV_TYPE_VFIO, | |
871 | }; | |
872 | ||
873 | if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) { | |
78e5b17f | 874 | error_report("Failed to create KVM VFIO device: %m"); |
e2c7d025 EA |
875 | return; |
876 | } | |
877 | ||
878 | vfio_kvm_device_fd = cd.fd; | |
879 | } | |
880 | ||
881 | if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { | |
882 | error_report("Failed to add group %d to KVM VFIO device: %m", | |
883 | group->groupid); | |
884 | } | |
885 | #endif | |
886 | } | |
887 | ||
888 | static void vfio_kvm_device_del_group(VFIOGroup *group) | |
889 | { | |
890 | #ifdef CONFIG_KVM | |
891 | struct kvm_device_attr attr = { | |
892 | .group = KVM_DEV_VFIO_GROUP, | |
893 | .attr = KVM_DEV_VFIO_GROUP_DEL, | |
894 | .addr = (uint64_t)(unsigned long)&group->fd, | |
895 | }; | |
896 | ||
897 | if (vfio_kvm_device_fd < 0) { | |
898 | return; | |
899 | } | |
900 | ||
901 | if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { | |
902 | error_report("Failed to remove group %d from KVM VFIO device: %m", | |
903 | group->groupid); | |
904 | } | |
905 | #endif | |
906 | } | |
907 | ||
908 | static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as) | |
909 | { | |
910 | VFIOAddressSpace *space; | |
911 | ||
912 | QLIST_FOREACH(space, &vfio_address_spaces, list) { | |
913 | if (space->as == as) { | |
914 | return space; | |
915 | } | |
916 | } | |
917 | ||
918 | /* No suitable VFIOAddressSpace, create a new one */ | |
919 | space = g_malloc0(sizeof(*space)); | |
920 | space->as = as; | |
921 | QLIST_INIT(&space->containers); | |
922 | ||
923 | QLIST_INSERT_HEAD(&vfio_address_spaces, space, list); | |
924 | ||
925 | return space; | |
926 | } | |
927 | ||
928 | static void vfio_put_address_space(VFIOAddressSpace *space) | |
929 | { | |
930 | if (QLIST_EMPTY(&space->containers)) { | |
931 | QLIST_REMOVE(space, list); | |
932 | g_free(space); | |
933 | } | |
934 | } | |
935 | ||
01905f58 EA |
936 | static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, |
937 | Error **errp) | |
e2c7d025 EA |
938 | { |
939 | VFIOContainer *container; | |
940 | int ret, fd; | |
941 | VFIOAddressSpace *space; | |
942 | ||
943 | space = vfio_get_address_space(as); | |
944 | ||
945 | QLIST_FOREACH(container, &space->containers, next) { | |
946 | if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) { | |
947 | group->container = container; | |
948 | QLIST_INSERT_HEAD(&container->group_list, group, container_next); | |
949 | return 0; | |
950 | } | |
951 | } | |
952 | ||
953 | fd = qemu_open("/dev/vfio/vfio", O_RDWR); | |
954 | if (fd < 0) { | |
01905f58 | 955 | error_setg_errno(errp, errno, "failed to open /dev/vfio/vfio"); |
e2c7d025 EA |
956 | ret = -errno; |
957 | goto put_space_exit; | |
958 | } | |
959 | ||
960 | ret = ioctl(fd, VFIO_GET_API_VERSION); | |
961 | if (ret != VFIO_API_VERSION) { | |
01905f58 EA |
962 | error_setg(errp, "supported vfio version: %d, " |
963 | "reported version: %d", VFIO_API_VERSION, ret); | |
e2c7d025 EA |
964 | ret = -EINVAL; |
965 | goto close_fd_exit; | |
966 | } | |
967 | ||
968 | container = g_malloc0(sizeof(*container)); | |
969 | container->space = space; | |
970 | container->fd = fd; | |
2e6e697e AW |
971 | if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) || |
972 | ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) { | |
973 | bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU); | |
7a140a57 | 974 | struct vfio_iommu_type1_info info; |
2e6e697e | 975 | |
e2c7d025 EA |
976 | ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd); |
977 | if (ret) { | |
01905f58 | 978 | error_setg_errno(errp, errno, "failed to set group container"); |
e2c7d025 EA |
979 | ret = -errno; |
980 | goto free_container_exit; | |
981 | } | |
982 | ||
318f67ce AK |
983 | container->iommu_type = v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU; |
984 | ret = ioctl(fd, VFIO_SET_IOMMU, container->iommu_type); | |
e2c7d025 | 985 | if (ret) { |
01905f58 | 986 | error_setg_errno(errp, errno, "failed to set iommu for container"); |
e2c7d025 EA |
987 | ret = -errno; |
988 | goto free_container_exit; | |
989 | } | |
3898aad3 DG |
990 | |
991 | /* | |
992 | * FIXME: This assumes that a Type1 IOMMU can map any 64-bit | |
993 | * IOVA whatsoever. That's not actually true, but the current | |
994 | * kernel interface doesn't tell us what it can map, and the | |
995 | * existing Type1 IOMMUs generally support any IOVA we're | |
996 | * going to actually try in practice. | |
997 | */ | |
7a140a57 DG |
998 | info.argsz = sizeof(info); |
999 | ret = ioctl(fd, VFIO_IOMMU_GET_INFO, &info); | |
1000 | /* Ignore errors */ | |
f4ec5e26 AK |
1001 | if (ret || !(info.flags & VFIO_IOMMU_INFO_PGSIZES)) { |
1002 | /* Assume 4k IOVA page size */ | |
1003 | info.iova_pgsizes = 4096; | |
7a140a57 | 1004 | } |
f4ec5e26 | 1005 | vfio_host_win_add(container, 0, (hwaddr)-1, info.iova_pgsizes); |
318f67ce AK |
1006 | } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU) || |
1007 | ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IOMMU)) { | |
3898aad3 | 1008 | struct vfio_iommu_spapr_tce_info info; |
318f67ce | 1009 | bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_v2_IOMMU); |
3898aad3 | 1010 | |
e2c7d025 EA |
1011 | ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd); |
1012 | if (ret) { | |
01905f58 | 1013 | error_setg_errno(errp, errno, "failed to set group container"); |
e2c7d025 EA |
1014 | ret = -errno; |
1015 | goto free_container_exit; | |
1016 | } | |
318f67ce AK |
1017 | container->iommu_type = |
1018 | v2 ? VFIO_SPAPR_TCE_v2_IOMMU : VFIO_SPAPR_TCE_IOMMU; | |
1019 | ret = ioctl(fd, VFIO_SET_IOMMU, container->iommu_type); | |
e2c7d025 | 1020 | if (ret) { |
01905f58 | 1021 | error_setg_errno(errp, errno, "failed to set iommu for container"); |
e2c7d025 EA |
1022 | ret = -errno; |
1023 | goto free_container_exit; | |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | * The host kernel code implementing VFIO_IOMMU_DISABLE is called | |
1028 | * when container fd is closed so we do not call it explicitly | |
1029 | * in this file. | |
1030 | */ | |
318f67ce AK |
1031 | if (!v2) { |
1032 | ret = ioctl(fd, VFIO_IOMMU_ENABLE); | |
1033 | if (ret) { | |
01905f58 | 1034 | error_setg_errno(errp, errno, "failed to enable container"); |
318f67ce AK |
1035 | ret = -errno; |
1036 | goto free_container_exit; | |
1037 | } | |
1038 | } else { | |
1039 | container->prereg_listener = vfio_prereg_listener; | |
1040 | ||
1041 | memory_listener_register(&container->prereg_listener, | |
1042 | &address_space_memory); | |
1043 | if (container->error) { | |
1044 | memory_listener_unregister(&container->prereg_listener); | |
01905f58 EA |
1045 | ret = container->error; |
1046 | error_setg(errp, | |
1047 | "RAM memory listener initialization failed for container"); | |
318f67ce AK |
1048 | goto free_container_exit; |
1049 | } | |
e2c7d025 | 1050 | } |
3898aad3 | 1051 | |
3898aad3 DG |
1052 | info.argsz = sizeof(info); |
1053 | ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info); | |
1054 | if (ret) { | |
01905f58 EA |
1055 | error_setg_errno(errp, errno, |
1056 | "VFIO_IOMMU_SPAPR_TCE_GET_INFO failed"); | |
3898aad3 | 1057 | ret = -errno; |
318f67ce AK |
1058 | if (v2) { |
1059 | memory_listener_unregister(&container->prereg_listener); | |
1060 | } | |
3898aad3 DG |
1061 | goto free_container_exit; |
1062 | } | |
7a140a57 | 1063 | |
2e4109de AK |
1064 | if (v2) { |
1065 | /* | |
1066 | * There is a default window in just created container. | |
1067 | * To make region_add/del simpler, we better remove this | |
1068 | * window now and let those iommu_listener callbacks | |
1069 | * create/remove them when needed. | |
1070 | */ | |
1071 | ret = vfio_spapr_remove_window(container, info.dma32_window_start); | |
1072 | if (ret) { | |
01905f58 EA |
1073 | error_setg_errno(errp, -ret, |
1074 | "failed to remove existing window"); | |
2e4109de AK |
1075 | goto free_container_exit; |
1076 | } | |
1077 | } else { | |
1078 | /* The default table uses 4K pages */ | |
1079 | vfio_host_win_add(container, info.dma32_window_start, | |
1080 | info.dma32_window_start + | |
1081 | info.dma32_window_size - 1, | |
1082 | 0x1000); | |
1083 | } | |
e2c7d025 | 1084 | } else { |
01905f58 | 1085 | error_setg(errp, "No available IOMMU models"); |
e2c7d025 EA |
1086 | ret = -EINVAL; |
1087 | goto free_container_exit; | |
1088 | } | |
1089 | ||
ee0bf0e5 DG |
1090 | container->listener = vfio_memory_listener; |
1091 | ||
1092 | memory_listener_register(&container->listener, container->space->as); | |
1093 | ||
1094 | if (container->error) { | |
1095 | ret = container->error; | |
01905f58 EA |
1096 | error_setg_errno(errp, -ret, |
1097 | "memory listener initialization failed for container"); | |
ee0bf0e5 DG |
1098 | goto listener_release_exit; |
1099 | } | |
1100 | ||
1101 | container->initialized = true; | |
1102 | ||
e2c7d025 EA |
1103 | QLIST_INIT(&container->group_list); |
1104 | QLIST_INSERT_HEAD(&space->containers, container, next); | |
1105 | ||
1106 | group->container = container; | |
1107 | QLIST_INSERT_HEAD(&container->group_list, group, container_next); | |
1108 | ||
1109 | return 0; | |
1110 | listener_release_exit: | |
1111 | vfio_listener_release(container); | |
1112 | ||
1113 | free_container_exit: | |
1114 | g_free(container); | |
1115 | ||
1116 | close_fd_exit: | |
1117 | close(fd); | |
1118 | ||
1119 | put_space_exit: | |
1120 | vfio_put_address_space(space); | |
1121 | ||
1122 | return ret; | |
1123 | } | |
1124 | ||
1125 | static void vfio_disconnect_container(VFIOGroup *group) | |
1126 | { | |
1127 | VFIOContainer *container = group->container; | |
1128 | ||
1129 | if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) { | |
1130 | error_report("vfio: error disconnecting group %d from container", | |
1131 | group->groupid); | |
1132 | } | |
1133 | ||
1134 | QLIST_REMOVE(group, container_next); | |
1135 | group->container = NULL; | |
1136 | ||
1137 | if (QLIST_EMPTY(&container->group_list)) { | |
1138 | VFIOAddressSpace *space = container->space; | |
f8d8a944 | 1139 | VFIOGuestIOMMU *giommu, *tmp; |
e2c7d025 | 1140 | |
ee0bf0e5 | 1141 | vfio_listener_release(container); |
e2c7d025 | 1142 | QLIST_REMOVE(container, next); |
f8d8a944 AK |
1143 | |
1144 | QLIST_FOREACH_SAFE(giommu, &container->giommu_list, giommu_next, tmp) { | |
d22d8956 | 1145 | memory_region_unregister_iommu_notifier(giommu->iommu, &giommu->n); |
f8d8a944 AK |
1146 | QLIST_REMOVE(giommu, giommu_next); |
1147 | g_free(giommu); | |
1148 | } | |
1149 | ||
e2c7d025 EA |
1150 | trace_vfio_disconnect_container(container->fd); |
1151 | close(container->fd); | |
1152 | g_free(container); | |
1153 | ||
1154 | vfio_put_address_space(space); | |
1155 | } | |
1156 | } | |
1157 | ||
1b808d5b | 1158 | VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp) |
e2c7d025 EA |
1159 | { |
1160 | VFIOGroup *group; | |
1161 | char path[32]; | |
1162 | struct vfio_group_status status = { .argsz = sizeof(status) }; | |
1163 | ||
1164 | QLIST_FOREACH(group, &vfio_group_list, next) { | |
1165 | if (group->groupid == groupid) { | |
1166 | /* Found it. Now is it already in the right context? */ | |
1167 | if (group->container->space->as == as) { | |
1168 | return group; | |
1169 | } else { | |
1b808d5b EA |
1170 | error_setg(errp, "group %d used in multiple address spaces", |
1171 | group->groupid); | |
e2c7d025 EA |
1172 | return NULL; |
1173 | } | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | group = g_malloc0(sizeof(*group)); | |
1178 | ||
1179 | snprintf(path, sizeof(path), "/dev/vfio/%d", groupid); | |
1180 | group->fd = qemu_open(path, O_RDWR); | |
1181 | if (group->fd < 0) { | |
1b808d5b | 1182 | error_setg_errno(errp, errno, "failed to open %s", path); |
e2c7d025 EA |
1183 | goto free_group_exit; |
1184 | } | |
1185 | ||
1186 | if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) { | |
1b808d5b | 1187 | error_setg_errno(errp, errno, "failed to get group %d status", groupid); |
e2c7d025 EA |
1188 | goto close_fd_exit; |
1189 | } | |
1190 | ||
1191 | if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) { | |
1b808d5b EA |
1192 | error_setg(errp, "group %d is not viable", groupid); |
1193 | error_append_hint(errp, | |
1194 | "Please ensure all devices within the iommu_group " | |
1195 | "are bound to their vfio bus driver.\n"); | |
e2c7d025 EA |
1196 | goto close_fd_exit; |
1197 | } | |
1198 | ||
1199 | group->groupid = groupid; | |
1200 | QLIST_INIT(&group->device_list); | |
1201 | ||
1b808d5b EA |
1202 | if (vfio_connect_container(group, as, errp)) { |
1203 | error_prepend(errp, "failed to setup container for group %d: ", | |
1204 | groupid); | |
e2c7d025 EA |
1205 | goto close_fd_exit; |
1206 | } | |
1207 | ||
1208 | if (QLIST_EMPTY(&vfio_group_list)) { | |
1209 | qemu_register_reset(vfio_reset_handler, NULL); | |
1210 | } | |
1211 | ||
1212 | QLIST_INSERT_HEAD(&vfio_group_list, group, next); | |
1213 | ||
1214 | vfio_kvm_device_add_group(group); | |
1215 | ||
1216 | return group; | |
1217 | ||
1218 | close_fd_exit: | |
1219 | close(group->fd); | |
1220 | ||
1221 | free_group_exit: | |
1222 | g_free(group); | |
1223 | ||
1224 | return NULL; | |
1225 | } | |
1226 | ||
1227 | void vfio_put_group(VFIOGroup *group) | |
1228 | { | |
77a10d04 | 1229 | if (!group || !QLIST_EMPTY(&group->device_list)) { |
e2c7d025 EA |
1230 | return; |
1231 | } | |
1232 | ||
1233 | vfio_kvm_device_del_group(group); | |
1234 | vfio_disconnect_container(group); | |
1235 | QLIST_REMOVE(group, next); | |
1236 | trace_vfio_put_group(group->fd); | |
1237 | close(group->fd); | |
1238 | g_free(group); | |
1239 | ||
1240 | if (QLIST_EMPTY(&vfio_group_list)) { | |
1241 | qemu_unregister_reset(vfio_reset_handler, NULL); | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | int vfio_get_device(VFIOGroup *group, const char *name, | |
59f7d674 | 1246 | VFIODevice *vbasedev, Error **errp) |
e2c7d025 EA |
1247 | { |
1248 | struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; | |
217e9fdc | 1249 | int ret, fd; |
e2c7d025 | 1250 | |
217e9fdc PB |
1251 | fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name); |
1252 | if (fd < 0) { | |
59f7d674 EA |
1253 | error_setg_errno(errp, errno, "error getting device from group %d", |
1254 | group->groupid); | |
1255 | error_append_hint(errp, | |
1256 | "Verify all devices in group %d are bound to vfio-<bus> " | |
1257 | "or pci-stub and not already in use\n", group->groupid); | |
217e9fdc | 1258 | return fd; |
e2c7d025 EA |
1259 | } |
1260 | ||
217e9fdc | 1261 | ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info); |
e2c7d025 | 1262 | if (ret) { |
59f7d674 | 1263 | error_setg_errno(errp, errno, "error getting device info"); |
217e9fdc PB |
1264 | close(fd); |
1265 | return ret; | |
e2c7d025 EA |
1266 | } |
1267 | ||
217e9fdc PB |
1268 | vbasedev->fd = fd; |
1269 | vbasedev->group = group; | |
1270 | QLIST_INSERT_HEAD(&group->device_list, vbasedev, next); | |
1271 | ||
e2c7d025 EA |
1272 | vbasedev->num_irqs = dev_info.num_irqs; |
1273 | vbasedev->num_regions = dev_info.num_regions; | |
1274 | vbasedev->flags = dev_info.flags; | |
1275 | ||
1276 | trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions, | |
1277 | dev_info.num_irqs); | |
1278 | ||
1279 | vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); | |
217e9fdc | 1280 | return 0; |
e2c7d025 EA |
1281 | } |
1282 | ||
1283 | void vfio_put_base_device(VFIODevice *vbasedev) | |
1284 | { | |
77a10d04 PB |
1285 | if (!vbasedev->group) { |
1286 | return; | |
1287 | } | |
e2c7d025 EA |
1288 | QLIST_REMOVE(vbasedev, next); |
1289 | vbasedev->group = NULL; | |
1290 | trace_vfio_put_base_device(vbasedev->fd); | |
1291 | close(vbasedev->fd); | |
1292 | } | |
1293 | ||
46900226 AW |
1294 | int vfio_get_region_info(VFIODevice *vbasedev, int index, |
1295 | struct vfio_region_info **info) | |
1296 | { | |
1297 | size_t argsz = sizeof(struct vfio_region_info); | |
1298 | ||
1299 | *info = g_malloc0(argsz); | |
1300 | ||
1301 | (*info)->index = index; | |
b53b0f69 | 1302 | retry: |
46900226 AW |
1303 | (*info)->argsz = argsz; |
1304 | ||
1305 | if (ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, *info)) { | |
1306 | g_free(*info); | |
e61a424f | 1307 | *info = NULL; |
46900226 AW |
1308 | return -errno; |
1309 | } | |
1310 | ||
b53b0f69 AW |
1311 | if ((*info)->argsz > argsz) { |
1312 | argsz = (*info)->argsz; | |
1313 | *info = g_realloc(*info, argsz); | |
1314 | ||
1315 | goto retry; | |
1316 | } | |
1317 | ||
46900226 AW |
1318 | return 0; |
1319 | } | |
1320 | ||
e61a424f AW |
1321 | int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, |
1322 | uint32_t subtype, struct vfio_region_info **info) | |
1323 | { | |
1324 | int i; | |
1325 | ||
1326 | for (i = 0; i < vbasedev->num_regions; i++) { | |
1327 | struct vfio_info_cap_header *hdr; | |
1328 | struct vfio_region_info_cap_type *cap_type; | |
1329 | ||
1330 | if (vfio_get_region_info(vbasedev, i, info)) { | |
1331 | continue; | |
1332 | } | |
1333 | ||
1334 | hdr = vfio_get_region_info_cap(*info, VFIO_REGION_INFO_CAP_TYPE); | |
1335 | if (!hdr) { | |
1336 | g_free(*info); | |
1337 | continue; | |
1338 | } | |
1339 | ||
1340 | cap_type = container_of(hdr, struct vfio_region_info_cap_type, header); | |
1341 | ||
1342 | trace_vfio_get_dev_region(vbasedev->name, i, | |
1343 | cap_type->type, cap_type->subtype); | |
1344 | ||
1345 | if (cap_type->type == type && cap_type->subtype == subtype) { | |
1346 | return 0; | |
1347 | } | |
1348 | ||
1349 | g_free(*info); | |
1350 | } | |
1351 | ||
1352 | *info = NULL; | |
1353 | return -ENODEV; | |
1354 | } | |
1355 | ||
3153119e DG |
1356 | /* |
1357 | * Interfaces for IBM EEH (Enhanced Error Handling) | |
1358 | */ | |
1359 | static bool vfio_eeh_container_ok(VFIOContainer *container) | |
1360 | { | |
1361 | /* | |
1362 | * As of 2016-03-04 (linux-4.5) the host kernel EEH/VFIO | |
1363 | * implementation is broken if there are multiple groups in a | |
1364 | * container. The hardware works in units of Partitionable | |
1365 | * Endpoints (== IOMMU groups) and the EEH operations naively | |
1366 | * iterate across all groups in the container, without any logic | |
1367 | * to make sure the groups have their state synchronized. For | |
1368 | * certain operations (ENABLE) that might be ok, until an error | |
1369 | * occurs, but for others (GET_STATE) it's clearly broken. | |
1370 | */ | |
1371 | ||
1372 | /* | |
1373 | * XXX Once fixed kernels exist, test for them here | |
1374 | */ | |
1375 | ||
1376 | if (QLIST_EMPTY(&container->group_list)) { | |
1377 | return false; | |
1378 | } | |
1379 | ||
1380 | if (QLIST_NEXT(QLIST_FIRST(&container->group_list), container_next)) { | |
1381 | return false; | |
1382 | } | |
1383 | ||
1384 | return true; | |
1385 | } | |
1386 | ||
1387 | static int vfio_eeh_container_op(VFIOContainer *container, uint32_t op) | |
1388 | { | |
1389 | struct vfio_eeh_pe_op pe_op = { | |
1390 | .argsz = sizeof(pe_op), | |
1391 | .op = op, | |
1392 | }; | |
1393 | int ret; | |
1394 | ||
1395 | if (!vfio_eeh_container_ok(container)) { | |
1396 | error_report("vfio/eeh: EEH_PE_OP 0x%x: " | |
1397 | "kernel requires a container with exactly one group", op); | |
1398 | return -EPERM; | |
1399 | } | |
1400 | ||
1401 | ret = ioctl(container->fd, VFIO_EEH_PE_OP, &pe_op); | |
1402 | if (ret < 0) { | |
1403 | error_report("vfio/eeh: EEH_PE_OP 0x%x failed: %m", op); | |
1404 | return -errno; | |
1405 | } | |
1406 | ||
d917e88d | 1407 | return ret; |
3153119e DG |
1408 | } |
1409 | ||
1410 | static VFIOContainer *vfio_eeh_as_container(AddressSpace *as) | |
1411 | { | |
1412 | VFIOAddressSpace *space = vfio_get_address_space(as); | |
1413 | VFIOContainer *container = NULL; | |
1414 | ||
1415 | if (QLIST_EMPTY(&space->containers)) { | |
1416 | /* No containers to act on */ | |
1417 | goto out; | |
1418 | } | |
1419 | ||
1420 | container = QLIST_FIRST(&space->containers); | |
1421 | ||
1422 | if (QLIST_NEXT(container, next)) { | |
1423 | /* We don't yet have logic to synchronize EEH state across | |
1424 | * multiple containers */ | |
1425 | container = NULL; | |
1426 | goto out; | |
1427 | } | |
1428 | ||
1429 | out: | |
1430 | vfio_put_address_space(space); | |
1431 | return container; | |
1432 | } | |
1433 | ||
1434 | bool vfio_eeh_as_ok(AddressSpace *as) | |
1435 | { | |
1436 | VFIOContainer *container = vfio_eeh_as_container(as); | |
1437 | ||
1438 | return (container != NULL) && vfio_eeh_container_ok(container); | |
1439 | } | |
1440 | ||
1441 | int vfio_eeh_as_op(AddressSpace *as, uint32_t op) | |
1442 | { | |
1443 | VFIOContainer *container = vfio_eeh_as_container(as); | |
1444 | ||
1445 | if (!container) { | |
1446 | return -ENODEV; | |
1447 | } | |
1448 | return vfio_eeh_container_op(container, op); | |
1449 | } |