1 /* 2 * DMA memory preregistration 3 * 4 * Authors: 5 * Alexey Kardashevskiy <aik@ozlabs.ru> 6 * 7 * This work is licensed under the terms of the GNU GPL, version 2. See 8 * the COPYING file in the top-level directory. 9 */ 10 11 #include "qemu/osdep.h" 12 #include "cpu.h" 13 #include <sys/ioctl.h> 14 #include <linux/vfio.h> 15 16 #include "hw/vfio/vfio-common.h" 17 #include "hw/hw.h" 18 #include "qemu/error-report.h" 19 #include "trace.h" 20 21 static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section) 22 { 23 if (memory_region_is_iommu(section->mr)) { 24 hw_error("Cannot possibly preregister IOMMU memory"); 25 } 26 27 return !memory_region_is_ram(section->mr) || 28 memory_region_is_ram_device(section->mr); 29 } 30 31 static void *vfio_prereg_gpa_to_vaddr(MemoryRegionSection *section, hwaddr gpa) 32 { 33 return memory_region_get_ram_ptr(section->mr) + 34 section->offset_within_region + 35 (gpa - section->offset_within_address_space); 36 } 37 38 static void vfio_prereg_listener_region_add(MemoryListener *listener, 39 MemoryRegionSection *section) 40 { 41 VFIOContainer *container = container_of(listener, VFIOContainer, 42 prereg_listener); 43 const hwaddr gpa = section->offset_within_address_space; 44 hwaddr end; 45 int ret; 46 hwaddr page_mask = qemu_real_host_page_mask; 47 struct vfio_iommu_spapr_register_memory reg = { 48 .argsz = sizeof(reg), 49 .flags = 0, 50 }; 51 52 if (vfio_prereg_listener_skipped_section(section)) { 53 trace_vfio_prereg_listener_region_add_skip( 54 section->offset_within_address_space, 55 section->offset_within_address_space + 56 int128_get64(int128_sub(section->size, int128_one()))); 57 return; 58 } 59 60 if (unlikely((section->offset_within_address_space & ~page_mask) || 61 (section->offset_within_region & ~page_mask) || 62 (int128_get64(section->size) & ~page_mask))) { 63 error_report("%s received unaligned region", __func__); 64 return; 65 } 66 67 end = section->offset_within_address_space + int128_get64(section->size); 68 if (gpa >= end) { 69 return; 70 } 71 72 memory_region_ref(section->mr); 73 74 reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa); 75 reg.size = end - gpa; 76 77 ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®); 78 trace_vfio_prereg_register(reg.vaddr, reg.size, ret ? -errno : 0); 79 if (ret) { 80 /* 81 * On the initfn path, store the first error in the container so we 82 * can gracefully fail. Runtime, there's not much we can do other 83 * than throw a hardware error. 84 */ 85 if (!container->initialized) { 86 if (!container->error) { 87 container->error = ret; 88 } 89 } else { 90 hw_error("vfio: Memory registering failed, unable to continue"); 91 } 92 } 93 } 94 95 static void vfio_prereg_listener_region_del(MemoryListener *listener, 96 MemoryRegionSection *section) 97 { 98 VFIOContainer *container = container_of(listener, VFIOContainer, 99 prereg_listener); 100 const hwaddr gpa = section->offset_within_address_space; 101 hwaddr end; 102 int ret; 103 hwaddr page_mask = qemu_real_host_page_mask; 104 struct vfio_iommu_spapr_register_memory reg = { 105 .argsz = sizeof(reg), 106 .flags = 0, 107 }; 108 109 if (vfio_prereg_listener_skipped_section(section)) { 110 trace_vfio_prereg_listener_region_del_skip( 111 section->offset_within_address_space, 112 section->offset_within_address_space + 113 int128_get64(int128_sub(section->size, int128_one()))); 114 return; 115 } 116 117 if (unlikely((section->offset_within_address_space & ~page_mask) || 118 (section->offset_within_region & ~page_mask) || 119 (int128_get64(section->size) & ~page_mask))) { 120 error_report("%s received unaligned region", __func__); 121 return; 122 } 123 124 end = section->offset_within_address_space + int128_get64(section->size); 125 if (gpa >= end) { 126 return; 127 } 128 129 reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa); 130 reg.size = end - gpa; 131 132 ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®); 133 trace_vfio_prereg_unregister(reg.vaddr, reg.size, ret ? -errno : 0); 134 } 135 136 const MemoryListener vfio_prereg_listener = { 137 .region_add = vfio_prereg_listener_region_add, 138 .region_del = vfio_prereg_listener_region_del, 139 }; 140 141 int vfio_spapr_create_window(VFIOContainer *container, 142 MemoryRegionSection *section, 143 hwaddr *pgsize) 144 { 145 int ret; 146 IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); 147 unsigned pagesize = memory_region_iommu_get_min_page_size(iommu_mr); 148 unsigned entries, pages; 149 struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) }; 150 151 /* 152 * FIXME: For VFIO iommu types which have KVM acceleration to 153 * avoid bouncing all map/unmaps through qemu this way, this 154 * would be the right place to wire that up (tell the KVM 155 * device emulation the VFIO iommu handles to use). 156 */ 157 create.window_size = int128_get64(section->size); 158 create.page_shift = ctz64(pagesize); 159 /* 160 * SPAPR host supports multilevel TCE tables, there is some 161 * heuristic to decide how many levels we want for our table: 162 * 0..64 = 1; 65..4096 = 2; 4097..262144 = 3; 262145.. = 4 163 */ 164 entries = create.window_size >> create.page_shift; 165 pages = MAX((entries * sizeof(uint64_t)) / getpagesize(), 1); 166 pages = MAX(pow2ceil(pages) - 1, 1); /* Round up */ 167 create.levels = ctz64(pages) / 6 + 1; 168 169 ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create); 170 if (ret) { 171 error_report("Failed to create a window, ret = %d (%m)", ret); 172 return -errno; 173 } 174 175 if (create.start_addr != section->offset_within_address_space) { 176 vfio_spapr_remove_window(container, create.start_addr); 177 178 error_report("Host doesn't support DMA window at %"HWADDR_PRIx", must be %"PRIx64, 179 section->offset_within_address_space, 180 (uint64_t)create.start_addr); 181 return -EINVAL; 182 } 183 trace_vfio_spapr_create_window(create.page_shift, 184 create.window_size, 185 create.start_addr); 186 *pgsize = pagesize; 187 188 return 0; 189 } 190 191 int vfio_spapr_remove_window(VFIOContainer *container, 192 hwaddr offset_within_address_space) 193 { 194 struct vfio_iommu_spapr_tce_remove remove = { 195 .argsz = sizeof(remove), 196 .start_addr = offset_within_address_space, 197 }; 198 int ret; 199 200 ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove); 201 if (ret) { 202 error_report("Failed to remove window at %"PRIx64, 203 (uint64_t)remove.start_addr); 204 return -errno; 205 } 206 207 trace_vfio_spapr_remove_window(offset_within_address_space); 208 209 return 0; 210 } 211