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