1108a6481SCindy Lu /* 2108a6481SCindy Lu * vhost-vdpa 3108a6481SCindy Lu * 4108a6481SCindy Lu * Copyright(c) 2017-2018 Intel Corporation. 5108a6481SCindy Lu * Copyright(c) 2020 Red Hat, Inc. 6108a6481SCindy Lu * 7108a6481SCindy Lu * This work is licensed under the terms of the GNU GPL, version 2 or later. 8108a6481SCindy Lu * See the COPYING file in the top-level directory. 9108a6481SCindy Lu * 10108a6481SCindy Lu */ 11108a6481SCindy Lu 12108a6481SCindy Lu #include "qemu/osdep.h" 13108a6481SCindy Lu #include <linux/vhost.h> 14108a6481SCindy Lu #include <linux/vfio.h> 15108a6481SCindy Lu #include <sys/eventfd.h> 16108a6481SCindy Lu #include <sys/ioctl.h> 17108a6481SCindy Lu #include "hw/virtio/vhost.h" 18108a6481SCindy Lu #include "hw/virtio/vhost-backend.h" 19108a6481SCindy Lu #include "hw/virtio/virtio-net.h" 20dff4426fSEugenio Pérez #include "hw/virtio/vhost-shadow-virtqueue.h" 21108a6481SCindy Lu #include "hw/virtio/vhost-vdpa.h" 22df77d45aSXie Yongji #include "exec/address-spaces.h" 23c156d5bfSEugenio Pérez #include "migration/blocker.h" 24415b7327SMarc-André Lureau #include "qemu/cutils.h" 25108a6481SCindy Lu #include "qemu/main-loop.h" 264dc5acc0SCindy Lu #include "cpu.h" 27778e67deSLaurent Vivier #include "trace.h" 28dff4426fSEugenio Pérez #include "qapi/error.h" 29108a6481SCindy Lu 30032e4d68SEugenio Pérez /* 31032e4d68SEugenio Pérez * Return one past the end of the end of section. Be careful with uint64_t 32032e4d68SEugenio Pérez * conversions! 33032e4d68SEugenio Pérez */ 34032e4d68SEugenio Pérez static Int128 vhost_vdpa_section_end(const MemoryRegionSection *section) 35032e4d68SEugenio Pérez { 36032e4d68SEugenio Pérez Int128 llend = int128_make64(section->offset_within_address_space); 37032e4d68SEugenio Pérez llend = int128_add(llend, section->size); 38032e4d68SEugenio Pérez llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK)); 39032e4d68SEugenio Pérez 40032e4d68SEugenio Pérez return llend; 41032e4d68SEugenio Pérez } 42032e4d68SEugenio Pérez 43013108b6SEugenio Pérez static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection *section, 44013108b6SEugenio Pérez uint64_t iova_min, 45013108b6SEugenio Pérez uint64_t iova_max) 46108a6481SCindy Lu { 47013108b6SEugenio Pérez Int128 llend; 48013108b6SEugenio Pérez 49013108b6SEugenio Pérez if ((!memory_region_is_ram(section->mr) && 50108a6481SCindy Lu !memory_region_is_iommu(section->mr)) || 51c64038c9SEugenio Pérez memory_region_is_protected(section->mr) || 52d60c75d2SJason Wang /* vhost-vDPA doesn't allow MMIO to be mapped */ 53013108b6SEugenio Pérez memory_region_is_ram_device(section->mr)) { 54013108b6SEugenio Pérez return true; 55013108b6SEugenio Pérez } 56013108b6SEugenio Pérez 57013108b6SEugenio Pérez if (section->offset_within_address_space < iova_min) { 58013108b6SEugenio Pérez error_report("RAM section out of device range (min=0x%" PRIx64 59013108b6SEugenio Pérez ", addr=0x%" HWADDR_PRIx ")", 60013108b6SEugenio Pérez iova_min, section->offset_within_address_space); 61013108b6SEugenio Pérez return true; 62013108b6SEugenio Pérez } 63013108b6SEugenio Pérez 64013108b6SEugenio Pérez llend = vhost_vdpa_section_end(section); 65013108b6SEugenio Pérez if (int128_gt(llend, int128_make64(iova_max))) { 66013108b6SEugenio Pérez error_report("RAM section out of device range (max=0x%" PRIx64 67013108b6SEugenio Pérez ", end addr=0x%" PRIx64 ")", 68013108b6SEugenio Pérez iova_max, int128_get64(llend)); 69013108b6SEugenio Pérez return true; 70013108b6SEugenio Pérez } 71013108b6SEugenio Pérez 72013108b6SEugenio Pérez return false; 73108a6481SCindy Lu } 74108a6481SCindy Lu 75cd831ed5SEugenio Pérez /* 76cd831ed5SEugenio Pérez * The caller must set asid = 0 if the device does not support asid. 77cd831ed5SEugenio Pérez * This is not an ABI break since it is set to 0 by the initializer anyway. 78cd831ed5SEugenio Pérez */ 79cd831ed5SEugenio Pérez int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, 80cd831ed5SEugenio Pérez hwaddr size, void *vaddr, bool readonly) 81108a6481SCindy Lu { 82386494f2SCindy Lu struct vhost_msg_v2 msg = {}; 83108a6481SCindy Lu int fd = v->device_fd; 84108a6481SCindy Lu int ret = 0; 85108a6481SCindy Lu 86108a6481SCindy Lu msg.type = v->msg_type; 87cd831ed5SEugenio Pérez msg.asid = asid; 88108a6481SCindy Lu msg.iotlb.iova = iova; 89108a6481SCindy Lu msg.iotlb.size = size; 90108a6481SCindy Lu msg.iotlb.uaddr = (uint64_t)(uintptr_t)vaddr; 91108a6481SCindy Lu msg.iotlb.perm = readonly ? VHOST_ACCESS_RO : VHOST_ACCESS_RW; 92108a6481SCindy Lu msg.iotlb.type = VHOST_IOTLB_UPDATE; 93108a6481SCindy Lu 94cd831ed5SEugenio Pérez trace_vhost_vdpa_dma_map(v, fd, msg.type, msg.asid, msg.iotlb.iova, 95cd831ed5SEugenio Pérez msg.iotlb.size, msg.iotlb.uaddr, msg.iotlb.perm, 96cd831ed5SEugenio Pérez msg.iotlb.type); 97778e67deSLaurent Vivier 98108a6481SCindy Lu if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 99108a6481SCindy Lu error_report("failed to write, fd=%d, errno=%d (%s)", 100108a6481SCindy Lu fd, errno, strerror(errno)); 101108a6481SCindy Lu return -EIO ; 102108a6481SCindy Lu } 103108a6481SCindy Lu 104108a6481SCindy Lu return ret; 105108a6481SCindy Lu } 106108a6481SCindy Lu 107cd831ed5SEugenio Pérez /* 108cd831ed5SEugenio Pérez * The caller must set asid = 0 if the device does not support asid. 109cd831ed5SEugenio Pérez * This is not an ABI break since it is set to 0 by the initializer anyway. 110cd831ed5SEugenio Pérez */ 111cd831ed5SEugenio Pérez int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, 112cd831ed5SEugenio Pérez hwaddr size) 113108a6481SCindy Lu { 114386494f2SCindy Lu struct vhost_msg_v2 msg = {}; 115108a6481SCindy Lu int fd = v->device_fd; 116108a6481SCindy Lu int ret = 0; 117108a6481SCindy Lu 118108a6481SCindy Lu msg.type = v->msg_type; 119cd831ed5SEugenio Pérez msg.asid = asid; 120108a6481SCindy Lu msg.iotlb.iova = iova; 121108a6481SCindy Lu msg.iotlb.size = size; 122108a6481SCindy Lu msg.iotlb.type = VHOST_IOTLB_INVALIDATE; 123108a6481SCindy Lu 124cd831ed5SEugenio Pérez trace_vhost_vdpa_dma_unmap(v, fd, msg.type, msg.asid, msg.iotlb.iova, 125778e67deSLaurent Vivier msg.iotlb.size, msg.iotlb.type); 126778e67deSLaurent Vivier 127108a6481SCindy Lu if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 128108a6481SCindy Lu error_report("failed to write, fd=%d, errno=%d (%s)", 129108a6481SCindy Lu fd, errno, strerror(errno)); 130108a6481SCindy Lu return -EIO ; 131108a6481SCindy Lu } 132108a6481SCindy Lu 133108a6481SCindy Lu return ret; 134108a6481SCindy Lu } 135108a6481SCindy Lu 136e6db5df7SEugenio Pérez static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa *v) 137a5bd0580SJason Wang { 138a5bd0580SJason Wang int fd = v->device_fd; 139e6db5df7SEugenio Pérez struct vhost_msg_v2 msg = { 140e6db5df7SEugenio Pérez .type = v->msg_type, 141e6db5df7SEugenio Pérez .iotlb.type = VHOST_IOTLB_BATCH_BEGIN, 142e6db5df7SEugenio Pérez }; 143a5bd0580SJason Wang 1445580b9f0SEugenio Pérez trace_vhost_vdpa_listener_begin_batch(v, fd, msg.type, msg.iotlb.type); 145a5bd0580SJason Wang if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 146a5bd0580SJason Wang error_report("failed to write, fd=%d, errno=%d (%s)", 147a5bd0580SJason Wang fd, errno, strerror(errno)); 148a5bd0580SJason Wang } 149a5bd0580SJason Wang } 150a5bd0580SJason Wang 151e6db5df7SEugenio Pérez static void vhost_vdpa_iotlb_batch_begin_once(struct vhost_vdpa *v) 152e6db5df7SEugenio Pérez { 153e6db5df7SEugenio Pérez if (v->dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH) && 154e6db5df7SEugenio Pérez !v->iotlb_batch_begin_sent) { 155e6db5df7SEugenio Pérez vhost_vdpa_listener_begin_batch(v); 156e6db5df7SEugenio Pérez } 157e6db5df7SEugenio Pérez 158e6db5df7SEugenio Pérez v->iotlb_batch_begin_sent = true; 159e6db5df7SEugenio Pérez } 160e6db5df7SEugenio Pérez 161a5bd0580SJason Wang static void vhost_vdpa_listener_commit(MemoryListener *listener) 162a5bd0580SJason Wang { 163a5bd0580SJason Wang struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 164a5bd0580SJason Wang struct vhost_dev *dev = v->dev; 1658acb3218SPhilippe Mathieu-Daudé struct vhost_msg_v2 msg = {}; 166a5bd0580SJason Wang int fd = v->device_fd; 167a5bd0580SJason Wang 168a5bd0580SJason Wang if (!(dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH))) { 169a5bd0580SJason Wang return; 170a5bd0580SJason Wang } 171a5bd0580SJason Wang 172e6db5df7SEugenio Pérez if (!v->iotlb_batch_begin_sent) { 173e6db5df7SEugenio Pérez return; 174e6db5df7SEugenio Pérez } 175e6db5df7SEugenio Pérez 176a5bd0580SJason Wang msg.type = v->msg_type; 177a5bd0580SJason Wang msg.iotlb.type = VHOST_IOTLB_BATCH_END; 178a5bd0580SJason Wang 1795580b9f0SEugenio Pérez trace_vhost_vdpa_listener_commit(v, fd, msg.type, msg.iotlb.type); 180a5bd0580SJason Wang if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 181a5bd0580SJason Wang error_report("failed to write, fd=%d, errno=%d (%s)", 182a5bd0580SJason Wang fd, errno, strerror(errno)); 183a5bd0580SJason Wang } 184e6db5df7SEugenio Pérez 185e6db5df7SEugenio Pérez v->iotlb_batch_begin_sent = false; 186a5bd0580SJason Wang } 187a5bd0580SJason Wang 188108a6481SCindy Lu static void vhost_vdpa_listener_region_add(MemoryListener *listener, 189108a6481SCindy Lu MemoryRegionSection *section) 190108a6481SCindy Lu { 1917dab70beSEugenio Pérez DMAMap mem_region = {}; 192108a6481SCindy Lu struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 193108a6481SCindy Lu hwaddr iova; 194108a6481SCindy Lu Int128 llend, llsize; 195108a6481SCindy Lu void *vaddr; 196108a6481SCindy Lu int ret; 197108a6481SCindy Lu 198013108b6SEugenio Pérez if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first, 199013108b6SEugenio Pérez v->iova_range.last)) { 200108a6481SCindy Lu return; 201108a6481SCindy Lu } 202108a6481SCindy Lu 203108a6481SCindy Lu if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) != 204108a6481SCindy Lu (section->offset_within_region & ~TARGET_PAGE_MASK))) { 205108a6481SCindy Lu error_report("%s received unaligned region", __func__); 206108a6481SCindy Lu return; 207108a6481SCindy Lu } 208108a6481SCindy Lu 209108a6481SCindy Lu iova = TARGET_PAGE_ALIGN(section->offset_within_address_space); 210032e4d68SEugenio Pérez llend = vhost_vdpa_section_end(section); 211108a6481SCindy Lu if (int128_ge(int128_make64(iova), llend)) { 212108a6481SCindy Lu return; 213108a6481SCindy Lu } 214108a6481SCindy Lu 215108a6481SCindy Lu memory_region_ref(section->mr); 216108a6481SCindy Lu 217108a6481SCindy Lu /* Here we assume that memory_region_is_ram(section->mr)==true */ 218108a6481SCindy Lu 219108a6481SCindy Lu vaddr = memory_region_get_ram_ptr(section->mr) + 220108a6481SCindy Lu section->offset_within_region + 221108a6481SCindy Lu (iova - section->offset_within_address_space); 222108a6481SCindy Lu 223778e67deSLaurent Vivier trace_vhost_vdpa_listener_region_add(v, iova, int128_get64(llend), 224778e67deSLaurent Vivier vaddr, section->readonly); 225778e67deSLaurent Vivier 226108a6481SCindy Lu llsize = int128_sub(llend, int128_make64(iova)); 2276188d78aSEugenio Pérez if (v->shadow_data) { 2287dab70beSEugenio Pérez int r; 22934e3c94eSEugenio Pérez 2307dab70beSEugenio Pérez mem_region.translated_addr = (hwaddr)(uintptr_t)vaddr, 2317dab70beSEugenio Pérez mem_region.size = int128_get64(llsize) - 1, 2327dab70beSEugenio Pérez mem_region.perm = IOMMU_ACCESS_FLAG(true, section->readonly), 2337dab70beSEugenio Pérez 2347dab70beSEugenio Pérez r = vhost_iova_tree_map_alloc(v->iova_tree, &mem_region); 23534e3c94eSEugenio Pérez if (unlikely(r != IOVA_OK)) { 23634e3c94eSEugenio Pérez error_report("Can't allocate a mapping (%d)", r); 23734e3c94eSEugenio Pérez goto fail; 23834e3c94eSEugenio Pérez } 23934e3c94eSEugenio Pérez 24034e3c94eSEugenio Pérez iova = mem_region.iova; 24134e3c94eSEugenio Pérez } 242108a6481SCindy Lu 243e6db5df7SEugenio Pérez vhost_vdpa_iotlb_batch_begin_once(v); 244cd831ed5SEugenio Pérez ret = vhost_vdpa_dma_map(v, VHOST_VDPA_GUEST_PA_ASID, iova, 245cd831ed5SEugenio Pérez int128_get64(llsize), vaddr, section->readonly); 246108a6481SCindy Lu if (ret) { 247108a6481SCindy Lu error_report("vhost vdpa map fail!"); 2487dab70beSEugenio Pérez goto fail_map; 249108a6481SCindy Lu } 250108a6481SCindy Lu 251108a6481SCindy Lu return; 252108a6481SCindy Lu 2537dab70beSEugenio Pérez fail_map: 2546188d78aSEugenio Pérez if (v->shadow_data) { 25569292a8eSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, mem_region); 2567dab70beSEugenio Pérez } 2577dab70beSEugenio Pérez 258108a6481SCindy Lu fail: 259108a6481SCindy Lu /* 260108a6481SCindy Lu * On the initfn path, store the first error in the container so we 261108a6481SCindy Lu * can gracefully fail. Runtime, there's not much we can do other 262108a6481SCindy Lu * than throw a hardware error. 263108a6481SCindy Lu */ 264108a6481SCindy Lu error_report("vhost-vdpa: DMA mapping failed, unable to continue"); 265108a6481SCindy Lu return; 266108a6481SCindy Lu 267108a6481SCindy Lu } 268108a6481SCindy Lu 269108a6481SCindy Lu static void vhost_vdpa_listener_region_del(MemoryListener *listener, 270108a6481SCindy Lu MemoryRegionSection *section) 271108a6481SCindy Lu { 272108a6481SCindy Lu struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 273108a6481SCindy Lu hwaddr iova; 274108a6481SCindy Lu Int128 llend, llsize; 275108a6481SCindy Lu int ret; 276108a6481SCindy Lu 277013108b6SEugenio Pérez if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first, 278013108b6SEugenio Pérez v->iova_range.last)) { 279108a6481SCindy Lu return; 280108a6481SCindy Lu } 281108a6481SCindy Lu 282108a6481SCindy Lu if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) != 283108a6481SCindy Lu (section->offset_within_region & ~TARGET_PAGE_MASK))) { 284108a6481SCindy Lu error_report("%s received unaligned region", __func__); 285108a6481SCindy Lu return; 286108a6481SCindy Lu } 287108a6481SCindy Lu 288108a6481SCindy Lu iova = TARGET_PAGE_ALIGN(section->offset_within_address_space); 289032e4d68SEugenio Pérez llend = vhost_vdpa_section_end(section); 290108a6481SCindy Lu 291*3d1e4d34SCindy Lu trace_vhost_vdpa_listener_region_del(v, iova, 292*3d1e4d34SCindy Lu int128_get64(int128_sub(llend, int128_one()))); 293778e67deSLaurent Vivier 294108a6481SCindy Lu if (int128_ge(int128_make64(iova), llend)) { 295108a6481SCindy Lu return; 296108a6481SCindy Lu } 297108a6481SCindy Lu 298108a6481SCindy Lu llsize = int128_sub(llend, int128_make64(iova)); 299108a6481SCindy Lu 3006188d78aSEugenio Pérez if (v->shadow_data) { 30134e3c94eSEugenio Pérez const DMAMap *result; 30234e3c94eSEugenio Pérez const void *vaddr = memory_region_get_ram_ptr(section->mr) + 30334e3c94eSEugenio Pérez section->offset_within_region + 30434e3c94eSEugenio Pérez (iova - section->offset_within_address_space); 30534e3c94eSEugenio Pérez DMAMap mem_region = { 30634e3c94eSEugenio Pérez .translated_addr = (hwaddr)(uintptr_t)vaddr, 30734e3c94eSEugenio Pérez .size = int128_get64(llsize) - 1, 30834e3c94eSEugenio Pérez }; 30934e3c94eSEugenio Pérez 31034e3c94eSEugenio Pérez result = vhost_iova_tree_find_iova(v->iova_tree, &mem_region); 31110dab9f2SEugenio Pérez if (!result) { 31210dab9f2SEugenio Pérez /* The memory listener map wasn't mapped */ 31310dab9f2SEugenio Pérez return; 31410dab9f2SEugenio Pérez } 31534e3c94eSEugenio Pérez iova = result->iova; 31669292a8eSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, *result); 31734e3c94eSEugenio Pérez } 318e6db5df7SEugenio Pérez vhost_vdpa_iotlb_batch_begin_once(v); 319cd831ed5SEugenio Pérez ret = vhost_vdpa_dma_unmap(v, VHOST_VDPA_GUEST_PA_ASID, iova, 320cd831ed5SEugenio Pérez int128_get64(llsize)); 321108a6481SCindy Lu if (ret) { 322108a6481SCindy Lu error_report("vhost_vdpa dma unmap error!"); 323108a6481SCindy Lu } 324108a6481SCindy Lu 325108a6481SCindy Lu memory_region_unref(section->mr); 326108a6481SCindy Lu } 327108a6481SCindy Lu /* 328ef4ff56cSStefano Garzarella * IOTLB API is used by vhost-vdpa which requires incremental updating 329108a6481SCindy Lu * of the mapping. So we can not use generic vhost memory listener which 330108a6481SCindy Lu * depends on the addnop(). 331108a6481SCindy Lu */ 332108a6481SCindy Lu static const MemoryListener vhost_vdpa_memory_listener = { 333142518bdSPeter Xu .name = "vhost-vdpa", 334a5bd0580SJason Wang .commit = vhost_vdpa_listener_commit, 335108a6481SCindy Lu .region_add = vhost_vdpa_listener_region_add, 336108a6481SCindy Lu .region_del = vhost_vdpa_listener_region_del, 337108a6481SCindy Lu }; 338108a6481SCindy Lu 339108a6481SCindy Lu static int vhost_vdpa_call(struct vhost_dev *dev, unsigned long int request, 340108a6481SCindy Lu void *arg) 341108a6481SCindy Lu { 342108a6481SCindy Lu struct vhost_vdpa *v = dev->opaque; 343108a6481SCindy Lu int fd = v->device_fd; 344f2a6e6c4SKevin Wolf int ret; 345108a6481SCindy Lu 346108a6481SCindy Lu assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 347108a6481SCindy Lu 348f2a6e6c4SKevin Wolf ret = ioctl(fd, request, arg); 349f2a6e6c4SKevin Wolf return ret < 0 ? -errno : ret; 350108a6481SCindy Lu } 351108a6481SCindy Lu 3523631151bSRoman Kagan static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status) 353108a6481SCindy Lu { 354108a6481SCindy Lu uint8_t s; 3553631151bSRoman Kagan int ret; 356108a6481SCindy Lu 357778e67deSLaurent Vivier trace_vhost_vdpa_add_status(dev, status); 3583631151bSRoman Kagan ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s); 3593631151bSRoman Kagan if (ret < 0) { 3603631151bSRoman Kagan return ret; 361108a6481SCindy Lu } 362108a6481SCindy Lu 363108a6481SCindy Lu s |= status; 364108a6481SCindy Lu 3653631151bSRoman Kagan ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &s); 3663631151bSRoman Kagan if (ret < 0) { 3673631151bSRoman Kagan return ret; 3683631151bSRoman Kagan } 3693631151bSRoman Kagan 3703631151bSRoman Kagan ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s); 3713631151bSRoman Kagan if (ret < 0) { 3723631151bSRoman Kagan return ret; 3733631151bSRoman Kagan } 3743631151bSRoman Kagan 3753631151bSRoman Kagan if (!(s & status)) { 3763631151bSRoman Kagan return -EIO; 3773631151bSRoman Kagan } 3783631151bSRoman Kagan 3793631151bSRoman Kagan return 0; 380108a6481SCindy Lu } 381108a6481SCindy Lu 382c672f348SLongpeng int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_range) 383c672f348SLongpeng { 384c672f348SLongpeng int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range); 385c672f348SLongpeng 386c672f348SLongpeng return ret < 0 ? -errno : 0; 387c672f348SLongpeng } 388c672f348SLongpeng 389d71b0609SSi-Wei Liu /* 390d71b0609SSi-Wei Liu * The use of this function is for requests that only need to be 391d71b0609SSi-Wei Liu * applied once. Typically such request occurs at the beginning 392d71b0609SSi-Wei Liu * of operation, and before setting up queues. It should not be 393d71b0609SSi-Wei Liu * used for request that performs operation until all queues are 394d71b0609SSi-Wei Liu * set, which would need to check dev->vq_index_end instead. 395d71b0609SSi-Wei Liu */ 396d71b0609SSi-Wei Liu static bool vhost_vdpa_first_dev(struct vhost_dev *dev) 3974d191cfdSJason Wang { 3984d191cfdSJason Wang struct vhost_vdpa *v = dev->opaque; 3994d191cfdSJason Wang 400d71b0609SSi-Wei Liu return v->index == 0; 4014d191cfdSJason Wang } 4024d191cfdSJason Wang 40312a195faSEugenio Pérez static int vhost_vdpa_get_dev_features(struct vhost_dev *dev, 40412a195faSEugenio Pérez uint64_t *features) 40512a195faSEugenio Pérez { 40612a195faSEugenio Pérez int ret; 40712a195faSEugenio Pérez 40812a195faSEugenio Pérez ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features); 40912a195faSEugenio Pérez trace_vhost_vdpa_get_features(dev, *features); 41012a195faSEugenio Pérez return ret; 41112a195faSEugenio Pérez } 41212a195faSEugenio Pérez 413258a0394SEugenio Pérez static void vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v) 414dff4426fSEugenio Pérez { 415dff4426fSEugenio Pérez g_autoptr(GPtrArray) shadow_vqs = NULL; 4164725a418SEugenio Pérez 417dff4426fSEugenio Pérez shadow_vqs = g_ptr_array_new_full(hdev->nvqs, vhost_svq_free); 418dff4426fSEugenio Pérez for (unsigned n = 0; n < hdev->nvqs; ++n) { 4193cfb4d06SEugenio Pérez VhostShadowVirtqueue *svq; 420dff4426fSEugenio Pérez 4215fde952bSEugenio Pérez svq = vhost_svq_new(v->shadow_vq_ops, v->shadow_vq_ops_opaque); 4223cfb4d06SEugenio Pérez g_ptr_array_add(shadow_vqs, svq); 423dff4426fSEugenio Pérez } 424dff4426fSEugenio Pérez 425dff4426fSEugenio Pérez v->shadow_vqs = g_steal_pointer(&shadow_vqs); 426dff4426fSEugenio Pérez } 427dff4426fSEugenio Pérez 42828770ff9SKevin Wolf static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) 429108a6481SCindy Lu { 430108a6481SCindy Lu struct vhost_vdpa *v; 431108a6481SCindy Lu assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 432778e67deSLaurent Vivier trace_vhost_vdpa_init(dev, opaque); 433e1c1915bSDavid Hildenbrand int ret; 434e1c1915bSDavid Hildenbrand 435108a6481SCindy Lu v = opaque; 436a5bd0580SJason Wang v->dev = dev; 437108a6481SCindy Lu dev->opaque = opaque ; 438108a6481SCindy Lu v->listener = vhost_vdpa_memory_listener; 439108a6481SCindy Lu v->msg_type = VHOST_IOTLB_MSG_V2; 440258a0394SEugenio Pérez vhost_vdpa_init_svq(dev, v); 441108a6481SCindy Lu 4429c363cf6SEugenio Pérez error_propagate(&dev->migration_blocker, v->migration_blocker); 443d71b0609SSi-Wei Liu if (!vhost_vdpa_first_dev(dev)) { 4444d191cfdSJason Wang return 0; 4454d191cfdSJason Wang } 4464d191cfdSJason Wang 447a230c471SEugenio Pérez /* 44857ac8318SEugenio Pérez * If dev->shadow_vqs_enabled at initialization that means the device has 44957ac8318SEugenio Pérez * been started with x-svq=on, so don't block migration 45057ac8318SEugenio Pérez */ 45157ac8318SEugenio Pérez if (dev->migration_blocker == NULL && !v->shadow_vqs_enabled) { 45257ac8318SEugenio Pérez /* We don't have dev->features yet */ 45357ac8318SEugenio Pérez uint64_t features; 45457ac8318SEugenio Pérez ret = vhost_vdpa_get_dev_features(dev, &features); 45557ac8318SEugenio Pérez if (unlikely(ret)) { 45657ac8318SEugenio Pérez error_setg_errno(errp, -ret, "Could not get device features"); 45757ac8318SEugenio Pérez return ret; 45857ac8318SEugenio Pérez } 45957ac8318SEugenio Pérez vhost_svq_valid_features(features, &dev->migration_blocker); 46057ac8318SEugenio Pérez } 46157ac8318SEugenio Pérez 46257ac8318SEugenio Pérez /* 463a230c471SEugenio Pérez * Similar to VFIO, we end up pinning all guest memory and have to 464a230c471SEugenio Pérez * disable discarding of RAM. 465a230c471SEugenio Pérez */ 466a230c471SEugenio Pérez ret = ram_block_discard_disable(true); 467a230c471SEugenio Pérez if (ret) { 468a230c471SEugenio Pérez error_report("Cannot set discarding of RAM broken"); 469a230c471SEugenio Pérez return ret; 470a230c471SEugenio Pérez } 471a230c471SEugenio Pérez 472108a6481SCindy Lu vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | 473108a6481SCindy Lu VIRTIO_CONFIG_S_DRIVER); 474108a6481SCindy Lu 475108a6481SCindy Lu return 0; 476108a6481SCindy Lu } 477108a6481SCindy Lu 478d0416d48SJason Wang static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev, 479d0416d48SJason Wang int queue_index) 480d0416d48SJason Wang { 4818e3b0cbbSMarc-André Lureau size_t page_size = qemu_real_host_page_size(); 482d0416d48SJason Wang struct vhost_vdpa *v = dev->opaque; 483d0416d48SJason Wang VirtIODevice *vdev = dev->vdev; 484d0416d48SJason Wang VhostVDPAHostNotifier *n; 485d0416d48SJason Wang 486d0416d48SJason Wang n = &v->notifier[queue_index]; 487d0416d48SJason Wang 488d0416d48SJason Wang if (n->addr) { 489d0416d48SJason Wang virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, false); 490d0416d48SJason Wang object_unparent(OBJECT(&n->mr)); 491d0416d48SJason Wang munmap(n->addr, page_size); 492d0416d48SJason Wang n->addr = NULL; 493d0416d48SJason Wang } 494d0416d48SJason Wang } 495d0416d48SJason Wang 496d0416d48SJason Wang static int vhost_vdpa_host_notifier_init(struct vhost_dev *dev, int queue_index) 497d0416d48SJason Wang { 4988e3b0cbbSMarc-André Lureau size_t page_size = qemu_real_host_page_size(); 499d0416d48SJason Wang struct vhost_vdpa *v = dev->opaque; 500d0416d48SJason Wang VirtIODevice *vdev = dev->vdev; 501d0416d48SJason Wang VhostVDPAHostNotifier *n; 502d0416d48SJason Wang int fd = v->device_fd; 503d0416d48SJason Wang void *addr; 504d0416d48SJason Wang char *name; 505d0416d48SJason Wang 506d0416d48SJason Wang vhost_vdpa_host_notifier_uninit(dev, queue_index); 507d0416d48SJason Wang 508d0416d48SJason Wang n = &v->notifier[queue_index]; 509d0416d48SJason Wang 510d0416d48SJason Wang addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd, 511d0416d48SJason Wang queue_index * page_size); 512d0416d48SJason Wang if (addr == MAP_FAILED) { 513d0416d48SJason Wang goto err; 514d0416d48SJason Wang } 515d0416d48SJason Wang 516d0416d48SJason Wang name = g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]", 517d0416d48SJason Wang v, queue_index); 518d0416d48SJason Wang memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name, 519d0416d48SJason Wang page_size, addr); 520d0416d48SJason Wang g_free(name); 521d0416d48SJason Wang 522d0416d48SJason Wang if (virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, true)) { 52398f7607eSLaurent Vivier object_unparent(OBJECT(&n->mr)); 524d0416d48SJason Wang munmap(addr, page_size); 525d0416d48SJason Wang goto err; 526d0416d48SJason Wang } 527d0416d48SJason Wang n->addr = addr; 528d0416d48SJason Wang 529d0416d48SJason Wang return 0; 530d0416d48SJason Wang 531d0416d48SJason Wang err: 532d0416d48SJason Wang return -1; 533d0416d48SJason Wang } 534d0416d48SJason Wang 535b1f030a0SLaurent Vivier static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n) 536b1f030a0SLaurent Vivier { 537b1f030a0SLaurent Vivier int i; 538b1f030a0SLaurent Vivier 539e66f2311SLongpeng /* 540e66f2311SLongpeng * Pack all the changes to the memory regions in a single 541e66f2311SLongpeng * transaction to avoid a few updating of the address space 542e66f2311SLongpeng * topology. 543e66f2311SLongpeng */ 544e66f2311SLongpeng memory_region_transaction_begin(); 545e66f2311SLongpeng 546b1f030a0SLaurent Vivier for (i = dev->vq_index; i < dev->vq_index + n; i++) { 547b1f030a0SLaurent Vivier vhost_vdpa_host_notifier_uninit(dev, i); 548b1f030a0SLaurent Vivier } 549e66f2311SLongpeng 550e66f2311SLongpeng memory_region_transaction_commit(); 551b1f030a0SLaurent Vivier } 552b1f030a0SLaurent Vivier 553d0416d48SJason Wang static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev) 554d0416d48SJason Wang { 555dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 556d0416d48SJason Wang int i; 557d0416d48SJason Wang 558dff4426fSEugenio Pérez if (v->shadow_vqs_enabled) { 559dff4426fSEugenio Pérez /* FIXME SVQ is not compatible with host notifiers mr */ 560dff4426fSEugenio Pérez return; 561dff4426fSEugenio Pérez } 562dff4426fSEugenio Pérez 563e66f2311SLongpeng /* 564e66f2311SLongpeng * Pack all the changes to the memory regions in a single 565e66f2311SLongpeng * transaction to avoid a few updating of the address space 566e66f2311SLongpeng * topology. 567e66f2311SLongpeng */ 568e66f2311SLongpeng memory_region_transaction_begin(); 569e66f2311SLongpeng 570d0416d48SJason Wang for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) { 571d0416d48SJason Wang if (vhost_vdpa_host_notifier_init(dev, i)) { 572b1f030a0SLaurent Vivier vhost_vdpa_host_notifiers_uninit(dev, i - dev->vq_index); 573e66f2311SLongpeng break; 574e66f2311SLongpeng } 575e66f2311SLongpeng } 576e66f2311SLongpeng 577e66f2311SLongpeng memory_region_transaction_commit(); 578d0416d48SJason Wang } 579d0416d48SJason Wang 580dff4426fSEugenio Pérez static void vhost_vdpa_svq_cleanup(struct vhost_dev *dev) 581dff4426fSEugenio Pérez { 582dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 583dff4426fSEugenio Pérez size_t idx; 584dff4426fSEugenio Pérez 585dff4426fSEugenio Pérez for (idx = 0; idx < v->shadow_vqs->len; ++idx) { 586dff4426fSEugenio Pérez vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, idx)); 587dff4426fSEugenio Pérez } 588dff4426fSEugenio Pérez g_ptr_array_free(v->shadow_vqs, true); 589dff4426fSEugenio Pérez } 590dff4426fSEugenio Pérez 591108a6481SCindy Lu static int vhost_vdpa_cleanup(struct vhost_dev *dev) 592108a6481SCindy Lu { 593108a6481SCindy Lu struct vhost_vdpa *v; 594108a6481SCindy Lu assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 595108a6481SCindy Lu v = dev->opaque; 596778e67deSLaurent Vivier trace_vhost_vdpa_cleanup(dev, v); 597a230c471SEugenio Pérez if (vhost_vdpa_first_dev(dev)) { 598a230c471SEugenio Pérez ram_block_discard_disable(false); 599a230c471SEugenio Pérez } 600a230c471SEugenio Pérez 601d0416d48SJason Wang vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); 602108a6481SCindy Lu memory_listener_unregister(&v->listener); 603dff4426fSEugenio Pérez vhost_vdpa_svq_cleanup(dev); 604108a6481SCindy Lu 605108a6481SCindy Lu dev->opaque = NULL; 606e1c1915bSDavid Hildenbrand 607108a6481SCindy Lu return 0; 608108a6481SCindy Lu } 609108a6481SCindy Lu 610108a6481SCindy Lu static int vhost_vdpa_memslots_limit(struct vhost_dev *dev) 611108a6481SCindy Lu { 612778e67deSLaurent Vivier trace_vhost_vdpa_memslots_limit(dev, INT_MAX); 613108a6481SCindy Lu return INT_MAX; 614108a6481SCindy Lu } 615108a6481SCindy Lu 616108a6481SCindy Lu static int vhost_vdpa_set_mem_table(struct vhost_dev *dev, 617108a6481SCindy Lu struct vhost_memory *mem) 618108a6481SCindy Lu { 619d71b0609SSi-Wei Liu if (!vhost_vdpa_first_dev(dev)) { 6204d191cfdSJason Wang return 0; 6214d191cfdSJason Wang } 6224d191cfdSJason Wang 623778e67deSLaurent Vivier trace_vhost_vdpa_set_mem_table(dev, mem->nregions, mem->padding); 624778e67deSLaurent Vivier if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE) && 625778e67deSLaurent Vivier trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS)) { 626778e67deSLaurent Vivier int i; 627778e67deSLaurent Vivier for (i = 0; i < mem->nregions; i++) { 628778e67deSLaurent Vivier trace_vhost_vdpa_dump_regions(dev, i, 629778e67deSLaurent Vivier mem->regions[i].guest_phys_addr, 630778e67deSLaurent Vivier mem->regions[i].memory_size, 631778e67deSLaurent Vivier mem->regions[i].userspace_addr, 632778e67deSLaurent Vivier mem->regions[i].flags_padding); 633778e67deSLaurent Vivier } 634778e67deSLaurent Vivier } 635108a6481SCindy Lu if (mem->padding) { 6363631151bSRoman Kagan return -EINVAL; 637108a6481SCindy Lu } 638108a6481SCindy Lu 639108a6481SCindy Lu return 0; 640108a6481SCindy Lu } 641108a6481SCindy Lu 642108a6481SCindy Lu static int vhost_vdpa_set_features(struct vhost_dev *dev, 643108a6481SCindy Lu uint64_t features) 644108a6481SCindy Lu { 64512a195faSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 646108a6481SCindy Lu int ret; 6474d191cfdSJason Wang 648d71b0609SSi-Wei Liu if (!vhost_vdpa_first_dev(dev)) { 6494d191cfdSJason Wang return 0; 6504d191cfdSJason Wang } 6514d191cfdSJason Wang 65212a195faSEugenio Pérez if (v->shadow_vqs_enabled) { 65312a195faSEugenio Pérez if ((v->acked_features ^ features) == BIT_ULL(VHOST_F_LOG_ALL)) { 65412a195faSEugenio Pérez /* 65512a195faSEugenio Pérez * QEMU is just trying to enable or disable logging. SVQ handles 65612a195faSEugenio Pérez * this sepparately, so no need to forward this. 65712a195faSEugenio Pérez */ 65812a195faSEugenio Pérez v->acked_features = features; 65912a195faSEugenio Pérez return 0; 66012a195faSEugenio Pérez } 66112a195faSEugenio Pérez 66212a195faSEugenio Pérez v->acked_features = features; 66312a195faSEugenio Pérez 66412a195faSEugenio Pérez /* We must not ack _F_LOG if SVQ is enabled */ 66512a195faSEugenio Pérez features &= ~BIT_ULL(VHOST_F_LOG_ALL); 66612a195faSEugenio Pérez } 66712a195faSEugenio Pérez 668778e67deSLaurent Vivier trace_vhost_vdpa_set_features(dev, features); 669108a6481SCindy Lu ret = vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features); 670108a6481SCindy Lu if (ret) { 671108a6481SCindy Lu return ret; 672108a6481SCindy Lu } 673108a6481SCindy Lu 6743631151bSRoman Kagan return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK); 675108a6481SCindy Lu } 676108a6481SCindy Lu 677a5bd0580SJason Wang static int vhost_vdpa_set_backend_cap(struct vhost_dev *dev) 678a5bd0580SJason Wang { 679a5bd0580SJason Wang uint64_t features; 680a5bd0580SJason Wang uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | 681c1a10086SEugenio Pérez 0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH | 682d83b4945SEugenio Pérez 0x1ULL << VHOST_BACKEND_F_IOTLB_ASID | 683d83b4945SEugenio Pérez 0x1ULL << VHOST_BACKEND_F_SUSPEND; 684a5bd0580SJason Wang int r; 685a5bd0580SJason Wang 686a5bd0580SJason Wang if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) { 6872a83e97eSJason Wang return -EFAULT; 688a5bd0580SJason Wang } 689a5bd0580SJason Wang 690a5bd0580SJason Wang features &= f; 6914d191cfdSJason Wang 692d71b0609SSi-Wei Liu if (vhost_vdpa_first_dev(dev)) { 693a5bd0580SJason Wang r = vhost_vdpa_call(dev, VHOST_SET_BACKEND_FEATURES, &features); 694a5bd0580SJason Wang if (r) { 6952a83e97eSJason Wang return -EFAULT; 696a5bd0580SJason Wang } 6974d191cfdSJason Wang } 698a5bd0580SJason Wang 699a5bd0580SJason Wang dev->backend_cap = features; 700a5bd0580SJason Wang 701a5bd0580SJason Wang return 0; 702a5bd0580SJason Wang } 703a5bd0580SJason Wang 704c232b8f4SZenghui Yu static int vhost_vdpa_get_device_id(struct vhost_dev *dev, 705108a6481SCindy Lu uint32_t *device_id) 706108a6481SCindy Lu { 707778e67deSLaurent Vivier int ret; 708778e67deSLaurent Vivier ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_DEVICE_ID, device_id); 709778e67deSLaurent Vivier trace_vhost_vdpa_get_device_id(dev, *device_id); 710778e67deSLaurent Vivier return ret; 711108a6481SCindy Lu } 712108a6481SCindy Lu 713108a6481SCindy Lu static int vhost_vdpa_reset_device(struct vhost_dev *dev) 714108a6481SCindy Lu { 7150bb302a9SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 716778e67deSLaurent Vivier int ret; 717108a6481SCindy Lu uint8_t status = 0; 718108a6481SCindy Lu 719778e67deSLaurent Vivier ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status); 720778e67deSLaurent Vivier trace_vhost_vdpa_reset_device(dev, status); 7210bb302a9SEugenio Pérez v->suspended = false; 722778e67deSLaurent Vivier return ret; 723108a6481SCindy Lu } 724108a6481SCindy Lu 725108a6481SCindy Lu static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx) 726108a6481SCindy Lu { 727108a6481SCindy Lu assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); 728108a6481SCindy Lu 729353244d8SJason Wang trace_vhost_vdpa_get_vq_index(dev, idx, idx); 730353244d8SJason Wang return idx; 731108a6481SCindy Lu } 732108a6481SCindy Lu 733108a6481SCindy Lu static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev) 734108a6481SCindy Lu { 735108a6481SCindy Lu int i; 736778e67deSLaurent Vivier trace_vhost_vdpa_set_vring_ready(dev); 737108a6481SCindy Lu for (i = 0; i < dev->nvqs; ++i) { 738108a6481SCindy Lu struct vhost_vring_state state = { 739108a6481SCindy Lu .index = dev->vq_index + i, 740108a6481SCindy Lu .num = 1, 741108a6481SCindy Lu }; 742108a6481SCindy Lu vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); 743108a6481SCindy Lu } 744108a6481SCindy Lu return 0; 745108a6481SCindy Lu } 746108a6481SCindy Lu 747259f3accSCindy Lu static int vhost_vdpa_set_config_call(struct vhost_dev *dev, 748259f3accSCindy Lu int fd) 749259f3accSCindy Lu { 750259f3accSCindy Lu trace_vhost_vdpa_set_config_call(dev, fd); 751259f3accSCindy Lu return vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG_CALL, &fd); 752259f3accSCindy Lu } 753259f3accSCindy Lu 754778e67deSLaurent Vivier static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config, 755778e67deSLaurent Vivier uint32_t config_len) 756778e67deSLaurent Vivier { 757778e67deSLaurent Vivier int b, len; 758778e67deSLaurent Vivier char line[QEMU_HEXDUMP_LINE_LEN]; 759778e67deSLaurent Vivier 760778e67deSLaurent Vivier for (b = 0; b < config_len; b += 16) { 761778e67deSLaurent Vivier len = config_len - b; 762778e67deSLaurent Vivier qemu_hexdump_line(line, b, config, len, false); 763778e67deSLaurent Vivier trace_vhost_vdpa_dump_config(dev, line); 764778e67deSLaurent Vivier } 765778e67deSLaurent Vivier } 766778e67deSLaurent Vivier 767108a6481SCindy Lu static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data, 768108a6481SCindy Lu uint32_t offset, uint32_t size, 769108a6481SCindy Lu uint32_t flags) 770108a6481SCindy Lu { 771108a6481SCindy Lu struct vhost_vdpa_config *config; 772108a6481SCindy Lu int ret; 773108a6481SCindy Lu unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 774986d4f78SLi Qiang 775778e67deSLaurent Vivier trace_vhost_vdpa_set_config(dev, offset, size, flags); 776108a6481SCindy Lu config = g_malloc(size + config_size); 777108a6481SCindy Lu config->off = offset; 778108a6481SCindy Lu config->len = size; 779108a6481SCindy Lu memcpy(config->buf, data, size); 780778e67deSLaurent Vivier if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG) && 781778e67deSLaurent Vivier trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) { 782778e67deSLaurent Vivier vhost_vdpa_dump_config(dev, data, size); 783778e67deSLaurent Vivier } 784108a6481SCindy Lu ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG, config); 785108a6481SCindy Lu g_free(config); 786108a6481SCindy Lu return ret; 787108a6481SCindy Lu } 788108a6481SCindy Lu 789108a6481SCindy Lu static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config, 79050de5138SKevin Wolf uint32_t config_len, Error **errp) 791108a6481SCindy Lu { 792108a6481SCindy Lu struct vhost_vdpa_config *v_config; 793108a6481SCindy Lu unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 794108a6481SCindy Lu int ret; 795108a6481SCindy Lu 796778e67deSLaurent Vivier trace_vhost_vdpa_get_config(dev, config, config_len); 797108a6481SCindy Lu v_config = g_malloc(config_len + config_size); 798108a6481SCindy Lu v_config->len = config_len; 799108a6481SCindy Lu v_config->off = 0; 800108a6481SCindy Lu ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config); 801108a6481SCindy Lu memcpy(config, v_config->buf, config_len); 802108a6481SCindy Lu g_free(v_config); 803778e67deSLaurent Vivier if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG) && 804778e67deSLaurent Vivier trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) { 805778e67deSLaurent Vivier vhost_vdpa_dump_config(dev, config, config_len); 806778e67deSLaurent Vivier } 807108a6481SCindy Lu return ret; 808108a6481SCindy Lu } 809108a6481SCindy Lu 810d96be4c8SEugenio Pérez static int vhost_vdpa_set_dev_vring_base(struct vhost_dev *dev, 811d96be4c8SEugenio Pérez struct vhost_vring_state *ring) 812d96be4c8SEugenio Pérez { 813d96be4c8SEugenio Pérez trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num); 814d96be4c8SEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring); 815d96be4c8SEugenio Pérez } 816d96be4c8SEugenio Pérez 817dff4426fSEugenio Pérez static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev, 818dff4426fSEugenio Pérez struct vhost_vring_file *file) 819dff4426fSEugenio Pérez { 820dff4426fSEugenio Pérez trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd); 821dff4426fSEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file); 822dff4426fSEugenio Pérez } 823dff4426fSEugenio Pérez 824a8ac8858SEugenio Pérez static int vhost_vdpa_set_vring_dev_call(struct vhost_dev *dev, 825a8ac8858SEugenio Pérez struct vhost_vring_file *file) 826a8ac8858SEugenio Pérez { 827a8ac8858SEugenio Pérez trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd); 828a8ac8858SEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file); 829a8ac8858SEugenio Pérez } 830a8ac8858SEugenio Pérez 831d96be4c8SEugenio Pérez static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev *dev, 832d96be4c8SEugenio Pérez struct vhost_vring_addr *addr) 833d96be4c8SEugenio Pérez { 834d96be4c8SEugenio Pérez trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags, 835d96be4c8SEugenio Pérez addr->desc_user_addr, addr->used_user_addr, 836d96be4c8SEugenio Pérez addr->avail_user_addr, 837d96be4c8SEugenio Pérez addr->log_guest_addr); 838d96be4c8SEugenio Pérez 839d96be4c8SEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr); 840d96be4c8SEugenio Pérez 841d96be4c8SEugenio Pérez } 842d96be4c8SEugenio Pérez 843dff4426fSEugenio Pérez /** 844dff4426fSEugenio Pérez * Set the shadow virtqueue descriptors to the device 845dff4426fSEugenio Pérez * 846dff4426fSEugenio Pérez * @dev: The vhost device model 847dff4426fSEugenio Pérez * @svq: The shadow virtqueue 848dff4426fSEugenio Pérez * @idx: The index of the virtqueue in the vhost device 849dff4426fSEugenio Pérez * @errp: Error 850a8ac8858SEugenio Pérez * 851a8ac8858SEugenio Pérez * Note that this function does not rewind kick file descriptor if cannot set 852a8ac8858SEugenio Pérez * call one. 853dff4426fSEugenio Pérez */ 854100890f7SEugenio Pérez static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev, 855dff4426fSEugenio Pérez VhostShadowVirtqueue *svq, unsigned idx, 856dff4426fSEugenio Pérez Error **errp) 857dff4426fSEugenio Pérez { 858dff4426fSEugenio Pérez struct vhost_vring_file file = { 859dff4426fSEugenio Pérez .index = dev->vq_index + idx, 860dff4426fSEugenio Pérez }; 861dff4426fSEugenio Pérez const EventNotifier *event_notifier = &svq->hdev_kick; 862dff4426fSEugenio Pérez int r; 863dff4426fSEugenio Pérez 8643cfb4d06SEugenio Pérez r = event_notifier_init(&svq->hdev_kick, 0); 8653cfb4d06SEugenio Pérez if (r != 0) { 8663cfb4d06SEugenio Pérez error_setg_errno(errp, -r, "Couldn't create kick event notifier"); 8673cfb4d06SEugenio Pérez goto err_init_hdev_kick; 8683cfb4d06SEugenio Pérez } 8693cfb4d06SEugenio Pérez 8703cfb4d06SEugenio Pérez r = event_notifier_init(&svq->hdev_call, 0); 8713cfb4d06SEugenio Pérez if (r != 0) { 8723cfb4d06SEugenio Pérez error_setg_errno(errp, -r, "Couldn't create call event notifier"); 8733cfb4d06SEugenio Pérez goto err_init_hdev_call; 8743cfb4d06SEugenio Pérez } 8753cfb4d06SEugenio Pérez 876dff4426fSEugenio Pérez file.fd = event_notifier_get_fd(event_notifier); 877dff4426fSEugenio Pérez r = vhost_vdpa_set_vring_dev_kick(dev, &file); 878dff4426fSEugenio Pérez if (unlikely(r != 0)) { 879dff4426fSEugenio Pérez error_setg_errno(errp, -r, "Can't set device kick fd"); 8803cfb4d06SEugenio Pérez goto err_init_set_dev_fd; 881a8ac8858SEugenio Pérez } 882a8ac8858SEugenio Pérez 883a8ac8858SEugenio Pérez event_notifier = &svq->hdev_call; 884a8ac8858SEugenio Pérez file.fd = event_notifier_get_fd(event_notifier); 885a8ac8858SEugenio Pérez r = vhost_vdpa_set_vring_dev_call(dev, &file); 886a8ac8858SEugenio Pérez if (unlikely(r != 0)) { 887a8ac8858SEugenio Pérez error_setg_errno(errp, -r, "Can't set device call fd"); 8883cfb4d06SEugenio Pérez goto err_init_set_dev_fd; 889dff4426fSEugenio Pérez } 890dff4426fSEugenio Pérez 8913cfb4d06SEugenio Pérez return 0; 8923cfb4d06SEugenio Pérez 8933cfb4d06SEugenio Pérez err_init_set_dev_fd: 8943cfb4d06SEugenio Pérez event_notifier_set_handler(&svq->hdev_call, NULL); 8953cfb4d06SEugenio Pérez 8963cfb4d06SEugenio Pérez err_init_hdev_call: 8973cfb4d06SEugenio Pérez event_notifier_cleanup(&svq->hdev_kick); 8983cfb4d06SEugenio Pérez 8993cfb4d06SEugenio Pérez err_init_hdev_kick: 900100890f7SEugenio Pérez return r; 901100890f7SEugenio Pérez } 902100890f7SEugenio Pérez 903100890f7SEugenio Pérez /** 904100890f7SEugenio Pérez * Unmap a SVQ area in the device 905100890f7SEugenio Pérez */ 9068b6d6119SEugenio Pérez static void vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr addr) 907100890f7SEugenio Pérez { 9088b6d6119SEugenio Pérez const DMAMap needle = { 9098b6d6119SEugenio Pérez .translated_addr = addr, 9108b6d6119SEugenio Pérez }; 9118b6d6119SEugenio Pérez const DMAMap *result = vhost_iova_tree_find_iova(v->iova_tree, &needle); 91234e3c94eSEugenio Pérez hwaddr size; 913100890f7SEugenio Pérez int r; 914100890f7SEugenio Pérez 91534e3c94eSEugenio Pérez if (unlikely(!result)) { 91634e3c94eSEugenio Pérez error_report("Unable to find SVQ address to unmap"); 9175b590f51SEugenio Pérez return; 91834e3c94eSEugenio Pérez } 91934e3c94eSEugenio Pérez 9208e3b0cbbSMarc-André Lureau size = ROUND_UP(result->size, qemu_real_host_page_size()); 921cd831ed5SEugenio Pérez r = vhost_vdpa_dma_unmap(v, v->address_space_id, result->iova, size); 922b37c12beSEugenio Pérez if (unlikely(r < 0)) { 923b37c12beSEugenio Pérez error_report("Unable to unmap SVQ vring: %s (%d)", g_strerror(-r), -r); 9245b590f51SEugenio Pérez return; 925b37c12beSEugenio Pérez } 926b37c12beSEugenio Pérez 927b37c12beSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, *result); 928100890f7SEugenio Pérez } 929100890f7SEugenio Pérez 9305b590f51SEugenio Pérez static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev, 931100890f7SEugenio Pérez const VhostShadowVirtqueue *svq) 932100890f7SEugenio Pérez { 933100890f7SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 934100890f7SEugenio Pérez struct vhost_vring_addr svq_addr; 935100890f7SEugenio Pérez 936100890f7SEugenio Pérez vhost_svq_get_vring_addr(svq, &svq_addr); 937100890f7SEugenio Pérez 9388b6d6119SEugenio Pérez vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr); 939100890f7SEugenio Pérez 9408b6d6119SEugenio Pérez vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr); 94134e3c94eSEugenio Pérez } 94234e3c94eSEugenio Pérez 94334e3c94eSEugenio Pérez /** 94434e3c94eSEugenio Pérez * Map the SVQ area in the device 94534e3c94eSEugenio Pérez * 94634e3c94eSEugenio Pérez * @v: Vhost-vdpa device 94734e3c94eSEugenio Pérez * @needle: The area to search iova 94834e3c94eSEugenio Pérez * @errorp: Error pointer 94934e3c94eSEugenio Pérez */ 95034e3c94eSEugenio Pérez static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle, 95134e3c94eSEugenio Pérez Error **errp) 95234e3c94eSEugenio Pérez { 95334e3c94eSEugenio Pérez int r; 95434e3c94eSEugenio Pérez 95534e3c94eSEugenio Pérez r = vhost_iova_tree_map_alloc(v->iova_tree, needle); 95634e3c94eSEugenio Pérez if (unlikely(r != IOVA_OK)) { 95734e3c94eSEugenio Pérez error_setg(errp, "Cannot allocate iova (%d)", r); 95834e3c94eSEugenio Pérez return false; 95934e3c94eSEugenio Pérez } 96034e3c94eSEugenio Pérez 961cd831ed5SEugenio Pérez r = vhost_vdpa_dma_map(v, v->address_space_id, needle->iova, 962cd831ed5SEugenio Pérez needle->size + 1, 96334e3c94eSEugenio Pérez (void *)(uintptr_t)needle->translated_addr, 96434e3c94eSEugenio Pérez needle->perm == IOMMU_RO); 96534e3c94eSEugenio Pérez if (unlikely(r != 0)) { 96634e3c94eSEugenio Pérez error_setg_errno(errp, -r, "Cannot map region to device"); 96769292a8eSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, *needle); 96834e3c94eSEugenio Pérez } 96934e3c94eSEugenio Pérez 97034e3c94eSEugenio Pérez return r == 0; 971100890f7SEugenio Pérez } 972100890f7SEugenio Pérez 973100890f7SEugenio Pérez /** 974100890f7SEugenio Pérez * Map the shadow virtqueue rings in the device 975100890f7SEugenio Pérez * 976100890f7SEugenio Pérez * @dev: The vhost device 977100890f7SEugenio Pérez * @svq: The shadow virtqueue 978100890f7SEugenio Pérez * @addr: Assigned IOVA addresses 979100890f7SEugenio Pérez * @errp: Error pointer 980100890f7SEugenio Pérez */ 981100890f7SEugenio Pérez static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, 982100890f7SEugenio Pérez const VhostShadowVirtqueue *svq, 983100890f7SEugenio Pérez struct vhost_vring_addr *addr, 984100890f7SEugenio Pérez Error **errp) 985100890f7SEugenio Pérez { 98605e385d2SMarkus Armbruster ERRP_GUARD(); 98734e3c94eSEugenio Pérez DMAMap device_region, driver_region; 98834e3c94eSEugenio Pérez struct vhost_vring_addr svq_addr; 989100890f7SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 990100890f7SEugenio Pérez size_t device_size = vhost_svq_device_area_size(svq); 991100890f7SEugenio Pérez size_t driver_size = vhost_svq_driver_area_size(svq); 99234e3c94eSEugenio Pérez size_t avail_offset; 99334e3c94eSEugenio Pérez bool ok; 994100890f7SEugenio Pérez 99534e3c94eSEugenio Pérez vhost_svq_get_vring_addr(svq, &svq_addr); 996100890f7SEugenio Pérez 99734e3c94eSEugenio Pérez driver_region = (DMAMap) { 99834e3c94eSEugenio Pérez .translated_addr = svq_addr.desc_user_addr, 99934e3c94eSEugenio Pérez .size = driver_size - 1, 100034e3c94eSEugenio Pérez .perm = IOMMU_RO, 100134e3c94eSEugenio Pérez }; 100234e3c94eSEugenio Pérez ok = vhost_vdpa_svq_map_ring(v, &driver_region, errp); 100334e3c94eSEugenio Pérez if (unlikely(!ok)) { 100434e3c94eSEugenio Pérez error_prepend(errp, "Cannot create vq driver region: "); 1005100890f7SEugenio Pérez return false; 1006100890f7SEugenio Pérez } 100734e3c94eSEugenio Pérez addr->desc_user_addr = driver_region.iova; 100834e3c94eSEugenio Pérez avail_offset = svq_addr.avail_user_addr - svq_addr.desc_user_addr; 100934e3c94eSEugenio Pérez addr->avail_user_addr = driver_region.iova + avail_offset; 1010100890f7SEugenio Pérez 101134e3c94eSEugenio Pérez device_region = (DMAMap) { 101234e3c94eSEugenio Pérez .translated_addr = svq_addr.used_user_addr, 101334e3c94eSEugenio Pérez .size = device_size - 1, 101434e3c94eSEugenio Pérez .perm = IOMMU_RW, 101534e3c94eSEugenio Pérez }; 101634e3c94eSEugenio Pérez ok = vhost_vdpa_svq_map_ring(v, &device_region, errp); 101734e3c94eSEugenio Pérez if (unlikely(!ok)) { 101834e3c94eSEugenio Pérez error_prepend(errp, "Cannot create vq device region: "); 10198b6d6119SEugenio Pérez vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr); 1020100890f7SEugenio Pérez } 102134e3c94eSEugenio Pérez addr->used_user_addr = device_region.iova; 1022100890f7SEugenio Pérez 102334e3c94eSEugenio Pérez return ok; 1024100890f7SEugenio Pérez } 1025100890f7SEugenio Pérez 1026100890f7SEugenio Pérez static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, 1027100890f7SEugenio Pérez VhostShadowVirtqueue *svq, unsigned idx, 1028100890f7SEugenio Pérez Error **errp) 1029100890f7SEugenio Pérez { 1030100890f7SEugenio Pérez uint16_t vq_index = dev->vq_index + idx; 1031100890f7SEugenio Pérez struct vhost_vring_state s = { 1032100890f7SEugenio Pérez .index = vq_index, 1033100890f7SEugenio Pérez }; 1034100890f7SEugenio Pérez int r; 1035100890f7SEugenio Pérez 1036100890f7SEugenio Pérez r = vhost_vdpa_set_dev_vring_base(dev, &s); 1037100890f7SEugenio Pérez if (unlikely(r)) { 1038100890f7SEugenio Pérez error_setg_errno(errp, -r, "Cannot set vring base"); 1039100890f7SEugenio Pérez return false; 1040100890f7SEugenio Pérez } 1041100890f7SEugenio Pérez 1042100890f7SEugenio Pérez r = vhost_vdpa_svq_set_fds(dev, svq, idx, errp); 1043dff4426fSEugenio Pérez return r == 0; 1044dff4426fSEugenio Pérez } 1045dff4426fSEugenio Pérez 1046dff4426fSEugenio Pérez static bool vhost_vdpa_svqs_start(struct vhost_dev *dev) 1047dff4426fSEugenio Pérez { 1048dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1049dff4426fSEugenio Pérez Error *err = NULL; 1050dff4426fSEugenio Pérez unsigned i; 1051dff4426fSEugenio Pérez 1052712c1a31SEugenio Pérez if (!v->shadow_vqs_enabled) { 1053dff4426fSEugenio Pérez return true; 1054dff4426fSEugenio Pérez } 1055dff4426fSEugenio Pérez 1056dff4426fSEugenio Pérez for (i = 0; i < v->shadow_vqs->len; ++i) { 1057100890f7SEugenio Pérez VirtQueue *vq = virtio_get_queue(dev->vdev, dev->vq_index + i); 1058dff4426fSEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i); 1059100890f7SEugenio Pérez struct vhost_vring_addr addr = { 10601c82fdfeSEugenio Pérez .index = dev->vq_index + i, 1061100890f7SEugenio Pérez }; 1062100890f7SEugenio Pérez int r; 1063dff4426fSEugenio Pérez bool ok = vhost_vdpa_svq_setup(dev, svq, i, &err); 1064dff4426fSEugenio Pérez if (unlikely(!ok)) { 1065100890f7SEugenio Pérez goto err; 1066100890f7SEugenio Pérez } 1067100890f7SEugenio Pérez 10685fde952bSEugenio Pérez vhost_svq_start(svq, dev->vdev, vq, v->iova_tree); 1069100890f7SEugenio Pérez ok = vhost_vdpa_svq_map_rings(dev, svq, &addr, &err); 1070100890f7SEugenio Pérez if (unlikely(!ok)) { 1071100890f7SEugenio Pérez goto err_map; 1072100890f7SEugenio Pérez } 1073100890f7SEugenio Pérez 1074100890f7SEugenio Pérez /* Override vring GPA set by vhost subsystem */ 1075100890f7SEugenio Pérez r = vhost_vdpa_set_vring_dev_addr(dev, &addr); 1076100890f7SEugenio Pérez if (unlikely(r != 0)) { 1077100890f7SEugenio Pérez error_setg_errno(&err, -r, "Cannot set device address"); 1078100890f7SEugenio Pérez goto err_set_addr; 1079100890f7SEugenio Pérez } 1080100890f7SEugenio Pérez } 1081100890f7SEugenio Pérez 1082100890f7SEugenio Pérez return true; 1083100890f7SEugenio Pérez 1084100890f7SEugenio Pérez err_set_addr: 1085100890f7SEugenio Pérez vhost_vdpa_svq_unmap_rings(dev, g_ptr_array_index(v->shadow_vqs, i)); 1086100890f7SEugenio Pérez 1087100890f7SEugenio Pérez err_map: 1088100890f7SEugenio Pérez vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, i)); 1089100890f7SEugenio Pérez 1090100890f7SEugenio Pérez err: 1091dff4426fSEugenio Pérez error_reportf_err(err, "Cannot setup SVQ %u: ", i); 1092100890f7SEugenio Pérez for (unsigned j = 0; j < i; ++j) { 1093100890f7SEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, j); 1094100890f7SEugenio Pérez vhost_vdpa_svq_unmap_rings(dev, svq); 1095100890f7SEugenio Pérez vhost_svq_stop(svq); 1096100890f7SEugenio Pérez } 1097100890f7SEugenio Pérez 1098100890f7SEugenio Pérez return false; 1099100890f7SEugenio Pérez } 1100100890f7SEugenio Pérez 11015b590f51SEugenio Pérez static void vhost_vdpa_svqs_stop(struct vhost_dev *dev) 1102100890f7SEugenio Pérez { 1103100890f7SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1104100890f7SEugenio Pérez 1105712c1a31SEugenio Pérez if (!v->shadow_vqs_enabled) { 11065b590f51SEugenio Pérez return; 1107100890f7SEugenio Pérez } 1108100890f7SEugenio Pérez 1109100890f7SEugenio Pérez for (unsigned i = 0; i < v->shadow_vqs->len; ++i) { 1110100890f7SEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i); 11112e1a9de9SEugenio Pérez 11122e1a9de9SEugenio Pérez vhost_svq_stop(svq); 11135b590f51SEugenio Pérez vhost_vdpa_svq_unmap_rings(dev, svq); 11143cfb4d06SEugenio Pérez 11153cfb4d06SEugenio Pérez event_notifier_cleanup(&svq->hdev_kick); 11163cfb4d06SEugenio Pérez event_notifier_cleanup(&svq->hdev_call); 1117dff4426fSEugenio Pérez } 1118dff4426fSEugenio Pérez } 1119dff4426fSEugenio Pérez 11200bb302a9SEugenio Pérez static void vhost_vdpa_suspend(struct vhost_dev *dev) 11210bb302a9SEugenio Pérez { 11220bb302a9SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 11230bb302a9SEugenio Pérez int r; 11240bb302a9SEugenio Pérez 11250bb302a9SEugenio Pérez if (!vhost_vdpa_first_dev(dev)) { 11260bb302a9SEugenio Pérez return; 11270bb302a9SEugenio Pérez } 11280bb302a9SEugenio Pérez 11290bb302a9SEugenio Pérez if (dev->backend_cap & BIT_ULL(VHOST_BACKEND_F_SUSPEND)) { 11300bb302a9SEugenio Pérez trace_vhost_vdpa_suspend(dev); 11310bb302a9SEugenio Pérez r = ioctl(v->device_fd, VHOST_VDPA_SUSPEND); 11320bb302a9SEugenio Pérez if (unlikely(r)) { 11330bb302a9SEugenio Pérez error_report("Cannot suspend: %s(%d)", g_strerror(errno), errno); 11340bb302a9SEugenio Pérez } else { 11350bb302a9SEugenio Pérez v->suspended = true; 11360bb302a9SEugenio Pérez return; 11370bb302a9SEugenio Pérez } 11380bb302a9SEugenio Pérez } 11390bb302a9SEugenio Pérez 11400bb302a9SEugenio Pérez vhost_vdpa_reset_device(dev); 11410bb302a9SEugenio Pérez } 11420bb302a9SEugenio Pérez 1143108a6481SCindy Lu static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) 1144108a6481SCindy Lu { 1145108a6481SCindy Lu struct vhost_vdpa *v = dev->opaque; 1146dff4426fSEugenio Pérez bool ok; 1147778e67deSLaurent Vivier trace_vhost_vdpa_dev_start(dev, started); 11484d191cfdSJason Wang 11494d191cfdSJason Wang if (started) { 11504d191cfdSJason Wang vhost_vdpa_host_notifiers_init(dev); 1151dff4426fSEugenio Pérez ok = vhost_vdpa_svqs_start(dev); 1152dff4426fSEugenio Pérez if (unlikely(!ok)) { 1153dff4426fSEugenio Pérez return -1; 1154dff4426fSEugenio Pérez } 11554d191cfdSJason Wang vhost_vdpa_set_vring_ready(dev); 11564d191cfdSJason Wang } else { 11570bb302a9SEugenio Pérez vhost_vdpa_suspend(dev); 11585b590f51SEugenio Pérez vhost_vdpa_svqs_stop(dev); 11594d191cfdSJason Wang vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); 11604d191cfdSJason Wang } 11614d191cfdSJason Wang 1162245cf2c2SEugenio Pérez if (dev->vq_index + dev->nvqs != dev->vq_index_end) { 11634d191cfdSJason Wang return 0; 11644d191cfdSJason Wang } 11654d191cfdSJason Wang 1166108a6481SCindy Lu if (started) { 1167108a6481SCindy Lu memory_listener_register(&v->listener, &address_space_memory); 11683631151bSRoman Kagan return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK); 1169c3716f26SEugenio Pérez } 1170c3716f26SEugenio Pérez 1171c3716f26SEugenio Pérez return 0; 1172c3716f26SEugenio Pérez } 1173c3716f26SEugenio Pérez 1174c3716f26SEugenio Pérez static void vhost_vdpa_reset_status(struct vhost_dev *dev) 1175c3716f26SEugenio Pérez { 1176c3716f26SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1177c3716f26SEugenio Pérez 1178c3716f26SEugenio Pérez if (dev->vq_index + dev->nvqs != dev->vq_index_end) { 1179c3716f26SEugenio Pérez return; 1180c3716f26SEugenio Pérez } 1181c3716f26SEugenio Pérez 1182108a6481SCindy Lu vhost_vdpa_reset_device(dev); 1183108a6481SCindy Lu vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | 1184108a6481SCindy Lu VIRTIO_CONFIG_S_DRIVER); 1185108a6481SCindy Lu memory_listener_unregister(&v->listener); 1186108a6481SCindy Lu } 1187108a6481SCindy Lu 1188108a6481SCindy Lu static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base, 1189108a6481SCindy Lu struct vhost_log *log) 1190108a6481SCindy Lu { 1191773ebc95SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1192d71b0609SSi-Wei Liu if (v->shadow_vqs_enabled || !vhost_vdpa_first_dev(dev)) { 11934d191cfdSJason Wang return 0; 11944d191cfdSJason Wang } 11954d191cfdSJason Wang 1196778e67deSLaurent Vivier trace_vhost_vdpa_set_log_base(dev, base, log->size, log->refcnt, log->fd, 1197778e67deSLaurent Vivier log->log); 1198108a6481SCindy Lu return vhost_vdpa_call(dev, VHOST_SET_LOG_BASE, &base); 1199108a6481SCindy Lu } 1200108a6481SCindy Lu 1201108a6481SCindy Lu static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev, 1202108a6481SCindy Lu struct vhost_vring_addr *addr) 1203108a6481SCindy Lu { 1204d96be4c8SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1205d96be4c8SEugenio Pérez 1206d96be4c8SEugenio Pérez if (v->shadow_vqs_enabled) { 1207d96be4c8SEugenio Pérez /* 1208d96be4c8SEugenio Pérez * Device vring addr was set at device start. SVQ base is handled by 1209d96be4c8SEugenio Pérez * VirtQueue code. 1210d96be4c8SEugenio Pérez */ 1211d96be4c8SEugenio Pérez return 0; 1212d96be4c8SEugenio Pérez } 1213d96be4c8SEugenio Pérez 1214d96be4c8SEugenio Pérez return vhost_vdpa_set_vring_dev_addr(dev, addr); 1215108a6481SCindy Lu } 1216108a6481SCindy Lu 1217108a6481SCindy Lu static int vhost_vdpa_set_vring_num(struct vhost_dev *dev, 1218108a6481SCindy Lu struct vhost_vring_state *ring) 1219108a6481SCindy Lu { 1220778e67deSLaurent Vivier trace_vhost_vdpa_set_vring_num(dev, ring->index, ring->num); 1221108a6481SCindy Lu return vhost_vdpa_call(dev, VHOST_SET_VRING_NUM, ring); 1222108a6481SCindy Lu } 1223108a6481SCindy Lu 1224108a6481SCindy Lu static int vhost_vdpa_set_vring_base(struct vhost_dev *dev, 1225108a6481SCindy Lu struct vhost_vring_state *ring) 1226108a6481SCindy Lu { 1227d96be4c8SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1228d96be4c8SEugenio Pérez 1229d96be4c8SEugenio Pérez if (v->shadow_vqs_enabled) { 1230d96be4c8SEugenio Pérez /* 1231d96be4c8SEugenio Pérez * Device vring base was set at device start. SVQ base is handled by 1232d96be4c8SEugenio Pérez * VirtQueue code. 1233d96be4c8SEugenio Pérez */ 1234d96be4c8SEugenio Pérez return 0; 1235d96be4c8SEugenio Pérez } 1236d96be4c8SEugenio Pérez 1237d96be4c8SEugenio Pérez return vhost_vdpa_set_dev_vring_base(dev, ring); 1238108a6481SCindy Lu } 1239108a6481SCindy Lu 1240108a6481SCindy Lu static int vhost_vdpa_get_vring_base(struct vhost_dev *dev, 1241108a6481SCindy Lu struct vhost_vring_state *ring) 1242108a6481SCindy Lu { 12436d0b2226SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1244778e67deSLaurent Vivier int ret; 1245778e67deSLaurent Vivier 12466d0b2226SEugenio Pérez if (v->shadow_vqs_enabled) { 12472fdac348SEugenio Pérez ring->num = virtio_queue_get_last_avail_idx(dev->vdev, ring->index); 12486d0b2226SEugenio Pérez return 0; 12496d0b2226SEugenio Pérez } 12506d0b2226SEugenio Pérez 1251b6662cb7SEugenio Pérez if (!v->suspended) { 1252b6662cb7SEugenio Pérez /* 1253b6662cb7SEugenio Pérez * Cannot trust in value returned by device, let vhost recover used 1254b6662cb7SEugenio Pérez * idx from guest. 1255b6662cb7SEugenio Pérez */ 1256b6662cb7SEugenio Pérez return -1; 1257b6662cb7SEugenio Pérez } 1258b6662cb7SEugenio Pérez 1259778e67deSLaurent Vivier ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring); 1260778e67deSLaurent Vivier trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num); 1261778e67deSLaurent Vivier return ret; 1262108a6481SCindy Lu } 1263108a6481SCindy Lu 1264108a6481SCindy Lu static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, 1265108a6481SCindy Lu struct vhost_vring_file *file) 1266108a6481SCindy Lu { 1267dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1268dff4426fSEugenio Pérez int vdpa_idx = file->index - dev->vq_index; 1269dff4426fSEugenio Pérez 1270dff4426fSEugenio Pérez if (v->shadow_vqs_enabled) { 1271dff4426fSEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx); 1272dff4426fSEugenio Pérez vhost_svq_set_svq_kick_fd(svq, file->fd); 1273dff4426fSEugenio Pérez return 0; 1274dff4426fSEugenio Pérez } else { 1275dff4426fSEugenio Pérez return vhost_vdpa_set_vring_dev_kick(dev, file); 1276dff4426fSEugenio Pérez } 1277108a6481SCindy Lu } 1278108a6481SCindy Lu 1279108a6481SCindy Lu static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, 1280108a6481SCindy Lu struct vhost_vring_file *file) 1281108a6481SCindy Lu { 1282a8ac8858SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1283a8ac8858SEugenio Pérez int vdpa_idx = file->index - dev->vq_index; 1284a8ac8858SEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx); 1285a8ac8858SEugenio Pérez 1286b2765243SEugenio Pérez /* Remember last call fd because we can switch to SVQ anytime. */ 1287a8ac8858SEugenio Pérez vhost_svq_set_svq_call_fd(svq, file->fd); 1288b2765243SEugenio Pérez if (v->shadow_vqs_enabled) { 1289a8ac8858SEugenio Pérez return 0; 1290a8ac8858SEugenio Pérez } 1291b2765243SEugenio Pérez 1292b2765243SEugenio Pérez return vhost_vdpa_set_vring_dev_call(dev, file); 1293108a6481SCindy Lu } 1294108a6481SCindy Lu 1295108a6481SCindy Lu static int vhost_vdpa_get_features(struct vhost_dev *dev, 1296108a6481SCindy Lu uint64_t *features) 1297108a6481SCindy Lu { 129812a195faSEugenio Pérez int ret = vhost_vdpa_get_dev_features(dev, features); 1299778e67deSLaurent Vivier 1300ab7337e3SEugenio Pérez if (ret == 0) { 130112a195faSEugenio Pérez /* Add SVQ logging capabilities */ 130212a195faSEugenio Pérez *features |= BIT_ULL(VHOST_F_LOG_ALL); 130312a195faSEugenio Pérez } 130412a195faSEugenio Pérez 1305778e67deSLaurent Vivier return ret; 1306108a6481SCindy Lu } 1307108a6481SCindy Lu 1308108a6481SCindy Lu static int vhost_vdpa_set_owner(struct vhost_dev *dev) 1309108a6481SCindy Lu { 1310d71b0609SSi-Wei Liu if (!vhost_vdpa_first_dev(dev)) { 13114d191cfdSJason Wang return 0; 13124d191cfdSJason Wang } 13134d191cfdSJason Wang 1314778e67deSLaurent Vivier trace_vhost_vdpa_set_owner(dev); 1315108a6481SCindy Lu return vhost_vdpa_call(dev, VHOST_SET_OWNER, NULL); 1316108a6481SCindy Lu } 1317108a6481SCindy Lu 1318108a6481SCindy Lu static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev, 1319108a6481SCindy Lu struct vhost_vring_addr *addr, struct vhost_virtqueue *vq) 1320108a6481SCindy Lu { 1321108a6481SCindy Lu assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 1322108a6481SCindy Lu addr->desc_user_addr = (uint64_t)(unsigned long)vq->desc_phys; 1323108a6481SCindy Lu addr->avail_user_addr = (uint64_t)(unsigned long)vq->avail_phys; 1324108a6481SCindy Lu addr->used_user_addr = (uint64_t)(unsigned long)vq->used_phys; 1325778e67deSLaurent Vivier trace_vhost_vdpa_vq_get_addr(dev, vq, addr->desc_user_addr, 1326778e67deSLaurent Vivier addr->avail_user_addr, addr->used_user_addr); 1327108a6481SCindy Lu return 0; 1328108a6481SCindy Lu } 1329108a6481SCindy Lu 1330108a6481SCindy Lu static bool vhost_vdpa_force_iommu(struct vhost_dev *dev) 1331108a6481SCindy Lu { 1332108a6481SCindy Lu return true; 1333108a6481SCindy Lu } 1334108a6481SCindy Lu 1335108a6481SCindy Lu const VhostOps vdpa_ops = { 1336108a6481SCindy Lu .backend_type = VHOST_BACKEND_TYPE_VDPA, 1337108a6481SCindy Lu .vhost_backend_init = vhost_vdpa_init, 1338108a6481SCindy Lu .vhost_backend_cleanup = vhost_vdpa_cleanup, 1339108a6481SCindy Lu .vhost_set_log_base = vhost_vdpa_set_log_base, 1340108a6481SCindy Lu .vhost_set_vring_addr = vhost_vdpa_set_vring_addr, 1341108a6481SCindy Lu .vhost_set_vring_num = vhost_vdpa_set_vring_num, 1342108a6481SCindy Lu .vhost_set_vring_base = vhost_vdpa_set_vring_base, 1343108a6481SCindy Lu .vhost_get_vring_base = vhost_vdpa_get_vring_base, 1344108a6481SCindy Lu .vhost_set_vring_kick = vhost_vdpa_set_vring_kick, 1345108a6481SCindy Lu .vhost_set_vring_call = vhost_vdpa_set_vring_call, 1346108a6481SCindy Lu .vhost_get_features = vhost_vdpa_get_features, 1347a5bd0580SJason Wang .vhost_set_backend_cap = vhost_vdpa_set_backend_cap, 1348108a6481SCindy Lu .vhost_set_owner = vhost_vdpa_set_owner, 1349108a6481SCindy Lu .vhost_set_vring_endian = NULL, 1350108a6481SCindy Lu .vhost_backend_memslots_limit = vhost_vdpa_memslots_limit, 1351108a6481SCindy Lu .vhost_set_mem_table = vhost_vdpa_set_mem_table, 1352108a6481SCindy Lu .vhost_set_features = vhost_vdpa_set_features, 1353108a6481SCindy Lu .vhost_reset_device = vhost_vdpa_reset_device, 1354108a6481SCindy Lu .vhost_get_vq_index = vhost_vdpa_get_vq_index, 1355108a6481SCindy Lu .vhost_get_config = vhost_vdpa_get_config, 1356108a6481SCindy Lu .vhost_set_config = vhost_vdpa_set_config, 1357108a6481SCindy Lu .vhost_requires_shm_log = NULL, 1358108a6481SCindy Lu .vhost_migration_done = NULL, 1359108a6481SCindy Lu .vhost_backend_can_merge = NULL, 1360108a6481SCindy Lu .vhost_net_set_mtu = NULL, 1361108a6481SCindy Lu .vhost_set_iotlb_callback = NULL, 1362108a6481SCindy Lu .vhost_send_device_iotlb_msg = NULL, 1363108a6481SCindy Lu .vhost_dev_start = vhost_vdpa_dev_start, 1364108a6481SCindy Lu .vhost_get_device_id = vhost_vdpa_get_device_id, 1365108a6481SCindy Lu .vhost_vq_get_addr = vhost_vdpa_vq_get_addr, 1366108a6481SCindy Lu .vhost_force_iommu = vhost_vdpa_force_iommu, 1367259f3accSCindy Lu .vhost_set_config_call = vhost_vdpa_set_config_call, 1368c3716f26SEugenio Pérez .vhost_reset_status = vhost_vdpa_reset_status, 1369108a6481SCindy Lu }; 1370