15f94c174SLaurent Vivier /* 25f94c174SLaurent Vivier * KVM coalesced MMIO 35f94c174SLaurent Vivier * 45f94c174SLaurent Vivier * Copyright (c) 2008 Bull S.A.S. 5*221d059dSAvi Kivity * Copyright 2009 Red Hat, Inc. and/or its affiliates. 65f94c174SLaurent Vivier * 75f94c174SLaurent Vivier * Author: Laurent Vivier <Laurent.Vivier@bull.net> 85f94c174SLaurent Vivier * 95f94c174SLaurent Vivier */ 105f94c174SLaurent Vivier 115f94c174SLaurent Vivier #include "iodev.h" 125f94c174SLaurent Vivier 135f94c174SLaurent Vivier #include <linux/kvm_host.h> 145a0e3ad6STejun Heo #include <linux/slab.h> 155f94c174SLaurent Vivier #include <linux/kvm.h> 165f94c174SLaurent Vivier 175f94c174SLaurent Vivier #include "coalesced_mmio.h" 185f94c174SLaurent Vivier 19d76685c4SGregory Haskins static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev) 20d76685c4SGregory Haskins { 21d76685c4SGregory Haskins return container_of(dev, struct kvm_coalesced_mmio_dev, dev); 22d76685c4SGregory Haskins } 23d76685c4SGregory Haskins 24bda9020eSMichael S. Tsirkin static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev, 25bda9020eSMichael S. Tsirkin gpa_t addr, int len) 265f94c174SLaurent Vivier { 275f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *zone; 28105f8d40SAvi Kivity struct kvm_coalesced_mmio_ring *ring; 29105f8d40SAvi Kivity unsigned avail; 305f94c174SLaurent Vivier int i; 315f94c174SLaurent Vivier 325f94c174SLaurent Vivier /* Are we able to batch it ? */ 335f94c174SLaurent Vivier 345f94c174SLaurent Vivier /* last is the first free entry 355f94c174SLaurent Vivier * check if we don't meet the first used entry 365f94c174SLaurent Vivier * there is always one unused entry in the buffer 375f94c174SLaurent Vivier */ 38105f8d40SAvi Kivity ring = dev->kvm->coalesced_mmio_ring; 39105f8d40SAvi Kivity avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX; 4064a2268dSMarcelo Tosatti if (avail < KVM_MAX_VCPUS) { 415f94c174SLaurent Vivier /* full */ 425f94c174SLaurent Vivier return 0; 435f94c174SLaurent Vivier } 445f94c174SLaurent Vivier 455f94c174SLaurent Vivier /* is it in a batchable area ? */ 465f94c174SLaurent Vivier 475f94c174SLaurent Vivier for (i = 0; i < dev->nb_zones; i++) { 485f94c174SLaurent Vivier zone = &dev->zone[i]; 495f94c174SLaurent Vivier 505f94c174SLaurent Vivier /* (addr,len) is fully included in 515f94c174SLaurent Vivier * (zone->addr, zone->size) 525f94c174SLaurent Vivier */ 535f94c174SLaurent Vivier 545f94c174SLaurent Vivier if (zone->addr <= addr && 555f94c174SLaurent Vivier addr + len <= zone->addr + zone->size) 565f94c174SLaurent Vivier return 1; 575f94c174SLaurent Vivier } 585f94c174SLaurent Vivier return 0; 595f94c174SLaurent Vivier } 605f94c174SLaurent Vivier 61bda9020eSMichael S. Tsirkin static int coalesced_mmio_write(struct kvm_io_device *this, 625f94c174SLaurent Vivier gpa_t addr, int len, const void *val) 635f94c174SLaurent Vivier { 64d76685c4SGregory Haskins struct kvm_coalesced_mmio_dev *dev = to_mmio(this); 655f94c174SLaurent Vivier struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; 66bda9020eSMichael S. Tsirkin if (!coalesced_mmio_in_range(dev, addr, len)) 67bda9020eSMichael S. Tsirkin return -EOPNOTSUPP; 685f94c174SLaurent Vivier 6964a2268dSMarcelo Tosatti spin_lock(&dev->lock); 705f94c174SLaurent Vivier 715f94c174SLaurent Vivier /* copy data in first free entry of the ring */ 725f94c174SLaurent Vivier 735f94c174SLaurent Vivier ring->coalesced_mmio[ring->last].phys_addr = addr; 745f94c174SLaurent Vivier ring->coalesced_mmio[ring->last].len = len; 755f94c174SLaurent Vivier memcpy(ring->coalesced_mmio[ring->last].data, val, len); 765f94c174SLaurent Vivier smp_wmb(); 775f94c174SLaurent Vivier ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; 7864a2268dSMarcelo Tosatti spin_unlock(&dev->lock); 79bda9020eSMichael S. Tsirkin return 0; 805f94c174SLaurent Vivier } 815f94c174SLaurent Vivier 825f94c174SLaurent Vivier static void coalesced_mmio_destructor(struct kvm_io_device *this) 835f94c174SLaurent Vivier { 84d76685c4SGregory Haskins struct kvm_coalesced_mmio_dev *dev = to_mmio(this); 85787a660aSGregory Haskins 86787a660aSGregory Haskins kfree(dev); 875f94c174SLaurent Vivier } 885f94c174SLaurent Vivier 89d76685c4SGregory Haskins static const struct kvm_io_device_ops coalesced_mmio_ops = { 90d76685c4SGregory Haskins .write = coalesced_mmio_write, 91d76685c4SGregory Haskins .destructor = coalesced_mmio_destructor, 92d76685c4SGregory Haskins }; 93d76685c4SGregory Haskins 945f94c174SLaurent Vivier int kvm_coalesced_mmio_init(struct kvm *kvm) 955f94c174SLaurent Vivier { 965f94c174SLaurent Vivier struct kvm_coalesced_mmio_dev *dev; 97980da6ceSAvi Kivity struct page *page; 98090b7affSGregory Haskins int ret; 995f94c174SLaurent Vivier 100980da6ceSAvi Kivity ret = -ENOMEM; 101980da6ceSAvi Kivity page = alloc_page(GFP_KERNEL | __GFP_ZERO); 102980da6ceSAvi Kivity if (!page) 103980da6ceSAvi Kivity goto out_err; 104980da6ceSAvi Kivity kvm->coalesced_mmio_ring = page_address(page); 105980da6ceSAvi Kivity 106980da6ceSAvi Kivity ret = -ENOMEM; 1075f94c174SLaurent Vivier dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); 1085f94c174SLaurent Vivier if (!dev) 109980da6ceSAvi Kivity goto out_free_page; 11064a2268dSMarcelo Tosatti spin_lock_init(&dev->lock); 111d76685c4SGregory Haskins kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); 1125f94c174SLaurent Vivier dev->kvm = kvm; 1135f94c174SLaurent Vivier kvm->coalesced_mmio_dev = dev; 1145f94c174SLaurent Vivier 11579fac95eSMarcelo Tosatti mutex_lock(&kvm->slots_lock); 116e93f8a0fSMarcelo Tosatti ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev); 11779fac95eSMarcelo Tosatti mutex_unlock(&kvm->slots_lock); 118090b7affSGregory Haskins if (ret < 0) 119980da6ceSAvi Kivity goto out_free_dev; 120090b7affSGregory Haskins 121090b7affSGregory Haskins return ret; 122980da6ceSAvi Kivity 123980da6ceSAvi Kivity out_free_dev: 1246ce5a090STakuya Yoshikawa kvm->coalesced_mmio_dev = NULL; 125980da6ceSAvi Kivity kfree(dev); 126980da6ceSAvi Kivity out_free_page: 1276ce5a090STakuya Yoshikawa kvm->coalesced_mmio_ring = NULL; 128980da6ceSAvi Kivity __free_page(page); 129980da6ceSAvi Kivity out_err: 130980da6ceSAvi Kivity return ret; 131980da6ceSAvi Kivity } 132980da6ceSAvi Kivity 133980da6ceSAvi Kivity void kvm_coalesced_mmio_free(struct kvm *kvm) 134980da6ceSAvi Kivity { 135980da6ceSAvi Kivity if (kvm->coalesced_mmio_ring) 136980da6ceSAvi Kivity free_page((unsigned long)kvm->coalesced_mmio_ring); 1375f94c174SLaurent Vivier } 1385f94c174SLaurent Vivier 1395f94c174SLaurent Vivier int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, 1405f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *zone) 1415f94c174SLaurent Vivier { 1425f94c174SLaurent Vivier struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; 1435f94c174SLaurent Vivier 1445f94c174SLaurent Vivier if (dev == NULL) 145a87fa355SWei Yongjun return -ENXIO; 1465f94c174SLaurent Vivier 14779fac95eSMarcelo Tosatti mutex_lock(&kvm->slots_lock); 1485f94c174SLaurent Vivier if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) { 14979fac95eSMarcelo Tosatti mutex_unlock(&kvm->slots_lock); 1505f94c174SLaurent Vivier return -ENOBUFS; 1515f94c174SLaurent Vivier } 1525f94c174SLaurent Vivier 1535f94c174SLaurent Vivier dev->zone[dev->nb_zones] = *zone; 1545f94c174SLaurent Vivier dev->nb_zones++; 1555f94c174SLaurent Vivier 15679fac95eSMarcelo Tosatti mutex_unlock(&kvm->slots_lock); 1575f94c174SLaurent Vivier return 0; 1585f94c174SLaurent Vivier } 1595f94c174SLaurent Vivier 1605f94c174SLaurent Vivier int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, 1615f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *zone) 1625f94c174SLaurent Vivier { 1635f94c174SLaurent Vivier int i; 1645f94c174SLaurent Vivier struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev; 1655f94c174SLaurent Vivier struct kvm_coalesced_mmio_zone *z; 1665f94c174SLaurent Vivier 1675f94c174SLaurent Vivier if (dev == NULL) 168a87fa355SWei Yongjun return -ENXIO; 1695f94c174SLaurent Vivier 17079fac95eSMarcelo Tosatti mutex_lock(&kvm->slots_lock); 1715f94c174SLaurent Vivier 1725f94c174SLaurent Vivier i = dev->nb_zones; 1735f94c174SLaurent Vivier while (i) { 1745f94c174SLaurent Vivier z = &dev->zone[i - 1]; 1755f94c174SLaurent Vivier 1765f94c174SLaurent Vivier /* unregister all zones 1775f94c174SLaurent Vivier * included in (zone->addr, zone->size) 1785f94c174SLaurent Vivier */ 1795f94c174SLaurent Vivier 1805f94c174SLaurent Vivier if (zone->addr <= z->addr && 1815f94c174SLaurent Vivier z->addr + z->size <= zone->addr + zone->size) { 1825f94c174SLaurent Vivier dev->nb_zones--; 1835f94c174SLaurent Vivier *z = dev->zone[dev->nb_zones]; 1845f94c174SLaurent Vivier } 1855f94c174SLaurent Vivier i--; 1865f94c174SLaurent Vivier } 1875f94c174SLaurent Vivier 18879fac95eSMarcelo Tosatti mutex_unlock(&kvm->slots_lock); 1895f94c174SLaurent Vivier 1905f94c174SLaurent Vivier return 0; 1915f94c174SLaurent Vivier } 192