1318f67ceSAlexey Kardashevskiy /* 2318f67ceSAlexey Kardashevskiy * DMA memory preregistration 3318f67ceSAlexey Kardashevskiy * 4318f67ceSAlexey Kardashevskiy * Authors: 5318f67ceSAlexey Kardashevskiy * Alexey Kardashevskiy <aik@ozlabs.ru> 6318f67ceSAlexey Kardashevskiy * 7318f67ceSAlexey Kardashevskiy * This work is licensed under the terms of the GNU GPL, version 2. See 8318f67ceSAlexey Kardashevskiy * the COPYING file in the top-level directory. 9318f67ceSAlexey Kardashevskiy */ 10318f67ceSAlexey Kardashevskiy 11318f67ceSAlexey Kardashevskiy #include "qemu/osdep.h" 12318f67ceSAlexey Kardashevskiy #include <sys/ioctl.h> 13318f67ceSAlexey Kardashevskiy #include <linux/vfio.h> 14521c8f4eSZhenzhong Duan #ifdef CONFIG_KVM 15521c8f4eSZhenzhong Duan #include <linux/kvm.h> 16521c8f4eSZhenzhong Duan #endif 17521c8f4eSZhenzhong Duan #include "sysemu/kvm.h" 18770c3b6eSZhenzhong Duan #include "exec/address-spaces.h" 19318f67ceSAlexey Kardashevskiy 20318f67ceSAlexey Kardashevskiy #include "hw/vfio/vfio-common.h" 21318f67ceSAlexey Kardashevskiy #include "hw/hw.h" 22c26bc185SAlexey Kardashevskiy #include "exec/ram_addr.h" 23318f67ceSAlexey Kardashevskiy #include "qemu/error-report.h" 24d7d87836SEric Auger #include "qapi/error.h" 25318f67ceSAlexey Kardashevskiy #include "trace.h" 26318f67ceSAlexey Kardashevskiy 279b7d38bfSZhenzhong Duan typedef struct VFIOSpaprContainer { 289b7d38bfSZhenzhong Duan VFIOContainer container; 296ad359ecSZhenzhong Duan MemoryListener prereg_listener; 30dbb9d0c9SZhenzhong Duan QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; 319b7d38bfSZhenzhong Duan } VFIOSpaprContainer; 329b7d38bfSZhenzhong Duan 33*504d297eSCédric Le Goater OBJECT_DECLARE_SIMPLE_TYPE(VFIOSpaprContainer, VFIO_IOMMU_SPAPR); 34*504d297eSCédric Le Goater 35318f67ceSAlexey Kardashevskiy static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section) 36318f67ceSAlexey Kardashevskiy { 37318f67ceSAlexey Kardashevskiy if (memory_region_is_iommu(section->mr)) { 38318f67ceSAlexey Kardashevskiy hw_error("Cannot possibly preregister IOMMU memory"); 39318f67ceSAlexey Kardashevskiy } 40318f67ceSAlexey Kardashevskiy 41318f67ceSAlexey Kardashevskiy return !memory_region_is_ram(section->mr) || 4221e00fa5SAlex Williamson memory_region_is_ram_device(section->mr); 43318f67ceSAlexey Kardashevskiy } 44318f67ceSAlexey Kardashevskiy 45318f67ceSAlexey Kardashevskiy static void *vfio_prereg_gpa_to_vaddr(MemoryRegionSection *section, hwaddr gpa) 46318f67ceSAlexey Kardashevskiy { 47318f67ceSAlexey Kardashevskiy return memory_region_get_ram_ptr(section->mr) + 48318f67ceSAlexey Kardashevskiy section->offset_within_region + 49318f67ceSAlexey Kardashevskiy (gpa - section->offset_within_address_space); 50318f67ceSAlexey Kardashevskiy } 51318f67ceSAlexey Kardashevskiy 52318f67ceSAlexey Kardashevskiy static void vfio_prereg_listener_region_add(MemoryListener *listener, 53318f67ceSAlexey Kardashevskiy MemoryRegionSection *section) 54318f67ceSAlexey Kardashevskiy { 556ad359ecSZhenzhong Duan VFIOSpaprContainer *scontainer = container_of(listener, VFIOSpaprContainer, 56318f67ceSAlexey Kardashevskiy prereg_listener); 576ad359ecSZhenzhong Duan VFIOContainer *container = &scontainer->container; 58c7b313d3SEric Auger VFIOContainerBase *bcontainer = &container->bcontainer; 59318f67ceSAlexey Kardashevskiy const hwaddr gpa = section->offset_within_address_space; 60318f67ceSAlexey Kardashevskiy hwaddr end; 61318f67ceSAlexey Kardashevskiy int ret; 628e3b0cbbSMarc-André Lureau hwaddr page_mask = qemu_real_host_page_mask(); 63318f67ceSAlexey Kardashevskiy struct vfio_iommu_spapr_register_memory reg = { 64318f67ceSAlexey Kardashevskiy .argsz = sizeof(reg), 65318f67ceSAlexey Kardashevskiy .flags = 0, 66318f67ceSAlexey Kardashevskiy }; 67318f67ceSAlexey Kardashevskiy 68318f67ceSAlexey Kardashevskiy if (vfio_prereg_listener_skipped_section(section)) { 69318f67ceSAlexey Kardashevskiy trace_vfio_prereg_listener_region_add_skip( 70318f67ceSAlexey Kardashevskiy section->offset_within_address_space, 71318f67ceSAlexey Kardashevskiy section->offset_within_address_space + 72318f67ceSAlexey Kardashevskiy int128_get64(int128_sub(section->size, int128_one()))); 73318f67ceSAlexey Kardashevskiy return; 74318f67ceSAlexey Kardashevskiy } 75318f67ceSAlexey Kardashevskiy 76318f67ceSAlexey Kardashevskiy if (unlikely((section->offset_within_address_space & ~page_mask) || 77318f67ceSAlexey Kardashevskiy (section->offset_within_region & ~page_mask) || 78318f67ceSAlexey Kardashevskiy (int128_get64(section->size) & ~page_mask))) { 79318f67ceSAlexey Kardashevskiy error_report("%s received unaligned region", __func__); 80318f67ceSAlexey Kardashevskiy return; 81318f67ceSAlexey Kardashevskiy } 82318f67ceSAlexey Kardashevskiy 83318f67ceSAlexey Kardashevskiy end = section->offset_within_address_space + int128_get64(section->size); 84318f67ceSAlexey Kardashevskiy if (gpa >= end) { 85318f67ceSAlexey Kardashevskiy return; 86318f67ceSAlexey Kardashevskiy } 87318f67ceSAlexey Kardashevskiy 88318f67ceSAlexey Kardashevskiy memory_region_ref(section->mr); 89318f67ceSAlexey Kardashevskiy 90318f67ceSAlexey Kardashevskiy reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa); 91318f67ceSAlexey Kardashevskiy reg.size = end - gpa; 92318f67ceSAlexey Kardashevskiy 93318f67ceSAlexey Kardashevskiy ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®); 94318f67ceSAlexey Kardashevskiy trace_vfio_prereg_register(reg.vaddr, reg.size, ret ? -errno : 0); 95318f67ceSAlexey Kardashevskiy if (ret) { 96318f67ceSAlexey Kardashevskiy /* 97318f67ceSAlexey Kardashevskiy * On the initfn path, store the first error in the container so we 98318f67ceSAlexey Kardashevskiy * can gracefully fail. Runtime, there's not much we can do other 99318f67ceSAlexey Kardashevskiy * than throw a hardware error. 100318f67ceSAlexey Kardashevskiy */ 101c7b313d3SEric Auger if (!bcontainer->initialized) { 102c7b313d3SEric Auger if (!bcontainer->error) { 103c7b313d3SEric Auger error_setg_errno(&bcontainer->error, -ret, 104d7d87836SEric Auger "Memory registering failed"); 105318f67ceSAlexey Kardashevskiy } 106318f67ceSAlexey Kardashevskiy } else { 107318f67ceSAlexey Kardashevskiy hw_error("vfio: Memory registering failed, unable to continue"); 108318f67ceSAlexey Kardashevskiy } 109318f67ceSAlexey Kardashevskiy } 110318f67ceSAlexey Kardashevskiy } 111318f67ceSAlexey Kardashevskiy 112318f67ceSAlexey Kardashevskiy static void vfio_prereg_listener_region_del(MemoryListener *listener, 113318f67ceSAlexey Kardashevskiy MemoryRegionSection *section) 114318f67ceSAlexey Kardashevskiy { 1156ad359ecSZhenzhong Duan VFIOSpaprContainer *scontainer = container_of(listener, VFIOSpaprContainer, 116318f67ceSAlexey Kardashevskiy prereg_listener); 1176ad359ecSZhenzhong Duan VFIOContainer *container = &scontainer->container; 118318f67ceSAlexey Kardashevskiy const hwaddr gpa = section->offset_within_address_space; 119318f67ceSAlexey Kardashevskiy hwaddr end; 120318f67ceSAlexey Kardashevskiy int ret; 1218e3b0cbbSMarc-André Lureau hwaddr page_mask = qemu_real_host_page_mask(); 122318f67ceSAlexey Kardashevskiy struct vfio_iommu_spapr_register_memory reg = { 123318f67ceSAlexey Kardashevskiy .argsz = sizeof(reg), 124318f67ceSAlexey Kardashevskiy .flags = 0, 125318f67ceSAlexey Kardashevskiy }; 126318f67ceSAlexey Kardashevskiy 127318f67ceSAlexey Kardashevskiy if (vfio_prereg_listener_skipped_section(section)) { 128318f67ceSAlexey Kardashevskiy trace_vfio_prereg_listener_region_del_skip( 129318f67ceSAlexey Kardashevskiy section->offset_within_address_space, 130318f67ceSAlexey Kardashevskiy section->offset_within_address_space + 131318f67ceSAlexey Kardashevskiy int128_get64(int128_sub(section->size, int128_one()))); 132318f67ceSAlexey Kardashevskiy return; 133318f67ceSAlexey Kardashevskiy } 134318f67ceSAlexey Kardashevskiy 135318f67ceSAlexey Kardashevskiy if (unlikely((section->offset_within_address_space & ~page_mask) || 136318f67ceSAlexey Kardashevskiy (section->offset_within_region & ~page_mask) || 137318f67ceSAlexey Kardashevskiy (int128_get64(section->size) & ~page_mask))) { 138318f67ceSAlexey Kardashevskiy error_report("%s received unaligned region", __func__); 139318f67ceSAlexey Kardashevskiy return; 140318f67ceSAlexey Kardashevskiy } 141318f67ceSAlexey Kardashevskiy 142318f67ceSAlexey Kardashevskiy end = section->offset_within_address_space + int128_get64(section->size); 143318f67ceSAlexey Kardashevskiy if (gpa >= end) { 144318f67ceSAlexey Kardashevskiy return; 145318f67ceSAlexey Kardashevskiy } 146318f67ceSAlexey Kardashevskiy 147318f67ceSAlexey Kardashevskiy reg.vaddr = (uintptr_t) vfio_prereg_gpa_to_vaddr(section, gpa); 148318f67ceSAlexey Kardashevskiy reg.size = end - gpa; 149318f67ceSAlexey Kardashevskiy 150318f67ceSAlexey Kardashevskiy ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_UNREGISTER_MEMORY, ®); 151318f67ceSAlexey Kardashevskiy trace_vfio_prereg_unregister(reg.vaddr, reg.size, ret ? -errno : 0); 152318f67ceSAlexey Kardashevskiy } 153318f67ceSAlexey Kardashevskiy 154770c3b6eSZhenzhong Duan static const MemoryListener vfio_prereg_listener = { 155142518bdSPeter Xu .name = "vfio-pre-reg", 156318f67ceSAlexey Kardashevskiy .region_add = vfio_prereg_listener_region_add, 157318f67ceSAlexey Kardashevskiy .region_del = vfio_prereg_listener_region_del, 158318f67ceSAlexey Kardashevskiy }; 1592e4109deSAlexey Kardashevskiy 160dbb9d0c9SZhenzhong Duan static void vfio_host_win_add(VFIOSpaprContainer *scontainer, hwaddr min_iova, 161a2347c60SZhenzhong Duan hwaddr max_iova, uint64_t iova_pgsizes) 162a2347c60SZhenzhong Duan { 163a2347c60SZhenzhong Duan VFIOHostDMAWindow *hostwin; 164a2347c60SZhenzhong Duan 165dbb9d0c9SZhenzhong Duan QLIST_FOREACH(hostwin, &scontainer->hostwin_list, hostwin_next) { 166a2347c60SZhenzhong Duan if (ranges_overlap(hostwin->min_iova, 167a2347c60SZhenzhong Duan hostwin->max_iova - hostwin->min_iova + 1, 168a2347c60SZhenzhong Duan min_iova, 169a2347c60SZhenzhong Duan max_iova - min_iova + 1)) { 170a2347c60SZhenzhong Duan hw_error("%s: Overlapped IOMMU are not enabled", __func__); 171a2347c60SZhenzhong Duan } 172a2347c60SZhenzhong Duan } 173a2347c60SZhenzhong Duan 174a2347c60SZhenzhong Duan hostwin = g_malloc0(sizeof(*hostwin)); 175a2347c60SZhenzhong Duan 176a2347c60SZhenzhong Duan hostwin->min_iova = min_iova; 177a2347c60SZhenzhong Duan hostwin->max_iova = max_iova; 178a2347c60SZhenzhong Duan hostwin->iova_pgsizes = iova_pgsizes; 179dbb9d0c9SZhenzhong Duan QLIST_INSERT_HEAD(&scontainer->hostwin_list, hostwin, hostwin_next); 180a2347c60SZhenzhong Duan } 181a2347c60SZhenzhong Duan 182dbb9d0c9SZhenzhong Duan static int vfio_host_win_del(VFIOSpaprContainer *scontainer, 183a2347c60SZhenzhong Duan hwaddr min_iova, hwaddr max_iova) 184a2347c60SZhenzhong Duan { 185a2347c60SZhenzhong Duan VFIOHostDMAWindow *hostwin; 186a2347c60SZhenzhong Duan 187dbb9d0c9SZhenzhong Duan QLIST_FOREACH(hostwin, &scontainer->hostwin_list, hostwin_next) { 188a2347c60SZhenzhong Duan if (hostwin->min_iova == min_iova && hostwin->max_iova == max_iova) { 189a2347c60SZhenzhong Duan QLIST_REMOVE(hostwin, hostwin_next); 190a2347c60SZhenzhong Duan g_free(hostwin); 191a2347c60SZhenzhong Duan return 0; 192a2347c60SZhenzhong Duan } 193a2347c60SZhenzhong Duan } 194a2347c60SZhenzhong Duan 195a2347c60SZhenzhong Duan return -1; 196a2347c60SZhenzhong Duan } 197a2347c60SZhenzhong Duan 198dbb9d0c9SZhenzhong Duan static VFIOHostDMAWindow *vfio_find_hostwin(VFIOSpaprContainer *container, 199a2347c60SZhenzhong Duan hwaddr iova, hwaddr end) 200a2347c60SZhenzhong Duan { 201a2347c60SZhenzhong Duan VFIOHostDMAWindow *hostwin; 202a2347c60SZhenzhong Duan bool hostwin_found = false; 203a2347c60SZhenzhong Duan 204a2347c60SZhenzhong Duan QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { 205a2347c60SZhenzhong Duan if (hostwin->min_iova <= iova && end <= hostwin->max_iova) { 206a2347c60SZhenzhong Duan hostwin_found = true; 207a2347c60SZhenzhong Duan break; 208a2347c60SZhenzhong Duan } 209a2347c60SZhenzhong Duan } 210a2347c60SZhenzhong Duan 211a2347c60SZhenzhong Duan return hostwin_found ? hostwin : NULL; 212a2347c60SZhenzhong Duan } 213a2347c60SZhenzhong Duan 214a17879f0SZhenzhong Duan static int vfio_spapr_remove_window(VFIOContainer *container, 215a17879f0SZhenzhong Duan hwaddr offset_within_address_space) 216a17879f0SZhenzhong Duan { 217a17879f0SZhenzhong Duan struct vfio_iommu_spapr_tce_remove remove = { 218a17879f0SZhenzhong Duan .argsz = sizeof(remove), 219a17879f0SZhenzhong Duan .start_addr = offset_within_address_space, 220a17879f0SZhenzhong Duan }; 221a17879f0SZhenzhong Duan int ret; 222a17879f0SZhenzhong Duan 223a17879f0SZhenzhong Duan ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove); 224a17879f0SZhenzhong Duan if (ret) { 225a17879f0SZhenzhong Duan error_report("Failed to remove window at %"PRIx64, 226a17879f0SZhenzhong Duan (uint64_t)remove.start_addr); 227a17879f0SZhenzhong Duan return -errno; 228a17879f0SZhenzhong Duan } 229a17879f0SZhenzhong Duan 230a17879f0SZhenzhong Duan trace_vfio_spapr_remove_window(offset_within_address_space); 231a17879f0SZhenzhong Duan 232a17879f0SZhenzhong Duan return 0; 233a17879f0SZhenzhong Duan } 234a17879f0SZhenzhong Duan 235a17879f0SZhenzhong Duan static int vfio_spapr_create_window(VFIOContainer *container, 2362e4109deSAlexey Kardashevskiy MemoryRegionSection *section, 2372e4109deSAlexey Kardashevskiy hwaddr *pgsize) 2382e4109deSAlexey Kardashevskiy { 23916107998SAlexey Kardashevskiy int ret = 0; 2407ab1cb74SEric Auger VFIOContainerBase *bcontainer = &container->bcontainer; 2413df9d748SAlexey Kardashevskiy IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); 24279178eddSAlexey Kardashevskiy uint64_t pagesize = memory_region_iommu_get_min_page_size(iommu_mr), pgmask; 24316107998SAlexey Kardashevskiy unsigned entries, bits_total, bits_per_level, max_levels; 2442e4109deSAlexey Kardashevskiy struct vfio_iommu_spapr_tce_create create = { .argsz = sizeof(create) }; 245905b7ee4SDavid Hildenbrand long rampagesize = qemu_minrampagesize(); 246c26bc185SAlexey Kardashevskiy 247c26bc185SAlexey Kardashevskiy /* 248c26bc185SAlexey Kardashevskiy * The host might not support the guest supported IOMMU page size, 249c26bc185SAlexey Kardashevskiy * so we will use smaller physical IOMMU pages to back them. 250c26bc185SAlexey Kardashevskiy */ 2513cdd801bSAlexey Kardashevskiy if (pagesize > rampagesize) { 2523cdd801bSAlexey Kardashevskiy pagesize = rampagesize; 253c26bc185SAlexey Kardashevskiy } 2547ab1cb74SEric Auger pgmask = bcontainer->pgsizes & (pagesize | (pagesize - 1)); 25579178eddSAlexey Kardashevskiy pagesize = pgmask ? (1ULL << (63 - clz64(pgmask))) : 0; 256c26bc185SAlexey Kardashevskiy if (!pagesize) { 257c26bc185SAlexey Kardashevskiy error_report("Host doesn't support page size 0x%"PRIx64 258c26bc185SAlexey Kardashevskiy ", the supported mask is 0x%lx", 259c26bc185SAlexey Kardashevskiy memory_region_iommu_get_min_page_size(iommu_mr), 2607ab1cb74SEric Auger bcontainer->pgsizes); 261c26bc185SAlexey Kardashevskiy return -EINVAL; 262c26bc185SAlexey Kardashevskiy } 2632e4109deSAlexey Kardashevskiy 2642e4109deSAlexey Kardashevskiy /* 2652e4109deSAlexey Kardashevskiy * FIXME: For VFIO iommu types which have KVM acceleration to 2662e4109deSAlexey Kardashevskiy * avoid bouncing all map/unmaps through qemu this way, this 2672e4109deSAlexey Kardashevskiy * would be the right place to wire that up (tell the KVM 2682e4109deSAlexey Kardashevskiy * device emulation the VFIO iommu handles to use). 2692e4109deSAlexey Kardashevskiy */ 2702e4109deSAlexey Kardashevskiy create.window_size = int128_get64(section->size); 2712e4109deSAlexey Kardashevskiy create.page_shift = ctz64(pagesize); 2722e4109deSAlexey Kardashevskiy /* 27316107998SAlexey Kardashevskiy * SPAPR host supports multilevel TCE tables. We try to guess optimal 27416107998SAlexey Kardashevskiy * levels number and if this fails (for example due to the host memory 27516107998SAlexey Kardashevskiy * fragmentation), we increase levels. The DMA address structure is: 27616107998SAlexey Kardashevskiy * rrrrrrrr rxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx iiiiiiii 27716107998SAlexey Kardashevskiy * where: 27816107998SAlexey Kardashevskiy * r = reserved (bits >= 55 are reserved in the existing hardware) 27916107998SAlexey Kardashevskiy * i = IOMMU page offset (64K in this example) 28016107998SAlexey Kardashevskiy * x = bits to index a TCE which can be split to equal chunks to index 28116107998SAlexey Kardashevskiy * within the level. 28216107998SAlexey Kardashevskiy * The aim is to split "x" to smaller possible number of levels. 2832e4109deSAlexey Kardashevskiy */ 2842e4109deSAlexey Kardashevskiy entries = create.window_size >> create.page_shift; 28516107998SAlexey Kardashevskiy /* bits_total is number of "x" needed */ 28616107998SAlexey Kardashevskiy bits_total = ctz64(entries * sizeof(uint64_t)); 28716107998SAlexey Kardashevskiy /* 28816107998SAlexey Kardashevskiy * bits_per_level is a safe guess of how much we can allocate per level: 28916107998SAlexey Kardashevskiy * 8 is the current minimum for CONFIG_FORCE_MAX_ZONEORDER and MAX_ORDER 29016107998SAlexey Kardashevskiy * is usually bigger than that. 291038adc2fSWei Yang * Below we look at qemu_real_host_page_size as TCEs are allocated from 292038adc2fSWei Yang * system pages. 29316107998SAlexey Kardashevskiy */ 2948e3b0cbbSMarc-André Lureau bits_per_level = ctz64(qemu_real_host_page_size()) + 8; 29516107998SAlexey Kardashevskiy create.levels = bits_total / bits_per_level; 29616107998SAlexey Kardashevskiy if (bits_total % bits_per_level) { 29716107998SAlexey Kardashevskiy ++create.levels; 29816107998SAlexey Kardashevskiy } 2998e3b0cbbSMarc-André Lureau max_levels = (64 - create.page_shift) / ctz64(qemu_real_host_page_size()); 30016107998SAlexey Kardashevskiy for ( ; create.levels <= max_levels; ++create.levels) { 3012e4109deSAlexey Kardashevskiy ret = ioctl(container->fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create); 30216107998SAlexey Kardashevskiy if (!ret) { 30316107998SAlexey Kardashevskiy break; 30416107998SAlexey Kardashevskiy } 30516107998SAlexey Kardashevskiy } 3062e4109deSAlexey Kardashevskiy if (ret) { 3072e4109deSAlexey Kardashevskiy error_report("Failed to create a window, ret = %d (%m)", ret); 3082e4109deSAlexey Kardashevskiy return -errno; 3092e4109deSAlexey Kardashevskiy } 3102e4109deSAlexey Kardashevskiy 3112e4109deSAlexey Kardashevskiy if (create.start_addr != section->offset_within_address_space) { 3122e4109deSAlexey Kardashevskiy vfio_spapr_remove_window(container, create.start_addr); 3132e4109deSAlexey Kardashevskiy 3142e4109deSAlexey Kardashevskiy error_report("Host doesn't support DMA window at %"HWADDR_PRIx", must be %"PRIx64, 3152e4109deSAlexey Kardashevskiy section->offset_within_address_space, 3162e4109deSAlexey Kardashevskiy (uint64_t)create.start_addr); 3172e4109deSAlexey Kardashevskiy return -EINVAL; 3182e4109deSAlexey Kardashevskiy } 3192e4109deSAlexey Kardashevskiy trace_vfio_spapr_create_window(create.page_shift, 32016107998SAlexey Kardashevskiy create.levels, 3212e4109deSAlexey Kardashevskiy create.window_size, 3222e4109deSAlexey Kardashevskiy create.start_addr); 3232e4109deSAlexey Kardashevskiy *pgsize = pagesize; 3242e4109deSAlexey Kardashevskiy 3252e4109deSAlexey Kardashevskiy return 0; 3262e4109deSAlexey Kardashevskiy } 3272e4109deSAlexey Kardashevskiy 32833e4c22fSZhenzhong Duan static bool 329233309e8SZhenzhong Duan vfio_spapr_container_add_section_window(VFIOContainerBase *bcontainer, 330521c8f4eSZhenzhong Duan MemoryRegionSection *section, 331521c8f4eSZhenzhong Duan Error **errp) 332521c8f4eSZhenzhong Duan { 333233309e8SZhenzhong Duan VFIOContainer *container = container_of(bcontainer, VFIOContainer, 334233309e8SZhenzhong Duan bcontainer); 335dbb9d0c9SZhenzhong Duan VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer, 336dbb9d0c9SZhenzhong Duan container); 337521c8f4eSZhenzhong Duan VFIOHostDMAWindow *hostwin; 338521c8f4eSZhenzhong Duan hwaddr pgsize = 0; 339521c8f4eSZhenzhong Duan int ret; 340521c8f4eSZhenzhong Duan 341a2347c60SZhenzhong Duan /* 342a2347c60SZhenzhong Duan * VFIO_SPAPR_TCE_IOMMU supports a single host window between 343a2347c60SZhenzhong Duan * [dma32_window_start, dma32_window_size), we need to ensure 344a2347c60SZhenzhong Duan * the section fall in this range. 345a2347c60SZhenzhong Duan */ 346a2347c60SZhenzhong Duan if (container->iommu_type == VFIO_SPAPR_TCE_IOMMU) { 347a2347c60SZhenzhong Duan hwaddr iova, end; 348a2347c60SZhenzhong Duan 349a2347c60SZhenzhong Duan iova = section->offset_within_address_space; 350a2347c60SZhenzhong Duan end = iova + int128_get64(section->size) - 1; 351a2347c60SZhenzhong Duan 352dbb9d0c9SZhenzhong Duan if (!vfio_find_hostwin(scontainer, iova, end)) { 353a2347c60SZhenzhong Duan error_setg(errp, "Container %p can't map guest IOVA region" 354a2347c60SZhenzhong Duan " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx, container, 355a2347c60SZhenzhong Duan iova, end); 35633e4c22fSZhenzhong Duan return false; 357a2347c60SZhenzhong Duan } 35833e4c22fSZhenzhong Duan return true; 359a2347c60SZhenzhong Duan } 360a2347c60SZhenzhong Duan 361521c8f4eSZhenzhong Duan if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) { 36233e4c22fSZhenzhong Duan return true; 363521c8f4eSZhenzhong Duan } 364521c8f4eSZhenzhong Duan 365521c8f4eSZhenzhong Duan /* For now intersections are not allowed, we may relax this later */ 366dbb9d0c9SZhenzhong Duan QLIST_FOREACH(hostwin, &scontainer->hostwin_list, hostwin_next) { 367521c8f4eSZhenzhong Duan if (ranges_overlap(hostwin->min_iova, 368521c8f4eSZhenzhong Duan hostwin->max_iova - hostwin->min_iova + 1, 369521c8f4eSZhenzhong Duan section->offset_within_address_space, 370521c8f4eSZhenzhong Duan int128_get64(section->size))) { 371521c8f4eSZhenzhong Duan error_setg(errp, 372521c8f4eSZhenzhong Duan "region [0x%"PRIx64",0x%"PRIx64"] overlaps with existing" 373521c8f4eSZhenzhong Duan "host DMA window [0x%"PRIx64",0x%"PRIx64"]", 374521c8f4eSZhenzhong Duan section->offset_within_address_space, 375521c8f4eSZhenzhong Duan section->offset_within_address_space + 376521c8f4eSZhenzhong Duan int128_get64(section->size) - 1, 377521c8f4eSZhenzhong Duan hostwin->min_iova, hostwin->max_iova); 37833e4c22fSZhenzhong Duan return false; 379521c8f4eSZhenzhong Duan } 380521c8f4eSZhenzhong Duan } 381521c8f4eSZhenzhong Duan 382521c8f4eSZhenzhong Duan ret = vfio_spapr_create_window(container, section, &pgsize); 383521c8f4eSZhenzhong Duan if (ret) { 384521c8f4eSZhenzhong Duan error_setg_errno(errp, -ret, "Failed to create SPAPR window"); 38533e4c22fSZhenzhong Duan return false; 386521c8f4eSZhenzhong Duan } 387521c8f4eSZhenzhong Duan 388dbb9d0c9SZhenzhong Duan vfio_host_win_add(scontainer, section->offset_within_address_space, 389521c8f4eSZhenzhong Duan section->offset_within_address_space + 390521c8f4eSZhenzhong Duan int128_get64(section->size) - 1, pgsize); 391521c8f4eSZhenzhong Duan #ifdef CONFIG_KVM 392521c8f4eSZhenzhong Duan if (kvm_enabled()) { 393521c8f4eSZhenzhong Duan VFIOGroup *group; 394521c8f4eSZhenzhong Duan IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); 395521c8f4eSZhenzhong Duan struct kvm_vfio_spapr_tce param; 396521c8f4eSZhenzhong Duan struct kvm_device_attr attr = { 397521c8f4eSZhenzhong Duan .group = KVM_DEV_VFIO_GROUP, 398521c8f4eSZhenzhong Duan .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE, 399521c8f4eSZhenzhong Duan .addr = (uint64_t)(unsigned long)¶m, 400521c8f4eSZhenzhong Duan }; 401521c8f4eSZhenzhong Duan 402521c8f4eSZhenzhong Duan if (!memory_region_iommu_get_attr(iommu_mr, IOMMU_ATTR_SPAPR_TCE_FD, 403521c8f4eSZhenzhong Duan ¶m.tablefd)) { 404521c8f4eSZhenzhong Duan QLIST_FOREACH(group, &container->group_list, container_next) { 405521c8f4eSZhenzhong Duan param.groupfd = group->fd; 406521c8f4eSZhenzhong Duan if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { 407521c8f4eSZhenzhong Duan error_setg_errno(errp, errno, 408521c8f4eSZhenzhong Duan "vfio: failed GROUP_SET_SPAPR_TCE for " 409521c8f4eSZhenzhong Duan "KVM VFIO device %d and group fd %d", 410521c8f4eSZhenzhong Duan param.tablefd, param.groupfd); 41133e4c22fSZhenzhong Duan return false; 412521c8f4eSZhenzhong Duan } 413521c8f4eSZhenzhong Duan trace_vfio_spapr_group_attach(param.groupfd, param.tablefd); 414521c8f4eSZhenzhong Duan } 415521c8f4eSZhenzhong Duan } 416521c8f4eSZhenzhong Duan } 417521c8f4eSZhenzhong Duan #endif 41833e4c22fSZhenzhong Duan return true; 419521c8f4eSZhenzhong Duan } 420521c8f4eSZhenzhong Duan 421233309e8SZhenzhong Duan static void 422233309e8SZhenzhong Duan vfio_spapr_container_del_section_window(VFIOContainerBase *bcontainer, 423521c8f4eSZhenzhong Duan MemoryRegionSection *section) 424521c8f4eSZhenzhong Duan { 425233309e8SZhenzhong Duan VFIOContainer *container = container_of(bcontainer, VFIOContainer, 426233309e8SZhenzhong Duan bcontainer); 427dbb9d0c9SZhenzhong Duan VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer, 428dbb9d0c9SZhenzhong Duan container); 429233309e8SZhenzhong Duan 430521c8f4eSZhenzhong Duan if (container->iommu_type != VFIO_SPAPR_TCE_v2_IOMMU) { 431521c8f4eSZhenzhong Duan return; 432521c8f4eSZhenzhong Duan } 433521c8f4eSZhenzhong Duan 434521c8f4eSZhenzhong Duan vfio_spapr_remove_window(container, 435521c8f4eSZhenzhong Duan section->offset_within_address_space); 436dbb9d0c9SZhenzhong Duan if (vfio_host_win_del(scontainer, 437521c8f4eSZhenzhong Duan section->offset_within_address_space, 438521c8f4eSZhenzhong Duan section->offset_within_address_space + 439521c8f4eSZhenzhong Duan int128_get64(section->size) - 1) < 0) { 440521c8f4eSZhenzhong Duan hw_error("%s: Cannot delete missing window at %"HWADDR_PRIx, 441521c8f4eSZhenzhong Duan __func__, section->offset_within_address_space); 442521c8f4eSZhenzhong Duan } 443521c8f4eSZhenzhong Duan } 444770c3b6eSZhenzhong Duan 445001a013eSCédric Le Goater static void vfio_spapr_container_release(VFIOContainerBase *bcontainer) 446001a013eSCédric Le Goater { 447001a013eSCédric Le Goater VFIOContainer *container = container_of(bcontainer, VFIOContainer, 448001a013eSCédric Le Goater bcontainer); 449001a013eSCédric Le Goater VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer, 450001a013eSCédric Le Goater container); 451001a013eSCédric Le Goater VFIOHostDMAWindow *hostwin, *next; 452001a013eSCédric Le Goater 453001a013eSCédric Le Goater if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { 454001a013eSCédric Le Goater memory_listener_unregister(&scontainer->prereg_listener); 455001a013eSCédric Le Goater } 456001a013eSCédric Le Goater QLIST_FOREACH_SAFE(hostwin, &scontainer->hostwin_list, hostwin_next, 457001a013eSCédric Le Goater next) { 458001a013eSCédric Le Goater QLIST_REMOVE(hostwin, hostwin_next); 459001a013eSCédric Le Goater g_free(hostwin); 460001a013eSCédric Le Goater } 461001a013eSCédric Le Goater } 462001a013eSCédric Le Goater 46335b25cf4SZhenzhong Duan static bool vfio_spapr_container_setup(VFIOContainerBase *bcontainer, 464f221f641SCédric Le Goater Error **errp) 4659b7d38bfSZhenzhong Duan { 466f221f641SCédric Le Goater VFIOContainer *container = container_of(bcontainer, VFIOContainer, 467f221f641SCédric Le Goater bcontainer); 4686ad359ecSZhenzhong Duan VFIOSpaprContainer *scontainer = container_of(container, VFIOSpaprContainer, 4696ad359ecSZhenzhong Duan container); 470770c3b6eSZhenzhong Duan struct vfio_iommu_spapr_tce_info info; 471770c3b6eSZhenzhong Duan bool v2 = container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU; 472770c3b6eSZhenzhong Duan int ret, fd = container->fd; 473770c3b6eSZhenzhong Duan 474dbb9d0c9SZhenzhong Duan QLIST_INIT(&scontainer->hostwin_list); 475a2347c60SZhenzhong Duan 476770c3b6eSZhenzhong Duan /* 477770c3b6eSZhenzhong Duan * The host kernel code implementing VFIO_IOMMU_DISABLE is called 478770c3b6eSZhenzhong Duan * when container fd is closed so we do not call it explicitly 479770c3b6eSZhenzhong Duan * in this file. 480770c3b6eSZhenzhong Duan */ 481770c3b6eSZhenzhong Duan if (!v2) { 482770c3b6eSZhenzhong Duan ret = ioctl(fd, VFIO_IOMMU_ENABLE); 483770c3b6eSZhenzhong Duan if (ret) { 484770c3b6eSZhenzhong Duan error_setg_errno(errp, errno, "failed to enable container"); 48535b25cf4SZhenzhong Duan return false; 486770c3b6eSZhenzhong Duan } 487770c3b6eSZhenzhong Duan } else { 4886ad359ecSZhenzhong Duan scontainer->prereg_listener = vfio_prereg_listener; 489770c3b6eSZhenzhong Duan 4906ad359ecSZhenzhong Duan memory_listener_register(&scontainer->prereg_listener, 491770c3b6eSZhenzhong Duan &address_space_memory); 492c7b313d3SEric Auger if (bcontainer->error) { 493c7b313d3SEric Auger error_propagate_prepend(errp, bcontainer->error, 494770c3b6eSZhenzhong Duan "RAM memory listener initialization failed: "); 495770c3b6eSZhenzhong Duan goto listener_unregister_exit; 496770c3b6eSZhenzhong Duan } 497770c3b6eSZhenzhong Duan } 498770c3b6eSZhenzhong Duan 499770c3b6eSZhenzhong Duan info.argsz = sizeof(info); 500770c3b6eSZhenzhong Duan ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info); 501770c3b6eSZhenzhong Duan if (ret) { 502770c3b6eSZhenzhong Duan error_setg_errno(errp, errno, 503770c3b6eSZhenzhong Duan "VFIO_IOMMU_SPAPR_TCE_GET_INFO failed"); 504770c3b6eSZhenzhong Duan goto listener_unregister_exit; 505770c3b6eSZhenzhong Duan } 506770c3b6eSZhenzhong Duan 507770c3b6eSZhenzhong Duan if (v2) { 5087ab1cb74SEric Auger bcontainer->pgsizes = info.ddw.pgsizes; 509770c3b6eSZhenzhong Duan /* 510770c3b6eSZhenzhong Duan * There is a default window in just created container. 511770c3b6eSZhenzhong Duan * To make region_add/del simpler, we better remove this 512770c3b6eSZhenzhong Duan * window now and let those iommu_listener callbacks 513770c3b6eSZhenzhong Duan * create/remove them when needed. 514770c3b6eSZhenzhong Duan */ 515770c3b6eSZhenzhong Duan ret = vfio_spapr_remove_window(container, info.dma32_window_start); 516770c3b6eSZhenzhong Duan if (ret) { 517770c3b6eSZhenzhong Duan error_setg_errno(errp, -ret, 518770c3b6eSZhenzhong Duan "failed to remove existing window"); 519770c3b6eSZhenzhong Duan goto listener_unregister_exit; 520770c3b6eSZhenzhong Duan } 521770c3b6eSZhenzhong Duan } else { 522770c3b6eSZhenzhong Duan /* The default table uses 4K pages */ 5237ab1cb74SEric Auger bcontainer->pgsizes = 0x1000; 524dbb9d0c9SZhenzhong Duan vfio_host_win_add(scontainer, info.dma32_window_start, 525770c3b6eSZhenzhong Duan info.dma32_window_start + 526770c3b6eSZhenzhong Duan info.dma32_window_size - 1, 527770c3b6eSZhenzhong Duan 0x1000); 528770c3b6eSZhenzhong Duan } 529770c3b6eSZhenzhong Duan 53035b25cf4SZhenzhong Duan return true; 531770c3b6eSZhenzhong Duan 532770c3b6eSZhenzhong Duan listener_unregister_exit: 533770c3b6eSZhenzhong Duan if (v2) { 5346ad359ecSZhenzhong Duan memory_listener_unregister(&scontainer->prereg_listener); 535770c3b6eSZhenzhong Duan } 53635b25cf4SZhenzhong Duan return false; 537770c3b6eSZhenzhong Duan } 538f221f641SCédric Le Goater 539f221f641SCédric Le Goater static void vfio_iommu_spapr_class_init(ObjectClass *klass, void *data) 540f221f641SCédric Le Goater { 541f221f641SCédric Le Goater VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); 542f221f641SCédric Le Goater 543f221f641SCédric Le Goater vioc->add_window = vfio_spapr_container_add_section_window; 544f221f641SCédric Le Goater vioc->del_window = vfio_spapr_container_del_section_window; 545f221f641SCédric Le Goater vioc->release = vfio_spapr_container_release; 546f221f641SCédric Le Goater vioc->setup = vfio_spapr_container_setup; 547f221f641SCédric Le Goater }; 548f221f641SCédric Le Goater 549f221f641SCédric Le Goater static const TypeInfo types[] = { 550f221f641SCédric Le Goater { 551f221f641SCédric Le Goater .name = TYPE_VFIO_IOMMU_SPAPR, 552f221f641SCédric Le Goater .parent = TYPE_VFIO_IOMMU_LEGACY, 553*504d297eSCédric Le Goater .instance_size = sizeof(VFIOSpaprContainer), 554f221f641SCédric Le Goater .class_init = vfio_iommu_spapr_class_init, 555f221f641SCédric Le Goater }, 556f221f641SCédric Le Goater }; 557f221f641SCédric Le Goater 558f221f641SCédric Le Goater DEFINE_TYPES(types) 559