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 291778e67deSLaurent Vivier trace_vhost_vdpa_listener_region_del(v, iova, int128_get64(llend)); 292778e67deSLaurent Vivier 293108a6481SCindy Lu if (int128_ge(int128_make64(iova), llend)) { 294108a6481SCindy Lu return; 295108a6481SCindy Lu } 296108a6481SCindy Lu 297108a6481SCindy Lu llsize = int128_sub(llend, int128_make64(iova)); 298108a6481SCindy Lu 2996188d78aSEugenio Pérez if (v->shadow_data) { 30034e3c94eSEugenio Pérez const DMAMap *result; 30134e3c94eSEugenio Pérez const void *vaddr = memory_region_get_ram_ptr(section->mr) + 30234e3c94eSEugenio Pérez section->offset_within_region + 30334e3c94eSEugenio Pérez (iova - section->offset_within_address_space); 30434e3c94eSEugenio Pérez DMAMap mem_region = { 30534e3c94eSEugenio Pérez .translated_addr = (hwaddr)(uintptr_t)vaddr, 30634e3c94eSEugenio Pérez .size = int128_get64(llsize) - 1, 30734e3c94eSEugenio Pérez }; 30834e3c94eSEugenio Pérez 30934e3c94eSEugenio Pérez result = vhost_iova_tree_find_iova(v->iova_tree, &mem_region); 31010dab9f2SEugenio Pérez if (!result) { 31110dab9f2SEugenio Pérez /* The memory listener map wasn't mapped */ 31210dab9f2SEugenio Pérez return; 31310dab9f2SEugenio Pérez } 31434e3c94eSEugenio Pérez iova = result->iova; 31569292a8eSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, *result); 31634e3c94eSEugenio Pérez } 317e6db5df7SEugenio Pérez vhost_vdpa_iotlb_batch_begin_once(v); 318cd831ed5SEugenio Pérez ret = vhost_vdpa_dma_unmap(v, VHOST_VDPA_GUEST_PA_ASID, iova, 319cd831ed5SEugenio Pérez int128_get64(llsize)); 320108a6481SCindy Lu if (ret) { 321108a6481SCindy Lu error_report("vhost_vdpa dma unmap error!"); 322108a6481SCindy Lu } 323108a6481SCindy Lu 324108a6481SCindy Lu memory_region_unref(section->mr); 325108a6481SCindy Lu } 326108a6481SCindy Lu /* 327ef4ff56cSStefano Garzarella * IOTLB API is used by vhost-vdpa which requires incremental updating 328108a6481SCindy Lu * of the mapping. So we can not use generic vhost memory listener which 329108a6481SCindy Lu * depends on the addnop(). 330108a6481SCindy Lu */ 331108a6481SCindy Lu static const MemoryListener vhost_vdpa_memory_listener = { 332142518bdSPeter Xu .name = "vhost-vdpa", 333a5bd0580SJason Wang .commit = vhost_vdpa_listener_commit, 334108a6481SCindy Lu .region_add = vhost_vdpa_listener_region_add, 335108a6481SCindy Lu .region_del = vhost_vdpa_listener_region_del, 336108a6481SCindy Lu }; 337108a6481SCindy Lu 338108a6481SCindy Lu static int vhost_vdpa_call(struct vhost_dev *dev, unsigned long int request, 339108a6481SCindy Lu void *arg) 340108a6481SCindy Lu { 341108a6481SCindy Lu struct vhost_vdpa *v = dev->opaque; 342108a6481SCindy Lu int fd = v->device_fd; 343f2a6e6c4SKevin Wolf int ret; 344108a6481SCindy Lu 345108a6481SCindy Lu assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 346108a6481SCindy Lu 347f2a6e6c4SKevin Wolf ret = ioctl(fd, request, arg); 348f2a6e6c4SKevin Wolf return ret < 0 ? -errno : ret; 349108a6481SCindy Lu } 350108a6481SCindy Lu 3513631151bSRoman Kagan static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status) 352108a6481SCindy Lu { 353108a6481SCindy Lu uint8_t s; 3543631151bSRoman Kagan int ret; 355108a6481SCindy Lu 356778e67deSLaurent Vivier trace_vhost_vdpa_add_status(dev, status); 3573631151bSRoman Kagan ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s); 3583631151bSRoman Kagan if (ret < 0) { 3593631151bSRoman Kagan return ret; 360108a6481SCindy Lu } 361108a6481SCindy Lu 362108a6481SCindy Lu s |= status; 363108a6481SCindy Lu 3643631151bSRoman Kagan ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &s); 3653631151bSRoman Kagan if (ret < 0) { 3663631151bSRoman Kagan return ret; 3673631151bSRoman Kagan } 3683631151bSRoman Kagan 3693631151bSRoman Kagan ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s); 3703631151bSRoman Kagan if (ret < 0) { 3713631151bSRoman Kagan return ret; 3723631151bSRoman Kagan } 3733631151bSRoman Kagan 3743631151bSRoman Kagan if (!(s & status)) { 3753631151bSRoman Kagan return -EIO; 3763631151bSRoman Kagan } 3773631151bSRoman Kagan 3783631151bSRoman Kagan return 0; 379108a6481SCindy Lu } 380108a6481SCindy Lu 381c672f348SLongpeng int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_range) 382c672f348SLongpeng { 383c672f348SLongpeng int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range); 384c672f348SLongpeng 385c672f348SLongpeng return ret < 0 ? -errno : 0; 386c672f348SLongpeng } 387c672f348SLongpeng 388d71b0609SSi-Wei Liu /* 389d71b0609SSi-Wei Liu * The use of this function is for requests that only need to be 390d71b0609SSi-Wei Liu * applied once. Typically such request occurs at the beginning 391d71b0609SSi-Wei Liu * of operation, and before setting up queues. It should not be 392d71b0609SSi-Wei Liu * used for request that performs operation until all queues are 393d71b0609SSi-Wei Liu * set, which would need to check dev->vq_index_end instead. 394d71b0609SSi-Wei Liu */ 395d71b0609SSi-Wei Liu static bool vhost_vdpa_first_dev(struct vhost_dev *dev) 3964d191cfdSJason Wang { 3974d191cfdSJason Wang struct vhost_vdpa *v = dev->opaque; 3984d191cfdSJason Wang 399d71b0609SSi-Wei Liu return v->index == 0; 4004d191cfdSJason Wang } 4014d191cfdSJason Wang 40212a195faSEugenio Pérez static int vhost_vdpa_get_dev_features(struct vhost_dev *dev, 40312a195faSEugenio Pérez uint64_t *features) 40412a195faSEugenio Pérez { 40512a195faSEugenio Pérez int ret; 40612a195faSEugenio Pérez 40712a195faSEugenio Pérez ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features); 40812a195faSEugenio Pérez trace_vhost_vdpa_get_features(dev, *features); 40912a195faSEugenio Pérez return ret; 41012a195faSEugenio Pérez } 41112a195faSEugenio Pérez 412258a0394SEugenio Pérez static void vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v) 413dff4426fSEugenio Pérez { 414dff4426fSEugenio Pérez g_autoptr(GPtrArray) shadow_vqs = NULL; 4154725a418SEugenio Pérez 416dff4426fSEugenio Pérez shadow_vqs = g_ptr_array_new_full(hdev->nvqs, vhost_svq_free); 417dff4426fSEugenio Pérez for (unsigned n = 0; n < hdev->nvqs; ++n) { 4183cfb4d06SEugenio Pérez VhostShadowVirtqueue *svq; 419dff4426fSEugenio Pérez 4205fde952bSEugenio Pérez svq = vhost_svq_new(v->shadow_vq_ops, v->shadow_vq_ops_opaque); 4213cfb4d06SEugenio Pérez g_ptr_array_add(shadow_vqs, svq); 422dff4426fSEugenio Pérez } 423dff4426fSEugenio Pérez 424dff4426fSEugenio Pérez v->shadow_vqs = g_steal_pointer(&shadow_vqs); 425dff4426fSEugenio Pérez } 426dff4426fSEugenio Pérez 42728770ff9SKevin Wolf static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) 428108a6481SCindy Lu { 429108a6481SCindy Lu struct vhost_vdpa *v; 430108a6481SCindy Lu assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 431778e67deSLaurent Vivier trace_vhost_vdpa_init(dev, opaque); 432e1c1915bSDavid Hildenbrand int ret; 433e1c1915bSDavid Hildenbrand 434108a6481SCindy Lu v = opaque; 435a5bd0580SJason Wang v->dev = dev; 436108a6481SCindy Lu dev->opaque = opaque ; 437108a6481SCindy Lu v->listener = vhost_vdpa_memory_listener; 438108a6481SCindy Lu v->msg_type = VHOST_IOTLB_MSG_V2; 439258a0394SEugenio Pérez vhost_vdpa_init_svq(dev, v); 440108a6481SCindy Lu 4419c363cf6SEugenio Pérez error_propagate(&dev->migration_blocker, v->migration_blocker); 442d71b0609SSi-Wei Liu if (!vhost_vdpa_first_dev(dev)) { 4434d191cfdSJason Wang return 0; 4444d191cfdSJason Wang } 4454d191cfdSJason Wang 446a230c471SEugenio Pérez /* 447*57ac8318SEugenio Pérez * If dev->shadow_vqs_enabled at initialization that means the device has 448*57ac8318SEugenio Pérez * been started with x-svq=on, so don't block migration 449*57ac8318SEugenio Pérez */ 450*57ac8318SEugenio Pérez if (dev->migration_blocker == NULL && !v->shadow_vqs_enabled) { 451*57ac8318SEugenio Pérez /* We don't have dev->features yet */ 452*57ac8318SEugenio Pérez uint64_t features; 453*57ac8318SEugenio Pérez ret = vhost_vdpa_get_dev_features(dev, &features); 454*57ac8318SEugenio Pérez if (unlikely(ret)) { 455*57ac8318SEugenio Pérez error_setg_errno(errp, -ret, "Could not get device features"); 456*57ac8318SEugenio Pérez return ret; 457*57ac8318SEugenio Pérez } 458*57ac8318SEugenio Pérez vhost_svq_valid_features(features, &dev->migration_blocker); 459*57ac8318SEugenio Pérez } 460*57ac8318SEugenio Pérez 461*57ac8318SEugenio Pérez /* 462a230c471SEugenio Pérez * Similar to VFIO, we end up pinning all guest memory and have to 463a230c471SEugenio Pérez * disable discarding of RAM. 464a230c471SEugenio Pérez */ 465a230c471SEugenio Pérez ret = ram_block_discard_disable(true); 466a230c471SEugenio Pérez if (ret) { 467a230c471SEugenio Pérez error_report("Cannot set discarding of RAM broken"); 468a230c471SEugenio Pérez return ret; 469a230c471SEugenio Pérez } 470a230c471SEugenio Pérez 471108a6481SCindy Lu vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | 472108a6481SCindy Lu VIRTIO_CONFIG_S_DRIVER); 473108a6481SCindy Lu 474108a6481SCindy Lu return 0; 475108a6481SCindy Lu } 476108a6481SCindy Lu 477d0416d48SJason Wang static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev, 478d0416d48SJason Wang int queue_index) 479d0416d48SJason Wang { 4808e3b0cbbSMarc-André Lureau size_t page_size = qemu_real_host_page_size(); 481d0416d48SJason Wang struct vhost_vdpa *v = dev->opaque; 482d0416d48SJason Wang VirtIODevice *vdev = dev->vdev; 483d0416d48SJason Wang VhostVDPAHostNotifier *n; 484d0416d48SJason Wang 485d0416d48SJason Wang n = &v->notifier[queue_index]; 486d0416d48SJason Wang 487d0416d48SJason Wang if (n->addr) { 488d0416d48SJason Wang virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, false); 489d0416d48SJason Wang object_unparent(OBJECT(&n->mr)); 490d0416d48SJason Wang munmap(n->addr, page_size); 491d0416d48SJason Wang n->addr = NULL; 492d0416d48SJason Wang } 493d0416d48SJason Wang } 494d0416d48SJason Wang 495d0416d48SJason Wang static int vhost_vdpa_host_notifier_init(struct vhost_dev *dev, int queue_index) 496d0416d48SJason Wang { 4978e3b0cbbSMarc-André Lureau size_t page_size = qemu_real_host_page_size(); 498d0416d48SJason Wang struct vhost_vdpa *v = dev->opaque; 499d0416d48SJason Wang VirtIODevice *vdev = dev->vdev; 500d0416d48SJason Wang VhostVDPAHostNotifier *n; 501d0416d48SJason Wang int fd = v->device_fd; 502d0416d48SJason Wang void *addr; 503d0416d48SJason Wang char *name; 504d0416d48SJason Wang 505d0416d48SJason Wang vhost_vdpa_host_notifier_uninit(dev, queue_index); 506d0416d48SJason Wang 507d0416d48SJason Wang n = &v->notifier[queue_index]; 508d0416d48SJason Wang 509d0416d48SJason Wang addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd, 510d0416d48SJason Wang queue_index * page_size); 511d0416d48SJason Wang if (addr == MAP_FAILED) { 512d0416d48SJason Wang goto err; 513d0416d48SJason Wang } 514d0416d48SJason Wang 515d0416d48SJason Wang name = g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]", 516d0416d48SJason Wang v, queue_index); 517d0416d48SJason Wang memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name, 518d0416d48SJason Wang page_size, addr); 519d0416d48SJason Wang g_free(name); 520d0416d48SJason Wang 521d0416d48SJason Wang if (virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, true)) { 52298f7607eSLaurent Vivier object_unparent(OBJECT(&n->mr)); 523d0416d48SJason Wang munmap(addr, page_size); 524d0416d48SJason Wang goto err; 525d0416d48SJason Wang } 526d0416d48SJason Wang n->addr = addr; 527d0416d48SJason Wang 528d0416d48SJason Wang return 0; 529d0416d48SJason Wang 530d0416d48SJason Wang err: 531d0416d48SJason Wang return -1; 532d0416d48SJason Wang } 533d0416d48SJason Wang 534b1f030a0SLaurent Vivier static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n) 535b1f030a0SLaurent Vivier { 536b1f030a0SLaurent Vivier int i; 537b1f030a0SLaurent Vivier 538e66f2311SLongpeng /* 539e66f2311SLongpeng * Pack all the changes to the memory regions in a single 540e66f2311SLongpeng * transaction to avoid a few updating of the address space 541e66f2311SLongpeng * topology. 542e66f2311SLongpeng */ 543e66f2311SLongpeng memory_region_transaction_begin(); 544e66f2311SLongpeng 545b1f030a0SLaurent Vivier for (i = dev->vq_index; i < dev->vq_index + n; i++) { 546b1f030a0SLaurent Vivier vhost_vdpa_host_notifier_uninit(dev, i); 547b1f030a0SLaurent Vivier } 548e66f2311SLongpeng 549e66f2311SLongpeng memory_region_transaction_commit(); 550b1f030a0SLaurent Vivier } 551b1f030a0SLaurent Vivier 552d0416d48SJason Wang static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev) 553d0416d48SJason Wang { 554dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 555d0416d48SJason Wang int i; 556d0416d48SJason Wang 557dff4426fSEugenio Pérez if (v->shadow_vqs_enabled) { 558dff4426fSEugenio Pérez /* FIXME SVQ is not compatible with host notifiers mr */ 559dff4426fSEugenio Pérez return; 560dff4426fSEugenio Pérez } 561dff4426fSEugenio Pérez 562e66f2311SLongpeng /* 563e66f2311SLongpeng * Pack all the changes to the memory regions in a single 564e66f2311SLongpeng * transaction to avoid a few updating of the address space 565e66f2311SLongpeng * topology. 566e66f2311SLongpeng */ 567e66f2311SLongpeng memory_region_transaction_begin(); 568e66f2311SLongpeng 569d0416d48SJason Wang for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) { 570d0416d48SJason Wang if (vhost_vdpa_host_notifier_init(dev, i)) { 571b1f030a0SLaurent Vivier vhost_vdpa_host_notifiers_uninit(dev, i - dev->vq_index); 572e66f2311SLongpeng break; 573e66f2311SLongpeng } 574e66f2311SLongpeng } 575e66f2311SLongpeng 576e66f2311SLongpeng memory_region_transaction_commit(); 577d0416d48SJason Wang } 578d0416d48SJason Wang 579dff4426fSEugenio Pérez static void vhost_vdpa_svq_cleanup(struct vhost_dev *dev) 580dff4426fSEugenio Pérez { 581dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 582dff4426fSEugenio Pérez size_t idx; 583dff4426fSEugenio Pérez 584dff4426fSEugenio Pérez for (idx = 0; idx < v->shadow_vqs->len; ++idx) { 585dff4426fSEugenio Pérez vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, idx)); 586dff4426fSEugenio Pérez } 587dff4426fSEugenio Pérez g_ptr_array_free(v->shadow_vqs, true); 588dff4426fSEugenio Pérez } 589dff4426fSEugenio Pérez 590108a6481SCindy Lu static int vhost_vdpa_cleanup(struct vhost_dev *dev) 591108a6481SCindy Lu { 592108a6481SCindy Lu struct vhost_vdpa *v; 593108a6481SCindy Lu assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 594108a6481SCindy Lu v = dev->opaque; 595778e67deSLaurent Vivier trace_vhost_vdpa_cleanup(dev, v); 596a230c471SEugenio Pérez if (vhost_vdpa_first_dev(dev)) { 597a230c471SEugenio Pérez ram_block_discard_disable(false); 598a230c471SEugenio Pérez } 599a230c471SEugenio Pérez 600d0416d48SJason Wang vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); 601108a6481SCindy Lu memory_listener_unregister(&v->listener); 602dff4426fSEugenio Pérez vhost_vdpa_svq_cleanup(dev); 603108a6481SCindy Lu 604108a6481SCindy Lu dev->opaque = NULL; 605e1c1915bSDavid Hildenbrand 606108a6481SCindy Lu return 0; 607108a6481SCindy Lu } 608108a6481SCindy Lu 609108a6481SCindy Lu static int vhost_vdpa_memslots_limit(struct vhost_dev *dev) 610108a6481SCindy Lu { 611778e67deSLaurent Vivier trace_vhost_vdpa_memslots_limit(dev, INT_MAX); 612108a6481SCindy Lu return INT_MAX; 613108a6481SCindy Lu } 614108a6481SCindy Lu 615108a6481SCindy Lu static int vhost_vdpa_set_mem_table(struct vhost_dev *dev, 616108a6481SCindy Lu struct vhost_memory *mem) 617108a6481SCindy Lu { 618d71b0609SSi-Wei Liu if (!vhost_vdpa_first_dev(dev)) { 6194d191cfdSJason Wang return 0; 6204d191cfdSJason Wang } 6214d191cfdSJason Wang 622778e67deSLaurent Vivier trace_vhost_vdpa_set_mem_table(dev, mem->nregions, mem->padding); 623778e67deSLaurent Vivier if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE) && 624778e67deSLaurent Vivier trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS)) { 625778e67deSLaurent Vivier int i; 626778e67deSLaurent Vivier for (i = 0; i < mem->nregions; i++) { 627778e67deSLaurent Vivier trace_vhost_vdpa_dump_regions(dev, i, 628778e67deSLaurent Vivier mem->regions[i].guest_phys_addr, 629778e67deSLaurent Vivier mem->regions[i].memory_size, 630778e67deSLaurent Vivier mem->regions[i].userspace_addr, 631778e67deSLaurent Vivier mem->regions[i].flags_padding); 632778e67deSLaurent Vivier } 633778e67deSLaurent Vivier } 634108a6481SCindy Lu if (mem->padding) { 6353631151bSRoman Kagan return -EINVAL; 636108a6481SCindy Lu } 637108a6481SCindy Lu 638108a6481SCindy Lu return 0; 639108a6481SCindy Lu } 640108a6481SCindy Lu 641108a6481SCindy Lu static int vhost_vdpa_set_features(struct vhost_dev *dev, 642108a6481SCindy Lu uint64_t features) 643108a6481SCindy Lu { 64412a195faSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 645108a6481SCindy Lu int ret; 6464d191cfdSJason Wang 647d71b0609SSi-Wei Liu if (!vhost_vdpa_first_dev(dev)) { 6484d191cfdSJason Wang return 0; 6494d191cfdSJason Wang } 6504d191cfdSJason Wang 65112a195faSEugenio Pérez if (v->shadow_vqs_enabled) { 65212a195faSEugenio Pérez if ((v->acked_features ^ features) == BIT_ULL(VHOST_F_LOG_ALL)) { 65312a195faSEugenio Pérez /* 65412a195faSEugenio Pérez * QEMU is just trying to enable or disable logging. SVQ handles 65512a195faSEugenio Pérez * this sepparately, so no need to forward this. 65612a195faSEugenio Pérez */ 65712a195faSEugenio Pérez v->acked_features = features; 65812a195faSEugenio Pérez return 0; 65912a195faSEugenio Pérez } 66012a195faSEugenio Pérez 66112a195faSEugenio Pérez v->acked_features = features; 66212a195faSEugenio Pérez 66312a195faSEugenio Pérez /* We must not ack _F_LOG if SVQ is enabled */ 66412a195faSEugenio Pérez features &= ~BIT_ULL(VHOST_F_LOG_ALL); 66512a195faSEugenio Pérez } 66612a195faSEugenio Pérez 667778e67deSLaurent Vivier trace_vhost_vdpa_set_features(dev, features); 668108a6481SCindy Lu ret = vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features); 669108a6481SCindy Lu if (ret) { 670108a6481SCindy Lu return ret; 671108a6481SCindy Lu } 672108a6481SCindy Lu 6733631151bSRoman Kagan return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK); 674108a6481SCindy Lu } 675108a6481SCindy Lu 676a5bd0580SJason Wang static int vhost_vdpa_set_backend_cap(struct vhost_dev *dev) 677a5bd0580SJason Wang { 678a5bd0580SJason Wang uint64_t features; 679a5bd0580SJason Wang uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | 680c1a10086SEugenio Pérez 0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH | 681d83b4945SEugenio Pérez 0x1ULL << VHOST_BACKEND_F_IOTLB_ASID | 682d83b4945SEugenio Pérez 0x1ULL << VHOST_BACKEND_F_SUSPEND; 683a5bd0580SJason Wang int r; 684a5bd0580SJason Wang 685a5bd0580SJason Wang if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) { 6862a83e97eSJason Wang return -EFAULT; 687a5bd0580SJason Wang } 688a5bd0580SJason Wang 689a5bd0580SJason Wang features &= f; 6904d191cfdSJason Wang 691d71b0609SSi-Wei Liu if (vhost_vdpa_first_dev(dev)) { 692a5bd0580SJason Wang r = vhost_vdpa_call(dev, VHOST_SET_BACKEND_FEATURES, &features); 693a5bd0580SJason Wang if (r) { 6942a83e97eSJason Wang return -EFAULT; 695a5bd0580SJason Wang } 6964d191cfdSJason Wang } 697a5bd0580SJason Wang 698a5bd0580SJason Wang dev->backend_cap = features; 699a5bd0580SJason Wang 700a5bd0580SJason Wang return 0; 701a5bd0580SJason Wang } 702a5bd0580SJason Wang 703c232b8f4SZenghui Yu static int vhost_vdpa_get_device_id(struct vhost_dev *dev, 704108a6481SCindy Lu uint32_t *device_id) 705108a6481SCindy Lu { 706778e67deSLaurent Vivier int ret; 707778e67deSLaurent Vivier ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_DEVICE_ID, device_id); 708778e67deSLaurent Vivier trace_vhost_vdpa_get_device_id(dev, *device_id); 709778e67deSLaurent Vivier return ret; 710108a6481SCindy Lu } 711108a6481SCindy Lu 712108a6481SCindy Lu static int vhost_vdpa_reset_device(struct vhost_dev *dev) 713108a6481SCindy Lu { 7140bb302a9SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 715778e67deSLaurent Vivier int ret; 716108a6481SCindy Lu uint8_t status = 0; 717108a6481SCindy Lu 718778e67deSLaurent Vivier ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status); 719778e67deSLaurent Vivier trace_vhost_vdpa_reset_device(dev, status); 7200bb302a9SEugenio Pérez v->suspended = false; 721778e67deSLaurent Vivier return ret; 722108a6481SCindy Lu } 723108a6481SCindy Lu 724108a6481SCindy Lu static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx) 725108a6481SCindy Lu { 726108a6481SCindy Lu assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); 727108a6481SCindy Lu 728353244d8SJason Wang trace_vhost_vdpa_get_vq_index(dev, idx, idx); 729353244d8SJason Wang return idx; 730108a6481SCindy Lu } 731108a6481SCindy Lu 732108a6481SCindy Lu static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev) 733108a6481SCindy Lu { 734108a6481SCindy Lu int i; 735778e67deSLaurent Vivier trace_vhost_vdpa_set_vring_ready(dev); 736108a6481SCindy Lu for (i = 0; i < dev->nvqs; ++i) { 737108a6481SCindy Lu struct vhost_vring_state state = { 738108a6481SCindy Lu .index = dev->vq_index + i, 739108a6481SCindy Lu .num = 1, 740108a6481SCindy Lu }; 741108a6481SCindy Lu vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); 742108a6481SCindy Lu } 743108a6481SCindy Lu return 0; 744108a6481SCindy Lu } 745108a6481SCindy Lu 746259f3accSCindy Lu static int vhost_vdpa_set_config_call(struct vhost_dev *dev, 747259f3accSCindy Lu int fd) 748259f3accSCindy Lu { 749259f3accSCindy Lu trace_vhost_vdpa_set_config_call(dev, fd); 750259f3accSCindy Lu return vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG_CALL, &fd); 751259f3accSCindy Lu } 752259f3accSCindy Lu 753778e67deSLaurent Vivier static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config, 754778e67deSLaurent Vivier uint32_t config_len) 755778e67deSLaurent Vivier { 756778e67deSLaurent Vivier int b, len; 757778e67deSLaurent Vivier char line[QEMU_HEXDUMP_LINE_LEN]; 758778e67deSLaurent Vivier 759778e67deSLaurent Vivier for (b = 0; b < config_len; b += 16) { 760778e67deSLaurent Vivier len = config_len - b; 761778e67deSLaurent Vivier qemu_hexdump_line(line, b, config, len, false); 762778e67deSLaurent Vivier trace_vhost_vdpa_dump_config(dev, line); 763778e67deSLaurent Vivier } 764778e67deSLaurent Vivier } 765778e67deSLaurent Vivier 766108a6481SCindy Lu static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data, 767108a6481SCindy Lu uint32_t offset, uint32_t size, 768108a6481SCindy Lu uint32_t flags) 769108a6481SCindy Lu { 770108a6481SCindy Lu struct vhost_vdpa_config *config; 771108a6481SCindy Lu int ret; 772108a6481SCindy Lu unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 773986d4f78SLi Qiang 774778e67deSLaurent Vivier trace_vhost_vdpa_set_config(dev, offset, size, flags); 775108a6481SCindy Lu config = g_malloc(size + config_size); 776108a6481SCindy Lu config->off = offset; 777108a6481SCindy Lu config->len = size; 778108a6481SCindy Lu memcpy(config->buf, data, size); 779778e67deSLaurent Vivier if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG) && 780778e67deSLaurent Vivier trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) { 781778e67deSLaurent Vivier vhost_vdpa_dump_config(dev, data, size); 782778e67deSLaurent Vivier } 783108a6481SCindy Lu ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG, config); 784108a6481SCindy Lu g_free(config); 785108a6481SCindy Lu return ret; 786108a6481SCindy Lu } 787108a6481SCindy Lu 788108a6481SCindy Lu static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config, 78950de5138SKevin Wolf uint32_t config_len, Error **errp) 790108a6481SCindy Lu { 791108a6481SCindy Lu struct vhost_vdpa_config *v_config; 792108a6481SCindy Lu unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 793108a6481SCindy Lu int ret; 794108a6481SCindy Lu 795778e67deSLaurent Vivier trace_vhost_vdpa_get_config(dev, config, config_len); 796108a6481SCindy Lu v_config = g_malloc(config_len + config_size); 797108a6481SCindy Lu v_config->len = config_len; 798108a6481SCindy Lu v_config->off = 0; 799108a6481SCindy Lu ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config); 800108a6481SCindy Lu memcpy(config, v_config->buf, config_len); 801108a6481SCindy Lu g_free(v_config); 802778e67deSLaurent Vivier if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG) && 803778e67deSLaurent Vivier trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) { 804778e67deSLaurent Vivier vhost_vdpa_dump_config(dev, config, config_len); 805778e67deSLaurent Vivier } 806108a6481SCindy Lu return ret; 807108a6481SCindy Lu } 808108a6481SCindy Lu 809d96be4c8SEugenio Pérez static int vhost_vdpa_set_dev_vring_base(struct vhost_dev *dev, 810d96be4c8SEugenio Pérez struct vhost_vring_state *ring) 811d96be4c8SEugenio Pérez { 812d96be4c8SEugenio Pérez trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num); 813d96be4c8SEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring); 814d96be4c8SEugenio Pérez } 815d96be4c8SEugenio Pérez 816dff4426fSEugenio Pérez static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev, 817dff4426fSEugenio Pérez struct vhost_vring_file *file) 818dff4426fSEugenio Pérez { 819dff4426fSEugenio Pérez trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd); 820dff4426fSEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file); 821dff4426fSEugenio Pérez } 822dff4426fSEugenio Pérez 823a8ac8858SEugenio Pérez static int vhost_vdpa_set_vring_dev_call(struct vhost_dev *dev, 824a8ac8858SEugenio Pérez struct vhost_vring_file *file) 825a8ac8858SEugenio Pérez { 826a8ac8858SEugenio Pérez trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd); 827a8ac8858SEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file); 828a8ac8858SEugenio Pérez } 829a8ac8858SEugenio Pérez 830d96be4c8SEugenio Pérez static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev *dev, 831d96be4c8SEugenio Pérez struct vhost_vring_addr *addr) 832d96be4c8SEugenio Pérez { 833d96be4c8SEugenio Pérez trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags, 834d96be4c8SEugenio Pérez addr->desc_user_addr, addr->used_user_addr, 835d96be4c8SEugenio Pérez addr->avail_user_addr, 836d96be4c8SEugenio Pérez addr->log_guest_addr); 837d96be4c8SEugenio Pérez 838d96be4c8SEugenio Pérez return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr); 839d96be4c8SEugenio Pérez 840d96be4c8SEugenio Pérez } 841d96be4c8SEugenio Pérez 842dff4426fSEugenio Pérez /** 843dff4426fSEugenio Pérez * Set the shadow virtqueue descriptors to the device 844dff4426fSEugenio Pérez * 845dff4426fSEugenio Pérez * @dev: The vhost device model 846dff4426fSEugenio Pérez * @svq: The shadow virtqueue 847dff4426fSEugenio Pérez * @idx: The index of the virtqueue in the vhost device 848dff4426fSEugenio Pérez * @errp: Error 849a8ac8858SEugenio Pérez * 850a8ac8858SEugenio Pérez * Note that this function does not rewind kick file descriptor if cannot set 851a8ac8858SEugenio Pérez * call one. 852dff4426fSEugenio Pérez */ 853100890f7SEugenio Pérez static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev, 854dff4426fSEugenio Pérez VhostShadowVirtqueue *svq, unsigned idx, 855dff4426fSEugenio Pérez Error **errp) 856dff4426fSEugenio Pérez { 857dff4426fSEugenio Pérez struct vhost_vring_file file = { 858dff4426fSEugenio Pérez .index = dev->vq_index + idx, 859dff4426fSEugenio Pérez }; 860dff4426fSEugenio Pérez const EventNotifier *event_notifier = &svq->hdev_kick; 861dff4426fSEugenio Pérez int r; 862dff4426fSEugenio Pérez 8633cfb4d06SEugenio Pérez r = event_notifier_init(&svq->hdev_kick, 0); 8643cfb4d06SEugenio Pérez if (r != 0) { 8653cfb4d06SEugenio Pérez error_setg_errno(errp, -r, "Couldn't create kick event notifier"); 8663cfb4d06SEugenio Pérez goto err_init_hdev_kick; 8673cfb4d06SEugenio Pérez } 8683cfb4d06SEugenio Pérez 8693cfb4d06SEugenio Pérez r = event_notifier_init(&svq->hdev_call, 0); 8703cfb4d06SEugenio Pérez if (r != 0) { 8713cfb4d06SEugenio Pérez error_setg_errno(errp, -r, "Couldn't create call event notifier"); 8723cfb4d06SEugenio Pérez goto err_init_hdev_call; 8733cfb4d06SEugenio Pérez } 8743cfb4d06SEugenio Pérez 875dff4426fSEugenio Pérez file.fd = event_notifier_get_fd(event_notifier); 876dff4426fSEugenio Pérez r = vhost_vdpa_set_vring_dev_kick(dev, &file); 877dff4426fSEugenio Pérez if (unlikely(r != 0)) { 878dff4426fSEugenio Pérez error_setg_errno(errp, -r, "Can't set device kick fd"); 8793cfb4d06SEugenio Pérez goto err_init_set_dev_fd; 880a8ac8858SEugenio Pérez } 881a8ac8858SEugenio Pérez 882a8ac8858SEugenio Pérez event_notifier = &svq->hdev_call; 883a8ac8858SEugenio Pérez file.fd = event_notifier_get_fd(event_notifier); 884a8ac8858SEugenio Pérez r = vhost_vdpa_set_vring_dev_call(dev, &file); 885a8ac8858SEugenio Pérez if (unlikely(r != 0)) { 886a8ac8858SEugenio Pérez error_setg_errno(errp, -r, "Can't set device call fd"); 8873cfb4d06SEugenio Pérez goto err_init_set_dev_fd; 888dff4426fSEugenio Pérez } 889dff4426fSEugenio Pérez 8903cfb4d06SEugenio Pérez return 0; 8913cfb4d06SEugenio Pérez 8923cfb4d06SEugenio Pérez err_init_set_dev_fd: 8933cfb4d06SEugenio Pérez event_notifier_set_handler(&svq->hdev_call, NULL); 8943cfb4d06SEugenio Pérez 8953cfb4d06SEugenio Pérez err_init_hdev_call: 8963cfb4d06SEugenio Pérez event_notifier_cleanup(&svq->hdev_kick); 8973cfb4d06SEugenio Pérez 8983cfb4d06SEugenio Pérez err_init_hdev_kick: 899100890f7SEugenio Pérez return r; 900100890f7SEugenio Pérez } 901100890f7SEugenio Pérez 902100890f7SEugenio Pérez /** 903100890f7SEugenio Pérez * Unmap a SVQ area in the device 904100890f7SEugenio Pérez */ 9058b6d6119SEugenio Pérez static void vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr addr) 906100890f7SEugenio Pérez { 9078b6d6119SEugenio Pérez const DMAMap needle = { 9088b6d6119SEugenio Pérez .translated_addr = addr, 9098b6d6119SEugenio Pérez }; 9108b6d6119SEugenio Pérez const DMAMap *result = vhost_iova_tree_find_iova(v->iova_tree, &needle); 91134e3c94eSEugenio Pérez hwaddr size; 912100890f7SEugenio Pérez int r; 913100890f7SEugenio Pérez 91434e3c94eSEugenio Pérez if (unlikely(!result)) { 91534e3c94eSEugenio Pérez error_report("Unable to find SVQ address to unmap"); 9165b590f51SEugenio Pérez return; 91734e3c94eSEugenio Pérez } 91834e3c94eSEugenio Pérez 9198e3b0cbbSMarc-André Lureau size = ROUND_UP(result->size, qemu_real_host_page_size()); 920cd831ed5SEugenio Pérez r = vhost_vdpa_dma_unmap(v, v->address_space_id, result->iova, size); 921b37c12beSEugenio Pérez if (unlikely(r < 0)) { 922b37c12beSEugenio Pérez error_report("Unable to unmap SVQ vring: %s (%d)", g_strerror(-r), -r); 9235b590f51SEugenio Pérez return; 924b37c12beSEugenio Pérez } 925b37c12beSEugenio Pérez 926b37c12beSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, *result); 927100890f7SEugenio Pérez } 928100890f7SEugenio Pérez 9295b590f51SEugenio Pérez static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev, 930100890f7SEugenio Pérez const VhostShadowVirtqueue *svq) 931100890f7SEugenio Pérez { 932100890f7SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 933100890f7SEugenio Pérez struct vhost_vring_addr svq_addr; 934100890f7SEugenio Pérez 935100890f7SEugenio Pérez vhost_svq_get_vring_addr(svq, &svq_addr); 936100890f7SEugenio Pérez 9378b6d6119SEugenio Pérez vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr); 938100890f7SEugenio Pérez 9398b6d6119SEugenio Pérez vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr); 94034e3c94eSEugenio Pérez } 94134e3c94eSEugenio Pérez 94234e3c94eSEugenio Pérez /** 94334e3c94eSEugenio Pérez * Map the SVQ area in the device 94434e3c94eSEugenio Pérez * 94534e3c94eSEugenio Pérez * @v: Vhost-vdpa device 94634e3c94eSEugenio Pérez * @needle: The area to search iova 94734e3c94eSEugenio Pérez * @errorp: Error pointer 94834e3c94eSEugenio Pérez */ 94934e3c94eSEugenio Pérez static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle, 95034e3c94eSEugenio Pérez Error **errp) 95134e3c94eSEugenio Pérez { 95234e3c94eSEugenio Pérez int r; 95334e3c94eSEugenio Pérez 95434e3c94eSEugenio Pérez r = vhost_iova_tree_map_alloc(v->iova_tree, needle); 95534e3c94eSEugenio Pérez if (unlikely(r != IOVA_OK)) { 95634e3c94eSEugenio Pérez error_setg(errp, "Cannot allocate iova (%d)", r); 95734e3c94eSEugenio Pérez return false; 95834e3c94eSEugenio Pérez } 95934e3c94eSEugenio Pérez 960cd831ed5SEugenio Pérez r = vhost_vdpa_dma_map(v, v->address_space_id, needle->iova, 961cd831ed5SEugenio Pérez needle->size + 1, 96234e3c94eSEugenio Pérez (void *)(uintptr_t)needle->translated_addr, 96334e3c94eSEugenio Pérez needle->perm == IOMMU_RO); 96434e3c94eSEugenio Pérez if (unlikely(r != 0)) { 96534e3c94eSEugenio Pérez error_setg_errno(errp, -r, "Cannot map region to device"); 96669292a8eSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, *needle); 96734e3c94eSEugenio Pérez } 96834e3c94eSEugenio Pérez 96934e3c94eSEugenio Pérez return r == 0; 970100890f7SEugenio Pérez } 971100890f7SEugenio Pérez 972100890f7SEugenio Pérez /** 973100890f7SEugenio Pérez * Map the shadow virtqueue rings in the device 974100890f7SEugenio Pérez * 975100890f7SEugenio Pérez * @dev: The vhost device 976100890f7SEugenio Pérez * @svq: The shadow virtqueue 977100890f7SEugenio Pérez * @addr: Assigned IOVA addresses 978100890f7SEugenio Pérez * @errp: Error pointer 979100890f7SEugenio Pérez */ 980100890f7SEugenio Pérez static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, 981100890f7SEugenio Pérez const VhostShadowVirtqueue *svq, 982100890f7SEugenio Pérez struct vhost_vring_addr *addr, 983100890f7SEugenio Pérez Error **errp) 984100890f7SEugenio Pérez { 98505e385d2SMarkus Armbruster ERRP_GUARD(); 98634e3c94eSEugenio Pérez DMAMap device_region, driver_region; 98734e3c94eSEugenio Pérez struct vhost_vring_addr svq_addr; 988100890f7SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 989100890f7SEugenio Pérez size_t device_size = vhost_svq_device_area_size(svq); 990100890f7SEugenio Pérez size_t driver_size = vhost_svq_driver_area_size(svq); 99134e3c94eSEugenio Pérez size_t avail_offset; 99234e3c94eSEugenio Pérez bool ok; 993100890f7SEugenio Pérez 99434e3c94eSEugenio Pérez vhost_svq_get_vring_addr(svq, &svq_addr); 995100890f7SEugenio Pérez 99634e3c94eSEugenio Pérez driver_region = (DMAMap) { 99734e3c94eSEugenio Pérez .translated_addr = svq_addr.desc_user_addr, 99834e3c94eSEugenio Pérez .size = driver_size - 1, 99934e3c94eSEugenio Pérez .perm = IOMMU_RO, 100034e3c94eSEugenio Pérez }; 100134e3c94eSEugenio Pérez ok = vhost_vdpa_svq_map_ring(v, &driver_region, errp); 100234e3c94eSEugenio Pérez if (unlikely(!ok)) { 100334e3c94eSEugenio Pérez error_prepend(errp, "Cannot create vq driver region: "); 1004100890f7SEugenio Pérez return false; 1005100890f7SEugenio Pérez } 100634e3c94eSEugenio Pérez addr->desc_user_addr = driver_region.iova; 100734e3c94eSEugenio Pérez avail_offset = svq_addr.avail_user_addr - svq_addr.desc_user_addr; 100834e3c94eSEugenio Pérez addr->avail_user_addr = driver_region.iova + avail_offset; 1009100890f7SEugenio Pérez 101034e3c94eSEugenio Pérez device_region = (DMAMap) { 101134e3c94eSEugenio Pérez .translated_addr = svq_addr.used_user_addr, 101234e3c94eSEugenio Pérez .size = device_size - 1, 101334e3c94eSEugenio Pérez .perm = IOMMU_RW, 101434e3c94eSEugenio Pérez }; 101534e3c94eSEugenio Pérez ok = vhost_vdpa_svq_map_ring(v, &device_region, errp); 101634e3c94eSEugenio Pérez if (unlikely(!ok)) { 101734e3c94eSEugenio Pérez error_prepend(errp, "Cannot create vq device region: "); 10188b6d6119SEugenio Pérez vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr); 1019100890f7SEugenio Pérez } 102034e3c94eSEugenio Pérez addr->used_user_addr = device_region.iova; 1021100890f7SEugenio Pérez 102234e3c94eSEugenio Pérez return ok; 1023100890f7SEugenio Pérez } 1024100890f7SEugenio Pérez 1025100890f7SEugenio Pérez static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, 1026100890f7SEugenio Pérez VhostShadowVirtqueue *svq, unsigned idx, 1027100890f7SEugenio Pérez Error **errp) 1028100890f7SEugenio Pérez { 1029100890f7SEugenio Pérez uint16_t vq_index = dev->vq_index + idx; 1030100890f7SEugenio Pérez struct vhost_vring_state s = { 1031100890f7SEugenio Pérez .index = vq_index, 1032100890f7SEugenio Pérez }; 1033100890f7SEugenio Pérez int r; 1034100890f7SEugenio Pérez 1035100890f7SEugenio Pérez r = vhost_vdpa_set_dev_vring_base(dev, &s); 1036100890f7SEugenio Pérez if (unlikely(r)) { 1037100890f7SEugenio Pérez error_setg_errno(errp, -r, "Cannot set vring base"); 1038100890f7SEugenio Pérez return false; 1039100890f7SEugenio Pérez } 1040100890f7SEugenio Pérez 1041100890f7SEugenio Pérez r = vhost_vdpa_svq_set_fds(dev, svq, idx, errp); 1042dff4426fSEugenio Pérez return r == 0; 1043dff4426fSEugenio Pérez } 1044dff4426fSEugenio Pérez 1045dff4426fSEugenio Pérez static bool vhost_vdpa_svqs_start(struct vhost_dev *dev) 1046dff4426fSEugenio Pérez { 1047dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1048dff4426fSEugenio Pérez Error *err = NULL; 1049dff4426fSEugenio Pérez unsigned i; 1050dff4426fSEugenio Pérez 1051712c1a31SEugenio Pérez if (!v->shadow_vqs_enabled) { 1052dff4426fSEugenio Pérez return true; 1053dff4426fSEugenio Pérez } 1054dff4426fSEugenio Pérez 1055dff4426fSEugenio Pérez for (i = 0; i < v->shadow_vqs->len; ++i) { 1056100890f7SEugenio Pérez VirtQueue *vq = virtio_get_queue(dev->vdev, dev->vq_index + i); 1057dff4426fSEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i); 1058100890f7SEugenio Pérez struct vhost_vring_addr addr = { 10591c82fdfeSEugenio Pérez .index = dev->vq_index + i, 1060100890f7SEugenio Pérez }; 1061100890f7SEugenio Pérez int r; 1062dff4426fSEugenio Pérez bool ok = vhost_vdpa_svq_setup(dev, svq, i, &err); 1063dff4426fSEugenio Pérez if (unlikely(!ok)) { 1064100890f7SEugenio Pérez goto err; 1065100890f7SEugenio Pérez } 1066100890f7SEugenio Pérez 10675fde952bSEugenio Pérez vhost_svq_start(svq, dev->vdev, vq, v->iova_tree); 1068100890f7SEugenio Pérez ok = vhost_vdpa_svq_map_rings(dev, svq, &addr, &err); 1069100890f7SEugenio Pérez if (unlikely(!ok)) { 1070100890f7SEugenio Pérez goto err_map; 1071100890f7SEugenio Pérez } 1072100890f7SEugenio Pérez 1073100890f7SEugenio Pérez /* Override vring GPA set by vhost subsystem */ 1074100890f7SEugenio Pérez r = vhost_vdpa_set_vring_dev_addr(dev, &addr); 1075100890f7SEugenio Pérez if (unlikely(r != 0)) { 1076100890f7SEugenio Pérez error_setg_errno(&err, -r, "Cannot set device address"); 1077100890f7SEugenio Pérez goto err_set_addr; 1078100890f7SEugenio Pérez } 1079100890f7SEugenio Pérez } 1080100890f7SEugenio Pérez 1081100890f7SEugenio Pérez return true; 1082100890f7SEugenio Pérez 1083100890f7SEugenio Pérez err_set_addr: 1084100890f7SEugenio Pérez vhost_vdpa_svq_unmap_rings(dev, g_ptr_array_index(v->shadow_vqs, i)); 1085100890f7SEugenio Pérez 1086100890f7SEugenio Pérez err_map: 1087100890f7SEugenio Pérez vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, i)); 1088100890f7SEugenio Pérez 1089100890f7SEugenio Pérez err: 1090dff4426fSEugenio Pérez error_reportf_err(err, "Cannot setup SVQ %u: ", i); 1091100890f7SEugenio Pérez for (unsigned j = 0; j < i; ++j) { 1092100890f7SEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, j); 1093100890f7SEugenio Pérez vhost_vdpa_svq_unmap_rings(dev, svq); 1094100890f7SEugenio Pérez vhost_svq_stop(svq); 1095100890f7SEugenio Pérez } 1096100890f7SEugenio Pérez 1097100890f7SEugenio Pérez return false; 1098100890f7SEugenio Pérez } 1099100890f7SEugenio Pérez 11005b590f51SEugenio Pérez static void vhost_vdpa_svqs_stop(struct vhost_dev *dev) 1101100890f7SEugenio Pérez { 1102100890f7SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1103100890f7SEugenio Pérez 1104712c1a31SEugenio Pérez if (!v->shadow_vqs_enabled) { 11055b590f51SEugenio Pérez return; 1106100890f7SEugenio Pérez } 1107100890f7SEugenio Pérez 1108100890f7SEugenio Pérez for (unsigned i = 0; i < v->shadow_vqs->len; ++i) { 1109100890f7SEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i); 11102e1a9de9SEugenio Pérez 11112e1a9de9SEugenio Pérez vhost_svq_stop(svq); 11125b590f51SEugenio Pérez vhost_vdpa_svq_unmap_rings(dev, svq); 11133cfb4d06SEugenio Pérez 11143cfb4d06SEugenio Pérez event_notifier_cleanup(&svq->hdev_kick); 11153cfb4d06SEugenio Pérez event_notifier_cleanup(&svq->hdev_call); 1116dff4426fSEugenio Pérez } 1117dff4426fSEugenio Pérez } 1118dff4426fSEugenio Pérez 11190bb302a9SEugenio Pérez static void vhost_vdpa_suspend(struct vhost_dev *dev) 11200bb302a9SEugenio Pérez { 11210bb302a9SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 11220bb302a9SEugenio Pérez int r; 11230bb302a9SEugenio Pérez 11240bb302a9SEugenio Pérez if (!vhost_vdpa_first_dev(dev)) { 11250bb302a9SEugenio Pérez return; 11260bb302a9SEugenio Pérez } 11270bb302a9SEugenio Pérez 11280bb302a9SEugenio Pérez if (dev->backend_cap & BIT_ULL(VHOST_BACKEND_F_SUSPEND)) { 11290bb302a9SEugenio Pérez trace_vhost_vdpa_suspend(dev); 11300bb302a9SEugenio Pérez r = ioctl(v->device_fd, VHOST_VDPA_SUSPEND); 11310bb302a9SEugenio Pérez if (unlikely(r)) { 11320bb302a9SEugenio Pérez error_report("Cannot suspend: %s(%d)", g_strerror(errno), errno); 11330bb302a9SEugenio Pérez } else { 11340bb302a9SEugenio Pérez v->suspended = true; 11350bb302a9SEugenio Pérez return; 11360bb302a9SEugenio Pérez } 11370bb302a9SEugenio Pérez } 11380bb302a9SEugenio Pérez 11390bb302a9SEugenio Pérez vhost_vdpa_reset_device(dev); 11400bb302a9SEugenio Pérez } 11410bb302a9SEugenio Pérez 1142108a6481SCindy Lu static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) 1143108a6481SCindy Lu { 1144108a6481SCindy Lu struct vhost_vdpa *v = dev->opaque; 1145dff4426fSEugenio Pérez bool ok; 1146778e67deSLaurent Vivier trace_vhost_vdpa_dev_start(dev, started); 11474d191cfdSJason Wang 11484d191cfdSJason Wang if (started) { 11494d191cfdSJason Wang vhost_vdpa_host_notifiers_init(dev); 1150dff4426fSEugenio Pérez ok = vhost_vdpa_svqs_start(dev); 1151dff4426fSEugenio Pérez if (unlikely(!ok)) { 1152dff4426fSEugenio Pérez return -1; 1153dff4426fSEugenio Pérez } 11544d191cfdSJason Wang vhost_vdpa_set_vring_ready(dev); 11554d191cfdSJason Wang } else { 11560bb302a9SEugenio Pérez vhost_vdpa_suspend(dev); 11575b590f51SEugenio Pérez vhost_vdpa_svqs_stop(dev); 11584d191cfdSJason Wang vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); 11594d191cfdSJason Wang } 11604d191cfdSJason Wang 1161245cf2c2SEugenio Pérez if (dev->vq_index + dev->nvqs != dev->vq_index_end) { 11624d191cfdSJason Wang return 0; 11634d191cfdSJason Wang } 11644d191cfdSJason Wang 1165108a6481SCindy Lu if (started) { 1166108a6481SCindy Lu memory_listener_register(&v->listener, &address_space_memory); 11673631151bSRoman Kagan return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK); 1168c3716f26SEugenio Pérez } 1169c3716f26SEugenio Pérez 1170c3716f26SEugenio Pérez return 0; 1171c3716f26SEugenio Pérez } 1172c3716f26SEugenio Pérez 1173c3716f26SEugenio Pérez static void vhost_vdpa_reset_status(struct vhost_dev *dev) 1174c3716f26SEugenio Pérez { 1175c3716f26SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1176c3716f26SEugenio Pérez 1177c3716f26SEugenio Pérez if (dev->vq_index + dev->nvqs != dev->vq_index_end) { 1178c3716f26SEugenio Pérez return; 1179c3716f26SEugenio Pérez } 1180c3716f26SEugenio Pérez 1181108a6481SCindy Lu vhost_vdpa_reset_device(dev); 1182108a6481SCindy Lu vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | 1183108a6481SCindy Lu VIRTIO_CONFIG_S_DRIVER); 1184108a6481SCindy Lu memory_listener_unregister(&v->listener); 1185108a6481SCindy Lu } 1186108a6481SCindy Lu 1187108a6481SCindy Lu static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base, 1188108a6481SCindy Lu struct vhost_log *log) 1189108a6481SCindy Lu { 1190773ebc95SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1191d71b0609SSi-Wei Liu if (v->shadow_vqs_enabled || !vhost_vdpa_first_dev(dev)) { 11924d191cfdSJason Wang return 0; 11934d191cfdSJason Wang } 11944d191cfdSJason Wang 1195778e67deSLaurent Vivier trace_vhost_vdpa_set_log_base(dev, base, log->size, log->refcnt, log->fd, 1196778e67deSLaurent Vivier log->log); 1197108a6481SCindy Lu return vhost_vdpa_call(dev, VHOST_SET_LOG_BASE, &base); 1198108a6481SCindy Lu } 1199108a6481SCindy Lu 1200108a6481SCindy Lu static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev, 1201108a6481SCindy Lu struct vhost_vring_addr *addr) 1202108a6481SCindy Lu { 1203d96be4c8SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1204d96be4c8SEugenio Pérez 1205d96be4c8SEugenio Pérez if (v->shadow_vqs_enabled) { 1206d96be4c8SEugenio Pérez /* 1207d96be4c8SEugenio Pérez * Device vring addr was set at device start. SVQ base is handled by 1208d96be4c8SEugenio Pérez * VirtQueue code. 1209d96be4c8SEugenio Pérez */ 1210d96be4c8SEugenio Pérez return 0; 1211d96be4c8SEugenio Pérez } 1212d96be4c8SEugenio Pérez 1213d96be4c8SEugenio Pérez return vhost_vdpa_set_vring_dev_addr(dev, addr); 1214108a6481SCindy Lu } 1215108a6481SCindy Lu 1216108a6481SCindy Lu static int vhost_vdpa_set_vring_num(struct vhost_dev *dev, 1217108a6481SCindy Lu struct vhost_vring_state *ring) 1218108a6481SCindy Lu { 1219778e67deSLaurent Vivier trace_vhost_vdpa_set_vring_num(dev, ring->index, ring->num); 1220108a6481SCindy Lu return vhost_vdpa_call(dev, VHOST_SET_VRING_NUM, ring); 1221108a6481SCindy Lu } 1222108a6481SCindy Lu 1223108a6481SCindy Lu static int vhost_vdpa_set_vring_base(struct vhost_dev *dev, 1224108a6481SCindy Lu struct vhost_vring_state *ring) 1225108a6481SCindy Lu { 1226d96be4c8SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1227d96be4c8SEugenio Pérez 1228d96be4c8SEugenio Pérez if (v->shadow_vqs_enabled) { 1229d96be4c8SEugenio Pérez /* 1230d96be4c8SEugenio Pérez * Device vring base was set at device start. SVQ base is handled by 1231d96be4c8SEugenio Pérez * VirtQueue code. 1232d96be4c8SEugenio Pérez */ 1233d96be4c8SEugenio Pérez return 0; 1234d96be4c8SEugenio Pérez } 1235d96be4c8SEugenio Pérez 1236d96be4c8SEugenio Pérez return vhost_vdpa_set_dev_vring_base(dev, ring); 1237108a6481SCindy Lu } 1238108a6481SCindy Lu 1239108a6481SCindy Lu static int vhost_vdpa_get_vring_base(struct vhost_dev *dev, 1240108a6481SCindy Lu struct vhost_vring_state *ring) 1241108a6481SCindy Lu { 12426d0b2226SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1243778e67deSLaurent Vivier int ret; 1244778e67deSLaurent Vivier 12456d0b2226SEugenio Pérez if (v->shadow_vqs_enabled) { 12462fdac348SEugenio Pérez ring->num = virtio_queue_get_last_avail_idx(dev->vdev, ring->index); 12476d0b2226SEugenio Pérez return 0; 12486d0b2226SEugenio Pérez } 12496d0b2226SEugenio Pérez 1250b6662cb7SEugenio Pérez if (!v->suspended) { 1251b6662cb7SEugenio Pérez /* 1252b6662cb7SEugenio Pérez * Cannot trust in value returned by device, let vhost recover used 1253b6662cb7SEugenio Pérez * idx from guest. 1254b6662cb7SEugenio Pérez */ 1255b6662cb7SEugenio Pérez return -1; 1256b6662cb7SEugenio Pérez } 1257b6662cb7SEugenio Pérez 1258778e67deSLaurent Vivier ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring); 1259778e67deSLaurent Vivier trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num); 1260778e67deSLaurent Vivier return ret; 1261108a6481SCindy Lu } 1262108a6481SCindy Lu 1263108a6481SCindy Lu static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, 1264108a6481SCindy Lu struct vhost_vring_file *file) 1265108a6481SCindy Lu { 1266dff4426fSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1267dff4426fSEugenio Pérez int vdpa_idx = file->index - dev->vq_index; 1268dff4426fSEugenio Pérez 1269dff4426fSEugenio Pérez if (v->shadow_vqs_enabled) { 1270dff4426fSEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx); 1271dff4426fSEugenio Pérez vhost_svq_set_svq_kick_fd(svq, file->fd); 1272dff4426fSEugenio Pérez return 0; 1273dff4426fSEugenio Pérez } else { 1274dff4426fSEugenio Pérez return vhost_vdpa_set_vring_dev_kick(dev, file); 1275dff4426fSEugenio Pérez } 1276108a6481SCindy Lu } 1277108a6481SCindy Lu 1278108a6481SCindy Lu static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, 1279108a6481SCindy Lu struct vhost_vring_file *file) 1280108a6481SCindy Lu { 1281a8ac8858SEugenio Pérez struct vhost_vdpa *v = dev->opaque; 1282a8ac8858SEugenio Pérez int vdpa_idx = file->index - dev->vq_index; 1283a8ac8858SEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx); 1284a8ac8858SEugenio Pérez 1285b2765243SEugenio Pérez /* Remember last call fd because we can switch to SVQ anytime. */ 1286a8ac8858SEugenio Pérez vhost_svq_set_svq_call_fd(svq, file->fd); 1287b2765243SEugenio Pérez if (v->shadow_vqs_enabled) { 1288a8ac8858SEugenio Pérez return 0; 1289a8ac8858SEugenio Pérez } 1290b2765243SEugenio Pérez 1291b2765243SEugenio Pérez return vhost_vdpa_set_vring_dev_call(dev, file); 1292108a6481SCindy Lu } 1293108a6481SCindy Lu 1294108a6481SCindy Lu static int vhost_vdpa_get_features(struct vhost_dev *dev, 1295108a6481SCindy Lu uint64_t *features) 1296108a6481SCindy Lu { 129712a195faSEugenio Pérez struct vhost_vdpa *v = dev->opaque; 129812a195faSEugenio Pérez int ret = vhost_vdpa_get_dev_features(dev, features); 1299778e67deSLaurent Vivier 130012a195faSEugenio Pérez if (ret == 0 && v->shadow_vqs_enabled) { 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